xref: /openbmc/u-boot/disk/part_efi.c (revision 68fbc0e6)
1 /*
2  * Copyright (C) 2008 RuggedCom, Inc.
3  * Richard Retanubun <RichardRetanubun@RuggedCom.com>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 /*
25  * Problems with CONFIG_SYS_64BIT_LBA:
26  *
27  * struct disk_partition.start in include/part.h is sized as ulong.
28  * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t.
29  * For now, it is cast back to ulong at assignment.
30  *
31  * This limits the maximum size of addressable storage to < 2 Terra Bytes
32  */
33 #include <common.h>
34 #include <command.h>
35 #include <ide.h>
36 #include <malloc.h>
37 #include <part_efi.h>
38 #include <linux/ctype.h>
39 
40 DECLARE_GLOBAL_DATA_PTR;
41 
42 #ifdef HAVE_BLOCK_DEVICE
43 /**
44  * efi_crc32() - EFI version of crc32 function
45  * @buf: buffer to calculate crc32 of
46  * @len - length of buf
47  *
48  * Description: Returns EFI-style CRC32 value for @buf
49  */
50 static inline u32 efi_crc32(const void *buf, u32 len)
51 {
52 	return crc32(0, buf, len);
53 }
54 
55 /*
56  * Private function prototypes
57  */
58 
59 static int pmbr_part_valid(struct partition *part);
60 static int is_pmbr_valid(legacy_mbr * mbr);
61 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
62 				gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
63 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
64 				gpt_header * pgpt_head);
65 static int is_pte_valid(gpt_entry * pte);
66 
67 static char *print_efiname(gpt_entry *pte)
68 {
69 	static char name[PARTNAME_SZ + 1];
70 	int i;
71 	for (i = 0; i < PARTNAME_SZ; i++) {
72 		u8 c;
73 		c = pte->partition_name[i] & 0xff;
74 		c = (c && !isprint(c)) ? '.' : c;
75 		name[i] = c;
76 	}
77 	name[PARTNAME_SZ] = 0;
78 	return name;
79 }
80 
81 static void uuid_string(unsigned char *uuid, char *str)
82 {
83 	static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
84 				  12, 13, 14, 15};
85 	int i;
86 
87 	for (i = 0; i < 16; i++) {
88 		sprintf(str, "%02x", uuid[le[i]]);
89 		str += 2;
90 		switch (i) {
91 		case 3:
92 		case 5:
93 		case 7:
94 		case 9:
95 			*str++ = '-';
96 			break;
97 		}
98 	}
99 }
100 
101 static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
102 
103 static inline int is_bootable(gpt_entry *p)
104 {
105 	return p->attributes.fields.legacy_bios_bootable ||
106 		!memcmp(&(p->partition_type_guid), &system_guid,
107 			sizeof(efi_guid_t));
108 }
109 
110 #ifdef CONFIG_EFI_PARTITION
111 /*
112  * Public Functions (include/part.h)
113  */
114 
115 void print_part_efi(block_dev_desc_t * dev_desc)
116 {
117 	ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
118 	gpt_entry *gpt_pte = NULL;
119 	int i = 0;
120 	char uuid[37];
121 
122 	if (!dev_desc) {
123 		printf("%s: Invalid Argument(s)\n", __func__);
124 		return;
125 	}
126 	/* This function validates AND fills in the GPT header and PTE */
127 	if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
128 			 gpt_head, &gpt_pte) != 1) {
129 		printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
130 		return;
131 	}
132 
133 	debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
134 
135 	printf("Part\tStart LBA\tEnd LBA\t\tName\n");
136 	printf("\tAttributes\n");
137 	printf("\tType UUID\n");
138 	printf("\tPartition UUID\n");
139 
140 	for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
141 		/* Stop at the first non valid PTE */
142 		if (!is_pte_valid(&gpt_pte[i]))
143 			break;
144 
145 		printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
146 			le64_to_cpu(gpt_pte[i].starting_lba),
147 			le64_to_cpu(gpt_pte[i].ending_lba),
148 			print_efiname(&gpt_pte[i]));
149 		printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
150 		uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
151 		printf("\ttype:\t%s\n", uuid);
152 		uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
153 		printf("\tuuid:\t%s\n", uuid);
154 	}
155 
156 	/* Remember to free pte */
157 	free(gpt_pte);
158 	return;
159 }
160 
161 int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
162 				disk_partition_t * info)
163 {
164 	ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
165 	gpt_entry *gpt_pte = NULL;
166 
167 	/* "part" argument must be at least 1 */
168 	if (!dev_desc || !info || part < 1) {
169 		printf("%s: Invalid Argument(s)\n", __func__);
170 		return -1;
171 	}
172 
173 	/* This function validates AND fills in the GPT header and PTE */
174 	if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
175 			gpt_head, &gpt_pte) != 1) {
176 		printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
177 		return -1;
178 	}
179 
180 	if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
181 	    !is_pte_valid(&gpt_pte[part - 1])) {
182 		printf("%s: *** ERROR: Invalid partition number %d ***\n",
183 			__func__, part);
184 		return -1;
185 	}
186 
187 	/* The ulong casting limits the maximum disk size to 2 TB */
188 	info->start = (u64)le64_to_cpu(gpt_pte[part - 1].starting_lba);
189 	/* The ending LBA is inclusive, to calculate size, add 1 to it */
190 	info->size = ((u64)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1)
191 		     - info->start;
192 	info->blksz = GPT_BLOCK_SIZE;
193 
194 	sprintf((char *)info->name, "%s",
195 			print_efiname(&gpt_pte[part - 1]));
196 	sprintf((char *)info->type, "U-Boot");
197 	info->bootable = is_bootable(&gpt_pte[part - 1]);
198 #ifdef CONFIG_PARTITION_UUIDS
199 	uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
200 #endif
201 
202 	debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
203 		info->start, info->size, info->name);
204 
205 	/* Remember to free pte */
206 	free(gpt_pte);
207 	return 0;
208 }
209 
210 int test_part_efi(block_dev_desc_t * dev_desc)
211 {
212 	ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
213 
214 	/* Read legacy MBR from block 0 and validate it */
215 	if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
216 		|| (is_pmbr_valid(legacymbr) != 1)) {
217 		return -1;
218 	}
219 	return 0;
220 }
221 
222 /**
223  * set_protective_mbr(): Set the EFI protective MBR
224  * @param dev_desc - block device descriptor
225  *
226  * @return - zero on success, otherwise error
227  */
228 static int set_protective_mbr(block_dev_desc_t *dev_desc)
229 {
230 	legacy_mbr *p_mbr;
231 
232 	/* Setup the Protective MBR */
233 	p_mbr = calloc(1, sizeof(p_mbr));
234 	if (p_mbr == NULL) {
235 		printf("%s: calloc failed!\n", __func__);
236 		return -1;
237 	}
238 	/* Append signature */
239 	p_mbr->signature = MSDOS_MBR_SIGNATURE;
240 	p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
241 	p_mbr->partition_record[0].start_sect = 1;
242 	p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba;
243 
244 	/* Write MBR sector to the MMC device */
245 	if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
246 		printf("** Can't write to device %d **\n",
247 			dev_desc->dev);
248 		free(p_mbr);
249 		return -1;
250 	}
251 
252 	free(p_mbr);
253 	return 0;
254 }
255 
256 /**
257  * string_uuid(); Convert UUID stored as string to bytes
258  *
259  * @param uuid - UUID represented as string
260  * @param dst - GUID buffer
261  *
262  * @return return 0 on successful conversion
263  */
264 static int string_uuid(char *uuid, u8 *dst)
265 {
266 	efi_guid_t guid;
267 	u16 b, c, d;
268 	u64 e;
269 	u32 a;
270 	u8 *p;
271 	u8 i;
272 
273 	const u8 uuid_str_len = 36;
274 
275 	/* The UUID is written in text: */
276 	/* 1        9    14   19   24 */
277 	/* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */
278 
279 	debug("%s: uuid: %s\n", __func__, uuid);
280 
281 	if (strlen(uuid) != uuid_str_len)
282 		return -1;
283 
284 	for (i = 0; i < uuid_str_len; i++) {
285 		if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
286 			if (uuid[i] != '-')
287 				return -1;
288 		} else {
289 			if (!isxdigit(uuid[i]))
290 				return -1;
291 		}
292 	}
293 
294 	a = (u32)simple_strtoul(uuid, NULL, 16);
295 	b = (u16)simple_strtoul(uuid + 9, NULL, 16);
296 	c = (u16)simple_strtoul(uuid + 14, NULL, 16);
297 	d = (u16)simple_strtoul(uuid + 19, NULL, 16);
298 	e = (u64)simple_strtoull(uuid + 24, NULL, 16);
299 
300 	p = (u8 *) &e;
301 	guid = EFI_GUID(a, b, c, d >> 8, d & 0xFF,
302 			*(p + 5), *(p + 4), *(p + 3),
303 			*(p + 2), *(p + 1) , *p);
304 
305 	memcpy(dst, guid.b, sizeof(efi_guid_t));
306 
307 	return 0;
308 }
309 
310 int write_gpt_table(block_dev_desc_t *dev_desc,
311 		gpt_header *gpt_h, gpt_entry *gpt_e)
312 {
313 	const int pte_blk_num = (gpt_h->num_partition_entries
314 		* sizeof(gpt_entry)) / dev_desc->blksz;
315 
316 	u32 calc_crc32;
317 	u64 val;
318 
319 	debug("max lba: %x\n", (u32) dev_desc->lba);
320 	/* Setup the Protective MBR */
321 	if (set_protective_mbr(dev_desc) < 0)
322 		goto err;
323 
324 	/* Generate CRC for the Primary GPT Header */
325 	calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
326 			      le32_to_cpu(gpt_h->num_partition_entries) *
327 			      le32_to_cpu(gpt_h->sizeof_partition_entry));
328 	gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
329 
330 	calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
331 			      le32_to_cpu(gpt_h->header_size));
332 	gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
333 
334 	/* Write the First GPT to the block right after the Legacy MBR */
335 	if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
336 		goto err;
337 
338 	if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_num, gpt_e)
339 	    != pte_blk_num)
340 		goto err;
341 
342 	/* recalculate the values for the Second GPT Header */
343 	val = le64_to_cpu(gpt_h->my_lba);
344 	gpt_h->my_lba = gpt_h->alternate_lba;
345 	gpt_h->alternate_lba = cpu_to_le64(val);
346 	gpt_h->header_crc32 = 0;
347 
348 	calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
349 			      le32_to_cpu(gpt_h->header_size));
350 	gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
351 
352 	if (dev_desc->block_write(dev_desc->dev,
353 				  le32_to_cpu(gpt_h->last_usable_lba + 1),
354 				  pte_blk_num, gpt_e) != pte_blk_num)
355 		goto err;
356 
357 	if (dev_desc->block_write(dev_desc->dev,
358 				  le32_to_cpu(gpt_h->my_lba), 1, gpt_h) != 1)
359 		goto err;
360 
361 	debug("GPT successfully written to block device!\n");
362 	return 0;
363 
364  err:
365 	printf("** Can't write to device %d **\n", dev_desc->dev);
366 	return -1;
367 }
368 
369 int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
370 		disk_partition_t *partitions, int parts)
371 {
372 	u32 offset = (u32)le32_to_cpu(gpt_h->first_usable_lba);
373 	ulong start;
374 	int i, k;
375 	size_t name_len;
376 #ifdef CONFIG_PARTITION_UUIDS
377 	char *str_uuid;
378 #endif
379 
380 	for (i = 0; i < parts; i++) {
381 		/* partition starting lba */
382 		start = partitions[i].start;
383 		if (start && (start < offset)) {
384 			printf("Partition overlap\n");
385 			return -1;
386 		}
387 		if (start) {
388 			gpt_e[i].starting_lba = cpu_to_le64(start);
389 			offset = start + partitions[i].size;
390 		} else {
391 			gpt_e[i].starting_lba = cpu_to_le64(offset);
392 			offset += partitions[i].size;
393 		}
394 		if (offset >= gpt_h->last_usable_lba) {
395 			printf("Partitions layout exceds disk size\n");
396 			return -1;
397 		}
398 		/* partition ending lba */
399 		if ((i == parts - 1) && (partitions[i].size == 0))
400 			/* extend the last partition to maximuim */
401 			gpt_e[i].ending_lba = gpt_h->last_usable_lba;
402 		else
403 			gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
404 
405 		/* partition type GUID */
406 		memcpy(gpt_e[i].partition_type_guid.b,
407 			&PARTITION_BASIC_DATA_GUID, 16);
408 
409 #ifdef CONFIG_PARTITION_UUIDS
410 		str_uuid = partitions[i].uuid;
411 		if (string_uuid(str_uuid, gpt_e[i].unique_partition_guid.b)) {
412 			printf("Partition no. %d: invalid guid: %s\n",
413 				i, str_uuid);
414 			return -1;
415 		}
416 #endif
417 
418 		/* partition attributes */
419 		memset(&gpt_e[i].attributes, 0,
420 		       sizeof(gpt_entry_attributes));
421 
422 		/* partition name */
423 		name_len = sizeof(gpt_e[i].partition_name)
424 			/ sizeof(efi_char16_t);
425 		for (k = 0; k < name_len; k++)
426 			gpt_e[i].partition_name[k] =
427 				(efi_char16_t)(partitions[i].name[k]);
428 
429 		debug("%s: name: %s offset[%d]: 0x%x size[%d]: 0x%lx\n",
430 		      __func__, partitions[i].name, i,
431 		      offset, i, partitions[i].size);
432 	}
433 
434 	return 0;
435 }
436 
437 int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
438 		char *str_guid, int parts_count)
439 {
440 	gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
441 	gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
442 	gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
443 	gpt_h->my_lba = cpu_to_le64(1);
444 	gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
445 	gpt_h->first_usable_lba = cpu_to_le64(34);
446 	gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
447 	gpt_h->partition_entry_lba = cpu_to_le64(2);
448 	gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
449 	gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
450 	gpt_h->header_crc32 = 0;
451 	gpt_h->partition_entry_array_crc32 = 0;
452 
453 	if (string_uuid(str_guid, gpt_h->disk_guid.b))
454 		return -1;
455 
456 	return 0;
457 }
458 
459 int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
460 		disk_partition_t *partitions, int parts_count)
461 {
462 	int ret;
463 
464 	gpt_header *gpt_h = calloc(1, sizeof(gpt_header));
465 	if (gpt_h == NULL) {
466 		printf("%s: calloc failed!\n", __func__);
467 		return -1;
468 	}
469 
470 	gpt_entry *gpt_e = calloc(GPT_ENTRY_NUMBERS, sizeof(gpt_entry));
471 	if (gpt_e == NULL) {
472 		printf("%s: calloc failed!\n", __func__);
473 		free(gpt_h);
474 		return -1;
475 	}
476 
477 	/* Generate Primary GPT header (LBA1) */
478 	ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
479 	if (ret)
480 		goto err;
481 
482 	/* Generate partition entries */
483 	ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
484 	if (ret)
485 		goto err;
486 
487 	/* Write GPT partition table */
488 	ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
489 
490 err:
491 	free(gpt_e);
492 	free(gpt_h);
493 	return ret;
494 }
495 #endif
496 
497 /*
498  * Private functions
499  */
500 /*
501  * pmbr_part_valid(): Check for EFI partition signature
502  *
503  * Returns: 1 if EFI GPT partition type is found.
504  */
505 static int pmbr_part_valid(struct partition *part)
506 {
507 	if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
508 		le32_to_cpu(part->start_sect) == 1UL) {
509 		return 1;
510 	}
511 
512 	return 0;
513 }
514 
515 /*
516  * is_pmbr_valid(): test Protective MBR for validity
517  *
518  * Returns: 1 if PMBR is valid, 0 otherwise.
519  * Validity depends on two things:
520  *  1) MSDOS signature is in the last two bytes of the MBR
521  *  2) One partition of type 0xEE is found, checked by pmbr_part_valid()
522  */
523 static int is_pmbr_valid(legacy_mbr * mbr)
524 {
525 	int i = 0;
526 
527 	if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
528 		return 0;
529 
530 	for (i = 0; i < 4; i++) {
531 		if (pmbr_part_valid(&mbr->partition_record[i])) {
532 			return 1;
533 		}
534 	}
535 	return 0;
536 }
537 
538 /**
539  * is_gpt_valid() - tests one GPT header and PTEs for validity
540  *
541  * lba is the logical block address of the GPT header to test
542  * gpt is a GPT header ptr, filled on return.
543  * ptes is a PTEs ptr, filled on return.
544  *
545  * Description: returns 1 if valid,  0 on error.
546  * If valid, returns pointers to PTEs.
547  */
548 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
549 			gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
550 {
551 	u32 crc32_backup = 0;
552 	u32 calc_crc32;
553 	unsigned long long lastlba;
554 
555 	if (!dev_desc || !pgpt_head) {
556 		printf("%s: Invalid Argument(s)\n", __func__);
557 		return 0;
558 	}
559 
560 	/* Read GPT Header from device */
561 	if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
562 		printf("*** ERROR: Can't read GPT header ***\n");
563 		return 0;
564 	}
565 
566 	/* Check the GPT header signature */
567 	if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
568 		printf("GUID Partition Table Header signature is wrong:"
569 			"0x%llX != 0x%llX\n",
570 			le64_to_cpu(pgpt_head->signature),
571 			GPT_HEADER_SIGNATURE);
572 		return 0;
573 	}
574 
575 	/* Check the GUID Partition Table CRC */
576 	memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
577 	memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
578 
579 	calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
580 		le32_to_cpu(pgpt_head->header_size));
581 
582 	memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
583 
584 	if (calc_crc32 != le32_to_cpu(crc32_backup)) {
585 		printf("GUID Partition Table Header CRC is wrong:"
586 			"0x%x != 0x%x\n",
587 		       le32_to_cpu(crc32_backup), calc_crc32);
588 		return 0;
589 	}
590 
591 	/* Check that the my_lba entry points to the LBA that contains the GPT */
592 	if (le64_to_cpu(pgpt_head->my_lba) != lba) {
593 		printf("GPT: my_lba incorrect: %llX != %llX\n",
594 			le64_to_cpu(pgpt_head->my_lba),
595 			lba);
596 		return 0;
597 	}
598 
599 	/* Check the first_usable_lba and last_usable_lba are within the disk. */
600 	lastlba = (unsigned long long)dev_desc->lba;
601 	if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
602 		printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
603 			le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
604 		return 0;
605 	}
606 	if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
607 		printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
608 			(u64) le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
609 		return 0;
610 	}
611 
612 	debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
613 		le64_to_cpu(pgpt_head->first_usable_lba),
614 		le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
615 
616 	/* Read and allocate Partition Table Entries */
617 	*pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
618 	if (*pgpt_pte == NULL) {
619 		printf("GPT: Failed to allocate memory for PTE\n");
620 		return 0;
621 	}
622 
623 	/* Check the GUID Partition Table Entry Array CRC */
624 	calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
625 		le32_to_cpu(pgpt_head->num_partition_entries) *
626 		le32_to_cpu(pgpt_head->sizeof_partition_entry));
627 
628 	if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
629 		printf("GUID Partition Table Entry Array CRC is wrong:"
630 			"0x%x != 0x%x\n",
631 			le32_to_cpu(pgpt_head->partition_entry_array_crc32),
632 			calc_crc32);
633 
634 		free(*pgpt_pte);
635 		return 0;
636 	}
637 
638 	/* We're done, all's well */
639 	return 1;
640 }
641 
642 /**
643  * alloc_read_gpt_entries(): reads partition entries from disk
644  * @dev_desc
645  * @gpt - GPT header
646  *
647  * Description: Returns ptes on success,  NULL on error.
648  * Allocates space for PTEs based on information found in @gpt.
649  * Notes: remember to free pte when you're done!
650  */
651 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
652 					 gpt_header * pgpt_head)
653 {
654 	size_t count = 0;
655 	gpt_entry *pte = NULL;
656 
657 	if (!dev_desc || !pgpt_head) {
658 		printf("%s: Invalid Argument(s)\n", __func__);
659 		return NULL;
660 	}
661 
662 	count = le32_to_cpu(pgpt_head->num_partition_entries) *
663 		le32_to_cpu(pgpt_head->sizeof_partition_entry);
664 
665 	debug("%s: count = %u * %u = %zu\n", __func__,
666 	      (u32) le32_to_cpu(pgpt_head->num_partition_entries),
667 	      (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
668 
669 	/* Allocate memory for PTE, remember to FREE */
670 	if (count != 0) {
671 		pte = memalign(ARCH_DMA_MINALIGN, count);
672 	}
673 
674 	if (count == 0 || pte == NULL) {
675 		printf("%s: ERROR: Can't allocate 0x%zX "
676 		       "bytes for GPT Entries\n",
677 			__func__, count);
678 		return NULL;
679 	}
680 
681 	/* Read GPT Entries from device */
682 	if (dev_desc->block_read (dev_desc->dev,
683 		le64_to_cpu(pgpt_head->partition_entry_lba),
684 		(lbaint_t) (count / GPT_BLOCK_SIZE), pte)
685 		!= (count / GPT_BLOCK_SIZE)) {
686 
687 		printf("*** ERROR: Can't read GPT Entries ***\n");
688 		free(pte);
689 		return NULL;
690 	}
691 	return pte;
692 }
693 
694 /**
695  * is_pte_valid(): validates a single Partition Table Entry
696  * @gpt_entry - Pointer to a single Partition Table Entry
697  *
698  * Description: returns 1 if valid,  0 on error.
699  */
700 static int is_pte_valid(gpt_entry * pte)
701 {
702 	efi_guid_t unused_guid;
703 
704 	if (!pte) {
705 		printf("%s: Invalid Argument(s)\n", __func__);
706 		return 0;
707 	}
708 
709 	/* Only one validation for now:
710 	 * The GUID Partition Type != Unused Entry (ALL-ZERO)
711 	 */
712 	memset(unused_guid.b, 0, sizeof(unused_guid.b));
713 
714 	if (memcmp(pte->partition_type_guid.b, unused_guid.b,
715 		sizeof(unused_guid.b)) == 0) {
716 
717 		debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
718 		      (unsigned int)(uintptr_t)pte);
719 
720 		return 0;
721 	} else {
722 		return 1;
723 	}
724 }
725 #endif
726