]>
Commit | Line | Data |
---|---|---|
07f3d789 | 1 | /* |
2 | * Copyright (C) 2008 RuggedCom, Inc. | |
3 | * Richard Retanubun <RichardRetanubun@RuggedCom.com> | |
4 | * | |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
07f3d789 | 6 | */ |
7 | ||
8 | /* | |
e04350d2 SR |
9 | * NOTE: |
10 | * when CONFIG_SYS_64BIT_LBA is not defined, lbaint_t is 32 bits; this | |
11 | * limits the maximum size of addressable storage to < 2 Terra Bytes | |
07f3d789 | 12 | */ |
8faefadb | 13 | #include <asm/unaligned.h> |
07f3d789 | 14 | #include <common.h> |
15 | #include <command.h> | |
16 | #include <ide.h> | |
e48f3741 | 17 | #include <inttypes.h> |
07f3d789 | 18 | #include <malloc.h> |
fae2bf22 | 19 | #include <part_efi.h> |
6eecc030 | 20 | #include <linux/ctype.h> |
07f3d789 | 21 | |
40684ddb ŁM |
22 | DECLARE_GLOBAL_DATA_PTR; |
23 | ||
2c1af9dc | 24 | #ifdef HAVE_BLOCK_DEVICE |
07f3d789 | 25 | /** |
26 | * efi_crc32() - EFI version of crc32 function | |
27 | * @buf: buffer to calculate crc32 of | |
28 | * @len - length of buf | |
29 | * | |
30 | * Description: Returns EFI-style CRC32 value for @buf | |
31 | */ | |
fae2bf22 | 32 | static inline u32 efi_crc32(const void *buf, u32 len) |
07f3d789 | 33 | { |
34 | return crc32(0, buf, len); | |
35 | } | |
36 | ||
37 | /* | |
38 | * Private function prototypes | |
39 | */ | |
40 | ||
41 | static int pmbr_part_valid(struct partition *part); | |
42 | static int is_pmbr_valid(legacy_mbr * mbr); | |
e04350d2 SR |
43 | static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba, |
44 | gpt_header *pgpt_head, gpt_entry **pgpt_pte); | |
07f3d789 | 45 | static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc, |
46 | gpt_header * pgpt_head); | |
07f3d789 | 47 | static int is_pte_valid(gpt_entry * pte); |
48 | ||
6eecc030 LW |
49 | static char *print_efiname(gpt_entry *pte) |
50 | { | |
51 | static char name[PARTNAME_SZ + 1]; | |
52 | int i; | |
53 | for (i = 0; i < PARTNAME_SZ; i++) { | |
54 | u8 c; | |
55 | c = pte->partition_name[i] & 0xff; | |
56 | c = (c && !isprint(c)) ? '.' : c; | |
57 | name[i] = c; | |
58 | } | |
59 | name[PARTNAME_SZ] = 0; | |
60 | return name; | |
61 | } | |
62 | ||
b4414f4a SW |
63 | static efi_guid_t system_guid = PARTITION_SYSTEM_GUID; |
64 | ||
65 | static inline int is_bootable(gpt_entry *p) | |
66 | { | |
67 | return p->attributes.fields.legacy_bios_bootable || | |
68 | !memcmp(&(p->partition_type_guid), &system_guid, | |
69 | sizeof(efi_guid_t)); | |
70 | } | |
71 | ||
40684ddb | 72 | #ifdef CONFIG_EFI_PARTITION |
07f3d789 | 73 | /* |
74 | * Public Functions (include/part.h) | |
75 | */ | |
76 | ||
77 | void print_part_efi(block_dev_desc_t * dev_desc) | |
78 | { | |
ae1768a7 | 79 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); |
deb5ca80 | 80 | gpt_entry *gpt_pte = NULL; |
07f3d789 | 81 | int i = 0; |
f07cd2c4 | 82 | char uuid[37]; |
a96a0e61 | 83 | unsigned char *uuid_bin; |
07f3d789 | 84 | |
85 | if (!dev_desc) { | |
df70b1c2 | 86 | printf("%s: Invalid Argument(s)\n", __func__); |
07f3d789 | 87 | return; |
88 | } | |
89 | /* This function validates AND fills in the GPT header and PTE */ | |
90 | if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, | |
4715a811 | 91 | gpt_head, &gpt_pte) != 1) { |
df70b1c2 | 92 | printf("%s: *** ERROR: Invalid GPT ***\n", __func__); |
ae95fad5 SR |
93 | if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), |
94 | gpt_head, &gpt_pte) != 1) { | |
95 | printf("%s: *** ERROR: Invalid Backup GPT ***\n", | |
96 | __func__); | |
97 | return; | |
98 | } else { | |
99 | printf("%s: *** Using Backup GPT ***\n", | |
100 | __func__); | |
101 | } | |
07f3d789 | 102 | } |
103 | ||
deb5ca80 | 104 | debug("%s: gpt-entry at %p\n", __func__, gpt_pte); |
07f3d789 | 105 | |
788a8c1f | 106 | printf("Part\tStart LBA\tEnd LBA\t\tName\n"); |
13bf2f55 | 107 | printf("\tAttributes\n"); |
d718ded0 PM |
108 | printf("\tType GUID\n"); |
109 | printf("\tPartition GUID\n"); | |
f07cd2c4 | 110 | |
fae2bf22 | 111 | for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) { |
38a3021e SW |
112 | /* Stop at the first non valid PTE */ |
113 | if (!is_pte_valid(&gpt_pte[i])) | |
114 | break; | |
115 | ||
788a8c1f | 116 | printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1), |
fae2bf22 CHP |
117 | le64_to_cpu(gpt_pte[i].starting_lba), |
118 | le64_to_cpu(gpt_pte[i].ending_lba), | |
788a8c1f | 119 | print_efiname(&gpt_pte[i])); |
13bf2f55 | 120 | printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw); |
a96a0e61 | 121 | uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b; |
d718ded0 | 122 | uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID); |
f07cd2c4 | 123 | printf("\ttype:\t%s\n", uuid); |
a96a0e61 | 124 | uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b; |
d718ded0 PM |
125 | uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID); |
126 | printf("\tguid:\t%s\n", uuid); | |
07f3d789 | 127 | } |
128 | ||
129 | /* Remember to free pte */ | |
deb5ca80 | 130 | free(gpt_pte); |
07f3d789 | 131 | return; |
132 | } | |
133 | ||
134 | int get_partition_info_efi(block_dev_desc_t * dev_desc, int part, | |
135 | disk_partition_t * info) | |
136 | { | |
ae1768a7 | 137 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); |
deb5ca80 | 138 | gpt_entry *gpt_pte = NULL; |
07f3d789 | 139 | |
140 | /* "part" argument must be at least 1 */ | |
141 | if (!dev_desc || !info || part < 1) { | |
df70b1c2 | 142 | printf("%s: Invalid Argument(s)\n", __func__); |
07f3d789 | 143 | return -1; |
144 | } | |
145 | ||
146 | /* This function validates AND fills in the GPT header and PTE */ | |
147 | if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, | |
4715a811 | 148 | gpt_head, &gpt_pte) != 1) { |
df70b1c2 | 149 | printf("%s: *** ERROR: Invalid GPT ***\n", __func__); |
ae95fad5 SR |
150 | if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), |
151 | gpt_head, &gpt_pte) != 1) { | |
152 | printf("%s: *** ERROR: Invalid Backup GPT ***\n", | |
153 | __func__); | |
154 | return -1; | |
155 | } else { | |
156 | printf("%s: *** Using Backup GPT ***\n", | |
157 | __func__); | |
158 | } | |
07f3d789 | 159 | } |
160 | ||
fae2bf22 | 161 | if (part > le32_to_cpu(gpt_head->num_partition_entries) || |
c04d68c6 | 162 | !is_pte_valid(&gpt_pte[part - 1])) { |
6d2ee5a3 | 163 | debug("%s: *** ERROR: Invalid partition number %d ***\n", |
c04d68c6 | 164 | __func__, part); |
6d2ee5a3 | 165 | free(gpt_pte); |
c04d68c6 SW |
166 | return -1; |
167 | } | |
168 | ||
e04350d2 SR |
169 | /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */ |
170 | info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba); | |
50970839 | 171 | /* The ending LBA is inclusive, to calculate size, add 1 to it */ |
e04350d2 | 172 | info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1 |
50970839 | 173 | - info->start; |
ae1768a7 | 174 | info->blksz = dev_desc->blksz; |
07f3d789 | 175 | |
6eecc030 | 176 | sprintf((char *)info->name, "%s", |
deb5ca80 | 177 | print_efiname(&gpt_pte[part - 1])); |
07f3d789 | 178 | sprintf((char *)info->type, "U-Boot"); |
b4414f4a | 179 | info->bootable = is_bootable(&gpt_pte[part - 1]); |
894bfbbf | 180 | #ifdef CONFIG_PARTITION_UUIDS |
d718ded0 PM |
181 | uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid, |
182 | UUID_STR_FORMAT_GUID); | |
894bfbbf | 183 | #endif |
07f3d789 | 184 | |
60bf9416 | 185 | debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__, |
04735e9c | 186 | info->start, info->size, info->name); |
07f3d789 | 187 | |
188 | /* Remember to free pte */ | |
deb5ca80 | 189 | free(gpt_pte); |
07f3d789 | 190 | return 0; |
191 | } | |
192 | ||
60bf9416 SR |
193 | int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc, |
194 | const char *name, disk_partition_t *info) | |
195 | { | |
196 | int ret; | |
197 | int i; | |
198 | for (i = 1; i < GPT_ENTRY_NUMBERS; i++) { | |
199 | ret = get_partition_info_efi(dev_desc, i, info); | |
200 | if (ret != 0) { | |
201 | /* no more entries in table */ | |
202 | return -1; | |
203 | } | |
204 | if (strcmp(name, (const char *)info->name) == 0) { | |
205 | /* matched */ | |
206 | return 0; | |
207 | } | |
208 | } | |
209 | return -2; | |
210 | } | |
211 | ||
07f3d789 | 212 | int test_part_efi(block_dev_desc_t * dev_desc) |
213 | { | |
ae1768a7 | 214 | ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz); |
07f3d789 | 215 | |
216 | /* Read legacy MBR from block 0 and validate it */ | |
f75dd584 A |
217 | if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1) |
218 | || (is_pmbr_valid(legacymbr) != 1)) { | |
07f3d789 | 219 | return -1; |
220 | } | |
221 | return 0; | |
222 | } | |
223 | ||
40684ddb ŁM |
224 | /** |
225 | * set_protective_mbr(): Set the EFI protective MBR | |
226 | * @param dev_desc - block device descriptor | |
227 | * | |
228 | * @return - zero on success, otherwise error | |
229 | */ | |
230 | static int set_protective_mbr(block_dev_desc_t *dev_desc) | |
231 | { | |
40684ddb | 232 | /* Setup the Protective MBR */ |
61fcc7d2 HP |
233 | ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1); |
234 | memset(p_mbr, 0, sizeof(*p_mbr)); | |
235 | ||
40684ddb ŁM |
236 | if (p_mbr == NULL) { |
237 | printf("%s: calloc failed!\n", __func__); | |
238 | return -1; | |
239 | } | |
240 | /* Append signature */ | |
241 | p_mbr->signature = MSDOS_MBR_SIGNATURE; | |
242 | p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT; | |
243 | p_mbr->partition_record[0].start_sect = 1; | |
244 | p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba; | |
245 | ||
246 | /* Write MBR sector to the MMC device */ | |
247 | if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) { | |
248 | printf("** Can't write to device %d **\n", | |
249 | dev_desc->dev); | |
40684ddb ŁM |
250 | return -1; |
251 | } | |
252 | ||
40684ddb ŁM |
253 | return 0; |
254 | } | |
255 | ||
40684ddb ŁM |
256 | int write_gpt_table(block_dev_desc_t *dev_desc, |
257 | gpt_header *gpt_h, gpt_entry *gpt_e) | |
258 | { | |
ae1768a7 EE |
259 | const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries |
260 | * sizeof(gpt_entry)), dev_desc); | |
40684ddb ŁM |
261 | u32 calc_crc32; |
262 | u64 val; | |
263 | ||
264 | debug("max lba: %x\n", (u32) dev_desc->lba); | |
265 | /* Setup the Protective MBR */ | |
266 | if (set_protective_mbr(dev_desc) < 0) | |
267 | goto err; | |
268 | ||
269 | /* Generate CRC for the Primary GPT Header */ | |
270 | calc_crc32 = efi_crc32((const unsigned char *)gpt_e, | |
271 | le32_to_cpu(gpt_h->num_partition_entries) * | |
272 | le32_to_cpu(gpt_h->sizeof_partition_entry)); | |
273 | gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32); | |
274 | ||
275 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, | |
276 | le32_to_cpu(gpt_h->header_size)); | |
277 | gpt_h->header_crc32 = cpu_to_le32(calc_crc32); | |
278 | ||
279 | /* Write the First GPT to the block right after the Legacy MBR */ | |
280 | if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1) | |
281 | goto err; | |
282 | ||
ae1768a7 EE |
283 | if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e) |
284 | != pte_blk_cnt) | |
40684ddb ŁM |
285 | goto err; |
286 | ||
ae95fad5 | 287 | /* recalculate the values for the Backup GPT Header */ |
40684ddb ŁM |
288 | val = le64_to_cpu(gpt_h->my_lba); |
289 | gpt_h->my_lba = gpt_h->alternate_lba; | |
290 | gpt_h->alternate_lba = cpu_to_le64(val); | |
291 | gpt_h->header_crc32 = 0; | |
292 | ||
293 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, | |
294 | le32_to_cpu(gpt_h->header_size)); | |
295 | gpt_h->header_crc32 = cpu_to_le32(calc_crc32); | |
296 | ||
297 | if (dev_desc->block_write(dev_desc->dev, | |
e04350d2 SR |
298 | (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba) |
299 | + 1, | |
ae1768a7 | 300 | pte_blk_cnt, gpt_e) != pte_blk_cnt) |
40684ddb ŁM |
301 | goto err; |
302 | ||
303 | if (dev_desc->block_write(dev_desc->dev, | |
e04350d2 SR |
304 | (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1, |
305 | gpt_h) != 1) | |
40684ddb ŁM |
306 | goto err; |
307 | ||
308 | debug("GPT successfully written to block device!\n"); | |
309 | return 0; | |
310 | ||
311 | err: | |
312 | printf("** Can't write to device %d **\n", dev_desc->dev); | |
313 | return -1; | |
314 | } | |
315 | ||
316 | int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e, | |
317 | disk_partition_t *partitions, int parts) | |
318 | { | |
e04350d2 SR |
319 | lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba); |
320 | lbaint_t start; | |
321 | lbaint_t last_usable_lba = (lbaint_t) | |
322 | le64_to_cpu(gpt_h->last_usable_lba); | |
40684ddb | 323 | int i, k; |
67cd4a63 | 324 | size_t efiname_len, dosname_len; |
40684ddb ŁM |
325 | #ifdef CONFIG_PARTITION_UUIDS |
326 | char *str_uuid; | |
a96a0e61 | 327 | unsigned char *bin_uuid; |
40684ddb ŁM |
328 | #endif |
329 | ||
330 | for (i = 0; i < parts; i++) { | |
331 | /* partition starting lba */ | |
332 | start = partitions[i].start; | |
333 | if (start && (start < offset)) { | |
334 | printf("Partition overlap\n"); | |
335 | return -1; | |
336 | } | |
337 | if (start) { | |
338 | gpt_e[i].starting_lba = cpu_to_le64(start); | |
339 | offset = start + partitions[i].size; | |
340 | } else { | |
341 | gpt_e[i].starting_lba = cpu_to_le64(offset); | |
342 | offset += partitions[i].size; | |
343 | } | |
dedf37bb | 344 | if (offset >= last_usable_lba) { |
40684ddb ŁM |
345 | printf("Partitions layout exceds disk size\n"); |
346 | return -1; | |
347 | } | |
348 | /* partition ending lba */ | |
349 | if ((i == parts - 1) && (partitions[i].size == 0)) | |
350 | /* extend the last partition to maximuim */ | |
351 | gpt_e[i].ending_lba = gpt_h->last_usable_lba; | |
352 | else | |
353 | gpt_e[i].ending_lba = cpu_to_le64(offset - 1); | |
354 | ||
355 | /* partition type GUID */ | |
356 | memcpy(gpt_e[i].partition_type_guid.b, | |
357 | &PARTITION_BASIC_DATA_GUID, 16); | |
358 | ||
359 | #ifdef CONFIG_PARTITION_UUIDS | |
360 | str_uuid = partitions[i].uuid; | |
a96a0e61 PM |
361 | bin_uuid = gpt_e[i].unique_partition_guid.b; |
362 | ||
d718ded0 | 363 | if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) { |
40684ddb ŁM |
364 | printf("Partition no. %d: invalid guid: %s\n", |
365 | i, str_uuid); | |
366 | return -1; | |
367 | } | |
368 | #endif | |
369 | ||
370 | /* partition attributes */ | |
371 | memset(&gpt_e[i].attributes, 0, | |
372 | sizeof(gpt_entry_attributes)); | |
373 | ||
374 | /* partition name */ | |
67cd4a63 | 375 | efiname_len = sizeof(gpt_e[i].partition_name) |
40684ddb | 376 | / sizeof(efi_char16_t); |
67cd4a63 MV |
377 | dosname_len = sizeof(partitions[i].name); |
378 | ||
379 | memset(gpt_e[i].partition_name, 0, | |
380 | sizeof(gpt_e[i].partition_name)); | |
381 | ||
382 | for (k = 0; k < min(dosname_len, efiname_len); k++) | |
40684ddb ŁM |
383 | gpt_e[i].partition_name[k] = |
384 | (efi_char16_t)(partitions[i].name[k]); | |
385 | ||
e04350d2 SR |
386 | debug("%s: name: %s offset[%d]: 0x" LBAF |
387 | " size[%d]: 0x" LBAF "\n", | |
40684ddb ŁM |
388 | __func__, partitions[i].name, i, |
389 | offset, i, partitions[i].size); | |
390 | } | |
391 | ||
392 | return 0; | |
393 | } | |
394 | ||
395 | int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h, | |
396 | char *str_guid, int parts_count) | |
397 | { | |
398 | gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE); | |
399 | gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1); | |
400 | gpt_h->header_size = cpu_to_le32(sizeof(gpt_header)); | |
401 | gpt_h->my_lba = cpu_to_le64(1); | |
402 | gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1); | |
403 | gpt_h->first_usable_lba = cpu_to_le64(34); | |
404 | gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34); | |
405 | gpt_h->partition_entry_lba = cpu_to_le64(2); | |
406 | gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS); | |
407 | gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry)); | |
408 | gpt_h->header_crc32 = 0; | |
409 | gpt_h->partition_entry_array_crc32 = 0; | |
410 | ||
d718ded0 | 411 | if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID)) |
40684ddb ŁM |
412 | return -1; |
413 | ||
414 | return 0; | |
415 | } | |
416 | ||
417 | int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid, | |
418 | disk_partition_t *partitions, int parts_count) | |
419 | { | |
420 | int ret; | |
421 | ||
ae1768a7 EE |
422 | gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header), |
423 | dev_desc)); | |
424 | gpt_entry *gpt_e; | |
425 | ||
40684ddb ŁM |
426 | if (gpt_h == NULL) { |
427 | printf("%s: calloc failed!\n", __func__); | |
428 | return -1; | |
429 | } | |
430 | ||
ae1768a7 EE |
431 | gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS |
432 | * sizeof(gpt_entry), | |
433 | dev_desc)); | |
40684ddb ŁM |
434 | if (gpt_e == NULL) { |
435 | printf("%s: calloc failed!\n", __func__); | |
436 | free(gpt_h); | |
437 | return -1; | |
438 | } | |
439 | ||
440 | /* Generate Primary GPT header (LBA1) */ | |
441 | ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count); | |
442 | if (ret) | |
443 | goto err; | |
444 | ||
445 | /* Generate partition entries */ | |
446 | ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count); | |
447 | if (ret) | |
448 | goto err; | |
449 | ||
450 | /* Write GPT partition table */ | |
451 | ret = write_gpt_table(dev_desc, gpt_h, gpt_e); | |
452 | ||
453 | err: | |
454 | free(gpt_e); | |
455 | free(gpt_h); | |
456 | return ret; | |
457 | } | |
458 | #endif | |
459 | ||
07f3d789 | 460 | /* |
461 | * Private functions | |
462 | */ | |
463 | /* | |
464 | * pmbr_part_valid(): Check for EFI partition signature | |
465 | * | |
466 | * Returns: 1 if EFI GPT partition type is found. | |
467 | */ | |
468 | static int pmbr_part_valid(struct partition *part) | |
469 | { | |
470 | if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT && | |
8faefadb | 471 | get_unaligned_le32(&part->start_sect) == 1UL) { |
07f3d789 | 472 | return 1; |
473 | } | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
478 | /* | |
479 | * is_pmbr_valid(): test Protective MBR for validity | |
480 | * | |
481 | * Returns: 1 if PMBR is valid, 0 otherwise. | |
482 | * Validity depends on two things: | |
483 | * 1) MSDOS signature is in the last two bytes of the MBR | |
484 | * 2) One partition of type 0xEE is found, checked by pmbr_part_valid() | |
485 | */ | |
486 | static int is_pmbr_valid(legacy_mbr * mbr) | |
487 | { | |
488 | int i = 0; | |
489 | ||
fae2bf22 | 490 | if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE) |
07f3d789 | 491 | return 0; |
07f3d789 | 492 | |
493 | for (i = 0; i < 4; i++) { | |
494 | if (pmbr_part_valid(&mbr->partition_record[i])) { | |
495 | return 1; | |
496 | } | |
497 | } | |
498 | return 0; | |
499 | } | |
500 | ||
501 | /** | |
502 | * is_gpt_valid() - tests one GPT header and PTEs for validity | |
503 | * | |
504 | * lba is the logical block address of the GPT header to test | |
505 | * gpt is a GPT header ptr, filled on return. | |
506 | * ptes is a PTEs ptr, filled on return. | |
507 | * | |
508 | * Description: returns 1 if valid, 0 on error. | |
509 | * If valid, returns pointers to PTEs. | |
510 | */ | |
e04350d2 SR |
511 | static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba, |
512 | gpt_header *pgpt_head, gpt_entry **pgpt_pte) | |
07f3d789 | 513 | { |
fae2bf22 CHP |
514 | u32 crc32_backup = 0; |
515 | u32 calc_crc32; | |
e04350d2 | 516 | u64 lastlba; |
07f3d789 | 517 | |
518 | if (!dev_desc || !pgpt_head) { | |
df70b1c2 | 519 | printf("%s: Invalid Argument(s)\n", __func__); |
07f3d789 | 520 | return 0; |
521 | } | |
522 | ||
523 | /* Read GPT Header from device */ | |
e04350d2 SR |
524 | if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head) |
525 | != 1) { | |
07f3d789 | 526 | printf("*** ERROR: Can't read GPT header ***\n"); |
527 | return 0; | |
528 | } | |
529 | ||
530 | /* Check the GPT header signature */ | |
fae2bf22 | 531 | if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) { |
07f3d789 | 532 | printf("GUID Partition Table Header signature is wrong:" |
533 | "0x%llX != 0x%llX\n", | |
fae2bf22 CHP |
534 | le64_to_cpu(pgpt_head->signature), |
535 | GPT_HEADER_SIGNATURE); | |
07f3d789 | 536 | return 0; |
537 | } | |
538 | ||
539 | /* Check the GUID Partition Table CRC */ | |
fae2bf22 CHP |
540 | memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup)); |
541 | memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32)); | |
07f3d789 | 542 | |
543 | calc_crc32 = efi_crc32((const unsigned char *)pgpt_head, | |
fae2bf22 | 544 | le32_to_cpu(pgpt_head->header_size)); |
07f3d789 | 545 | |
fae2bf22 | 546 | memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup)); |
07f3d789 | 547 | |
fae2bf22 | 548 | if (calc_crc32 != le32_to_cpu(crc32_backup)) { |
07f3d789 | 549 | printf("GUID Partition Table Header CRC is wrong:" |
fae2bf22 CHP |
550 | "0x%x != 0x%x\n", |
551 | le32_to_cpu(crc32_backup), calc_crc32); | |
07f3d789 | 552 | return 0; |
553 | } | |
554 | ||
555 | /* Check that the my_lba entry points to the LBA that contains the GPT */ | |
fae2bf22 | 556 | if (le64_to_cpu(pgpt_head->my_lba) != lba) { |
e48f3741 SG |
557 | printf("GPT: my_lba incorrect: %llX != %" PRIX64 "\n", |
558 | le64_to_cpu(pgpt_head->my_lba), | |
559 | lba); | |
07f3d789 | 560 | return 0; |
561 | } | |
562 | ||
563 | /* Check the first_usable_lba and last_usable_lba are within the disk. */ | |
e04350d2 | 564 | lastlba = (u64)dev_desc->lba; |
fae2bf22 | 565 | if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) { |
e48f3741 SG |
566 | printf("GPT: first_usable_lba incorrect: %llX > %" PRIX64 "\n", |
567 | le64_to_cpu(pgpt_head->first_usable_lba), lastlba); | |
07f3d789 | 568 | return 0; |
569 | } | |
fae2bf22 | 570 | if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) { |
e48f3741 SG |
571 | printf("GPT: last_usable_lba incorrect: %llX > %" PRIX64 "\n", |
572 | le64_to_cpu(pgpt_head->last_usable_lba), lastlba); | |
07f3d789 | 573 | return 0; |
574 | } | |
575 | ||
e48f3741 SG |
576 | debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %" |
577 | PRIX64 "\n", le64_to_cpu(pgpt_head->first_usable_lba), | |
578 | le64_to_cpu(pgpt_head->last_usable_lba), lastlba); | |
07f3d789 | 579 | |
580 | /* Read and allocate Partition Table Entries */ | |
581 | *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head); | |
582 | if (*pgpt_pte == NULL) { | |
583 | printf("GPT: Failed to allocate memory for PTE\n"); | |
584 | return 0; | |
585 | } | |
586 | ||
587 | /* Check the GUID Partition Table Entry Array CRC */ | |
588 | calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte, | |
fae2bf22 CHP |
589 | le32_to_cpu(pgpt_head->num_partition_entries) * |
590 | le32_to_cpu(pgpt_head->sizeof_partition_entry)); | |
07f3d789 | 591 | |
fae2bf22 | 592 | if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) { |
07f3d789 | 593 | printf("GUID Partition Table Entry Array CRC is wrong:" |
fae2bf22 CHP |
594 | "0x%x != 0x%x\n", |
595 | le32_to_cpu(pgpt_head->partition_entry_array_crc32), | |
07f3d789 | 596 | calc_crc32); |
597 | ||
deb5ca80 | 598 | free(*pgpt_pte); |
07f3d789 | 599 | return 0; |
600 | } | |
601 | ||
602 | /* We're done, all's well */ | |
603 | return 1; | |
604 | } | |
605 | ||
606 | /** | |
607 | * alloc_read_gpt_entries(): reads partition entries from disk | |
608 | * @dev_desc | |
609 | * @gpt - GPT header | |
610 | * | |
611 | * Description: Returns ptes on success, NULL on error. | |
612 | * Allocates space for PTEs based on information found in @gpt. | |
613 | * Notes: remember to free pte when you're done! | |
614 | */ | |
615 | static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc, | |
616 | gpt_header * pgpt_head) | |
617 | { | |
ae1768a7 | 618 | size_t count = 0, blk_cnt; |
07f3d789 | 619 | gpt_entry *pte = NULL; |
620 | ||
621 | if (!dev_desc || !pgpt_head) { | |
df70b1c2 | 622 | printf("%s: Invalid Argument(s)\n", __func__); |
07f3d789 | 623 | return NULL; |
624 | } | |
625 | ||
fae2bf22 CHP |
626 | count = le32_to_cpu(pgpt_head->num_partition_entries) * |
627 | le32_to_cpu(pgpt_head->sizeof_partition_entry); | |
07f3d789 | 628 | |
fae2bf22 CHP |
629 | debug("%s: count = %u * %u = %zu\n", __func__, |
630 | (u32) le32_to_cpu(pgpt_head->num_partition_entries), | |
631 | (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count); | |
07f3d789 | 632 | |
633 | /* Allocate memory for PTE, remember to FREE */ | |
634 | if (count != 0) { | |
ae1768a7 EE |
635 | pte = memalign(ARCH_DMA_MINALIGN, |
636 | PAD_TO_BLOCKSIZE(count, dev_desc)); | |
07f3d789 | 637 | } |
638 | ||
639 | if (count == 0 || pte == NULL) { | |
9936be31 TH |
640 | printf("%s: ERROR: Can't allocate 0x%zX " |
641 | "bytes for GPT Entries\n", | |
df70b1c2 | 642 | __func__, count); |
07f3d789 | 643 | return NULL; |
644 | } | |
645 | ||
646 | /* Read GPT Entries from device */ | |
ae1768a7 | 647 | blk_cnt = BLOCK_CNT(count, dev_desc); |
07f3d789 | 648 | if (dev_desc->block_read (dev_desc->dev, |
e04350d2 | 649 | (lbaint_t)le64_to_cpu(pgpt_head->partition_entry_lba), |
ae1768a7 EE |
650 | (lbaint_t) (blk_cnt), pte) |
651 | != blk_cnt) { | |
07f3d789 | 652 | |
653 | printf("*** ERROR: Can't read GPT Entries ***\n"); | |
654 | free(pte); | |
655 | return NULL; | |
656 | } | |
657 | return pte; | |
658 | } | |
659 | ||
660 | /** | |
661 | * is_pte_valid(): validates a single Partition Table Entry | |
662 | * @gpt_entry - Pointer to a single Partition Table Entry | |
663 | * | |
664 | * Description: returns 1 if valid, 0 on error. | |
665 | */ | |
666 | static int is_pte_valid(gpt_entry * pte) | |
667 | { | |
668 | efi_guid_t unused_guid; | |
669 | ||
670 | if (!pte) { | |
df70b1c2 | 671 | printf("%s: Invalid Argument(s)\n", __func__); |
07f3d789 | 672 | return 0; |
673 | } | |
674 | ||
675 | /* Only one validation for now: | |
676 | * The GUID Partition Type != Unused Entry (ALL-ZERO) | |
677 | */ | |
678 | memset(unused_guid.b, 0, sizeof(unused_guid.b)); | |
679 | ||
680 | if (memcmp(pte->partition_type_guid.b, unused_guid.b, | |
681 | sizeof(unused_guid.b)) == 0) { | |
682 | ||
df70b1c2 | 683 | debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__, |
9936be31 | 684 | (unsigned int)(uintptr_t)pte); |
07f3d789 | 685 | |
686 | return 0; | |
687 | } else { | |
688 | return 1; | |
689 | } | |
690 | } | |
691 | #endif |