fdisk_gpt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 name[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_array_len;
  42. static unsigned int part_entry_len;
  43. static inline gpt_partition *
  44. gpt_part(int i)
  45. {
  46. if (i >= n_parts) {
  47. return NULL;
  48. }
  49. return (gpt_partition *)&part_array[i * part_entry_len];
  50. }
  51. static uint32_t
  52. gpt_crc32(void *buf, int len)
  53. {
  54. return ~crc32_block_endian0(0xffffffff, buf, len, global_crc32_table);
  55. }
  56. static void
  57. gpt_print_guid(uint8_t *buf)
  58. {
  59. printf(
  60. "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  61. buf[3], buf[2], buf[1], buf[0],
  62. buf[5], buf[4],
  63. buf[7], buf[6],
  64. buf[8], buf[9],
  65. buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
  66. }
  67. /* TODO: real unicode support */
  68. static void
  69. gpt_print_wide(uint16_t *s, int max_len)
  70. {
  71. int i = 0;
  72. while (i < max_len) {
  73. if (*s == 0)
  74. return;
  75. fputc(*s, stdout);
  76. s++;
  77. }
  78. }
  79. static void
  80. gpt_list_table(int xtra UNUSED_PARAM)
  81. {
  82. int i;
  83. char numstr6[6];
  84. numstr6[5] = '\0';
  85. smart_ulltoa5(total_number_of_sectors, numstr6, " KMGTPEZY");
  86. printf("Disk %s: %llu sectors, %s\n", disk_device,
  87. (unsigned long long)total_number_of_sectors,
  88. numstr6);
  89. printf("Logical sector size: %u\n", sector_size);
  90. printf("Disk identifier (GUID): ");
  91. gpt_print_guid(gpt_hdr->disk_guid);
  92. printf("\nPartition table holds up to %u entries\n",
  93. (int)SWAP_LE32(gpt_hdr->n_parts));
  94. printf("First usable sector is %llu, last usable sector is %llu\n\n",
  95. (unsigned long long)SWAP_LE64(gpt_hdr->first_usable_lba),
  96. (unsigned long long)SWAP_LE64(gpt_hdr->last_usable_lba));
  97. printf("Number Start (sector) End (sector) Size Code Name\n");
  98. for (i = 0; i < n_parts; i++) {
  99. gpt_partition *p = gpt_part(i);
  100. if (p->lba_start) {
  101. smart_ulltoa5(1 + SWAP_LE64(p->lba_end) - SWAP_LE64(p->lba_start),
  102. numstr6, " KMGTPEZY");
  103. printf("%4u %15llu %15llu %11s %04x ",
  104. i + 1,
  105. (unsigned long long)SWAP_LE64(p->lba_start),
  106. (unsigned long long)SWAP_LE64(p->lba_end),
  107. numstr6,
  108. 0x0700 /* FIXME */);
  109. gpt_print_wide(p->name, 18);
  110. printf("\n");
  111. }
  112. }
  113. }
  114. static int
  115. check_gpt_label(void)
  116. {
  117. struct partition *first = pt_offset(MBRbuffer, 0);
  118. struct pte pe;
  119. uint32_t crc;
  120. /* LBA 0 contains the legacy MBR */
  121. if (!valid_part_table_flag(MBRbuffer)
  122. || first->sys_ind != LEGACY_GPT_TYPE
  123. ) {
  124. current_label_type = 0;
  125. return 0;
  126. }
  127. /* LBA 1 contains the GPT header */
  128. read_pte(&pe, 1);
  129. gpt_hdr = (void *)pe.sectorbuffer;
  130. if (gpt_hdr->magic != SWAP_LE64(GPT_MAGIC)) {
  131. current_label_type = 0;
  132. return 0;
  133. }
  134. if (!global_crc32_table) {
  135. global_crc32_table = crc32_filltable(NULL, 0);
  136. }
  137. crc = SWAP_LE32(gpt_hdr->hdr_crc32);
  138. gpt_hdr->hdr_crc32 = 0;
  139. if (gpt_crc32(gpt_hdr, SWAP_LE32(gpt_hdr->hdr_size)) != crc) {
  140. /* FIXME: read the backup table */
  141. puts("\nwarning: GPT header CRC is invalid\n");
  142. }
  143. n_parts = SWAP_LE32(gpt_hdr->n_parts);
  144. part_entry_len = SWAP_LE32(gpt_hdr->part_entry_len);
  145. if (n_parts > GPT_MAX_PARTS
  146. || part_entry_len > GPT_MAX_PART_ENTRY_LEN
  147. || SWAP_LE32(gpt_hdr->hdr_size) > sector_size
  148. ) {
  149. puts("\nwarning: unable to parse GPT disklabel\n");
  150. current_label_type = 0;
  151. return 0;
  152. }
  153. part_array_len = n_parts * part_entry_len;
  154. part_array = xmalloc(part_array_len);
  155. seek_sector(SWAP_LE64(gpt_hdr->first_part_lba));
  156. if (full_read(dev_fd, part_array, part_array_len) != part_array_len) {
  157. fdisk_fatal(unable_to_read);
  158. }
  159. if (gpt_crc32(part_array, part_array_len) != gpt_hdr->part_array_crc32) {
  160. /* FIXME: read the backup table */
  161. puts("\nwarning: GPT array CRC is invalid\n");
  162. }
  163. puts("Found valid GPT with protective MBR; using GPT\n");
  164. current_label_type = LABEL_GPT;
  165. return 1;
  166. }
  167. #endif /* GPT_LABEL */