msdospart.c 4.1 KB

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