blob: fdca91a69747c24bf1c36903f712aec8d5387135 [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 Glass655306c2020-05-10 11:39:58 -060012#include <common.h>
13#include <blk.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060015#include <part.h>
Simon Glass6f044982020-05-10 11:39:52 -060016#include <uuid.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Marc Dietrich20ca3ad2013-03-29 07:57:10 +000019#include <asm/unaligned.h>
richardretanubune6745592008-09-26 11:13:22 -040020#include <command.h>
Philipp Tomsicha3da0e92017-03-01 21:10:39 +010021#include <fdtdec.h>
richardretanubune6745592008-09-26 11:13:22 -040022#include <ide.h>
23#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060024#include <memalign.h>
Chang Hyun Park823ffde2012-12-11 11:09:45 +010025#include <part_efi.h>
Philipp Tomsicha3da0e92017-03-01 21:10:39 +010026#include <linux/compiler.h>
Lei Wen757ddfe2011-09-07 18:11:19 +000027#include <linux/ctype.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070028#include <u-boot/crc.h>
richardretanubune6745592008-09-26 11:13:22 -040029
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +010030DECLARE_GLOBAL_DATA_PTR;
31
Simon Glass56d59a62021-07-02 12:36:14 -060032#ifdef CONFIG_HAVE_BLOCK_DEVICE
33
34/* GUID for basic data partitons */
35#if CONFIG_IS_ENABLED(EFI_PARTITION)
Heinrich Schuchardtfac78672018-06-09 17:50:18 +020036static const efi_guid_t partition_basic_data_guid = PARTITION_BASIC_DATA_GUID;
Simon Glass56d59a62021-07-02 12:36:14 -060037#endif
Heinrich Schuchardtfac78672018-06-09 17:50:18 +020038
richardretanubune6745592008-09-26 11:13:22 -040039/**
40 * efi_crc32() - EFI version of crc32 function
41 * @buf: buffer to calculate crc32 of
42 * @len - length of buf
43 *
44 * Description: Returns EFI-style CRC32 value for @buf
45 */
Chang Hyun Park823ffde2012-12-11 11:09:45 +010046static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubune6745592008-09-26 11:13:22 -040047{
48 return crc32(0, buf, len);
49}
50
51/*
52 * Private function prototypes
53 */
54
55static int pmbr_part_valid(struct partition *part);
56static int is_pmbr_valid(legacy_mbr * mbr);
Simon Glasse3394752016-02-29 15:25:34 -070057static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
Steve Raed30cbae2014-05-26 11:52:23 -070058 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
Simon Glasse3394752016-02-29 15:25:34 -070059static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
60 gpt_header *pgpt_head);
richardretanubune6745592008-09-26 11:13:22 -040061static int is_pte_valid(gpt_entry * pte);
Urja Rannikko80bded72019-04-11 20:27:50 +000062static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head,
63 gpt_entry **pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -040064
Lei Wen757ddfe2011-09-07 18:11:19 +000065static char *print_efiname(gpt_entry *pte)
66{
67 static char name[PARTNAME_SZ + 1];
68 int i;
69 for (i = 0; i < PARTNAME_SZ; i++) {
70 u8 c;
71 c = pte->partition_name[i] & 0xff;
72 c = (c && !isprint(c)) ? '.' : c;
73 name[i] = c;
74 }
75 name[PARTNAME_SZ] = 0;
76 return name;
77}
78
Heinrich Schuchardt35ea8682019-01-12 11:25:25 +010079static const efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
Stephen Warrend0cf3b62012-10-08 08:14:37 +000080
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010081static int get_bootable(gpt_entry *p)
Stephen Warrend0cf3b62012-10-08 08:14:37 +000082{
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010083 int ret = 0;
84
85 if (!memcmp(&p->partition_type_guid, &system_guid, sizeof(efi_guid_t)))
86 ret |= PART_EFI_SYSTEM_PARTITION;
87 if (p->attributes.fields.legacy_bios_bootable)
88 ret |= PART_BOOTABLE;
89 return ret;
Stephen Warrend0cf3b62012-10-08 08:14:37 +000090}
91
Steve Rae6f5e17d2014-12-18 12:13:42 +010092static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba,
93 lbaint_t lastlba)
94{
95 uint32_t crc32_backup = 0;
96 uint32_t calc_crc32;
97
98 /* Check the GPT header signature */
Simon Glass7903d212018-10-01 12:22:35 -060099 if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE_UBOOT) {
Steve Rae6f5e17d2014-12-18 12:13:42 +0100100 printf("%s signature is wrong: 0x%llX != 0x%llX\n",
101 "GUID Partition Table Header",
102 le64_to_cpu(gpt_h->signature),
Simon Glass7903d212018-10-01 12:22:35 -0600103 GPT_HEADER_SIGNATURE_UBOOT);
Steve Rae6f5e17d2014-12-18 12:13:42 +0100104 return -1;
105 }
106
107 /* Check the GUID Partition Table CRC */
108 memcpy(&crc32_backup, &gpt_h->header_crc32, sizeof(crc32_backup));
109 memset(&gpt_h->header_crc32, 0, sizeof(gpt_h->header_crc32));
110
111 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
112 le32_to_cpu(gpt_h->header_size));
113
114 memcpy(&gpt_h->header_crc32, &crc32_backup, sizeof(crc32_backup));
115
116 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
117 printf("%s CRC is wrong: 0x%x != 0x%x\n",
118 "GUID Partition Table Header",
119 le32_to_cpu(crc32_backup), calc_crc32);
120 return -1;
121 }
122
123 /*
124 * Check that the my_lba entry points to the LBA that contains the GPT
125 */
126 if (le64_to_cpu(gpt_h->my_lba) != lba) {
127 printf("GPT: my_lba incorrect: %llX != " LBAF "\n",
128 le64_to_cpu(gpt_h->my_lba),
129 lba);
130 return -1;
131 }
132
133 /*
134 * Check that the first_usable_lba and that the last_usable_lba are
135 * within the disk.
136 */
137 if (le64_to_cpu(gpt_h->first_usable_lba) > lastlba) {
138 printf("GPT: first_usable_lba incorrect: %llX > " LBAF "\n",
139 le64_to_cpu(gpt_h->first_usable_lba), lastlba);
140 return -1;
141 }
142 if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) {
143 printf("GPT: last_usable_lba incorrect: %llX > " LBAF "\n",
144 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
145 return -1;
146 }
147
148 debug("GPT: first_usable_lba: %llX last_usable_lba: %llX last lba: "
149 LBAF "\n", le64_to_cpu(gpt_h->first_usable_lba),
150 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
151
152 return 0;
153}
154
155static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
156{
157 uint32_t calc_crc32;
158
159 /* Check the GUID Partition Table Entry Array CRC */
160 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
161 le32_to_cpu(gpt_h->num_partition_entries) *
162 le32_to_cpu(gpt_h->sizeof_partition_entry));
163
164 if (calc_crc32 != le32_to_cpu(gpt_h->partition_entry_array_crc32)) {
165 printf("%s: 0x%x != 0x%x\n",
166 "GUID Partition Table Entry Array CRC is wrong",
167 le32_to_cpu(gpt_h->partition_entry_array_crc32),
168 calc_crc32);
169 return -1;
170 }
171
172 return 0;
173}
174
175static void prepare_backup_gpt_header(gpt_header *gpt_h)
176{
177 uint32_t calc_crc32;
178 uint64_t val;
179
180 /* recalculate the values for the Backup GPT Header */
181 val = le64_to_cpu(gpt_h->my_lba);
182 gpt_h->my_lba = gpt_h->alternate_lba;
183 gpt_h->alternate_lba = cpu_to_le64(val);
Steve Rae7d059342014-12-12 15:51:54 -0800184 gpt_h->partition_entry_lba =
185 cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
Steve Rae6f5e17d2014-12-18 12:13:42 +0100186 gpt_h->header_crc32 = 0;
187
188 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
189 le32_to_cpu(gpt_h->header_size));
190 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
191}
192
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +0100193#if CONFIG_IS_ENABLED(EFI_PARTITION)
richardretanubune6745592008-09-26 11:13:22 -0400194/*
195 * Public Functions (include/part.h)
196 */
197
Alison Chaikene4222582017-06-25 16:43:23 -0700198/*
199 * UUID is displayed as 32 hexadecimal digits, in 5 groups,
200 * separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters
201 */
202int get_disk_guid(struct blk_desc * dev_desc, char *guid)
203{
204 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
205 gpt_entry *gpt_pte = NULL;
206 unsigned char *guid_bin;
207
208 /* This function validates AND fills in the GPT header and PTE */
Urja Rannikko80bded72019-04-11 20:27:50 +0000209 if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1)
210 return -EINVAL;
Alison Chaikene4222582017-06-25 16:43:23 -0700211
212 guid_bin = gpt_head->disk_guid.b;
213 uuid_bin_to_str(guid_bin, guid, UUID_STR_FORMAT_GUID);
214
Eugeniu Roscaeb11c2e2019-04-30 04:53:44 +0200215 /* Remember to free pte */
216 free(gpt_pte);
Alison Chaikene4222582017-06-25 16:43:23 -0700217 return 0;
218}
219
Simon Glass0f1c9522016-02-29 15:26:04 -0700220void part_print_efi(struct blk_desc *dev_desc)
richardretanubune6745592008-09-26 11:13:22 -0400221{
Egbert Eich071e4232013-04-09 06:03:36 +0000222 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000223 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400224 int i = 0;
Alison Chaiken2cb47e62017-06-25 16:43:17 -0700225 char uuid[UUID_STR_LEN + 1];
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200226 unsigned char *uuid_bin;
richardretanubune6745592008-09-26 11:13:22 -0400227
richardretanubune6745592008-09-26 11:13:22 -0400228 /* This function validates AND fills in the GPT header and PTE */
Urja Rannikko80bded72019-04-11 20:27:50 +0000229 if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1)
230 return;
richardretanubune6745592008-09-26 11:13:22 -0400231
Doug Anderson698a51d2011-10-19 09:47:31 +0000232 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400233
Stephen Warren26163262012-10-08 08:14:33 +0000234 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000235 printf("\tAttributes\n");
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200236 printf("\tType GUID\n");
237 printf("\tPartition GUID\n");
Stephen Warren8b855b52012-10-08 08:14:34 +0000238
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100239 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrene6df59e2012-10-08 08:14:32 +0000240 /* Stop at the first non valid PTE */
241 if (!is_pte_valid(&gpt_pte[i]))
242 break;
richardretanubune6745592008-09-26 11:13:22 -0400243
Stephen Warren26163262012-10-08 08:14:33 +0000244 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100245 le64_to_cpu(gpt_pte[i].starting_lba),
246 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warren26163262012-10-08 08:14:33 +0000247 print_efiname(&gpt_pte[i]));
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000248 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200249 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200250 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
Stephen Warren8b855b52012-10-08 08:14:34 +0000251 printf("\ttype:\t%s\n", uuid);
Rasmus Villemoes7b70bdc2020-11-20 11:45:35 +0100252 if (CONFIG_IS_ENABLED(PARTITION_TYPE_GUID)) {
253 const char *type = uuid_guid_get_str(uuid_bin);
254 if (type)
255 printf("\ttype:\t%s\n", type);
256 }
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200257 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200258 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
259 printf("\tguid:\t%s\n", uuid);
richardretanubune6745592008-09-26 11:13:22 -0400260 }
261
262 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000263 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400264 return;
265}
266
Simon Glassb89a8442016-02-29 15:25:48 -0700267int part_get_info_efi(struct blk_desc *dev_desc, int part,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600268 struct disk_partition *info)
richardretanubune6745592008-09-26 11:13:22 -0400269{
Egbert Eich071e4232013-04-09 06:03:36 +0000270 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000271 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400272
273 /* "part" argument must be at least 1 */
Simon Glassc5b65372016-03-16 07:45:36 -0600274 if (part < 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000275 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400276 return -1;
277 }
278
279 /* This function validates AND fills in the GPT header and PTE */
Urja Rannikko80bded72019-04-11 20:27:50 +0000280 if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1)
281 return -1;
richardretanubune6745592008-09-26 11:13:22 -0400282
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100283 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrene47dbba2012-09-21 09:50:58 +0000284 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorfe7775312013-09-10 15:14:56 -0500285 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrene47dbba2012-09-21 09:50:58 +0000286 __func__, part);
Mark Langsdorfe7775312013-09-10 15:14:56 -0500287 free(gpt_pte);
Stephen Warrene47dbba2012-09-21 09:50:58 +0000288 return -1;
289 }
290
Steve Raed30cbae2014-05-26 11:52:23 -0700291 /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
292 info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
Richard Retanubun413dfc72009-01-26 08:45:14 -0500293 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Steve Raed30cbae2014-05-26 11:52:23 -0700294 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
Richard Retanubun413dfc72009-01-26 08:45:14 -0500295 - info->start;
Egbert Eich071e4232013-04-09 06:03:36 +0000296 info->blksz = dev_desc->blksz;
richardretanubune6745592008-09-26 11:13:22 -0400297
Heinrich Schuchardt08299a92019-07-05 21:27:13 +0200298 snprintf((char *)info->name, sizeof(info->name), "%s",
299 print_efiname(&gpt_pte[part - 1]));
Ben Whitten34fd6c92015-12-30 13:05:58 +0000300 strcpy((char *)info->type, "U-Boot");
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100301 info->bootable = get_bootable(&gpt_pte[part - 1]);
Patrick Delaunay73287092017-01-27 11:00:42 +0100302#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200303 uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
304 UUID_STR_FORMAT_GUID);
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000305#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100306#ifdef CONFIG_PARTITION_TYPE_GUID
307 uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
308 info->type_guid, UUID_STR_FORMAT_GUID);
309#endif
richardretanubune6745592008-09-26 11:13:22 -0400310
Steve Raef7f10b82014-05-26 11:52:24 -0700311 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
Frederic Leroye7ee0282013-06-26 18:11:25 +0200312 info->start, info->size, info->name);
richardretanubune6745592008-09-26 11:13:22 -0400313
314 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000315 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400316 return 0;
317}
318
Simon Glass0f1c9522016-02-29 15:26:04 -0700319static int part_test_efi(struct blk_desc *dev_desc)
richardretanubune6745592008-09-26 11:13:22 -0400320{
Egbert Eich071e4232013-04-09 06:03:36 +0000321 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubune6745592008-09-26 11:13:22 -0400322
323 /* Read legacy MBR from block 0 and validate it */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700324 if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1)
Anton staaf3b3df4e2011-10-12 13:56:04 +0000325 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubune6745592008-09-26 11:13:22 -0400326 return -1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100327 }
328 return 0;
329}
330
331/**
332 * set_protective_mbr(): Set the EFI protective MBR
333 * @param dev_desc - block device descriptor
334 *
335 * @return - zero on success, otherwise error
336 */
Simon Glasse3394752016-02-29 15:25:34 -0700337static int set_protective_mbr(struct blk_desc *dev_desc)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100338{
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100339 /* Setup the Protective MBR */
Patrick Delaunaye54b5472017-11-17 10:08:18 +0100340 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, p_mbr, 1, dev_desc->blksz);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100341 if (p_mbr == NULL) {
342 printf("%s: calloc failed!\n", __func__);
343 return -1;
344 }
Vincent Tinelli35620492017-01-30 15:46:07 +0300345
346 /* Read MBR to backup boot code if it exists */
347 if (blk_dread(dev_desc, 0, 1, p_mbr) != 1) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900348 pr_err("** Can't read from device %d **\n", dev_desc->devnum);
Vincent Tinelli35620492017-01-30 15:46:07 +0300349 return -1;
350 }
351
Sam Protsenkoa3a51a42018-05-22 02:04:21 +0300352 /* Clear all data in MBR except of backed up boot code */
353 memset((char *)p_mbr + MSDOS_MBR_BOOT_CODE_SIZE, 0, sizeof(*p_mbr) -
354 MSDOS_MBR_BOOT_CODE_SIZE);
355
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100356 /* Append signature */
357 p_mbr->signature = MSDOS_MBR_SIGNATURE;
358 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
359 p_mbr->partition_record[0].start_sect = 1;
Maxime Ripard6d8a1522015-01-08 12:26:44 +0100360 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100361
362 /* Write MBR sector to the MMC device */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700363 if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) {
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100364 printf("** Can't write to device %d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700365 dev_desc->devnum);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100366 return -1;
367 }
368
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100369 return 0;
370}
371
Simon Glasse3394752016-02-29 15:25:34 -0700372int write_gpt_table(struct blk_desc *dev_desc,
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100373 gpt_header *gpt_h, gpt_entry *gpt_e)
374{
Egbert Eich071e4232013-04-09 06:03:36 +0000375 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
376 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100377 u32 calc_crc32;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100378
379 debug("max lba: %x\n", (u32) dev_desc->lba);
380 /* Setup the Protective MBR */
381 if (set_protective_mbr(dev_desc) < 0)
382 goto err;
383
384 /* Generate CRC for the Primary GPT Header */
385 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
386 le32_to_cpu(gpt_h->num_partition_entries) *
387 le32_to_cpu(gpt_h->sizeof_partition_entry));
388 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
389
390 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
391 le32_to_cpu(gpt_h->header_size));
392 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
393
394 /* Write the First GPT to the block right after the Legacy MBR */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700395 if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100396 goto err;
397
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100398 if (blk_dwrite(dev_desc, le64_to_cpu(gpt_h->partition_entry_lba),
399 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100400 goto err;
401
Steve Rae6f5e17d2014-12-18 12:13:42 +0100402 prepare_backup_gpt_header(gpt_h);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100403
Simon Glass2ee8ada2016-02-29 15:25:52 -0700404 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
405 + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100406 goto err;
407
Simon Glass2ee8ada2016-02-29 15:25:52 -0700408 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
409 gpt_h) != 1)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100410 goto err;
411
412 debug("GPT successfully written to block device!\n");
413 return 0;
414
415 err:
Simon Glass2f26fff2016-02-29 15:25:51 -0700416 printf("** Can't write to device %d **\n", dev_desc->devnum);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100417 return -1;
418}
419
Maxime Ripard0d390872017-08-23 16:01:32 +0200420int gpt_fill_pte(struct blk_desc *dev_desc,
421 gpt_header *gpt_h, gpt_entry *gpt_e,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600422 struct disk_partition *partitions, int parts)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100423{
Steve Raed30cbae2014-05-26 11:52:23 -0700424 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
Steve Raed30cbae2014-05-26 11:52:23 -0700425 lbaint_t last_usable_lba = (lbaint_t)
426 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100427 int i, k;
Marek Vasut941579e2013-05-19 12:53:34 +0000428 size_t efiname_len, dosname_len;
Patrick Delaunay73287092017-01-27 11:00:42 +0100429#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100430 char *str_uuid;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200431 unsigned char *bin_uuid;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100432#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100433#ifdef CONFIG_PARTITION_TYPE_GUID
434 char *str_type_guid;
435 unsigned char *bin_type_guid;
436#endif
Maxime Ripard7947c692017-08-23 16:01:33 +0200437 size_t hdr_start = gpt_h->my_lba;
438 size_t hdr_end = hdr_start + 1;
439
440 size_t pte_start = gpt_h->partition_entry_lba;
441 size_t pte_end = pte_start +
442 gpt_h->num_partition_entries * gpt_h->sizeof_partition_entry /
443 dev_desc->blksz;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100444
445 for (i = 0; i < parts; i++) {
446 /* partition starting lba */
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200447 lbaint_t start = partitions[i].start;
448 lbaint_t size = partitions[i].size;
449
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100450 if (start) {
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200451 offset = start + size;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100452 } else {
Maxime Ripard7947c692017-08-23 16:01:33 +0200453 start = offset;
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200454 offset += size;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100455 }
Maxime Ripard7947c692017-08-23 16:01:33 +0200456
457 /*
458 * If our partition overlaps with either the GPT
459 * header, or the partition entry, reject it.
460 */
Patrick Delaunaybe4a0fa2017-10-18 15:11:05 +0200461 if (((start < hdr_end && hdr_start < (start + size)) ||
462 (start < pte_end && pte_start < (start + size)))) {
Maxime Ripard7947c692017-08-23 16:01:33 +0200463 printf("Partition overlap\n");
464 return -1;
465 }
466
467 gpt_e[i].starting_lba = cpu_to_le64(start);
468
Patrick Delaunay10172f32016-05-02 14:43:33 +0200469 if (offset > (last_usable_lba + 1)) {
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100470 printf("Partitions layout exceds disk size\n");
471 return -1;
472 }
473 /* partition ending lba */
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200474 if ((i == parts - 1) && (size == 0))
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100475 /* extend the last partition to maximuim */
476 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
477 else
478 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
479
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100480#ifdef CONFIG_PARTITION_TYPE_GUID
481 str_type_guid = partitions[i].type_guid;
482 bin_type_guid = gpt_e[i].partition_type_guid.b;
483 if (strlen(str_type_guid)) {
484 if (uuid_str_to_bin(str_type_guid, bin_type_guid,
485 UUID_STR_FORMAT_GUID)) {
486 printf("Partition no. %d: invalid type guid: %s\n",
487 i, str_type_guid);
488 return -1;
489 }
490 } else {
491 /* default partition type GUID */
492 memcpy(bin_type_guid,
Heinrich Schuchardtfac78672018-06-09 17:50:18 +0200493 &partition_basic_data_guid, 16);
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100494 }
495#else
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100496 /* partition type GUID */
497 memcpy(gpt_e[i].partition_type_guid.b,
Heinrich Schuchardtfac78672018-06-09 17:50:18 +0200498 &partition_basic_data_guid, 16);
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100499#endif
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100500
Patrick Delaunay73287092017-01-27 11:00:42 +0100501#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100502 str_uuid = partitions[i].uuid;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200503 bin_uuid = gpt_e[i].unique_partition_guid.b;
504
Vincent Tinellibdfacea2017-02-27 16:11:15 +0200505 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_GUID)) {
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100506 printf("Partition no. %d: invalid guid: %s\n",
507 i, str_uuid);
508 return -1;
509 }
510#endif
511
512 /* partition attributes */
513 memset(&gpt_e[i].attributes, 0,
514 sizeof(gpt_entry_attributes));
515
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100516 if (partitions[i].bootable & PART_BOOTABLE)
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100517 gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
518
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100519 /* partition name */
Marek Vasut941579e2013-05-19 12:53:34 +0000520 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100521 / sizeof(efi_char16_t);
Marek Vasut941579e2013-05-19 12:53:34 +0000522 dosname_len = sizeof(partitions[i].name);
523
524 memset(gpt_e[i].partition_name, 0,
525 sizeof(gpt_e[i].partition_name));
526
527 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100528 gpt_e[i].partition_name[k] =
529 (efi_char16_t)(partitions[i].name[k]);
530
Steve Raed30cbae2014-05-26 11:52:23 -0700531 debug("%s: name: %s offset[%d]: 0x" LBAF
532 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100533 __func__, partitions[i].name, i,
Maxime Ripardbe4a6b82017-08-23 16:01:31 +0200534 offset, i, size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100535 }
536
537 return 0;
538}
539
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100540static uint32_t partition_entries_offset(struct blk_desc *dev_desc)
541{
542 uint32_t offset_blks = 2;
Maxime Ripard1d088f02017-08-23 16:01:30 +0200543 uint32_t __maybe_unused offset_bytes;
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100544 int __maybe_unused config_offset;
545
546#if defined(CONFIG_EFI_PARTITION_ENTRIES_OFF)
547 /*
548 * Some architectures require their SPL loader at a fixed
549 * address within the first 16KB of the disk. To avoid an
550 * overlap with the partition entries of the EFI partition
551 * table, the first safe offset (in bytes, from the start of
552 * the disk) for the entries can be set in
553 * CONFIG_EFI_PARTITION_ENTRIES_OFF.
554 */
Maxime Ripard1d088f02017-08-23 16:01:30 +0200555 offset_bytes =
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100556 PAD_TO_BLOCKSIZE(CONFIG_EFI_PARTITION_ENTRIES_OFF, dev_desc);
Maxime Ripard1d088f02017-08-23 16:01:30 +0200557 offset_blks = offset_bytes / dev_desc->blksz;
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100558#endif
559
560#if defined(CONFIG_OF_CONTROL)
561 /*
562 * Allow the offset of the first partition entires (in bytes
563 * from the start of the device) to be specified as a property
564 * of the device tree '/config' node.
565 */
566 config_offset = fdtdec_get_config_int(gd->fdt_blob,
567 "u-boot,efi-partition-entries-offset",
568 -EINVAL);
Maxime Ripard1d088f02017-08-23 16:01:30 +0200569 if (config_offset != -EINVAL) {
570 offset_bytes = PAD_TO_BLOCKSIZE(config_offset, dev_desc);
571 offset_blks = offset_bytes / dev_desc->blksz;
572 }
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100573#endif
574
575 debug("efi: partition entries offset (in blocks): %d\n", offset_blks);
576
577 /*
578 * The earliest LBA this can be at is LBA#2 (i.e. right behind
579 * the (protective) MBR and the GPT header.
580 */
581 if (offset_blks < 2)
582 offset_blks = 2;
583
584 return offset_blks;
585}
586
Simon Glasse3394752016-02-29 15:25:34 -0700587int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100588 char *str_guid, int parts_count)
589{
Simon Glass7903d212018-10-01 12:22:35 -0600590 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE_UBOOT);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100591 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
592 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
593 gpt_h->my_lba = cpu_to_le64(1);
594 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100595 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
Philipp Tomsicha3da0e92017-03-01 21:10:39 +0100596 gpt_h->partition_entry_lba =
597 cpu_to_le64(partition_entries_offset(dev_desc));
598 gpt_h->first_usable_lba =
599 cpu_to_le64(le64_to_cpu(gpt_h->partition_entry_lba) + 32);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100600 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
601 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
602 gpt_h->header_crc32 = 0;
603 gpt_h->partition_entry_array_crc32 = 0;
604
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200605 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100606 return -1;
607
608 return 0;
609}
610
Simon Glasse3394752016-02-29 15:25:34 -0700611int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600612 struct disk_partition *partitions, int parts_count)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100613{
Lukasz Majewski60119122017-10-27 12:28:10 +0200614 gpt_header *gpt_h;
Egbert Eich071e4232013-04-09 06:03:36 +0000615 gpt_entry *gpt_e;
Lukasz Majewski60119122017-10-27 12:28:10 +0200616 int ret, size;
Egbert Eich071e4232013-04-09 06:03:36 +0000617
Lukasz Majewski60119122017-10-27 12:28:10 +0200618 size = PAD_TO_BLOCKSIZE(sizeof(gpt_header), dev_desc);
619 gpt_h = malloc_cache_aligned(size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100620 if (gpt_h == NULL) {
621 printf("%s: calloc failed!\n", __func__);
622 return -1;
623 }
Lukasz Majewski60119122017-10-27 12:28:10 +0200624 memset(gpt_h, 0, size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100625
Lukasz Majewski60119122017-10-27 12:28:10 +0200626 size = PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS * sizeof(gpt_entry),
627 dev_desc);
628 gpt_e = malloc_cache_aligned(size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100629 if (gpt_e == NULL) {
630 printf("%s: calloc failed!\n", __func__);
631 free(gpt_h);
632 return -1;
633 }
Lukasz Majewski60119122017-10-27 12:28:10 +0200634 memset(gpt_e, 0, size);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100635
636 /* Generate Primary GPT header (LBA1) */
637 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
638 if (ret)
639 goto err;
640
641 /* Generate partition entries */
Maxime Ripard0d390872017-08-23 16:01:32 +0200642 ret = gpt_fill_pte(dev_desc, gpt_h, gpt_e, partitions, parts_count);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100643 if (ret)
644 goto err;
645
646 /* Write GPT partition table */
647 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
648
649err:
650 free(gpt_e);
651 free(gpt_h);
652 return ret;
653}
Steve Rae7d059342014-12-12 15:51:54 -0800654
Heinrich Schuchardt7cfc0332019-07-14 18:12:32 +0200655/**
656 * gpt_convert_efi_name_to_char() - convert u16 string to char string
657 *
658 * TODO: this conversion only supports ANSI characters
659 *
660 * @s: target buffer
661 * @es: u16 string to be converted
662 * @n: size of target buffer
663 */
664static void gpt_convert_efi_name_to_char(char *s, void *es, int n)
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100665{
Heinrich Schuchardt7cfc0332019-07-14 18:12:32 +0200666 char *ess = es;
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100667 int i, j;
668
669 memset(s, '\0', n);
670
671 for (i = 0, j = 0; j < n; i += 2, j++) {
672 s[j] = ess[i];
673 if (!ess[i])
674 return;
675 }
676}
677
Simon Glasse3394752016-02-29 15:25:34 -0700678int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100679 gpt_entry **gpt_pte)
680{
681 /*
682 * This function validates AND
683 * fills in the GPT header and PTE
684 */
685 if (is_gpt_valid(dev_desc,
686 GPT_PRIMARY_PARTITION_TABLE_LBA,
687 gpt_head, gpt_pte) != 1) {
688 printf("%s: *** ERROR: Invalid GPT ***\n",
689 __func__);
690 return -1;
691 }
Eugeniu Rosca27901b92019-04-30 04:53:45 +0200692
693 /* Free pte before allocating again */
694 free(*gpt_pte);
695
Stefan Herbrechtsmeier76df0732021-03-08 16:07:11 +0000696 /*
697 * Check that the alternate_lba entry points to the last LBA
698 */
699 if (le64_to_cpu(gpt_head->alternate_lba) != (dev_desc->lba - 1)) {
700 printf("%s: *** ERROR: Misplaced Backup GPT ***\n",
701 __func__);
702 return -1;
703 }
704
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100705 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
706 gpt_head, gpt_pte) != 1) {
707 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
708 __func__);
709 return -1;
710 }
711
712 return 0;
713}
714
Simon Glasse3394752016-02-29 15:25:34 -0700715int gpt_verify_partitions(struct blk_desc *dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600716 struct disk_partition *partitions, int parts,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100717 gpt_header *gpt_head, gpt_entry **gpt_pte)
718{
719 char efi_str[PARTNAME_SZ + 1];
720 u64 gpt_part_size;
721 gpt_entry *gpt_e;
722 int ret, i;
723
724 ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
725 if (ret)
726 return ret;
727
728 gpt_e = *gpt_pte;
729
730 for (i = 0; i < parts; i++) {
731 if (i == gpt_head->num_partition_entries) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900732 pr_err("More partitions than allowed!\n");
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100733 return -1;
734 }
735
736 /* Check if GPT and ENV partition names match */
737 gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
738 PARTNAME_SZ + 1);
739
740 debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
741 __func__, i, efi_str, partitions[i].name);
742
743 if (strncmp(efi_str, (char *)partitions[i].name,
744 sizeof(partitions->name))) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900745 pr_err("Partition name: %s does not match %s!\n",
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100746 efi_str, (char *)partitions[i].name);
747 return -1;
748 }
749
750 /* Check if GPT and ENV sizes match */
751 gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
752 le64_to_cpu(gpt_e[i].starting_lba) + 1;
753 debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
Simon Glass03d7b602016-02-29 15:25:36 -0700754 (unsigned long long)gpt_part_size,
755 (unsigned long long)partitions[i].size);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100756
757 if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
Kever Yangf1d070d2016-07-29 11:12:18 +0800758 /* We do not check the extend partition size */
759 if ((i == parts - 1) && (partitions[i].size == 0))
760 continue;
761
Masahiro Yamada81e10422017-09-16 14:10:41 +0900762 pr_err("Partition %s size: %llu does not match %llu!\n",
Simon Glass03d7b602016-02-29 15:25:36 -0700763 efi_str, (unsigned long long)gpt_part_size,
764 (unsigned long long)partitions[i].size);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100765 return -1;
766 }
767
768 /*
769 * Start address is optional - check only if provided
770 * in '$partition' variable
771 */
772 if (!partitions[i].start) {
773 debug("\n");
774 continue;
775 }
776
777 /* Check if GPT and ENV start LBAs match */
778 debug("start LBA - GPT: %8llu, ENV: %8llu\n",
779 le64_to_cpu(gpt_e[i].starting_lba),
Simon Glass03d7b602016-02-29 15:25:36 -0700780 (unsigned long long)partitions[i].start);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100781
782 if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900783 pr_err("Partition %s start: %llu does not match %llu!\n",
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100784 efi_str, le64_to_cpu(gpt_e[i].starting_lba),
Simon Glass03d7b602016-02-29 15:25:36 -0700785 (unsigned long long)partitions[i].start);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100786 return -1;
787 }
788 }
789
790 return 0;
791}
792
Simon Glasse3394752016-02-29 15:25:34 -0700793int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf)
Steve Rae7d059342014-12-12 15:51:54 -0800794{
795 gpt_header *gpt_h;
796 gpt_entry *gpt_e;
797
798 /* determine start of GPT Header in the buffer */
799 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
800 dev_desc->blksz);
801 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
802 dev_desc->lba))
803 return -1;
804
805 /* determine start of GPT Entries in the buffer */
806 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
807 dev_desc->blksz);
808 if (validate_gpt_entries(gpt_h, gpt_e))
809 return -1;
810
811 return 0;
812}
813
Simon Glasse3394752016-02-29 15:25:34 -0700814int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
Steve Rae7d059342014-12-12 15:51:54 -0800815{
816 gpt_header *gpt_h;
817 gpt_entry *gpt_e;
818 int gpt_e_blk_cnt;
819 lbaint_t lba;
820 int cnt;
821
822 if (is_valid_gpt_buf(dev_desc, buf))
823 return -1;
824
825 /* determine start of GPT Header in the buffer */
826 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
827 dev_desc->blksz);
828
829 /* determine start of GPT Entries in the buffer */
830 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
831 dev_desc->blksz);
832 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
833 le32_to_cpu(gpt_h->sizeof_partition_entry)),
834 dev_desc);
835
836 /* write MBR */
837 lba = 0; /* MBR is always at 0 */
838 cnt = 1; /* MBR (1 block) */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700839 if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800840 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
841 __func__, "MBR", cnt, lba);
842 return 1;
843 }
844
845 /* write Primary GPT */
846 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
847 cnt = 1; /* GPT Header (1 block) */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700848 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800849 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
850 __func__, "Primary GPT Header", cnt, lba);
851 return 1;
852 }
853
854 lba = le64_to_cpu(gpt_h->partition_entry_lba);
855 cnt = gpt_e_blk_cnt;
Simon Glass2ee8ada2016-02-29 15:25:52 -0700856 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800857 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
858 __func__, "Primary GPT Entries", cnt, lba);
859 return 1;
860 }
861
862 prepare_backup_gpt_header(gpt_h);
863
864 /* write Backup GPT */
865 lba = le64_to_cpu(gpt_h->partition_entry_lba);
866 cnt = gpt_e_blk_cnt;
Simon Glass2ee8ada2016-02-29 15:25:52 -0700867 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800868 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
869 __func__, "Backup GPT Entries", cnt, lba);
870 return 1;
871 }
872
873 lba = le64_to_cpu(gpt_h->my_lba);
874 cnt = 1; /* GPT Header (1 block) */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700875 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800876 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
877 __func__, "Backup GPT Header", cnt, lba);
878 return 1;
879 }
880
Gary Bissonb5fe1992021-01-26 14:56:23 +0100881 /* Update the partition table entries*/
882 part_init(dev_desc);
883
Steve Rae7d059342014-12-12 15:51:54 -0800884 return 0;
885}
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100886#endif
887
richardretanubune6745592008-09-26 11:13:22 -0400888/*
889 * Private functions
890 */
891/*
892 * pmbr_part_valid(): Check for EFI partition signature
893 *
894 * Returns: 1 if EFI GPT partition type is found.
895 */
896static int pmbr_part_valid(struct partition *part)
897{
898 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich20ca3ad2013-03-29 07:57:10 +0000899 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubune6745592008-09-26 11:13:22 -0400900 return 1;
901 }
902
903 return 0;
904}
905
906/*
907 * is_pmbr_valid(): test Protective MBR for validity
908 *
909 * Returns: 1 if PMBR is valid, 0 otherwise.
910 * Validity depends on two things:
911 * 1) MSDOS signature is in the last two bytes of the MBR
912 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
913 */
914static int is_pmbr_valid(legacy_mbr * mbr)
915{
916 int i = 0;
917
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100918 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubune6745592008-09-26 11:13:22 -0400919 return 0;
richardretanubune6745592008-09-26 11:13:22 -0400920
921 for (i = 0; i < 4; i++) {
922 if (pmbr_part_valid(&mbr->partition_record[i])) {
923 return 1;
924 }
925 }
926 return 0;
927}
928
929/**
930 * is_gpt_valid() - tests one GPT header and PTEs for validity
931 *
932 * lba is the logical block address of the GPT header to test
933 * gpt is a GPT header ptr, filled on return.
934 * ptes is a PTEs ptr, filled on return.
935 *
Urja Rannikko54950fa2019-04-11 20:27:51 +0000936 * Description: returns 1 if valid, 0 on error, 2 if ignored header
richardretanubune6745592008-09-26 11:13:22 -0400937 * If valid, returns pointers to PTEs.
938 */
Simon Glasse3394752016-02-29 15:25:34 -0700939static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
Steve Raed30cbae2014-05-26 11:52:23 -0700940 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
richardretanubune6745592008-09-26 11:13:22 -0400941{
Tom Rinie6131f12017-10-03 09:38:44 -0400942 /* Confirm valid arguments prior to allocation. */
richardretanubune6745592008-09-26 11:13:22 -0400943 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000944 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400945 return 0;
946 }
947
Patrick Delaunaye54b5472017-11-17 10:08:18 +0100948 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, mbr, 1, dev_desc->blksz);
Tom Rinie6131f12017-10-03 09:38:44 -0400949
Peter Jonesc27e1c12017-09-13 18:05:25 -0400950 /* Read MBR Header from device */
951 if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1) {
952 printf("*** ERROR: Can't read MBR header ***\n");
953 return 0;
954 }
955
richardretanubune6745592008-09-26 11:13:22 -0400956 /* Read GPT Header from device */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700957 if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
richardretanubune6745592008-09-26 11:13:22 -0400958 printf("*** ERROR: Can't read GPT header ***\n");
959 return 0;
960 }
961
Urja Rannikko54950fa2019-04-11 20:27:51 +0000962 /* Invalid but nothing to yell about. */
963 if (le64_to_cpu(pgpt_head->signature) == GPT_HEADER_CHROMEOS_IGNORE) {
964 debug("ChromeOS 'IGNOREME' GPT header found and ignored\n");
965 return 2;
966 }
967
Steve Rae6f5e17d2014-12-18 12:13:42 +0100968 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
richardretanubune6745592008-09-26 11:13:22 -0400969 return 0;
richardretanubune6745592008-09-26 11:13:22 -0400970
Peter Jonesc27e1c12017-09-13 18:05:25 -0400971 if (dev_desc->sig_type == SIG_TYPE_NONE) {
972 efi_guid_t empty = {};
973 if (memcmp(&pgpt_head->disk_guid, &empty, sizeof(empty))) {
974 dev_desc->sig_type = SIG_TYPE_GUID;
975 memcpy(&dev_desc->guid_sig, &pgpt_head->disk_guid,
976 sizeof(empty));
977 } else if (mbr->unique_mbr_signature != 0) {
978 dev_desc->sig_type = SIG_TYPE_MBR;
979 dev_desc->mbr_sig = mbr->unique_mbr_signature;
980 }
981 }
982
richardretanubune6745592008-09-26 11:13:22 -0400983 /* Read and allocate Partition Table Entries */
984 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
985 if (*pgpt_pte == NULL) {
986 printf("GPT: Failed to allocate memory for PTE\n");
987 return 0;
988 }
989
Steve Rae6f5e17d2014-12-18 12:13:42 +0100990 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
Doug Anderson698a51d2011-10-19 09:47:31 +0000991 free(*pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400992 return 0;
993 }
994
995 /* We're done, all's well */
996 return 1;
997}
998
999/**
Urja Rannikko80bded72019-04-11 20:27:50 +00001000 * find_valid_gpt() - finds a valid GPT header and PTEs
1001 *
1002 * gpt is a GPT header ptr, filled on return.
1003 * ptes is a PTEs ptr, filled on return.
1004 *
1005 * Description: returns 1 if found a valid gpt, 0 on error.
1006 * If valid, returns pointers to PTEs.
1007 */
1008static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head,
1009 gpt_entry **pgpt_pte)
1010{
Urja Rannikko54950fa2019-04-11 20:27:51 +00001011 int r;
1012
1013 r = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_head,
1014 pgpt_pte);
1015
1016 if (r != 1) {
1017 if (r != 2)
1018 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
1019
1020 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), gpt_head,
1021 pgpt_pte) != 1) {
Urja Rannikko80bded72019-04-11 20:27:50 +00001022 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
1023 __func__);
1024 return 0;
1025 }
Urja Rannikko54950fa2019-04-11 20:27:51 +00001026 if (r != 2)
1027 printf("%s: *** Using Backup GPT ***\n",
1028 __func__);
Urja Rannikko80bded72019-04-11 20:27:50 +00001029 }
1030 return 1;
1031}
1032
1033/**
richardretanubune6745592008-09-26 11:13:22 -04001034 * alloc_read_gpt_entries(): reads partition entries from disk
1035 * @dev_desc
1036 * @gpt - GPT header
1037 *
1038 * Description: Returns ptes on success, NULL on error.
1039 * Allocates space for PTEs based on information found in @gpt.
1040 * Notes: remember to free pte when you're done!
1041 */
Simon Glasse3394752016-02-29 15:25:34 -07001042static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
1043 gpt_header *pgpt_head)
richardretanubune6745592008-09-26 11:13:22 -04001044{
Egbert Eich071e4232013-04-09 06:03:36 +00001045 size_t count = 0, blk_cnt;
Stephen Warrene73f2962015-12-07 11:38:48 -07001046 lbaint_t blk;
richardretanubune6745592008-09-26 11:13:22 -04001047 gpt_entry *pte = NULL;
1048
1049 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +00001050 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -04001051 return NULL;
1052 }
1053
Chang Hyun Park823ffde2012-12-11 11:09:45 +01001054 count = le32_to_cpu(pgpt_head->num_partition_entries) *
1055 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubune6745592008-09-26 11:13:22 -04001056
Simon Glassbb10e9b2016-07-22 09:22:47 -06001057 debug("%s: count = %u * %u = %lu\n", __func__,
Chang Hyun Park823ffde2012-12-11 11:09:45 +01001058 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
Simon Glassbb10e9b2016-07-22 09:22:47 -06001059 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry),
1060 (ulong)count);
richardretanubune6745592008-09-26 11:13:22 -04001061
1062 /* Allocate memory for PTE, remember to FREE */
1063 if (count != 0) {
Egbert Eich071e4232013-04-09 06:03:36 +00001064 pte = memalign(ARCH_DMA_MINALIGN,
1065 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubune6745592008-09-26 11:13:22 -04001066 }
1067
1068 if (count == 0 || pte == NULL) {
Simon Glassbb10e9b2016-07-22 09:22:47 -06001069 printf("%s: ERROR: Can't allocate %#lX bytes for GPT Entries\n",
1070 __func__, (ulong)count);
richardretanubune6745592008-09-26 11:13:22 -04001071 return NULL;
1072 }
1073
1074 /* Read GPT Entries from device */
Stephen Warrene73f2962015-12-07 11:38:48 -07001075 blk = le64_to_cpu(pgpt_head->partition_entry_lba);
Egbert Eich071e4232013-04-09 06:03:36 +00001076 blk_cnt = BLOCK_CNT(count, dev_desc);
Simon Glass2ee8ada2016-02-29 15:25:52 -07001077 if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
richardretanubune6745592008-09-26 11:13:22 -04001078 printf("*** ERROR: Can't read GPT Entries ***\n");
1079 free(pte);
1080 return NULL;
1081 }
1082 return pte;
1083}
1084
1085/**
1086 * is_pte_valid(): validates a single Partition Table Entry
1087 * @gpt_entry - Pointer to a single Partition Table Entry
1088 *
1089 * Description: returns 1 if valid, 0 on error.
1090 */
1091static int is_pte_valid(gpt_entry * pte)
1092{
1093 efi_guid_t unused_guid;
1094
1095 if (!pte) {
Doug Andersondc036672011-10-19 08:04:46 +00001096 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -04001097 return 0;
1098 }
1099
1100 /* Only one validation for now:
1101 * The GUID Partition Type != Unused Entry (ALL-ZERO)
1102 */
1103 memset(unused_guid.b, 0, sizeof(unused_guid.b));
1104
1105 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
1106 sizeof(unused_guid.b)) == 0) {
1107
Doug Andersondc036672011-10-19 08:04:46 +00001108 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt864074d2012-10-12 14:26:09 +00001109 (unsigned int)(uintptr_t)pte);
richardretanubune6745592008-09-26 11:13:22 -04001110
1111 return 0;
1112 } else {
1113 return 1;
1114 }
1115}
Simon Glass83ce5632016-02-29 15:25:47 -07001116
1117/*
1118 * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to
1119 * check EFI first, since a DOS partition is often used as a 'protective MBR'
1120 * with EFI.
1121 */
1122U_BOOT_PART_TYPE(a_efi) = {
1123 .name = "EFI",
1124 .part_type = PART_TYPE_EFI,
Petr Kulhavy712257e2016-09-09 10:27:15 +02001125 .max_entries = GPT_ENTRY_NUMBERS,
Simon Glassb89a8442016-02-29 15:25:48 -07001126 .get_info = part_get_info_ptr(part_get_info_efi),
Simon Glass0f1c9522016-02-29 15:26:04 -07001127 .print = part_print_ptr(part_print_efi),
1128 .test = part_test_efi,
Simon Glass83ce5632016-02-29 15:25:47 -07001129};
Simon Glass56d59a62021-07-02 12:36:14 -06001130#endif /* CONFIG_HAVE_BLOCK_DEVICE */