gpt.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* gpt.c - Read GUID Partition Tables (GPT). */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2005,2006,2007,2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB 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 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/disk.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/partition.h>
  23. #include <grub/dl.h>
  24. #include <grub/msdos_partition.h>
  25. #include <grub/gpt_partition.h>
  26. static grub_uint8_t grub_gpt_magic[8] =
  27. {
  28. 0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54
  29. };
  30. static const grub_gpt_part_type_t grub_gpt_partition_type_empty = GRUB_GPT_PARTITION_TYPE_EMPTY;
  31. static struct grub_partition_map grub_gpt_partition_map;
  32. static grub_err_t
  33. gpt_partition_map_iterate (grub_disk_t disk,
  34. int (*hook) (grub_disk_t disk,
  35. const grub_partition_t partition,
  36. void *closure),
  37. void *closure)
  38. {
  39. struct grub_partition part;
  40. struct grub_gpt_header gpt;
  41. struct grub_gpt_partentry entry;
  42. struct grub_msdos_partition_mbr mbr;
  43. grub_uint64_t entries;
  44. unsigned int i;
  45. int last_offset = 0;
  46. /* Read the protective MBR. */
  47. if (grub_disk_read (disk, 0, 0, sizeof (mbr), &mbr))
  48. return grub_errno;
  49. /* Check if it is valid. */
  50. if (mbr.signature != grub_cpu_to_le16 (GRUB_PC_PARTITION_SIGNATURE))
  51. return grub_error (GRUB_ERR_BAD_PART_TABLE, "no signature");
  52. /* Make sure the MBR is a protective MBR and not a normal MBR. */
  53. if (mbr.entries[0].type != GRUB_PC_PARTITION_TYPE_GPT_DISK)
  54. return grub_error (GRUB_ERR_BAD_PART_TABLE, "no GPT partition map found");
  55. /* Read the GPT header. */
  56. if (grub_disk_read (disk, 1, 0, sizeof (gpt), &gpt))
  57. return grub_errno;
  58. if (grub_memcmp (gpt.magic, grub_gpt_magic, sizeof (grub_gpt_magic)))
  59. return grub_error (GRUB_ERR_BAD_PART_TABLE, "no valid GPT header");
  60. grub_dprintf ("gpt", "Read a valid GPT header\n");
  61. entries = grub_le_to_cpu64 (gpt.partitions);
  62. for (i = 0; i < grub_le_to_cpu32 (gpt.maxpart); i++)
  63. {
  64. if (grub_disk_read (disk, entries, last_offset,
  65. sizeof (entry), &entry))
  66. return grub_errno;
  67. if (grub_memcmp (&grub_gpt_partition_type_empty, &entry.type,
  68. sizeof (grub_gpt_partition_type_empty)))
  69. {
  70. /* Calculate the first block and the size of the partition. */
  71. part.start = grub_le_to_cpu64 (entry.start);
  72. part.len = (grub_le_to_cpu64 (entry.end)
  73. - grub_le_to_cpu64 (entry.start) + 1);
  74. part.offset = entries;
  75. part.number = i;
  76. part.index = last_offset;
  77. part.partmap = &grub_gpt_partition_map;
  78. grub_dprintf ("gpt", "GPT entry %d: start=%lld, length=%lld\n", i,
  79. (unsigned long long) part.start,
  80. (unsigned long long) part.len);
  81. if (hook (disk, &part, closure))
  82. return grub_errno;
  83. }
  84. last_offset += grub_le_to_cpu32 (gpt.partentry_size);
  85. if (last_offset == GRUB_DISK_SECTOR_SIZE)
  86. {
  87. last_offset = 0;
  88. entries++;
  89. }
  90. }
  91. return GRUB_ERR_NONE;
  92. }
  93. /* Partition map type. */
  94. static struct grub_partition_map grub_gpt_partition_map =
  95. {
  96. .name = "gpt",
  97. .iterate = gpt_partition_map_iterate,
  98. };
  99. GRUB_MOD_INIT(part_gpt)
  100. {
  101. grub_partition_map_register (&grub_gpt_partition_map);
  102. }
  103. GRUB_MOD_FINI(part_gpt)
  104. {
  105. grub_partition_map_unregister (&grub_gpt_partition_map);
  106. }