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