msdospart.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include <grub/i18n.h>
  30. GRUB_MOD_LICENSE ("GPLv2+");
  31. static int activate_table_handle = -1;
  32. static int type_table_handle = -1;
  33. static struct grub_parttool_argdesc grub_pcpart_bootargs[] =
  34. {
  35. {"boot", N_("Make partition active"), GRUB_PARTTOOL_ARG_BOOL},
  36. {0, 0, 0}
  37. };
  38. static grub_err_t grub_pcpart_boot (const grub_device_t dev,
  39. const struct grub_parttool_args *args)
  40. {
  41. int i, index;
  42. grub_partition_t part;
  43. struct grub_msdos_partition_mbr mbr;
  44. if (dev->disk->partition->offset)
  45. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("not a primary partition"));
  46. index = dev->disk->partition->index;
  47. part = dev->disk->partition;
  48. dev->disk->partition = part->parent;
  49. /* Read the MBR. */
  50. if (grub_disk_read (dev->disk, 0, 0, sizeof (mbr), &mbr))
  51. {
  52. dev->disk->partition = part;
  53. return grub_errno;
  54. }
  55. if (args[0].set && args[0].b)
  56. {
  57. for (i = 0; i < 4; i++)
  58. mbr.entries[i].flag = 0x0;
  59. mbr.entries[index].flag = 0x80;
  60. grub_printf_ (N_("Partition %d is active now. \n"), index);
  61. }
  62. else
  63. {
  64. mbr.entries[index].flag = 0x0;
  65. grub_printf (N_("Cleared active flag on %d. \n"), index);
  66. }
  67. /* Write the MBR. */
  68. grub_disk_write (dev->disk, 0, 0, sizeof (mbr), &mbr);
  69. dev->disk->partition = part;
  70. return GRUB_ERR_NONE;
  71. }
  72. static struct grub_parttool_argdesc grub_pcpart_typeargs[] =
  73. {
  74. {"type", N_("Change partition type"), GRUB_PARTTOOL_ARG_VAL},
  75. {"hidden", N_("Set `hidden' flag in partition type"), GRUB_PARTTOOL_ARG_BOOL},
  76. {0, 0, 0}
  77. };
  78. static grub_err_t grub_pcpart_type (const grub_device_t dev,
  79. const struct grub_parttool_args *args)
  80. {
  81. int index;
  82. grub_uint8_t type;
  83. grub_partition_t part;
  84. struct grub_msdos_partition_mbr mbr;
  85. index = dev->disk->partition->index;
  86. part = dev->disk->partition;
  87. dev->disk->partition = part->parent;
  88. /* Read the parttable. */
  89. if (grub_disk_read (dev->disk, part->offset, 0,
  90. sizeof (mbr), &mbr))
  91. {
  92. dev->disk->partition = part;
  93. return grub_errno;
  94. }
  95. if (args[0].set)
  96. type = grub_strtoul (args[0].str, 0, 0);
  97. else
  98. type = mbr.entries[index].type;
  99. if (args[1].set)
  100. {
  101. if (args[1].b)
  102. type |= GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG;
  103. else
  104. type &= ~GRUB_PC_PARTITION_TYPE_HIDDEN_FLAG;
  105. }
  106. if (grub_msdos_partition_is_empty (type)
  107. || grub_msdos_partition_is_extended (type))
  108. {
  109. dev->disk->partition = part;
  110. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  111. N_("the partition type 0x%x isn't valid"), type);
  112. }
  113. mbr.entries[index].type = type;
  114. /* TRANSLATORS: In this case we're actually writing to the disk and actively
  115. modifying partition type rather than just defining it. */
  116. grub_printf_ (N_("Setting partition type to 0x%x\n"), type);
  117. /* Write the parttable. */
  118. grub_disk_write (dev->disk, part->offset, 0,
  119. sizeof (mbr), &mbr);
  120. dev->disk->partition = part;
  121. return GRUB_ERR_NONE;
  122. }
  123. GRUB_MOD_INIT (msdospart)
  124. {
  125. activate_table_handle = grub_parttool_register ("msdos",
  126. grub_pcpart_boot,
  127. grub_pcpart_bootargs);
  128. type_table_handle = grub_parttool_register ("msdos",
  129. grub_pcpart_type,
  130. grub_pcpart_typeargs);
  131. }
  132. GRUB_MOD_FINI(msdospart)
  133. {
  134. grub_parttool_unregister (activate_table_handle);
  135. grub_parttool_unregister (type_table_handle);
  136. }