]> git.ipfire.org Git - people/ms/u-boot.git/blame - disk/part_efi.c
disk: Make the disk partition code work with no specific partition types
[people/ms/u-boot.git] / disk / part_efi.c
CommitLineData
07f3d789 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/*
6d0f6bcf 25 * Problems with CONFIG_SYS_64BIT_LBA:
07f3d789 26 *
27 * struct disk_partition.start in include/part.h is sized as ulong.
6d0f6bcf 28 * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t.
07f3d789 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"
6eecc030 38#include <linux/ctype.h>
07f3d789 39
7bd2722e
MF
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)
07f3d789 46
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
6eecc030
LW
102static 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
f07cd2c4
SW
116static void uuid_string(unsigned char *uuid, char *str)
117{
118 static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
119 12, 13, 14, 15};
120 int i;
121
122 for (i = 0; i < 16; i++) {
123 sprintf(str, "%02x", uuid[le[i]]);
124 str += 2;
125 switch (i) {
126 case 3:
127 case 5:
128 case 7:
129 case 9:
130 *str++ = '-';
131 break;
132 }
133 }
134}
135
b4414f4a
SW
136static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
137
138static inline int is_bootable(gpt_entry *p)
139{
140 return p->attributes.fields.legacy_bios_bootable ||
141 !memcmp(&(p->partition_type_guid), &system_guid,
142 sizeof(efi_guid_t));
143}
144
07f3d789 145/*
146 * Public Functions (include/part.h)
147 */
148
149void print_part_efi(block_dev_desc_t * dev_desc)
150{
f75dd584 151 ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
deb5ca80 152 gpt_entry *gpt_pte = NULL;
07f3d789 153 int i = 0;
f07cd2c4 154 char uuid[37];
07f3d789 155
156 if (!dev_desc) {
df70b1c2 157 printf("%s: Invalid Argument(s)\n", __func__);
07f3d789 158 return;
159 }
160 /* This function validates AND fills in the GPT header and PTE */
161 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
4715a811 162 gpt_head, &gpt_pte) != 1) {
df70b1c2 163 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
07f3d789 164 return;
165 }
166
deb5ca80 167 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
07f3d789 168
788a8c1f 169 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
13bf2f55 170 printf("\tAttributes\n");
f07cd2c4
SW
171 printf("\tType UUID\n");
172 printf("\tPartition UUID\n");
173
f75dd584 174 for (i = 0; i < le32_to_int(gpt_head->num_partition_entries); i++) {
38a3021e
SW
175 /* Stop at the first non valid PTE */
176 if (!is_pte_valid(&gpt_pte[i]))
177 break;
178
788a8c1f 179 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
38a3021e 180 le64_to_int(gpt_pte[i].starting_lba),
788a8c1f
SW
181 le64_to_int(gpt_pte[i].ending_lba),
182 print_efiname(&gpt_pte[i]));
13bf2f55 183 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
f07cd2c4
SW
184 uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
185 printf("\ttype:\t%s\n", uuid);
186 uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
187 printf("\tuuid:\t%s\n", uuid);
07f3d789 188 }
189
190 /* Remember to free pte */
deb5ca80 191 free(gpt_pte);
07f3d789 192 return;
193}
194
195int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
196 disk_partition_t * info)
197{
f75dd584 198 ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
deb5ca80 199 gpt_entry *gpt_pte = NULL;
07f3d789 200
201 /* "part" argument must be at least 1 */
202 if (!dev_desc || !info || part < 1) {
df70b1c2 203 printf("%s: Invalid Argument(s)\n", __func__);
07f3d789 204 return -1;
205 }
206
207 /* This function validates AND fills in the GPT header and PTE */
208 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
4715a811 209 gpt_head, &gpt_pte) != 1) {
df70b1c2 210 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
07f3d789 211 return -1;
212 }
213
c04d68c6
SW
214 if (part > le32_to_int(gpt_head->num_partition_entries) ||
215 !is_pte_valid(&gpt_pte[part - 1])) {
216 printf("%s: *** ERROR: Invalid partition number %d ***\n",
217 __func__, part);
218 return -1;
219 }
220
07f3d789 221 /* The ulong casting limits the maximum disk size to 2 TB */
deb5ca80 222 info->start = (ulong) le64_to_int(gpt_pte[part - 1].starting_lba);
50970839 223 /* The ending LBA is inclusive, to calculate size, add 1 to it */
deb5ca80 224 info->size = ((ulong)le64_to_int(gpt_pte[part - 1].ending_lba) + 1)
50970839 225 - info->start;
07f3d789 226 info->blksz = GPT_BLOCK_SIZE;
227
6eecc030 228 sprintf((char *)info->name, "%s",
deb5ca80 229 print_efiname(&gpt_pte[part - 1]));
07f3d789 230 sprintf((char *)info->type, "U-Boot");
b4414f4a 231 info->bootable = is_bootable(&gpt_pte[part - 1]);
894bfbbf
SW
232#ifdef CONFIG_PARTITION_UUIDS
233 uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
234#endif
07f3d789 235
df70b1c2 236 debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
07f3d789 237 info->start, info->size, info->name);
238
239 /* Remember to free pte */
deb5ca80 240 free(gpt_pte);
07f3d789 241 return 0;
242}
243
244int test_part_efi(block_dev_desc_t * dev_desc)
245{
f75dd584 246 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
07f3d789 247
248 /* Read legacy MBR from block 0 and validate it */
f75dd584
A
249 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
250 || (is_pmbr_valid(legacymbr) != 1)) {
07f3d789 251 return -1;
252 }
253 return 0;
254}
255
256/*
257 * Private functions
258 */
259/*
260 * pmbr_part_valid(): Check for EFI partition signature
261 *
262 * Returns: 1 if EFI GPT partition type is found.
263 */
264static int pmbr_part_valid(struct partition *part)
265{
266 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
267 le32_to_int(part->start_sect) == 1UL) {
268 return 1;
269 }
270
271 return 0;
272}
273
274/*
275 * is_pmbr_valid(): test Protective MBR for validity
276 *
277 * Returns: 1 if PMBR is valid, 0 otherwise.
278 * Validity depends on two things:
279 * 1) MSDOS signature is in the last two bytes of the MBR
280 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
281 */
282static int is_pmbr_valid(legacy_mbr * mbr)
283{
284 int i = 0;
285
286 if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) {
287 return 0;
288 }
289
290 for (i = 0; i < 4; i++) {
291 if (pmbr_part_valid(&mbr->partition_record[i])) {
292 return 1;
293 }
294 }
295 return 0;
296}
297
298/**
299 * is_gpt_valid() - tests one GPT header and PTEs for validity
300 *
301 * lba is the logical block address of the GPT header to test
302 * gpt is a GPT header ptr, filled on return.
303 * ptes is a PTEs ptr, filled on return.
304 *
305 * Description: returns 1 if valid, 0 on error.
306 * If valid, returns pointers to PTEs.
307 */
308static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
309 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
310{
311 unsigned char crc32_backup[4] = { 0 };
312 unsigned long calc_crc32;
313 unsigned long long lastlba;
314
315 if (!dev_desc || !pgpt_head) {
df70b1c2 316 printf("%s: Invalid Argument(s)\n", __func__);
07f3d789 317 return 0;
318 }
319
320 /* Read GPT Header from device */
321 if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
322 printf("*** ERROR: Can't read GPT header ***\n");
323 return 0;
324 }
325
326 /* Check the GPT header signature */
327 if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
328 printf("GUID Partition Table Header signature is wrong:"
329 "0x%llX != 0x%llX\n",
330 (unsigned long long)le64_to_int(pgpt_head->signature),
331 (unsigned long long)GPT_HEADER_SIGNATURE);
332 return 0;
333 }
334
335 /* Check the GUID Partition Table CRC */
336 memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
337 memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
338
339 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
340 le32_to_int(pgpt_head->header_size));
341
342 memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
343
344 if (calc_crc32 != le32_to_int(crc32_backup)) {
345 printf("GUID Partition Table Header CRC is wrong:"
346 "0x%08lX != 0x%08lX\n",
347 le32_to_int(crc32_backup), calc_crc32);
348 return 0;
349 }
350
351 /* Check that the my_lba entry points to the LBA that contains the GPT */
352 if (le64_to_int(pgpt_head->my_lba) != lba) {
353 printf("GPT: my_lba incorrect: %llX != %llX\n",
354 (unsigned long long)le64_to_int(pgpt_head->my_lba),
355 (unsigned long long)lba);
356 return 0;
357 }
358
359 /* Check the first_usable_lba and last_usable_lba are within the disk. */
360 lastlba = (unsigned long long)dev_desc->lba;
361 if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
362 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
363 le64_to_int(pgpt_head->first_usable_lba), lastlba);
364 return 0;
365 }
366 if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
367 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
368 le64_to_int(pgpt_head->last_usable_lba), lastlba);
369 return 0;
370 }
371
372 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
373 le64_to_int(pgpt_head->first_usable_lba),
374 le64_to_int(pgpt_head->last_usable_lba), lastlba);
375
376 /* Read and allocate Partition Table Entries */
377 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
378 if (*pgpt_pte == NULL) {
379 printf("GPT: Failed to allocate memory for PTE\n");
380 return 0;
381 }
382
383 /* Check the GUID Partition Table Entry Array CRC */
384 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
385 le32_to_int(pgpt_head->num_partition_entries) *
386 le32_to_int(pgpt_head->sizeof_partition_entry));
387
388 if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
389 printf("GUID Partition Table Entry Array CRC is wrong:"
390 "0x%08lX != 0x%08lX\n",
391 le32_to_int(pgpt_head->partition_entry_array_crc32),
392 calc_crc32);
393
deb5ca80 394 free(*pgpt_pte);
07f3d789 395 return 0;
396 }
397
398 /* We're done, all's well */
399 return 1;
400}
401
402/**
403 * alloc_read_gpt_entries(): reads partition entries from disk
404 * @dev_desc
405 * @gpt - GPT header
406 *
407 * Description: Returns ptes on success, NULL on error.
408 * Allocates space for PTEs based on information found in @gpt.
409 * Notes: remember to free pte when you're done!
410 */
411static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
412 gpt_header * pgpt_head)
413{
414 size_t count = 0;
415 gpt_entry *pte = NULL;
416
417 if (!dev_desc || !pgpt_head) {
df70b1c2 418 printf("%s: Invalid Argument(s)\n", __func__);
07f3d789 419 return NULL;
420 }
421
422 count = le32_to_int(pgpt_head->num_partition_entries) *
423 le32_to_int(pgpt_head->sizeof_partition_entry);
424
df70b1c2 425 debug("%s: count = %lu * %lu = %u\n", __func__,
07f3d789 426 le32_to_int(pgpt_head->num_partition_entries),
427 le32_to_int(pgpt_head->sizeof_partition_entry), count);
428
429 /* Allocate memory for PTE, remember to FREE */
430 if (count != 0) {
29b7042a 431 pte = memalign(ARCH_DMA_MINALIGN, count);
07f3d789 432 }
433
434 if (count == 0 || pte == NULL) {
435 printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
df70b1c2 436 __func__, count);
07f3d789 437 return NULL;
438 }
439
440 /* Read GPT Entries from device */
441 if (dev_desc->block_read (dev_desc->dev,
442 (unsigned long)le64_to_int(pgpt_head->partition_entry_lba),
443 (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
444 != (count / GPT_BLOCK_SIZE)) {
445
446 printf("*** ERROR: Can't read GPT Entries ***\n");
447 free(pte);
448 return NULL;
449 }
450 return pte;
451}
452
453/**
454 * is_pte_valid(): validates a single Partition Table Entry
455 * @gpt_entry - Pointer to a single Partition Table Entry
456 *
457 * Description: returns 1 if valid, 0 on error.
458 */
459static int is_pte_valid(gpt_entry * pte)
460{
461 efi_guid_t unused_guid;
462
463 if (!pte) {
df70b1c2 464 printf("%s: Invalid Argument(s)\n", __func__);
07f3d789 465 return 0;
466 }
467
468 /* Only one validation for now:
469 * The GUID Partition Type != Unused Entry (ALL-ZERO)
470 */
471 memset(unused_guid.b, 0, sizeof(unused_guid.b));
472
473 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
474 sizeof(unused_guid.b)) == 0) {
475
df70b1c2 476 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
07f3d789 477 (unsigned int)pte);
478
479 return 0;
480 } else {
481 return 1;
482 }
483}
484#endif