]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - disk/part_efi.c
Merge branch 'master' of git://git.denx.de/u-boot-usb
[karo-tx-uboot.git] / disk / part_efi.c
1 /*
2  * Copyright (C) 2008 RuggedCom, Inc.
3  * Richard Retanubun <RichardRetanubun@RuggedCom.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 /*
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
12  */
13 #include <asm/unaligned.h>
14 #include <common.h>
15 #include <command.h>
16 #include <ide.h>
17 #include <inttypes.h>
18 #include <malloc.h>
19 #include <part_efi.h>
20 #include <linux/ctype.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 #ifdef HAVE_BLOCK_DEVICE
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  */
32 static inline u32 efi_crc32(const void *buf, u32 len)
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);
43 static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
44                                 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
45 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
46                                 gpt_header * pgpt_head);
47 static int is_pte_valid(gpt_entry * pte);
48
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
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
72 #ifdef CONFIG_EFI_PARTITION
73 /*
74  * Public Functions (include/part.h)
75  */
76
77 void print_part_efi(block_dev_desc_t * dev_desc)
78 {
79         ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
80         gpt_entry *gpt_pte = NULL;
81         int i = 0;
82         char uuid[37];
83         unsigned char *uuid_bin;
84
85         if (!dev_desc) {
86                 printf("%s: Invalid Argument(s)\n", __func__);
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,
91                          gpt_head, &gpt_pte) != 1) {
92                 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
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                 }
102         }
103
104         debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
105
106         printf("Part\tStart LBA\tEnd LBA\t\tName\n");
107         printf("\tAttributes\n");
108         printf("\tType GUID\n");
109         printf("\tPartition GUID\n");
110
111         for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
112                 /* Stop at the first non valid PTE */
113                 if (!is_pte_valid(&gpt_pte[i]))
114                         break;
115
116                 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
117                         le64_to_cpu(gpt_pte[i].starting_lba),
118                         le64_to_cpu(gpt_pte[i].ending_lba),
119                         print_efiname(&gpt_pte[i]));
120                 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
121                 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
122                 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
123                 printf("\ttype:\t%s\n", uuid);
124                 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
125                 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
126                 printf("\tguid:\t%s\n", uuid);
127         }
128
129         /* Remember to free pte */
130         free(gpt_pte);
131         return;
132 }
133
134 int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
135                                 disk_partition_t * info)
136 {
137         ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
138         gpt_entry *gpt_pte = NULL;
139
140         /* "part" argument must be at least 1 */
141         if (!dev_desc || !info || part < 1) {
142                 printf("%s: Invalid Argument(s)\n", __func__);
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,
148                         gpt_head, &gpt_pte) != 1) {
149                 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
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                 }
159         }
160
161         if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
162             !is_pte_valid(&gpt_pte[part - 1])) {
163                 debug("%s: *** ERROR: Invalid partition number %d ***\n",
164                         __func__, part);
165                 free(gpt_pte);
166                 return -1;
167         }
168
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);
171         /* The ending LBA is inclusive, to calculate size, add 1 to it */
172         info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
173                      - info->start;
174         info->blksz = dev_desc->blksz;
175
176         sprintf((char *)info->name, "%s",
177                         print_efiname(&gpt_pte[part - 1]));
178         sprintf((char *)info->type, "U-Boot");
179         info->bootable = is_bootable(&gpt_pte[part - 1]);
180 #ifdef CONFIG_PARTITION_UUIDS
181         uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
182                         UUID_STR_FORMAT_GUID);
183 #endif
184
185         debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
186               info->start, info->size, info->name);
187
188         /* Remember to free pte */
189         free(gpt_pte);
190         return 0;
191 }
192
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
212 int test_part_efi(block_dev_desc_t * dev_desc)
213 {
214         ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
215
216         /* Read legacy MBR from block 0 and validate it */
217         if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
218                 || (is_pmbr_valid(legacymbr) != 1)) {
219                 return -1;
220         }
221         return 0;
222 }
223
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 {
232         /* Setup the Protective MBR */
233         ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
234         memset(p_mbr, 0, sizeof(*p_mbr));
235
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);
250                 return -1;
251         }
252
253         return 0;
254 }
255
256 int write_gpt_table(block_dev_desc_t *dev_desc,
257                 gpt_header *gpt_h, gpt_entry *gpt_e)
258 {
259         const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
260                                            * sizeof(gpt_entry)), dev_desc);
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
283         if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
284             != pte_blk_cnt)
285                 goto err;
286
287         /* recalculate the values for the Backup GPT Header */
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,
298                                   (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
299                                   + 1,
300                                   pte_blk_cnt, gpt_e) != pte_blk_cnt)
301                 goto err;
302
303         if (dev_desc->block_write(dev_desc->dev,
304                                   (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
305                                   gpt_h) != 1)
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 {
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);
323         int i, k;
324         size_t efiname_len, dosname_len;
325 #ifdef CONFIG_PARTITION_UUIDS
326         char *str_uuid;
327         unsigned char *bin_uuid;
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                 }
344                 if (offset >= last_usable_lba) {
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;
361                 bin_uuid = gpt_e[i].unique_partition_guid.b;
362
363                 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
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 */
375                 efiname_len = sizeof(gpt_e[i].partition_name)
376                         / sizeof(efi_char16_t);
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++)
383                         gpt_e[i].partition_name[k] =
384                                 (efi_char16_t)(partitions[i].name[k]);
385
386                 debug("%s: name: %s offset[%d]: 0x" LBAF
387                       " size[%d]: 0x" LBAF "\n",
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
411         if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
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
422         gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
423                                                        dev_desc));
424         gpt_entry *gpt_e;
425
426         if (gpt_h == NULL) {
427                 printf("%s: calloc failed!\n", __func__);
428                 return -1;
429         }
430
431         gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
432                                                * sizeof(gpt_entry),
433                                                dev_desc));
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
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 &&
471                 get_unaligned_le32(&part->start_sect) == 1UL) {
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
490         if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
491                 return 0;
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  */
511 static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
512                         gpt_header *pgpt_head, gpt_entry **pgpt_pte)
513 {
514         u32 crc32_backup = 0;
515         u32 calc_crc32;
516         u64 lastlba;
517
518         if (!dev_desc || !pgpt_head) {
519                 printf("%s: Invalid Argument(s)\n", __func__);
520                 return 0;
521         }
522
523         /* Read GPT Header from device */
524         if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head)
525                         != 1) {
526                 printf("*** ERROR: Can't read GPT header ***\n");
527                 return 0;
528         }
529
530         /* Check the GPT header signature */
531         if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
532                 printf("GUID Partition Table Header signature is wrong:"
533                         "0x%llX != 0x%llX\n",
534                         le64_to_cpu(pgpt_head->signature),
535                         GPT_HEADER_SIGNATURE);
536                 return 0;
537         }
538
539         /* Check the GUID Partition Table CRC */
540         memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
541         memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
542
543         calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
544                 le32_to_cpu(pgpt_head->header_size));
545
546         memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
547
548         if (calc_crc32 != le32_to_cpu(crc32_backup)) {
549                 printf("GUID Partition Table Header CRC is wrong:"
550                         "0x%x != 0x%x\n",
551                        le32_to_cpu(crc32_backup), calc_crc32);
552                 return 0;
553         }
554
555         /* Check that the my_lba entry points to the LBA that contains the GPT */
556         if (le64_to_cpu(pgpt_head->my_lba) != lba) {
557                 printf("GPT: my_lba incorrect: %llX != %" PRIX64 "\n",
558                        le64_to_cpu(pgpt_head->my_lba),
559                        lba);
560                 return 0;
561         }
562
563         /* Check the first_usable_lba and last_usable_lba are within the disk. */
564         lastlba = (u64)dev_desc->lba;
565         if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
566                 printf("GPT: first_usable_lba incorrect: %llX > %" PRIX64 "\n",
567                        le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
568                 return 0;
569         }
570         if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
571                 printf("GPT: last_usable_lba incorrect: %llX > %" PRIX64 "\n",
572                        le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
573                 return 0;
574         }
575
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);
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,
589                 le32_to_cpu(pgpt_head->num_partition_entries) *
590                 le32_to_cpu(pgpt_head->sizeof_partition_entry));
591
592         if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
593                 printf("GUID Partition Table Entry Array CRC is wrong:"
594                         "0x%x != 0x%x\n",
595                         le32_to_cpu(pgpt_head->partition_entry_array_crc32),
596                         calc_crc32);
597
598                 free(*pgpt_pte);
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 {
618         size_t count = 0, blk_cnt;
619         gpt_entry *pte = NULL;
620
621         if (!dev_desc || !pgpt_head) {
622                 printf("%s: Invalid Argument(s)\n", __func__);
623                 return NULL;
624         }
625
626         count = le32_to_cpu(pgpt_head->num_partition_entries) *
627                 le32_to_cpu(pgpt_head->sizeof_partition_entry);
628
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);
632
633         /* Allocate memory for PTE, remember to FREE */
634         if (count != 0) {
635                 pte = memalign(ARCH_DMA_MINALIGN,
636                                PAD_TO_BLOCKSIZE(count, dev_desc));
637         }
638
639         if (count == 0 || pte == NULL) {
640                 printf("%s: ERROR: Can't allocate 0x%zX "
641                        "bytes for GPT Entries\n",
642                         __func__, count);
643                 return NULL;
644         }
645
646         /* Read GPT Entries from device */
647         blk_cnt = BLOCK_CNT(count, dev_desc);
648         if (dev_desc->block_read (dev_desc->dev,
649                 (lbaint_t)le64_to_cpu(pgpt_head->partition_entry_lba),
650                 (lbaint_t) (blk_cnt), pte)
651                 != blk_cnt) {
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) {
671                 printf("%s: Invalid Argument(s)\n", __func__);
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
683                 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
684                       (unsigned int)(uintptr_t)pte);
685
686                 return 0;
687         } else {
688                 return 1;
689         }
690 }
691 #endif