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