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