fdisk_gpt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #if ENABLE_FEATURE_GPT_LABEL
  2. /*
  3. * Copyright (C) 2010 Kevin Cernekee <cernekee@gmail.com>
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. #define GPT_MAGIC 0x5452415020494645ULL
  8. enum {
  9. LEGACY_GPT_TYPE = 0xee,
  10. GPT_MAX_PARTS = 256,
  11. GPT_MAX_PART_ENTRY_LEN = 4096,
  12. GUID_LEN = 16,
  13. };
  14. typedef struct {
  15. uint64_t magic;
  16. uint32_t revision;
  17. uint32_t hdr_size;
  18. uint32_t hdr_crc32;
  19. uint32_t reserved;
  20. uint64_t current_lba;
  21. uint64_t backup_lba;
  22. uint64_t first_usable_lba;
  23. uint64_t last_usable_lba;
  24. uint8_t disk_guid[GUID_LEN];
  25. uint64_t first_part_lba;
  26. uint32_t n_parts;
  27. uint32_t part_entry_len;
  28. uint32_t part_array_crc32;
  29. } gpt_header;
  30. typedef struct {
  31. uint8_t type_guid[GUID_LEN];
  32. uint8_t part_guid[GUID_LEN];
  33. uint64_t lba_start;
  34. uint64_t lba_end;
  35. uint64_t flags;
  36. uint16_t name36[36];
  37. } gpt_partition;
  38. static gpt_header *gpt_hdr;
  39. static char *part_array;
  40. static unsigned int n_parts;
  41. static unsigned int part_entry_len;
  42. static inline gpt_partition *
  43. gpt_part(int i)
  44. {
  45. if (i >= n_parts) {
  46. return NULL;
  47. }
  48. return (gpt_partition *)&part_array[i * part_entry_len];
  49. }
  50. static uint32_t
  51. gpt_crc32(void *buf, int len)
  52. {
  53. return ~crc32_block_endian0(0xffffffff, buf, len, global_crc32_table);
  54. }
  55. static void
  56. gpt_print_guid(uint8_t *buf)
  57. {
  58. printf(
  59. "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  60. buf[3], buf[2], buf[1], buf[0],
  61. buf[5], buf[4],
  62. buf[7], buf[6],
  63. buf[8], buf[9],
  64. buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
  65. }
  66. static void
  67. gpt_print_wide36(uint16_t *s)
  68. {
  69. #if ENABLE_UNICODE_SUPPORT
  70. char buf[37 * 4];
  71. wchar_t wc[37];
  72. int i = 0;
  73. while (i < ARRAY_SIZE(wc)-1) {
  74. if (s[i] == 0)
  75. break;
  76. wc[i] = s[i];
  77. i++;
  78. }
  79. wc[i] = 0;
  80. if (wcstombs(buf, wc, sizeof(buf)) <= sizeof(buf)-1)
  81. fputs(printable_string(NULL, buf), stdout);
  82. #else
  83. char buf[37];
  84. int i = 0;
  85. while (i < ARRAY_SIZE(buf)-1) {
  86. if (s[i] == 0)
  87. break;
  88. buf[i] = (0x20 <= s[i] && s[i] < 0x7f) ? s[i] : '?';
  89. i++;
  90. }
  91. buf[i] = '\0';
  92. fputs(buf, stdout);
  93. #endif
  94. }
  95. static void
  96. gpt_list_table(int xtra UNUSED_PARAM)
  97. {
  98. int i;
  99. char numstr6[6];
  100. smart_ulltoa5(total_number_of_sectors * sector_size, numstr6, " KMGTPEZY")[0] = '\0';
  101. printf("Disk %s: %llu sectors, %s\n", disk_device,
  102. (unsigned long long)total_number_of_sectors,
  103. numstr6);
  104. printf("Logical sector size: %u\n", sector_size);
  105. printf("Disk identifier (GUID): ");
  106. gpt_print_guid(gpt_hdr->disk_guid);
  107. printf("\nPartition table holds up to %u entries\n",
  108. (int)SWAP_LE32(gpt_hdr->n_parts));
  109. printf("First usable sector is %llu, last usable sector is %llu\n\n",
  110. (unsigned long long)SWAP_LE64(gpt_hdr->first_usable_lba),
  111. (unsigned long long)SWAP_LE64(gpt_hdr->last_usable_lba));
  112. /* "GPT fdisk" has a concept of 16-bit extension of the original MBR 8-bit type codes,
  113. * which it displays here: its output columns are ... Size Code Name
  114. * They are their own invention and are not stored on disk.
  115. * Looks like they use them to support "hybrid" GPT: for example, they have
  116. * AddType(0x8307, "69DAD710-2CE4-4E3C-B16C-21A1D49ABED3", "Linux ARM32 root (/)");
  117. * and then (code>>8) matches what you need to put into MBR's type field for such a partition.
  118. * To print those codes, we'd need a GUID lookup table. Lets just drop the "Code" column instead:
  119. */
  120. puts("Number Start (sector) End (sector) Size Name");
  121. // 123456 123456789012345 123456789012345 12345 abc
  122. for (i = 0; i < n_parts; i++) {
  123. gpt_partition *p = gpt_part(i);
  124. if (p->lba_start) {
  125. smart_ulltoa5((1 + SWAP_LE64(p->lba_end) - SWAP_LE64(p->lba_start)) * sector_size,
  126. numstr6, " KMGTPEZY")[0] = '\0';
  127. printf("%6u %15llu %15llu %s ",
  128. i + 1,
  129. (unsigned long long)SWAP_LE64(p->lba_start),
  130. (unsigned long long)SWAP_LE64(p->lba_end),
  131. numstr6
  132. );
  133. gpt_print_wide36(p->name36);
  134. bb_putchar('\n');
  135. }
  136. }
  137. }
  138. static int
  139. check_gpt_label(void)
  140. {
  141. unsigned part_array_len;
  142. struct partition *first = pt_offset(MBRbuffer, 0);
  143. struct pte pe;
  144. uint32_t crc;
  145. /* LBA 0 contains the legacy MBR */
  146. if (!valid_part_table_flag(MBRbuffer)
  147. || first->sys_ind != LEGACY_GPT_TYPE
  148. ) {
  149. current_label_type = 0;
  150. return 0;
  151. }
  152. /* LBA 1 contains the GPT header */
  153. read_pte(&pe, 1);
  154. gpt_hdr = (void *)pe.sectorbuffer;
  155. if (gpt_hdr->magic != SWAP_LE64(GPT_MAGIC)) {
  156. current_label_type = 0;
  157. return 0;
  158. }
  159. init_unicode();
  160. if (!global_crc32_table) {
  161. global_crc32_new_table_le();
  162. }
  163. crc = SWAP_LE32(gpt_hdr->hdr_crc32);
  164. gpt_hdr->hdr_crc32 = 0;
  165. if (gpt_crc32(gpt_hdr, SWAP_LE32(gpt_hdr->hdr_size)) != crc) {
  166. /* FIXME: read the backup table */
  167. puts("\nwarning: GPT header CRC is invalid\n");
  168. }
  169. n_parts = SWAP_LE32(gpt_hdr->n_parts);
  170. part_entry_len = SWAP_LE32(gpt_hdr->part_entry_len);
  171. if (n_parts > GPT_MAX_PARTS
  172. || part_entry_len > GPT_MAX_PART_ENTRY_LEN
  173. || SWAP_LE32(gpt_hdr->hdr_size) > sector_size
  174. ) {
  175. puts("\nwarning: unable to parse GPT disklabel\n");
  176. current_label_type = 0;
  177. return 0;
  178. }
  179. part_array_len = n_parts * part_entry_len;
  180. part_array = xmalloc(part_array_len);
  181. seek_sector(SWAP_LE64(gpt_hdr->first_part_lba));
  182. if (full_read(dev_fd, part_array, part_array_len) != part_array_len) {
  183. fdisk_fatal(unable_to_read);
  184. }
  185. if (gpt_crc32(part_array, part_array_len) != gpt_hdr->part_array_crc32) {
  186. /* FIXME: read the backup table */
  187. puts("\nwarning: GPT array CRC is invalid\n");
  188. }
  189. puts("Found valid GPT with protective MBR; using GPT\n");
  190. current_label_type = LABEL_GPT;
  191. return 1;
  192. }
  193. #endif /* GPT_LABEL */