2 * Copyright (C) 2008 RuggedCom, Inc.
3 * Richard Retanubun <RichardRetanubun@RuggedCom.com>
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * Problems with CONFIG_SYS_64BIT_LBA:
27 * struct disk_partition.start in include/part.h is sized as ulong.
28 * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t.
29 * For now, it is cast back to ulong at assignment.
31 * This limits the maximum size of addressable storage to < 2 Terra Bytes
33 #include <linux/ctype.h>
39 #include "part_uefi.h"
41 #if defined(CONFIG_CMD_IDE) || \
42 defined(CONFIG_CMD_MG_DISK) || \
43 defined(CONFIG_CMD_SATA) || \
44 defined(CONFIG_CMD_SCSI) || \
45 defined(CONFIG_CMD_USB) || \
46 defined(CONFIG_MMC) || \
47 defined(CONFIG_SYSTEMACE)
49 /* Convert char[2] in little endian format to the host format integer
51 static inline unsigned short le16_to_int(unsigned char *le16)
53 return ((le16[1] << 8) + le16[0]);
56 /* Convert char[4] in little endian format to the host format integer
58 static inline unsigned long le32_to_int(unsigned char *le32)
60 return ((le32[3] << 24) + (le32[2] << 16) + (le32[1] << 8) + le32[0]);
63 /* Convert char[8] in little endian format to the host format integer
65 static inline unsigned long long le64_to_int(unsigned char *le64)
67 return (((unsigned long long)le64[7] << 56) +
68 ((unsigned long long)le64[6] << 48) +
69 ((unsigned long long)le64[5] << 40) +
70 ((unsigned long long)le64[4] << 32) +
71 ((unsigned long long)le64[3] << 24) +
72 ((unsigned long long)le64[2] << 16) +
73 ((unsigned long long)le64[1] << 8) +
74 (unsigned long long)le64[0]);
78 * efi_crc32() - EFI version of crc32 function
79 * @buf: buffer to calculate crc32 of
80 * @len - length of buf
82 * Description: Returns EFI-style CRC32 value for @buf
84 static inline unsigned long efi_crc32(const void *buf, unsigned long len)
86 return crc32(0, buf, len);
90 * Private function prototypes
93 static int pmbr_part_valid(struct partition *part);
94 static int is_pmbr_valid(legacy_mbr * mbr);
96 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
97 gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
98 #ifdef CONFIG_EMMC_BOOT
99 static int format_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
100 gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
102 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
103 gpt_header * pgpt_head);
105 static int is_pte_valid(gpt_entry * pte);
107 #define PARTNAME_SZ (72 / sizeof(efi_char16_t))
108 static char *print_efiname(gpt_entry *pte)
110 static char name[PARTNAME_SZ + 1];
112 for (i = 0; i < PARTNAME_SZ; i++) {
114 c = pte->partition_name[i] & 0xff;
115 c = (c && !isprint(c)) ? '.' : c;
118 name[PARTNAME_SZ] = 0;
124 * Public Functions (include/part.h)
127 void print_part_efi(block_dev_desc_t * dev_desc)
130 gpt_entry *pgpt_pte = NULL;
134 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
137 /* This function validates AND fills in the GPT header and PTE */
138 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
139 &(gpt_head), &pgpt_pte) != 1) {
140 debug("%s: *** ERROR: Invalid GPT ***\n", __FUNCTION__);
144 debug("%s: gpt-entry at 0x%08X\n", __FUNCTION__, (unsigned int)pgpt_pte);
146 debug("Part Start LBA End LBA\n");
147 for (i = 0; i < le32_to_int(gpt_head.num_partition_entries); i++) {
149 if (is_pte_valid(&(pgpt_pte)[i])) {
150 debug("%s%d 0x%llX 0x%llX\n", GPT_ENTRY_NAME,
152 le64_to_int((pgpt_pte)[i].starting_lba),
153 le64_to_int((pgpt_pte)[i].ending_lba));
155 break; /* Stop at the first non valid PTE */
159 /* Remember to free pte */
160 if (pgpt_pte != NULL) {
161 debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
167 int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
168 disk_partition_t * info)
171 gpt_entry *pgpt_pte = NULL;
173 /* "part" argument must be at least 1 */
174 if (!dev_desc || !info || part < 1) {
175 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
179 /* This function validates AND fills in the GPT header and PTE */
180 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
181 &(gpt_head), &pgpt_pte) != 1) {
182 debug("%s: *** ERROR: Invalid Main GPT ***\n", __FUNCTION__);
183 if(is_gpt_valid(dev_desc, dev_desc->lba -1, &(gpt_head), &pgpt_pte) != 1){
184 debug("%s: *** ERROR: Invalid alternate GPT ***\n", __FUNCTION__);
190 /* The ulong casting limits the maximum disk size to 2 TB */
191 info->start = (ulong) le64_to_int((pgpt_pte)[part - 1].starting_lba);
192 /* The ending LBA is inclusive, to calculate size, add 1 to it */
193 info->size = ((ulong)le64_to_int((pgpt_pte)[part - 1].ending_lba) + 1)
195 info->blksz = GPT_BLOCK_SIZE;
197 sprintf((char *)info->name, "%s", print_efiname(&((pgpt_pte)[part - 1])));
198 sprintf((char *)info->type, "U-Boot");
200 debug("%s: start 0x%lX, size 0x%lX, name %s", __FUNCTION__,
201 info->start, info->size, info->name);
203 /* Remember to free pte */
204 if (pgpt_pte != NULL) {
205 debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
211 int get_partition_info_by_name_efi(block_dev_desc_t * dev_desc, wchar_t* partition_name,
212 disk_partition_t * info)
215 gpt_entry *pgpt_pte = NULL;
217 unsigned int i,j,partition_nums=0;
218 wchar_t disk_parition[MAX_UTF_PARTITION_NAME_LEN];
220 if (!dev_desc || !info || !partition_name) {
221 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
225 /* This function validates AND fills in the GPT header and PTE */
226 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
227 &(gpt_head), &pgpt_pte) != 1) {
228 debug("%s: *** ERROR: Invalid Main GPT ***\n", __FUNCTION__);
229 if(is_gpt_valid(dev_desc, dev_desc->lba -1, &(gpt_head), &pgpt_pte) != 1){
230 debug("%s: *** ERROR: Invalid alternate GPT ***\n", __FUNCTION__);
236 /*Get the partition info*/
237 partition_nums = le32_to_int(gpt_head.num_partition_entries);
238 for(i=0;i<partition_nums;i++)
240 for(j=0;j<MAX_UTF_PARTITION_NAME_LEN;j++)
242 disk_parition[j] = (wchar_t)pgpt_pte[i].partition_name[j];
245 if(0 == wcscmp(disk_parition, partition_name))
247 /* The ulong casting limits the maximum disk size to 2 TB */
248 info->start = (ulong) le64_to_int((pgpt_pte)[i].starting_lba);
249 /* The ending LBA is inclusive, to calculate size, add 1 to it */
250 info->size = ((ulong)le64_to_int((pgpt_pte)[i].ending_lba) + 1) - info->start;
251 info->blksz = GPT_BLOCK_SIZE;
253 sprintf((char *)info->name, "%s", print_efiname(&((pgpt_pte)[i])));
254 sprintf((char *)info->type, "U-Boot");
256 debug("%s: start 0x%lX, size 0x%lX, name %s", __FUNCTION__,
257 info->start, info->size, info->name);
263 /* Remember to free pte */
264 if (pgpt_pte != NULL) {
265 debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
272 int get_partition_num_by_name_efi(block_dev_desc_t *dev_desc, const char *partition_name)
275 gpt_entry *pgpt_pte = NULL;
277 unsigned int i, j, partition_nums = 0;
278 int part_name_len = (72 / sizeof(efi_char16_t));
280 if (!dev_desc || !partition_name) {
281 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
285 /* This function validates AND fills in the GPT header and PTE */
286 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
287 &(gpt_head), &pgpt_pte) != 1) {
288 debug("%s: *** ERROR: Invalid Main GPT ***\n", __FUNCTION__);
289 if (is_gpt_valid(dev_desc, dev_desc->lba -1, &(gpt_head), &pgpt_pte) != 1){
290 debug("%s: *** ERROR: Invalid alternate GPT ***\n", __FUNCTION__);
296 partition_nums = le32_to_int(gpt_head.num_partition_entries);
297 for (i = 0; i < partition_nums; i++) {
298 for (j = 0; j < part_name_len; j++) {
299 if (partition_name[j] != pgpt_pte[i].partition_name[j])
301 if (partition_name[j] == 0 && pgpt_pte[i].partition_name[j] == 0) {
309 /* Remember to free pte */
310 if (pgpt_pte != NULL) {
311 debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
318 int get_all_partition_info_efi(block_dev_desc_t * dev_desc, PARTITION_CFG * info, unsigned int *total_partition_num)
321 gpt_entry *pgpt_pte = NULL;
322 unsigned int i,j, partition_nums = 0;
324 if (!dev_desc || !info) {
325 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
329 /* This function validates AND fills in the GPT header and PTE */
330 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
331 &(gpt_head), &pgpt_pte) != 1) {
332 debug("%s: *** ERROR: Invalid Main GPT ***\n", __FUNCTION__);
333 if(is_gpt_valid(dev_desc, dev_desc->lba -1, &(gpt_head), &pgpt_pte) != 1){
334 debug("%s: *** ERROR: Invalid alternate GPT ***\n", __FUNCTION__);
340 partition_nums = le32_to_int(gpt_head.num_partition_entries);
342 //TODO:partitions shuld beyond MAX PARTITION NUM
343 for(i=0;i<partition_nums;i++)
345 /* The ulong casting limits the maximum disk size to 2 TB */
346 info[i].partition_offset = (ulong) le64_to_int((pgpt_pte)[i].starting_lba);
347 /* The ending LBA is inclusive, to calculate size, add 1 to it */
348 info[i].partition_size = ((ulong)le64_to_int((pgpt_pte)[i].ending_lba) + 1) - info[i].partition_offset;
349 //TODO: We write partition index at unique_partition_guid.b[15]
350 info[i].partition_index = pgpt_pte[i].unique_partition_guid.b[15];
352 for(j=0;j<MAX_UTF_PARTITION_NAME_LEN;j++)
354 info[i].partition_name[j] = (wchar_t)pgpt_pte[i].partition_name[j];
357 debug("%s: start 0x%lX, size 0x%lX, name %S", __FUNCTION__,
358 info[i].partition_offset, info[i].partition_size, info[i].partition_name);
361 *total_partition_num = partition_nums;
363 /* Remember to free pte */
364 if (pgpt_pte != NULL) {
365 debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
372 int get_partition_info_efi_with_partnum(block_dev_desc_t * dev_desc, int part,
373 disk_partition_t * info, unsigned long total, unsigned long sdidx, int sdpart, disk_partition_t *sdinfo)
376 gpt_entry *pgpt_pte = NULL;
378 /* "part" argument must be at least 1 */
379 if (!dev_desc || !info || part < 1) {
380 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
384 /* This function validates AND fills in the GPT header and PTE */
385 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
386 &(gpt_head), &pgpt_pte) != 1) {
387 debug("%s: *** ERROR: Invalid Main GPT ***\n", __FUNCTION__);
388 if(is_gpt_valid(dev_desc, dev_desc->lba -1, &(gpt_head), &pgpt_pte) != 1){
389 debug("%s: *** ERROR: Invalid alternate GPT ***\n", __FUNCTION__);
395 /* The ulong casting limits the maximum disk size to 2 TB */
396 info->start = (ulong) le64_to_int((pgpt_pte)[part - 1].starting_lba);
397 /* The ending LBA is inclusive, to calculate size, add 1 to it */
398 info->size = ((ulong)le64_to_int((pgpt_pte)[part - 1].ending_lba) + 1)
400 info->blksz = GPT_BLOCK_SIZE;
402 sprintf((char *)info->name, "%s",
403 print_efiname(&((pgpt_pte)[part - 1])));
404 sprintf((char *)info->type, "U-Boot");
406 debug("%s: start 0x%lX, size 0x%lX, name %s\n", __FUNCTION__,
407 info->start, info->size, info->name);
410 if (sdidx < (le32_to_int(gpt_head.num_partition_entries))) {
411 sdinfo->start = (ulong) le64_to_int((pgpt_pte)[sdpart - 1].starting_lba);
412 sdinfo->size = ((ulong)le64_to_int((pgpt_pte)[sdpart - 1].ending_lba) + 1) - sdinfo->start;
415 /* Remember to free pte */
416 if (pgpt_pte != NULL) {
417 debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
423 else if (total != le32_to_int(gpt_head.num_partition_entries))
429 int test_part_efi(block_dev_desc_t * dev_desc)
431 legacy_mbr legacymbr;
433 /* Read legacy MBR from block 0 and validate it */
434 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) & legacymbr) != 1)
435 || (is_pmbr_valid(&legacymbr) != 1)) {
445 * pmbr_part_valid(): Check for EFI partition signature
447 * Returns: 1 if EFI GPT partition type is found.
449 static int pmbr_part_valid(struct partition *part)
451 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
452 le32_to_int(part->start_sect) == 1UL) {
460 * is_pmbr_valid(): test Protective MBR for validity
462 * Returns: 1 if PMBR is valid, 0 otherwise.
463 * Validity depends on two things:
464 * 1) MSDOS signature is in the last two bytes of the MBR
465 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
467 static int is_pmbr_valid(legacy_mbr * mbr)
471 if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) {
475 for (i = 0; i < 4; i++) {
476 if (pmbr_part_valid(&mbr->partition_record[i])) {
484 * is_gpt_valid() - tests one GPT header and PTEs for validity
486 * lba is the logical block address of the GPT header to test
487 * gpt is a GPT header ptr, filled on return.
488 * ptes is a PTEs ptr, filled on return.
490 * Description: returns 1 if valid, 0 on error.
491 * If valid, returns pointers to PTEs.
493 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
494 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
496 unsigned char crc32_backup[4] = { 0 };
497 unsigned long calc_crc32;
498 unsigned long long lastlba;
499 //debug("Enter is_gpt_valid 0x%16Lx \n", lba);
500 if (!dev_desc || !pgpt_head) {
501 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
505 /* Read GPT Header from device */
506 if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
507 debug("*** ERROR: Can't read GPT header ***\n");
511 /* Check the GPT header signature */
512 if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
513 debug("GUID Partition Table Header signature is wrong:"
514 "0x%llX != 0x%llX\n",
515 (unsigned long long)le64_to_int(pgpt_head->signature),
516 (unsigned long long)GPT_HEADER_SIGNATURE);
520 /* Check the GUID Partition Table CRC */
521 memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
522 memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
524 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
525 le32_to_int(pgpt_head->header_size));
527 memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
529 if (calc_crc32 != le32_to_int(crc32_backup)) {
530 debug("GUID Partition Table Header CRC is wrong:"
531 "0x%08lX != 0x%08lX\n",
532 le32_to_int(crc32_backup), calc_crc32);
536 /* Check that the my_lba entry points to the LBA that contains the GPT */
537 if (le64_to_int(pgpt_head->my_lba) != lba) {
538 debug("GPT: my_lba incorrect: %llX != %llX\n",
539 (unsigned long long)le64_to_int(pgpt_head->my_lba),
540 (unsigned long long)lba);
544 /* Check the first_usable_lba and last_usable_lba are within the disk. */
545 lastlba = (unsigned long long)dev_desc->lba;
546 if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
547 debug("GPT: first_usable_lba incorrect: %llX > %llX\n",
548 le64_to_int(pgpt_head->first_usable_lba), lastlba);
551 if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
552 debug("GPT: last_usable_lba incorrect: %llX > %llX\n",
553 le64_to_int(pgpt_head->last_usable_lba), lastlba);
557 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
558 le64_to_int(pgpt_head->first_usable_lba),
559 le64_to_int(pgpt_head->last_usable_lba), lastlba);
561 /* Read and allocate Partition Table Entries */
562 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
563 if (*pgpt_pte == NULL) {
564 debug("GPT: Failed to allocate memory for PTE\n");
568 /* Check the GUID Partition Table Entry Array CRC */
569 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
570 le32_to_int(pgpt_head->num_partition_entries) *
571 le32_to_int(pgpt_head->sizeof_partition_entry));
573 if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
574 debug("GUID Partition Table Entry Array CRC is wrong:"
575 "0x%08lX != 0x%08lX\n",
576 le32_to_int(pgpt_head->partition_entry_array_crc32),
579 if (*pgpt_pte != NULL) {
585 /* We're done, all's well */
589 #ifdef CONFIG_EMMC_BOOT
591 * format_gpt_valid() - tests one GPT header and PTEs for validity
593 * lba is the logical block address of the GPT header to test
594 * gpt is a GPT header ptr, filled on return.
595 * ptes is a PTEs ptr, filled on return.
597 * Description: returns 1 if valid, 0 on error.
598 * If valid, returns pointers to PTEs.
600 static int format_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
601 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
603 unsigned char crc32_backup[4] = { 0 };
604 unsigned long calc_crc32;
605 unsigned long long lastlba;
608 if (!dev_desc || !pgpt_head) {
609 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
613 memset(pgpt_head, 0, sizeof(gpt_header));
615 /* set GPT Header from device */
616 for (idx = 0; idx < 8; idx ++) {
617 pgpt_head->signature[idx] = (GPT_HEADER_SIGNATURE >> (8 * idx)) & 0xff;
618 debug("signature[%d] = 0x%02x\n", idx, pgpt_head->signature[idx]);
621 if (dev_desc->block_write(dev_desc->dev, lba, 1, pgpt_head) != 1) {
622 debug("*** ERROR: Can't write GPT header ***\n");
627 /* Check the GPT header signature */
628 if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
629 debug("GUID Partition Table Header signature is wrong:"
630 "0x%llX != 0x%llX\n",
631 (unsigned long long)le64_to_int(pgpt_head->signature),
632 (unsigned long long)GPT_HEADER_SIGNATURE);
636 /* Check the GUID Partition Table CRC */
637 memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
638 memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
640 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
641 le32_to_int(pgpt_head->header_size));
643 memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
645 if (calc_crc32 != le32_to_int(crc32_backup)) {
646 debug("GUID Partition Table Header CRC is wrong:"
647 "0x%08lX != 0x%08lX\n",
648 le32_to_int(crc32_backup), calc_crc32);
652 /* Check that the my_lba entry points to the LBA that contains the GPT */
653 if (le64_to_int(pgpt_head->my_lba) != lba) {
654 debug("GPT: my_lba incorrect: %llX != %llX\n",
655 (unsigned long long)le64_to_int(pgpt_head->my_lba),
656 (unsigned long long)lba);
660 /* Check the first_usable_lba and last_usable_lba are within the disk. */
661 lastlba = (unsigned long long)dev_desc->lba;
662 if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
663 debug("GPT: first_usable_lba incorrect: %llX > %llX\n",
664 le64_to_int(pgpt_head->first_usable_lba), lastlba);
667 if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
668 debug("GPT: last_usable_lba incorrect: %llX > %llX\n",
669 le64_to_int(pgpt_head->last_usable_lba), lastlba);
673 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
674 le64_to_int(pgpt_head->first_usable_lba),
675 le64_to_int(pgpt_head->last_usable_lba), lastlba);
677 /* Read and allocate Partition Table Entries */
678 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
679 if (*pgpt_pte == NULL) {
680 debug("GPT: Failed to allocate memory for PTE\n");
684 /* Check the GUID Partition Table Entry Array CRC */
685 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
686 le32_to_int(pgpt_head->num_partition_entries) *
687 le32_to_int(pgpt_head->sizeof_partition_entry));
689 if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
690 debug("GUID Partition Table Entry Array CRC is wrong:"
691 "0x%08lX != 0x%08lX\n",
692 le32_to_int(pgpt_head->partition_entry_array_crc32),
695 if (*pgpt_pte != NULL) {
701 /* We're done, all's well */
707 * alloc_read_gpt_entries(): reads partition entries from disk
711 * Description: Returns ptes on success, NULL on error.
712 * Allocates space for PTEs based on information found in @gpt.
713 * Notes: remember to free pte when you're done!
715 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
716 gpt_header * pgpt_head)
719 gpt_entry *pte = NULL;
721 if (!dev_desc || !pgpt_head) {
722 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
726 count = le32_to_int(pgpt_head->num_partition_entries) *
727 le32_to_int(pgpt_head->sizeof_partition_entry);
729 debug("%s: count = %lu * %lu = %u\n", __FUNCTION__,
730 le32_to_int(pgpt_head->num_partition_entries),
731 le32_to_int(pgpt_head->sizeof_partition_entry), count);
732 if(count % GPT_BLOCK_SIZE){
734 ntemp = count/GPT_BLOCK_SIZE +1;
735 count = ntemp * GPT_BLOCK_SIZE;
737 /* Allocate memory for PTE, remember to FREE */
742 if (count == 0 || pte == NULL) {
743 debug("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
744 __FUNCTION__, count);
748 /* Read GPT Entries from device */
749 if (dev_desc->block_read (dev_desc->dev,
750 (unsigned long)le64_to_int(pgpt_head->partition_entry_lba),
751 (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
752 != (count / GPT_BLOCK_SIZE)) {
754 debug("*** ERROR: Can't read GPT Entries ***\n");
762 * is_pte_valid(): validates a single Partition Table Entry
763 * @gpt_entry - Pointer to a single Partition Table Entry
765 * Description: returns 1 if valid, 0 on error.
767 static int is_pte_valid(gpt_entry * pte)
769 efi_guid_t unused_guid;
772 debug("%s: Invalid Argument(s)\n", __FUNCTION__);
776 /* Only one validation for now:
777 * The GUID Partition Type != Unused Entry (ALL-ZERO)
779 memset(unused_guid.b, 0, sizeof(unused_guid.b));
781 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
782 sizeof(unused_guid.b)) == 0) {
784 debug("%s: Found an unused PTE GUID at 0x%08X\n", __FUNCTION__,