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 <asm/unaligned.h>
39 #include <linux/ctype.h>
41 DECLARE_GLOBAL_DATA_PTR;
43 #ifdef HAVE_BLOCK_DEVICE
45 * efi_crc32() - EFI version of crc32 function
46 * @buf: buffer to calculate crc32 of
47 * @len - length of buf
49 * Description: Returns EFI-style CRC32 value for @buf
51 static inline u32 efi_crc32(const void *buf, u32 len)
53 return crc32(0, buf, len);
57 * Private function prototypes
60 static int pmbr_part_valid(struct partition *part);
61 static int is_pmbr_valid(legacy_mbr * mbr);
62 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
63 gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
64 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
65 gpt_header * pgpt_head);
66 static int is_pte_valid(gpt_entry * pte);
68 static char *print_efiname(gpt_entry *pte)
70 static char name[PARTNAME_SZ + 1];
72 for (i = 0; i < PARTNAME_SZ; i++) {
74 c = pte->partition_name[i] & 0xff;
75 c = (c && !isprint(c)) ? '.' : c;
78 name[PARTNAME_SZ] = 0;
82 static void uuid_string(unsigned char *uuid, char *str)
84 static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
88 for (i = 0; i < 16; i++) {
89 sprintf(str, "%02x", uuid[le[i]]);
102 static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
104 static inline int is_bootable(gpt_entry *p)
106 return p->attributes.fields.legacy_bios_bootable ||
107 !memcmp(&(p->partition_type_guid), &system_guid,
111 #ifdef CONFIG_EFI_PARTITION
113 * Public Functions (include/part.h)
116 void print_part_efi(block_dev_desc_t * dev_desc)
118 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
119 gpt_entry *gpt_pte = NULL;
124 printf("%s: Invalid Argument(s)\n", __func__);
127 /* This function validates AND fills in the GPT header and PTE */
128 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
129 gpt_head, &gpt_pte) != 1) {
130 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
134 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
136 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
137 printf("\tAttributes\n");
138 printf("\tType UUID\n");
139 printf("\tPartition UUID\n");
141 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
142 /* Stop at the first non valid PTE */
143 if (!is_pte_valid(&gpt_pte[i]))
146 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
147 le64_to_cpu(gpt_pte[i].starting_lba),
148 le64_to_cpu(gpt_pte[i].ending_lba),
149 print_efiname(&gpt_pte[i]));
150 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
151 uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
152 printf("\ttype:\t%s\n", uuid);
153 uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
154 printf("\tuuid:\t%s\n", uuid);
157 /* Remember to free pte */
162 int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
163 disk_partition_t * info)
165 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
166 gpt_entry *gpt_pte = NULL;
168 /* "part" argument must be at least 1 */
169 if (!dev_desc || !info || part < 1) {
170 printf("%s: Invalid Argument(s)\n", __func__);
174 /* This function validates AND fills in the GPT header and PTE */
175 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
176 gpt_head, &gpt_pte) != 1) {
177 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
181 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
182 !is_pte_valid(&gpt_pte[part - 1])) {
183 printf("%s: *** ERROR: Invalid partition number %d ***\n",
188 /* The ulong casting limits the maximum disk size to 2 TB */
189 info->start = (u64)le64_to_cpu(gpt_pte[part - 1].starting_lba);
190 /* The ending LBA is inclusive, to calculate size, add 1 to it */
191 info->size = ((u64)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1)
193 info->blksz = dev_desc->blksz;
195 sprintf((char *)info->name, "%s",
196 print_efiname(&gpt_pte[part - 1]));
197 sprintf((char *)info->type, "U-Boot");
198 info->bootable = is_bootable(&gpt_pte[part - 1]);
199 #ifdef CONFIG_PARTITION_UUIDS
200 uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
203 debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
204 info->start, info->size, info->name);
206 /* Remember to free pte */
211 int test_part_efi(block_dev_desc_t * dev_desc)
213 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
215 /* Read legacy MBR from block 0 and validate it */
216 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
217 || (is_pmbr_valid(legacymbr) != 1)) {
224 * set_protective_mbr(): Set the EFI protective MBR
225 * @param dev_desc - block device descriptor
227 * @return - zero on success, otherwise error
229 static int set_protective_mbr(block_dev_desc_t *dev_desc)
233 /* Setup the Protective MBR */
234 p_mbr = calloc(1, sizeof(p_mbr));
236 printf("%s: calloc failed!\n", __func__);
239 /* Append signature */
240 p_mbr->signature = MSDOS_MBR_SIGNATURE;
241 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
242 p_mbr->partition_record[0].start_sect = 1;
243 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba;
245 /* Write MBR sector to the MMC device */
246 if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
247 printf("** Can't write to device %d **\n",
258 * string_uuid(); Convert UUID stored as string to bytes
260 * @param uuid - UUID represented as string
261 * @param dst - GUID buffer
263 * @return return 0 on successful conversion
265 static int string_uuid(char *uuid, u8 *dst)
274 const u8 uuid_str_len = 36;
276 /* The UUID is written in text: */
278 /* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */
280 debug("%s: uuid: %s\n", __func__, uuid);
282 if (strlen(uuid) != uuid_str_len)
285 for (i = 0; i < uuid_str_len; i++) {
286 if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
290 if (!isxdigit(uuid[i]))
295 a = (u32)simple_strtoul(uuid, NULL, 16);
296 b = (u16)simple_strtoul(uuid + 9, NULL, 16);
297 c = (u16)simple_strtoul(uuid + 14, NULL, 16);
298 d = (u16)simple_strtoul(uuid + 19, NULL, 16);
299 e = (u64)simple_strtoull(uuid + 24, NULL, 16);
302 guid = EFI_GUID(a, b, c, d >> 8, d & 0xFF,
303 *(p + 5), *(p + 4), *(p + 3),
304 *(p + 2), *(p + 1) , *p);
306 memcpy(dst, guid.b, sizeof(efi_guid_t));
311 int write_gpt_table(block_dev_desc_t *dev_desc,
312 gpt_header *gpt_h, gpt_entry *gpt_e)
314 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
315 * sizeof(gpt_entry)), dev_desc);
319 debug("max lba: %x\n", (u32) dev_desc->lba);
320 /* Setup the Protective MBR */
321 if (set_protective_mbr(dev_desc) < 0)
324 /* Generate CRC for the Primary GPT Header */
325 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
326 le32_to_cpu(gpt_h->num_partition_entries) *
327 le32_to_cpu(gpt_h->sizeof_partition_entry));
328 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
330 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
331 le32_to_cpu(gpt_h->header_size));
332 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
334 /* Write the First GPT to the block right after the Legacy MBR */
335 if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
338 if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
342 /* recalculate the values for the Second GPT Header */
343 val = le64_to_cpu(gpt_h->my_lba);
344 gpt_h->my_lba = gpt_h->alternate_lba;
345 gpt_h->alternate_lba = cpu_to_le64(val);
346 gpt_h->header_crc32 = 0;
348 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
349 le32_to_cpu(gpt_h->header_size));
350 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
352 if (dev_desc->block_write(dev_desc->dev,
353 le32_to_cpu(gpt_h->last_usable_lba + 1),
354 pte_blk_cnt, gpt_e) != pte_blk_cnt)
357 if (dev_desc->block_write(dev_desc->dev,
358 le32_to_cpu(gpt_h->my_lba), 1, gpt_h) != 1)
361 debug("GPT successfully written to block device!\n");
365 printf("** Can't write to device %d **\n", dev_desc->dev);
369 int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
370 disk_partition_t *partitions, int parts)
372 u32 offset = (u32)le32_to_cpu(gpt_h->first_usable_lba);
376 #ifdef CONFIG_PARTITION_UUIDS
380 for (i = 0; i < parts; i++) {
381 /* partition starting lba */
382 start = partitions[i].start;
383 if (start && (start < offset)) {
384 printf("Partition overlap\n");
388 gpt_e[i].starting_lba = cpu_to_le64(start);
389 offset = start + partitions[i].size;
391 gpt_e[i].starting_lba = cpu_to_le64(offset);
392 offset += partitions[i].size;
394 if (offset >= gpt_h->last_usable_lba) {
395 printf("Partitions layout exceds disk size\n");
398 /* partition ending lba */
399 if ((i == parts - 1) && (partitions[i].size == 0))
400 /* extend the last partition to maximuim */
401 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
403 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
405 /* partition type GUID */
406 memcpy(gpt_e[i].partition_type_guid.b,
407 &PARTITION_BASIC_DATA_GUID, 16);
409 #ifdef CONFIG_PARTITION_UUIDS
410 str_uuid = partitions[i].uuid;
411 if (string_uuid(str_uuid, gpt_e[i].unique_partition_guid.b)) {
412 printf("Partition no. %d: invalid guid: %s\n",
418 /* partition attributes */
419 memset(&gpt_e[i].attributes, 0,
420 sizeof(gpt_entry_attributes));
423 name_len = sizeof(gpt_e[i].partition_name)
424 / sizeof(efi_char16_t);
425 for (k = 0; k < name_len; k++)
426 gpt_e[i].partition_name[k] =
427 (efi_char16_t)(partitions[i].name[k]);
429 debug("%s: name: %s offset[%d]: 0x%x size[%d]: 0x%lx\n",
430 __func__, partitions[i].name, i,
431 offset, i, partitions[i].size);
437 int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
438 char *str_guid, int parts_count)
440 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
441 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
442 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
443 gpt_h->my_lba = cpu_to_le64(1);
444 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
445 gpt_h->first_usable_lba = cpu_to_le64(34);
446 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
447 gpt_h->partition_entry_lba = cpu_to_le64(2);
448 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
449 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
450 gpt_h->header_crc32 = 0;
451 gpt_h->partition_entry_array_crc32 = 0;
453 if (string_uuid(str_guid, gpt_h->disk_guid.b))
459 int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
460 disk_partition_t *partitions, int parts_count)
464 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
469 printf("%s: calloc failed!\n", __func__);
473 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
477 printf("%s: calloc failed!\n", __func__);
482 /* Generate Primary GPT header (LBA1) */
483 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
487 /* Generate partition entries */
488 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
492 /* Write GPT partition table */
493 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
506 * pmbr_part_valid(): Check for EFI partition signature
508 * Returns: 1 if EFI GPT partition type is found.
510 static int pmbr_part_valid(struct partition *part)
512 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
513 get_unaligned_le32(&part->start_sect) == 1UL) {
521 * is_pmbr_valid(): test Protective MBR for validity
523 * Returns: 1 if PMBR is valid, 0 otherwise.
524 * Validity depends on two things:
525 * 1) MSDOS signature is in the last two bytes of the MBR
526 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
528 static int is_pmbr_valid(legacy_mbr * mbr)
532 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
535 for (i = 0; i < 4; i++) {
536 if (pmbr_part_valid(&mbr->partition_record[i])) {
544 * is_gpt_valid() - tests one GPT header and PTEs for validity
546 * lba is the logical block address of the GPT header to test
547 * gpt is a GPT header ptr, filled on return.
548 * ptes is a PTEs ptr, filled on return.
550 * Description: returns 1 if valid, 0 on error.
551 * If valid, returns pointers to PTEs.
553 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
554 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
556 u32 crc32_backup = 0;
558 unsigned long long lastlba;
560 if (!dev_desc || !pgpt_head) {
561 printf("%s: Invalid Argument(s)\n", __func__);
565 /* Read GPT Header from device */
566 if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
567 printf("*** ERROR: Can't read GPT header ***\n");
571 /* Check the GPT header signature */
572 if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
573 printf("GUID Partition Table Header signature is wrong:"
574 "0x%llX != 0x%llX\n",
575 le64_to_cpu(pgpt_head->signature),
576 GPT_HEADER_SIGNATURE);
580 /* Check the GUID Partition Table CRC */
581 memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
582 memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
584 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
585 le32_to_cpu(pgpt_head->header_size));
587 memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
589 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
590 printf("GUID Partition Table Header CRC is wrong:"
592 le32_to_cpu(crc32_backup), calc_crc32);
596 /* Check that the my_lba entry points to the LBA that contains the GPT */
597 if (le64_to_cpu(pgpt_head->my_lba) != lba) {
598 printf("GPT: my_lba incorrect: %llX != %llX\n",
599 le64_to_cpu(pgpt_head->my_lba),
604 /* Check the first_usable_lba and last_usable_lba are within the disk. */
605 lastlba = (unsigned long long)dev_desc->lba;
606 if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
607 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
608 le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
611 if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
612 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
613 (u64) le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
617 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
618 le64_to_cpu(pgpt_head->first_usable_lba),
619 le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
621 /* Read and allocate Partition Table Entries */
622 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
623 if (*pgpt_pte == NULL) {
624 printf("GPT: Failed to allocate memory for PTE\n");
628 /* Check the GUID Partition Table Entry Array CRC */
629 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
630 le32_to_cpu(pgpt_head->num_partition_entries) *
631 le32_to_cpu(pgpt_head->sizeof_partition_entry));
633 if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
634 printf("GUID Partition Table Entry Array CRC is wrong:"
636 le32_to_cpu(pgpt_head->partition_entry_array_crc32),
643 /* We're done, all's well */
648 * alloc_read_gpt_entries(): reads partition entries from disk
652 * Description: Returns ptes on success, NULL on error.
653 * Allocates space for PTEs based on information found in @gpt.
654 * Notes: remember to free pte when you're done!
656 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
657 gpt_header * pgpt_head)
659 size_t count = 0, blk_cnt;
660 gpt_entry *pte = NULL;
662 if (!dev_desc || !pgpt_head) {
663 printf("%s: Invalid Argument(s)\n", __func__);
667 count = le32_to_cpu(pgpt_head->num_partition_entries) *
668 le32_to_cpu(pgpt_head->sizeof_partition_entry);
670 debug("%s: count = %u * %u = %zu\n", __func__,
671 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
672 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
674 /* Allocate memory for PTE, remember to FREE */
676 pte = memalign(ARCH_DMA_MINALIGN,
677 PAD_TO_BLOCKSIZE(count, dev_desc));
680 if (count == 0 || pte == NULL) {
681 printf("%s: ERROR: Can't allocate 0x%zX "
682 "bytes for GPT Entries\n",
687 /* Read GPT Entries from device */
688 blk_cnt = BLOCK_CNT(count, dev_desc);
689 if (dev_desc->block_read (dev_desc->dev,
690 le64_to_cpu(pgpt_head->partition_entry_lba),
691 (lbaint_t) (blk_cnt), pte)
694 printf("*** ERROR: Can't read GPT Entries ***\n");
702 * is_pte_valid(): validates a single Partition Table Entry
703 * @gpt_entry - Pointer to a single Partition Table Entry
705 * Description: returns 1 if valid, 0 on error.
707 static int is_pte_valid(gpt_entry * pte)
709 efi_guid_t unused_guid;
712 printf("%s: Invalid Argument(s)\n", __func__);
716 /* Only one validation for now:
717 * The GUID Partition Type != Unused Entry (ALL-ZERO)
719 memset(unused_guid.b, 0, sizeof(unused_guid.b));
721 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
722 sizeof(unused_guid.b)) == 0) {
724 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
725 (unsigned int)(uintptr_t)pte);