xref: /openbmc/u-boot/disk/part_efi.c (revision 26ddff2d)
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 #if defined(CONFIG_CMD_IDE) || \
41     defined(CONFIG_CMD_MG_DISK) || \
42     defined(CONFIG_CMD_SATA) || \
43     defined(CONFIG_CMD_SCSI) || \
44     defined(CONFIG_CMD_USB) || \
45     defined(CONFIG_MMC) || \
46     defined(CONFIG_SYSTEMACE)
47 
48 /* Convert char[2] in little endian format to the host format integer
49  */
50 static inline unsigned short le16_to_int(unsigned char *le16)
51 {
52 	return ((le16[1] << 8) + le16[0]);
53 }
54 
55 /* Convert char[4] in little endian format to the host format integer
56  */
57 static inline unsigned long le32_to_int(unsigned char *le32)
58 {
59 	return ((le32[3] << 24) + (le32[2] << 16) + (le32[1] << 8) + le32[0]);
60 }
61 
62 /* Convert char[8] in little endian format to the host format integer
63  */
64 static inline unsigned long long le64_to_int(unsigned char *le64)
65 {
66 	return (((unsigned long long)le64[7] << 56) +
67 		((unsigned long long)le64[6] << 48) +
68 		((unsigned long long)le64[5] << 40) +
69 		((unsigned long long)le64[4] << 32) +
70 		((unsigned long long)le64[3] << 24) +
71 		((unsigned long long)le64[2] << 16) +
72 		((unsigned long long)le64[1] << 8) +
73 		(unsigned long long)le64[0]);
74 }
75 
76 /**
77  * efi_crc32() - EFI version of crc32 function
78  * @buf: buffer to calculate crc32 of
79  * @len - length of buf
80  *
81  * Description: Returns EFI-style CRC32 value for @buf
82  */
83 static inline unsigned long efi_crc32(const void *buf, unsigned long len)
84 {
85 	return crc32(0, buf, len);
86 }
87 
88 /*
89  * Private function prototypes
90  */
91 
92 static int pmbr_part_valid(struct partition *part);
93 static int is_pmbr_valid(legacy_mbr * mbr);
94 
95 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
96 				gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
97 
98 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
99 				gpt_header * pgpt_head);
100 
101 static int is_pte_valid(gpt_entry * pte);
102 
103 static char *print_efiname(gpt_entry *pte)
104 {
105 	static char name[PARTNAME_SZ + 1];
106 	int i;
107 	for (i = 0; i < PARTNAME_SZ; i++) {
108 		u8 c;
109 		c = pte->partition_name[i] & 0xff;
110 		c = (c && !isprint(c)) ? '.' : c;
111 		name[i] = c;
112 	}
113 	name[PARTNAME_SZ] = 0;
114 	return name;
115 }
116 
117 /*
118  * Public Functions (include/part.h)
119  */
120 
121 void print_part_efi(block_dev_desc_t * dev_desc)
122 {
123 	gpt_header gpt_head;
124 	gpt_entry **pgpt_pte = NULL;
125 	int i = 0;
126 
127 	if (!dev_desc) {
128 		printf("%s: Invalid Argument(s)\n", __FUNCTION__);
129 		return;
130 	}
131 	/* This function validates AND fills in the GPT header and PTE */
132 	if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
133 			 &(gpt_head), pgpt_pte) != 1) {
134 		printf("%s: *** ERROR: Invalid GPT ***\n", __FUNCTION__);
135 		return;
136 	}
137 
138 	debug("%s: gpt-entry at 0x%08X\n", __FUNCTION__, (unsigned int)*pgpt_pte);
139 
140 	printf("Part\tName\t\t\tStart LBA\tEnd LBA\n");
141 	for (i = 0; i < le32_to_int(gpt_head.num_partition_entries); i++) {
142 
143 		if (is_pte_valid(&(*pgpt_pte)[i])) {
144 			printf("%3d\t%-18s\t0x%08llX\t0x%08llX\n", (i + 1),
145 				print_efiname(&(*pgpt_pte)[i]),
146 				le64_to_int((*pgpt_pte)[i].starting_lba),
147 				le64_to_int((*pgpt_pte)[i].ending_lba));
148 		} else {
149 			break;	/* Stop at the first non valid PTE */
150 		}
151 	}
152 
153 	/* Remember to free pte */
154 	if (*pgpt_pte != NULL) {
155 		debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
156 		free(*pgpt_pte);
157 	}
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 	gpt_header gpt_head;
165 	gpt_entry **pgpt_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", __FUNCTION__);
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), pgpt_pte) != 1) {
176 		printf("%s: *** ERROR: Invalid GPT ***\n", __FUNCTION__);
177 		return -1;
178 	}
179 
180 	/* The ulong casting limits the maximum disk size to 2 TB */
181 	info->start = (ulong) le64_to_int((*pgpt_pte)[part - 1].starting_lba);
182 	/* The ending LBA is inclusive, to calculate size, add 1 to it */
183 	info->size = ((ulong)le64_to_int((*pgpt_pte)[part - 1].ending_lba) + 1)
184 		     - info->start;
185 	info->blksz = GPT_BLOCK_SIZE;
186 
187 	sprintf((char *)info->name, "%s",
188 			print_efiname(&(*pgpt_pte)[part - 1]));
189 	sprintf((char *)info->type, "U-Boot");
190 
191 	debug("%s: start 0x%lX, size 0x%lX, name %s", __FUNCTION__,
192 		info->start, info->size, info->name);
193 
194 	/* Remember to free pte */
195 	if (*pgpt_pte != NULL) {
196 		debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
197 		free(*pgpt_pte);
198 	}
199 	return 0;
200 }
201 
202 int test_part_efi(block_dev_desc_t * dev_desc)
203 {
204 	legacy_mbr legacymbr;
205 
206 	/* Read legacy MBR from block 0 and validate it */
207 	if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) & legacymbr) != 1)
208 		|| (is_pmbr_valid(&legacymbr) != 1)) {
209 		return -1;
210 	}
211 	return 0;
212 }
213 
214 /*
215  * Private functions
216  */
217 /*
218  * pmbr_part_valid(): Check for EFI partition signature
219  *
220  * Returns: 1 if EFI GPT partition type is found.
221  */
222 static int pmbr_part_valid(struct partition *part)
223 {
224 	if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
225 		le32_to_int(part->start_sect) == 1UL) {
226 		return 1;
227 	}
228 
229 	return 0;
230 }
231 
232 /*
233  * is_pmbr_valid(): test Protective MBR for validity
234  *
235  * Returns: 1 if PMBR is valid, 0 otherwise.
236  * Validity depends on two things:
237  *  1) MSDOS signature is in the last two bytes of the MBR
238  *  2) One partition of type 0xEE is found, checked by pmbr_part_valid()
239  */
240 static int is_pmbr_valid(legacy_mbr * mbr)
241 {
242 	int i = 0;
243 
244 	if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) {
245 		return 0;
246 	}
247 
248 	for (i = 0; i < 4; i++) {
249 		if (pmbr_part_valid(&mbr->partition_record[i])) {
250 			return 1;
251 		}
252 	}
253 	return 0;
254 }
255 
256 /**
257  * is_gpt_valid() - tests one GPT header and PTEs for validity
258  *
259  * lba is the logical block address of the GPT header to test
260  * gpt is a GPT header ptr, filled on return.
261  * ptes is a PTEs ptr, filled on return.
262  *
263  * Description: returns 1 if valid,  0 on error.
264  * If valid, returns pointers to PTEs.
265  */
266 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
267 			gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
268 {
269 	unsigned char crc32_backup[4] = { 0 };
270 	unsigned long calc_crc32;
271 	unsigned long long lastlba;
272 
273 	if (!dev_desc || !pgpt_head) {
274 		printf("%s: Invalid Argument(s)\n", __FUNCTION__);
275 		return 0;
276 	}
277 
278 	/* Read GPT Header from device */
279 	if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
280 		printf("*** ERROR: Can't read GPT header ***\n");
281 		return 0;
282 	}
283 
284 	/* Check the GPT header signature */
285 	if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
286 		printf("GUID Partition Table Header signature is wrong:"
287 			"0x%llX != 0x%llX\n",
288 			(unsigned long long)le64_to_int(pgpt_head->signature),
289 			(unsigned long long)GPT_HEADER_SIGNATURE);
290 		return 0;
291 	}
292 
293 	/* Check the GUID Partition Table CRC */
294 	memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
295 	memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
296 
297 	calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
298 		le32_to_int(pgpt_head->header_size));
299 
300 	memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
301 
302 	if (calc_crc32 != le32_to_int(crc32_backup)) {
303 		printf("GUID Partition Table Header CRC is wrong:"
304 			"0x%08lX != 0x%08lX\n",
305 			le32_to_int(crc32_backup), calc_crc32);
306 		return 0;
307 	}
308 
309 	/* Check that the my_lba entry points to the LBA that contains the GPT */
310 	if (le64_to_int(pgpt_head->my_lba) != lba) {
311 		printf("GPT: my_lba incorrect: %llX != %llX\n",
312 			(unsigned long long)le64_to_int(pgpt_head->my_lba),
313 			(unsigned long long)lba);
314 		return 0;
315 	}
316 
317 	/* Check the first_usable_lba and last_usable_lba are within the disk. */
318 	lastlba = (unsigned long long)dev_desc->lba;
319 	if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
320 		printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
321 			le64_to_int(pgpt_head->first_usable_lba), lastlba);
322 		return 0;
323 	}
324 	if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
325 		printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
326 			le64_to_int(pgpt_head->last_usable_lba), lastlba);
327 		return 0;
328 	}
329 
330 	debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
331 		le64_to_int(pgpt_head->first_usable_lba),
332 		le64_to_int(pgpt_head->last_usable_lba), lastlba);
333 
334 	/* Read and allocate Partition Table Entries */
335 	*pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
336 	if (*pgpt_pte == NULL) {
337 		printf("GPT: Failed to allocate memory for PTE\n");
338 		return 0;
339 	}
340 
341 	/* Check the GUID Partition Table Entry Array CRC */
342 	calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
343 		le32_to_int(pgpt_head->num_partition_entries) *
344 		le32_to_int(pgpt_head->sizeof_partition_entry));
345 
346 	if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
347 		printf("GUID Partition Table Entry Array CRC is wrong:"
348 			"0x%08lX != 0x%08lX\n",
349 			le32_to_int(pgpt_head->partition_entry_array_crc32),
350 			calc_crc32);
351 
352 		if (*pgpt_pte != NULL) {
353 			free(*pgpt_pte);
354 		}
355 		return 0;
356 	}
357 
358 	/* We're done, all's well */
359 	return 1;
360 }
361 
362 /**
363  * alloc_read_gpt_entries(): reads partition entries from disk
364  * @dev_desc
365  * @gpt - GPT header
366  *
367  * Description: Returns ptes on success,  NULL on error.
368  * Allocates space for PTEs based on information found in @gpt.
369  * Notes: remember to free pte when you're done!
370  */
371 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
372 					 gpt_header * pgpt_head)
373 {
374 	size_t count = 0;
375 	gpt_entry *pte = NULL;
376 
377 	if (!dev_desc || !pgpt_head) {
378 		printf("%s: Invalid Argument(s)\n", __FUNCTION__);
379 		return NULL;
380 	}
381 
382 	count = le32_to_int(pgpt_head->num_partition_entries) *
383 		le32_to_int(pgpt_head->sizeof_partition_entry);
384 
385 	debug("%s: count = %lu * %lu = %u\n", __FUNCTION__,
386 		le32_to_int(pgpt_head->num_partition_entries),
387 		le32_to_int(pgpt_head->sizeof_partition_entry), count);
388 
389 	/* Allocate memory for PTE, remember to FREE */
390 	if (count != 0) {
391 		pte = malloc(count);
392 	}
393 
394 	if (count == 0 || pte == NULL) {
395 		printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
396 			__FUNCTION__, count);
397 		return NULL;
398 	}
399 
400 	/* Read GPT Entries from device */
401 	if (dev_desc->block_read (dev_desc->dev,
402 		(unsigned long)le64_to_int(pgpt_head->partition_entry_lba),
403 		(lbaint_t) (count / GPT_BLOCK_SIZE), pte)
404 		!= (count / GPT_BLOCK_SIZE)) {
405 
406 		printf("*** ERROR: Can't read GPT Entries ***\n");
407 		free(pte);
408 		return NULL;
409 	}
410 	return pte;
411 }
412 
413 /**
414  * is_pte_valid(): validates a single Partition Table Entry
415  * @gpt_entry - Pointer to a single Partition Table Entry
416  *
417  * Description: returns 1 if valid,  0 on error.
418  */
419 static int is_pte_valid(gpt_entry * pte)
420 {
421 	efi_guid_t unused_guid;
422 
423 	if (!pte) {
424 		printf("%s: Invalid Argument(s)\n", __FUNCTION__);
425 		return 0;
426 	}
427 
428 	/* Only one validation for now:
429 	 * The GUID Partition Type != Unused Entry (ALL-ZERO)
430 	 */
431 	memset(unused_guid.b, 0, sizeof(unused_guid.b));
432 
433 	if (memcmp(pte->partition_type_guid.b, unused_guid.b,
434 		sizeof(unused_guid.b)) == 0) {
435 
436 		debug("%s: Found an unused PTE GUID at 0x%08X\n", __FUNCTION__,
437 		(unsigned int)pte);
438 
439 		return 0;
440 	} else {
441 		return 1;
442 	}
443 }
444 #endif
445