X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=disk%2Fpart_efi.c;h=de3491821f082e80de02587e97570ce8b5954c78;hb=406df85345f942d9348443983d81a01e013e920b;hp=2945892a24d225d910e16720295e88dd01b8666e;hpb=9d0dc69235e8327dba5536761c768d40c4e514e5;p=oweals%2Fu-boot.git diff --git a/disk/part_efi.c b/disk/part_efi.c index 2945892a24..de3491821f 100644 --- a/disk/part_efi.c +++ b/disk/part_efi.c @@ -9,17 +9,22 @@ * when CONFIG_SYS_64BIT_LBA is not defined, lbaint_t is 32 bits; this * limits the maximum size of addressable storage to < 2 Terra Bytes */ -#include #include +#include +#include +#include +#include +#include +#include #include #include #include -#include #include #include #include #include #include +#include DECLARE_GLOBAL_DATA_PTR; @@ -52,6 +57,8 @@ static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc, gpt_header *pgpt_head); static int is_pte_valid(gpt_entry * pte); +static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head, + gpt_entry **pgpt_pte); static char *print_efiname(gpt_entry *pte) { @@ -67,13 +74,17 @@ static char *print_efiname(gpt_entry *pte) return name; } -static efi_guid_t system_guid = PARTITION_SYSTEM_GUID; +static const efi_guid_t system_guid = PARTITION_SYSTEM_GUID; -static inline int is_bootable(gpt_entry *p) +static int get_bootable(gpt_entry *p) { - return p->attributes.fields.legacy_bios_bootable || - !memcmp(&(p->partition_type_guid), &system_guid, - sizeof(efi_guid_t)); + int ret = 0; + + if (!memcmp(&p->partition_type_guid, &system_guid, sizeof(efi_guid_t))) + ret |= PART_EFI_SYSTEM_PARTITION; + if (p->attributes.fields.legacy_bios_bootable) + ret |= PART_BOOTABLE; + return ret; } static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba, @@ -83,11 +94,11 @@ static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba, uint32_t calc_crc32; /* Check the GPT header signature */ - if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE) { + if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE_UBOOT) { printf("%s signature is wrong: 0x%llX != 0x%llX\n", "GUID Partition Table Header", le64_to_cpu(gpt_h->signature), - GPT_HEADER_SIGNATURE); + GPT_HEADER_SIGNATURE_UBOOT); return -1; } @@ -193,23 +204,14 @@ int get_disk_guid(struct blk_desc * dev_desc, char *guid) unsigned char *guid_bin; /* This function validates AND fills in the GPT header and PTE */ - if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, - gpt_head, &gpt_pte) != 1) { - printf("%s: *** ERROR: Invalid GPT ***\n", __func__); - if (is_gpt_valid(dev_desc, dev_desc->lba - 1, - gpt_head, &gpt_pte) != 1) { - printf("%s: *** ERROR: Invalid Backup GPT ***\n", - __func__); - return -EINVAL; - } else { - printf("%s: *** Using Backup GPT ***\n", - __func__); - } - } + if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1) + return -EINVAL; guid_bin = gpt_head->disk_guid.b; uuid_bin_to_str(guid_bin, guid, UUID_STR_FORMAT_GUID); + /* Remember to free pte */ + free(gpt_pte); return 0; } @@ -222,19 +224,8 @@ void part_print_efi(struct blk_desc *dev_desc) unsigned char *uuid_bin; /* This function validates AND fills in the GPT header and PTE */ - if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, - gpt_head, &gpt_pte) != 1) { - printf("%s: *** ERROR: Invalid GPT ***\n", __func__); - if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), - gpt_head, &gpt_pte) != 1) { - printf("%s: *** ERROR: Invalid Backup GPT ***\n", - __func__); - return; - } else { - printf("%s: *** Using Backup GPT ***\n", - __func__); - } - } + if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1) + return; debug("%s: gpt-entry at %p\n", __func__, gpt_pte); @@ -271,7 +262,7 @@ void part_print_efi(struct blk_desc *dev_desc) } int part_get_info_efi(struct blk_desc *dev_desc, int part, - disk_partition_t *info) + struct disk_partition *info) { ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); gpt_entry *gpt_pte = NULL; @@ -283,19 +274,8 @@ int part_get_info_efi(struct blk_desc *dev_desc, int part, } /* This function validates AND fills in the GPT header and PTE */ - if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, - gpt_head, &gpt_pte) != 1) { - printf("%s: *** ERROR: Invalid GPT ***\n", __func__); - if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), - gpt_head, &gpt_pte) != 1) { - printf("%s: *** ERROR: Invalid Backup GPT ***\n", - __func__); - return -1; - } else { - printf("%s: *** Using Backup GPT ***\n", - __func__); - } - } + if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1) + return -1; if (part > le32_to_cpu(gpt_head->num_partition_entries) || !is_pte_valid(&gpt_pte[part - 1])) { @@ -312,10 +292,10 @@ int part_get_info_efi(struct blk_desc *dev_desc, int part, - info->start; info->blksz = dev_desc->blksz; - sprintf((char *)info->name, "%s", - print_efiname(&gpt_pte[part - 1])); + snprintf((char *)info->name, sizeof(info->name), "%s", + print_efiname(&gpt_pte[part - 1])); strcpy((char *)info->type, "U-Boot"); - info->bootable = is_bootable(&gpt_pte[part - 1]); + info->bootable = get_bootable(&gpt_pte[part - 1]); #if CONFIG_IS_ENABLED(PARTITION_UUIDS) uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid, UUID_STR_FORMAT_GUID); @@ -436,7 +416,7 @@ int write_gpt_table(struct blk_desc *dev_desc, int gpt_fill_pte(struct blk_desc *dev_desc, gpt_header *gpt_h, gpt_entry *gpt_e, - disk_partition_t *partitions, int parts) + struct disk_partition *partitions, int parts) { lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba); lbaint_t last_usable_lba = (lbaint_t) @@ -530,7 +510,7 @@ int gpt_fill_pte(struct blk_desc *dev_desc, memset(&gpt_e[i].attributes, 0, sizeof(gpt_entry_attributes)); - if (partitions[i].bootable) + if (partitions[i].bootable & PART_BOOTABLE) gpt_e[i].attributes.fields.legacy_bios_bootable = 1; /* partition name */ @@ -604,7 +584,7 @@ static uint32_t partition_entries_offset(struct blk_desc *dev_desc) int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h, char *str_guid, int parts_count) { - gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE); + gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE_UBOOT); gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1); gpt_h->header_size = cpu_to_le32(sizeof(gpt_header)); gpt_h->my_lba = cpu_to_le64(1); @@ -626,7 +606,7 @@ int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h, } int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid, - disk_partition_t *partitions, int parts_count) + struct disk_partition *partitions, int parts_count) { gpt_header *gpt_h; gpt_entry *gpt_e; @@ -669,9 +649,18 @@ err: return ret; } -static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n) +/** + * gpt_convert_efi_name_to_char() - convert u16 string to char string + * + * TODO: this conversion only supports ANSI characters + * + * @s: target buffer + * @es: u16 string to be converted + * @n: size of target buffer + */ +static void gpt_convert_efi_name_to_char(char *s, void *es, int n) { - char *ess = (char *)es; + char *ess = es; int i, j; memset(s, '\0', n); @@ -697,6 +686,10 @@ int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head, __func__); return -1; } + + /* Free pte before allocating again */ + free(*gpt_pte); + if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), gpt_head, gpt_pte) != 1) { printf("%s: *** ERROR: Invalid Backup GPT ***\n", @@ -708,7 +701,7 @@ int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head, } int gpt_verify_partitions(struct blk_desc *dev_desc, - disk_partition_t *partitions, int parts, + struct disk_partition *partitions, int parts, gpt_header *gpt_head, gpt_entry **gpt_pte) { char efi_str[PARTNAME_SZ + 1]; @@ -925,7 +918,7 @@ static int is_pmbr_valid(legacy_mbr * mbr) * gpt is a GPT header ptr, filled on return. * ptes is a PTEs ptr, filled on return. * - * Description: returns 1 if valid, 0 on error. + * Description: returns 1 if valid, 0 on error, 2 if ignored header * If valid, returns pointers to PTEs. */ static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, @@ -951,6 +944,12 @@ static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, return 0; } + /* Invalid but nothing to yell about. */ + if (le64_to_cpu(pgpt_head->signature) == GPT_HEADER_CHROMEOS_IGNORE) { + debug("ChromeOS 'IGNOREME' GPT header found and ignored\n"); + return 2; + } + if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba)) return 0; @@ -982,6 +981,40 @@ static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, return 1; } +/** + * find_valid_gpt() - finds a valid GPT header and PTEs + * + * gpt is a GPT header ptr, filled on return. + * ptes is a PTEs ptr, filled on return. + * + * Description: returns 1 if found a valid gpt, 0 on error. + * If valid, returns pointers to PTEs. + */ +static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head, + gpt_entry **pgpt_pte) +{ + int r; + + r = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_head, + pgpt_pte); + + if (r != 1) { + if (r != 2) + printf("%s: *** ERROR: Invalid GPT ***\n", __func__); + + if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), gpt_head, + pgpt_pte) != 1) { + printf("%s: *** ERROR: Invalid Backup GPT ***\n", + __func__); + return 0; + } + if (r != 2) + printf("%s: *** Using Backup GPT ***\n", + __func__); + } + return 1; +} + /** * alloc_read_gpt_entries(): reads partition entries from disk * @dev_desc