msdospart.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* pcpart.c - manipulate fdisk partitions */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <grub/types.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/err.h>
  24. #include <grub/msdos_partition.h>
  25. #include <grub/device.h>
  26. #include <grub/disk.h>
  27. #include <grub/partition.h>
  28. #include <grub/parttool.h>
  29. GRUB_MOD_LICENSE ("GPLv2+");
  30. static int activate_table_handle = -1;
  31. static int type_table_handle = -1;
  32. static struct grub_parttool_argdesc grub_pcpart_bootargs[] =
  33. {
  34. {"boot", "Make partition active", GRUB_PARTTOOL_ARG_BOOL},
  35. {0, 0, 0}
  36. };
  37. static grub_err_t grub_pcpart_boot (const grub_device_t dev,
  38. const struct grub_parttool_args *args)
  39. {
  40. int i, index;
  41. grub_partition_t part;
  42. struct grub_msdos_partition_mbr mbr;
  43. if (dev->disk->partition->offset)
  44. return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a primary partition");
  45. index = dev->disk->partition->index;
  46. part = dev->disk->partition;
  47. dev->disk->partition = part->parent;
  48. /* Read the MBR. */
  49. if (grub_disk_read (dev->disk, 0, 0, sizeof (mbr), &mbr))
  50. {
  51. dev->disk->partition = part;
  52. return grub_errno;
  53. }
  54. if (args[0].set && args[0].bool)
  55. {
  56. for (i = 0; i < 4; i++)
  57. mbr.entries[i].flag = 0x0;
  58. mbr.entries[index].flag = 0x80;
  59. grub_printf ("Partition %d is active now. \n", index);
  60. }
  61. else
  62. {
  63. mbr.entries[index].flag = 0x0;
  64. grub_printf ("Cleared active flag on %d. \n", index);
  65. }
  66. /* Write the MBR. */
  67. grub_disk_write (dev->disk, 0, 0, sizeof (mbr), &mbr);
  68. dev->disk->partition = part;
  69. return GRUB_ERR_NONE;
  70. }
  71. static struct grub_parttool_argdesc grub_pcpart_typeargs[] =
  72. {
  73. {"type", "Change partition type", GRUB_PARTTOOL_ARG_VAL},
  74. {"hidden", "Make partition hidden", GRUB_PARTTOOL_ARG_BOOL},
  75. {0, 0, 0}
  76. };
  77. static grub_err_t grub_pcpart_type (const grub_device_t dev,
  78. const struct grub_parttool_args *args)
  79. {
  80. int index;
  81. grub_uint8_t type;
  82. grub_partition_t part;
  83. struct grub_msdos_partition_mbr mbr;
  84. index = dev->disk->partition->index;
  85. part = dev->disk->partition;
  86. dev->disk->partition = part->parent;
  87. /* Read the parttable. */
  88. if (grub_disk_read (dev->disk, part->offset, 0,
  89. sizeof (mbr), &mbr))
  90. {
  91. dev->disk->partition = part;
  92. return grub_errno;
  93. }
  94. if (args[0].set)
  95. type = grub_strtoul (args[0].str, 0, 0);
  96. else
  97. type = mbr.entries[index].type;
  98. if (args[1].set)
  99. {
  100. if (args[1].bool)
  101. type |= GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG;
  102. else
  103. type &= ~GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG;
  104. }
  105. if (grub_msdos_partition_is_empty (type)
  106. || grub_msdos_partition_is_extended (type))
  107. {
  108. dev->disk->partition = part;
  109. return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid type");
  110. }
  111. mbr.entries[index].type = type;
  112. grub_printf ("Setting partition type to 0x%x\n", type);
  113. /* Write the parttable. */
  114. grub_disk_write (dev->disk, part->offset, 0,
  115. sizeof (mbr), &mbr);
  116. dev->disk->partition = part;
  117. return GRUB_ERR_NONE;
  118. }
  119. GRUB_MOD_INIT (msdospart)
  120. {
  121. activate_table_handle = grub_parttool_register ("msdos",
  122. grub_pcpart_boot,
  123. grub_pcpart_bootargs);
  124. type_table_handle = grub_parttool_register ("msdos",
  125. grub_pcpart_type,
  126. grub_pcpart_typeargs);
  127. }
  128. GRUB_MOD_FINI(msdospart)
  129. {
  130. grub_parttool_unregister (activate_table_handle);
  131. grub_parttool_unregister (type_table_handle);
  132. }