2 * Copyright (C) 2008 RuggedCom, Inc.
3 * Richard Retanubun <RichardRetanubun@RuggedCom.com>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 * Problems with CONFIG_SYS_64BIT_LBA:
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.
31 * This limits the maximum size of addressable storage to < 2 Terra Bytes
38 #include <linux/ctype.h>
40 #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)
47 /* Convert char[2] in little endian format to the host format integer
49 static inline unsigned short le16_to_int(unsigned char *le16
)
51 return ((le16
[1] << 8) + le16
[0]);
54 /* Convert char[4] in little endian format to the host format integer
56 static inline unsigned long le32_to_int(unsigned char *le32
)
58 return ((le32
[3] << 24) + (le32
[2] << 16) + (le32
[1] << 8) + le32
[0]);
61 /* Convert char[8] in little endian format to the host format integer
63 static inline unsigned long long le64_to_int(unsigned char *le64
)
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]);
76 * efi_crc32() - EFI version of crc32 function
77 * @buf: buffer to calculate crc32 of
78 * @len - length of buf
80 * Description: Returns EFI-style CRC32 value for @buf
82 static inline unsigned long efi_crc32(const void *buf
, unsigned long len
)
84 return crc32(0, buf
, len
);
88 * Private function prototypes
91 static int pmbr_part_valid(struct partition
*part
);
92 static int is_pmbr_valid(legacy_mbr
* mbr
);
94 static int is_gpt_valid(block_dev_desc_t
* dev_desc
, unsigned long long lba
,
95 gpt_header
* pgpt_head
, gpt_entry
** pgpt_pte
);
97 static gpt_entry
*alloc_read_gpt_entries(block_dev_desc_t
* dev_desc
,
98 gpt_header
* pgpt_head
);
100 static int is_pte_valid(gpt_entry
* pte
);
102 static char *print_efiname(gpt_entry
*pte
)
104 static char name
[PARTNAME_SZ
+ 1];
106 for (i
= 0; i
< PARTNAME_SZ
; i
++) {
108 c
= pte
->partition_name
[i
] & 0xff;
109 c
= (c
&& !isprint(c
)) ? '.' : c
;
112 name
[PARTNAME_SZ
] = 0;
116 static void uuid_string(unsigned char *uuid
, char *str
)
118 static const u8 le
[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
122 for (i
= 0; i
< 16; i
++) {
123 sprintf(str
, "%02x", uuid
[le
[i
]]);
137 * Public Functions (include/part.h)
140 void print_part_efi(block_dev_desc_t
* dev_desc
)
142 ALLOC_CACHE_ALIGN_BUFFER(gpt_header
, gpt_head
, 1);
143 gpt_entry
*gpt_pte
= NULL
;
148 printf("%s: Invalid Argument(s)\n", __func__
);
151 /* This function validates AND fills in the GPT header and PTE */
152 if (is_gpt_valid(dev_desc
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
153 gpt_head
, &gpt_pte
) != 1) {
154 printf("%s: *** ERROR: Invalid GPT ***\n", __func__
);
158 debug("%s: gpt-entry at %p\n", __func__
, gpt_pte
);
160 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
161 printf("\tAttributes\n");
162 printf("\tType UUID\n");
163 printf("\tPartition UUID\n");
165 for (i
= 0; i
< le32_to_int(gpt_head
->num_partition_entries
); i
++) {
166 /* Stop at the first non valid PTE */
167 if (!is_pte_valid(&gpt_pte
[i
]))
170 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i
+ 1),
171 le64_to_int(gpt_pte
[i
].starting_lba
),
172 le64_to_int(gpt_pte
[i
].ending_lba
),
173 print_efiname(&gpt_pte
[i
]));
174 printf("\tattrs:\t0x%016llx\n", gpt_pte
[i
].attributes
.raw
);
175 uuid_string(gpt_pte
[i
].partition_type_guid
.b
, uuid
);
176 printf("\ttype:\t%s\n", uuid
);
177 uuid_string(gpt_pte
[i
].unique_partition_guid
.b
, uuid
);
178 printf("\tuuid:\t%s\n", uuid
);
181 /* Remember to free pte */
186 int get_partition_info_efi(block_dev_desc_t
* dev_desc
, int part
,
187 disk_partition_t
* info
)
189 ALLOC_CACHE_ALIGN_BUFFER(gpt_header
, gpt_head
, 1);
190 gpt_entry
*gpt_pte
= NULL
;
192 /* "part" argument must be at least 1 */
193 if (!dev_desc
|| !info
|| part
< 1) {
194 printf("%s: Invalid Argument(s)\n", __func__
);
198 /* This function validates AND fills in the GPT header and PTE */
199 if (is_gpt_valid(dev_desc
, GPT_PRIMARY_PARTITION_TABLE_LBA
,
200 gpt_head
, &gpt_pte
) != 1) {
201 printf("%s: *** ERROR: Invalid GPT ***\n", __func__
);
205 if (part
> le32_to_int(gpt_head
->num_partition_entries
) ||
206 !is_pte_valid(&gpt_pte
[part
- 1])) {
207 printf("%s: *** ERROR: Invalid partition number %d ***\n",
212 /* The ulong casting limits the maximum disk size to 2 TB */
213 info
->start
= (ulong
) le64_to_int(gpt_pte
[part
- 1].starting_lba
);
214 /* The ending LBA is inclusive, to calculate size, add 1 to it */
215 info
->size
= ((ulong
)le64_to_int(gpt_pte
[part
- 1].ending_lba
) + 1)
217 info
->blksz
= GPT_BLOCK_SIZE
;
219 sprintf((char *)info
->name
, "%s",
220 print_efiname(&gpt_pte
[part
- 1]));
221 sprintf((char *)info
->type
, "U-Boot");
222 #ifdef CONFIG_PARTITION_UUIDS
223 uuid_string(gpt_pte
[part
- 1].unique_partition_guid
.b
, info
->uuid
);
226 debug("%s: start 0x%lX, size 0x%lX, name %s", __func__
,
227 info
->start
, info
->size
, info
->name
);
229 /* Remember to free pte */
234 int test_part_efi(block_dev_desc_t
* dev_desc
)
236 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr
, legacymbr
, 1);
238 /* Read legacy MBR from block 0 and validate it */
239 if ((dev_desc
->block_read(dev_desc
->dev
, 0, 1, (ulong
*)legacymbr
) != 1)
240 || (is_pmbr_valid(legacymbr
) != 1)) {
250 * pmbr_part_valid(): Check for EFI partition signature
252 * Returns: 1 if EFI GPT partition type is found.
254 static int pmbr_part_valid(struct partition
*part
)
256 if (part
->sys_ind
== EFI_PMBR_OSTYPE_EFI_GPT
&&
257 le32_to_int(part
->start_sect
) == 1UL) {
265 * is_pmbr_valid(): test Protective MBR for validity
267 * Returns: 1 if PMBR is valid, 0 otherwise.
268 * Validity depends on two things:
269 * 1) MSDOS signature is in the last two bytes of the MBR
270 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
272 static int is_pmbr_valid(legacy_mbr
* mbr
)
276 if (!mbr
|| le16_to_int(mbr
->signature
) != MSDOS_MBR_SIGNATURE
) {
280 for (i
= 0; i
< 4; i
++) {
281 if (pmbr_part_valid(&mbr
->partition_record
[i
])) {
289 * is_gpt_valid() - tests one GPT header and PTEs for validity
291 * lba is the logical block address of the GPT header to test
292 * gpt is a GPT header ptr, filled on return.
293 * ptes is a PTEs ptr, filled on return.
295 * Description: returns 1 if valid, 0 on error.
296 * If valid, returns pointers to PTEs.
298 static int is_gpt_valid(block_dev_desc_t
* dev_desc
, unsigned long long lba
,
299 gpt_header
* pgpt_head
, gpt_entry
** pgpt_pte
)
301 unsigned char crc32_backup
[4] = { 0 };
302 unsigned long calc_crc32
;
303 unsigned long long lastlba
;
305 if (!dev_desc
|| !pgpt_head
) {
306 printf("%s: Invalid Argument(s)\n", __func__
);
310 /* Read GPT Header from device */
311 if (dev_desc
->block_read(dev_desc
->dev
, lba
, 1, pgpt_head
) != 1) {
312 printf("*** ERROR: Can't read GPT header ***\n");
316 /* Check the GPT header signature */
317 if (le64_to_int(pgpt_head
->signature
) != GPT_HEADER_SIGNATURE
) {
318 printf("GUID Partition Table Header signature is wrong:"
319 "0x%llX != 0x%llX\n",
320 (unsigned long long)le64_to_int(pgpt_head
->signature
),
321 (unsigned long long)GPT_HEADER_SIGNATURE
);
325 /* Check the GUID Partition Table CRC */
326 memcpy(crc32_backup
, pgpt_head
->header_crc32
, sizeof(crc32_backup
));
327 memset(pgpt_head
->header_crc32
, 0, sizeof(pgpt_head
->header_crc32
));
329 calc_crc32
= efi_crc32((const unsigned char *)pgpt_head
,
330 le32_to_int(pgpt_head
->header_size
));
332 memcpy(pgpt_head
->header_crc32
, crc32_backup
, sizeof(crc32_backup
));
334 if (calc_crc32
!= le32_to_int(crc32_backup
)) {
335 printf("GUID Partition Table Header CRC is wrong:"
336 "0x%08lX != 0x%08lX\n",
337 le32_to_int(crc32_backup
), calc_crc32
);
341 /* Check that the my_lba entry points to the LBA that contains the GPT */
342 if (le64_to_int(pgpt_head
->my_lba
) != lba
) {
343 printf("GPT: my_lba incorrect: %llX != %llX\n",
344 (unsigned long long)le64_to_int(pgpt_head
->my_lba
),
345 (unsigned long long)lba
);
349 /* Check the first_usable_lba and last_usable_lba are within the disk. */
350 lastlba
= (unsigned long long)dev_desc
->lba
;
351 if (le64_to_int(pgpt_head
->first_usable_lba
) > lastlba
) {
352 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
353 le64_to_int(pgpt_head
->first_usable_lba
), lastlba
);
356 if (le64_to_int(pgpt_head
->last_usable_lba
) > lastlba
) {
357 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
358 le64_to_int(pgpt_head
->last_usable_lba
), lastlba
);
362 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
363 le64_to_int(pgpt_head
->first_usable_lba
),
364 le64_to_int(pgpt_head
->last_usable_lba
), lastlba
);
366 /* Read and allocate Partition Table Entries */
367 *pgpt_pte
= alloc_read_gpt_entries(dev_desc
, pgpt_head
);
368 if (*pgpt_pte
== NULL
) {
369 printf("GPT: Failed to allocate memory for PTE\n");
373 /* Check the GUID Partition Table Entry Array CRC */
374 calc_crc32
= efi_crc32((const unsigned char *)*pgpt_pte
,
375 le32_to_int(pgpt_head
->num_partition_entries
) *
376 le32_to_int(pgpt_head
->sizeof_partition_entry
));
378 if (calc_crc32
!= le32_to_int(pgpt_head
->partition_entry_array_crc32
)) {
379 printf("GUID Partition Table Entry Array CRC is wrong:"
380 "0x%08lX != 0x%08lX\n",
381 le32_to_int(pgpt_head
->partition_entry_array_crc32
),
388 /* We're done, all's well */
393 * alloc_read_gpt_entries(): reads partition entries from disk
397 * Description: Returns ptes on success, NULL on error.
398 * Allocates space for PTEs based on information found in @gpt.
399 * Notes: remember to free pte when you're done!
401 static gpt_entry
*alloc_read_gpt_entries(block_dev_desc_t
* dev_desc
,
402 gpt_header
* pgpt_head
)
405 gpt_entry
*pte
= NULL
;
407 if (!dev_desc
|| !pgpt_head
) {
408 printf("%s: Invalid Argument(s)\n", __func__
);
412 count
= le32_to_int(pgpt_head
->num_partition_entries
) *
413 le32_to_int(pgpt_head
->sizeof_partition_entry
);
415 debug("%s: count = %lu * %lu = %u\n", __func__
,
416 le32_to_int(pgpt_head
->num_partition_entries
),
417 le32_to_int(pgpt_head
->sizeof_partition_entry
), count
);
419 /* Allocate memory for PTE, remember to FREE */
421 pte
= memalign(ARCH_DMA_MINALIGN
, count
);
424 if (count
== 0 || pte
== NULL
) {
425 printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
430 /* Read GPT Entries from device */
431 if (dev_desc
->block_read (dev_desc
->dev
,
432 (unsigned long)le64_to_int(pgpt_head
->partition_entry_lba
),
433 (lbaint_t
) (count
/ GPT_BLOCK_SIZE
), pte
)
434 != (count
/ GPT_BLOCK_SIZE
)) {
436 printf("*** ERROR: Can't read GPT Entries ***\n");
444 * is_pte_valid(): validates a single Partition Table Entry
445 * @gpt_entry - Pointer to a single Partition Table Entry
447 * Description: returns 1 if valid, 0 on error.
449 static int is_pte_valid(gpt_entry
* pte
)
451 efi_guid_t unused_guid
;
454 printf("%s: Invalid Argument(s)\n", __func__
);
458 /* Only one validation for now:
459 * The GUID Partition Type != Unused Entry (ALL-ZERO)
461 memset(unused_guid
.b
, 0, sizeof(unused_guid
.b
));
463 if (memcmp(pte
->partition_type_guid
.b
, unused_guid
.b
,
464 sizeof(unused_guid
.b
)) == 0) {
466 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__
,