blob: 02927a0d9da5a84e47817947b693eb7ff8e2c58a [file] [log] [blame]
richardretanubun07f3d782008-09-26 11:13:22 -04001/*
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/*
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020025 * Problems with CONFIG_SYS_64BIT_LBA:
richardretanubun07f3d782008-09-26 11:13:22 -040026 *
27 * struct disk_partition.start in include/part.h is sized as ulong.
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020028 * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t.
richardretanubun07f3d782008-09-26 11:13:22 -040029 * 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"
Lei Wen6eecc032011-09-07 18:11:19 +000038#include <linux/ctype.h>
richardretanubun07f3d782008-09-26 11:13:22 -040039
Mike Frysinger7bd27222009-01-29 20:02:07 -050040#if defined(CONFIG_CMD_IDE) || \
41 defined(CONFIG_CMD_SATA) || \
42 defined(CONFIG_CMD_SCSI) || \
43 defined(CONFIG_CMD_USB) || \
44 defined(CONFIG_MMC) || \
45 defined(CONFIG_SYSTEMACE)
richardretanubun07f3d782008-09-26 11:13:22 -040046
47/* Convert char[2] in little endian format to the host format integer
48 */
49static inline unsigned short le16_to_int(unsigned char *le16)
50{
51 return ((le16[1] << 8) + le16[0]);
52}
53
54/* Convert char[4] in little endian format to the host format integer
55 */
56static inline unsigned long le32_to_int(unsigned char *le32)
57{
58 return ((le32[3] << 24) + (le32[2] << 16) + (le32[1] << 8) + le32[0]);
59}
60
61/* Convert char[8] in little endian format to the host format integer
62 */
63static inline unsigned long long le64_to_int(unsigned char *le64)
64{
65 return (((unsigned long long)le64[7] << 56) +
66 ((unsigned long long)le64[6] << 48) +
67 ((unsigned long long)le64[5] << 40) +
68 ((unsigned long long)le64[4] << 32) +
69 ((unsigned long long)le64[3] << 24) +
70 ((unsigned long long)le64[2] << 16) +
71 ((unsigned long long)le64[1] << 8) +
72 (unsigned long long)le64[0]);
73}
74
75/**
76 * efi_crc32() - EFI version of crc32 function
77 * @buf: buffer to calculate crc32 of
78 * @len - length of buf
79 *
80 * Description: Returns EFI-style CRC32 value for @buf
81 */
82static inline unsigned long efi_crc32(const void *buf, unsigned long len)
83{
84 return crc32(0, buf, len);
85}
86
87/*
88 * Private function prototypes
89 */
90
91static int pmbr_part_valid(struct partition *part);
92static int is_pmbr_valid(legacy_mbr * mbr);
93
94static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
95 gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
96
97static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
98 gpt_header * pgpt_head);
99
100static int is_pte_valid(gpt_entry * pte);
101
Lei Wen6eecc032011-09-07 18:11:19 +0000102static char *print_efiname(gpt_entry *pte)
103{
104 static char name[PARTNAME_SZ + 1];
105 int i;
106 for (i = 0; i < PARTNAME_SZ; i++) {
107 u8 c;
108 c = pte->partition_name[i] & 0xff;
109 c = (c && !isprint(c)) ? '.' : c;
110 name[i] = c;
111 }
112 name[PARTNAME_SZ] = 0;
113 return name;
114}
115
richardretanubun07f3d782008-09-26 11:13:22 -0400116/*
117 * Public Functions (include/part.h)
118 */
119
120void print_part_efi(block_dev_desc_t * dev_desc)
121{
Anton staaff75dd582011-10-12 13:56:04 +0000122 ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
Doug Andersondeb5ca82011-10-19 09:47:31 +0000123 gpt_entry *gpt_pte = NULL;
richardretanubun07f3d782008-09-26 11:13:22 -0400124 int i = 0;
125
126 if (!dev_desc) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000127 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400128 return;
129 }
130 /* This function validates AND fills in the GPT header and PTE */
131 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren4715a812011-10-28 09:21:46 +0000132 gpt_head, &gpt_pte) != 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000133 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400134 return;
135 }
136
Doug Andersondeb5ca82011-10-19 09:47:31 +0000137 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400138
Lei Wen6eecc032011-09-07 18:11:19 +0000139 printf("Part\tName\t\t\tStart LBA\tEnd LBA\n");
Anton staaff75dd582011-10-12 13:56:04 +0000140 for (i = 0; i < le32_to_int(gpt_head->num_partition_entries); i++) {
richardretanubun07f3d782008-09-26 11:13:22 -0400141
Doug Andersondeb5ca82011-10-19 09:47:31 +0000142 if (is_pte_valid(&gpt_pte[i])) {
Lei Wen6eecc032011-09-07 18:11:19 +0000143 printf("%3d\t%-18s\t0x%08llX\t0x%08llX\n", (i + 1),
Doug Andersondeb5ca82011-10-19 09:47:31 +0000144 print_efiname(&gpt_pte[i]),
145 le64_to_int(gpt_pte[i].starting_lba),
146 le64_to_int(gpt_pte[i].ending_lba));
richardretanubun07f3d782008-09-26 11:13:22 -0400147 } else {
148 break; /* Stop at the first non valid PTE */
149 }
150 }
151
152 /* Remember to free pte */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000153 free(gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400154 return;
155}
156
157int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
158 disk_partition_t * info)
159{
Anton staaff75dd582011-10-12 13:56:04 +0000160 ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
Doug Andersondeb5ca82011-10-19 09:47:31 +0000161 gpt_entry *gpt_pte = NULL;
richardretanubun07f3d782008-09-26 11:13:22 -0400162
163 /* "part" argument must be at least 1 */
164 if (!dev_desc || !info || part < 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000165 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400166 return -1;
167 }
168
169 /* This function validates AND fills in the GPT header and PTE */
170 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren4715a812011-10-28 09:21:46 +0000171 gpt_head, &gpt_pte) != 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000172 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400173 return -1;
174 }
175
176 /* The ulong casting limits the maximum disk size to 2 TB */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000177 info->start = (ulong) le64_to_int(gpt_pte[part - 1].starting_lba);
Richard Retanubun50970832009-01-26 08:45:14 -0500178 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000179 info->size = ((ulong)le64_to_int(gpt_pte[part - 1].ending_lba) + 1)
Richard Retanubun50970832009-01-26 08:45:14 -0500180 - info->start;
richardretanubun07f3d782008-09-26 11:13:22 -0400181 info->blksz = GPT_BLOCK_SIZE;
182
Lei Wen6eecc032011-09-07 18:11:19 +0000183 sprintf((char *)info->name, "%s",
Doug Andersondeb5ca82011-10-19 09:47:31 +0000184 print_efiname(&gpt_pte[part - 1]));
richardretanubun07f3d782008-09-26 11:13:22 -0400185 sprintf((char *)info->type, "U-Boot");
186
Doug Andersondf70b1c2011-10-19 08:04:46 +0000187 debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
richardretanubun07f3d782008-09-26 11:13:22 -0400188 info->start, info->size, info->name);
189
190 /* Remember to free pte */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000191 free(gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400192 return 0;
193}
194
195int test_part_efi(block_dev_desc_t * dev_desc)
196{
Anton staaff75dd582011-10-12 13:56:04 +0000197 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
richardretanubun07f3d782008-09-26 11:13:22 -0400198
199 /* Read legacy MBR from block 0 and validate it */
Anton staaff75dd582011-10-12 13:56:04 +0000200 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
201 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400202 return -1;
203 }
204 return 0;
205}
206
207/*
208 * Private functions
209 */
210/*
211 * pmbr_part_valid(): Check for EFI partition signature
212 *
213 * Returns: 1 if EFI GPT partition type is found.
214 */
215static int pmbr_part_valid(struct partition *part)
216{
217 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
218 le32_to_int(part->start_sect) == 1UL) {
219 return 1;
220 }
221
222 return 0;
223}
224
225/*
226 * is_pmbr_valid(): test Protective MBR for validity
227 *
228 * Returns: 1 if PMBR is valid, 0 otherwise.
229 * Validity depends on two things:
230 * 1) MSDOS signature is in the last two bytes of the MBR
231 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
232 */
233static int is_pmbr_valid(legacy_mbr * mbr)
234{
235 int i = 0;
236
237 if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) {
238 return 0;
239 }
240
241 for (i = 0; i < 4; i++) {
242 if (pmbr_part_valid(&mbr->partition_record[i])) {
243 return 1;
244 }
245 }
246 return 0;
247}
248
249/**
250 * is_gpt_valid() - tests one GPT header and PTEs for validity
251 *
252 * lba is the logical block address of the GPT header to test
253 * gpt is a GPT header ptr, filled on return.
254 * ptes is a PTEs ptr, filled on return.
255 *
256 * Description: returns 1 if valid, 0 on error.
257 * If valid, returns pointers to PTEs.
258 */
259static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
260 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
261{
262 unsigned char crc32_backup[4] = { 0 };
263 unsigned long calc_crc32;
264 unsigned long long lastlba;
265
266 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000267 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400268 return 0;
269 }
270
271 /* Read GPT Header from device */
272 if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
273 printf("*** ERROR: Can't read GPT header ***\n");
274 return 0;
275 }
276
277 /* Check the GPT header signature */
278 if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
279 printf("GUID Partition Table Header signature is wrong:"
280 "0x%llX != 0x%llX\n",
281 (unsigned long long)le64_to_int(pgpt_head->signature),
282 (unsigned long long)GPT_HEADER_SIGNATURE);
283 return 0;
284 }
285
286 /* Check the GUID Partition Table CRC */
287 memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
288 memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
289
290 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
291 le32_to_int(pgpt_head->header_size));
292
293 memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
294
295 if (calc_crc32 != le32_to_int(crc32_backup)) {
296 printf("GUID Partition Table Header CRC is wrong:"
297 "0x%08lX != 0x%08lX\n",
298 le32_to_int(crc32_backup), calc_crc32);
299 return 0;
300 }
301
302 /* Check that the my_lba entry points to the LBA that contains the GPT */
303 if (le64_to_int(pgpt_head->my_lba) != lba) {
304 printf("GPT: my_lba incorrect: %llX != %llX\n",
305 (unsigned long long)le64_to_int(pgpt_head->my_lba),
306 (unsigned long long)lba);
307 return 0;
308 }
309
310 /* Check the first_usable_lba and last_usable_lba are within the disk. */
311 lastlba = (unsigned long long)dev_desc->lba;
312 if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
313 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
314 le64_to_int(pgpt_head->first_usable_lba), lastlba);
315 return 0;
316 }
317 if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
318 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
319 le64_to_int(pgpt_head->last_usable_lba), lastlba);
320 return 0;
321 }
322
323 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
324 le64_to_int(pgpt_head->first_usable_lba),
325 le64_to_int(pgpt_head->last_usable_lba), lastlba);
326
327 /* Read and allocate Partition Table Entries */
328 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
329 if (*pgpt_pte == NULL) {
330 printf("GPT: Failed to allocate memory for PTE\n");
331 return 0;
332 }
333
334 /* Check the GUID Partition Table Entry Array CRC */
335 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
336 le32_to_int(pgpt_head->num_partition_entries) *
337 le32_to_int(pgpt_head->sizeof_partition_entry));
338
339 if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
340 printf("GUID Partition Table Entry Array CRC is wrong:"
341 "0x%08lX != 0x%08lX\n",
342 le32_to_int(pgpt_head->partition_entry_array_crc32),
343 calc_crc32);
344
Doug Andersondeb5ca82011-10-19 09:47:31 +0000345 free(*pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400346 return 0;
347 }
348
349 /* We're done, all's well */
350 return 1;
351}
352
353/**
354 * alloc_read_gpt_entries(): reads partition entries from disk
355 * @dev_desc
356 * @gpt - GPT header
357 *
358 * Description: Returns ptes on success, NULL on error.
359 * Allocates space for PTEs based on information found in @gpt.
360 * Notes: remember to free pte when you're done!
361 */
362static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
363 gpt_header * pgpt_head)
364{
365 size_t count = 0;
366 gpt_entry *pte = NULL;
367
368 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000369 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400370 return NULL;
371 }
372
373 count = le32_to_int(pgpt_head->num_partition_entries) *
374 le32_to_int(pgpt_head->sizeof_partition_entry);
375
Doug Andersondf70b1c2011-10-19 08:04:46 +0000376 debug("%s: count = %lu * %lu = %u\n", __func__,
richardretanubun07f3d782008-09-26 11:13:22 -0400377 le32_to_int(pgpt_head->num_partition_entries),
378 le32_to_int(pgpt_head->sizeof_partition_entry), count);
379
380 /* Allocate memory for PTE, remember to FREE */
381 if (count != 0) {
Sanjeev Premi29b70422011-11-16 10:20:47 -0500382 pte = memalign(ARCH_DMA_MINALIGN, count);
richardretanubun07f3d782008-09-26 11:13:22 -0400383 }
384
385 if (count == 0 || pte == NULL) {
386 printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
Doug Andersondf70b1c2011-10-19 08:04:46 +0000387 __func__, count);
richardretanubun07f3d782008-09-26 11:13:22 -0400388 return NULL;
389 }
390
391 /* Read GPT Entries from device */
392 if (dev_desc->block_read (dev_desc->dev,
393 (unsigned long)le64_to_int(pgpt_head->partition_entry_lba),
394 (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
395 != (count / GPT_BLOCK_SIZE)) {
396
397 printf("*** ERROR: Can't read GPT Entries ***\n");
398 free(pte);
399 return NULL;
400 }
401 return pte;
402}
403
404/**
405 * is_pte_valid(): validates a single Partition Table Entry
406 * @gpt_entry - Pointer to a single Partition Table Entry
407 *
408 * Description: returns 1 if valid, 0 on error.
409 */
410static int is_pte_valid(gpt_entry * pte)
411{
412 efi_guid_t unused_guid;
413
414 if (!pte) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000415 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400416 return 0;
417 }
418
419 /* Only one validation for now:
420 * The GUID Partition Type != Unused Entry (ALL-ZERO)
421 */
422 memset(unused_guid.b, 0, sizeof(unused_guid.b));
423
424 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
425 sizeof(unused_guid.b)) == 0) {
426
Doug Andersondf70b1c2011-10-19 08:04:46 +0000427 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
richardretanubun07f3d782008-09-26 11:13:22 -0400428 (unsigned int)pte);
429
430 return 0;
431 } else {
432 return 1;
433 }
434}
435#endif