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