blob: deb0d19123bed88fbc08f0c96d5424b16c001fdd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
richardretanubun07f3d782008-09-26 11:13:22 -04002/*
3 * Copyright (C) 2008 RuggedCom, Inc.
4 * Richard Retanubun <RichardRetanubun@RuggedCom.com>
richardretanubun07f3d782008-09-26 11:13:22 -04005 */
6
7/*
Steve Raee04350d2014-05-26 11:52:23 -07008 * NOTE:
9 * when CONFIG_SYS_64BIT_LBA is not defined, lbaint_t is 32 bits; this
Heinrich Schuchardt643b5e72020-09-17 17:57:21 +020010 * limits the maximum size of addressable storage to < 2 tebibytes
richardretanubun07f3d782008-09-26 11:13:22 -040011 */
Simon Glasse6f6f9e2020-05-10 11:39:58 -060012#include <common.h>
13#include <blk.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060015#include <part.h>
Simon Glassba06b3c2020-05-10 11:39:52 -060016#include <uuid.h>
Simon Glass90526e92020-05-10 11:39:56 -060017#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
Marc Dietrich8faefad2013-03-29 07:57:10 +000019#include <asm/unaligned.h>
richardretanubun07f3d782008-09-26 11:13:22 -040020#include <command.h>
Philipp Tomsich02e43532017-03-01 21:10:39 +010021#include <fdtdec.h>
richardretanubun07f3d782008-09-26 11:13:22 -040022#include <ide.h>
23#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060024#include <memalign.h>
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010025#include <part_efi.h>
Simon Glass7de8bd02021-08-07 07:24:01 -060026#include <dm/ofnode.h>
Philipp Tomsich02e43532017-03-01 21:10:39 +010027#include <linux/compiler.h>
Lei Wen6eecc032011-09-07 18:11:19 +000028#include <linux/ctype.h>
Simon Glass3db71102019-11-14 12:57:16 -070029#include <u-boot/crc.h>
Bo Lv72d0e902023-01-02 14:27:34 +000030#ifdef CONFIG_AMLOGIC_MODIFY
31#include <amlogic/emmc_partitions.h>
32#endif
richardretanubun07f3d782008-09-26 11:13:22 -040033
Simon Glassa594b412021-07-02 12:36:14 -060034/* GUID for basic data partitons */
35#if CONFIG_IS_ENABLED(EFI_PARTITION)
36static const efi_guid_t partition_basic_data_guid = PARTITION_BASIC_DATA_GUID;
37#endif
38
richardretanubun07f3d782008-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 Parkfae2bf22012-12-11 11:09:45 +010046static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubun07f3d782008-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);
Bo Lv72d0e902023-01-02 14:27:34 +000057#ifdef CONFIG_AMLOGIC_MODIFY
58int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
59 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
60#else
Simon Glass4101f682016-02-29 15:25:34 -070061static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
Steve Raee04350d2014-05-26 11:52:23 -070062 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
Bo Lv72d0e902023-01-02 14:27:34 +000063#endif
Simon Glass4101f682016-02-29 15:25:34 -070064static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
65 gpt_header *pgpt_head);
Bo Lv72d0e902023-01-02 14:27:34 +000066#ifdef CONFIG_AMLOGIC_MODIFY
67int is_pte_valid(gpt_entry * pte);
68#else
richardretanubun07f3d782008-09-26 11:13:22 -040069static int is_pte_valid(gpt_entry * pte);
Bo Lv72d0e902023-01-02 14:27:34 +000070#endif
Urja Rannikko20c568c2019-04-11 20:27:50 +000071static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head,
72 gpt_entry **pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -040073
Lei Wen6eecc032011-09-07 18:11:19 +000074static char *print_efiname(gpt_entry *pte)
75{
76 static char name[PARTNAME_SZ + 1];
77 int i;
78 for (i = 0; i < PARTNAME_SZ; i++) {
79 u8 c;
80 c = pte->partition_name[i] & 0xff;
81 c = (c && !isprint(c)) ? '.' : c;
82 name[i] = c;
83 }
84 name[PARTNAME_SZ] = 0;
85 return name;
86}
87
Heinrich Schuchardt7de24b22019-01-12 11:25:25 +010088static const efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
Stephen Warrenb4414f42012-10-08 08:14:37 +000089
Heinrich Schuchardt25801ac2020-03-19 13:49:34 +010090static int get_bootable(gpt_entry *p)
Stephen Warrenb4414f42012-10-08 08:14:37 +000091{
Heinrich Schuchardt25801ac2020-03-19 13:49:34 +010092 int ret = 0;
93
94 if (!memcmp(&p->partition_type_guid, &system_guid, sizeof(efi_guid_t)))
95 ret |= PART_EFI_SYSTEM_PARTITION;
96 if (p->attributes.fields.legacy_bios_bootable)
97 ret |= PART_BOOTABLE;
98 return ret;
Stephen Warrenb4414f42012-10-08 08:14:37 +000099}
100
Steve Raee1f6b0a2014-12-18 12:13:42 +0100101static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba,
102 lbaint_t lastlba)
103{
104 uint32_t crc32_backup = 0;
105 uint32_t calc_crc32;
106
107 /* Check the GPT header signature */
Simon Glass67b90522018-10-01 12:22:35 -0600108 if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE_UBOOT) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600109 log_debug("%s signature is wrong: %#llX != %#llX\n",
110 "GUID Partition Table Header",
111 le64_to_cpu(gpt_h->signature),
112 GPT_HEADER_SIGNATURE_UBOOT);
Steve Raee1f6b0a2014-12-18 12:13:42 +0100113 return -1;
114 }
115
116 /* Check the GUID Partition Table CRC */
117 memcpy(&crc32_backup, &gpt_h->header_crc32, sizeof(crc32_backup));
118 memset(&gpt_h->header_crc32, 0, sizeof(gpt_h->header_crc32));
119
120 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
121 le32_to_cpu(gpt_h->header_size));
122
123 memcpy(&gpt_h->header_crc32, &crc32_backup, sizeof(crc32_backup));
124
125 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600126 log_debug("%s: CRC is wrong: %#x != %#x\n",
127 "GUID Partition Table Header",
128 le32_to_cpu(crc32_backup), calc_crc32);
Steve Raee1f6b0a2014-12-18 12:13:42 +0100129 return -1;
130 }
131
132 /*
133 * Check that the my_lba entry points to the LBA that contains the GPT
134 */
135 if (le64_to_cpu(gpt_h->my_lba) != lba) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600136 log_debug("GPT: my_lba incorrect: %llX != " LBAF "\n",
137 le64_to_cpu(gpt_h->my_lba), lba);
Steve Raee1f6b0a2014-12-18 12:13:42 +0100138 return -1;
139 }
140
141 /*
142 * Check that the first_usable_lba and that the last_usable_lba are
143 * within the disk.
144 */
145 if (le64_to_cpu(gpt_h->first_usable_lba) > lastlba) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600146 log_debug("GPT: first_usable_lba incorrect: %llX > " LBAF "\n",
147 le64_to_cpu(gpt_h->first_usable_lba), lastlba);
Steve Raee1f6b0a2014-12-18 12:13:42 +0100148 return -1;
149 }
Xindong Xud9441422024-01-18 10:20:45 +0800150
151#ifndef CONFIG_AMLOGIC_MODIFY
Steve Raee1f6b0a2014-12-18 12:13:42 +0100152 if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600153 log_debug("GPT: last_usable_lba incorrect: %llX > " LBAF "\n",
154 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
Steve Raee1f6b0a2014-12-18 12:13:42 +0100155 return -1;
156 }
Xindong Xud9441422024-01-18 10:20:45 +0800157#endif
Steve Raee1f6b0a2014-12-18 12:13:42 +0100158
159 debug("GPT: first_usable_lba: %llX last_usable_lba: %llX last lba: "
160 LBAF "\n", le64_to_cpu(gpt_h->first_usable_lba),
161 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
162
163 return 0;
164}
165
166static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
167{
168 uint32_t calc_crc32;
169
170 /* Check the GUID Partition Table Entry Array CRC */
171 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
172 le32_to_cpu(gpt_h->num_partition_entries) *
173 le32_to_cpu(gpt_h->sizeof_partition_entry));
174
175 if (calc_crc32 != le32_to_cpu(gpt_h->partition_entry_array_crc32)) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600176 log_debug("%s: %#x != %#x\n",
177 "GUID Partition Table Entry Array CRC is wrong",
178 le32_to_cpu(gpt_h->partition_entry_array_crc32),
179 calc_crc32);
Steve Raee1f6b0a2014-12-18 12:13:42 +0100180 return -1;
181 }
182
183 return 0;
184}
185
186static void prepare_backup_gpt_header(gpt_header *gpt_h)
187{
188 uint32_t calc_crc32;
189 uint64_t val;
190
191 /* recalculate the values for the Backup GPT Header */
192 val = le64_to_cpu(gpt_h->my_lba);
193 gpt_h->my_lba = gpt_h->alternate_lba;
194 gpt_h->alternate_lba = cpu_to_le64(val);
Steve Rae0ff7e582014-12-12 15:51:54 -0800195 gpt_h->partition_entry_lba =
196 cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
Steve Raee1f6b0a2014-12-18 12:13:42 +0100197 gpt_h->header_crc32 = 0;
198
199 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
200 le32_to_cpu(gpt_h->header_size));
201 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
202}
203
Patrick Delaunaybd42a942017-01-27 11:00:41 +0100204#if CONFIG_IS_ENABLED(EFI_PARTITION)
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000205/*
206 * Public Functions (include/part.h)
207 */
208
Alison Chaiken73d6d182017-06-25 16:43:23 -0700209/*
210 * UUID is displayed as 32 hexadecimal digits, in 5 groups,
211 * separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters
212 */
213int get_disk_guid(struct blk_desc * dev_desc, char *guid)
214{
215 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
216 gpt_entry *gpt_pte = NULL;
217 unsigned char *guid_bin;
218
219 /* This function validates AND fills in the GPT header and PTE */
Urja Rannikko20c568c2019-04-11 20:27:50 +0000220 if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1)
221 return -EINVAL;
Alison Chaiken73d6d182017-06-25 16:43:23 -0700222
223 guid_bin = gpt_head->disk_guid.b;
224 uuid_bin_to_str(guid_bin, guid, UUID_STR_FORMAT_GUID);
225
Eugeniu Rosca1cfe9692019-04-30 04:53:44 +0200226 /* Remember to free pte */
227 free(gpt_pte);
Alison Chaiken73d6d182017-06-25 16:43:23 -0700228 return 0;
229}
230
Simon Glass084bf4c2016-02-29 15:26:04 -0700231void part_print_efi(struct blk_desc *dev_desc)
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000232{
Egbert Eichae1768a2013-04-09 06:03:36 +0000233 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000234 gpt_entry *gpt_pte = NULL;
235 int i = 0;
Heinrich Schuchardta4492ee2022-01-16 12:23:19 +0100236 unsigned char *uuid;
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000237
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000238 /* This function validates AND fills in the GPT header and PTE */
Urja Rannikko20c568c2019-04-11 20:27:50 +0000239 if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1)
240 return;
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000241
242 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
243
244 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren13bf2f52012-10-08 08:14:36 +0000245 printf("\tAttributes\n");
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200246 printf("\tType GUID\n");
247 printf("\tPartition GUID\n");
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000248
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100249 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Heinrich Schuchardt504dbd22022-01-11 15:43:05 +0100250 /* Skip invalid PTE */
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000251 if (!is_pte_valid(&gpt_pte[i]))
Heinrich Schuchardt504dbd22022-01-11 15:43:05 +0100252 continue;
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000253
254 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100255 le64_to_cpu(gpt_pte[i].starting_lba),
256 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000257 print_efiname(&gpt_pte[i]));
Stephen Warren13bf2f52012-10-08 08:14:36 +0000258 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Heinrich Schuchardta4492ee2022-01-16 12:23:19 +0100259 uuid = (unsigned char *)gpt_pte[i].partition_type_guid.b;
260 if (CONFIG_IS_ENABLED(PARTITION_TYPE_GUID))
261 printf("\ttype:\t%pUl\n\t\t(%pUs)\n", uuid, uuid);
262 else
263 printf("\ttype:\t%pUl\n", uuid);
264 uuid = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
265 printf("\tguid:\t%pUl\n", uuid);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000266 }
267
268 /* Remember to free pte */
269 free(gpt_pte);
270 return;
271}
Stephen Warren894bfbb2012-09-21 09:50:59 +0000272
Simon Glass3e8bd462016-02-29 15:25:48 -0700273int part_get_info_efi(struct blk_desc *dev_desc, int part,
Simon Glass05289792020-05-10 11:39:57 -0600274 struct disk_partition *info)
richardretanubun07f3d782008-09-26 11:13:22 -0400275{
Egbert Eichae1768a2013-04-09 06:03:36 +0000276 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Andersondeb5ca82011-10-19 09:47:31 +0000277 gpt_entry *gpt_pte = NULL;
richardretanubun07f3d782008-09-26 11:13:22 -0400278
279 /* "part" argument must be at least 1 */
Simon Glass4708a072016-03-16 07:45:36 -0600280 if (part < 1) {
Simon Glassf337fb92022-10-11 09:47:11 -0600281 log_debug("Invalid Argument(s)\n");
282 return -EINVAL;
richardretanubun07f3d782008-09-26 11:13:22 -0400283 }
284
285 /* This function validates AND fills in the GPT header and PTE */
Urja Rannikko20c568c2019-04-11 20:27:50 +0000286 if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1)
Simon Glassf337fb92022-10-11 09:47:11 -0600287 return -EINVAL;
richardretanubun07f3d782008-09-26 11:13:22 -0400288
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100289 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000290 !is_pte_valid(&gpt_pte[part - 1])) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600291 log_debug("Invalid partition number %d\n", part);
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500292 free(gpt_pte);
Simon Glassf337fb92022-10-11 09:47:11 -0600293 return -EPERM;
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000294 }
295
Steve Raee04350d2014-05-26 11:52:23 -0700296 /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
297 info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
Richard Retanubun50970832009-01-26 08:45:14 -0500298 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Steve Raee04350d2014-05-26 11:52:23 -0700299 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
Richard Retanubun50970832009-01-26 08:45:14 -0500300 - info->start;
Egbert Eichae1768a2013-04-09 06:03:36 +0000301 info->blksz = dev_desc->blksz;
richardretanubun07f3d782008-09-26 11:13:22 -0400302
Heinrich Schuchardt5375ee52019-07-05 21:27:13 +0200303 snprintf((char *)info->name, sizeof(info->name), "%s",
304 print_efiname(&gpt_pte[part - 1]));
Ben Whitten192bc692015-12-30 13:05:58 +0000305 strcpy((char *)info->type, "U-Boot");
Heinrich Schuchardt25801ac2020-03-19 13:49:34 +0100306 info->bootable = get_bootable(&gpt_pte[part - 1]);
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100307#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200308 uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
309 UUID_STR_FORMAT_GUID);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000310#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100311#ifdef CONFIG_PARTITION_TYPE_GUID
312 uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
313 info->type_guid, UUID_STR_FORMAT_GUID);
314#endif
richardretanubun07f3d782008-09-26 11:13:22 -0400315
Simon Glassf337fb92022-10-11 09:47:11 -0600316 log_debug("start 0x" LBAF ", size 0x" LBAF ", name %s\n", info->start,
317 info->size, info->name);
richardretanubun07f3d782008-09-26 11:13:22 -0400318
319 /* Remember to free pte */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000320 free(gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400321 return 0;
322}
323
Simon Glass084bf4c2016-02-29 15:26:04 -0700324static int part_test_efi(struct blk_desc *dev_desc)
richardretanubun07f3d782008-09-26 11:13:22 -0400325{
Egbert Eichae1768a2013-04-09 06:03:36 +0000326 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubun07f3d782008-09-26 11:13:22 -0400327
328 /* Read legacy MBR from block 0 and validate it */
Simon Glass2a981dc2016-02-29 15:25:52 -0700329 if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1)
Anton staaff75dd582011-10-12 13:56:04 +0000330 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400331 return -1;
332 }
333 return 0;
334}
335
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100336/**
337 * set_protective_mbr(): Set the EFI protective MBR
338 * @param dev_desc - block device descriptor
339 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100340 * Return: - zero on success, otherwise error
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100341 */
Simon Glass4101f682016-02-29 15:25:34 -0700342static int set_protective_mbr(struct blk_desc *dev_desc)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100343{
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100344 /* Setup the Protective MBR */
Patrick Delaunay3cc56612017-11-17 10:08:18 +0100345 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, p_mbr, 1, dev_desc->blksz);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100346 if (p_mbr == NULL) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600347 log_debug("calloc failed!\n");
348 return -ENOMEM;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100349 }
Vincent Tinellie163a932017-01-30 15:46:07 +0300350
351 /* Read MBR to backup boot code if it exists */
352 if (blk_dread(dev_desc, 0, 1, p_mbr) != 1) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600353 log_debug("** Can't read from device %d **\n",
354 dev_desc->devnum);
355 return -EIO;
Vincent Tinellie163a932017-01-30 15:46:07 +0300356 }
357
Sam Protsenko955575c2018-05-22 02:04:21 +0300358 /* Clear all data in MBR except of backed up boot code */
359 memset((char *)p_mbr + MSDOS_MBR_BOOT_CODE_SIZE, 0, sizeof(*p_mbr) -
360 MSDOS_MBR_BOOT_CODE_SIZE);
361
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100362 /* Append signature */
363 p_mbr->signature = MSDOS_MBR_SIGNATURE;
364 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
365 p_mbr->partition_record[0].start_sect = 1;
Maxime Ripardb349abb2015-01-08 12:26:44 +0100366 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100367
368 /* Write MBR sector to the MMC device */
Simon Glass2a981dc2016-02-29 15:25:52 -0700369 if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600370 log_debug("** Can't write to device %d **\n", dev_desc->devnum);
371 return -EIO;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100372 }
373
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100374 return 0;
375}
376
Simon Glass4101f682016-02-29 15:25:34 -0700377int write_gpt_table(struct blk_desc *dev_desc,
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100378 gpt_header *gpt_h, gpt_entry *gpt_e)
379{
Egbert Eichae1768a2013-04-09 06:03:36 +0000380 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
381 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100382 u32 calc_crc32;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100383
384 debug("max lba: %x\n", (u32) dev_desc->lba);
385 /* Setup the Protective MBR */
386 if (set_protective_mbr(dev_desc) < 0)
387 goto err;
388
389 /* Generate CRC for the Primary GPT Header */
390 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
391 le32_to_cpu(gpt_h->num_partition_entries) *
392 le32_to_cpu(gpt_h->sizeof_partition_entry));
393 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
394
395 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
396 le32_to_cpu(gpt_h->header_size));
397 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
398
399 /* Write the First GPT to the block right after the Legacy MBR */
Simon Glass2a981dc2016-02-29 15:25:52 -0700400 if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100401 goto err;
402
Philipp Tomsich02e43532017-03-01 21:10:39 +0100403 if (blk_dwrite(dev_desc, le64_to_cpu(gpt_h->partition_entry_lba),
404 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100405 goto err;
406
Steve Raee1f6b0a2014-12-18 12:13:42 +0100407 prepare_backup_gpt_header(gpt_h);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100408
Simon Glass2a981dc2016-02-29 15:25:52 -0700409 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
410 + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100411 goto err;
412
Simon Glass2a981dc2016-02-29 15:25:52 -0700413 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
414 gpt_h) != 1)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100415 goto err;
416
417 debug("GPT successfully written to block device!\n");
418 return 0;
419
420 err:
Simon Glass3e62adc2022-10-20 18:22:40 -0600421 log_debug("** Can't write to device %d **\n", dev_desc->devnum);
422 return -EIO;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100423}
424
Maxime Ripard47d7ee42017-08-23 16:01:32 +0200425int gpt_fill_pte(struct blk_desc *dev_desc,
426 gpt_header *gpt_h, gpt_entry *gpt_e,
Simon Glass05289792020-05-10 11:39:57 -0600427 struct disk_partition *partitions, int parts)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100428{
Steve Raee04350d2014-05-26 11:52:23 -0700429 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
Steve Raee04350d2014-05-26 11:52:23 -0700430 lbaint_t last_usable_lba = (lbaint_t)
431 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100432 int i, k;
Marek Vasut67cd4a62013-05-19 12:53:34 +0000433 size_t efiname_len, dosname_len;
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100434#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100435 char *str_uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200436 unsigned char *bin_uuid;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100437#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100438#ifdef CONFIG_PARTITION_TYPE_GUID
439 char *str_type_guid;
440 unsigned char *bin_type_guid;
441#endif
Maxime Ripard79c59122017-08-23 16:01:33 +0200442 size_t hdr_start = gpt_h->my_lba;
443 size_t hdr_end = hdr_start + 1;
444
445 size_t pte_start = gpt_h->partition_entry_lba;
446 size_t pte_end = pte_start +
447 gpt_h->num_partition_entries * gpt_h->sizeof_partition_entry /
448 dev_desc->blksz;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100449
450 for (i = 0; i < parts; i++) {
451 /* partition starting lba */
Maxime Ripard5276e8b2017-08-23 16:01:31 +0200452 lbaint_t start = partitions[i].start;
453 lbaint_t size = partitions[i].size;
454
Maxime Ripard79c59122017-08-23 16:01:33 +0200455 if (start) {
456 offset = start + size;
457 } else {
458 start = offset;
459 offset += size;
460 }
461
462 /*
463 * If our partition overlaps with either the GPT
464 * header, or the partition entry, reject it.
465 */
Patrick Delaunayae0e0222017-10-18 15:11:05 +0200466 if (((start < hdr_end && hdr_start < (start + size)) ||
467 (start < pte_end && pte_start < (start + size)))) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600468 log_debug("Partition overlap\n");
469 return -ENOSPC;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100470 }
Maxime Ripard5276e8b2017-08-23 16:01:31 +0200471
Maxime Ripard79c59122017-08-23 16:01:33 +0200472 gpt_e[i].starting_lba = cpu_to_le64(start);
473
Patrick Delaunaya5653862016-05-02 14:43:33 +0200474 if (offset > (last_usable_lba + 1)) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600475 log_debug("Partitions layout exceeds disk size\n");
476 return -E2BIG;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100477 }
478 /* partition ending lba */
Maxime Ripard5276e8b2017-08-23 16:01:31 +0200479 if ((i == parts - 1) && (size == 0))
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100480 /* extend the last partition to maximuim */
481 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
482 else
483 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
484
Patrick Delaunay7561b252015-10-27 11:00:27 +0100485#ifdef CONFIG_PARTITION_TYPE_GUID
486 str_type_guid = partitions[i].type_guid;
487 bin_type_guid = gpt_e[i].partition_type_guid.b;
488 if (strlen(str_type_guid)) {
489 if (uuid_str_to_bin(str_type_guid, bin_type_guid,
490 UUID_STR_FORMAT_GUID)) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600491 log_debug("Partition no. %d: invalid type guid: %s\n",
492 i, str_type_guid);
493 return -EINVAL;
Patrick Delaunay7561b252015-10-27 11:00:27 +0100494 }
495 } else {
496 /* default partition type GUID */
497 memcpy(bin_type_guid,
Heinrich Schuchardt44ab2d32018-06-09 17:50:18 +0200498 &partition_basic_data_guid, 16);
Patrick Delaunay7561b252015-10-27 11:00:27 +0100499 }
500#else
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100501 /* partition type GUID */
502 memcpy(gpt_e[i].partition_type_guid.b,
Heinrich Schuchardt44ab2d32018-06-09 17:50:18 +0200503 &partition_basic_data_guid, 16);
Patrick Delaunay7561b252015-10-27 11:00:27 +0100504#endif
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100505
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100506#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100507 str_uuid = partitions[i].uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200508 bin_uuid = gpt_e[i].unique_partition_guid.b;
509
Vincent Tinelli9da52f82017-02-27 16:11:15 +0200510 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_GUID)) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600511 log_debug("Partition no. %d: invalid guid: %s\n",
512 i, str_uuid);
513 return -EINVAL;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100514 }
515#endif
516
517 /* partition attributes */
518 memset(&gpt_e[i].attributes, 0,
519 sizeof(gpt_entry_attributes));
520
Heinrich Schuchardt25801ac2020-03-19 13:49:34 +0100521 if (partitions[i].bootable & PART_BOOTABLE)
Patrick Delaunaycfdaf4c2015-11-17 11:36:52 +0100522 gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
523
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100524 /* partition name */
Marek Vasut67cd4a62013-05-19 12:53:34 +0000525 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100526 / sizeof(efi_char16_t);
Marek Vasut67cd4a62013-05-19 12:53:34 +0000527 dosname_len = sizeof(partitions[i].name);
528
529 memset(gpt_e[i].partition_name, 0,
530 sizeof(gpt_e[i].partition_name));
531
532 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100533 gpt_e[i].partition_name[k] =
534 (efi_char16_t)(partitions[i].name[k]);
535
Steve Raee04350d2014-05-26 11:52:23 -0700536 debug("%s: name: %s offset[%d]: 0x" LBAF
537 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100538 __func__, partitions[i].name, i,
Maxime Ripard5276e8b2017-08-23 16:01:31 +0200539 offset, i, size);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100540 }
541
542 return 0;
543}
544
Philipp Tomsich02e43532017-03-01 21:10:39 +0100545static uint32_t partition_entries_offset(struct blk_desc *dev_desc)
546{
547 uint32_t offset_blks = 2;
Maxime Ripard89d33a22017-08-23 16:01:30 +0200548 uint32_t __maybe_unused offset_bytes;
Philipp Tomsich02e43532017-03-01 21:10:39 +0100549 int __maybe_unused config_offset;
550
551#if defined(CONFIG_EFI_PARTITION_ENTRIES_OFF)
552 /*
553 * Some architectures require their SPL loader at a fixed
554 * address within the first 16KB of the disk. To avoid an
555 * overlap with the partition entries of the EFI partition
556 * table, the first safe offset (in bytes, from the start of
557 * the disk) for the entries can be set in
558 * CONFIG_EFI_PARTITION_ENTRIES_OFF.
559 */
Maxime Ripard89d33a22017-08-23 16:01:30 +0200560 offset_bytes =
Philipp Tomsich02e43532017-03-01 21:10:39 +0100561 PAD_TO_BLOCKSIZE(CONFIG_EFI_PARTITION_ENTRIES_OFF, dev_desc);
Maxime Ripard89d33a22017-08-23 16:01:30 +0200562 offset_blks = offset_bytes / dev_desc->blksz;
Philipp Tomsich02e43532017-03-01 21:10:39 +0100563#endif
564
565#if defined(CONFIG_OF_CONTROL)
566 /*
567 * Allow the offset of the first partition entires (in bytes
568 * from the start of the device) to be specified as a property
569 * of the device tree '/config' node.
570 */
Simon Glass7de8bd02021-08-07 07:24:01 -0600571 config_offset = ofnode_conf_read_int(
572 "u-boot,efi-partition-entries-offset", -EINVAL);
Maxime Ripard89d33a22017-08-23 16:01:30 +0200573 if (config_offset != -EINVAL) {
574 offset_bytes = PAD_TO_BLOCKSIZE(config_offset, dev_desc);
575 offset_blks = offset_bytes / dev_desc->blksz;
576 }
Philipp Tomsich02e43532017-03-01 21:10:39 +0100577#endif
578
579 debug("efi: partition entries offset (in blocks): %d\n", offset_blks);
580
581 /*
582 * The earliest LBA this can be at is LBA#2 (i.e. right behind
583 * the (protective) MBR and the GPT header.
584 */
585 if (offset_blks < 2)
586 offset_blks = 2;
587
588 return offset_blks;
589}
590
Simon Glass4101f682016-02-29 15:25:34 -0700591int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100592 char *str_guid, int parts_count)
593{
Simon Glass67b90522018-10-01 12:22:35 -0600594 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE_UBOOT);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100595 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
596 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
597 gpt_h->my_lba = cpu_to_le64(1);
598 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100599 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
Philipp Tomsich02e43532017-03-01 21:10:39 +0100600 gpt_h->partition_entry_lba =
601 cpu_to_le64(partition_entries_offset(dev_desc));
602 gpt_h->first_usable_lba =
603 cpu_to_le64(le64_to_cpu(gpt_h->partition_entry_lba) + 32);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100604 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
605 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
606 gpt_h->header_crc32 = 0;
607 gpt_h->partition_entry_array_crc32 = 0;
608
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200609 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100610 return -1;
611
612 return 0;
613}
614
Simon Glass4101f682016-02-29 15:25:34 -0700615int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
Simon Glass05289792020-05-10 11:39:57 -0600616 struct disk_partition *partitions, int parts_count)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100617{
Lukasz Majewskibb021012017-10-27 12:28:10 +0200618 gpt_header *gpt_h;
Egbert Eichae1768a2013-04-09 06:03:36 +0000619 gpt_entry *gpt_e;
Lukasz Majewskibb021012017-10-27 12:28:10 +0200620 int ret, size;
Egbert Eichae1768a2013-04-09 06:03:36 +0000621
Lukasz Majewskibb021012017-10-27 12:28:10 +0200622 size = PAD_TO_BLOCKSIZE(sizeof(gpt_header), dev_desc);
623 gpt_h = malloc_cache_aligned(size);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100624 if (gpt_h == NULL) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600625 log_debug("calloc failed!\n");
626 return -ENOMEM;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100627 }
Lukasz Majewskibb021012017-10-27 12:28:10 +0200628 memset(gpt_h, 0, size);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100629
Lukasz Majewskibb021012017-10-27 12:28:10 +0200630 size = PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS * sizeof(gpt_entry),
631 dev_desc);
632 gpt_e = malloc_cache_aligned(size);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100633 if (gpt_e == NULL) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600634 log_debug("calloc failed!\n");
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100635 free(gpt_h);
Simon Glass3e62adc2022-10-20 18:22:40 -0600636 return -ENOMEM;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100637 }
Lukasz Majewskibb021012017-10-27 12:28:10 +0200638 memset(gpt_e, 0, size);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100639
640 /* Generate Primary GPT header (LBA1) */
641 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
642 if (ret)
643 goto err;
644
645 /* Generate partition entries */
Maxime Ripard47d7ee42017-08-23 16:01:32 +0200646 ret = gpt_fill_pte(dev_desc, gpt_h, gpt_e, partitions, parts_count);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100647 if (ret)
648 goto err;
649
650 /* Write GPT partition table */
651 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
652
653err:
654 free(gpt_e);
655 free(gpt_h);
656 return ret;
657}
Steve Rae0ff7e582014-12-12 15:51:54 -0800658
Heinrich Schuchardt06e921b2019-07-14 18:12:32 +0200659/**
660 * gpt_convert_efi_name_to_char() - convert u16 string to char string
661 *
662 * TODO: this conversion only supports ANSI characters
663 *
664 * @s: target buffer
665 * @es: u16 string to be converted
666 * @n: size of target buffer
667 */
668static void gpt_convert_efi_name_to_char(char *s, void *es, int n)
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100669{
Heinrich Schuchardt06e921b2019-07-14 18:12:32 +0200670 char *ess = es;
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100671 int i, j;
672
673 memset(s, '\0', n);
674
675 for (i = 0, j = 0; j < n; i += 2, j++) {
676 s[j] = ess[i];
677 if (!ess[i])
678 return;
679 }
680}
681
Simon Glass4101f682016-02-29 15:25:34 -0700682int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100683 gpt_entry **gpt_pte)
684{
685 /*
686 * This function validates AND
687 * fills in the GPT header and PTE
688 */
689 if (is_gpt_valid(dev_desc,
690 GPT_PRIMARY_PARTITION_TABLE_LBA,
691 gpt_head, gpt_pte) != 1) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600692 log_debug("Invalid GPT\n");
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100693 return -1;
694 }
Eugeniu Rosca716f9192019-04-30 04:53:45 +0200695
696 /* Free pte before allocating again */
697 free(*gpt_pte);
698
Stefan Herbrechtsmeierd4693382021-03-08 16:07:11 +0000699 /*
700 * Check that the alternate_lba entry points to the last LBA
701 */
702 if (le64_to_cpu(gpt_head->alternate_lba) != (dev_desc->lba - 1)) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600703 log_debug("Misplaced Backup GPT\n");
Stefan Herbrechtsmeierd4693382021-03-08 16:07:11 +0000704 return -1;
705 }
706
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100707 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
708 gpt_head, gpt_pte) != 1) {
Simon Glass3e62adc2022-10-20 18:22:40 -0600709 log_debug("Invalid Backup GPT\n");
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100710 return -1;
711 }
712
713 return 0;
714}
715
Philippe Reynes5c783b52022-04-22 17:46:48 +0200716static void restore_primary_gpt_header(gpt_header *gpt_h, struct blk_desc *dev_desc)
717{
718 u32 calc_crc32;
719 u64 val;
720
721 /* recalculate the values for the Primary GPT Header */
722 val = le64_to_cpu(gpt_h->my_lba);
723 gpt_h->my_lba = gpt_h->alternate_lba;
724 gpt_h->alternate_lba = cpu_to_le64(val);
725 gpt_h->partition_entry_lba = cpu_to_le64(partition_entries_offset(dev_desc));
726
727 gpt_h->header_crc32 = 0;
728
729 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
730 le32_to_cpu(gpt_h->header_size));
731 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
732}
733
734static int write_one_gpt_table(struct blk_desc *dev_desc,
735 gpt_header *gpt_h, gpt_entry *gpt_e)
736{
737 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
738 * sizeof(gpt_entry)), dev_desc);
739 lbaint_t start;
740 int ret = 0;
741
742 start = le64_to_cpu(gpt_h->my_lba);
743 if (blk_dwrite(dev_desc, start, 1, gpt_h) != 1) {
744 ret = -1;
745 goto out;
746 }
747
748 start = le64_to_cpu(gpt_h->partition_entry_lba);
749 if (blk_dwrite(dev_desc, start, pte_blk_cnt, gpt_e) != pte_blk_cnt) {
750 ret = -1;
751 goto out;
752 }
753
754 out:
755 return ret;
756}
757
758int gpt_repair_headers(struct blk_desc *dev_desc)
759{
760 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h1, 1, dev_desc->blksz);
761 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h2, 1, dev_desc->blksz);
762 gpt_entry *gpt_e1 = NULL, *gpt_e2 = NULL;
763 int is_gpt1_valid, is_gpt2_valid;
764 int ret = -1;
765
766 is_gpt1_valid = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
767 gpt_h1, &gpt_e1);
768 is_gpt2_valid = is_gpt_valid(dev_desc, dev_desc->lba - 1,
769 gpt_h2, &gpt_e2);
770
771 if (is_gpt1_valid && is_gpt2_valid) {
772 ret = 0;
773 goto out;
774 }
775
776 if (is_gpt1_valid && !is_gpt2_valid) {
777 prepare_backup_gpt_header(gpt_h1);
778 ret = write_one_gpt_table(dev_desc, gpt_h1, gpt_e1);
779 goto out;
780 }
781
782 if (!is_gpt1_valid && is_gpt2_valid) {
783 restore_primary_gpt_header(gpt_h2, dev_desc);
784 ret = write_one_gpt_table(dev_desc, gpt_h2, gpt_e2);
785 goto out;
786 }
787
788 if (!is_gpt1_valid && !is_gpt2_valid) {
789 ret = -1;
790 goto out;
791 }
792
793 out:
794 if (is_gpt1_valid)
795 free(gpt_e1);
796 if (is_gpt2_valid)
797 free(gpt_e2);
798
799 return ret;
800}
801
Simon Glass4101f682016-02-29 15:25:34 -0700802int gpt_verify_partitions(struct blk_desc *dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600803 struct disk_partition *partitions, int parts,
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100804 gpt_header *gpt_head, gpt_entry **gpt_pte)
805{
806 char efi_str[PARTNAME_SZ + 1];
807 u64 gpt_part_size;
808 gpt_entry *gpt_e;
809 int ret, i;
810
811 ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
812 if (ret)
813 return ret;
814
815 gpt_e = *gpt_pte;
816
817 for (i = 0; i < parts; i++) {
818 if (i == gpt_head->num_partition_entries) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900819 pr_err("More partitions than allowed!\n");
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100820 return -1;
821 }
822
823 /* Check if GPT and ENV partition names match */
824 gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
825 PARTNAME_SZ + 1);
826
827 debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
828 __func__, i, efi_str, partitions[i].name);
829
830 if (strncmp(efi_str, (char *)partitions[i].name,
831 sizeof(partitions->name))) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900832 pr_err("Partition name: %s does not match %s!\n",
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100833 efi_str, (char *)partitions[i].name);
834 return -1;
835 }
836
837 /* Check if GPT and ENV sizes match */
838 gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
839 le64_to_cpu(gpt_e[i].starting_lba) + 1;
840 debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
Simon Glassf8d61652016-02-29 15:25:36 -0700841 (unsigned long long)gpt_part_size,
842 (unsigned long long)partitions[i].size);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100843
844 if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
Kever Yangc2fdd342016-07-29 11:12:18 +0800845 /* We do not check the extend partition size */
846 if ((i == parts - 1) && (partitions[i].size == 0))
847 continue;
848
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900849 pr_err("Partition %s size: %llu does not match %llu!\n",
Simon Glassf8d61652016-02-29 15:25:36 -0700850 efi_str, (unsigned long long)gpt_part_size,
851 (unsigned long long)partitions[i].size);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100852 return -1;
853 }
854
855 /*
856 * Start address is optional - check only if provided
857 * in '$partition' variable
858 */
859 if (!partitions[i].start) {
860 debug("\n");
861 continue;
862 }
863
864 /* Check if GPT and ENV start LBAs match */
865 debug("start LBA - GPT: %8llu, ENV: %8llu\n",
866 le64_to_cpu(gpt_e[i].starting_lba),
Simon Glassf8d61652016-02-29 15:25:36 -0700867 (unsigned long long)partitions[i].start);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100868
869 if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900870 pr_err("Partition %s start: %llu does not match %llu!\n",
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100871 efi_str, le64_to_cpu(gpt_e[i].starting_lba),
Simon Glassf8d61652016-02-29 15:25:36 -0700872 (unsigned long long)partitions[i].start);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100873 return -1;
874 }
875 }
876
877 return 0;
878}
879
Simon Glass4101f682016-02-29 15:25:34 -0700880int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf)
Steve Rae0ff7e582014-12-12 15:51:54 -0800881{
882 gpt_header *gpt_h;
883 gpt_entry *gpt_e;
884
885 /* determine start of GPT Header in the buffer */
886 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
887 dev_desc->blksz);
888 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
889 dev_desc->lba))
890 return -1;
891
892 /* determine start of GPT Entries in the buffer */
893 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
894 dev_desc->blksz);
895 if (validate_gpt_entries(gpt_h, gpt_e))
896 return -1;
897
898 return 0;
899}
900
Bo Lv72d0e902023-01-02 14:27:34 +0000901#ifdef CONFIG_AMLOGIC_MODIFY
902int erase_gpt_part_table(struct blk_desc *dev_desc)
903{
904 gpt_header *gpt_h;
905 int size;
906 lbaint_t lba;
907 int cnt;
908
909 printf("come to erase_gpt_part_table \n");
910
911 size = PAD_TO_BLOCKSIZE(sizeof(gpt_header), dev_desc);
912 gpt_h = malloc_cache_aligned(size);
913 if (gpt_h == NULL) {
914 printf("%s: calloc failed!\n", __func__);
915 return -1;
916 }
917 memset(gpt_h, 0, size);
918
919 /* Setup the Protective MBR */
920 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, p_mbr, 1, dev_desc->blksz);
921 if (p_mbr == NULL) {
922 printf("%s: calloc failed!\n", __func__);
923 free(gpt_h);
924 return -1;
925 }
926
927 /* Clear all data in MBR except of backed up boot code */
928 memset((char *)p_mbr + MSDOS_MBR_BOOT_CODE_SIZE, 0, sizeof(*p_mbr) -
929 MSDOS_MBR_BOOT_CODE_SIZE);
930
931 /* Write MBR sector to the MMC device */
932 if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) {
933 printf("** Can't write to device %d **\n",
934 dev_desc->devnum);
935 free(gpt_h);
936 return -1;
937 }
938
939 /* write Primary GPT */
940 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
941 cnt = 1; /* GPT Header (1 block) */
942 printf("%s: erase '%s' (%d blks at 0x" LBAF ")\n",
943 __func__, "Primary GPT Header", cnt, lba);
944 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
945 printf("%s: failed erase '%s' (%d blks at 0x" LBAF ")\n",
946 __func__, "Primary GPT Header", cnt, lba);
947 free(gpt_h);
948 return 1;
949 }
950
951 lba = cpu_to_le64(dev_desc->lba - 1);
952 cnt = 1; /* GPT Header (1 block) */
953 printf("%s: erase '%s' (%d blks at 0x" LBAF ")\n",
954 __func__, "Backup GPT Header", cnt, lba);
955 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
956 printf("%s: failed erase '%s' (%d blks at 0x" LBAF ")\n",
957 __func__, "Backup GPT Header", cnt, lba);
958 free(gpt_h);
959 return 1;
960 }
961 gpt_partition = false;
962 free(gpt_h);
963 return 0;
964}
965#endif
966
Simon Glass4101f682016-02-29 15:25:34 -0700967int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
Steve Rae0ff7e582014-12-12 15:51:54 -0800968{
969 gpt_header *gpt_h;
970 gpt_entry *gpt_e;
971 int gpt_e_blk_cnt;
972 lbaint_t lba;
973 int cnt;
Xindong Xud9441422024-01-18 10:20:45 +0800974#ifdef CONFIG_AMLOGIC_MODIFY
975 int i;
976 u32 calc_crc32;
977 u32 entries_num;
978 bool flag = false;
979 bool alternate_flag = false;
980#if (ADD_LAST_PARTITION)
981 ulong gap = GPT_GAP;
982#endif
983#endif
Steve Rae0ff7e582014-12-12 15:51:54 -0800984
985 if (is_valid_gpt_buf(dev_desc, buf))
986 return -1;
987
988 /* determine start of GPT Header in the buffer */
989 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
990 dev_desc->blksz);
991
992 /* determine start of GPT Entries in the buffer */
993 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
994 dev_desc->blksz);
995 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
996 le32_to_cpu(gpt_h->sizeof_partition_entry)),
997 dev_desc);
998
Xindong Xud9441422024-01-18 10:20:45 +0800999#ifdef CONFIG_AMLOGIC_MODIFY
1000 entries_num = le32_to_cpu(gpt_h->num_partition_entries);
1001
1002 if (le64_to_cpu(gpt_h->alternate_lba) > dev_desc->lba ||
1003 le64_to_cpu(gpt_h->alternate_lba) == 0) {
1004 printf("GPT: alternate_lba: %llX, " LBAF ", reset it\n",
1005 le64_to_cpu(gpt_h->last_usable_lba), dev_desc->lba);
1006 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
1007 flag = true;
1008 alternate_flag = true;
1009 }
1010
1011 if (le64_to_cpu(gpt_h->last_usable_lba) > dev_desc->lba) {
1012 printf("GPT: last_usable_lba incorrect: %llX > " LBAF ", reset it\n",
1013 le64_to_cpu(gpt_h->last_usable_lba), dev_desc->lba);
1014 if (alternate_flag)
1015 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
1016 else
1017 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 1);
1018 flag = true;
1019 }
1020
1021 for (i = 0; i < entries_num; i++) {
1022#if (ADD_LAST_PARTITION)
1023 if (i == entries_num - 1) {
1024 gpt_e[i - 1].ending_lba -= gpt_e[i].ending_lba + le64_to_cpu(gap) + 1;
1025 gpt_e[i].starting_lba = gpt_e[i - 1].ending_lba + le64_to_cpu(gap) + 1;
1026 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
1027 }
1028
1029#endif
1030 if (le64_to_cpu(gpt_e[i].ending_lba) > gpt_h->last_usable_lba) {
1031 printf("gpt_e[%d].ending_lba: %llX > %llX, reset it\n",
1032 i, le64_to_cpu(gpt_e[i].ending_lba), le64_to_cpu(gpt_h->last_usable_lba));
1033 if (alternate_flag)
1034 gpt_e[i].ending_lba = ((gpt_h->last_usable_lba >> 12) << 12) - 1;
1035 else
1036 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
1037 printf("gpt_e[%d].ending_lba: %llX \n", i, gpt_e[i].ending_lba);
1038 flag = true;
1039 }
1040 }
1041
1042 if (flag) {
1043 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
1044 entries_num * le32_to_cpu(gpt_h->sizeof_partition_entry));
1045 gpt_h->partition_entry_array_crc32 = calc_crc32;
1046
1047 gpt_h->header_crc32 = 0;
1048 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
1049 le32_to_cpu(gpt_h->header_size));
1050 gpt_h->header_crc32 = calc_crc32;
1051 flag = false;
1052 }
1053#endif
1054
Steve Rae0ff7e582014-12-12 15:51:54 -08001055 /* write MBR */
1056 lba = 0; /* MBR is always at 0 */
1057 cnt = 1; /* MBR (1 block) */
Simon Glass2a981dc2016-02-29 15:25:52 -07001058 if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001059 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
1060 "MBR", cnt, lba);
Steve Rae0ff7e582014-12-12 15:51:54 -08001061 return 1;
1062 }
1063
1064 /* write Primary GPT */
1065 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
1066 cnt = 1; /* GPT Header (1 block) */
Simon Glass2a981dc2016-02-29 15:25:52 -07001067 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001068 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
1069 "Primary GPT Header", cnt, lba);
Steve Rae0ff7e582014-12-12 15:51:54 -08001070 return 1;
1071 }
1072
1073 lba = le64_to_cpu(gpt_h->partition_entry_lba);
1074 cnt = gpt_e_blk_cnt;
Simon Glass2a981dc2016-02-29 15:25:52 -07001075 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001076 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
1077 "Primary GPT Entries", cnt, lba);
Steve Rae0ff7e582014-12-12 15:51:54 -08001078 return 1;
1079 }
1080
1081 prepare_backup_gpt_header(gpt_h);
1082
1083 /* write Backup GPT */
1084 lba = le64_to_cpu(gpt_h->partition_entry_lba);
1085 cnt = gpt_e_blk_cnt;
Simon Glass2a981dc2016-02-29 15:25:52 -07001086 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001087 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
1088 "Backup GPT Entries", cnt, lba);
Steve Rae0ff7e582014-12-12 15:51:54 -08001089 return 1;
1090 }
1091
1092 lba = le64_to_cpu(gpt_h->my_lba);
1093 cnt = 1; /* GPT Header (1 block) */
Simon Glass2a981dc2016-02-29 15:25:52 -07001094 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001095 log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
1096 "Backup GPT Header", cnt, lba);
Steve Rae0ff7e582014-12-12 15:51:54 -08001097 return 1;
1098 }
1099
Gary Bisson4e9bd062021-01-26 14:56:23 +01001100 /* Update the partition table entries*/
1101 part_init(dev_desc);
1102
Steve Rae0ff7e582014-12-12 15:51:54 -08001103 return 0;
1104}
Lukasz Majewski40684dd2012-12-11 11:09:46 +01001105#endif
1106
richardretanubun07f3d782008-09-26 11:13:22 -04001107/*
1108 * Private functions
1109 */
1110/*
1111 * pmbr_part_valid(): Check for EFI partition signature
1112 *
1113 * Returns: 1 if EFI GPT partition type is found.
1114 */
1115static int pmbr_part_valid(struct partition *part)
1116{
1117 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich8faefad2013-03-29 07:57:10 +00001118 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubun07f3d782008-09-26 11:13:22 -04001119 return 1;
1120 }
1121
1122 return 0;
1123}
1124
1125/*
1126 * is_pmbr_valid(): test Protective MBR for validity
1127 *
1128 * Returns: 1 if PMBR is valid, 0 otherwise.
1129 * Validity depends on two things:
1130 * 1) MSDOS signature is in the last two bytes of the MBR
1131 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
1132 */
1133static int is_pmbr_valid(legacy_mbr * mbr)
1134{
1135 int i = 0;
1136
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +01001137 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubun07f3d782008-09-26 11:13:22 -04001138 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -04001139
1140 for (i = 0; i < 4; i++) {
1141 if (pmbr_part_valid(&mbr->partition_record[i])) {
1142 return 1;
1143 }
1144 }
1145 return 0;
1146}
1147
1148/**
1149 * is_gpt_valid() - tests one GPT header and PTEs for validity
1150 *
1151 * lba is the logical block address of the GPT header to test
1152 * gpt is a GPT header ptr, filled on return.
1153 * ptes is a PTEs ptr, filled on return.
1154 *
Urja Rannikko0557d462019-04-11 20:27:51 +00001155 * Description: returns 1 if valid, 0 on error, 2 if ignored header
richardretanubun07f3d782008-09-26 11:13:22 -04001156 * If valid, returns pointers to PTEs.
1157 */
Bo Lv72d0e902023-01-02 14:27:34 +00001158#ifdef CONFIG_AMLOGIC_MODIFY
1159int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
1160 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
1161#else
Simon Glass4101f682016-02-29 15:25:34 -07001162static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
Steve Raee04350d2014-05-26 11:52:23 -07001163 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
Bo Lv72d0e902023-01-02 14:27:34 +00001164#endif
richardretanubun07f3d782008-09-26 11:13:22 -04001165{
Tom Rinib351ccf2017-10-03 09:38:44 -04001166 /* Confirm valid arguments prior to allocation. */
richardretanubun07f3d782008-09-26 11:13:22 -04001167 if (!dev_desc || !pgpt_head) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001168 log_debug("Invalid Argument(s)\n");
richardretanubun07f3d782008-09-26 11:13:22 -04001169 return 0;
1170 }
1171
Patrick Delaunay3cc56612017-11-17 10:08:18 +01001172 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, mbr, 1, dev_desc->blksz);
Tom Rinib351ccf2017-10-03 09:38:44 -04001173
Peter Jonesff98cb92017-09-13 18:05:25 -04001174 /* Read MBR Header from device */
1175 if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001176 log_debug("Can't read MBR header\n");
Peter Jonesff98cb92017-09-13 18:05:25 -04001177 return 0;
1178 }
1179
richardretanubun07f3d782008-09-26 11:13:22 -04001180 /* Read GPT Header from device */
Simon Glass2a981dc2016-02-29 15:25:52 -07001181 if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001182 log_debug("Can't read GPT header\n");
richardretanubun07f3d782008-09-26 11:13:22 -04001183 return 0;
1184 }
1185
Urja Rannikko0557d462019-04-11 20:27:51 +00001186 /* Invalid but nothing to yell about. */
1187 if (le64_to_cpu(pgpt_head->signature) == GPT_HEADER_CHROMEOS_IGNORE) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001188 log_debug("ChromeOS 'IGNOREME' GPT header found and ignored\n");
Urja Rannikko0557d462019-04-11 20:27:51 +00001189 return 2;
1190 }
1191
Steve Raee1f6b0a2014-12-18 12:13:42 +01001192 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
richardretanubun07f3d782008-09-26 11:13:22 -04001193 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -04001194
Peter Jonesff98cb92017-09-13 18:05:25 -04001195 if (dev_desc->sig_type == SIG_TYPE_NONE) {
1196 efi_guid_t empty = {};
1197 if (memcmp(&pgpt_head->disk_guid, &empty, sizeof(empty))) {
1198 dev_desc->sig_type = SIG_TYPE_GUID;
1199 memcpy(&dev_desc->guid_sig, &pgpt_head->disk_guid,
1200 sizeof(empty));
1201 } else if (mbr->unique_mbr_signature != 0) {
1202 dev_desc->sig_type = SIG_TYPE_MBR;
1203 dev_desc->mbr_sig = mbr->unique_mbr_signature;
1204 }
1205 }
1206
richardretanubun07f3d782008-09-26 11:13:22 -04001207 /* Read and allocate Partition Table Entries */
1208 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
Heinrich Schuchardt1291c2d2022-05-09 21:35:44 +02001209 if (!*pgpt_pte)
richardretanubun07f3d782008-09-26 11:13:22 -04001210 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -04001211
Steve Raee1f6b0a2014-12-18 12:13:42 +01001212 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
Doug Andersondeb5ca82011-10-19 09:47:31 +00001213 free(*pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -04001214 return 0;
1215 }
1216
1217 /* We're done, all's well */
1218 return 1;
1219}
1220
1221/**
Urja Rannikko20c568c2019-04-11 20:27:50 +00001222 * find_valid_gpt() - finds a valid GPT header and PTEs
1223 *
1224 * gpt is a GPT header ptr, filled on return.
1225 * ptes is a PTEs ptr, filled on return.
1226 *
1227 * Description: returns 1 if found a valid gpt, 0 on error.
1228 * If valid, returns pointers to PTEs.
1229 */
1230static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head,
1231 gpt_entry **pgpt_pte)
1232{
Urja Rannikko0557d462019-04-11 20:27:51 +00001233 int r;
1234
1235 r = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_head,
1236 pgpt_pte);
1237
1238 if (r != 1) {
1239 if (r != 2)
Simon Glass3e62adc2022-10-20 18:22:40 -06001240 log_debug("Invalid GPT\n");
Urja Rannikko0557d462019-04-11 20:27:51 +00001241
1242 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), gpt_head,
1243 pgpt_pte) != 1) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001244 log_debug("Invalid Backup GPT\n");
Urja Rannikko20c568c2019-04-11 20:27:50 +00001245 return 0;
1246 }
Urja Rannikko0557d462019-04-11 20:27:51 +00001247 if (r != 2)
Simon Glass3e62adc2022-10-20 18:22:40 -06001248 log_debug(" Using Backup GPT\n");
Urja Rannikko20c568c2019-04-11 20:27:50 +00001249 }
1250 return 1;
1251}
1252
1253/**
richardretanubun07f3d782008-09-26 11:13:22 -04001254 * alloc_read_gpt_entries(): reads partition entries from disk
1255 * @dev_desc
1256 * @gpt - GPT header
1257 *
1258 * Description: Returns ptes on success, NULL on error.
1259 * Allocates space for PTEs based on information found in @gpt.
1260 * Notes: remember to free pte when you're done!
1261 */
Simon Glass4101f682016-02-29 15:25:34 -07001262static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
1263 gpt_header *pgpt_head)
richardretanubun07f3d782008-09-26 11:13:22 -04001264{
Egbert Eichae1768a2013-04-09 06:03:36 +00001265 size_t count = 0, blk_cnt;
Stephen Warren7c4213f2015-12-07 11:38:48 -07001266 lbaint_t blk;
richardretanubun07f3d782008-09-26 11:13:22 -04001267 gpt_entry *pte = NULL;
1268
1269 if (!dev_desc || !pgpt_head) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001270 log_debug("Invalid Argument(s)\n");
richardretanubun07f3d782008-09-26 11:13:22 -04001271 return NULL;
1272 }
1273
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +01001274 count = le32_to_cpu(pgpt_head->num_partition_entries) *
1275 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubun07f3d782008-09-26 11:13:22 -04001276
Simon Glass3e62adc2022-10-20 18:22:40 -06001277 log_debug("count = %u * %u = %lu\n",
1278 (u32)le32_to_cpu(pgpt_head->num_partition_entries),
1279 (u32)le32_to_cpu(pgpt_head->sizeof_partition_entry),
1280 (ulong)count);
richardretanubun07f3d782008-09-26 11:13:22 -04001281
1282 /* Allocate memory for PTE, remember to FREE */
1283 if (count != 0) {
Egbert Eichae1768a2013-04-09 06:03:36 +00001284 pte = memalign(ARCH_DMA_MINALIGN,
1285 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubun07f3d782008-09-26 11:13:22 -04001286 }
1287
1288 if (count == 0 || pte == NULL) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001289 log_debug("ERROR: Can't allocate %#lX bytes for GPT Entries\n",
1290 (ulong)count);
richardretanubun07f3d782008-09-26 11:13:22 -04001291 return NULL;
1292 }
1293
1294 /* Read GPT Entries from device */
Stephen Warren7c4213f2015-12-07 11:38:48 -07001295 blk = le64_to_cpu(pgpt_head->partition_entry_lba);
Egbert Eichae1768a2013-04-09 06:03:36 +00001296 blk_cnt = BLOCK_CNT(count, dev_desc);
Simon Glass2a981dc2016-02-29 15:25:52 -07001297 if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001298 log_debug("Can't read GPT Entries\n");
richardretanubun07f3d782008-09-26 11:13:22 -04001299 free(pte);
1300 return NULL;
1301 }
1302 return pte;
1303}
1304
1305/**
1306 * is_pte_valid(): validates a single Partition Table Entry
1307 * @gpt_entry - Pointer to a single Partition Table Entry
1308 *
1309 * Description: returns 1 if valid, 0 on error.
1310 */
Bo Lv72d0e902023-01-02 14:27:34 +00001311#ifdef CONFIG_AMLOGIC_MODIFY
1312int is_pte_valid(gpt_entry * pte)
1313#else
richardretanubun07f3d782008-09-26 11:13:22 -04001314static int is_pte_valid(gpt_entry * pte)
Bo Lv72d0e902023-01-02 14:27:34 +00001315#endif
richardretanubun07f3d782008-09-26 11:13:22 -04001316{
1317 efi_guid_t unused_guid;
1318
1319 if (!pte) {
Simon Glass3e62adc2022-10-20 18:22:40 -06001320 log_debug("Invalid Argument(s)\n");
richardretanubun07f3d782008-09-26 11:13:22 -04001321 return 0;
1322 }
1323
1324 /* Only one validation for now:
1325 * The GUID Partition Type != Unused Entry (ALL-ZERO)
1326 */
1327 memset(unused_guid.b, 0, sizeof(unused_guid.b));
1328
1329 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
1330 sizeof(unused_guid.b)) == 0) {
1331
Simon Glass3e62adc2022-10-20 18:22:40 -06001332 log_debug("Found an unused PTE GUID at 0x%08X\n",
1333 (unsigned int)(uintptr_t)pte);
richardretanubun07f3d782008-09-26 11:13:22 -04001334
1335 return 0;
1336 } else {
1337 return 1;
1338 }
1339}
Simon Glass96e5b032016-02-29 15:25:47 -07001340
1341/*
1342 * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to
1343 * check EFI first, since a DOS partition is often used as a 'protective MBR'
1344 * with EFI.
1345 */
1346U_BOOT_PART_TYPE(a_efi) = {
1347 .name = "EFI",
1348 .part_type = PART_TYPE_EFI,
Petr Kulhavy87b85302016-09-09 10:27:15 +02001349 .max_entries = GPT_ENTRY_NUMBERS,
Simon Glass3e8bd462016-02-29 15:25:48 -07001350 .get_info = part_get_info_ptr(part_get_info_efi),
Simon Glass084bf4c2016-02-29 15:26:04 -07001351 .print = part_print_ptr(part_print_efi),
1352 .test = part_test_efi,
Simon Glass96e5b032016-02-29 15:25:47 -07001353};