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>
20 #include <linux/ctype.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 #ifdef HAVE_BLOCK_DEVICE
26 * efi_crc32() - EFI version of crc32 function
27 * @buf: buffer to calculate crc32 of
28 * @len - length of buf
30 * Description: Returns EFI-style CRC32 value for @buf
32 static inline u32 efi_crc32(const void *buf, u32 len)
34 return crc32(0, buf, len);
38 * Private function prototypes
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);
49 static char *print_efiname(gpt_entry *pte)
51 static char name[PARTNAME_SZ + 1];
53 for (i = 0; i < PARTNAME_SZ; i++) {
55 c = pte->partition_name[i] & 0xff;
56 c = (c && !isprint(c)) ? '.' : c;
59 name[PARTNAME_SZ] = 0;
63 static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
65 static inline int is_bootable(gpt_entry *p)
67 return p->attributes.fields.legacy_bios_bootable ||
68 !memcmp(&(p->partition_type_guid), &system_guid,
72 #ifdef CONFIG_EFI_PARTITION
74 * Public Functions (include/part.h)
77 void print_part_efi(block_dev_desc_t * dev_desc)
79 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
80 gpt_entry *gpt_pte = NULL;
83 unsigned char *uuid_bin;
86 printf("%s: Invalid Argument(s)\n", __func__);
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",
99 printf("%s: *** Using Backup GPT ***\n",
104 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
106 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
107 printf("\tAttributes\n");
108 printf("\tType GUID\n");
109 printf("\tPartition GUID\n");
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]))
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);
129 /* Remember to free pte */
134 int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
135 disk_partition_t * info)
137 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
138 gpt_entry *gpt_pte = NULL;
140 /* "part" argument must be at least 1 */
141 if (!dev_desc || !info || part < 1) {
142 printf("%s: Invalid Argument(s)\n", __func__);
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",
156 printf("%s: *** Using Backup GPT ***\n",
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",
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
174 info->blksz = dev_desc->blksz;
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);
185 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
186 info->start, info->size, info->name);
188 /* Remember to free pte */
193 int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc,
194 const char *name, disk_partition_t *info)
198 for (i = 1; i < GPT_ENTRY_NUMBERS; i++) {
199 ret = get_partition_info_efi(dev_desc, i, info);
201 /* no more entries in table */
204 if (strcmp(name, (const char *)info->name) == 0) {
212 int test_part_efi(block_dev_desc_t * dev_desc)
214 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
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)) {
225 * set_protective_mbr(): Set the EFI protective MBR
226 * @param dev_desc - block device descriptor
228 * @return - zero on success, otherwise error
230 static int set_protective_mbr(block_dev_desc_t *dev_desc)
232 /* Setup the Protective MBR */
233 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
234 memset(p_mbr, 0, sizeof(*p_mbr));
237 printf("%s: calloc failed!\n", __func__);
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;
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",
256 int write_gpt_table(block_dev_desc_t *dev_desc,
257 gpt_header *gpt_h, gpt_entry *gpt_e)
259 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
260 * sizeof(gpt_entry)), dev_desc);
264 debug("max lba: %x\n", (u32) dev_desc->lba);
265 /* Setup the Protective MBR */
266 if (set_protective_mbr(dev_desc) < 0)
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);
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);
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)
283 if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
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;
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);
297 if (dev_desc->block_write(dev_desc->dev,
298 (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
300 pte_blk_cnt, gpt_e) != pte_blk_cnt)
303 if (dev_desc->block_write(dev_desc->dev,
304 (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
308 debug("GPT successfully written to block device!\n");
312 printf("** Can't write to device %d **\n", dev_desc->dev);
316 int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
317 disk_partition_t *partitions, int parts)
319 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
321 lbaint_t last_usable_lba = (lbaint_t)
322 le64_to_cpu(gpt_h->last_usable_lba);
324 size_t efiname_len, dosname_len;
325 #ifdef CONFIG_PARTITION_UUIDS
327 unsigned char *bin_uuid;
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");
338 gpt_e[i].starting_lba = cpu_to_le64(start);
339 offset = start + partitions[i].size;
341 gpt_e[i].starting_lba = cpu_to_le64(offset);
342 offset += partitions[i].size;
344 if (offset >= last_usable_lba) {
345 printf("Partitions layout exceds disk size\n");
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;
353 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
355 /* partition type GUID */
356 memcpy(gpt_e[i].partition_type_guid.b,
357 &PARTITION_BASIC_DATA_GUID, 16);
359 #ifdef CONFIG_PARTITION_UUIDS
360 str_uuid = partitions[i].uuid;
361 bin_uuid = gpt_e[i].unique_partition_guid.b;
363 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
364 printf("Partition no. %d: invalid guid: %s\n",
370 /* partition attributes */
371 memset(&gpt_e[i].attributes, 0,
372 sizeof(gpt_entry_attributes));
375 efiname_len = sizeof(gpt_e[i].partition_name)
376 / sizeof(efi_char16_t);
377 dosname_len = sizeof(partitions[i].name);
379 memset(gpt_e[i].partition_name, 0,
380 sizeof(gpt_e[i].partition_name));
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]);
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);
395 int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
396 char *str_guid, int parts_count)
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;
411 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
417 int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
418 disk_partition_t *partitions, int parts_count)
422 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
427 printf("%s: calloc failed!\n", __func__);
431 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
435 printf("%s: calloc failed!\n", __func__);
440 /* Generate Primary GPT header (LBA1) */
441 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
445 /* Generate partition entries */
446 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
450 /* Write GPT partition table */
451 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
464 * pmbr_part_valid(): Check for EFI partition signature
466 * Returns: 1 if EFI GPT partition type is found.
468 static int pmbr_part_valid(struct partition *part)
470 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
471 get_unaligned_le32(&part->start_sect) == 1UL) {
479 * is_pmbr_valid(): test Protective MBR for validity
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()
486 static int is_pmbr_valid(legacy_mbr * mbr)
490 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
493 for (i = 0; i < 4; i++) {
494 if (pmbr_part_valid(&mbr->partition_record[i])) {
502 * is_gpt_valid() - tests one GPT header and PTEs for validity
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.
508 * Description: returns 1 if valid, 0 on error.
509 * If valid, returns pointers to PTEs.
511 static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
512 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
514 u32 crc32_backup = 0;
518 if (!dev_desc || !pgpt_head) {
519 printf("%s: Invalid Argument(s)\n", __func__);
523 /* Read GPT Header from device */
524 if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head)
526 printf("*** ERROR: Can't read GPT header ***\n");
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);
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));
543 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
544 le32_to_cpu(pgpt_head->header_size));
546 memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
548 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
549 printf("GUID Partition Table Header CRC is wrong:"
551 le32_to_cpu(crc32_backup), calc_crc32);
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),
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);
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);
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);
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");
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));
592 if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
593 printf("GUID Partition Table Entry Array CRC is wrong:"
595 le32_to_cpu(pgpt_head->partition_entry_array_crc32),
602 /* We're done, all's well */
607 * alloc_read_gpt_entries(): reads partition entries from disk
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!
615 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
616 gpt_header * pgpt_head)
618 size_t count = 0, blk_cnt;
619 gpt_entry *pte = NULL;
621 if (!dev_desc || !pgpt_head) {
622 printf("%s: Invalid Argument(s)\n", __func__);
626 count = le32_to_cpu(pgpt_head->num_partition_entries) *
627 le32_to_cpu(pgpt_head->sizeof_partition_entry);
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);
633 /* Allocate memory for PTE, remember to FREE */
635 pte = memalign(ARCH_DMA_MINALIGN,
636 PAD_TO_BLOCKSIZE(count, dev_desc));
639 if (count == 0 || pte == NULL) {
640 printf("%s: ERROR: Can't allocate 0x%zX "
641 "bytes for GPT Entries\n",
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)
653 printf("*** ERROR: Can't read GPT Entries ***\n");
661 * is_pte_valid(): validates a single Partition Table Entry
662 * @gpt_entry - Pointer to a single Partition Table Entry
664 * Description: returns 1 if valid, 0 on error.
666 static int is_pte_valid(gpt_entry * pte)
668 efi_guid_t unused_guid;
671 printf("%s: Invalid Argument(s)\n", __func__);
675 /* Only one validation for now:
676 * The GUID Partition Type != Unused Entry (ALL-ZERO)
678 memset(unused_guid.b, 0, sizeof(unused_guid.b));
680 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
681 sizeof(unused_guid.b)) == 0) {
683 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
684 (unsigned int)(uintptr_t)pte);