]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - disk/part_efi.c
disk: part_efi: print partition UUIDs
[karo-tx-uboot.git] / disk / part_efi.c
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_SATA) || \
42     defined(CONFIG_CMD_SCSI) || \
43     defined(CONFIG_CMD_USB) || \
44     defined(CONFIG_MMC) || \
45     defined(CONFIG_SYSTEMACE)
46
47 /* Convert char[2] in little endian format to the host format integer
48  */
49 static 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  */
56 static 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  */
63 static 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  */
82 static 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
91 static int pmbr_part_valid(struct partition *part);
92 static int is_pmbr_valid(legacy_mbr * mbr);
93
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);
96
97 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
98                                 gpt_header * pgpt_head);
99
100 static int is_pte_valid(gpt_entry * pte);
101
102 static 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
116 static 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
136 /*
137  * Public Functions (include/part.h)
138  */
139
140 void print_part_efi(block_dev_desc_t * dev_desc)
141 {
142         ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
143         gpt_entry *gpt_pte = NULL;
144         int i = 0;
145         char uuid[37];
146
147         if (!dev_desc) {
148                 printf("%s: Invalid Argument(s)\n", __func__);
149                 return;
150         }
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__);
155                 return;
156         }
157
158         debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
159
160         printf("Part\tStart LBA\tEnd LBA\t\tName\n");
161         printf("\tType UUID\n");
162         printf("\tPartition UUID\n");
163
164         for (i = 0; i < le32_to_int(gpt_head->num_partition_entries); i++) {
165                 /* Stop at the first non valid PTE */
166                 if (!is_pte_valid(&gpt_pte[i]))
167                         break;
168
169                 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
170                         le64_to_int(gpt_pte[i].starting_lba),
171                         le64_to_int(gpt_pte[i].ending_lba),
172                         print_efiname(&gpt_pte[i]));
173                 uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
174                 printf("\ttype:\t%s\n", uuid);
175                 uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
176                 printf("\tuuid:\t%s\n", uuid);
177         }
178
179         /* Remember to free pte */
180         free(gpt_pte);
181         return;
182 }
183
184 int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
185                                 disk_partition_t * info)
186 {
187         ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
188         gpt_entry *gpt_pte = NULL;
189
190         /* "part" argument must be at least 1 */
191         if (!dev_desc || !info || part < 1) {
192                 printf("%s: Invalid Argument(s)\n", __func__);
193                 return -1;
194         }
195
196         /* This function validates AND fills in the GPT header and PTE */
197         if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
198                         gpt_head, &gpt_pte) != 1) {
199                 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
200                 return -1;
201         }
202
203         if (part > le32_to_int(gpt_head->num_partition_entries) ||
204             !is_pte_valid(&gpt_pte[part - 1])) {
205                 printf("%s: *** ERROR: Invalid partition number %d ***\n",
206                         __func__, part);
207                 return -1;
208         }
209
210         /* The ulong casting limits the maximum disk size to 2 TB */
211         info->start = (ulong) le64_to_int(gpt_pte[part - 1].starting_lba);
212         /* The ending LBA is inclusive, to calculate size, add 1 to it */
213         info->size = ((ulong)le64_to_int(gpt_pte[part - 1].ending_lba) + 1)
214                      - info->start;
215         info->blksz = GPT_BLOCK_SIZE;
216
217         sprintf((char *)info->name, "%s",
218                         print_efiname(&gpt_pte[part - 1]));
219         sprintf((char *)info->type, "U-Boot");
220 #ifdef CONFIG_PARTITION_UUIDS
221         uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
222 #endif
223
224         debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
225                 info->start, info->size, info->name);
226
227         /* Remember to free pte */
228         free(gpt_pte);
229         return 0;
230 }
231
232 int test_part_efi(block_dev_desc_t * dev_desc)
233 {
234         ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
235
236         /* Read legacy MBR from block 0 and validate it */
237         if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
238                 || (is_pmbr_valid(legacymbr) != 1)) {
239                 return -1;
240         }
241         return 0;
242 }
243
244 /*
245  * Private functions
246  */
247 /*
248  * pmbr_part_valid(): Check for EFI partition signature
249  *
250  * Returns: 1 if EFI GPT partition type is found.
251  */
252 static int pmbr_part_valid(struct partition *part)
253 {
254         if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
255                 le32_to_int(part->start_sect) == 1UL) {
256                 return 1;
257         }
258
259         return 0;
260 }
261
262 /*
263  * is_pmbr_valid(): test Protective MBR for validity
264  *
265  * Returns: 1 if PMBR is valid, 0 otherwise.
266  * Validity depends on two things:
267  *  1) MSDOS signature is in the last two bytes of the MBR
268  *  2) One partition of type 0xEE is found, checked by pmbr_part_valid()
269  */
270 static int is_pmbr_valid(legacy_mbr * mbr)
271 {
272         int i = 0;
273
274         if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) {
275                 return 0;
276         }
277
278         for (i = 0; i < 4; i++) {
279                 if (pmbr_part_valid(&mbr->partition_record[i])) {
280                         return 1;
281                 }
282         }
283         return 0;
284 }
285
286 /**
287  * is_gpt_valid() - tests one GPT header and PTEs for validity
288  *
289  * lba is the logical block address of the GPT header to test
290  * gpt is a GPT header ptr, filled on return.
291  * ptes is a PTEs ptr, filled on return.
292  *
293  * Description: returns 1 if valid,  0 on error.
294  * If valid, returns pointers to PTEs.
295  */
296 static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
297                         gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
298 {
299         unsigned char crc32_backup[4] = { 0 };
300         unsigned long calc_crc32;
301         unsigned long long lastlba;
302
303         if (!dev_desc || !pgpt_head) {
304                 printf("%s: Invalid Argument(s)\n", __func__);
305                 return 0;
306         }
307
308         /* Read GPT Header from device */
309         if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
310                 printf("*** ERROR: Can't read GPT header ***\n");
311                 return 0;
312         }
313
314         /* Check the GPT header signature */
315         if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
316                 printf("GUID Partition Table Header signature is wrong:"
317                         "0x%llX != 0x%llX\n",
318                         (unsigned long long)le64_to_int(pgpt_head->signature),
319                         (unsigned long long)GPT_HEADER_SIGNATURE);
320                 return 0;
321         }
322
323         /* Check the GUID Partition Table CRC */
324         memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
325         memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
326
327         calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
328                 le32_to_int(pgpt_head->header_size));
329
330         memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
331
332         if (calc_crc32 != le32_to_int(crc32_backup)) {
333                 printf("GUID Partition Table Header CRC is wrong:"
334                         "0x%08lX != 0x%08lX\n",
335                         le32_to_int(crc32_backup), calc_crc32);
336                 return 0;
337         }
338
339         /* Check that the my_lba entry points to the LBA that contains the GPT */
340         if (le64_to_int(pgpt_head->my_lba) != lba) {
341                 printf("GPT: my_lba incorrect: %llX != %llX\n",
342                         (unsigned long long)le64_to_int(pgpt_head->my_lba),
343                         (unsigned long long)lba);
344                 return 0;
345         }
346
347         /* Check the first_usable_lba and last_usable_lba are within the disk. */
348         lastlba = (unsigned long long)dev_desc->lba;
349         if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
350                 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
351                         le64_to_int(pgpt_head->first_usable_lba), lastlba);
352                 return 0;
353         }
354         if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
355                 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
356                         le64_to_int(pgpt_head->last_usable_lba), lastlba);
357                 return 0;
358         }
359
360         debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
361                 le64_to_int(pgpt_head->first_usable_lba),
362                 le64_to_int(pgpt_head->last_usable_lba), lastlba);
363
364         /* Read and allocate Partition Table Entries */
365         *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
366         if (*pgpt_pte == NULL) {
367                 printf("GPT: Failed to allocate memory for PTE\n");
368                 return 0;
369         }
370
371         /* Check the GUID Partition Table Entry Array CRC */
372         calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
373                 le32_to_int(pgpt_head->num_partition_entries) *
374                 le32_to_int(pgpt_head->sizeof_partition_entry));
375
376         if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
377                 printf("GUID Partition Table Entry Array CRC is wrong:"
378                         "0x%08lX != 0x%08lX\n",
379                         le32_to_int(pgpt_head->partition_entry_array_crc32),
380                         calc_crc32);
381
382                 free(*pgpt_pte);
383                 return 0;
384         }
385
386         /* We're done, all's well */
387         return 1;
388 }
389
390 /**
391  * alloc_read_gpt_entries(): reads partition entries from disk
392  * @dev_desc
393  * @gpt - GPT header
394  *
395  * Description: Returns ptes on success,  NULL on error.
396  * Allocates space for PTEs based on information found in @gpt.
397  * Notes: remember to free pte when you're done!
398  */
399 static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
400                                          gpt_header * pgpt_head)
401 {
402         size_t count = 0;
403         gpt_entry *pte = NULL;
404
405         if (!dev_desc || !pgpt_head) {
406                 printf("%s: Invalid Argument(s)\n", __func__);
407                 return NULL;
408         }
409
410         count = le32_to_int(pgpt_head->num_partition_entries) *
411                 le32_to_int(pgpt_head->sizeof_partition_entry);
412
413         debug("%s: count = %lu * %lu = %u\n", __func__,
414                 le32_to_int(pgpt_head->num_partition_entries),
415                 le32_to_int(pgpt_head->sizeof_partition_entry), count);
416
417         /* Allocate memory for PTE, remember to FREE */
418         if (count != 0) {
419                 pte = memalign(ARCH_DMA_MINALIGN, count);
420         }
421
422         if (count == 0 || pte == NULL) {
423                 printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
424                         __func__, count);
425                 return NULL;
426         }
427
428         /* Read GPT Entries from device */
429         if (dev_desc->block_read (dev_desc->dev,
430                 (unsigned long)le64_to_int(pgpt_head->partition_entry_lba),
431                 (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
432                 != (count / GPT_BLOCK_SIZE)) {
433
434                 printf("*** ERROR: Can't read GPT Entries ***\n");
435                 free(pte);
436                 return NULL;
437         }
438         return pte;
439 }
440
441 /**
442  * is_pte_valid(): validates a single Partition Table Entry
443  * @gpt_entry - Pointer to a single Partition Table Entry
444  *
445  * Description: returns 1 if valid,  0 on error.
446  */
447 static int is_pte_valid(gpt_entry * pte)
448 {
449         efi_guid_t unused_guid;
450
451         if (!pte) {
452                 printf("%s: Invalid Argument(s)\n", __func__);
453                 return 0;
454         }
455
456         /* Only one validation for now:
457          * The GUID Partition Type != Unused Entry (ALL-ZERO)
458          */
459         memset(unused_guid.b, 0, sizeof(unused_guid.b));
460
461         if (memcmp(pte->partition_type_guid.b, unused_guid.b,
462                 sizeof(unused_guid.b)) == 0) {
463
464                 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
465                 (unsigned int)pte);
466
467                 return 0;
468         } else {
469                 return 1;
470         }
471 }
472 #endif