2 * Copyright (C) 2008 RuggedCom, Inc.
3 * Richard Retanubun <RichardRetanubun@RuggedCom.com>
5 * SPDX-License-Identifier: GPL-2.0+
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
13 #include <asm/unaligned.h>
21 #include <linux/ctype.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 #ifdef HAVE_BLOCK_DEVICE
27 * efi_crc32() - EFI version of crc32 function
28 * @buf: buffer to calculate crc32 of
29 * @len - length of buf
31 * Description: Returns EFI-style CRC32 value for @buf
33 static inline u32 efi_crc32(const void *buf, u32 len)
35 return crc32(0, buf, len);
39 * Private function prototypes
42 static int pmbr_part_valid(struct partition *part);
43 static int is_pmbr_valid(legacy_mbr * mbr);
44 static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
45 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
46 static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
47 gpt_header *pgpt_head);
48 static int is_pte_valid(gpt_entry * pte);
50 static char *print_efiname(gpt_entry *pte)
52 static char name[PARTNAME_SZ + 1];
54 for (i = 0; i < PARTNAME_SZ; i++) {
56 c = pte->partition_name[i] & 0xff;
57 c = (c && !isprint(c)) ? '.' : c;
60 name[PARTNAME_SZ] = 0;
64 static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
66 static inline int is_bootable(gpt_entry *p)
68 return p->attributes.fields.legacy_bios_bootable ||
69 !memcmp(&(p->partition_type_guid), &system_guid,
73 static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba,
76 uint32_t crc32_backup = 0;
79 /* Check the GPT header signature */
80 if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE) {
81 printf("%s signature is wrong: 0x%llX != 0x%llX\n",
82 "GUID Partition Table Header",
83 le64_to_cpu(gpt_h->signature),
84 GPT_HEADER_SIGNATURE);
88 /* Check the GUID Partition Table CRC */
89 memcpy(&crc32_backup, &gpt_h->header_crc32, sizeof(crc32_backup));
90 memset(&gpt_h->header_crc32, 0, sizeof(gpt_h->header_crc32));
92 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
93 le32_to_cpu(gpt_h->header_size));
95 memcpy(&gpt_h->header_crc32, &crc32_backup, sizeof(crc32_backup));
97 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
98 printf("%s CRC is wrong: 0x%x != 0x%x\n",
99 "GUID Partition Table Header",
100 le32_to_cpu(crc32_backup), calc_crc32);
105 * Check that the my_lba entry points to the LBA that contains the GPT
107 if (le64_to_cpu(gpt_h->my_lba) != lba) {
108 printf("GPT: my_lba incorrect: %llX != " LBAF "\n",
109 le64_to_cpu(gpt_h->my_lba),
115 * Check that the first_usable_lba and that the last_usable_lba are
118 if (le64_to_cpu(gpt_h->first_usable_lba) > lastlba) {
119 printf("GPT: first_usable_lba incorrect: %llX > " LBAF "\n",
120 le64_to_cpu(gpt_h->first_usable_lba), lastlba);
123 if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) {
124 printf("GPT: last_usable_lba incorrect: %llX > " LBAF "\n",
125 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
129 debug("GPT: first_usable_lba: %llX last_usable_lba: %llX last lba: "
130 LBAF "\n", le64_to_cpu(gpt_h->first_usable_lba),
131 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
136 static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
140 /* Check the GUID Partition Table Entry Array CRC */
141 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
142 le32_to_cpu(gpt_h->num_partition_entries) *
143 le32_to_cpu(gpt_h->sizeof_partition_entry));
145 if (calc_crc32 != le32_to_cpu(gpt_h->partition_entry_array_crc32)) {
146 printf("%s: 0x%x != 0x%x\n",
147 "GUID Partition Table Entry Array CRC is wrong",
148 le32_to_cpu(gpt_h->partition_entry_array_crc32),
156 static void prepare_backup_gpt_header(gpt_header *gpt_h)
161 /* recalculate the values for the Backup GPT Header */
162 val = le64_to_cpu(gpt_h->my_lba);
163 gpt_h->my_lba = gpt_h->alternate_lba;
164 gpt_h->alternate_lba = cpu_to_le64(val);
165 gpt_h->partition_entry_lba =
166 cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
167 gpt_h->header_crc32 = 0;
169 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
170 le32_to_cpu(gpt_h->header_size));
171 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
174 #ifdef CONFIG_EFI_PARTITION
176 * Public Functions (include/part.h)
179 void part_print_efi(struct blk_desc *dev_desc)
181 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
182 gpt_entry *gpt_pte = NULL;
185 unsigned char *uuid_bin;
187 /* This function validates AND fills in the GPT header and PTE */
188 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
189 gpt_head, &gpt_pte) != 1) {
190 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
191 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
192 gpt_head, &gpt_pte) != 1) {
193 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
197 printf("%s: *** Using Backup GPT ***\n",
202 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
204 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
205 printf("\tAttributes\n");
206 printf("\tType GUID\n");
207 printf("\tPartition GUID\n");
209 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
210 /* Stop at the first non valid PTE */
211 if (!is_pte_valid(&gpt_pte[i]))
214 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
215 le64_to_cpu(gpt_pte[i].starting_lba),
216 le64_to_cpu(gpt_pte[i].ending_lba),
217 print_efiname(&gpt_pte[i]));
218 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
219 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
220 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
221 printf("\ttype:\t%s\n", uuid);
222 #ifdef CONFIG_PARTITION_TYPE_GUID
223 if (!uuid_guid_get_str(uuid_bin, uuid))
224 printf("\ttype:\t%s\n", uuid);
226 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
227 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
228 printf("\tguid:\t%s\n", uuid);
231 /* Remember to free pte */
236 int part_get_info_efi(struct blk_desc *dev_desc, int part,
237 disk_partition_t *info)
239 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
240 gpt_entry *gpt_pte = NULL;
242 /* "part" argument must be at least 1 */
244 printf("%s: Invalid Argument(s)\n", __func__);
248 /* This function validates AND fills in the GPT header and PTE */
249 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
250 gpt_head, &gpt_pte) != 1) {
251 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
252 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
253 gpt_head, &gpt_pte) != 1) {
254 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
258 printf("%s: *** Using Backup GPT ***\n",
263 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
264 !is_pte_valid(&gpt_pte[part - 1])) {
265 debug("%s: *** ERROR: Invalid partition number %d ***\n",
271 /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
272 info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
273 /* The ending LBA is inclusive, to calculate size, add 1 to it */
274 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
276 info->blksz = dev_desc->blksz;
278 sprintf((char *)info->name, "%s",
279 print_efiname(&gpt_pte[part - 1]));
280 strcpy((char *)info->type, "U-Boot");
281 info->bootable = is_bootable(&gpt_pte[part - 1]);
282 #ifdef CONFIG_PARTITION_UUIDS
283 uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
284 UUID_STR_FORMAT_GUID);
286 #ifdef CONFIG_PARTITION_TYPE_GUID
287 uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
288 info->type_guid, UUID_STR_FORMAT_GUID);
291 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
292 info->start, info->size, info->name);
294 /* Remember to free pte */
299 int part_get_info_efi_by_name(struct blk_desc *dev_desc,
300 const char *name, disk_partition_t *info)
304 for (i = 1; i < GPT_ENTRY_NUMBERS; i++) {
305 ret = part_get_info_efi(dev_desc, i, info);
307 /* no more entries in table */
310 if (strcmp(name, (const char *)info->name) == 0) {
318 static int part_test_efi(struct blk_desc *dev_desc)
320 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
322 /* Read legacy MBR from block 0 and validate it */
323 if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1)
324 || (is_pmbr_valid(legacymbr) != 1)) {
331 * set_protective_mbr(): Set the EFI protective MBR
332 * @param dev_desc - block device descriptor
334 * @return - zero on success, otherwise error
336 static int set_protective_mbr(struct blk_desc *dev_desc)
338 /* Setup the Protective MBR */
339 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
340 memset(p_mbr, 0, sizeof(*p_mbr));
343 printf("%s: calloc failed!\n", __func__);
346 /* Append signature */
347 p_mbr->signature = MSDOS_MBR_SIGNATURE;
348 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
349 p_mbr->partition_record[0].start_sect = 1;
350 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
352 /* Write MBR sector to the MMC device */
353 if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) {
354 printf("** Can't write to device %d **\n",
362 int write_gpt_table(struct blk_desc *dev_desc,
363 gpt_header *gpt_h, gpt_entry *gpt_e)
365 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
366 * sizeof(gpt_entry)), dev_desc);
369 debug("max lba: %x\n", (u32) dev_desc->lba);
370 /* Setup the Protective MBR */
371 if (set_protective_mbr(dev_desc) < 0)
374 /* Generate CRC for the Primary GPT Header */
375 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
376 le32_to_cpu(gpt_h->num_partition_entries) *
377 le32_to_cpu(gpt_h->sizeof_partition_entry));
378 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
380 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
381 le32_to_cpu(gpt_h->header_size));
382 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
384 /* Write the First GPT to the block right after the Legacy MBR */
385 if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1)
388 if (blk_dwrite(dev_desc, 2, pte_blk_cnt, gpt_e)
392 prepare_backup_gpt_header(gpt_h);
394 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
395 + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
398 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
402 debug("GPT successfully written to block device!\n");
406 printf("** Can't write to device %d **\n", dev_desc->devnum);
410 int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
411 disk_partition_t *partitions, int parts)
413 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
415 lbaint_t last_usable_lba = (lbaint_t)
416 le64_to_cpu(gpt_h->last_usable_lba);
418 size_t efiname_len, dosname_len;
419 #ifdef CONFIG_PARTITION_UUIDS
421 unsigned char *bin_uuid;
423 #ifdef CONFIG_PARTITION_TYPE_GUID
425 unsigned char *bin_type_guid;
428 for (i = 0; i < parts; i++) {
429 /* partition starting lba */
430 start = partitions[i].start;
431 if (start && (start < offset)) {
432 printf("Partition overlap\n");
436 gpt_e[i].starting_lba = cpu_to_le64(start);
437 offset = start + partitions[i].size;
439 gpt_e[i].starting_lba = cpu_to_le64(offset);
440 offset += partitions[i].size;
442 if (offset > (last_usable_lba + 1)) {
443 printf("Partitions layout exceds disk size\n");
446 /* partition ending lba */
447 if ((i == parts - 1) && (partitions[i].size == 0))
448 /* extend the last partition to maximuim */
449 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
451 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
453 #ifdef CONFIG_PARTITION_TYPE_GUID
454 str_type_guid = partitions[i].type_guid;
455 bin_type_guid = gpt_e[i].partition_type_guid.b;
456 if (strlen(str_type_guid)) {
457 if (uuid_str_to_bin(str_type_guid, bin_type_guid,
458 UUID_STR_FORMAT_GUID)) {
459 printf("Partition no. %d: invalid type guid: %s\n",
464 /* default partition type GUID */
465 memcpy(bin_type_guid,
466 &PARTITION_BASIC_DATA_GUID, 16);
469 /* partition type GUID */
470 memcpy(gpt_e[i].partition_type_guid.b,
471 &PARTITION_BASIC_DATA_GUID, 16);
474 #ifdef CONFIG_PARTITION_UUIDS
475 str_uuid = partitions[i].uuid;
476 bin_uuid = gpt_e[i].unique_partition_guid.b;
478 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
479 printf("Partition no. %d: invalid guid: %s\n",
485 /* partition attributes */
486 memset(&gpt_e[i].attributes, 0,
487 sizeof(gpt_entry_attributes));
489 if (partitions[i].bootable)
490 gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
493 efiname_len = sizeof(gpt_e[i].partition_name)
494 / sizeof(efi_char16_t);
495 dosname_len = sizeof(partitions[i].name);
497 memset(gpt_e[i].partition_name, 0,
498 sizeof(gpt_e[i].partition_name));
500 for (k = 0; k < min(dosname_len, efiname_len); k++)
501 gpt_e[i].partition_name[k] =
502 (efi_char16_t)(partitions[i].name[k]);
504 debug("%s: name: %s offset[%d]: 0x" LBAF
505 " size[%d]: 0x" LBAF "\n",
506 __func__, partitions[i].name, i,
507 offset, i, partitions[i].size);
513 int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
514 char *str_guid, int parts_count)
516 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
517 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
518 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
519 gpt_h->my_lba = cpu_to_le64(1);
520 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
521 gpt_h->first_usable_lba = cpu_to_le64(34);
522 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
523 gpt_h->partition_entry_lba = cpu_to_le64(2);
524 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
525 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
526 gpt_h->header_crc32 = 0;
527 gpt_h->partition_entry_array_crc32 = 0;
529 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
535 int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
536 disk_partition_t *partitions, int parts_count)
540 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
545 printf("%s: calloc failed!\n", __func__);
549 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
553 printf("%s: calloc failed!\n", __func__);
558 /* Generate Primary GPT header (LBA1) */
559 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
563 /* Generate partition entries */
564 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
568 /* Write GPT partition table */
569 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
577 static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n)
579 char *ess = (char *)es;
584 for (i = 0, j = 0; j < n; i += 2, j++) {
591 int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
595 * This function validates AND
596 * fills in the GPT header and PTE
598 if (is_gpt_valid(dev_desc,
599 GPT_PRIMARY_PARTITION_TABLE_LBA,
600 gpt_head, gpt_pte) != 1) {
601 printf("%s: *** ERROR: Invalid GPT ***\n",
605 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
606 gpt_head, gpt_pte) != 1) {
607 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
615 int gpt_verify_partitions(struct blk_desc *dev_desc,
616 disk_partition_t *partitions, int parts,
617 gpt_header *gpt_head, gpt_entry **gpt_pte)
619 char efi_str[PARTNAME_SZ + 1];
624 ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
630 for (i = 0; i < parts; i++) {
631 if (i == gpt_head->num_partition_entries) {
632 error("More partitions than allowed!\n");
636 /* Check if GPT and ENV partition names match */
637 gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
640 debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
641 __func__, i, efi_str, partitions[i].name);
643 if (strncmp(efi_str, (char *)partitions[i].name,
644 sizeof(partitions->name))) {
645 error("Partition name: %s does not match %s!\n",
646 efi_str, (char *)partitions[i].name);
650 /* Check if GPT and ENV sizes match */
651 gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
652 le64_to_cpu(gpt_e[i].starting_lba) + 1;
653 debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
654 (unsigned long long)gpt_part_size,
655 (unsigned long long)partitions[i].size);
657 if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
658 /* We do not check the extend partition size */
659 if ((i == parts - 1) && (partitions[i].size == 0))
662 error("Partition %s size: %llu does not match %llu!\n",
663 efi_str, (unsigned long long)gpt_part_size,
664 (unsigned long long)partitions[i].size);
669 * Start address is optional - check only if provided
670 * in '$partition' variable
672 if (!partitions[i].start) {
677 /* Check if GPT and ENV start LBAs match */
678 debug("start LBA - GPT: %8llu, ENV: %8llu\n",
679 le64_to_cpu(gpt_e[i].starting_lba),
680 (unsigned long long)partitions[i].start);
682 if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
683 error("Partition %s start: %llu does not match %llu!\n",
684 efi_str, le64_to_cpu(gpt_e[i].starting_lba),
685 (unsigned long long)partitions[i].start);
693 int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf)
698 /* determine start of GPT Header in the buffer */
699 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
701 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
705 /* determine start of GPT Entries in the buffer */
706 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
708 if (validate_gpt_entries(gpt_h, gpt_e))
714 int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
722 if (is_valid_gpt_buf(dev_desc, buf))
725 /* determine start of GPT Header in the buffer */
726 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
729 /* determine start of GPT Entries in the buffer */
730 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
732 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
733 le32_to_cpu(gpt_h->sizeof_partition_entry)),
737 lba = 0; /* MBR is always at 0 */
738 cnt = 1; /* MBR (1 block) */
739 if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) {
740 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
741 __func__, "MBR", cnt, lba);
745 /* write Primary GPT */
746 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
747 cnt = 1; /* GPT Header (1 block) */
748 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
749 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
750 __func__, "Primary GPT Header", cnt, lba);
754 lba = le64_to_cpu(gpt_h->partition_entry_lba);
756 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
757 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
758 __func__, "Primary GPT Entries", cnt, lba);
762 prepare_backup_gpt_header(gpt_h);
764 /* write Backup GPT */
765 lba = le64_to_cpu(gpt_h->partition_entry_lba);
767 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
768 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
769 __func__, "Backup GPT Entries", cnt, lba);
773 lba = le64_to_cpu(gpt_h->my_lba);
774 cnt = 1; /* GPT Header (1 block) */
775 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
776 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
777 __func__, "Backup GPT Header", cnt, lba);
789 * pmbr_part_valid(): Check for EFI partition signature
791 * Returns: 1 if EFI GPT partition type is found.
793 static int pmbr_part_valid(struct partition *part)
795 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
796 get_unaligned_le32(&part->start_sect) == 1UL) {
804 * is_pmbr_valid(): test Protective MBR for validity
806 * Returns: 1 if PMBR is valid, 0 otherwise.
807 * Validity depends on two things:
808 * 1) MSDOS signature is in the last two bytes of the MBR
809 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
811 static int is_pmbr_valid(legacy_mbr * mbr)
815 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
818 for (i = 0; i < 4; i++) {
819 if (pmbr_part_valid(&mbr->partition_record[i])) {
827 * is_gpt_valid() - tests one GPT header and PTEs for validity
829 * lba is the logical block address of the GPT header to test
830 * gpt is a GPT header ptr, filled on return.
831 * ptes is a PTEs ptr, filled on return.
833 * Description: returns 1 if valid, 0 on error.
834 * If valid, returns pointers to PTEs.
836 static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
837 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
839 if (!dev_desc || !pgpt_head) {
840 printf("%s: Invalid Argument(s)\n", __func__);
844 /* Read GPT Header from device */
845 if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
846 printf("*** ERROR: Can't read GPT header ***\n");
850 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
853 /* Read and allocate Partition Table Entries */
854 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
855 if (*pgpt_pte == NULL) {
856 printf("GPT: Failed to allocate memory for PTE\n");
860 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
865 /* We're done, all's well */
870 * alloc_read_gpt_entries(): reads partition entries from disk
874 * Description: Returns ptes on success, NULL on error.
875 * Allocates space for PTEs based on information found in @gpt.
876 * Notes: remember to free pte when you're done!
878 static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
879 gpt_header *pgpt_head)
881 size_t count = 0, blk_cnt;
883 gpt_entry *pte = NULL;
885 if (!dev_desc || !pgpt_head) {
886 printf("%s: Invalid Argument(s)\n", __func__);
890 count = le32_to_cpu(pgpt_head->num_partition_entries) *
891 le32_to_cpu(pgpt_head->sizeof_partition_entry);
893 debug("%s: count = %u * %u = %lu\n", __func__,
894 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
895 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry),
898 /* Allocate memory for PTE, remember to FREE */
900 pte = memalign(ARCH_DMA_MINALIGN,
901 PAD_TO_BLOCKSIZE(count, dev_desc));
904 if (count == 0 || pte == NULL) {
905 printf("%s: ERROR: Can't allocate %#lX bytes for GPT Entries\n",
906 __func__, (ulong)count);
910 /* Read GPT Entries from device */
911 blk = le64_to_cpu(pgpt_head->partition_entry_lba);
912 blk_cnt = BLOCK_CNT(count, dev_desc);
913 if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
914 printf("*** ERROR: Can't read GPT Entries ***\n");
922 * is_pte_valid(): validates a single Partition Table Entry
923 * @gpt_entry - Pointer to a single Partition Table Entry
925 * Description: returns 1 if valid, 0 on error.
927 static int is_pte_valid(gpt_entry * pte)
929 efi_guid_t unused_guid;
932 printf("%s: Invalid Argument(s)\n", __func__);
936 /* Only one validation for now:
937 * The GUID Partition Type != Unused Entry (ALL-ZERO)
939 memset(unused_guid.b, 0, sizeof(unused_guid.b));
941 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
942 sizeof(unused_guid.b)) == 0) {
944 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
945 (unsigned int)(uintptr_t)pte);
954 * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to
955 * check EFI first, since a DOS partition is often used as a 'protective MBR'
958 U_BOOT_PART_TYPE(a_efi) = {
960 .part_type = PART_TYPE_EFI,
961 .get_info = part_get_info_ptr(part_get_info_efi),
962 .print = part_print_ptr(part_print_efi),
963 .test = part_test_efi,