blob: d0b1f96adff2094ac164a1118fc27c08294af0f7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
richardretanubune6745592008-09-26 11:13:22 -04002/*
3 * Copyright (C) 2008 RuggedCom, Inc.
4 * Richard Retanubun <RichardRetanubun@RuggedCom.com>
richardretanubune6745592008-09-26 11:13:22 -04005 */
6
7/*
Steve Raed30cbae2014-05-26 11:52:23 -07008 * NOTE:
9 * when CONFIG_SYS_64BIT_LBA is not defined, lbaint_t is 32 bits; this
Heinrich Schuchardt932b9a92020-09-17 17:57:21 +020010 * limits the maximum size of addressable storage to < 2 tebibytes
richardretanubune6745592008-09-26 11:13:22 -040011 */
Simon Glass9137f662023-08-24 13:55:34 -060012
13#define LOG_CATEGORY LOGC_FS
14
Simon Glass655306c2020-05-10 11:39:58 -060015#include <blk.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060017#include <part.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010018#include <u-boot/uuid.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Marc Dietrich20ca3ad2013-03-29 07:57:10 +000021#include <asm/unaligned.h>
richardretanubune6745592008-09-26 11:13:22 -040022#include <command.h>
Philipp Tomsicha3da0e92017-03-01 21:10:39 +010023#include <fdtdec.h>
richardretanubune6745592008-09-26 11:13:22 -040024#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060025#include <memalign.h>
Chang Hyun Park823ffde2012-12-11 11:09:45 +010026#include <part_efi.h>
Simon Glass0034d962021-08-07 07:24:01 -060027#include <dm/ofnode.h>
Philipp Tomsicha3da0e92017-03-01 21:10:39 +010028#include <linux/compiler.h>
Lei Wen757ddfe2011-09-07 18:11:19 +000029#include <linux/ctype.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060030#include <linux/printk.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070031#include <u-boot/crc.h>
richardretanubune6745592008-09-26 11:13:22 -040032
Simon Glass56d59a62021-07-02 12:36:14 -060033/* GUID for basic data partitons */
34#if CONFIG_IS_ENABLED(EFI_PARTITION)
Heinrich Schuchardtfac78672018-06-09 17:50:18 +020035static const efi_guid_t partition_basic_data_guid = PARTITION_BASIC_DATA_GUID;
Simon Glass56d59a62021-07-02 12:36:14 -060036#endif
Heinrich Schuchardtfac78672018-06-09 17:50:18 +020037
richardretanubune6745592008-09-26 11:13:22 -040038/**
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 Park823ffde2012-12-11 11:09:45 +010045static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubune6745592008-09-26 11:13:22 -040046{
47 return crc32(0, buf, len);
48}
49
50/*
51 * Private function prototypes
52 */
53
54static int pmbr_part_valid(struct partition *part);
55static int is_pmbr_valid(legacy_mbr * mbr);
Simon Glass1369ad42023-08-24 13:55:27 -060056static int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,
57 gpt_entry **pgpt_pte);
58static gpt_entry *alloc_read_gpt_entries(struct blk_desc *desc,
Simon Glasse3394752016-02-29 15:25:34 -070059 gpt_header *pgpt_head);
richardretanubune6745592008-09-26 11:13:22 -040060static int is_pte_valid(gpt_entry * pte);
Simon Glass1369ad42023-08-24 13:55:27 -060061static int find_valid_gpt(struct blk_desc *desc, gpt_header *gpt_head,
Urja Rannikko80bded72019-04-11 20:27:50 +000062 gpt_entry **pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -040063
Lei Wen757ddfe2011-09-07 18:11:19 +000064static 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 Schuchardt35ea8682019-01-12 11:25:25 +010078static const efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
Stephen Warrend0cf3b62012-10-08 08:14:37 +000079
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010080static int get_bootable(gpt_entry *p)
Stephen Warrend0cf3b62012-10-08 08:14:37 +000081{
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010082 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 Warrend0cf3b62012-10-08 08:14:37 +000089}
90
Steve Rae6f5e17d2014-12-18 12:13:42 +010091static 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 Glass7903d212018-10-01 12:22:35 -060098 if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE_UBOOT) {
Simon Glassa6265b52022-10-20 18:22:40 -060099 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 Rae6f5e17d2014-12-18 12:13:42 +0100103 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 Glassa6265b52022-10-20 18:22:40 -0600116 log_debug("%s: CRC is wrong: %#x != %#x\n",
117 "GUID Partition Table Header",
118 le32_to_cpu(crc32_backup), calc_crc32);
Steve Rae6f5e17d2014-12-18 12:13:42 +0100119 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 Glassa6265b52022-10-20 18:22:40 -0600126 log_debug("GPT: my_lba incorrect: %llX != " LBAF "\n",
127 le64_to_cpu(gpt_h->my_lba), lba);
Steve Rae6f5e17d2014-12-18 12:13:42 +0100128 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 Glassa6265b52022-10-20 18:22:40 -0600136 log_debug("GPT: first_usable_lba incorrect: %llX > " LBAF "\n",
137 le64_to_cpu(gpt_h->first_usable_lba), lastlba);
Steve Rae6f5e17d2014-12-18 12:13:42 +0100138 return -1;
139 }
140 if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) {
Simon Glassa6265b52022-10-20 18:22:40 -0600141 log_debug("GPT: last_usable_lba incorrect: %llX > " LBAF "\n",
142 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
Steve Rae6f5e17d2014-12-18 12:13:42 +0100143 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
153static 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 Glassa6265b52022-10-20 18:22:40 -0600163 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 Rae6f5e17d2014-12-18 12:13:42 +0100167 return -1;
168 }
169
170 return 0;
171}
172
173static 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 Rae7d059342014-12-12 15:51:54 -0800182 gpt_h->partition_entry_lba =
183 cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
Steve Rae6f5e17d2014-12-18 12:13:42 +0100184 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 Delaunay8a4f2bd2017-01-27 11:00:41 +0100191#if CONFIG_IS_ENABLED(EFI_PARTITION)
richardretanubune6745592008-09-26 11:13:22 -0400192/*
193 * Public Functions (include/part.h)
194 */
195
Alison Chaikene4222582017-06-25 16:43:23 -0700196/*
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 Glass1369ad42023-08-24 13:55:27 -0600200int get_disk_guid(struct blk_desc *desc, char *guid)
Alison Chaikene4222582017-06-25 16:43:23 -0700201{
Simon Glass1369ad42023-08-24 13:55:27 -0600202 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
Alison Chaikene4222582017-06-25 16:43:23 -0700203 gpt_entry *gpt_pte = NULL;
204 unsigned char *guid_bin;
205
206 /* This function validates AND fills in the GPT header and PTE */
Simon Glass1369ad42023-08-24 13:55:27 -0600207 if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
Urja Rannikko80bded72019-04-11 20:27:50 +0000208 return -EINVAL;
Alison Chaikene4222582017-06-25 16:43:23 -0700209
210 guid_bin = gpt_head->disk_guid.b;
211 uuid_bin_to_str(guid_bin, guid, UUID_STR_FORMAT_GUID);
212
Eugeniu Roscaeb11c2e2019-04-30 04:53:44 +0200213 /* Remember to free pte */
214 free(gpt_pte);
Alison Chaikene4222582017-06-25 16:43:23 -0700215 return 0;
216}
217
Javier Martinez Canillasac8ac572025-06-19 10:33:59 +0200218static 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 Apalodimas50823f02024-10-26 11:05:53 +0300246static void __maybe_unused part_print_efi(struct blk_desc *desc)
richardretanubune6745592008-09-26 11:13:22 -0400247{
Simon Glass1369ad42023-08-24 13:55:27 -0600248 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000249 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400250 int i = 0;
Heinrich Schuchardt79f29c82022-01-16 12:23:19 +0100251 unsigned char *uuid;
richardretanubune6745592008-09-26 11:13:22 -0400252
richardretanubune6745592008-09-26 11:13:22 -0400253 /* This function validates AND fills in the GPT header and PTE */
Simon Glass1369ad42023-08-24 13:55:27 -0600254 if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
Urja Rannikko80bded72019-04-11 20:27:50 +0000255 return;
richardretanubune6745592008-09-26 11:13:22 -0400256
Doug Anderson698a51d2011-10-19 09:47:31 +0000257 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400258
Stephen Warren26163262012-10-08 08:14:33 +0000259 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000260 printf("\tAttributes\n");
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200261 printf("\tType GUID\n");
262 printf("\tPartition GUID\n");
Stephen Warren8b855b52012-10-08 08:14:34 +0000263
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100264 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Heinrich Schuchardt8c0435e2022-01-11 15:43:05 +0100265 /* Skip invalid PTE */
Stephen Warrene6df59e2012-10-08 08:14:32 +0000266 if (!is_pte_valid(&gpt_pte[i]))
Heinrich Schuchardt8c0435e2022-01-11 15:43:05 +0100267 continue;
richardretanubune6745592008-09-26 11:13:22 -0400268
Stephen Warren26163262012-10-08 08:14:33 +0000269 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100270 le64_to_cpu(gpt_pte[i].starting_lba),
271 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warren26163262012-10-08 08:14:33 +0000272 print_efiname(&gpt_pte[i]));
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000273 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Heinrich Schuchardt79f29c82022-01-16 12:23:19 +0100274 uuid = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Simon Glassb17cfad2023-02-05 15:40:27 -0700275 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID))
Heinrich Schuchardt79f29c82022-01-16 12:23:19 +0100276 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);
richardretanubune6745592008-09-26 11:13:22 -0400281 }
282
283 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000284 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400285 return;
286}
287
Ilias Apalodimas50823f02024-10-26 11:05:53 +0300288static int __maybe_unused part_get_info_efi(struct blk_desc *desc, int part,
289 struct disk_partition *info)
richardretanubune6745592008-09-26 11:13:22 -0400290{
Javier Martinez Canillasac8ac572025-06-19 10:33:59 +0200291 gpt_entry gpt_pte = {};
292 int ret;
richardretanubune6745592008-09-26 11:13:22 -0400293
Javier Martinez Canillasac8ac572025-06-19 10:33:59 +0200294 ret = part_get_gpt_pte(desc, part, &gpt_pte);
295 if (ret)
296 return ret;
Stephen Warrene47dbba2012-09-21 09:50:58 +0000297
Steve Raed30cbae2014-05-26 11:52:23 -0700298 /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
Javier Martinez Canillasac8ac572025-06-19 10:33:59 +0200299 info->start = (lbaint_t)le64_to_cpu(gpt_pte.starting_lba);
Richard Retanubun413dfc72009-01-26 08:45:14 -0500300 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Javier Martinez Canillasac8ac572025-06-19 10:33:59 +0200301 info->size = (lbaint_t)le64_to_cpu(gpt_pte.ending_lba) + 1
Richard Retanubun413dfc72009-01-26 08:45:14 -0500302 - info->start;
Simon Glass1369ad42023-08-24 13:55:27 -0600303 info->blksz = desc->blksz;
richardretanubune6745592008-09-26 11:13:22 -0400304
Heinrich Schuchardt08299a92019-07-05 21:27:13 +0200305 snprintf((char *)info->name, sizeof(info->name), "%s",
Javier Martinez Canillasac8ac572025-06-19 10:33:59 +0200306 print_efiname(&gpt_pte));
Ben Whitten34fd6c92015-12-30 13:05:58 +0000307 strcpy((char *)info->type, "U-Boot");
Javier Martinez Canillasac8ac572025-06-19 10:33:59 +0200308 info->bootable = get_bootable(&gpt_pte);
309 info->type_flags = gpt_pte.attributes.fields.type_guid_specific;
Simon Glass23e34532023-08-24 13:55:31 -0600310 if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) {
Javier Martinez Canillasac8ac572025-06-19 10:33:59 +0200311 uuid_bin_to_str(gpt_pte.unique_partition_guid.b,
Simon Glass23e34532023-08-24 13:55:31 -0600312 (char *)disk_partition_uuid(info),
313 UUID_STR_FORMAT_GUID);
314 }
Simon Glassb8d3a782023-08-24 13:55:32 -0600315 if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) {
Javier Martinez Canillasac8ac572025-06-19 10:33:59 +0200316 uuid_bin_to_str(gpt_pte.partition_type_guid.b,
Heinrich Schuchardtf4b4c4e2023-09-02 09:35:21 +0200317 (char *)disk_partition_type_guid(info),
Simon Glassb8d3a782023-08-24 13:55:32 -0600318 UUID_STR_FORMAT_GUID);
319 }
richardretanubune6745592008-09-26 11:13:22 -0400320
Simon Glass3862fab2022-10-11 09:47:11 -0600321 log_debug("start 0x" LBAF ", size 0x" LBAF ", name %s\n", info->start,
322 info->size, info->name);
richardretanubune6745592008-09-26 11:13:22 -0400323
richardretanubune6745592008-09-26 11:13:22 -0400324 return 0;
325}
326
Simon Glass1369ad42023-08-24 13:55:27 -0600327static int part_test_efi(struct blk_desc *desc)
richardretanubune6745592008-09-26 11:13:22 -0400328{
Simon Glass1369ad42023-08-24 13:55:27 -0600329 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, desc->blksz);
richardretanubune6745592008-09-26 11:13:22 -0400330
331 /* Read legacy MBR from block 0 and validate it */
Simon Glass1369ad42023-08-24 13:55:27 -0600332 if ((blk_dread(desc, 0, 1, (ulong *)legacymbr) != 1)
Anton staaf3b3df4e2011-10-12 13:56:04 +0000333 || (is_pmbr_valid(legacymbr) != 1)) {
Svyatoslav Ryhelf19eb7c2024-07-31 11:22:54 +0300334 /*
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;
richardretanubune6745592008-09-26 11:13:22 -0400345 return -1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100346 }
347 return 0;
348}
349
350/**
351 * set_protective_mbr(): Set the EFI protective MBR
Simon Glass1369ad42023-08-24 13:55:27 -0600352 * @param desc - block device descriptor
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100353 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100354 * Return: - zero on success, otherwise error
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100355 */
Simon Glass1369ad42023-08-24 13:55:27 -0600356static int set_protective_mbr(struct blk_desc *desc)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100357{
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100358 /* Setup the Protective MBR */
Simon Glass1369ad42023-08-24 13:55:27 -0600359 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, p_mbr, 1, desc->blksz);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100360 if (p_mbr == NULL) {
Simon Glassa6265b52022-10-20 18:22:40 -0600361 log_debug("calloc failed!\n");
362 return -ENOMEM;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100363 }
Vincent Tinelli35620492017-01-30 15:46:07 +0300364
365 /* Read MBR to backup boot code if it exists */
Simon Glass1369ad42023-08-24 13:55:27 -0600366 if (blk_dread(desc, 0, 1, p_mbr) != 1) {
Simon Glassa6265b52022-10-20 18:22:40 -0600367 log_debug("** Can't read from device %d **\n",
Simon Glass1369ad42023-08-24 13:55:27 -0600368 desc->devnum);
Simon Glassa6265b52022-10-20 18:22:40 -0600369 return -EIO;
Vincent Tinelli35620492017-01-30 15:46:07 +0300370 }
371
Sam Protsenkoa3a51a42018-05-22 02:04:21 +0300372 /* 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 Majewski0dcfc5c2012-12-11 11:09:46 +0100376 /* 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 Glass1369ad42023-08-24 13:55:27 -0600380 p_mbr->partition_record[0].nr_sects = (u32)desc->lba - 1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100381
382 /* Write MBR sector to the MMC device */
Simon Glass1369ad42023-08-24 13:55:27 -0600383 if (blk_dwrite(desc, 0, 1, p_mbr) != 1) {
384 log_debug("** Can't write to device %d **\n", desc->devnum);
Simon Glassa6265b52022-10-20 18:22:40 -0600385 return -EIO;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100386 }
387
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100388 return 0;
389}
390
Simon Glass1369ad42023-08-24 13:55:27 -0600391int write_gpt_table(struct blk_desc *desc, gpt_header *gpt_h, gpt_entry *gpt_e)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100392{
Egbert Eich071e4232013-04-09 06:03:36 +0000393 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
Simon Glass1369ad42023-08-24 13:55:27 -0600394 * sizeof(gpt_entry)), desc);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100395 u32 calc_crc32;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100396
Simon Glass1369ad42023-08-24 13:55:27 -0600397 debug("max lba: %x\n", (u32)desc->lba);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100398 /* Setup the Protective MBR */
Simon Glass1369ad42023-08-24 13:55:27 -0600399 if (set_protective_mbr(desc) < 0)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100400 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 Glass1369ad42023-08-24 13:55:27 -0600413 if (blk_dwrite(desc, 1, 1, gpt_h) != 1)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100414 goto err;
415
Simon Glass1369ad42023-08-24 13:55:27 -0600416 if (blk_dwrite(desc, le64_to_cpu(gpt_h->partition_entry_lba),
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100417 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100418 goto err;
419
Steve Rae6f5e17d2014-12-18 12:13:42 +0100420 prepare_backup_gpt_header(gpt_h);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100421
Simon Glass1369ad42023-08-24 13:55:27 -0600422 if (blk_dwrite(desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
Simon Glass2ee8ada2016-02-29 15:25:52 -0700423 + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100424 goto err;
425
Simon Glass1369ad42023-08-24 13:55:27 -0600426 if (blk_dwrite(desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
Simon Glass2ee8ada2016-02-29 15:25:52 -0700427 gpt_h) != 1)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100428 goto err;
429
430 debug("GPT successfully written to block device!\n");
431 return 0;
432
433 err:
Simon Glass1369ad42023-08-24 13:55:27 -0600434 log_debug("** Can't write to device %d **\n", desc->devnum);
Simon Glassa6265b52022-10-20 18:22:40 -0600435 return -EIO;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100436}
437
Simon Glass1369ad42023-08-24 13:55:27 -0600438int gpt_fill_pte(struct blk_desc *desc,
Maxime Ripard0d390872017-08-23 16:01:32 +0200439 gpt_header *gpt_h, gpt_entry *gpt_e,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600440 struct disk_partition *partitions, int parts)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100441{
Steve Raed30cbae2014-05-26 11:52:23 -0700442 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
Steve Raed30cbae2014-05-26 11:52:23 -0700443 lbaint_t last_usable_lba = (lbaint_t)
444 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100445 int i, k;
Marek Vasut941579e2013-05-19 12:53:34 +0000446 size_t efiname_len, dosname_len;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200447 unsigned char *bin_uuid;
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100448#ifdef CONFIG_PARTITION_TYPE_GUID
449 char *str_type_guid;
450 unsigned char *bin_type_guid;
451#endif
Maxime Ripard7947c692017-08-23 16:01:33 +0200452 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 Glass1369ad42023-08-24 13:55:27 -0600458 desc->blksz;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100459
460 for (i = 0; i < parts; i++) {
461 /* partition starting lba */
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200462 lbaint_t start = partitions[i].start;
463 lbaint_t size = partitions[i].size;
464
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100465 if (start) {
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200466 offset = start + size;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100467 } else {
Maxime Ripard7947c692017-08-23 16:01:33 +0200468 start = offset;
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200469 offset += size;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100470 }
Maxime Ripard7947c692017-08-23 16:01:33 +0200471
472 /*
473 * If our partition overlaps with either the GPT
474 * header, or the partition entry, reject it.
475 */
Patrick Delaunaybe4a0fa2017-10-18 15:11:05 +0200476 if (((start < hdr_end && hdr_start < (start + size)) ||
477 (start < pte_end && pte_start < (start + size)))) {
Simon Glassa6265b52022-10-20 18:22:40 -0600478 log_debug("Partition overlap\n");
479 return -ENOSPC;
Maxime Ripard7947c692017-08-23 16:01:33 +0200480 }
481
482 gpt_e[i].starting_lba = cpu_to_le64(start);
483
Patrick Delaunay10172f32016-05-02 14:43:33 +0200484 if (offset > (last_usable_lba + 1)) {
Simon Glassa6265b52022-10-20 18:22:40 -0600485 log_debug("Partitions layout exceeds disk size\n");
486 return -E2BIG;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100487 }
488 /* partition ending lba */
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200489 if ((i == parts - 1) && (size == 0))
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100490 /* 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 Delaunay8248c8d2015-10-27 11:00:27 +0100495#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 Glassa6265b52022-10-20 18:22:40 -0600501 log_debug("Partition no. %d: invalid type guid: %s\n",
502 i, str_type_guid);
503 return -EINVAL;
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100504 }
505 } else {
506 /* default partition type GUID */
507 memcpy(bin_type_guid,
Heinrich Schuchardtfac78672018-06-09 17:50:18 +0200508 &partition_basic_data_guid, 16);
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100509 }
510#else
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100511 /* partition type GUID */
512 memcpy(gpt_e[i].partition_type_guid.b,
Heinrich Schuchardtfac78672018-06-09 17:50:18 +0200513 &partition_basic_data_guid, 16);
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100514#endif
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100515
Simon Glass23e34532023-08-24 13:55:31 -0600516 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 Marczakfa907342014-04-02 10:20:02 +0200521
Simon Glass23e34532023-08-24 13:55:31 -0600522 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 Majewski0dcfc5c2012-12-11 11:09:46 +0100528 }
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100529
530 /* partition attributes */
531 memset(&gpt_e[i].attributes, 0,
532 sizeof(gpt_entry_attributes));
533
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100534 if (partitions[i].bootable & PART_BOOTABLE)
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100535 gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
536
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100537 /* partition name */
Marek Vasut941579e2013-05-19 12:53:34 +0000538 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100539 / sizeof(efi_char16_t);
Marek Vasut941579e2013-05-19 12:53:34 +0000540 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 Majewski0dcfc5c2012-12-11 11:09:46 +0100546 gpt_e[i].partition_name[k] =
547 (efi_char16_t)(partitions[i].name[k]);
548
Steve Raed30cbae2014-05-26 11:52:23 -0700549 debug("%s: name: %s offset[%d]: 0x" LBAF
550 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100551 __func__, partitions[i].name, i,
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200552 offset, i, size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100553 }
554
555 return 0;
556}
557
Simon Glass1369ad42023-08-24 13:55:27 -0600558static uint32_t partition_entries_offset(struct blk_desc *desc)
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100559{
560 uint32_t offset_blks = 2;
Maxime Ripard1d088f02017-08-23 16:01:30 +0200561 uint32_t __maybe_unused offset_bytes;
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100562 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 Ripard1d088f02017-08-23 16:01:30 +0200573 offset_bytes =
Simon Glass1369ad42023-08-24 13:55:27 -0600574 PAD_TO_BLOCKSIZE(CONFIG_EFI_PARTITION_ENTRIES_OFF, desc);
575 offset_blks = offset_bytes / desc->blksz;
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100576#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 Glass0034d962021-08-07 07:24:01 -0600584 config_offset = ofnode_conf_read_int(
585 "u-boot,efi-partition-entries-offset", -EINVAL);
Maxime Ripard1d088f02017-08-23 16:01:30 +0200586 if (config_offset != -EINVAL) {
Simon Glass1369ad42023-08-24 13:55:27 -0600587 offset_bytes = PAD_TO_BLOCKSIZE(config_offset, desc);
588 offset_blks = offset_bytes / desc->blksz;
Maxime Ripard1d088f02017-08-23 16:01:30 +0200589 }
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100590#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 Glass1369ad42023-08-24 13:55:27 -0600604int gpt_fill_header(struct blk_desc *desc, gpt_header *gpt_h, char *str_guid,
605 int parts_count)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100606{
Simon Glass7903d212018-10-01 12:22:35 -0600607 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE_UBOOT);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100608 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 Glass1369ad42023-08-24 13:55:27 -0600611 gpt_h->alternate_lba = cpu_to_le64(desc->lba - 1);
612 gpt_h->last_usable_lba = cpu_to_le64(desc->lba - 34);
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100613 gpt_h->partition_entry_lba =
Simon Glass1369ad42023-08-24 13:55:27 -0600614 cpu_to_le64(partition_entries_offset(desc));
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100615 gpt_h->first_usable_lba =
616 cpu_to_le64(le64_to_cpu(gpt_h->partition_entry_lba) + 32);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100617 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 Marczak0c813362014-04-02 10:20:03 +0200622 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100623 return -1;
624
625 return 0;
626}
627
Simon Glass1369ad42023-08-24 13:55:27 -0600628int gpt_restore(struct blk_desc *desc, char *str_disk_guid,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600629 struct disk_partition *partitions, int parts_count)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100630{
Lukasz Majewski60119122017-10-27 12:28:10 +0200631 gpt_header *gpt_h;
Egbert Eich071e4232013-04-09 06:03:36 +0000632 gpt_entry *gpt_e;
Lukasz Majewski60119122017-10-27 12:28:10 +0200633 int ret, size;
Egbert Eich071e4232013-04-09 06:03:36 +0000634
Simon Glass1369ad42023-08-24 13:55:27 -0600635 size = PAD_TO_BLOCKSIZE(sizeof(gpt_header), desc);
Lukasz Majewski60119122017-10-27 12:28:10 +0200636 gpt_h = malloc_cache_aligned(size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100637 if (gpt_h == NULL) {
Simon Glassa6265b52022-10-20 18:22:40 -0600638 log_debug("calloc failed!\n");
639 return -ENOMEM;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100640 }
Lukasz Majewski60119122017-10-27 12:28:10 +0200641 memset(gpt_h, 0, size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100642
Lukasz Majewski60119122017-10-27 12:28:10 +0200643 size = PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS * sizeof(gpt_entry),
Simon Glass1369ad42023-08-24 13:55:27 -0600644 desc);
Lukasz Majewski60119122017-10-27 12:28:10 +0200645 gpt_e = malloc_cache_aligned(size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100646 if (gpt_e == NULL) {
Simon Glassa6265b52022-10-20 18:22:40 -0600647 log_debug("calloc failed!\n");
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100648 free(gpt_h);
Simon Glassa6265b52022-10-20 18:22:40 -0600649 return -ENOMEM;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100650 }
Lukasz Majewski60119122017-10-27 12:28:10 +0200651 memset(gpt_e, 0, size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100652
653 /* Generate Primary GPT header (LBA1) */
Simon Glass1369ad42023-08-24 13:55:27 -0600654 ret = gpt_fill_header(desc, gpt_h, str_disk_guid, parts_count);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100655 if (ret)
656 goto err;
657
658 /* Generate partition entries */
Simon Glass1369ad42023-08-24 13:55:27 -0600659 ret = gpt_fill_pte(desc, gpt_h, gpt_e, partitions, parts_count);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100660 if (ret)
661 goto err;
662
663 /* Write GPT partition table */
Simon Glass1369ad42023-08-24 13:55:27 -0600664 ret = write_gpt_table(desc, gpt_h, gpt_e);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100665
666err:
667 free(gpt_e);
668 free(gpt_h);
669 return ret;
670}
Steve Rae7d059342014-12-12 15:51:54 -0800671
Heinrich Schuchardt7cfc0332019-07-14 18:12:32 +0200672/**
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 */
681static void gpt_convert_efi_name_to_char(char *s, void *es, int n)
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100682{
Heinrich Schuchardt7cfc0332019-07-14 18:12:32 +0200683 char *ess = es;
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100684 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 Glass1369ad42023-08-24 13:55:27 -0600695int gpt_verify_headers(struct blk_desc *desc, gpt_header *gpt_head,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100696 gpt_entry **gpt_pte)
697{
698 /*
699 * This function validates AND
700 * fills in the GPT header and PTE
701 */
Simon Glass1369ad42023-08-24 13:55:27 -0600702 if (is_gpt_valid(desc,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100703 GPT_PRIMARY_PARTITION_TABLE_LBA,
704 gpt_head, gpt_pte) != 1) {
Simon Glassa6265b52022-10-20 18:22:40 -0600705 log_debug("Invalid GPT\n");
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100706 return -1;
707 }
Eugeniu Rosca27901b92019-04-30 04:53:45 +0200708
709 /* Free pte before allocating again */
710 free(*gpt_pte);
711
Stefan Herbrechtsmeier76df0732021-03-08 16:07:11 +0000712 /*
713 * Check that the alternate_lba entry points to the last LBA
714 */
Simon Glass1369ad42023-08-24 13:55:27 -0600715 if (le64_to_cpu(gpt_head->alternate_lba) != (desc->lba - 1)) {
Simon Glassa6265b52022-10-20 18:22:40 -0600716 log_debug("Misplaced Backup GPT\n");
Stefan Herbrechtsmeier76df0732021-03-08 16:07:11 +0000717 return -1;
718 }
719
Simon Glass1369ad42023-08-24 13:55:27 -0600720 if (is_gpt_valid(desc, (desc->lba - 1),
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100721 gpt_head, gpt_pte) != 1) {
Simon Glassa6265b52022-10-20 18:22:40 -0600722 log_debug("Invalid Backup GPT\n");
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100723 return -1;
724 }
725
726 return 0;
727}
728
Simon Glass1369ad42023-08-24 13:55:27 -0600729static void restore_primary_gpt_header(gpt_header *gpt_h, struct blk_desc *desc)
Philippe Reynes8b054f52022-04-22 17:46:48 +0200730{
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 Glass1369ad42023-08-24 13:55:27 -0600738 gpt_h->partition_entry_lba = cpu_to_le64(partition_entries_offset(desc));
Philippe Reynes8b054f52022-04-22 17:46:48 +0200739
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 Glass1369ad42023-08-24 13:55:27 -0600747static int write_one_gpt_table(struct blk_desc *desc, gpt_header *gpt_h,
748 gpt_entry *gpt_e)
Philippe Reynes8b054f52022-04-22 17:46:48 +0200749{
750 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
Simon Glass1369ad42023-08-24 13:55:27 -0600751 * sizeof(gpt_entry)), desc);
Philippe Reynes8b054f52022-04-22 17:46:48 +0200752 lbaint_t start;
753 int ret = 0;
754
755 start = le64_to_cpu(gpt_h->my_lba);
Simon Glass1369ad42023-08-24 13:55:27 -0600756 if (blk_dwrite(desc, start, 1, gpt_h) != 1) {
Philippe Reynes8b054f52022-04-22 17:46:48 +0200757 ret = -1;
758 goto out;
759 }
760
761 start = le64_to_cpu(gpt_h->partition_entry_lba);
Simon Glass1369ad42023-08-24 13:55:27 -0600762 if (blk_dwrite(desc, start, pte_blk_cnt, gpt_e) != pte_blk_cnt) {
Philippe Reynes8b054f52022-04-22 17:46:48 +0200763 ret = -1;
764 goto out;
765 }
766
767 out:
768 return ret;
769}
770
Simon Glass1369ad42023-08-24 13:55:27 -0600771int gpt_repair_headers(struct blk_desc *desc)
Philippe Reynes8b054f52022-04-22 17:46:48 +0200772{
Simon Glass1369ad42023-08-24 13:55:27 -0600773 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 Reynes8b054f52022-04-22 17:46:48 +0200775 gpt_entry *gpt_e1 = NULL, *gpt_e2 = NULL;
776 int is_gpt1_valid, is_gpt2_valid;
777 int ret = -1;
778
Simon Glass1369ad42023-08-24 13:55:27 -0600779 is_gpt1_valid = is_gpt_valid(desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Philippe Reynes8b054f52022-04-22 17:46:48 +0200780 gpt_h1, &gpt_e1);
Simon Glass1369ad42023-08-24 13:55:27 -0600781 is_gpt2_valid = is_gpt_valid(desc, desc->lba - 1,
Philippe Reynes8b054f52022-04-22 17:46:48 +0200782 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 Glass1369ad42023-08-24 13:55:27 -0600791 ret = write_one_gpt_table(desc, gpt_h1, gpt_e1);
Philippe Reynes8b054f52022-04-22 17:46:48 +0200792 goto out;
793 }
794
795 if (!is_gpt1_valid && is_gpt2_valid) {
Simon Glass1369ad42023-08-24 13:55:27 -0600796 restore_primary_gpt_header(gpt_h2, desc);
797 ret = write_one_gpt_table(desc, gpt_h2, gpt_e2);
Philippe Reynes8b054f52022-04-22 17:46:48 +0200798 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 Glass1369ad42023-08-24 13:55:27 -0600815int gpt_verify_partitions(struct blk_desc *desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600816 struct disk_partition *partitions, int parts,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100817 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 Glass1369ad42023-08-24 13:55:27 -0600824 ret = gpt_verify_headers(desc, gpt_head, gpt_pte);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100825 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 Yamada81e10422017-09-16 14:10:41 +0900832 pr_err("More partitions than allowed!\n");
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100833 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 Yamada81e10422017-09-16 14:10:41 +0900845 pr_err("Partition name: %s does not match %s!\n",
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100846 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 Glass03d7b602016-02-29 15:25:36 -0700854 (unsigned long long)gpt_part_size,
855 (unsigned long long)partitions[i].size);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100856
857 if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
Kever Yangf1d070d2016-07-29 11:12:18 +0800858 /* We do not check the extend partition size */
859 if ((i == parts - 1) && (partitions[i].size == 0))
860 continue;
861
Masahiro Yamada81e10422017-09-16 14:10:41 +0900862 pr_err("Partition %s size: %llu does not match %llu!\n",
Simon Glass03d7b602016-02-29 15:25:36 -0700863 efi_str, (unsigned long long)gpt_part_size,
864 (unsigned long long)partitions[i].size);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100865 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 Glass03d7b602016-02-29 15:25:36 -0700880 (unsigned long long)partitions[i].start);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100881
882 if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900883 pr_err("Partition %s start: %llu does not match %llu!\n",
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100884 efi_str, le64_to_cpu(gpt_e[i].starting_lba),
Simon Glass03d7b602016-02-29 15:25:36 -0700885 (unsigned long long)partitions[i].start);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100886 return -1;
887 }
888 }
889
890 return 0;
891}
892
Simon Glass1369ad42023-08-24 13:55:27 -0600893int is_valid_gpt_buf(struct blk_desc *desc, void *buf)
Steve Rae7d059342014-12-12 15:51:54 -0800894{
895 gpt_header *gpt_h;
896 gpt_entry *gpt_e;
897
898 /* determine start of GPT Header in the buffer */
Simon Glass1369ad42023-08-24 13:55:27 -0600899 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * desc->blksz);
Steve Rae7d059342014-12-12 15:51:54 -0800900 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
Simon Glass1369ad42023-08-24 13:55:27 -0600901 desc->lba))
Steve Rae7d059342014-12-12 15:51:54 -0800902 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 Glass1369ad42023-08-24 13:55:27 -0600906 desc->blksz);
Steve Rae7d059342014-12-12 15:51:54 -0800907 if (validate_gpt_entries(gpt_h, gpt_e))
908 return -1;
909
910 return 0;
911}
912
Simon Glass1369ad42023-08-24 13:55:27 -0600913int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)
Steve Rae7d059342014-12-12 15:51:54 -0800914{
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 Glass1369ad42023-08-24 13:55:27 -0600921 if (is_valid_gpt_buf(desc, buf))
Steve Rae7d059342014-12-12 15:51:54 -0800922 return -1;
923
924 /* determine start of GPT Header in the buffer */
Simon Glass1369ad42023-08-24 13:55:27 -0600925 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * desc->blksz);
Steve Rae7d059342014-12-12 15:51:54 -0800926
927 /* determine start of GPT Entries in the buffer */
Simon Glass1369ad42023-08-24 13:55:27 -0600928 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) * desc->blksz);
Steve Rae7d059342014-12-12 15:51:54 -0800929 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
930 le32_to_cpu(gpt_h->sizeof_partition_entry)),
Simon Glass1369ad42023-08-24 13:55:27 -0600931 desc);
Steve Rae7d059342014-12-12 15:51:54 -0800932
933 /* write MBR */
934 lba = 0; /* MBR is always at 0 */
935 cnt = 1; /* MBR (1 block) */
Simon Glass1369ad42023-08-24 13:55:27 -0600936 if (blk_dwrite(desc, lba, cnt, buf) != cnt) {
Simon Glassa6265b52022-10-20 18:22:40 -0600937 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
938 "MBR", cnt, lba);
Steve Rae7d059342014-12-12 15:51:54 -0800939 return 1;
940 }
941
942 /* write Primary GPT */
943 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
944 cnt = 1; /* GPT Header (1 block) */
Simon Glass1369ad42023-08-24 13:55:27 -0600945 if (blk_dwrite(desc, lba, cnt, gpt_h) != cnt) {
Simon Glassa6265b52022-10-20 18:22:40 -0600946 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
947 "Primary GPT Header", cnt, lba);
Steve Rae7d059342014-12-12 15:51:54 -0800948 return 1;
949 }
950
951 lba = le64_to_cpu(gpt_h->partition_entry_lba);
952 cnt = gpt_e_blk_cnt;
Simon Glass1369ad42023-08-24 13:55:27 -0600953 if (blk_dwrite(desc, lba, cnt, gpt_e) != cnt) {
Simon Glassa6265b52022-10-20 18:22:40 -0600954 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
955 "Primary GPT Entries", cnt, lba);
Steve Rae7d059342014-12-12 15:51:54 -0800956 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 Glass1369ad42023-08-24 13:55:27 -0600964 if (blk_dwrite(desc, lba, cnt, gpt_e) != cnt) {
Simon Glassa6265b52022-10-20 18:22:40 -0600965 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
966 "Backup GPT Entries", cnt, lba);
Steve Rae7d059342014-12-12 15:51:54 -0800967 return 1;
968 }
969
970 lba = le64_to_cpu(gpt_h->my_lba);
971 cnt = 1; /* GPT Header (1 block) */
Simon Glass1369ad42023-08-24 13:55:27 -0600972 if (blk_dwrite(desc, lba, cnt, gpt_h) != cnt) {
Simon Glassa6265b52022-10-20 18:22:40 -0600973 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
974 "Backup GPT Header", cnt, lba);
Steve Rae7d059342014-12-12 15:51:54 -0800975 return 1;
976 }
977
Gary Bissonb5fe1992021-01-26 14:56:23 +0100978 /* Update the partition table entries*/
Simon Glass1369ad42023-08-24 13:55:27 -0600979 part_init(desc);
Gary Bissonb5fe1992021-01-26 14:56:23 +0100980
Steve Rae7d059342014-12-12 15:51:54 -0800981 return 0;
982}
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100983#endif
984
richardretanubune6745592008-09-26 11:13:22 -0400985/*
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 */
993static int pmbr_part_valid(struct partition *part)
994{
995 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich20ca3ad2013-03-29 07:57:10 +0000996 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubune6745592008-09-26 11:13:22 -0400997 return 1;
998 }
999
1000 return 0;
1001}
1002
1003/*
1004 * is_pmbr_valid(): test Protective MBR for validity
1005 *
Simon Glass9137f662023-08-24 13:55:34 -06001006 * @mbr: Pointer to Master Boot-Record data
1007 *
richardretanubune6745592008-09-26 11:13:22 -04001008 * 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 Glass9137f662023-08-24 13:55:34 -06001013static int is_pmbr_valid(legacy_mbr *mbr)
richardretanubune6745592008-09-26 11:13:22 -04001014{
Simon Glass9137f662023-08-24 13:55:34 -06001015 uint sig = le16_to_cpu(mbr->signature);
richardretanubune6745592008-09-26 11:13:22 -04001016 int i = 0;
1017
Simon Glass9137f662023-08-24 13:55:34 -06001018 if (sig != MSDOS_MBR_SIGNATURE) {
1019 log_debug("Invalid signature %x\n", sig);
richardretanubune6745592008-09-26 11:13:22 -04001020 return 0;
Simon Glass9137f662023-08-24 13:55:34 -06001021 }
1022 log_debug("Signature %x valid\n", sig);
richardretanubune6745592008-09-26 11:13:22 -04001023
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 Rannikko54950fa2019-04-11 20:27:51 +00001039 * Description: returns 1 if valid, 0 on error, 2 if ignored header
richardretanubune6745592008-09-26 11:13:22 -04001040 * If valid, returns pointers to PTEs.
1041 */
Simon Glass1369ad42023-08-24 13:55:27 -06001042static int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,
1043 gpt_entry **pgpt_pte)
richardretanubune6745592008-09-26 11:13:22 -04001044{
Tom Rinie6131f12017-10-03 09:38:44 -04001045 /* Confirm valid arguments prior to allocation. */
Simon Glass1369ad42023-08-24 13:55:27 -06001046 if (!desc || !pgpt_head) {
Simon Glassa6265b52022-10-20 18:22:40 -06001047 log_debug("Invalid Argument(s)\n");
richardretanubune6745592008-09-26 11:13:22 -04001048 return 0;
1049 }
1050
Simon Glass1369ad42023-08-24 13:55:27 -06001051 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, mbr, 1, desc->blksz);
Tom Rinie6131f12017-10-03 09:38:44 -04001052
Peter Jonesc27e1c12017-09-13 18:05:25 -04001053 /* Read MBR Header from device */
Simon Glass1369ad42023-08-24 13:55:27 -06001054 if (blk_dread(desc, 0, 1, (ulong *)mbr) != 1) {
Simon Glassa6265b52022-10-20 18:22:40 -06001055 log_debug("Can't read MBR header\n");
Peter Jonesc27e1c12017-09-13 18:05:25 -04001056 return 0;
1057 }
1058
richardretanubune6745592008-09-26 11:13:22 -04001059 /* Read GPT Header from device */
Simon Glass1369ad42023-08-24 13:55:27 -06001060 if (blk_dread(desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
Simon Glassa6265b52022-10-20 18:22:40 -06001061 log_debug("Can't read GPT header\n");
richardretanubune6745592008-09-26 11:13:22 -04001062 return 0;
1063 }
1064
Urja Rannikko54950fa2019-04-11 20:27:51 +00001065 /* Invalid but nothing to yell about. */
1066 if (le64_to_cpu(pgpt_head->signature) == GPT_HEADER_CHROMEOS_IGNORE) {
Simon Glassa6265b52022-10-20 18:22:40 -06001067 log_debug("ChromeOS 'IGNOREME' GPT header found and ignored\n");
Urja Rannikko54950fa2019-04-11 20:27:51 +00001068 return 2;
1069 }
1070
Simon Glass1369ad42023-08-24 13:55:27 -06001071 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, desc->lba))
richardretanubune6745592008-09-26 11:13:22 -04001072 return 0;
richardretanubune6745592008-09-26 11:13:22 -04001073
Simon Glass1369ad42023-08-24 13:55:27 -06001074 if (desc->sig_type == SIG_TYPE_NONE) {
Peter Jonesc27e1c12017-09-13 18:05:25 -04001075 efi_guid_t empty = {};
1076 if (memcmp(&pgpt_head->disk_guid, &empty, sizeof(empty))) {
Simon Glass1369ad42023-08-24 13:55:27 -06001077 desc->sig_type = SIG_TYPE_GUID;
1078 memcpy(&desc->guid_sig, &pgpt_head->disk_guid,
1079 sizeof(empty));
Peter Jonesc27e1c12017-09-13 18:05:25 -04001080 } else if (mbr->unique_mbr_signature != 0) {
Simon Glass1369ad42023-08-24 13:55:27 -06001081 desc->sig_type = SIG_TYPE_MBR;
1082 desc->mbr_sig = mbr->unique_mbr_signature;
Peter Jonesc27e1c12017-09-13 18:05:25 -04001083 }
1084 }
1085
richardretanubune6745592008-09-26 11:13:22 -04001086 /* Read and allocate Partition Table Entries */
Simon Glass1369ad42023-08-24 13:55:27 -06001087 *pgpt_pte = alloc_read_gpt_entries(desc, pgpt_head);
Heinrich Schuchardtaaeb39a2022-05-09 21:35:44 +02001088 if (!*pgpt_pte)
richardretanubune6745592008-09-26 11:13:22 -04001089 return 0;
richardretanubune6745592008-09-26 11:13:22 -04001090
Steve Rae6f5e17d2014-12-18 12:13:42 +01001091 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
Doug Anderson698a51d2011-10-19 09:47:31 +00001092 free(*pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -04001093 return 0;
1094 }
1095
1096 /* We're done, all's well */
1097 return 1;
1098}
1099
1100/**
Urja Rannikko80bded72019-04-11 20:27:50 +00001101 * 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 Glass1369ad42023-08-24 13:55:27 -06001109static int find_valid_gpt(struct blk_desc *desc, gpt_header *gpt_head,
Urja Rannikko80bded72019-04-11 20:27:50 +00001110 gpt_entry **pgpt_pte)
1111{
Urja Rannikko54950fa2019-04-11 20:27:51 +00001112 int r;
1113
Simon Glass1369ad42023-08-24 13:55:27 -06001114 r = is_gpt_valid(desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_head,
Urja Rannikko54950fa2019-04-11 20:27:51 +00001115 pgpt_pte);
1116
1117 if (r != 1) {
1118 if (r != 2)
Simon Glassa6265b52022-10-20 18:22:40 -06001119 log_debug("Invalid GPT\n");
Urja Rannikko54950fa2019-04-11 20:27:51 +00001120
Simon Glass1369ad42023-08-24 13:55:27 -06001121 if (is_gpt_valid(desc, desc->lba - 1, gpt_head, pgpt_pte)
1122 != 1) {
Simon Glassa6265b52022-10-20 18:22:40 -06001123 log_debug("Invalid Backup GPT\n");
Urja Rannikko80bded72019-04-11 20:27:50 +00001124 return 0;
1125 }
Urja Rannikko54950fa2019-04-11 20:27:51 +00001126 if (r != 2)
Simon Glassa6265b52022-10-20 18:22:40 -06001127 log_debug(" Using Backup GPT\n");
Urja Rannikko80bded72019-04-11 20:27:50 +00001128 }
1129 return 1;
1130}
1131
1132/**
richardretanubune6745592008-09-26 11:13:22 -04001133 * alloc_read_gpt_entries(): reads partition entries from disk
Simon Glass1369ad42023-08-24 13:55:27 -06001134 * @desc
richardretanubune6745592008-09-26 11:13:22 -04001135 * @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 Glass1369ad42023-08-24 13:55:27 -06001141static gpt_entry *alloc_read_gpt_entries(struct blk_desc *desc,
Simon Glasse3394752016-02-29 15:25:34 -07001142 gpt_header *pgpt_head)
richardretanubune6745592008-09-26 11:13:22 -04001143{
Egbert Eich071e4232013-04-09 06:03:36 +00001144 size_t count = 0, blk_cnt;
Stephen Warrene73f2962015-12-07 11:38:48 -07001145 lbaint_t blk;
richardretanubune6745592008-09-26 11:13:22 -04001146 gpt_entry *pte = NULL;
1147
Simon Glass1369ad42023-08-24 13:55:27 -06001148 if (!desc || !pgpt_head) {
Simon Glassa6265b52022-10-20 18:22:40 -06001149 log_debug("Invalid Argument(s)\n");
richardretanubune6745592008-09-26 11:13:22 -04001150 return NULL;
1151 }
1152
Chang Hyun Park823ffde2012-12-11 11:09:45 +01001153 count = le32_to_cpu(pgpt_head->num_partition_entries) *
1154 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubune6745592008-09-26 11:13:22 -04001155
Simon Glassa6265b52022-10-20 18:22:40 -06001156 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);
richardretanubune6745592008-09-26 11:13:22 -04001160
1161 /* Allocate memory for PTE, remember to FREE */
1162 if (count != 0) {
Egbert Eich071e4232013-04-09 06:03:36 +00001163 pte = memalign(ARCH_DMA_MINALIGN,
Simon Glass1369ad42023-08-24 13:55:27 -06001164 PAD_TO_BLOCKSIZE(count, desc));
richardretanubune6745592008-09-26 11:13:22 -04001165 }
1166
1167 if (count == 0 || pte == NULL) {
Simon Glassa6265b52022-10-20 18:22:40 -06001168 log_debug("ERROR: Can't allocate %#lX bytes for GPT Entries\n",
1169 (ulong)count);
richardretanubune6745592008-09-26 11:13:22 -04001170 return NULL;
1171 }
1172
1173 /* Read GPT Entries from device */
Stephen Warrene73f2962015-12-07 11:38:48 -07001174 blk = le64_to_cpu(pgpt_head->partition_entry_lba);
Simon Glass1369ad42023-08-24 13:55:27 -06001175 blk_cnt = BLOCK_CNT(count, desc);
1176 if (blk_dread(desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
Simon Glassa6265b52022-10-20 18:22:40 -06001177 log_debug("Can't read GPT Entries\n");
richardretanubune6745592008-09-26 11:13:22 -04001178 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 */
1190static int is_pte_valid(gpt_entry * pte)
1191{
1192 efi_guid_t unused_guid;
1193
1194 if (!pte) {
Simon Glassa6265b52022-10-20 18:22:40 -06001195 log_debug("Invalid Argument(s)\n");
richardretanubune6745592008-09-26 11:13:22 -04001196 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 Glassa6265b52022-10-20 18:22:40 -06001207 log_debug("Found an unused PTE GUID at 0x%08X\n",
1208 (unsigned int)(uintptr_t)pte);
richardretanubune6745592008-09-26 11:13:22 -04001209
1210 return 0;
1211 } else {
1212 return 1;
1213 }
1214}
Simon Glass83ce5632016-02-29 15:25:47 -07001215
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 */
1221U_BOOT_PART_TYPE(a_efi) = {
1222 .name = "EFI",
1223 .part_type = PART_TYPE_EFI,
Petr Kulhavy712257e2016-09-09 10:27:15 +02001224 .max_entries = GPT_ENTRY_NUMBERS,
Simon Glassb89a8442016-02-29 15:25:48 -07001225 .get_info = part_get_info_ptr(part_get_info_efi),
Simon Glass0f1c9522016-02-29 15:26:04 -07001226 .print = part_print_ptr(part_print_efi),
1227 .test = part_test_efi,
Simon Glass83ce5632016-02-29 15:25:47 -07001228};