blob: b6cda57ff592667230e1d7c09dd64e991146701e [file] [log] [blame]
richardretanubune6745592008-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-VILLARD03836942008-10-16 15:01:15 +020025 * Problems with CONFIG_SYS_64BIT_LBA:
richardretanubune6745592008-09-26 11:13:22 -040026 *
27 * struct disk_partition.start in include/part.h is sized as ulong.
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020028 * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t.
richardretanubune6745592008-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 Wen757ddfe2011-09-07 18:11:19 +000038#include <linux/ctype.h>
richardretanubune6745592008-09-26 11:13:22 -040039
Mike Frysinger0ea0aa82009-01-29 20:02:07 -050040#if defined(CONFIG_CMD_IDE) || \
unsik Kimf0b1bdd2009-02-25 11:31:24 +090041 defined(CONFIG_CMD_MG_DISK) || \
Mike Frysinger0ea0aa82009-01-29 20:02:07 -050042 defined(CONFIG_CMD_SATA) || \
43 defined(CONFIG_CMD_SCSI) || \
44 defined(CONFIG_CMD_USB) || \
45 defined(CONFIG_MMC) || \
46 defined(CONFIG_SYSTEMACE)
richardretanubune6745592008-09-26 11:13:22 -040047
48/* Convert char[2] in little endian format to the host format integer
49 */
50static 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 */
57static 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 */
64static 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 */
83static 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
92static int pmbr_part_valid(struct partition *part);
93static int is_pmbr_valid(legacy_mbr * mbr);
94
95static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
96 gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
97
98static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
99 gpt_header * pgpt_head);
100
101static int is_pte_valid(gpt_entry * pte);
102
Lei Wen757ddfe2011-09-07 18:11:19 +0000103static 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
richardretanubune6745592008-09-26 11:13:22 -0400117/*
118 * Public Functions (include/part.h)
119 */
120
121void print_part_efi(block_dev_desc_t * dev_desc)
122{
Anton staaf3b3df4e2011-10-12 13:56:04 +0000123 ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
Doug Anderson698a51d2011-10-19 09:47:31 +0000124 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400125 int i = 0;
126
127 if (!dev_desc) {
Doug Andersondc036672011-10-19 08:04:46 +0000128 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400129 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,
Stephen Warren30f39de2011-10-28 09:21:46 +0000133 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000134 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400135 return;
136 }
137
Doug Anderson698a51d2011-10-19 09:47:31 +0000138 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400139
Lei Wen757ddfe2011-09-07 18:11:19 +0000140 printf("Part\tName\t\t\tStart LBA\tEnd LBA\n");
Anton staaf3b3df4e2011-10-12 13:56:04 +0000141 for (i = 0; i < le32_to_int(gpt_head->num_partition_entries); i++) {
richardretanubune6745592008-09-26 11:13:22 -0400142
Doug Anderson698a51d2011-10-19 09:47:31 +0000143 if (is_pte_valid(&gpt_pte[i])) {
Lei Wen757ddfe2011-09-07 18:11:19 +0000144 printf("%3d\t%-18s\t0x%08llX\t0x%08llX\n", (i + 1),
Doug Anderson698a51d2011-10-19 09:47:31 +0000145 print_efiname(&gpt_pte[i]),
146 le64_to_int(gpt_pte[i].starting_lba),
147 le64_to_int(gpt_pte[i].ending_lba));
richardretanubune6745592008-09-26 11:13:22 -0400148 } else {
149 break; /* Stop at the first non valid PTE */
150 }
151 }
152
153 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000154 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400155 return;
156}
157
158int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
159 disk_partition_t * info)
160{
Anton staaf3b3df4e2011-10-12 13:56:04 +0000161 ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
Doug Anderson698a51d2011-10-19 09:47:31 +0000162 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400163
164 /* "part" argument must be at least 1 */
165 if (!dev_desc || !info || part < 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000166 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400167 return -1;
168 }
169
170 /* This function validates AND fills in the GPT header and PTE */
171 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren30f39de2011-10-28 09:21:46 +0000172 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000173 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400174 return -1;
175 }
176
177 /* The ulong casting limits the maximum disk size to 2 TB */
Doug Anderson698a51d2011-10-19 09:47:31 +0000178 info->start = (ulong) le64_to_int(gpt_pte[part - 1].starting_lba);
Richard Retanubun413dfc72009-01-26 08:45:14 -0500179 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Doug Anderson698a51d2011-10-19 09:47:31 +0000180 info->size = ((ulong)le64_to_int(gpt_pte[part - 1].ending_lba) + 1)
Richard Retanubun413dfc72009-01-26 08:45:14 -0500181 - info->start;
richardretanubune6745592008-09-26 11:13:22 -0400182 info->blksz = GPT_BLOCK_SIZE;
183
Lei Wen757ddfe2011-09-07 18:11:19 +0000184 sprintf((char *)info->name, "%s",
Doug Anderson698a51d2011-10-19 09:47:31 +0000185 print_efiname(&gpt_pte[part - 1]));
richardretanubune6745592008-09-26 11:13:22 -0400186 sprintf((char *)info->type, "U-Boot");
187
Doug Andersondc036672011-10-19 08:04:46 +0000188 debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
richardretanubune6745592008-09-26 11:13:22 -0400189 info->start, info->size, info->name);
190
191 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000192 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400193 return 0;
194}
195
196int test_part_efi(block_dev_desc_t * dev_desc)
197{
Anton staaf3b3df4e2011-10-12 13:56:04 +0000198 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
richardretanubune6745592008-09-26 11:13:22 -0400199
200 /* Read legacy MBR from block 0 and validate it */
Anton staaf3b3df4e2011-10-12 13:56:04 +0000201 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
202 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubune6745592008-09-26 11:13:22 -0400203 return -1;
204 }
205 return 0;
206}
207
208/*
209 * Private functions
210 */
211/*
212 * pmbr_part_valid(): Check for EFI partition signature
213 *
214 * Returns: 1 if EFI GPT partition type is found.
215 */
216static int pmbr_part_valid(struct partition *part)
217{
218 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
219 le32_to_int(part->start_sect) == 1UL) {
220 return 1;
221 }
222
223 return 0;
224}
225
226/*
227 * is_pmbr_valid(): test Protective MBR for validity
228 *
229 * Returns: 1 if PMBR is valid, 0 otherwise.
230 * Validity depends on two things:
231 * 1) MSDOS signature is in the last two bytes of the MBR
232 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
233 */
234static int is_pmbr_valid(legacy_mbr * mbr)
235{
236 int i = 0;
237
238 if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) {
239 return 0;
240 }
241
242 for (i = 0; i < 4; i++) {
243 if (pmbr_part_valid(&mbr->partition_record[i])) {
244 return 1;
245 }
246 }
247 return 0;
248}
249
250/**
251 * is_gpt_valid() - tests one GPT header and PTEs for validity
252 *
253 * lba is the logical block address of the GPT header to test
254 * gpt is a GPT header ptr, filled on return.
255 * ptes is a PTEs ptr, filled on return.
256 *
257 * Description: returns 1 if valid, 0 on error.
258 * If valid, returns pointers to PTEs.
259 */
260static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
261 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
262{
263 unsigned char crc32_backup[4] = { 0 };
264 unsigned long calc_crc32;
265 unsigned long long lastlba;
266
267 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000268 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400269 return 0;
270 }
271
272 /* Read GPT Header from device */
273 if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
274 printf("*** ERROR: Can't read GPT header ***\n");
275 return 0;
276 }
277
278 /* Check the GPT header signature */
279 if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
280 printf("GUID Partition Table Header signature is wrong:"
281 "0x%llX != 0x%llX\n",
282 (unsigned long long)le64_to_int(pgpt_head->signature),
283 (unsigned long long)GPT_HEADER_SIGNATURE);
284 return 0;
285 }
286
287 /* Check the GUID Partition Table CRC */
288 memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
289 memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
290
291 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
292 le32_to_int(pgpt_head->header_size));
293
294 memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
295
296 if (calc_crc32 != le32_to_int(crc32_backup)) {
297 printf("GUID Partition Table Header CRC is wrong:"
298 "0x%08lX != 0x%08lX\n",
299 le32_to_int(crc32_backup), calc_crc32);
300 return 0;
301 }
302
303 /* Check that the my_lba entry points to the LBA that contains the GPT */
304 if (le64_to_int(pgpt_head->my_lba) != lba) {
305 printf("GPT: my_lba incorrect: %llX != %llX\n",
306 (unsigned long long)le64_to_int(pgpt_head->my_lba),
307 (unsigned long long)lba);
308 return 0;
309 }
310
311 /* Check the first_usable_lba and last_usable_lba are within the disk. */
312 lastlba = (unsigned long long)dev_desc->lba;
313 if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
314 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
315 le64_to_int(pgpt_head->first_usable_lba), lastlba);
316 return 0;
317 }
318 if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
319 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
320 le64_to_int(pgpt_head->last_usable_lba), lastlba);
321 return 0;
322 }
323
324 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
325 le64_to_int(pgpt_head->first_usable_lba),
326 le64_to_int(pgpt_head->last_usable_lba), lastlba);
327
328 /* Read and allocate Partition Table Entries */
329 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
330 if (*pgpt_pte == NULL) {
331 printf("GPT: Failed to allocate memory for PTE\n");
332 return 0;
333 }
334
335 /* Check the GUID Partition Table Entry Array CRC */
336 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
337 le32_to_int(pgpt_head->num_partition_entries) *
338 le32_to_int(pgpt_head->sizeof_partition_entry));
339
340 if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
341 printf("GUID Partition Table Entry Array CRC is wrong:"
342 "0x%08lX != 0x%08lX\n",
343 le32_to_int(pgpt_head->partition_entry_array_crc32),
344 calc_crc32);
345
Doug Anderson698a51d2011-10-19 09:47:31 +0000346 free(*pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400347 return 0;
348 }
349
350 /* We're done, all's well */
351 return 1;
352}
353
354/**
355 * alloc_read_gpt_entries(): reads partition entries from disk
356 * @dev_desc
357 * @gpt - GPT header
358 *
359 * Description: Returns ptes on success, NULL on error.
360 * Allocates space for PTEs based on information found in @gpt.
361 * Notes: remember to free pte when you're done!
362 */
363static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
364 gpt_header * pgpt_head)
365{
366 size_t count = 0;
367 gpt_entry *pte = NULL;
368
369 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000370 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400371 return NULL;
372 }
373
374 count = le32_to_int(pgpt_head->num_partition_entries) *
375 le32_to_int(pgpt_head->sizeof_partition_entry);
376
Doug Andersondc036672011-10-19 08:04:46 +0000377 debug("%s: count = %lu * %lu = %u\n", __func__,
richardretanubune6745592008-09-26 11:13:22 -0400378 le32_to_int(pgpt_head->num_partition_entries),
379 le32_to_int(pgpt_head->sizeof_partition_entry), count);
380
381 /* Allocate memory for PTE, remember to FREE */
382 if (count != 0) {
Sanjeev Premif54988b2011-11-16 10:20:47 -0500383 pte = memalign(ARCH_DMA_MINALIGN, count);
richardretanubune6745592008-09-26 11:13:22 -0400384 }
385
386 if (count == 0 || pte == NULL) {
387 printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
Doug Andersondc036672011-10-19 08:04:46 +0000388 __func__, count);
richardretanubune6745592008-09-26 11:13:22 -0400389 return NULL;
390 }
391
392 /* Read GPT Entries from device */
393 if (dev_desc->block_read (dev_desc->dev,
394 (unsigned long)le64_to_int(pgpt_head->partition_entry_lba),
395 (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
396 != (count / GPT_BLOCK_SIZE)) {
397
398 printf("*** ERROR: Can't read GPT Entries ***\n");
399 free(pte);
400 return NULL;
401 }
402 return pte;
403}
404
405/**
406 * is_pte_valid(): validates a single Partition Table Entry
407 * @gpt_entry - Pointer to a single Partition Table Entry
408 *
409 * Description: returns 1 if valid, 0 on error.
410 */
411static int is_pte_valid(gpt_entry * pte)
412{
413 efi_guid_t unused_guid;
414
415 if (!pte) {
Doug Andersondc036672011-10-19 08:04:46 +0000416 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400417 return 0;
418 }
419
420 /* Only one validation for now:
421 * The GUID Partition Type != Unused Entry (ALL-ZERO)
422 */
423 memset(unused_guid.b, 0, sizeof(unused_guid.b));
424
425 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
426 sizeof(unused_guid.b)) == 0) {
427
Doug Andersondc036672011-10-19 08:04:46 +0000428 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
richardretanubune6745592008-09-26 11:13:22 -0400429 (unsigned int)pte);
430
431 return 0;
432 } else {
433 return 1;
434 }
435}
436#endif