xref: /rk3399_rockchip-uboot/disk/part_efi.c (revision 14569d26731af93622d8fcf87429b06ece85047b)
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 static void gpt_entry_modify(struct blk_desc *dev_desc,
345 			     gpt_entry *gpt_pte,
346 			     gpt_header *gpt_head)
347 {
348 	int i;
349 	uint32_t calc_crc32;
350 
351 	for (i = 0; i < gpt_head->num_partition_entries; i++) {
352 		if (!is_pte_valid(&gpt_pte[i]))
353 			break;
354 	}
355 
356 	if (gpt_pte[i - 1].ending_lba <= (dev_desc->lba - 0x22))
357 		return;
358 
359 	gpt_pte[i - 1].ending_lba = dev_desc->lba - 0x22;
360 	calc_crc32 = efi_crc32((const unsigned char *)gpt_pte,
361 			       le32_to_cpu(gpt_head->num_partition_entries) *
362 			       le32_to_cpu(gpt_head->sizeof_partition_entry));
363 	gpt_head->partition_entry_array_crc32 = calc_crc32;
364 }
365 
366 static int part_efi_repair(struct blk_desc *dev_desc, gpt_entry *gpt_pte,
367 			   gpt_header *gpt_head, int head_gpt_valid,
368 			   int backup_gpt_valid)
369 {
370 	uint32_t calc_crc32;
371 	size_t count = 0, blk_cnt;
372 	lbaint_t blk;
373 
374 	if (head_gpt_valid == 1 && backup_gpt_valid == 1) {
375 		return 0;
376 	} else if (head_gpt_valid == 0 && backup_gpt_valid == 0) {
377 		return -1;
378 	} else if (head_gpt_valid == 1 && backup_gpt_valid == 0) {
379 		gpt_head->header_crc32 = 0;
380 		gpt_head->my_lba = dev_desc->lba - 1;
381 		gpt_head->alternate_lba = 1;
382 		gpt_head->partition_entry_lba = dev_desc->lba - 0x21;
383 		gpt_entry_modify(dev_desc, gpt_pte, gpt_head);
384 		calc_crc32 = efi_crc32((const unsigned char *)gpt_head,
385 				       le32_to_cpu(gpt_head->header_size));
386 		gpt_head->header_crc32 = calc_crc32;
387 		if (blk_dwrite(dev_desc, dev_desc->lba - 1, 1, gpt_head) != 1) {
388 			printf("*** ERROR: Can't write GPT header ***\n");
389 			return -1;
390 		}
391 		count = le32_to_cpu(gpt_head->num_partition_entries) *
392 			le32_to_cpu(gpt_head->sizeof_partition_entry);
393 		blk = le64_to_cpu(gpt_head->partition_entry_lba);
394 		blk_cnt = BLOCK_CNT(count, dev_desc);
395 		if (blk_dwrite(dev_desc, blk, (lbaint_t)blk_cnt, gpt_pte) !=
396 		    blk_cnt) {
397 			printf("*** ERROR: Can't write entry partitions ***\n");
398 			return -1;
399 		}
400 		printf("Repair the backup gpt table OK!\n");
401 	} else if (head_gpt_valid == 0 && backup_gpt_valid == 1) {
402 		gpt_head->header_crc32 = 0;
403 		gpt_head->my_lba = 1;
404 		gpt_head->alternate_lba = dev_desc->lba - 1;
405 		gpt_head->partition_entry_lba = 0x22;
406 		gpt_entry_modify(dev_desc, gpt_pte, gpt_head);
407 		calc_crc32 = efi_crc32((const unsigned char *)gpt_head,
408 				       le32_to_cpu(gpt_head->header_size));
409 		gpt_head->header_crc32 = calc_crc32;
410 		if (blk_dwrite(dev_desc, 1, 1, gpt_head) != 1) {
411 			printf("*** ERROR: Can't write GPT header ***\n");
412 			return -1;
413 		}
414 		count = le32_to_cpu(gpt_head->num_partition_entries) *
415 			le32_to_cpu(gpt_head->sizeof_partition_entry);
416 		blk = le64_to_cpu(gpt_head->partition_entry_lba);
417 		blk_cnt = BLOCK_CNT(count, dev_desc);
418 		if (blk_dwrite(dev_desc, blk, (lbaint_t)blk_cnt, gpt_pte) !=
419 		    blk_cnt) {
420 			printf("*** ERROR: Can't write entry partitions ***\n");
421 			return -1;
422 		}
423 		printf("Repair the Primary gpt table OK!\n");
424 	}
425 
426 	return 0;
427 }
428 #endif
429 
430 static int part_test_efi(struct blk_desc *dev_desc)
431 {
432 	ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
433 	int ret = 0;
434 
435 	/* Read legacy MBR from block 0 and validate it */
436 	if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1)
437 		|| (is_pmbr_valid(legacymbr) != 1)) {
438 		return -1;
439 	}
440 #ifdef CONFIG_RKIMG_BOOTLOADER
441 	gpt_entry *h_gpt_pte = NULL;
442 	gpt_header *h_gpt_head = NULL;
443 	gpt_entry *b_gpt_pte = NULL;
444 	gpt_header *b_gpt_head = NULL;
445 	int head_gpt_valid = 0;
446 	int backup_gpt_valid = 0;
447 
448 	if (!h_gpt_head)
449 		h_gpt_head = memalign(ARCH_DMA_MINALIGN, dev_desc->blksz);
450 	if (!b_gpt_head)
451 		b_gpt_head = memalign(ARCH_DMA_MINALIGN, dev_desc->blksz);
452 
453 	head_gpt_valid = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
454 				      h_gpt_head, &h_gpt_pte);
455 	backup_gpt_valid = is_gpt_valid(dev_desc, (dev_desc->lba - 1),
456 					b_gpt_head, &b_gpt_pte);
457 	if (head_gpt_valid == 1 && backup_gpt_valid == 0) {
458 		if (part_efi_repair(dev_desc, h_gpt_pte, h_gpt_head,
459 				    head_gpt_valid, backup_gpt_valid))
460 			printf("Backup GPT repair fail!\n");
461 	} else if (head_gpt_valid == 0 && backup_gpt_valid == 1) {
462 		if (part_efi_repair(dev_desc, b_gpt_pte, b_gpt_head,
463 				    head_gpt_valid, backup_gpt_valid))
464 			printf("Primary GPT repair fail!\n");
465 	} else if (head_gpt_valid == 0 && backup_gpt_valid == 0) {
466 		ret = -1;
467 	}
468 
469 	free(h_gpt_pte);
470 	h_gpt_pte = NULL;
471 	free(h_gpt_head);
472 	h_gpt_head = NULL;
473 	free(b_gpt_pte);
474 	b_gpt_pte = NULL;
475 	free(b_gpt_head);
476 	b_gpt_head = NULL;
477 #endif
478 	return ret;
479 }
480 
481 /**
482  * set_protective_mbr(): Set the EFI protective MBR
483  * @param dev_desc - block device descriptor
484  *
485  * @return - zero on success, otherwise error
486  */
487 static int set_protective_mbr(struct blk_desc *dev_desc)
488 {
489 	/* Setup the Protective MBR */
490 	ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
491 	memset(p_mbr, 0, sizeof(*p_mbr));
492 
493 	if (p_mbr == NULL) {
494 		printf("%s: calloc failed!\n", __func__);
495 		return -1;
496 	}
497 
498 	/* Read MBR to backup boot code if it exists */
499 	if (blk_dread(dev_desc, 0, 1, p_mbr) != 1) {
500 		pr_err("** Can't read from device %d **\n", dev_desc->devnum);
501 		return -1;
502 	}
503 
504 	/* Append signature */
505 	p_mbr->signature = MSDOS_MBR_SIGNATURE;
506 	p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
507 	p_mbr->partition_record[0].start_sect = 1;
508 	p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
509 
510 	/* Write MBR sector to the MMC device */
511 	if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) {
512 		printf("** Can't write to device %d **\n",
513 			dev_desc->devnum);
514 		return -1;
515 	}
516 
517 	return 0;
518 }
519 
520 int write_gpt_table(struct blk_desc *dev_desc,
521 		gpt_header *gpt_h, gpt_entry *gpt_e)
522 {
523 	const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
524 					   * sizeof(gpt_entry)), dev_desc);
525 	u32 calc_crc32;
526 
527 	debug("max lba: %x\n", (u32) dev_desc->lba);
528 	/* Setup the Protective MBR */
529 	if (set_protective_mbr(dev_desc) < 0)
530 		goto err;
531 
532 	/* Generate CRC for the Primary GPT Header */
533 	calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
534 			      le32_to_cpu(gpt_h->num_partition_entries) *
535 			      le32_to_cpu(gpt_h->sizeof_partition_entry));
536 	gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
537 
538 	calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
539 			      le32_to_cpu(gpt_h->header_size));
540 	gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
541 
542 	/* Write the First GPT to the block right after the Legacy MBR */
543 	if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1)
544 		goto err;
545 
546 	if (blk_dwrite(dev_desc, le64_to_cpu(gpt_h->partition_entry_lba),
547 		       pte_blk_cnt, gpt_e) != pte_blk_cnt)
548 		goto err;
549 
550 	prepare_backup_gpt_header(gpt_h);
551 
552 	if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
553 		       + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
554 		goto err;
555 
556 	if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
557 		       gpt_h) != 1)
558 		goto err;
559 
560 	debug("GPT successfully written to block device!\n");
561 	return 0;
562 
563  err:
564 	printf("** Can't write to device %d **\n", dev_desc->devnum);
565 	return -1;
566 }
567 
568 int gpt_fill_pte(struct blk_desc *dev_desc,
569 		 gpt_header *gpt_h, gpt_entry *gpt_e,
570 		 disk_partition_t *partitions, int parts)
571 {
572 	lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
573 	lbaint_t last_usable_lba = (lbaint_t)
574 			le64_to_cpu(gpt_h->last_usable_lba);
575 	int i, k;
576 	size_t efiname_len, dosname_len;
577 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
578 	char *str_uuid;
579 	unsigned char *bin_uuid;
580 #endif
581 #ifdef CONFIG_PARTITION_TYPE_GUID
582 	char *str_type_guid;
583 	unsigned char *bin_type_guid;
584 #endif
585 	size_t hdr_start = gpt_h->my_lba;
586 	size_t hdr_end = hdr_start + 1;
587 
588 	size_t pte_start = gpt_h->partition_entry_lba;
589 	size_t pte_end = pte_start +
590 		gpt_h->num_partition_entries * gpt_h->sizeof_partition_entry /
591 		dev_desc->blksz;
592 
593 	for (i = 0; i < parts; i++) {
594 		/* partition starting lba */
595 		lbaint_t start = partitions[i].start;
596 		lbaint_t size = partitions[i].size;
597 
598 		if (start) {
599 			offset = start + size;
600 		} else {
601 			start = offset;
602 			offset += size;
603 		}
604 
605 		/*
606 		 * If our partition overlaps with either the GPT
607 		 * header, or the partition entry, reject it.
608 		 */
609 		if (((start < hdr_end && hdr_start < (start + size)) ||
610 		     (start < pte_end && pte_start < (start + size)))) {
611 			printf("Partition overlap\n");
612 			return -1;
613 		}
614 
615 		gpt_e[i].starting_lba = cpu_to_le64(start);
616 
617 		if (offset > (last_usable_lba + 1)) {
618 			printf("Partitions layout exceds disk size\n");
619 			return -1;
620 		}
621 		/* partition ending lba */
622 		if ((i == parts - 1) && (size == 0))
623 			/* extend the last partition to maximuim */
624 			gpt_e[i].ending_lba = gpt_h->last_usable_lba;
625 		else
626 			gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
627 
628 #ifdef CONFIG_PARTITION_TYPE_GUID
629 		str_type_guid = partitions[i].type_guid;
630 		bin_type_guid = gpt_e[i].partition_type_guid.b;
631 		if (strlen(str_type_guid)) {
632 			if (uuid_str_to_bin(str_type_guid, bin_type_guid,
633 					    UUID_STR_FORMAT_GUID)) {
634 				printf("Partition no. %d: invalid type guid: %s\n",
635 				       i, str_type_guid);
636 				return -1;
637 			}
638 		} else {
639 			/* default partition type GUID */
640 			memcpy(bin_type_guid,
641 			       &PARTITION_BASIC_DATA_GUID, 16);
642 		}
643 #else
644 		/* partition type GUID */
645 		memcpy(gpt_e[i].partition_type_guid.b,
646 			&PARTITION_BASIC_DATA_GUID, 16);
647 #endif
648 
649 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
650 		str_uuid = partitions[i].uuid;
651 		bin_uuid = gpt_e[i].unique_partition_guid.b;
652 
653 		if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_GUID)) {
654 			printf("Partition no. %d: invalid guid: %s\n",
655 				i, str_uuid);
656 			return -1;
657 		}
658 #endif
659 
660 		/* partition attributes */
661 		memset(&gpt_e[i].attributes, 0,
662 		       sizeof(gpt_entry_attributes));
663 
664 		if (partitions[i].bootable)
665 			gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
666 
667 		/* partition name */
668 		efiname_len = sizeof(gpt_e[i].partition_name)
669 			/ sizeof(efi_char16_t);
670 		dosname_len = sizeof(partitions[i].name);
671 
672 		memset(gpt_e[i].partition_name, 0,
673 		       sizeof(gpt_e[i].partition_name));
674 
675 		for (k = 0; k < min(dosname_len, efiname_len); k++)
676 			gpt_e[i].partition_name[k] =
677 				(efi_char16_t)(partitions[i].name[k]);
678 
679 		debug("%s: name: %s offset[%d]: 0x" LBAF
680 		      " size[%d]: 0x" LBAF "\n",
681 		      __func__, partitions[i].name, i,
682 		      offset, i, size);
683 	}
684 
685 	return 0;
686 }
687 
688 static uint32_t partition_entries_offset(struct blk_desc *dev_desc)
689 {
690 	uint32_t offset_blks = 2;
691 	uint32_t __maybe_unused offset_bytes;
692 	int __maybe_unused config_offset;
693 
694 #if defined(CONFIG_EFI_PARTITION_ENTRIES_OFF)
695 	/*
696 	 * Some architectures require their SPL loader at a fixed
697 	 * address within the first 16KB of the disk.  To avoid an
698 	 * overlap with the partition entries of the EFI partition
699 	 * table, the first safe offset (in bytes, from the start of
700 	 * the disk) for the entries can be set in
701 	 * CONFIG_EFI_PARTITION_ENTRIES_OFF.
702 	 */
703 	offset_bytes =
704 		PAD_TO_BLOCKSIZE(CONFIG_EFI_PARTITION_ENTRIES_OFF, dev_desc);
705 	offset_blks = offset_bytes / dev_desc->blksz;
706 #endif
707 
708 #if defined(CONFIG_OF_CONTROL)
709 	/*
710 	 * Allow the offset of the first partition entires (in bytes
711 	 * from the start of the device) to be specified as a property
712 	 * of the device tree '/config' node.
713 	 */
714 	config_offset = fdtdec_get_config_int(gd->fdt_blob,
715 					      "u-boot,efi-partition-entries-offset",
716 					      -EINVAL);
717 	if (config_offset != -EINVAL) {
718 		offset_bytes = PAD_TO_BLOCKSIZE(config_offset, dev_desc);
719 		offset_blks = offset_bytes / dev_desc->blksz;
720 	}
721 #endif
722 
723 	debug("efi: partition entries offset (in blocks): %d\n", offset_blks);
724 
725 	/*
726 	 * The earliest LBA this can be at is LBA#2 (i.e. right behind
727 	 * the (protective) MBR and the GPT header.
728 	 */
729 	if (offset_blks < 2)
730 		offset_blks = 2;
731 
732 	return offset_blks;
733 }
734 
735 int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
736 		char *str_guid, int parts_count)
737 {
738 	gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
739 	gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
740 	gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
741 	gpt_h->my_lba = cpu_to_le64(1);
742 	gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
743 	gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
744 	gpt_h->partition_entry_lba =
745 		cpu_to_le64(partition_entries_offset(dev_desc));
746 	gpt_h->first_usable_lba =
747 		cpu_to_le64(le64_to_cpu(gpt_h->partition_entry_lba) + 32);
748 	gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
749 	gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
750 	gpt_h->header_crc32 = 0;
751 	gpt_h->partition_entry_array_crc32 = 0;
752 
753 	if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
754 		return -1;
755 
756 	return 0;
757 }
758 
759 int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
760 		disk_partition_t *partitions, int parts_count)
761 {
762 	gpt_header *gpt_h;
763 	gpt_entry *gpt_e;
764 	int ret, size;
765 
766 	size = PAD_TO_BLOCKSIZE(sizeof(gpt_header), dev_desc);
767 	gpt_h = malloc_cache_aligned(size);
768 	if (gpt_h == NULL) {
769 		printf("%s: calloc failed!\n", __func__);
770 		return -1;
771 	}
772 	memset(gpt_h, 0, size);
773 
774 	size = PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS * sizeof(gpt_entry),
775 				dev_desc);
776 	gpt_e = malloc_cache_aligned(size);
777 	if (gpt_e == NULL) {
778 		printf("%s: calloc failed!\n", __func__);
779 		free(gpt_h);
780 		return -1;
781 	}
782 	memset(gpt_e, 0, size);
783 
784 	/* Generate Primary GPT header (LBA1) */
785 	ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
786 	if (ret)
787 		goto err;
788 
789 	/* Generate partition entries */
790 	ret = gpt_fill_pte(dev_desc, gpt_h, gpt_e, partitions, parts_count);
791 	if (ret)
792 		goto err;
793 
794 	/* Write GPT partition table */
795 	ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
796 
797 err:
798 	free(gpt_e);
799 	free(gpt_h);
800 	return ret;
801 }
802 
803 static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n)
804 {
805 	char *ess = (char *)es;
806 	int i, j;
807 
808 	memset(s, '\0', n);
809 
810 	for (i = 0, j = 0; j < n; i += 2, j++) {
811 		s[j] = ess[i];
812 		if (!ess[i])
813 			return;
814 	}
815 }
816 
817 int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
818 		       gpt_entry **gpt_pte)
819 {
820 	/*
821 	 * This function validates AND
822 	 * fills in the GPT header and PTE
823 	 */
824 	if (is_gpt_valid(dev_desc,
825 			 GPT_PRIMARY_PARTITION_TABLE_LBA,
826 			 gpt_head, gpt_pte) != 1) {
827 		printf("%s: *** ERROR: Invalid GPT ***\n",
828 		       __func__);
829 		return -1;
830 	}
831 	if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
832 			 gpt_head, gpt_pte) != 1) {
833 		printf("%s: *** ERROR: Invalid Backup GPT ***\n",
834 		       __func__);
835 		return -1;
836 	}
837 
838 	return 0;
839 }
840 
841 int gpt_verify_partitions(struct blk_desc *dev_desc,
842 			  disk_partition_t *partitions, int parts,
843 			  gpt_header *gpt_head, gpt_entry **gpt_pte)
844 {
845 	char efi_str[PARTNAME_SZ + 1];
846 	u64 gpt_part_size;
847 	gpt_entry *gpt_e;
848 	int ret, i;
849 
850 	ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
851 	if (ret)
852 		return ret;
853 
854 	gpt_e = *gpt_pte;
855 
856 	for (i = 0; i < parts; i++) {
857 		if (i == gpt_head->num_partition_entries) {
858 			pr_err("More partitions than allowed!\n");
859 			return -1;
860 		}
861 
862 		/* Check if GPT and ENV partition names match */
863 		gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
864 					     PARTNAME_SZ + 1);
865 
866 		debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
867 		      __func__, i, efi_str, partitions[i].name);
868 
869 		if (strncmp(efi_str, (char *)partitions[i].name,
870 			    sizeof(partitions->name))) {
871 			pr_err("Partition name: %s does not match %s!\n",
872 			      efi_str, (char *)partitions[i].name);
873 			return -1;
874 		}
875 
876 		/* Check if GPT and ENV sizes match */
877 		gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
878 			le64_to_cpu(gpt_e[i].starting_lba) + 1;
879 		debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
880 		      (unsigned long long)gpt_part_size,
881 		      (unsigned long long)partitions[i].size);
882 
883 		if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
884 			/* We do not check the extend partition size */
885 			if ((i == parts - 1) && (partitions[i].size == 0))
886 				continue;
887 
888 			pr_err("Partition %s size: %llu does not match %llu!\n",
889 			      efi_str, (unsigned long long)gpt_part_size,
890 			      (unsigned long long)partitions[i].size);
891 			return -1;
892 		}
893 
894 		/*
895 		 * Start address is optional - check only if provided
896 		 * in '$partition' variable
897 		 */
898 		if (!partitions[i].start) {
899 			debug("\n");
900 			continue;
901 		}
902 
903 		/* Check if GPT and ENV start LBAs match */
904 		debug("start LBA - GPT: %8llu, ENV: %8llu\n",
905 		      le64_to_cpu(gpt_e[i].starting_lba),
906 		      (unsigned long long)partitions[i].start);
907 
908 		if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
909 			pr_err("Partition %s start: %llu does not match %llu!\n",
910 			      efi_str, le64_to_cpu(gpt_e[i].starting_lba),
911 			      (unsigned long long)partitions[i].start);
912 			return -1;
913 		}
914 	}
915 
916 	return 0;
917 }
918 
919 int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf)
920 {
921 	gpt_header *gpt_h;
922 	gpt_entry *gpt_e;
923 
924 	/* determine start of GPT Header in the buffer */
925 	gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
926 		       dev_desc->blksz);
927 
928 	if ((le64_to_cpu(gpt_h->alternate_lba) + 1)
929 			!= cpu_to_le64(dev_desc->lba)) {
930 		printf("%s: failed checking '%s'\n", __func__,
931 		       "invalid GPT Disk Size");
932 		return -1;
933 	}
934 
935 	if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
936 				dev_desc->lba))
937 		return -1;
938 
939 	/* determine start of GPT Entries in the buffer */
940 	gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
941 		       dev_desc->blksz);
942 	if (validate_gpt_entries(gpt_h, gpt_e))
943 		return -1;
944 
945 	return 0;
946 }
947 
948 int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
949 {
950 	gpt_header *gpt_h;
951 	gpt_entry *gpt_e;
952 	int gpt_e_blk_cnt;
953 	lbaint_t lba;
954 	int cnt;
955 
956 	if (is_valid_gpt_buf(dev_desc, buf))
957 		return -1;
958 
959 	/* determine start of GPT Header in the buffer */
960 	gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
961 		       dev_desc->blksz);
962 
963 	/* determine start of GPT Entries in the buffer */
964 	gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
965 		       dev_desc->blksz);
966 	gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
967 				   le32_to_cpu(gpt_h->sizeof_partition_entry)),
968 				  dev_desc);
969 
970 	/* write MBR */
971 	lba = 0;	/* MBR is always at 0 */
972 	cnt = 1;	/* MBR (1 block) */
973 	if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) {
974 		printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
975 		       __func__, "MBR", cnt, lba);
976 		return 1;
977 	}
978 
979 	/* write Primary GPT */
980 	lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
981 	cnt = 1;	/* GPT Header (1 block) */
982 	if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
983 		printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
984 		       __func__, "Primary GPT Header", cnt, lba);
985 		return 1;
986 	}
987 
988 	lba = le64_to_cpu(gpt_h->partition_entry_lba);
989 	cnt = gpt_e_blk_cnt;
990 	if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
991 		printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
992 		       __func__, "Primary GPT Entries", cnt, lba);
993 		return 1;
994 	}
995 
996 	prepare_backup_gpt_header(gpt_h);
997 
998 	/* write Backup GPT */
999 	lba = le64_to_cpu(gpt_h->partition_entry_lba);
1000 	cnt = gpt_e_blk_cnt;
1001 	if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
1002 		printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
1003 		       __func__, "Backup GPT Entries", cnt, lba);
1004 		return 1;
1005 	}
1006 
1007 	lba = le64_to_cpu(gpt_h->my_lba);
1008 	cnt = 1;	/* GPT Header (1 block) */
1009 	if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
1010 		printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
1011 		       __func__, "Backup GPT Header", cnt, lba);
1012 		return 1;
1013 	}
1014 
1015 	return 0;
1016 }
1017 #endif
1018 
1019 /*
1020  * Private functions
1021  */
1022 /*
1023  * pmbr_part_valid(): Check for EFI partition signature
1024  *
1025  * Returns: 1 if EFI GPT partition type is found.
1026  */
1027 static int pmbr_part_valid(struct partition *part)
1028 {
1029 	if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
1030 		get_unaligned_le32(&part->start_sect) == 1UL) {
1031 		return 1;
1032 	}
1033 
1034 	return 0;
1035 }
1036 
1037 /*
1038  * is_pmbr_valid(): test Protective MBR for validity
1039  *
1040  * Returns: 1 if PMBR is valid, 0 otherwise.
1041  * Validity depends on two things:
1042  *  1) MSDOS signature is in the last two bytes of the MBR
1043  *  2) One partition of type 0xEE is found, checked by pmbr_part_valid()
1044  */
1045 static int is_pmbr_valid(legacy_mbr * mbr)
1046 {
1047 	int i = 0;
1048 
1049 	if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
1050 		return 0;
1051 
1052 #ifdef CONFIG_ARCH_ROCKCHIP
1053 	/*
1054 	 * In sd-update card, we use RKPARM partition in bootloader to load
1055 	 * firmware, and use MS-DOS partition in recovery to update system.
1056 	 * Now, we want to use gpt in bootloader and abandon the RKPARM
1057 	 * partition. So in new sd-update card, we write the MS-DOS partition
1058 	 * table and gpt to sd card. Then we must return 1 directly when test
1059 	 * the mbr sector otherwise the gpt is unavailable.
1060 	 */
1061 	return 1;
1062 #endif
1063 	for (i = 0; i < 4; i++) {
1064 		if (pmbr_part_valid(&mbr->partition_record[i])) {
1065 			return 1;
1066 		}
1067 	}
1068 	return 0;
1069 }
1070 
1071 /**
1072  * is_gpt_valid() - tests one GPT header and PTEs for validity
1073  *
1074  * lba is the logical block address of the GPT header to test
1075  * gpt is a GPT header ptr, filled on return.
1076  * ptes is a PTEs ptr, filled on return.
1077  *
1078  * Description: returns 1 if valid,  0 on error.
1079  * If valid, returns pointers to PTEs.
1080  */
1081 static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
1082 			gpt_header *pgpt_head, gpt_entry **pgpt_pte)
1083 {
1084 	/* Confirm valid arguments prior to allocation. */
1085 	if (!dev_desc || !pgpt_head) {
1086 		printf("%s: Invalid Argument(s)\n", __func__);
1087 		return 0;
1088 	}
1089 
1090 	/* Re-use pte if it's not NULL */
1091 	if (*pgpt_pte)
1092 		return 1;
1093 
1094 	ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr, dev_desc->blksz);
1095 
1096 	/* Read MBR Header from device */
1097 	if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1) {
1098 		printf("*** ERROR: Can't read MBR header ***\n");
1099 		return 0;
1100 	}
1101 
1102 	/* Read GPT Header from device */
1103 	if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
1104 		printf("*** ERROR: Can't read GPT header ***\n");
1105 		return 0;
1106 	}
1107 
1108 	if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
1109 		return 0;
1110 
1111 	if (dev_desc->sig_type == SIG_TYPE_NONE) {
1112 		efi_guid_t empty = {};
1113 		if (memcmp(&pgpt_head->disk_guid, &empty, sizeof(empty))) {
1114 			dev_desc->sig_type = SIG_TYPE_GUID;
1115 			memcpy(&dev_desc->guid_sig, &pgpt_head->disk_guid,
1116 			      sizeof(empty));
1117 		} else if (mbr->unique_mbr_signature != 0) {
1118 			dev_desc->sig_type = SIG_TYPE_MBR;
1119 			dev_desc->mbr_sig = mbr->unique_mbr_signature;
1120 		}
1121 	}
1122 
1123 	/* Read and allocate Partition Table Entries */
1124 	*pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
1125 	if (*pgpt_pte == NULL) {
1126 		printf("GPT: Failed to allocate memory for PTE\n");
1127 		return 0;
1128 	}
1129 
1130 	if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
1131 		free(*pgpt_pte);
1132 		*pgpt_pte = NULL;
1133 		return 0;
1134 	}
1135 
1136 	/* We're done, all's well */
1137 	return 1;
1138 }
1139 
1140 /**
1141  * alloc_read_gpt_entries(): reads partition entries from disk
1142  * @dev_desc
1143  * @gpt - GPT header
1144  *
1145  * Description: Returns ptes on success,  NULL on error.
1146  * Allocates space for PTEs based on information found in @gpt.
1147  * Notes: remember to free pte when you're done!
1148  */
1149 static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
1150 					 gpt_header *pgpt_head)
1151 {
1152 	size_t count = 0, blk_cnt;
1153 	lbaint_t blk;
1154 	gpt_entry *pte = NULL;
1155 
1156 	if (!dev_desc || !pgpt_head) {
1157 		printf("%s: Invalid Argument(s)\n", __func__);
1158 		return NULL;
1159 	}
1160 
1161 	count = le32_to_cpu(pgpt_head->num_partition_entries) *
1162 		le32_to_cpu(pgpt_head->sizeof_partition_entry);
1163 
1164 	debug("%s: count = %u * %u = %lu\n", __func__,
1165 	      (u32) le32_to_cpu(pgpt_head->num_partition_entries),
1166 	      (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry),
1167 	      (ulong)count);
1168 
1169 	/* Allocate memory for PTE, remember to FREE */
1170 	if (count != 0) {
1171 		pte = memalign(ARCH_DMA_MINALIGN,
1172 			       PAD_TO_BLOCKSIZE(count, dev_desc));
1173 	}
1174 
1175 	if (count == 0 || pte == NULL) {
1176 		printf("%s: ERROR: Can't allocate %#lX bytes for GPT Entries\n",
1177 		       __func__, (ulong)count);
1178 		return NULL;
1179 	}
1180 
1181 	/* Read GPT Entries from device */
1182 	blk = le64_to_cpu(pgpt_head->partition_entry_lba);
1183 	blk_cnt = BLOCK_CNT(count, dev_desc);
1184 	if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
1185 		printf("*** ERROR: Can't read GPT Entries ***\n");
1186 		free(pte);
1187 		return NULL;
1188 	}
1189 	return pte;
1190 }
1191 
1192 /**
1193  * is_pte_valid(): validates a single Partition Table Entry
1194  * @gpt_entry - Pointer to a single Partition Table Entry
1195  *
1196  * Description: returns 1 if valid,  0 on error.
1197  */
1198 static int is_pte_valid(gpt_entry * pte)
1199 {
1200 	efi_guid_t unused_guid;
1201 
1202 	if (!pte) {
1203 		printf("%s: Invalid Argument(s)\n", __func__);
1204 		return 0;
1205 	}
1206 
1207 	/* Only one validation for now:
1208 	 * The GUID Partition Type != Unused Entry (ALL-ZERO)
1209 	 */
1210 	memset(unused_guid.b, 0, sizeof(unused_guid.b));
1211 
1212 	if (memcmp(pte->partition_type_guid.b, unused_guid.b,
1213 		sizeof(unused_guid.b)) == 0) {
1214 
1215 		debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
1216 		      (unsigned int)(uintptr_t)pte);
1217 
1218 		return 0;
1219 	} else {
1220 		return 1;
1221 	}
1222 }
1223 
1224 /*
1225  * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to
1226  * check EFI first, since a DOS partition is often used as a 'protective MBR'
1227  * with EFI.
1228  */
1229 U_BOOT_PART_TYPE(a_efi) = {
1230 	.name		= "EFI",
1231 	.part_type	= PART_TYPE_EFI,
1232 	.max_entries	= GPT_ENTRY_NUMBERS,
1233 	.get_info	= part_get_info_ptr(part_get_info_efi),
1234 	.print		= part_print_ptr(part_print_efi),
1235 	.test		= part_test_efi,
1236 };
1237 #endif
1238