blob: e9987f055cb1c123c5df1c361db20ca38b538dd3 [file] [log] [blame]
richardretanubun07f3d782008-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-VILLARD6d0f6bc2008-10-16 15:01:15 +020025 * Problems with CONFIG_SYS_64BIT_LBA:
richardretanubun07f3d782008-09-26 11:13:22 -040026 *
27 * struct disk_partition.start in include/part.h is sized as ulong.
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020028 * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t.
richardretanubun07f3d782008-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 */
Marc Dietrich8faefad2013-03-29 07:57:10 +000033#include <asm/unaligned.h>
richardretanubun07f3d782008-09-26 11:13:22 -040034#include <common.h>
35#include <command.h>
36#include <ide.h>
37#include <malloc.h>
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010038#include <part_efi.h>
Lei Wen6eecc032011-09-07 18:11:19 +000039#include <linux/ctype.h>
richardretanubun07f3d782008-09-26 11:13:22 -040040
Lukasz Majewski40684dd2012-12-11 11:09:46 +010041DECLARE_GLOBAL_DATA_PTR;
42
Stephen Warren2c1af9d2013-02-28 15:03:46 +000043#ifdef HAVE_BLOCK_DEVICE
richardretanubun07f3d782008-09-26 11:13:22 -040044/**
45 * efi_crc32() - EFI version of crc32 function
46 * @buf: buffer to calculate crc32 of
47 * @len - length of buf
48 *
49 * Description: Returns EFI-style CRC32 value for @buf
50 */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010051static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubun07f3d782008-09-26 11:13:22 -040052{
53 return crc32(0, buf, len);
54}
55
56/*
57 * Private function prototypes
58 */
59
60static int pmbr_part_valid(struct partition *part);
61static int is_pmbr_valid(legacy_mbr * mbr);
richardretanubun07f3d782008-09-26 11:13:22 -040062static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
63 gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -040064static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
65 gpt_header * pgpt_head);
richardretanubun07f3d782008-09-26 11:13:22 -040066static int is_pte_valid(gpt_entry * pte);
67
Lei Wen6eecc032011-09-07 18:11:19 +000068static char *print_efiname(gpt_entry *pte)
69{
70 static char name[PARTNAME_SZ + 1];
71 int i;
72 for (i = 0; i < PARTNAME_SZ; i++) {
73 u8 c;
74 c = pte->partition_name[i] & 0xff;
75 c = (c && !isprint(c)) ? '.' : c;
76 name[i] = c;
77 }
78 name[PARTNAME_SZ] = 0;
79 return name;
80}
81
Stephen Warren894bfbb2012-09-21 09:50:59 +000082static void uuid_string(unsigned char *uuid, char *str)
83{
84 static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
85 12, 13, 14, 15};
86 int i;
87
88 for (i = 0; i < 16; i++) {
89 sprintf(str, "%02x", uuid[le[i]]);
90 str += 2;
91 switch (i) {
92 case 3:
93 case 5:
94 case 7:
95 case 9:
96 *str++ = '-';
97 break;
98 }
99 }
100}
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000101
Stephen Warrenb4414f42012-10-08 08:14:37 +0000102static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
103
104static inline int is_bootable(gpt_entry *p)
105{
106 return p->attributes.fields.legacy_bios_bootable ||
107 !memcmp(&(p->partition_type_guid), &system_guid,
108 sizeof(efi_guid_t));
109}
110
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100111#ifdef CONFIG_EFI_PARTITION
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000112/*
113 * Public Functions (include/part.h)
114 */
115
116void print_part_efi(block_dev_desc_t * dev_desc)
117{
118 ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
119 gpt_entry *gpt_pte = NULL;
120 int i = 0;
121 char uuid[37];
122
123 if (!dev_desc) {
124 printf("%s: Invalid Argument(s)\n", __func__);
125 return;
126 }
127 /* This function validates AND fills in the GPT header and PTE */
128 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
129 gpt_head, &gpt_pte) != 1) {
130 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
131 return;
132 }
133
134 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
135
136 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren13bf2f52012-10-08 08:14:36 +0000137 printf("\tAttributes\n");
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000138 printf("\tType UUID\n");
139 printf("\tPartition UUID\n");
140
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100141 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000142 /* Stop at the first non valid PTE */
143 if (!is_pte_valid(&gpt_pte[i]))
144 break;
145
146 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100147 le64_to_cpu(gpt_pte[i].starting_lba),
148 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000149 print_efiname(&gpt_pte[i]));
Stephen Warren13bf2f52012-10-08 08:14:36 +0000150 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000151 uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
152 printf("\ttype:\t%s\n", uuid);
153 uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
154 printf("\tuuid:\t%s\n", uuid);
155 }
156
157 /* Remember to free pte */
158 free(gpt_pte);
159 return;
160}
Stephen Warren894bfbb2012-09-21 09:50:59 +0000161
richardretanubun07f3d782008-09-26 11:13:22 -0400162int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
163 disk_partition_t * info)
164{
Anton staaff75dd582011-10-12 13:56:04 +0000165 ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
Doug Andersondeb5ca82011-10-19 09:47:31 +0000166 gpt_entry *gpt_pte = NULL;
richardretanubun07f3d782008-09-26 11:13:22 -0400167
168 /* "part" argument must be at least 1 */
169 if (!dev_desc || !info || part < 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000170 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400171 return -1;
172 }
173
174 /* This function validates AND fills in the GPT header and PTE */
175 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren4715a812011-10-28 09:21:46 +0000176 gpt_head, &gpt_pte) != 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000177 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400178 return -1;
179 }
180
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100181 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000182 !is_pte_valid(&gpt_pte[part - 1])) {
183 printf("%s: *** ERROR: Invalid partition number %d ***\n",
184 __func__, part);
185 return -1;
186 }
187
richardretanubun07f3d782008-09-26 11:13:22 -0400188 /* The ulong casting limits the maximum disk size to 2 TB */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100189 info->start = (u64)le64_to_cpu(gpt_pte[part - 1].starting_lba);
Richard Retanubun50970832009-01-26 08:45:14 -0500190 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100191 info->size = ((u64)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1)
Richard Retanubun50970832009-01-26 08:45:14 -0500192 - info->start;
richardretanubun07f3d782008-09-26 11:13:22 -0400193 info->blksz = GPT_BLOCK_SIZE;
194
Lei Wen6eecc032011-09-07 18:11:19 +0000195 sprintf((char *)info->name, "%s",
Doug Andersondeb5ca82011-10-19 09:47:31 +0000196 print_efiname(&gpt_pte[part - 1]));
richardretanubun07f3d782008-09-26 11:13:22 -0400197 sprintf((char *)info->type, "U-Boot");
Stephen Warrenb4414f42012-10-08 08:14:37 +0000198 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000199#ifdef CONFIG_PARTITION_UUIDS
200 uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
201#endif
richardretanubun07f3d782008-09-26 11:13:22 -0400202
Doug Andersondf70b1c2011-10-19 08:04:46 +0000203 debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
richardretanubun07f3d782008-09-26 11:13:22 -0400204 info->start, info->size, info->name);
205
206 /* Remember to free pte */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000207 free(gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400208 return 0;
209}
210
211int test_part_efi(block_dev_desc_t * dev_desc)
212{
Anton staaff75dd582011-10-12 13:56:04 +0000213 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
richardretanubun07f3d782008-09-26 11:13:22 -0400214
215 /* Read legacy MBR from block 0 and validate it */
Anton staaff75dd582011-10-12 13:56:04 +0000216 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
217 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400218 return -1;
219 }
220 return 0;
221}
222
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100223/**
224 * set_protective_mbr(): Set the EFI protective MBR
225 * @param dev_desc - block device descriptor
226 *
227 * @return - zero on success, otherwise error
228 */
229static int set_protective_mbr(block_dev_desc_t *dev_desc)
230{
231 legacy_mbr *p_mbr;
232
233 /* Setup the Protective MBR */
234 p_mbr = calloc(1, sizeof(p_mbr));
235 if (p_mbr == NULL) {
236 printf("%s: calloc failed!\n", __func__);
237 return -1;
238 }
239 /* Append signature */
240 p_mbr->signature = MSDOS_MBR_SIGNATURE;
241 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
242 p_mbr->partition_record[0].start_sect = 1;
243 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba;
244
245 /* Write MBR sector to the MMC device */
246 if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
247 printf("** Can't write to device %d **\n",
248 dev_desc->dev);
249 free(p_mbr);
250 return -1;
251 }
252
253 free(p_mbr);
254 return 0;
255}
256
257/**
258 * string_uuid(); Convert UUID stored as string to bytes
259 *
260 * @param uuid - UUID represented as string
261 * @param dst - GUID buffer
262 *
263 * @return return 0 on successful conversion
264 */
265static int string_uuid(char *uuid, u8 *dst)
266{
267 efi_guid_t guid;
268 u16 b, c, d;
269 u64 e;
270 u32 a;
271 u8 *p;
272 u8 i;
273
274 const u8 uuid_str_len = 36;
275
276 /* The UUID is written in text: */
277 /* 1 9 14 19 24 */
278 /* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */
279
280 debug("%s: uuid: %s\n", __func__, uuid);
281
282 if (strlen(uuid) != uuid_str_len)
283 return -1;
284
285 for (i = 0; i < uuid_str_len; i++) {
286 if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
287 if (uuid[i] != '-')
288 return -1;
289 } else {
290 if (!isxdigit(uuid[i]))
291 return -1;
292 }
293 }
294
295 a = (u32)simple_strtoul(uuid, NULL, 16);
296 b = (u16)simple_strtoul(uuid + 9, NULL, 16);
297 c = (u16)simple_strtoul(uuid + 14, NULL, 16);
298 d = (u16)simple_strtoul(uuid + 19, NULL, 16);
299 e = (u64)simple_strtoull(uuid + 24, NULL, 16);
300
301 p = (u8 *) &e;
302 guid = EFI_GUID(a, b, c, d >> 8, d & 0xFF,
303 *(p + 5), *(p + 4), *(p + 3),
304 *(p + 2), *(p + 1) , *p);
305
306 memcpy(dst, guid.b, sizeof(efi_guid_t));
307
308 return 0;
309}
310
311int write_gpt_table(block_dev_desc_t *dev_desc,
312 gpt_header *gpt_h, gpt_entry *gpt_e)
313{
314 const int pte_blk_num = (gpt_h->num_partition_entries
315 * sizeof(gpt_entry)) / dev_desc->blksz;
316
317 u32 calc_crc32;
318 u64 val;
319
320 debug("max lba: %x\n", (u32) dev_desc->lba);
321 /* Setup the Protective MBR */
322 if (set_protective_mbr(dev_desc) < 0)
323 goto err;
324
325 /* Generate CRC for the Primary GPT Header */
326 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
327 le32_to_cpu(gpt_h->num_partition_entries) *
328 le32_to_cpu(gpt_h->sizeof_partition_entry));
329 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
330
331 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
332 le32_to_cpu(gpt_h->header_size));
333 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
334
335 /* Write the First GPT to the block right after the Legacy MBR */
336 if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
337 goto err;
338
339 if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_num, gpt_e)
340 != pte_blk_num)
341 goto err;
342
343 /* recalculate the values for the Second GPT Header */
344 val = le64_to_cpu(gpt_h->my_lba);
345 gpt_h->my_lba = gpt_h->alternate_lba;
346 gpt_h->alternate_lba = cpu_to_le64(val);
347 gpt_h->header_crc32 = 0;
348
349 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
350 le32_to_cpu(gpt_h->header_size));
351 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
352
353 if (dev_desc->block_write(dev_desc->dev,
354 le32_to_cpu(gpt_h->last_usable_lba + 1),
355 pte_blk_num, gpt_e) != pte_blk_num)
356 goto err;
357
358 if (dev_desc->block_write(dev_desc->dev,
359 le32_to_cpu(gpt_h->my_lba), 1, gpt_h) != 1)
360 goto err;
361
362 debug("GPT successfully written to block device!\n");
363 return 0;
364
365 err:
366 printf("** Can't write to device %d **\n", dev_desc->dev);
367 return -1;
368}
369
370int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
371 disk_partition_t *partitions, int parts)
372{
373 u32 offset = (u32)le32_to_cpu(gpt_h->first_usable_lba);
374 ulong start;
375 int i, k;
376 size_t name_len;
377#ifdef CONFIG_PARTITION_UUIDS
378 char *str_uuid;
379#endif
380
381 for (i = 0; i < parts; i++) {
382 /* partition starting lba */
383 start = partitions[i].start;
384 if (start && (start < offset)) {
385 printf("Partition overlap\n");
386 return -1;
387 }
388 if (start) {
389 gpt_e[i].starting_lba = cpu_to_le64(start);
390 offset = start + partitions[i].size;
391 } else {
392 gpt_e[i].starting_lba = cpu_to_le64(offset);
393 offset += partitions[i].size;
394 }
395 if (offset >= gpt_h->last_usable_lba) {
396 printf("Partitions layout exceds disk size\n");
397 return -1;
398 }
399 /* partition ending lba */
400 if ((i == parts - 1) && (partitions[i].size == 0))
401 /* extend the last partition to maximuim */
402 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
403 else
404 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
405
406 /* partition type GUID */
407 memcpy(gpt_e[i].partition_type_guid.b,
408 &PARTITION_BASIC_DATA_GUID, 16);
409
410#ifdef CONFIG_PARTITION_UUIDS
411 str_uuid = partitions[i].uuid;
412 if (string_uuid(str_uuid, gpt_e[i].unique_partition_guid.b)) {
413 printf("Partition no. %d: invalid guid: %s\n",
414 i, str_uuid);
415 return -1;
416 }
417#endif
418
419 /* partition attributes */
420 memset(&gpt_e[i].attributes, 0,
421 sizeof(gpt_entry_attributes));
422
423 /* partition name */
424 name_len = sizeof(gpt_e[i].partition_name)
425 / sizeof(efi_char16_t);
426 for (k = 0; k < name_len; k++)
427 gpt_e[i].partition_name[k] =
428 (efi_char16_t)(partitions[i].name[k]);
429
430 debug("%s: name: %s offset[%d]: 0x%x size[%d]: 0x%lx\n",
431 __func__, partitions[i].name, i,
432 offset, i, partitions[i].size);
433 }
434
435 return 0;
436}
437
438int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
439 char *str_guid, int parts_count)
440{
441 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
442 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
443 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
444 gpt_h->my_lba = cpu_to_le64(1);
445 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
446 gpt_h->first_usable_lba = cpu_to_le64(34);
447 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
448 gpt_h->partition_entry_lba = cpu_to_le64(2);
449 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
450 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
451 gpt_h->header_crc32 = 0;
452 gpt_h->partition_entry_array_crc32 = 0;
453
454 if (string_uuid(str_guid, gpt_h->disk_guid.b))
455 return -1;
456
457 return 0;
458}
459
460int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
461 disk_partition_t *partitions, int parts_count)
462{
463 int ret;
464
465 gpt_header *gpt_h = calloc(1, sizeof(gpt_header));
466 if (gpt_h == NULL) {
467 printf("%s: calloc failed!\n", __func__);
468 return -1;
469 }
470
471 gpt_entry *gpt_e = calloc(GPT_ENTRY_NUMBERS, sizeof(gpt_entry));
472 if (gpt_e == NULL) {
473 printf("%s: calloc failed!\n", __func__);
474 free(gpt_h);
475 return -1;
476 }
477
478 /* Generate Primary GPT header (LBA1) */
479 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
480 if (ret)
481 goto err;
482
483 /* Generate partition entries */
484 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
485 if (ret)
486 goto err;
487
488 /* Write GPT partition table */
489 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
490
491err:
492 free(gpt_e);
493 free(gpt_h);
494 return ret;
495}
496#endif
497
richardretanubun07f3d782008-09-26 11:13:22 -0400498/*
499 * Private functions
500 */
501/*
502 * pmbr_part_valid(): Check for EFI partition signature
503 *
504 * Returns: 1 if EFI GPT partition type is found.
505 */
506static int pmbr_part_valid(struct partition *part)
507{
508 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich8faefad2013-03-29 07:57:10 +0000509 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubun07f3d782008-09-26 11:13:22 -0400510 return 1;
511 }
512
513 return 0;
514}
515
516/*
517 * is_pmbr_valid(): test Protective MBR for validity
518 *
519 * Returns: 1 if PMBR is valid, 0 otherwise.
520 * Validity depends on two things:
521 * 1) MSDOS signature is in the last two bytes of the MBR
522 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
523 */
524static int is_pmbr_valid(legacy_mbr * mbr)
525{
526 int i = 0;
527
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100528 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubun07f3d782008-09-26 11:13:22 -0400529 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400530
531 for (i = 0; i < 4; i++) {
532 if (pmbr_part_valid(&mbr->partition_record[i])) {
533 return 1;
534 }
535 }
536 return 0;
537}
538
539/**
540 * is_gpt_valid() - tests one GPT header and PTEs for validity
541 *
542 * lba is the logical block address of the GPT header to test
543 * gpt is a GPT header ptr, filled on return.
544 * ptes is a PTEs ptr, filled on return.
545 *
546 * Description: returns 1 if valid, 0 on error.
547 * If valid, returns pointers to PTEs.
548 */
549static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
550 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
551{
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100552 u32 crc32_backup = 0;
553 u32 calc_crc32;
richardretanubun07f3d782008-09-26 11:13:22 -0400554 unsigned long long lastlba;
555
556 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000557 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400558 return 0;
559 }
560
561 /* Read GPT Header from device */
562 if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
563 printf("*** ERROR: Can't read GPT header ***\n");
564 return 0;
565 }
566
567 /* Check the GPT header signature */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100568 if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
richardretanubun07f3d782008-09-26 11:13:22 -0400569 printf("GUID Partition Table Header signature is wrong:"
570 "0x%llX != 0x%llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100571 le64_to_cpu(pgpt_head->signature),
572 GPT_HEADER_SIGNATURE);
richardretanubun07f3d782008-09-26 11:13:22 -0400573 return 0;
574 }
575
576 /* Check the GUID Partition Table CRC */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100577 memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
578 memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
richardretanubun07f3d782008-09-26 11:13:22 -0400579
580 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100581 le32_to_cpu(pgpt_head->header_size));
richardretanubun07f3d782008-09-26 11:13:22 -0400582
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100583 memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
richardretanubun07f3d782008-09-26 11:13:22 -0400584
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100585 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400586 printf("GUID Partition Table Header CRC is wrong:"
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100587 "0x%x != 0x%x\n",
588 le32_to_cpu(crc32_backup), calc_crc32);
richardretanubun07f3d782008-09-26 11:13:22 -0400589 return 0;
590 }
591
592 /* Check that the my_lba entry points to the LBA that contains the GPT */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100593 if (le64_to_cpu(pgpt_head->my_lba) != lba) {
richardretanubun07f3d782008-09-26 11:13:22 -0400594 printf("GPT: my_lba incorrect: %llX != %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100595 le64_to_cpu(pgpt_head->my_lba),
596 lba);
richardretanubun07f3d782008-09-26 11:13:22 -0400597 return 0;
598 }
599
600 /* Check the first_usable_lba and last_usable_lba are within the disk. */
601 lastlba = (unsigned long long)dev_desc->lba;
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100602 if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
richardretanubun07f3d782008-09-26 11:13:22 -0400603 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100604 le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400605 return 0;
606 }
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100607 if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
richardretanubun07f3d782008-09-26 11:13:22 -0400608 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100609 (u64) le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400610 return 0;
611 }
612
613 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100614 le64_to_cpu(pgpt_head->first_usable_lba),
615 le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400616
617 /* Read and allocate Partition Table Entries */
618 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
619 if (*pgpt_pte == NULL) {
620 printf("GPT: Failed to allocate memory for PTE\n");
621 return 0;
622 }
623
624 /* Check the GUID Partition Table Entry Array CRC */
625 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100626 le32_to_cpu(pgpt_head->num_partition_entries) *
627 le32_to_cpu(pgpt_head->sizeof_partition_entry));
richardretanubun07f3d782008-09-26 11:13:22 -0400628
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100629 if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400630 printf("GUID Partition Table Entry Array CRC is wrong:"
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100631 "0x%x != 0x%x\n",
632 le32_to_cpu(pgpt_head->partition_entry_array_crc32),
richardretanubun07f3d782008-09-26 11:13:22 -0400633 calc_crc32);
634
Doug Andersondeb5ca82011-10-19 09:47:31 +0000635 free(*pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400636 return 0;
637 }
638
639 /* We're done, all's well */
640 return 1;
641}
642
643/**
644 * alloc_read_gpt_entries(): reads partition entries from disk
645 * @dev_desc
646 * @gpt - GPT header
647 *
648 * Description: Returns ptes on success, NULL on error.
649 * Allocates space for PTEs based on information found in @gpt.
650 * Notes: remember to free pte when you're done!
651 */
652static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
653 gpt_header * pgpt_head)
654{
655 size_t count = 0;
656 gpt_entry *pte = NULL;
657
658 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000659 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400660 return NULL;
661 }
662
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100663 count = le32_to_cpu(pgpt_head->num_partition_entries) *
664 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubun07f3d782008-09-26 11:13:22 -0400665
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100666 debug("%s: count = %u * %u = %zu\n", __func__,
667 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
668 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
richardretanubun07f3d782008-09-26 11:13:22 -0400669
670 /* Allocate memory for PTE, remember to FREE */
671 if (count != 0) {
Sanjeev Premi29b70422011-11-16 10:20:47 -0500672 pte = memalign(ARCH_DMA_MINALIGN, count);
richardretanubun07f3d782008-09-26 11:13:22 -0400673 }
674
675 if (count == 0 || pte == NULL) {
Taylor Hutt9936be32012-10-12 14:26:09 +0000676 printf("%s: ERROR: Can't allocate 0x%zX "
677 "bytes for GPT Entries\n",
Doug Andersondf70b1c2011-10-19 08:04:46 +0000678 __func__, count);
richardretanubun07f3d782008-09-26 11:13:22 -0400679 return NULL;
680 }
681
682 /* Read GPT Entries from device */
683 if (dev_desc->block_read (dev_desc->dev,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100684 le64_to_cpu(pgpt_head->partition_entry_lba),
richardretanubun07f3d782008-09-26 11:13:22 -0400685 (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
686 != (count / GPT_BLOCK_SIZE)) {
687
688 printf("*** ERROR: Can't read GPT Entries ***\n");
689 free(pte);
690 return NULL;
691 }
692 return pte;
693}
694
695/**
696 * is_pte_valid(): validates a single Partition Table Entry
697 * @gpt_entry - Pointer to a single Partition Table Entry
698 *
699 * Description: returns 1 if valid, 0 on error.
700 */
701static int is_pte_valid(gpt_entry * pte)
702{
703 efi_guid_t unused_guid;
704
705 if (!pte) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000706 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400707 return 0;
708 }
709
710 /* Only one validation for now:
711 * The GUID Partition Type != Unused Entry (ALL-ZERO)
712 */
713 memset(unused_guid.b, 0, sizeof(unused_guid.b));
714
715 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
716 sizeof(unused_guid.b)) == 0) {
717
Doug Andersondf70b1c2011-10-19 08:04:46 +0000718 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt9936be32012-10-12 14:26:09 +0000719 (unsigned int)(uintptr_t)pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400720
721 return 0;
722 } else {
723 return 1;
724 }
725}
726#endif