blob: 70f62cc9afe03ed02b84228acf53233baa3a0632 [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"
38
Mike Frysinger0ea0aa82009-01-29 20:02:07 -050039#if defined(CONFIG_CMD_IDE) || \
40 defined(CONFIG_CMD_SATA) || \
41 defined(CONFIG_CMD_SCSI) || \
42 defined(CONFIG_CMD_USB) || \
43 defined(CONFIG_MMC) || \
44 defined(CONFIG_SYSTEMACE)
richardretanubune6745592008-09-26 11:13:22 -040045
46/* Convert char[2] in little endian format to the host format integer
47 */
48static inline unsigned short le16_to_int(unsigned char *le16)
49{
50 return ((le16[1] << 8) + le16[0]);
51}
52
53/* Convert char[4] in little endian format to the host format integer
54 */
55static inline unsigned long le32_to_int(unsigned char *le32)
56{
57 return ((le32[3] << 24) + (le32[2] << 16) + (le32[1] << 8) + le32[0]);
58}
59
60/* Convert char[8] in little endian format to the host format integer
61 */
62static inline unsigned long long le64_to_int(unsigned char *le64)
63{
64 return (((unsigned long long)le64[7] << 56) +
65 ((unsigned long long)le64[6] << 48) +
66 ((unsigned long long)le64[5] << 40) +
67 ((unsigned long long)le64[4] << 32) +
68 ((unsigned long long)le64[3] << 24) +
69 ((unsigned long long)le64[2] << 16) +
70 ((unsigned long long)le64[1] << 8) +
71 (unsigned long long)le64[0]);
72}
73
74/**
75 * efi_crc32() - EFI version of crc32 function
76 * @buf: buffer to calculate crc32 of
77 * @len - length of buf
78 *
79 * Description: Returns EFI-style CRC32 value for @buf
80 */
81static inline unsigned long efi_crc32(const void *buf, unsigned long len)
82{
83 return crc32(0, buf, len);
84}
85
86/*
87 * Private function prototypes
88 */
89
90static int pmbr_part_valid(struct partition *part);
91static int is_pmbr_valid(legacy_mbr * mbr);
92
93static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
94 gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
95
96static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
97 gpt_header * pgpt_head);
98
99static int is_pte_valid(gpt_entry * pte);
100
101/*
102 * Public Functions (include/part.h)
103 */
104
105void print_part_efi(block_dev_desc_t * dev_desc)
106{
107 gpt_header gpt_head;
108 gpt_entry **pgpt_pte = NULL;
109 int i = 0;
110
111 if (!dev_desc) {
112 printf("%s: Invalid Argument(s)\n", __FUNCTION__);
113 return;
114 }
115 /* This function validates AND fills in the GPT header and PTE */
116 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
117 &(gpt_head), pgpt_pte) != 1) {
118 printf("%s: *** ERROR: Invalid GPT ***\n", __FUNCTION__);
119 return;
120 }
121
122 debug("%s: gpt-entry at 0x%08X\n", __FUNCTION__, (unsigned int)*pgpt_pte);
123
124 printf("Part Start LBA End LBA\n");
125 for (i = 0; i < le32_to_int(gpt_head.num_partition_entries); i++) {
126
127 if (is_pte_valid(&(*pgpt_pte)[i])) {
128 printf("%s%d 0x%llX 0x%llX\n", GPT_ENTRY_NAME,
129 (i + 1),
130 le64_to_int((*pgpt_pte)[i].starting_lba),
131 le64_to_int((*pgpt_pte)[i].ending_lba));
132 } else {
133 break; /* Stop at the first non valid PTE */
134 }
135 }
136
137 /* Remember to free pte */
138 if (*pgpt_pte != NULL) {
139 debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
140 free(*pgpt_pte);
141 }
142 return;
143}
144
145int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
146 disk_partition_t * info)
147{
148 gpt_header gpt_head;
149 gpt_entry **pgpt_pte = NULL;
150
151 /* "part" argument must be at least 1 */
152 if (!dev_desc || !info || part < 1) {
153 printf("%s: Invalid Argument(s)\n", __FUNCTION__);
154 return -1;
155 }
156
157 /* This function validates AND fills in the GPT header and PTE */
158 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
159 &(gpt_head), pgpt_pte) != 1) {
160 printf("%s: *** ERROR: Invalid GPT ***\n", __FUNCTION__);
161 return -1;
162 }
163
164 /* The ulong casting limits the maximum disk size to 2 TB */
165 info->start = (ulong) le64_to_int((*pgpt_pte)[part - 1].starting_lba);
Richard Retanubun413dfc72009-01-26 08:45:14 -0500166 /* The ending LBA is inclusive, to calculate size, add 1 to it */
167 info->size = ((ulong)le64_to_int((*pgpt_pte)[part - 1].ending_lba) + 1)
168 - info->start;
richardretanubune6745592008-09-26 11:13:22 -0400169 info->blksz = GPT_BLOCK_SIZE;
170
171 sprintf((char *)info->name, "%s%d\n", GPT_ENTRY_NAME, part);
172 sprintf((char *)info->type, "U-Boot");
173
174 debug("%s: start 0x%lX, size 0x%lX, name %s", __FUNCTION__,
175 info->start, info->size, info->name);
176
177 /* Remember to free pte */
178 if (*pgpt_pte != NULL) {
179 debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
180 free(*pgpt_pte);
181 }
182 return 0;
183}
184
185int test_part_efi(block_dev_desc_t * dev_desc)
186{
187 legacy_mbr legacymbr;
188
189 /* Read legacy MBR from block 0 and validate it */
190 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) & legacymbr) != 1)
191 || (is_pmbr_valid(&legacymbr) != 1)) {
192 return -1;
193 }
194 return 0;
195}
196
197/*
198 * Private functions
199 */
200/*
201 * pmbr_part_valid(): Check for EFI partition signature
202 *
203 * Returns: 1 if EFI GPT partition type is found.
204 */
205static int pmbr_part_valid(struct partition *part)
206{
207 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
208 le32_to_int(part->start_sect) == 1UL) {
209 return 1;
210 }
211
212 return 0;
213}
214
215/*
216 * is_pmbr_valid(): test Protective MBR for validity
217 *
218 * Returns: 1 if PMBR is valid, 0 otherwise.
219 * Validity depends on two things:
220 * 1) MSDOS signature is in the last two bytes of the MBR
221 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
222 */
223static int is_pmbr_valid(legacy_mbr * mbr)
224{
225 int i = 0;
226
227 if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) {
228 return 0;
229 }
230
231 for (i = 0; i < 4; i++) {
232 if (pmbr_part_valid(&mbr->partition_record[i])) {
233 return 1;
234 }
235 }
236 return 0;
237}
238
239/**
240 * is_gpt_valid() - tests one GPT header and PTEs for validity
241 *
242 * lba is the logical block address of the GPT header to test
243 * gpt is a GPT header ptr, filled on return.
244 * ptes is a PTEs ptr, filled on return.
245 *
246 * Description: returns 1 if valid, 0 on error.
247 * If valid, returns pointers to PTEs.
248 */
249static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
250 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
251{
252 unsigned char crc32_backup[4] = { 0 };
253 unsigned long calc_crc32;
254 unsigned long long lastlba;
255
256 if (!dev_desc || !pgpt_head) {
257 printf("%s: Invalid Argument(s)\n", __FUNCTION__);
258 return 0;
259 }
260
261 /* Read GPT Header from device */
262 if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
263 printf("*** ERROR: Can't read GPT header ***\n");
264 return 0;
265 }
266
267 /* Check the GPT header signature */
268 if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
269 printf("GUID Partition Table Header signature is wrong:"
270 "0x%llX != 0x%llX\n",
271 (unsigned long long)le64_to_int(pgpt_head->signature),
272 (unsigned long long)GPT_HEADER_SIGNATURE);
273 return 0;
274 }
275
276 /* Check the GUID Partition Table CRC */
277 memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup));
278 memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
279
280 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
281 le32_to_int(pgpt_head->header_size));
282
283 memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup));
284
285 if (calc_crc32 != le32_to_int(crc32_backup)) {
286 printf("GUID Partition Table Header CRC is wrong:"
287 "0x%08lX != 0x%08lX\n",
288 le32_to_int(crc32_backup), calc_crc32);
289 return 0;
290 }
291
292 /* Check that the my_lba entry points to the LBA that contains the GPT */
293 if (le64_to_int(pgpt_head->my_lba) != lba) {
294 printf("GPT: my_lba incorrect: %llX != %llX\n",
295 (unsigned long long)le64_to_int(pgpt_head->my_lba),
296 (unsigned long long)lba);
297 return 0;
298 }
299
300 /* Check the first_usable_lba and last_usable_lba are within the disk. */
301 lastlba = (unsigned long long)dev_desc->lba;
302 if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) {
303 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
304 le64_to_int(pgpt_head->first_usable_lba), lastlba);
305 return 0;
306 }
307 if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) {
308 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
309 le64_to_int(pgpt_head->last_usable_lba), lastlba);
310 return 0;
311 }
312
313 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
314 le64_to_int(pgpt_head->first_usable_lba),
315 le64_to_int(pgpt_head->last_usable_lba), lastlba);
316
317 /* Read and allocate Partition Table Entries */
318 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
319 if (*pgpt_pte == NULL) {
320 printf("GPT: Failed to allocate memory for PTE\n");
321 return 0;
322 }
323
324 /* Check the GUID Partition Table Entry Array CRC */
325 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
326 le32_to_int(pgpt_head->num_partition_entries) *
327 le32_to_int(pgpt_head->sizeof_partition_entry));
328
329 if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) {
330 printf("GUID Partition Table Entry Array CRC is wrong:"
331 "0x%08lX != 0x%08lX\n",
332 le32_to_int(pgpt_head->partition_entry_array_crc32),
333 calc_crc32);
334
335 if (*pgpt_pte != NULL) {
336 free(*pgpt_pte);
337 }
338 return 0;
339 }
340
341 /* We're done, all's well */
342 return 1;
343}
344
345/**
346 * alloc_read_gpt_entries(): reads partition entries from disk
347 * @dev_desc
348 * @gpt - GPT header
349 *
350 * Description: Returns ptes on success, NULL on error.
351 * Allocates space for PTEs based on information found in @gpt.
352 * Notes: remember to free pte when you're done!
353 */
354static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
355 gpt_header * pgpt_head)
356{
357 size_t count = 0;
358 gpt_entry *pte = NULL;
359
360 if (!dev_desc || !pgpt_head) {
361 printf("%s: Invalid Argument(s)\n", __FUNCTION__);
362 return NULL;
363 }
364
365 count = le32_to_int(pgpt_head->num_partition_entries) *
366 le32_to_int(pgpt_head->sizeof_partition_entry);
367
368 debug("%s: count = %lu * %lu = %u\n", __FUNCTION__,
369 le32_to_int(pgpt_head->num_partition_entries),
370 le32_to_int(pgpt_head->sizeof_partition_entry), count);
371
372 /* Allocate memory for PTE, remember to FREE */
373 if (count != 0) {
374 pte = malloc(count);
375 }
376
377 if (count == 0 || pte == NULL) {
378 printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
379 __FUNCTION__, count);
380 return NULL;
381 }
382
383 /* Read GPT Entries from device */
384 if (dev_desc->block_read (dev_desc->dev,
385 (unsigned long)le64_to_int(pgpt_head->partition_entry_lba),
386 (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
387 != (count / GPT_BLOCK_SIZE)) {
388
389 printf("*** ERROR: Can't read GPT Entries ***\n");
390 free(pte);
391 return NULL;
392 }
393 return pte;
394}
395
396/**
397 * is_pte_valid(): validates a single Partition Table Entry
398 * @gpt_entry - Pointer to a single Partition Table Entry
399 *
400 * Description: returns 1 if valid, 0 on error.
401 */
402static int is_pte_valid(gpt_entry * pte)
403{
404 efi_guid_t unused_guid;
405
406 if (!pte) {
407 printf("%s: Invalid Argument(s)\n", __FUNCTION__);
408 return 0;
409 }
410
411 /* Only one validation for now:
412 * The GUID Partition Type != Unused Entry (ALL-ZERO)
413 */
414 memset(unused_guid.b, 0, sizeof(unused_guid.b));
415
416 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
417 sizeof(unused_guid.b)) == 0) {
418
419 debug("%s: Found an unused PTE GUID at 0x%08X\n", __FUNCTION__,
420 (unsigned int)pte);
421
422 return 0;
423 } else {
424 return 1;
425 }
426}
427#endif