Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 RuggedCom, Inc. |
| 4 | * Richard Retanubun <RichardRetanubun@RuggedCom.com> |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 8 | * NOTE: |
| 9 | * when CONFIG_SYS_64BIT_LBA is not defined, lbaint_t is 32 bits; this |
Heinrich Schuchardt | 643b5e7 | 2020-09-17 17:57:21 +0200 | [diff] [blame] | 10 | * limits the maximum size of addressable storage to < 2 tebibytes |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 11 | */ |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 12 | #include <common.h> |
| 13 | #include <blk.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 15 | #include <part.h> |
Simon Glass | ba06b3c | 2020-05-10 11:39:52 -0600 | [diff] [blame] | 16 | #include <uuid.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 17 | #include <asm/cache.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Marc Dietrich | 8faefad | 2013-03-29 07:57:10 +0000 | [diff] [blame] | 19 | #include <asm/unaligned.h> |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 20 | #include <command.h> |
Philipp Tomsich | 02e4353 | 2017-03-01 21:10:39 +0100 | [diff] [blame] | 21 | #include <fdtdec.h> |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 22 | #include <ide.h> |
| 23 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 24 | #include <memalign.h> |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 25 | #include <part_efi.h> |
Simon Glass | 7de8bd0 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 26 | #include <dm/ofnode.h> |
Philipp Tomsich | 02e4353 | 2017-03-01 21:10:39 +0100 | [diff] [blame] | 27 | #include <linux/compiler.h> |
Lei Wen | 6eecc03 | 2011-09-07 18:11:19 +0000 | [diff] [blame] | 28 | #include <linux/ctype.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 29 | #include <u-boot/crc.h> |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 30 | #ifdef CONFIG_AMLOGIC_MODIFY |
| 31 | #include <amlogic/emmc_partitions.h> |
| 32 | #endif |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 33 | |
Simon Glass | a594b41 | 2021-07-02 12:36:14 -0600 | [diff] [blame] | 34 | /* GUID for basic data partitons */ |
| 35 | #if CONFIG_IS_ENABLED(EFI_PARTITION) |
| 36 | static const efi_guid_t partition_basic_data_guid = PARTITION_BASIC_DATA_GUID; |
| 37 | #endif |
| 38 | |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 39 | /** |
| 40 | * efi_crc32() - EFI version of crc32 function |
| 41 | * @buf: buffer to calculate crc32 of |
| 42 | * @len - length of buf |
| 43 | * |
| 44 | * Description: Returns EFI-style CRC32 value for @buf |
| 45 | */ |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 46 | static inline u32 efi_crc32(const void *buf, u32 len) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 47 | { |
| 48 | return crc32(0, buf, len); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Private function prototypes |
| 53 | */ |
| 54 | |
| 55 | static int pmbr_part_valid(struct partition *part); |
| 56 | static int is_pmbr_valid(legacy_mbr * mbr); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 57 | #ifdef CONFIG_AMLOGIC_MODIFY |
| 58 | int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, |
| 59 | gpt_header *pgpt_head, gpt_entry **pgpt_pte); |
| 60 | #else |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 61 | static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 62 | gpt_header *pgpt_head, gpt_entry **pgpt_pte); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 63 | #endif |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 64 | static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc, |
| 65 | gpt_header *pgpt_head); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 66 | #ifdef CONFIG_AMLOGIC_MODIFY |
| 67 | int is_pte_valid(gpt_entry * pte); |
| 68 | #else |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 69 | static int is_pte_valid(gpt_entry * pte); |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 70 | #endif |
Urja Rannikko | 20c568c | 2019-04-11 20:27:50 +0000 | [diff] [blame] | 71 | static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head, |
| 72 | gpt_entry **pgpt_pte); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 73 | |
Lei Wen | 6eecc03 | 2011-09-07 18:11:19 +0000 | [diff] [blame] | 74 | static char *print_efiname(gpt_entry *pte) |
| 75 | { |
| 76 | static char name[PARTNAME_SZ + 1]; |
| 77 | int i; |
| 78 | for (i = 0; i < PARTNAME_SZ; i++) { |
| 79 | u8 c; |
| 80 | c = pte->partition_name[i] & 0xff; |
| 81 | c = (c && !isprint(c)) ? '.' : c; |
| 82 | name[i] = c; |
| 83 | } |
| 84 | name[PARTNAME_SZ] = 0; |
| 85 | return name; |
| 86 | } |
| 87 | |
Heinrich Schuchardt | 7de24b2 | 2019-01-12 11:25:25 +0100 | [diff] [blame] | 88 | static const efi_guid_t system_guid = PARTITION_SYSTEM_GUID; |
Stephen Warren | b4414f4 | 2012-10-08 08:14:37 +0000 | [diff] [blame] | 89 | |
Heinrich Schuchardt | 25801ac | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 90 | static int get_bootable(gpt_entry *p) |
Stephen Warren | b4414f4 | 2012-10-08 08:14:37 +0000 | [diff] [blame] | 91 | { |
Heinrich Schuchardt | 25801ac | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 92 | int ret = 0; |
| 93 | |
| 94 | if (!memcmp(&p->partition_type_guid, &system_guid, sizeof(efi_guid_t))) |
| 95 | ret |= PART_EFI_SYSTEM_PARTITION; |
| 96 | if (p->attributes.fields.legacy_bios_bootable) |
| 97 | ret |= PART_BOOTABLE; |
| 98 | return ret; |
Stephen Warren | b4414f4 | 2012-10-08 08:14:37 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 101 | static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba, |
| 102 | lbaint_t lastlba) |
| 103 | { |
| 104 | uint32_t crc32_backup = 0; |
| 105 | uint32_t calc_crc32; |
| 106 | |
| 107 | /* Check the GPT header signature */ |
Simon Glass | 67b9052 | 2018-10-01 12:22:35 -0600 | [diff] [blame] | 108 | if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE_UBOOT) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 109 | log_debug("%s signature is wrong: %#llX != %#llX\n", |
| 110 | "GUID Partition Table Header", |
| 111 | le64_to_cpu(gpt_h->signature), |
| 112 | GPT_HEADER_SIGNATURE_UBOOT); |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | /* Check the GUID Partition Table CRC */ |
| 117 | memcpy(&crc32_backup, &gpt_h->header_crc32, sizeof(crc32_backup)); |
| 118 | memset(&gpt_h->header_crc32, 0, sizeof(gpt_h->header_crc32)); |
| 119 | |
| 120 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, |
| 121 | le32_to_cpu(gpt_h->header_size)); |
| 122 | |
| 123 | memcpy(&gpt_h->header_crc32, &crc32_backup, sizeof(crc32_backup)); |
| 124 | |
| 125 | if (calc_crc32 != le32_to_cpu(crc32_backup)) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 126 | log_debug("%s: CRC is wrong: %#x != %#x\n", |
| 127 | "GUID Partition Table Header", |
| 128 | le32_to_cpu(crc32_backup), calc_crc32); |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Check that the my_lba entry points to the LBA that contains the GPT |
| 134 | */ |
| 135 | if (le64_to_cpu(gpt_h->my_lba) != lba) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 136 | log_debug("GPT: my_lba incorrect: %llX != " LBAF "\n", |
| 137 | le64_to_cpu(gpt_h->my_lba), lba); |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Check that the first_usable_lba and that the last_usable_lba are |
| 143 | * within the disk. |
| 144 | */ |
| 145 | if (le64_to_cpu(gpt_h->first_usable_lba) > lastlba) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 146 | log_debug("GPT: first_usable_lba incorrect: %llX > " LBAF "\n", |
| 147 | le64_to_cpu(gpt_h->first_usable_lba), lastlba); |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 148 | return -1; |
| 149 | } |
Xindong Xu | d944142 | 2024-01-18 10:20:45 +0800 | [diff] [blame] | 150 | |
| 151 | #ifndef CONFIG_AMLOGIC_MODIFY |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 152 | if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 153 | log_debug("GPT: last_usable_lba incorrect: %llX > " LBAF "\n", |
| 154 | le64_to_cpu(gpt_h->last_usable_lba), lastlba); |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 155 | return -1; |
| 156 | } |
Xindong Xu | d944142 | 2024-01-18 10:20:45 +0800 | [diff] [blame] | 157 | #endif |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 158 | |
| 159 | debug("GPT: first_usable_lba: %llX last_usable_lba: %llX last lba: " |
| 160 | LBAF "\n", le64_to_cpu(gpt_h->first_usable_lba), |
| 161 | le64_to_cpu(gpt_h->last_usable_lba), lastlba); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e) |
| 167 | { |
| 168 | uint32_t calc_crc32; |
| 169 | |
| 170 | /* Check the GUID Partition Table Entry Array CRC */ |
| 171 | calc_crc32 = efi_crc32((const unsigned char *)gpt_e, |
| 172 | le32_to_cpu(gpt_h->num_partition_entries) * |
| 173 | le32_to_cpu(gpt_h->sizeof_partition_entry)); |
| 174 | |
| 175 | if (calc_crc32 != le32_to_cpu(gpt_h->partition_entry_array_crc32)) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 176 | log_debug("%s: %#x != %#x\n", |
| 177 | "GUID Partition Table Entry Array CRC is wrong", |
| 178 | le32_to_cpu(gpt_h->partition_entry_array_crc32), |
| 179 | calc_crc32); |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static void prepare_backup_gpt_header(gpt_header *gpt_h) |
| 187 | { |
| 188 | uint32_t calc_crc32; |
| 189 | uint64_t val; |
| 190 | |
| 191 | /* recalculate the values for the Backup GPT Header */ |
| 192 | val = le64_to_cpu(gpt_h->my_lba); |
| 193 | gpt_h->my_lba = gpt_h->alternate_lba; |
| 194 | gpt_h->alternate_lba = cpu_to_le64(val); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 195 | gpt_h->partition_entry_lba = |
| 196 | cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1); |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 197 | gpt_h->header_crc32 = 0; |
| 198 | |
| 199 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, |
| 200 | le32_to_cpu(gpt_h->header_size)); |
| 201 | gpt_h->header_crc32 = cpu_to_le32(calc_crc32); |
| 202 | } |
| 203 | |
Patrick Delaunay | bd42a94 | 2017-01-27 11:00:41 +0100 | [diff] [blame] | 204 | #if CONFIG_IS_ENABLED(EFI_PARTITION) |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 205 | /* |
| 206 | * Public Functions (include/part.h) |
| 207 | */ |
| 208 | |
Alison Chaiken | 73d6d18 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 209 | /* |
| 210 | * UUID is displayed as 32 hexadecimal digits, in 5 groups, |
| 211 | * separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters |
| 212 | */ |
| 213 | int get_disk_guid(struct blk_desc * dev_desc, char *guid) |
| 214 | { |
| 215 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); |
| 216 | gpt_entry *gpt_pte = NULL; |
| 217 | unsigned char *guid_bin; |
| 218 | |
| 219 | /* This function validates AND fills in the GPT header and PTE */ |
Urja Rannikko | 20c568c | 2019-04-11 20:27:50 +0000 | [diff] [blame] | 220 | if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1) |
| 221 | return -EINVAL; |
Alison Chaiken | 73d6d18 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 222 | |
| 223 | guid_bin = gpt_head->disk_guid.b; |
| 224 | uuid_bin_to_str(guid_bin, guid, UUID_STR_FORMAT_GUID); |
| 225 | |
Eugeniu Rosca | 1cfe969 | 2019-04-30 04:53:44 +0200 | [diff] [blame] | 226 | /* Remember to free pte */ |
| 227 | free(gpt_pte); |
Alison Chaiken | 73d6d18 | 2017-06-25 16:43:23 -0700 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Simon Glass | 084bf4c | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 231 | void part_print_efi(struct blk_desc *dev_desc) |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 232 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 233 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 234 | gpt_entry *gpt_pte = NULL; |
| 235 | int i = 0; |
Heinrich Schuchardt | a4492ee | 2022-01-16 12:23:19 +0100 | [diff] [blame] | 236 | unsigned char *uuid; |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 237 | |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 238 | /* This function validates AND fills in the GPT header and PTE */ |
Urja Rannikko | 20c568c | 2019-04-11 20:27:50 +0000 | [diff] [blame] | 239 | if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1) |
| 240 | return; |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 241 | |
| 242 | debug("%s: gpt-entry at %p\n", __func__, gpt_pte); |
| 243 | |
| 244 | printf("Part\tStart LBA\tEnd LBA\t\tName\n"); |
Stephen Warren | 13bf2f5 | 2012-10-08 08:14:36 +0000 | [diff] [blame] | 245 | printf("\tAttributes\n"); |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 246 | printf("\tType GUID\n"); |
| 247 | printf("\tPartition GUID\n"); |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 248 | |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 249 | for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) { |
Heinrich Schuchardt | 504dbd2 | 2022-01-11 15:43:05 +0100 | [diff] [blame] | 250 | /* Skip invalid PTE */ |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 251 | if (!is_pte_valid(&gpt_pte[i])) |
Heinrich Schuchardt | 504dbd2 | 2022-01-11 15:43:05 +0100 | [diff] [blame] | 252 | continue; |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 253 | |
| 254 | printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1), |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 255 | le64_to_cpu(gpt_pte[i].starting_lba), |
| 256 | le64_to_cpu(gpt_pte[i].ending_lba), |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 257 | print_efiname(&gpt_pte[i])); |
Stephen Warren | 13bf2f5 | 2012-10-08 08:14:36 +0000 | [diff] [blame] | 258 | printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw); |
Heinrich Schuchardt | a4492ee | 2022-01-16 12:23:19 +0100 | [diff] [blame] | 259 | uuid = (unsigned char *)gpt_pte[i].partition_type_guid.b; |
| 260 | if (CONFIG_IS_ENABLED(PARTITION_TYPE_GUID)) |
| 261 | printf("\ttype:\t%pUl\n\t\t(%pUs)\n", uuid, uuid); |
| 262 | else |
| 263 | printf("\ttype:\t%pUl\n", uuid); |
| 264 | uuid = (unsigned char *)gpt_pte[i].unique_partition_guid.b; |
| 265 | printf("\tguid:\t%pUl\n", uuid); |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* Remember to free pte */ |
| 269 | free(gpt_pte); |
| 270 | return; |
| 271 | } |
Stephen Warren | 894bfbb | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 272 | |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 273 | int part_get_info_efi(struct blk_desc *dev_desc, int part, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 274 | struct disk_partition *info) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 275 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 276 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); |
Doug Anderson | deb5ca8 | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 277 | gpt_entry *gpt_pte = NULL; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 278 | |
| 279 | /* "part" argument must be at least 1 */ |
Simon Glass | 4708a07 | 2016-03-16 07:45:36 -0600 | [diff] [blame] | 280 | if (part < 1) { |
Simon Glass | f337fb9 | 2022-10-11 09:47:11 -0600 | [diff] [blame] | 281 | log_debug("Invalid Argument(s)\n"); |
| 282 | return -EINVAL; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* This function validates AND fills in the GPT header and PTE */ |
Urja Rannikko | 20c568c | 2019-04-11 20:27:50 +0000 | [diff] [blame] | 286 | if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1) |
Simon Glass | f337fb9 | 2022-10-11 09:47:11 -0600 | [diff] [blame] | 287 | return -EINVAL; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 288 | |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 289 | if (part > le32_to_cpu(gpt_head->num_partition_entries) || |
Stephen Warren | c04d68c | 2012-09-21 09:50:58 +0000 | [diff] [blame] | 290 | !is_pte_valid(&gpt_pte[part - 1])) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 291 | log_debug("Invalid partition number %d\n", part); |
Mark Langsdorf | 6d2ee5a | 2013-09-10 15:14:56 -0500 | [diff] [blame] | 292 | free(gpt_pte); |
Simon Glass | f337fb9 | 2022-10-11 09:47:11 -0600 | [diff] [blame] | 293 | return -EPERM; |
Stephen Warren | c04d68c | 2012-09-21 09:50:58 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 296 | /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */ |
| 297 | info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba); |
Richard Retanubun | 5097083 | 2009-01-26 08:45:14 -0500 | [diff] [blame] | 298 | /* The ending LBA is inclusive, to calculate size, add 1 to it */ |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 299 | info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1 |
Richard Retanubun | 5097083 | 2009-01-26 08:45:14 -0500 | [diff] [blame] | 300 | - info->start; |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 301 | info->blksz = dev_desc->blksz; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 302 | |
Heinrich Schuchardt | 5375ee5 | 2019-07-05 21:27:13 +0200 | [diff] [blame] | 303 | snprintf((char *)info->name, sizeof(info->name), "%s", |
| 304 | print_efiname(&gpt_pte[part - 1])); |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 305 | strcpy((char *)info->type, "U-Boot"); |
Heinrich Schuchardt | 25801ac | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 306 | info->bootable = get_bootable(&gpt_pte[part - 1]); |
Patrick Delaunay | b331cd6 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 307 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 308 | uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid, |
| 309 | UUID_STR_FORMAT_GUID); |
Stephen Warren | 894bfbb | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 310 | #endif |
Patrick Delaunay | 7561b25 | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 311 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 312 | uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b, |
| 313 | info->type_guid, UUID_STR_FORMAT_GUID); |
| 314 | #endif |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 315 | |
Simon Glass | f337fb9 | 2022-10-11 09:47:11 -0600 | [diff] [blame] | 316 | log_debug("start 0x" LBAF ", size 0x" LBAF ", name %s\n", info->start, |
| 317 | info->size, info->name); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 318 | |
| 319 | /* Remember to free pte */ |
Doug Anderson | deb5ca8 | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 320 | free(gpt_pte); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
Simon Glass | 084bf4c | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 324 | static int part_test_efi(struct blk_desc *dev_desc) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 325 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 326 | ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 327 | |
| 328 | /* Read legacy MBR from block 0 and validate it */ |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 329 | if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1) |
Anton staaf | f75dd58 | 2011-10-12 13:56:04 +0000 | [diff] [blame] | 330 | || (is_pmbr_valid(legacymbr) != 1)) { |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 331 | return -1; |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 336 | /** |
| 337 | * set_protective_mbr(): Set the EFI protective MBR |
| 338 | * @param dev_desc - block device descriptor |
| 339 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 340 | * Return: - zero on success, otherwise error |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 341 | */ |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 342 | static int set_protective_mbr(struct blk_desc *dev_desc) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 343 | { |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 344 | /* Setup the Protective MBR */ |
Patrick Delaunay | 3cc5661 | 2017-11-17 10:08:18 +0100 | [diff] [blame] | 345 | ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, p_mbr, 1, dev_desc->blksz); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 346 | if (p_mbr == NULL) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 347 | log_debug("calloc failed!\n"); |
| 348 | return -ENOMEM; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 349 | } |
Vincent Tinelli | e163a93 | 2017-01-30 15:46:07 +0300 | [diff] [blame] | 350 | |
| 351 | /* Read MBR to backup boot code if it exists */ |
| 352 | if (blk_dread(dev_desc, 0, 1, p_mbr) != 1) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 353 | log_debug("** Can't read from device %d **\n", |
| 354 | dev_desc->devnum); |
| 355 | return -EIO; |
Vincent Tinelli | e163a93 | 2017-01-30 15:46:07 +0300 | [diff] [blame] | 356 | } |
| 357 | |
Sam Protsenko | 955575c | 2018-05-22 02:04:21 +0300 | [diff] [blame] | 358 | /* Clear all data in MBR except of backed up boot code */ |
| 359 | memset((char *)p_mbr + MSDOS_MBR_BOOT_CODE_SIZE, 0, sizeof(*p_mbr) - |
| 360 | MSDOS_MBR_BOOT_CODE_SIZE); |
| 361 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 362 | /* Append signature */ |
| 363 | p_mbr->signature = MSDOS_MBR_SIGNATURE; |
| 364 | p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT; |
| 365 | p_mbr->partition_record[0].start_sect = 1; |
Maxime Ripard | b349abb | 2015-01-08 12:26:44 +0100 | [diff] [blame] | 366 | p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 367 | |
| 368 | /* Write MBR sector to the MMC device */ |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 369 | if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 370 | log_debug("** Can't write to device %d **\n", dev_desc->devnum); |
| 371 | return -EIO; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 372 | } |
| 373 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 377 | int write_gpt_table(struct blk_desc *dev_desc, |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 378 | gpt_header *gpt_h, gpt_entry *gpt_e) |
| 379 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 380 | const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries |
| 381 | * sizeof(gpt_entry)), dev_desc); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 382 | u32 calc_crc32; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 383 | |
| 384 | debug("max lba: %x\n", (u32) dev_desc->lba); |
| 385 | /* Setup the Protective MBR */ |
| 386 | if (set_protective_mbr(dev_desc) < 0) |
| 387 | goto err; |
| 388 | |
| 389 | /* Generate CRC for the Primary GPT Header */ |
| 390 | calc_crc32 = efi_crc32((const unsigned char *)gpt_e, |
| 391 | le32_to_cpu(gpt_h->num_partition_entries) * |
| 392 | le32_to_cpu(gpt_h->sizeof_partition_entry)); |
| 393 | gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32); |
| 394 | |
| 395 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, |
| 396 | le32_to_cpu(gpt_h->header_size)); |
| 397 | gpt_h->header_crc32 = cpu_to_le32(calc_crc32); |
| 398 | |
| 399 | /* Write the First GPT to the block right after the Legacy MBR */ |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 400 | if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 401 | goto err; |
| 402 | |
Philipp Tomsich | 02e4353 | 2017-03-01 21:10:39 +0100 | [diff] [blame] | 403 | if (blk_dwrite(dev_desc, le64_to_cpu(gpt_h->partition_entry_lba), |
| 404 | pte_blk_cnt, gpt_e) != pte_blk_cnt) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 405 | goto err; |
| 406 | |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 407 | prepare_backup_gpt_header(gpt_h); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 408 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 409 | if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba) |
| 410 | + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 411 | goto err; |
| 412 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 413 | if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1, |
| 414 | gpt_h) != 1) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 415 | goto err; |
| 416 | |
| 417 | debug("GPT successfully written to block device!\n"); |
| 418 | return 0; |
| 419 | |
| 420 | err: |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 421 | log_debug("** Can't write to device %d **\n", dev_desc->devnum); |
| 422 | return -EIO; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Maxime Ripard | 47d7ee4 | 2017-08-23 16:01:32 +0200 | [diff] [blame] | 425 | int gpt_fill_pte(struct blk_desc *dev_desc, |
| 426 | gpt_header *gpt_h, gpt_entry *gpt_e, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 427 | struct disk_partition *partitions, int parts) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 428 | { |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 429 | lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba); |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 430 | lbaint_t last_usable_lba = (lbaint_t) |
| 431 | le64_to_cpu(gpt_h->last_usable_lba); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 432 | int i, k; |
Marek Vasut | 67cd4a6 | 2013-05-19 12:53:34 +0000 | [diff] [blame] | 433 | size_t efiname_len, dosname_len; |
Patrick Delaunay | b331cd6 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 434 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 435 | char *str_uuid; |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 436 | unsigned char *bin_uuid; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 437 | #endif |
Patrick Delaunay | 7561b25 | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 438 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 439 | char *str_type_guid; |
| 440 | unsigned char *bin_type_guid; |
| 441 | #endif |
Maxime Ripard | 79c5912 | 2017-08-23 16:01:33 +0200 | [diff] [blame] | 442 | size_t hdr_start = gpt_h->my_lba; |
| 443 | size_t hdr_end = hdr_start + 1; |
| 444 | |
| 445 | size_t pte_start = gpt_h->partition_entry_lba; |
| 446 | size_t pte_end = pte_start + |
| 447 | gpt_h->num_partition_entries * gpt_h->sizeof_partition_entry / |
| 448 | dev_desc->blksz; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 449 | |
| 450 | for (i = 0; i < parts; i++) { |
| 451 | /* partition starting lba */ |
Maxime Ripard | 5276e8b | 2017-08-23 16:01:31 +0200 | [diff] [blame] | 452 | lbaint_t start = partitions[i].start; |
| 453 | lbaint_t size = partitions[i].size; |
| 454 | |
Maxime Ripard | 79c5912 | 2017-08-23 16:01:33 +0200 | [diff] [blame] | 455 | if (start) { |
| 456 | offset = start + size; |
| 457 | } else { |
| 458 | start = offset; |
| 459 | offset += size; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * If our partition overlaps with either the GPT |
| 464 | * header, or the partition entry, reject it. |
| 465 | */ |
Patrick Delaunay | ae0e022 | 2017-10-18 15:11:05 +0200 | [diff] [blame] | 466 | if (((start < hdr_end && hdr_start < (start + size)) || |
| 467 | (start < pte_end && pte_start < (start + size)))) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 468 | log_debug("Partition overlap\n"); |
| 469 | return -ENOSPC; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 470 | } |
Maxime Ripard | 5276e8b | 2017-08-23 16:01:31 +0200 | [diff] [blame] | 471 | |
Maxime Ripard | 79c5912 | 2017-08-23 16:01:33 +0200 | [diff] [blame] | 472 | gpt_e[i].starting_lba = cpu_to_le64(start); |
| 473 | |
Patrick Delaunay | a565386 | 2016-05-02 14:43:33 +0200 | [diff] [blame] | 474 | if (offset > (last_usable_lba + 1)) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 475 | log_debug("Partitions layout exceeds disk size\n"); |
| 476 | return -E2BIG; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 477 | } |
| 478 | /* partition ending lba */ |
Maxime Ripard | 5276e8b | 2017-08-23 16:01:31 +0200 | [diff] [blame] | 479 | if ((i == parts - 1) && (size == 0)) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 480 | /* extend the last partition to maximuim */ |
| 481 | gpt_e[i].ending_lba = gpt_h->last_usable_lba; |
| 482 | else |
| 483 | gpt_e[i].ending_lba = cpu_to_le64(offset - 1); |
| 484 | |
Patrick Delaunay | 7561b25 | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 485 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 486 | str_type_guid = partitions[i].type_guid; |
| 487 | bin_type_guid = gpt_e[i].partition_type_guid.b; |
| 488 | if (strlen(str_type_guid)) { |
| 489 | if (uuid_str_to_bin(str_type_guid, bin_type_guid, |
| 490 | UUID_STR_FORMAT_GUID)) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 491 | log_debug("Partition no. %d: invalid type guid: %s\n", |
| 492 | i, str_type_guid); |
| 493 | return -EINVAL; |
Patrick Delaunay | 7561b25 | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 494 | } |
| 495 | } else { |
| 496 | /* default partition type GUID */ |
| 497 | memcpy(bin_type_guid, |
Heinrich Schuchardt | 44ab2d3 | 2018-06-09 17:50:18 +0200 | [diff] [blame] | 498 | &partition_basic_data_guid, 16); |
Patrick Delaunay | 7561b25 | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 499 | } |
| 500 | #else |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 501 | /* partition type GUID */ |
| 502 | memcpy(gpt_e[i].partition_type_guid.b, |
Heinrich Schuchardt | 44ab2d3 | 2018-06-09 17:50:18 +0200 | [diff] [blame] | 503 | &partition_basic_data_guid, 16); |
Patrick Delaunay | 7561b25 | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 504 | #endif |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 505 | |
Patrick Delaunay | b331cd6 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 506 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 507 | str_uuid = partitions[i].uuid; |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 508 | bin_uuid = gpt_e[i].unique_partition_guid.b; |
| 509 | |
Vincent Tinelli | 9da52f8 | 2017-02-27 16:11:15 +0200 | [diff] [blame] | 510 | if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_GUID)) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 511 | log_debug("Partition no. %d: invalid guid: %s\n", |
| 512 | i, str_uuid); |
| 513 | return -EINVAL; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 514 | } |
| 515 | #endif |
| 516 | |
| 517 | /* partition attributes */ |
| 518 | memset(&gpt_e[i].attributes, 0, |
| 519 | sizeof(gpt_entry_attributes)); |
| 520 | |
Heinrich Schuchardt | 25801ac | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 521 | if (partitions[i].bootable & PART_BOOTABLE) |
Patrick Delaunay | cfdaf4c | 2015-11-17 11:36:52 +0100 | [diff] [blame] | 522 | gpt_e[i].attributes.fields.legacy_bios_bootable = 1; |
| 523 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 524 | /* partition name */ |
Marek Vasut | 67cd4a6 | 2013-05-19 12:53:34 +0000 | [diff] [blame] | 525 | efiname_len = sizeof(gpt_e[i].partition_name) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 526 | / sizeof(efi_char16_t); |
Marek Vasut | 67cd4a6 | 2013-05-19 12:53:34 +0000 | [diff] [blame] | 527 | dosname_len = sizeof(partitions[i].name); |
| 528 | |
| 529 | memset(gpt_e[i].partition_name, 0, |
| 530 | sizeof(gpt_e[i].partition_name)); |
| 531 | |
| 532 | for (k = 0; k < min(dosname_len, efiname_len); k++) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 533 | gpt_e[i].partition_name[k] = |
| 534 | (efi_char16_t)(partitions[i].name[k]); |
| 535 | |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 536 | debug("%s: name: %s offset[%d]: 0x" LBAF |
| 537 | " size[%d]: 0x" LBAF "\n", |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 538 | __func__, partitions[i].name, i, |
Maxime Ripard | 5276e8b | 2017-08-23 16:01:31 +0200 | [diff] [blame] | 539 | offset, i, size); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Philipp Tomsich | 02e4353 | 2017-03-01 21:10:39 +0100 | [diff] [blame] | 545 | static uint32_t partition_entries_offset(struct blk_desc *dev_desc) |
| 546 | { |
| 547 | uint32_t offset_blks = 2; |
Maxime Ripard | 89d33a2 | 2017-08-23 16:01:30 +0200 | [diff] [blame] | 548 | uint32_t __maybe_unused offset_bytes; |
Philipp Tomsich | 02e4353 | 2017-03-01 21:10:39 +0100 | [diff] [blame] | 549 | int __maybe_unused config_offset; |
| 550 | |
| 551 | #if defined(CONFIG_EFI_PARTITION_ENTRIES_OFF) |
| 552 | /* |
| 553 | * Some architectures require their SPL loader at a fixed |
| 554 | * address within the first 16KB of the disk. To avoid an |
| 555 | * overlap with the partition entries of the EFI partition |
| 556 | * table, the first safe offset (in bytes, from the start of |
| 557 | * the disk) for the entries can be set in |
| 558 | * CONFIG_EFI_PARTITION_ENTRIES_OFF. |
| 559 | */ |
Maxime Ripard | 89d33a2 | 2017-08-23 16:01:30 +0200 | [diff] [blame] | 560 | offset_bytes = |
Philipp Tomsich | 02e4353 | 2017-03-01 21:10:39 +0100 | [diff] [blame] | 561 | PAD_TO_BLOCKSIZE(CONFIG_EFI_PARTITION_ENTRIES_OFF, dev_desc); |
Maxime Ripard | 89d33a2 | 2017-08-23 16:01:30 +0200 | [diff] [blame] | 562 | offset_blks = offset_bytes / dev_desc->blksz; |
Philipp Tomsich | 02e4353 | 2017-03-01 21:10:39 +0100 | [diff] [blame] | 563 | #endif |
| 564 | |
| 565 | #if defined(CONFIG_OF_CONTROL) |
| 566 | /* |
| 567 | * Allow the offset of the first partition entires (in bytes |
| 568 | * from the start of the device) to be specified as a property |
| 569 | * of the device tree '/config' node. |
| 570 | */ |
Simon Glass | 7de8bd0 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 571 | config_offset = ofnode_conf_read_int( |
| 572 | "u-boot,efi-partition-entries-offset", -EINVAL); |
Maxime Ripard | 89d33a2 | 2017-08-23 16:01:30 +0200 | [diff] [blame] | 573 | if (config_offset != -EINVAL) { |
| 574 | offset_bytes = PAD_TO_BLOCKSIZE(config_offset, dev_desc); |
| 575 | offset_blks = offset_bytes / dev_desc->blksz; |
| 576 | } |
Philipp Tomsich | 02e4353 | 2017-03-01 21:10:39 +0100 | [diff] [blame] | 577 | #endif |
| 578 | |
| 579 | debug("efi: partition entries offset (in blocks): %d\n", offset_blks); |
| 580 | |
| 581 | /* |
| 582 | * The earliest LBA this can be at is LBA#2 (i.e. right behind |
| 583 | * the (protective) MBR and the GPT header. |
| 584 | */ |
| 585 | if (offset_blks < 2) |
| 586 | offset_blks = 2; |
| 587 | |
| 588 | return offset_blks; |
| 589 | } |
| 590 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 591 | int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h, |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 592 | char *str_guid, int parts_count) |
| 593 | { |
Simon Glass | 67b9052 | 2018-10-01 12:22:35 -0600 | [diff] [blame] | 594 | gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE_UBOOT); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 595 | gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1); |
| 596 | gpt_h->header_size = cpu_to_le32(sizeof(gpt_header)); |
| 597 | gpt_h->my_lba = cpu_to_le64(1); |
| 598 | gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 599 | gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34); |
Philipp Tomsich | 02e4353 | 2017-03-01 21:10:39 +0100 | [diff] [blame] | 600 | gpt_h->partition_entry_lba = |
| 601 | cpu_to_le64(partition_entries_offset(dev_desc)); |
| 602 | gpt_h->first_usable_lba = |
| 603 | cpu_to_le64(le64_to_cpu(gpt_h->partition_entry_lba) + 32); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 604 | gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS); |
| 605 | gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry)); |
| 606 | gpt_h->header_crc32 = 0; |
| 607 | gpt_h->partition_entry_array_crc32 = 0; |
| 608 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 609 | if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID)) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 610 | return -1; |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 615 | int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 616 | struct disk_partition *partitions, int parts_count) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 617 | { |
Lukasz Majewski | bb02101 | 2017-10-27 12:28:10 +0200 | [diff] [blame] | 618 | gpt_header *gpt_h; |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 619 | gpt_entry *gpt_e; |
Lukasz Majewski | bb02101 | 2017-10-27 12:28:10 +0200 | [diff] [blame] | 620 | int ret, size; |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 621 | |
Lukasz Majewski | bb02101 | 2017-10-27 12:28:10 +0200 | [diff] [blame] | 622 | size = PAD_TO_BLOCKSIZE(sizeof(gpt_header), dev_desc); |
| 623 | gpt_h = malloc_cache_aligned(size); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 624 | if (gpt_h == NULL) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 625 | log_debug("calloc failed!\n"); |
| 626 | return -ENOMEM; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 627 | } |
Lukasz Majewski | bb02101 | 2017-10-27 12:28:10 +0200 | [diff] [blame] | 628 | memset(gpt_h, 0, size); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 629 | |
Lukasz Majewski | bb02101 | 2017-10-27 12:28:10 +0200 | [diff] [blame] | 630 | size = PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS * sizeof(gpt_entry), |
| 631 | dev_desc); |
| 632 | gpt_e = malloc_cache_aligned(size); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 633 | if (gpt_e == NULL) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 634 | log_debug("calloc failed!\n"); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 635 | free(gpt_h); |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 636 | return -ENOMEM; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 637 | } |
Lukasz Majewski | bb02101 | 2017-10-27 12:28:10 +0200 | [diff] [blame] | 638 | memset(gpt_e, 0, size); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 639 | |
| 640 | /* Generate Primary GPT header (LBA1) */ |
| 641 | ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count); |
| 642 | if (ret) |
| 643 | goto err; |
| 644 | |
| 645 | /* Generate partition entries */ |
Maxime Ripard | 47d7ee4 | 2017-08-23 16:01:32 +0200 | [diff] [blame] | 646 | ret = gpt_fill_pte(dev_desc, gpt_h, gpt_e, partitions, parts_count); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 647 | if (ret) |
| 648 | goto err; |
| 649 | |
| 650 | /* Write GPT partition table */ |
| 651 | ret = write_gpt_table(dev_desc, gpt_h, gpt_e); |
| 652 | |
| 653 | err: |
| 654 | free(gpt_e); |
| 655 | free(gpt_h); |
| 656 | return ret; |
| 657 | } |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 658 | |
Heinrich Schuchardt | 06e921b | 2019-07-14 18:12:32 +0200 | [diff] [blame] | 659 | /** |
| 660 | * gpt_convert_efi_name_to_char() - convert u16 string to char string |
| 661 | * |
| 662 | * TODO: this conversion only supports ANSI characters |
| 663 | * |
| 664 | * @s: target buffer |
| 665 | * @es: u16 string to be converted |
| 666 | * @n: size of target buffer |
| 667 | */ |
| 668 | static void gpt_convert_efi_name_to_char(char *s, void *es, int n) |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 669 | { |
Heinrich Schuchardt | 06e921b | 2019-07-14 18:12:32 +0200 | [diff] [blame] | 670 | char *ess = es; |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 671 | int i, j; |
| 672 | |
| 673 | memset(s, '\0', n); |
| 674 | |
| 675 | for (i = 0, j = 0; j < n; i += 2, j++) { |
| 676 | s[j] = ess[i]; |
| 677 | if (!ess[i]) |
| 678 | return; |
| 679 | } |
| 680 | } |
| 681 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 682 | int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head, |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 683 | gpt_entry **gpt_pte) |
| 684 | { |
| 685 | /* |
| 686 | * This function validates AND |
| 687 | * fills in the GPT header and PTE |
| 688 | */ |
| 689 | if (is_gpt_valid(dev_desc, |
| 690 | GPT_PRIMARY_PARTITION_TABLE_LBA, |
| 691 | gpt_head, gpt_pte) != 1) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 692 | log_debug("Invalid GPT\n"); |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 693 | return -1; |
| 694 | } |
Eugeniu Rosca | 716f919 | 2019-04-30 04:53:45 +0200 | [diff] [blame] | 695 | |
| 696 | /* Free pte before allocating again */ |
| 697 | free(*gpt_pte); |
| 698 | |
Stefan Herbrechtsmeier | d469338 | 2021-03-08 16:07:11 +0000 | [diff] [blame] | 699 | /* |
| 700 | * Check that the alternate_lba entry points to the last LBA |
| 701 | */ |
| 702 | if (le64_to_cpu(gpt_head->alternate_lba) != (dev_desc->lba - 1)) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 703 | log_debug("Misplaced Backup GPT\n"); |
Stefan Herbrechtsmeier | d469338 | 2021-03-08 16:07:11 +0000 | [diff] [blame] | 704 | return -1; |
| 705 | } |
| 706 | |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 707 | if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), |
| 708 | gpt_head, gpt_pte) != 1) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 709 | log_debug("Invalid Backup GPT\n"); |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 710 | return -1; |
| 711 | } |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
Philippe Reynes | 5c783b5 | 2022-04-22 17:46:48 +0200 | [diff] [blame] | 716 | static void restore_primary_gpt_header(gpt_header *gpt_h, struct blk_desc *dev_desc) |
| 717 | { |
| 718 | u32 calc_crc32; |
| 719 | u64 val; |
| 720 | |
| 721 | /* recalculate the values for the Primary GPT Header */ |
| 722 | val = le64_to_cpu(gpt_h->my_lba); |
| 723 | gpt_h->my_lba = gpt_h->alternate_lba; |
| 724 | gpt_h->alternate_lba = cpu_to_le64(val); |
| 725 | gpt_h->partition_entry_lba = cpu_to_le64(partition_entries_offset(dev_desc)); |
| 726 | |
| 727 | gpt_h->header_crc32 = 0; |
| 728 | |
| 729 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, |
| 730 | le32_to_cpu(gpt_h->header_size)); |
| 731 | gpt_h->header_crc32 = cpu_to_le32(calc_crc32); |
| 732 | } |
| 733 | |
| 734 | static int write_one_gpt_table(struct blk_desc *dev_desc, |
| 735 | gpt_header *gpt_h, gpt_entry *gpt_e) |
| 736 | { |
| 737 | const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries |
| 738 | * sizeof(gpt_entry)), dev_desc); |
| 739 | lbaint_t start; |
| 740 | int ret = 0; |
| 741 | |
| 742 | start = le64_to_cpu(gpt_h->my_lba); |
| 743 | if (blk_dwrite(dev_desc, start, 1, gpt_h) != 1) { |
| 744 | ret = -1; |
| 745 | goto out; |
| 746 | } |
| 747 | |
| 748 | start = le64_to_cpu(gpt_h->partition_entry_lba); |
| 749 | if (blk_dwrite(dev_desc, start, pte_blk_cnt, gpt_e) != pte_blk_cnt) { |
| 750 | ret = -1; |
| 751 | goto out; |
| 752 | } |
| 753 | |
| 754 | out: |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | int gpt_repair_headers(struct blk_desc *dev_desc) |
| 759 | { |
| 760 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h1, 1, dev_desc->blksz); |
| 761 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h2, 1, dev_desc->blksz); |
| 762 | gpt_entry *gpt_e1 = NULL, *gpt_e2 = NULL; |
| 763 | int is_gpt1_valid, is_gpt2_valid; |
| 764 | int ret = -1; |
| 765 | |
| 766 | is_gpt1_valid = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, |
| 767 | gpt_h1, &gpt_e1); |
| 768 | is_gpt2_valid = is_gpt_valid(dev_desc, dev_desc->lba - 1, |
| 769 | gpt_h2, &gpt_e2); |
| 770 | |
| 771 | if (is_gpt1_valid && is_gpt2_valid) { |
| 772 | ret = 0; |
| 773 | goto out; |
| 774 | } |
| 775 | |
| 776 | if (is_gpt1_valid && !is_gpt2_valid) { |
| 777 | prepare_backup_gpt_header(gpt_h1); |
| 778 | ret = write_one_gpt_table(dev_desc, gpt_h1, gpt_e1); |
| 779 | goto out; |
| 780 | } |
| 781 | |
| 782 | if (!is_gpt1_valid && is_gpt2_valid) { |
| 783 | restore_primary_gpt_header(gpt_h2, dev_desc); |
| 784 | ret = write_one_gpt_table(dev_desc, gpt_h2, gpt_e2); |
| 785 | goto out; |
| 786 | } |
| 787 | |
| 788 | if (!is_gpt1_valid && !is_gpt2_valid) { |
| 789 | ret = -1; |
| 790 | goto out; |
| 791 | } |
| 792 | |
| 793 | out: |
| 794 | if (is_gpt1_valid) |
| 795 | free(gpt_e1); |
| 796 | if (is_gpt2_valid) |
| 797 | free(gpt_e2); |
| 798 | |
| 799 | return ret; |
| 800 | } |
| 801 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 802 | int gpt_verify_partitions(struct blk_desc *dev_desc, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 803 | struct disk_partition *partitions, int parts, |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 804 | gpt_header *gpt_head, gpt_entry **gpt_pte) |
| 805 | { |
| 806 | char efi_str[PARTNAME_SZ + 1]; |
| 807 | u64 gpt_part_size; |
| 808 | gpt_entry *gpt_e; |
| 809 | int ret, i; |
| 810 | |
| 811 | ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte); |
| 812 | if (ret) |
| 813 | return ret; |
| 814 | |
| 815 | gpt_e = *gpt_pte; |
| 816 | |
| 817 | for (i = 0; i < parts; i++) { |
| 818 | if (i == gpt_head->num_partition_entries) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 819 | pr_err("More partitions than allowed!\n"); |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 820 | return -1; |
| 821 | } |
| 822 | |
| 823 | /* Check if GPT and ENV partition names match */ |
| 824 | gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name, |
| 825 | PARTNAME_SZ + 1); |
| 826 | |
| 827 | debug("%s: part: %2d name - GPT: %16s, ENV: %16s ", |
| 828 | __func__, i, efi_str, partitions[i].name); |
| 829 | |
| 830 | if (strncmp(efi_str, (char *)partitions[i].name, |
| 831 | sizeof(partitions->name))) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 832 | pr_err("Partition name: %s does not match %s!\n", |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 833 | efi_str, (char *)partitions[i].name); |
| 834 | return -1; |
| 835 | } |
| 836 | |
| 837 | /* Check if GPT and ENV sizes match */ |
| 838 | gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) - |
| 839 | le64_to_cpu(gpt_e[i].starting_lba) + 1; |
| 840 | debug("size(LBA) - GPT: %8llu, ENV: %8llu ", |
Simon Glass | f8d6165 | 2016-02-29 15:25:36 -0700 | [diff] [blame] | 841 | (unsigned long long)gpt_part_size, |
| 842 | (unsigned long long)partitions[i].size); |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 843 | |
| 844 | if (le64_to_cpu(gpt_part_size) != partitions[i].size) { |
Kever Yang | c2fdd34 | 2016-07-29 11:12:18 +0800 | [diff] [blame] | 845 | /* We do not check the extend partition size */ |
| 846 | if ((i == parts - 1) && (partitions[i].size == 0)) |
| 847 | continue; |
| 848 | |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 849 | pr_err("Partition %s size: %llu does not match %llu!\n", |
Simon Glass | f8d6165 | 2016-02-29 15:25:36 -0700 | [diff] [blame] | 850 | efi_str, (unsigned long long)gpt_part_size, |
| 851 | (unsigned long long)partitions[i].size); |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 852 | return -1; |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * Start address is optional - check only if provided |
| 857 | * in '$partition' variable |
| 858 | */ |
| 859 | if (!partitions[i].start) { |
| 860 | debug("\n"); |
| 861 | continue; |
| 862 | } |
| 863 | |
| 864 | /* Check if GPT and ENV start LBAs match */ |
| 865 | debug("start LBA - GPT: %8llu, ENV: %8llu\n", |
| 866 | le64_to_cpu(gpt_e[i].starting_lba), |
Simon Glass | f8d6165 | 2016-02-29 15:25:36 -0700 | [diff] [blame] | 867 | (unsigned long long)partitions[i].start); |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 868 | |
| 869 | if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 870 | pr_err("Partition %s start: %llu does not match %llu!\n", |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 871 | efi_str, le64_to_cpu(gpt_e[i].starting_lba), |
Simon Glass | f8d6165 | 2016-02-29 15:25:36 -0700 | [diff] [blame] | 872 | (unsigned long long)partitions[i].start); |
Lukasz Majewski | cef68bf | 2015-11-20 08:06:16 +0100 | [diff] [blame] | 873 | return -1; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 880 | int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf) |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 881 | { |
| 882 | gpt_header *gpt_h; |
| 883 | gpt_entry *gpt_e; |
| 884 | |
| 885 | /* determine start of GPT Header in the buffer */ |
| 886 | gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * |
| 887 | dev_desc->blksz); |
| 888 | if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA, |
| 889 | dev_desc->lba)) |
| 890 | return -1; |
| 891 | |
| 892 | /* determine start of GPT Entries in the buffer */ |
| 893 | gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) * |
| 894 | dev_desc->blksz); |
| 895 | if (validate_gpt_entries(gpt_h, gpt_e)) |
| 896 | return -1; |
| 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 901 | #ifdef CONFIG_AMLOGIC_MODIFY |
| 902 | int erase_gpt_part_table(struct blk_desc *dev_desc) |
| 903 | { |
| 904 | gpt_header *gpt_h; |
| 905 | int size; |
| 906 | lbaint_t lba; |
| 907 | int cnt; |
| 908 | |
| 909 | printf("come to erase_gpt_part_table \n"); |
| 910 | |
| 911 | size = PAD_TO_BLOCKSIZE(sizeof(gpt_header), dev_desc); |
| 912 | gpt_h = malloc_cache_aligned(size); |
| 913 | if (gpt_h == NULL) { |
| 914 | printf("%s: calloc failed!\n", __func__); |
| 915 | return -1; |
| 916 | } |
| 917 | memset(gpt_h, 0, size); |
| 918 | |
| 919 | /* Setup the Protective MBR */ |
| 920 | ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, p_mbr, 1, dev_desc->blksz); |
| 921 | if (p_mbr == NULL) { |
| 922 | printf("%s: calloc failed!\n", __func__); |
| 923 | free(gpt_h); |
| 924 | return -1; |
| 925 | } |
| 926 | |
| 927 | /* Clear all data in MBR except of backed up boot code */ |
| 928 | memset((char *)p_mbr + MSDOS_MBR_BOOT_CODE_SIZE, 0, sizeof(*p_mbr) - |
| 929 | MSDOS_MBR_BOOT_CODE_SIZE); |
| 930 | |
| 931 | /* Write MBR sector to the MMC device */ |
| 932 | if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) { |
| 933 | printf("** Can't write to device %d **\n", |
| 934 | dev_desc->devnum); |
| 935 | free(gpt_h); |
| 936 | return -1; |
| 937 | } |
| 938 | |
| 939 | /* write Primary GPT */ |
| 940 | lba = GPT_PRIMARY_PARTITION_TABLE_LBA; |
| 941 | cnt = 1; /* GPT Header (1 block) */ |
| 942 | printf("%s: erase '%s' (%d blks at 0x" LBAF ")\n", |
| 943 | __func__, "Primary GPT Header", cnt, lba); |
| 944 | if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) { |
| 945 | printf("%s: failed erase '%s' (%d blks at 0x" LBAF ")\n", |
| 946 | __func__, "Primary GPT Header", cnt, lba); |
| 947 | free(gpt_h); |
| 948 | return 1; |
| 949 | } |
| 950 | |
| 951 | lba = cpu_to_le64(dev_desc->lba - 1); |
| 952 | cnt = 1; /* GPT Header (1 block) */ |
| 953 | printf("%s: erase '%s' (%d blks at 0x" LBAF ")\n", |
| 954 | __func__, "Backup GPT Header", cnt, lba); |
| 955 | if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) { |
| 956 | printf("%s: failed erase '%s' (%d blks at 0x" LBAF ")\n", |
| 957 | __func__, "Backup GPT Header", cnt, lba); |
| 958 | free(gpt_h); |
| 959 | return 1; |
| 960 | } |
| 961 | gpt_partition = false; |
| 962 | free(gpt_h); |
| 963 | return 0; |
| 964 | } |
| 965 | #endif |
| 966 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 967 | int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf) |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 968 | { |
| 969 | gpt_header *gpt_h; |
| 970 | gpt_entry *gpt_e; |
| 971 | int gpt_e_blk_cnt; |
| 972 | lbaint_t lba; |
| 973 | int cnt; |
Xindong Xu | d944142 | 2024-01-18 10:20:45 +0800 | [diff] [blame] | 974 | #ifdef CONFIG_AMLOGIC_MODIFY |
| 975 | int i; |
| 976 | u32 calc_crc32; |
| 977 | u32 entries_num; |
| 978 | bool flag = false; |
| 979 | bool alternate_flag = false; |
| 980 | #if (ADD_LAST_PARTITION) |
| 981 | ulong gap = GPT_GAP; |
| 982 | #endif |
| 983 | #endif |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 984 | |
| 985 | if (is_valid_gpt_buf(dev_desc, buf)) |
| 986 | return -1; |
| 987 | |
| 988 | /* determine start of GPT Header in the buffer */ |
| 989 | gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * |
| 990 | dev_desc->blksz); |
| 991 | |
| 992 | /* determine start of GPT Entries in the buffer */ |
| 993 | gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) * |
| 994 | dev_desc->blksz); |
| 995 | gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) * |
| 996 | le32_to_cpu(gpt_h->sizeof_partition_entry)), |
| 997 | dev_desc); |
| 998 | |
Xindong Xu | d944142 | 2024-01-18 10:20:45 +0800 | [diff] [blame] | 999 | #ifdef CONFIG_AMLOGIC_MODIFY |
| 1000 | entries_num = le32_to_cpu(gpt_h->num_partition_entries); |
| 1001 | |
| 1002 | if (le64_to_cpu(gpt_h->alternate_lba) > dev_desc->lba || |
| 1003 | le64_to_cpu(gpt_h->alternate_lba) == 0) { |
| 1004 | printf("GPT: alternate_lba: %llX, " LBAF ", reset it\n", |
Xindong Xu | 7f58de9 | 2024-03-01 14:21:52 +0800 | [diff] [blame] | 1005 | le64_to_cpu(gpt_h->alternate_lba), dev_desc->lba); |
Xindong Xu | d944142 | 2024-01-18 10:20:45 +0800 | [diff] [blame] | 1006 | gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1); |
| 1007 | flag = true; |
| 1008 | alternate_flag = true; |
| 1009 | } |
| 1010 | |
| 1011 | if (le64_to_cpu(gpt_h->last_usable_lba) > dev_desc->lba) { |
| 1012 | printf("GPT: last_usable_lba incorrect: %llX > " LBAF ", reset it\n", |
| 1013 | le64_to_cpu(gpt_h->last_usable_lba), dev_desc->lba); |
| 1014 | if (alternate_flag) |
| 1015 | gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34); |
| 1016 | else |
| 1017 | gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 1); |
| 1018 | flag = true; |
| 1019 | } |
| 1020 | |
| 1021 | for (i = 0; i < entries_num; i++) { |
| 1022 | #if (ADD_LAST_PARTITION) |
| 1023 | if (i == entries_num - 1) { |
| 1024 | gpt_e[i - 1].ending_lba -= gpt_e[i].ending_lba + le64_to_cpu(gap) + 1; |
| 1025 | gpt_e[i].starting_lba = gpt_e[i - 1].ending_lba + le64_to_cpu(gap) + 1; |
| 1026 | gpt_e[i].ending_lba = gpt_h->last_usable_lba; |
| 1027 | } |
| 1028 | |
| 1029 | #endif |
| 1030 | if (le64_to_cpu(gpt_e[i].ending_lba) > gpt_h->last_usable_lba) { |
| 1031 | printf("gpt_e[%d].ending_lba: %llX > %llX, reset it\n", |
| 1032 | i, le64_to_cpu(gpt_e[i].ending_lba), le64_to_cpu(gpt_h->last_usable_lba)); |
| 1033 | if (alternate_flag) |
| 1034 | gpt_e[i].ending_lba = ((gpt_h->last_usable_lba >> 12) << 12) - 1; |
| 1035 | else |
| 1036 | gpt_e[i].ending_lba = gpt_h->last_usable_lba; |
| 1037 | printf("gpt_e[%d].ending_lba: %llX \n", i, gpt_e[i].ending_lba); |
| 1038 | flag = true; |
| 1039 | } |
Xindong Xu | 7f58de9 | 2024-03-01 14:21:52 +0800 | [diff] [blame] | 1040 | |
| 1041 | if (le64_to_cpu(gpt_h->alternate_lba) > le64_to_cpu(gpt_e[i].starting_lba) && |
| 1042 | le64_to_cpu(gpt_h->alternate_lba) < le64_to_cpu(gpt_e[i].ending_lba)) { |
| 1043 | printf("%s: alternate_lba: %llX during part %d, invalid, reset it\n", |
| 1044 | __func__, le64_to_cpu(gpt_h->alternate_lba), i); |
| 1045 | gpt_h->alternate_lba = gpt_e[1].starting_lba - 1; |
| 1046 | } |
Xindong Xu | d944142 | 2024-01-18 10:20:45 +0800 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | if (flag) { |
| 1050 | calc_crc32 = efi_crc32((const unsigned char *)gpt_e, |
| 1051 | entries_num * le32_to_cpu(gpt_h->sizeof_partition_entry)); |
| 1052 | gpt_h->partition_entry_array_crc32 = calc_crc32; |
| 1053 | |
| 1054 | gpt_h->header_crc32 = 0; |
| 1055 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, |
| 1056 | le32_to_cpu(gpt_h->header_size)); |
| 1057 | gpt_h->header_crc32 = calc_crc32; |
| 1058 | flag = false; |
| 1059 | } |
| 1060 | #endif |
| 1061 | |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 1062 | /* write MBR */ |
| 1063 | lba = 0; /* MBR is always at 0 */ |
| 1064 | cnt = 1; /* MBR (1 block) */ |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 1065 | if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1066 | log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 1067 | "MBR", cnt, lba); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 1068 | return 1; |
| 1069 | } |
| 1070 | |
| 1071 | /* write Primary GPT */ |
| 1072 | lba = GPT_PRIMARY_PARTITION_TABLE_LBA; |
| 1073 | cnt = 1; /* GPT Header (1 block) */ |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 1074 | if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1075 | log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 1076 | "Primary GPT Header", cnt, lba); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 1077 | return 1; |
| 1078 | } |
| 1079 | |
| 1080 | lba = le64_to_cpu(gpt_h->partition_entry_lba); |
| 1081 | cnt = gpt_e_blk_cnt; |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 1082 | if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1083 | log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 1084 | "Primary GPT Entries", cnt, lba); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 1085 | return 1; |
| 1086 | } |
| 1087 | |
| 1088 | prepare_backup_gpt_header(gpt_h); |
| 1089 | |
| 1090 | /* write Backup GPT */ |
| 1091 | lba = le64_to_cpu(gpt_h->partition_entry_lba); |
| 1092 | cnt = gpt_e_blk_cnt; |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 1093 | if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1094 | log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 1095 | "Backup GPT Entries", cnt, lba); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 1096 | return 1; |
| 1097 | } |
| 1098 | |
| 1099 | lba = le64_to_cpu(gpt_h->my_lba); |
| 1100 | cnt = 1; /* GPT Header (1 block) */ |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 1101 | if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1102 | log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 1103 | "Backup GPT Header", cnt, lba); |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 1104 | return 1; |
| 1105 | } |
| 1106 | |
Gary Bisson | 4e9bd06 | 2021-01-26 14:56:23 +0100 | [diff] [blame] | 1107 | /* Update the partition table entries*/ |
| 1108 | part_init(dev_desc); |
| 1109 | |
Steve Rae | 0ff7e58 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 1110 | return 0; |
| 1111 | } |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 1112 | #endif |
| 1113 | |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1114 | /* |
| 1115 | * Private functions |
| 1116 | */ |
| 1117 | /* |
| 1118 | * pmbr_part_valid(): Check for EFI partition signature |
| 1119 | * |
| 1120 | * Returns: 1 if EFI GPT partition type is found. |
| 1121 | */ |
| 1122 | static int pmbr_part_valid(struct partition *part) |
| 1123 | { |
| 1124 | if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT && |
Marc Dietrich | 8faefad | 2013-03-29 07:57:10 +0000 | [diff] [blame] | 1125 | get_unaligned_le32(&part->start_sect) == 1UL) { |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1126 | return 1; |
| 1127 | } |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * is_pmbr_valid(): test Protective MBR for validity |
| 1134 | * |
| 1135 | * Returns: 1 if PMBR is valid, 0 otherwise. |
| 1136 | * Validity depends on two things: |
| 1137 | * 1) MSDOS signature is in the last two bytes of the MBR |
| 1138 | * 2) One partition of type 0xEE is found, checked by pmbr_part_valid() |
| 1139 | */ |
| 1140 | static int is_pmbr_valid(legacy_mbr * mbr) |
| 1141 | { |
| 1142 | int i = 0; |
| 1143 | |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 1144 | if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1145 | return 0; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1146 | |
| 1147 | for (i = 0; i < 4; i++) { |
| 1148 | if (pmbr_part_valid(&mbr->partition_record[i])) { |
| 1149 | return 1; |
| 1150 | } |
| 1151 | } |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
| 1155 | /** |
| 1156 | * is_gpt_valid() - tests one GPT header and PTEs for validity |
| 1157 | * |
| 1158 | * lba is the logical block address of the GPT header to test |
| 1159 | * gpt is a GPT header ptr, filled on return. |
| 1160 | * ptes is a PTEs ptr, filled on return. |
| 1161 | * |
Urja Rannikko | 0557d46 | 2019-04-11 20:27:51 +0000 | [diff] [blame] | 1162 | * Description: returns 1 if valid, 0 on error, 2 if ignored header |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1163 | * If valid, returns pointers to PTEs. |
| 1164 | */ |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 1165 | #ifdef CONFIG_AMLOGIC_MODIFY |
| 1166 | int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, |
| 1167 | gpt_header *pgpt_head, gpt_entry **pgpt_pte) |
| 1168 | #else |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 1169 | static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 1170 | gpt_header *pgpt_head, gpt_entry **pgpt_pte) |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 1171 | #endif |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1172 | { |
Tom Rini | b351ccf | 2017-10-03 09:38:44 -0400 | [diff] [blame] | 1173 | /* Confirm valid arguments prior to allocation. */ |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1174 | if (!dev_desc || !pgpt_head) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1175 | log_debug("Invalid Argument(s)\n"); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
Patrick Delaunay | 3cc5661 | 2017-11-17 10:08:18 +0100 | [diff] [blame] | 1179 | ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, mbr, 1, dev_desc->blksz); |
Tom Rini | b351ccf | 2017-10-03 09:38:44 -0400 | [diff] [blame] | 1180 | |
Peter Jones | ff98cb9 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 1181 | /* Read MBR Header from device */ |
| 1182 | if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1183 | log_debug("Can't read MBR header\n"); |
Peter Jones | ff98cb9 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 1184 | return 0; |
| 1185 | } |
| 1186 | |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1187 | /* Read GPT Header from device */ |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 1188 | if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1189 | log_debug("Can't read GPT header\n"); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
Urja Rannikko | 0557d46 | 2019-04-11 20:27:51 +0000 | [diff] [blame] | 1193 | /* Invalid but nothing to yell about. */ |
| 1194 | if (le64_to_cpu(pgpt_head->signature) == GPT_HEADER_CHROMEOS_IGNORE) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1195 | log_debug("ChromeOS 'IGNOREME' GPT header found and ignored\n"); |
Urja Rannikko | 0557d46 | 2019-04-11 20:27:51 +0000 | [diff] [blame] | 1196 | return 2; |
| 1197 | } |
| 1198 | |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 1199 | if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba)) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1200 | return 0; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1201 | |
Peter Jones | ff98cb9 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 1202 | if (dev_desc->sig_type == SIG_TYPE_NONE) { |
| 1203 | efi_guid_t empty = {}; |
| 1204 | if (memcmp(&pgpt_head->disk_guid, &empty, sizeof(empty))) { |
| 1205 | dev_desc->sig_type = SIG_TYPE_GUID; |
| 1206 | memcpy(&dev_desc->guid_sig, &pgpt_head->disk_guid, |
| 1207 | sizeof(empty)); |
| 1208 | } else if (mbr->unique_mbr_signature != 0) { |
| 1209 | dev_desc->sig_type = SIG_TYPE_MBR; |
| 1210 | dev_desc->mbr_sig = mbr->unique_mbr_signature; |
| 1211 | } |
| 1212 | } |
| 1213 | |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1214 | /* Read and allocate Partition Table Entries */ |
| 1215 | *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head); |
Heinrich Schuchardt | 1291c2d | 2022-05-09 21:35:44 +0200 | [diff] [blame] | 1216 | if (!*pgpt_pte) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1217 | return 0; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1218 | |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 1219 | if (validate_gpt_entries(pgpt_head, *pgpt_pte)) { |
Doug Anderson | deb5ca8 | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 1220 | free(*pgpt_pte); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | /* We're done, all's well */ |
| 1225 | return 1; |
| 1226 | } |
| 1227 | |
| 1228 | /** |
Urja Rannikko | 20c568c | 2019-04-11 20:27:50 +0000 | [diff] [blame] | 1229 | * find_valid_gpt() - finds a valid GPT header and PTEs |
| 1230 | * |
| 1231 | * gpt is a GPT header ptr, filled on return. |
| 1232 | * ptes is a PTEs ptr, filled on return. |
| 1233 | * |
| 1234 | * Description: returns 1 if found a valid gpt, 0 on error. |
| 1235 | * If valid, returns pointers to PTEs. |
| 1236 | */ |
| 1237 | static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head, |
| 1238 | gpt_entry **pgpt_pte) |
| 1239 | { |
Urja Rannikko | 0557d46 | 2019-04-11 20:27:51 +0000 | [diff] [blame] | 1240 | int r; |
| 1241 | |
| 1242 | r = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_head, |
| 1243 | pgpt_pte); |
| 1244 | |
| 1245 | if (r != 1) { |
| 1246 | if (r != 2) |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1247 | log_debug("Invalid GPT\n"); |
Urja Rannikko | 0557d46 | 2019-04-11 20:27:51 +0000 | [diff] [blame] | 1248 | |
| 1249 | if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), gpt_head, |
| 1250 | pgpt_pte) != 1) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1251 | log_debug("Invalid Backup GPT\n"); |
Urja Rannikko | 20c568c | 2019-04-11 20:27:50 +0000 | [diff] [blame] | 1252 | return 0; |
| 1253 | } |
Urja Rannikko | 0557d46 | 2019-04-11 20:27:51 +0000 | [diff] [blame] | 1254 | if (r != 2) |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1255 | log_debug(" Using Backup GPT\n"); |
Urja Rannikko | 20c568c | 2019-04-11 20:27:50 +0000 | [diff] [blame] | 1256 | } |
| 1257 | return 1; |
| 1258 | } |
| 1259 | |
| 1260 | /** |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1261 | * alloc_read_gpt_entries(): reads partition entries from disk |
| 1262 | * @dev_desc |
| 1263 | * @gpt - GPT header |
| 1264 | * |
| 1265 | * Description: Returns ptes on success, NULL on error. |
| 1266 | * Allocates space for PTEs based on information found in @gpt. |
| 1267 | * Notes: remember to free pte when you're done! |
| 1268 | */ |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 1269 | static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc, |
| 1270 | gpt_header *pgpt_head) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1271 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 1272 | size_t count = 0, blk_cnt; |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 1273 | lbaint_t blk; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1274 | gpt_entry *pte = NULL; |
| 1275 | |
| 1276 | if (!dev_desc || !pgpt_head) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1277 | log_debug("Invalid Argument(s)\n"); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1278 | return NULL; |
| 1279 | } |
| 1280 | |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 1281 | count = le32_to_cpu(pgpt_head->num_partition_entries) * |
| 1282 | le32_to_cpu(pgpt_head->sizeof_partition_entry); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1283 | |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1284 | log_debug("count = %u * %u = %lu\n", |
| 1285 | (u32)le32_to_cpu(pgpt_head->num_partition_entries), |
| 1286 | (u32)le32_to_cpu(pgpt_head->sizeof_partition_entry), |
| 1287 | (ulong)count); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1288 | |
| 1289 | /* Allocate memory for PTE, remember to FREE */ |
| 1290 | if (count != 0) { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 1291 | pte = memalign(ARCH_DMA_MINALIGN, |
| 1292 | PAD_TO_BLOCKSIZE(count, dev_desc)); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | if (count == 0 || pte == NULL) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1296 | log_debug("ERROR: Can't allocate %#lX bytes for GPT Entries\n", |
| 1297 | (ulong)count); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1298 | return NULL; |
| 1299 | } |
| 1300 | |
| 1301 | /* Read GPT Entries from device */ |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 1302 | blk = le64_to_cpu(pgpt_head->partition_entry_lba); |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 1303 | blk_cnt = BLOCK_CNT(count, dev_desc); |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 1304 | if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1305 | log_debug("Can't read GPT Entries\n"); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1306 | free(pte); |
| 1307 | return NULL; |
| 1308 | } |
| 1309 | return pte; |
| 1310 | } |
| 1311 | |
| 1312 | /** |
| 1313 | * is_pte_valid(): validates a single Partition Table Entry |
| 1314 | * @gpt_entry - Pointer to a single Partition Table Entry |
| 1315 | * |
| 1316 | * Description: returns 1 if valid, 0 on error. |
| 1317 | */ |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 1318 | #ifdef CONFIG_AMLOGIC_MODIFY |
| 1319 | int is_pte_valid(gpt_entry * pte) |
| 1320 | #else |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1321 | static int is_pte_valid(gpt_entry * pte) |
Bo Lv | 72d0e90 | 2023-01-02 14:27:34 +0000 | [diff] [blame] | 1322 | #endif |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1323 | { |
| 1324 | efi_guid_t unused_guid; |
| 1325 | |
| 1326 | if (!pte) { |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1327 | log_debug("Invalid Argument(s)\n"); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | /* Only one validation for now: |
| 1332 | * The GUID Partition Type != Unused Entry (ALL-ZERO) |
| 1333 | */ |
| 1334 | memset(unused_guid.b, 0, sizeof(unused_guid.b)); |
| 1335 | |
| 1336 | if (memcmp(pte->partition_type_guid.b, unused_guid.b, |
| 1337 | sizeof(unused_guid.b)) == 0) { |
| 1338 | |
Simon Glass | 3e62adc | 2022-10-20 18:22:40 -0600 | [diff] [blame] | 1339 | log_debug("Found an unused PTE GUID at 0x%08X\n", |
| 1340 | (unsigned int)(uintptr_t)pte); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 1341 | |
| 1342 | return 0; |
| 1343 | } else { |
| 1344 | return 1; |
| 1345 | } |
| 1346 | } |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 1347 | |
| 1348 | /* |
| 1349 | * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to |
| 1350 | * check EFI first, since a DOS partition is often used as a 'protective MBR' |
| 1351 | * with EFI. |
| 1352 | */ |
| 1353 | U_BOOT_PART_TYPE(a_efi) = { |
| 1354 | .name = "EFI", |
| 1355 | .part_type = PART_TYPE_EFI, |
Petr Kulhavy | 87b8530 | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 1356 | .max_entries = GPT_ENTRY_NUMBERS, |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 1357 | .get_info = part_get_info_ptr(part_get_info_efi), |
Simon Glass | 084bf4c | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 1358 | .print = part_print_ptr(part_print_efi), |
| 1359 | .test = part_test_efi, |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 1360 | }; |