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