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