blob: 733d5bde94ed416e0831d326a0e865c56edc02b8 [file] [log] [blame]
richardretanubune6745592008-09-26 11:13:22 -04001/*
2 * Copyright (C) 2008 RuggedCom, Inc.
3 * Richard Retanubun <RichardRetanubun@RuggedCom.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
richardretanubune6745592008-09-26 11:13:22 -04006 */
7
8/*
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02009 * Problems with CONFIG_SYS_64BIT_LBA:
richardretanubune6745592008-09-26 11:13:22 -040010 *
11 * struct disk_partition.start in include/part.h is sized as ulong.
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020012 * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t.
richardretanubune6745592008-09-26 11:13:22 -040013 * For now, it is cast back to ulong at assignment.
14 *
15 * This limits the maximum size of addressable storage to < 2 Terra Bytes
16 */
Marc Dietrich20ca3ad2013-03-29 07:57:10 +000017#include <asm/unaligned.h>
richardretanubune6745592008-09-26 11:13:22 -040018#include <common.h>
19#include <command.h>
20#include <ide.h>
21#include <malloc.h>
Chang Hyun Park823ffde2012-12-11 11:09:45 +010022#include <part_efi.h>
Lei Wen757ddfe2011-09-07 18:11:19 +000023#include <linux/ctype.h>
richardretanubune6745592008-09-26 11:13:22 -040024
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +010025DECLARE_GLOBAL_DATA_PTR;
26
Stephen Warren8ec730d2013-02-28 15:03:46 +000027#ifdef HAVE_BLOCK_DEVICE
richardretanubune6745592008-09-26 11:13:22 -040028/**
29 * efi_crc32() - EFI version of crc32 function
30 * @buf: buffer to calculate crc32 of
31 * @len - length of buf
32 *
33 * Description: Returns EFI-style CRC32 value for @buf
34 */
Chang Hyun Park823ffde2012-12-11 11:09:45 +010035static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubune6745592008-09-26 11:13:22 -040036{
37 return crc32(0, buf, len);
38}
39
40/*
41 * Private function prototypes
42 */
43
44static int pmbr_part_valid(struct partition *part);
45static int is_pmbr_valid(legacy_mbr * mbr);
richardretanubune6745592008-09-26 11:13:22 -040046static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
47 gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -040048static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
49 gpt_header * pgpt_head);
richardretanubune6745592008-09-26 11:13:22 -040050static int is_pte_valid(gpt_entry * pte);
51
Lei Wen757ddfe2011-09-07 18:11:19 +000052static char *print_efiname(gpt_entry *pte)
53{
54 static char name[PARTNAME_SZ + 1];
55 int i;
56 for (i = 0; i < PARTNAME_SZ; i++) {
57 u8 c;
58 c = pte->partition_name[i] & 0xff;
59 c = (c && !isprint(c)) ? '.' : c;
60 name[i] = c;
61 }
62 name[PARTNAME_SZ] = 0;
63 return name;
64}
65
Stephen Warren8b855b52012-10-08 08:14:34 +000066static void uuid_string(unsigned char *uuid, char *str)
67{
68 static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
69 12, 13, 14, 15};
70 int i;
71
72 for (i = 0; i < 16; i++) {
73 sprintf(str, "%02x", uuid[le[i]]);
74 str += 2;
75 switch (i) {
76 case 3:
77 case 5:
78 case 7:
79 case 9:
80 *str++ = '-';
81 break;
82 }
83 }
84}
85
Stephen Warrend0cf3b62012-10-08 08:14:37 +000086static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
87
88static inline int is_bootable(gpt_entry *p)
89{
90 return p->attributes.fields.legacy_bios_bootable ||
91 !memcmp(&(p->partition_type_guid), &system_guid,
92 sizeof(efi_guid_t));
93}
94
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +010095#ifdef CONFIG_EFI_PARTITION
richardretanubune6745592008-09-26 11:13:22 -040096/*
97 * Public Functions (include/part.h)
98 */
99
100void print_part_efi(block_dev_desc_t * dev_desc)
101{
Egbert Eich071e4232013-04-09 06:03:36 +0000102 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000103 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400104 int i = 0;
Stephen Warren8b855b52012-10-08 08:14:34 +0000105 char uuid[37];
richardretanubune6745592008-09-26 11:13:22 -0400106
107 if (!dev_desc) {
Doug Andersondc036672011-10-19 08:04:46 +0000108 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400109 return;
110 }
111 /* This function validates AND fills in the GPT header and PTE */
112 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren30f39de2011-10-28 09:21:46 +0000113 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000114 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400115 return;
116 }
117
Doug Anderson698a51d2011-10-19 09:47:31 +0000118 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400119
Stephen Warren26163262012-10-08 08:14:33 +0000120 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000121 printf("\tAttributes\n");
Stephen Warren8b855b52012-10-08 08:14:34 +0000122 printf("\tType UUID\n");
123 printf("\tPartition UUID\n");
124
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100125 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrene6df59e2012-10-08 08:14:32 +0000126 /* Stop at the first non valid PTE */
127 if (!is_pte_valid(&gpt_pte[i]))
128 break;
richardretanubune6745592008-09-26 11:13:22 -0400129
Stephen Warren26163262012-10-08 08:14:33 +0000130 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100131 le64_to_cpu(gpt_pte[i].starting_lba),
132 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warren26163262012-10-08 08:14:33 +0000133 print_efiname(&gpt_pte[i]));
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000134 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Stephen Warren8b855b52012-10-08 08:14:34 +0000135 uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
136 printf("\ttype:\t%s\n", uuid);
137 uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
138 printf("\tuuid:\t%s\n", uuid);
richardretanubune6745592008-09-26 11:13:22 -0400139 }
140
141 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000142 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400143 return;
144}
145
146int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
147 disk_partition_t * info)
148{
Egbert Eich071e4232013-04-09 06:03:36 +0000149 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000150 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400151
152 /* "part" argument must be at least 1 */
153 if (!dev_desc || !info || part < 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000154 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400155 return -1;
156 }
157
158 /* This function validates AND fills in the GPT header and PTE */
159 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren30f39de2011-10-28 09:21:46 +0000160 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000161 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400162 return -1;
163 }
164
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100165 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrene47dbba2012-09-21 09:50:58 +0000166 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorfe7775312013-09-10 15:14:56 -0500167 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrene47dbba2012-09-21 09:50:58 +0000168 __func__, part);
Mark Langsdorfe7775312013-09-10 15:14:56 -0500169 free(gpt_pte);
Stephen Warrene47dbba2012-09-21 09:50:58 +0000170 return -1;
171 }
172
richardretanubune6745592008-09-26 11:13:22 -0400173 /* The ulong casting limits the maximum disk size to 2 TB */
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100174 info->start = (u64)le64_to_cpu(gpt_pte[part - 1].starting_lba);
Richard Retanubun413dfc72009-01-26 08:45:14 -0500175 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100176 info->size = ((u64)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1)
Richard Retanubun413dfc72009-01-26 08:45:14 -0500177 - info->start;
Egbert Eich071e4232013-04-09 06:03:36 +0000178 info->blksz = dev_desc->blksz;
richardretanubune6745592008-09-26 11:13:22 -0400179
Lei Wen757ddfe2011-09-07 18:11:19 +0000180 sprintf((char *)info->name, "%s",
Doug Anderson698a51d2011-10-19 09:47:31 +0000181 print_efiname(&gpt_pte[part - 1]));
richardretanubune6745592008-09-26 11:13:22 -0400182 sprintf((char *)info->type, "U-Boot");
Stephen Warrend0cf3b62012-10-08 08:14:37 +0000183 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000184#ifdef CONFIG_PARTITION_UUIDS
185 uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
186#endif
richardretanubune6745592008-09-26 11:13:22 -0400187
Frederic Leroye7ee0282013-06-26 18:11:25 +0200188 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s", __func__,
189 info->start, info->size, info->name);
richardretanubune6745592008-09-26 11:13:22 -0400190
191 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000192 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400193 return 0;
194}
195
196int test_part_efi(block_dev_desc_t * dev_desc)
197{
Egbert Eich071e4232013-04-09 06:03:36 +0000198 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubune6745592008-09-26 11:13:22 -0400199
200 /* Read legacy MBR from block 0 and validate it */
Anton staaf3b3df4e2011-10-12 13:56:04 +0000201 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
202 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubune6745592008-09-26 11:13:22 -0400203 return -1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100204 }
205 return 0;
206}
207
208/**
209 * set_protective_mbr(): Set the EFI protective MBR
210 * @param dev_desc - block device descriptor
211 *
212 * @return - zero on success, otherwise error
213 */
214static int set_protective_mbr(block_dev_desc_t *dev_desc)
215{
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100216 /* Setup the Protective MBR */
Hector Palaciosb0a54ff2014-02-13 09:48:24 +0100217 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
218 memset(p_mbr, 0, sizeof(*p_mbr));
219
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100220 if (p_mbr == NULL) {
221 printf("%s: calloc failed!\n", __func__);
222 return -1;
223 }
224 /* Append signature */
225 p_mbr->signature = MSDOS_MBR_SIGNATURE;
226 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
227 p_mbr->partition_record[0].start_sect = 1;
228 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba;
229
230 /* Write MBR sector to the MMC device */
231 if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
232 printf("** Can't write to device %d **\n",
233 dev_desc->dev);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100234 return -1;
235 }
236
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100237 return 0;
238}
239
240/**
241 * string_uuid(); Convert UUID stored as string to bytes
242 *
243 * @param uuid - UUID represented as string
244 * @param dst - GUID buffer
245 *
246 * @return return 0 on successful conversion
247 */
248static int string_uuid(char *uuid, u8 *dst)
249{
250 efi_guid_t guid;
251 u16 b, c, d;
252 u64 e;
253 u32 a;
254 u8 *p;
255 u8 i;
256
257 const u8 uuid_str_len = 36;
258
259 /* The UUID is written in text: */
260 /* 1 9 14 19 24 */
261 /* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */
262
263 debug("%s: uuid: %s\n", __func__, uuid);
264
265 if (strlen(uuid) != uuid_str_len)
266 return -1;
267
268 for (i = 0; i < uuid_str_len; i++) {
269 if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
270 if (uuid[i] != '-')
271 return -1;
272 } else {
273 if (!isxdigit(uuid[i]))
274 return -1;
275 }
richardretanubune6745592008-09-26 11:13:22 -0400276 }
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100277
278 a = (u32)simple_strtoul(uuid, NULL, 16);
279 b = (u16)simple_strtoul(uuid + 9, NULL, 16);
280 c = (u16)simple_strtoul(uuid + 14, NULL, 16);
281 d = (u16)simple_strtoul(uuid + 19, NULL, 16);
282 e = (u64)simple_strtoull(uuid + 24, NULL, 16);
283
284 p = (u8 *) &e;
285 guid = EFI_GUID(a, b, c, d >> 8, d & 0xFF,
286 *(p + 5), *(p + 4), *(p + 3),
287 *(p + 2), *(p + 1) , *p);
288
289 memcpy(dst, guid.b, sizeof(efi_guid_t));
290
richardretanubune6745592008-09-26 11:13:22 -0400291 return 0;
292}
293
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100294int write_gpt_table(block_dev_desc_t *dev_desc,
295 gpt_header *gpt_h, gpt_entry *gpt_e)
296{
Egbert Eich071e4232013-04-09 06:03:36 +0000297 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
298 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100299 u32 calc_crc32;
300 u64 val;
301
302 debug("max lba: %x\n", (u32) dev_desc->lba);
303 /* Setup the Protective MBR */
304 if (set_protective_mbr(dev_desc) < 0)
305 goto err;
306
307 /* Generate CRC for the Primary GPT Header */
308 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
309 le32_to_cpu(gpt_h->num_partition_entries) *
310 le32_to_cpu(gpt_h->sizeof_partition_entry));
311 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
312
313 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
314 le32_to_cpu(gpt_h->header_size));
315 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
316
317 /* Write the First GPT to the block right after the Legacy MBR */
318 if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
319 goto err;
320
Egbert Eich071e4232013-04-09 06:03:36 +0000321 if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
322 != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100323 goto err;
324
325 /* recalculate the values for the Second GPT Header */
326 val = le64_to_cpu(gpt_h->my_lba);
327 gpt_h->my_lba = gpt_h->alternate_lba;
328 gpt_h->alternate_lba = cpu_to_le64(val);
329 gpt_h->header_crc32 = 0;
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 if (dev_desc->block_write(dev_desc->dev,
336 le32_to_cpu(gpt_h->last_usable_lba + 1),
Egbert Eich071e4232013-04-09 06:03:36 +0000337 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100338 goto err;
339
340 if (dev_desc->block_write(dev_desc->dev,
341 le32_to_cpu(gpt_h->my_lba), 1, gpt_h) != 1)
342 goto err;
343
344 debug("GPT successfully written to block device!\n");
345 return 0;
346
347 err:
348 printf("** Can't write to device %d **\n", dev_desc->dev);
349 return -1;
350}
351
352int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
353 disk_partition_t *partitions, int parts)
354{
355 u32 offset = (u32)le32_to_cpu(gpt_h->first_usable_lba);
356 ulong start;
357 int i, k;
Marek Vasut941579e2013-05-19 12:53:34 +0000358 size_t efiname_len, dosname_len;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100359#ifdef CONFIG_PARTITION_UUIDS
360 char *str_uuid;
361#endif
362
363 for (i = 0; i < parts; i++) {
364 /* partition starting lba */
365 start = partitions[i].start;
366 if (start && (start < offset)) {
367 printf("Partition overlap\n");
368 return -1;
369 }
370 if (start) {
371 gpt_e[i].starting_lba = cpu_to_le64(start);
372 offset = start + partitions[i].size;
373 } else {
374 gpt_e[i].starting_lba = cpu_to_le64(offset);
375 offset += partitions[i].size;
376 }
377 if (offset >= gpt_h->last_usable_lba) {
378 printf("Partitions layout exceds disk size\n");
379 return -1;
380 }
381 /* partition ending lba */
382 if ((i == parts - 1) && (partitions[i].size == 0))
383 /* extend the last partition to maximuim */
384 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
385 else
386 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
387
388 /* partition type GUID */
389 memcpy(gpt_e[i].partition_type_guid.b,
390 &PARTITION_BASIC_DATA_GUID, 16);
391
392#ifdef CONFIG_PARTITION_UUIDS
393 str_uuid = partitions[i].uuid;
394 if (string_uuid(str_uuid, gpt_e[i].unique_partition_guid.b)) {
395 printf("Partition no. %d: invalid guid: %s\n",
396 i, str_uuid);
397 return -1;
398 }
399#endif
400
401 /* partition attributes */
402 memset(&gpt_e[i].attributes, 0,
403 sizeof(gpt_entry_attributes));
404
405 /* partition name */
Marek Vasut941579e2013-05-19 12:53:34 +0000406 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100407 / sizeof(efi_char16_t);
Marek Vasut941579e2013-05-19 12:53:34 +0000408 dosname_len = sizeof(partitions[i].name);
409
410 memset(gpt_e[i].partition_name, 0,
411 sizeof(gpt_e[i].partition_name));
412
413 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100414 gpt_e[i].partition_name[k] =
415 (efi_char16_t)(partitions[i].name[k]);
416
Frederic Leroye7ee0282013-06-26 18:11:25 +0200417 debug("%s: name: %s offset[%d]: 0x%x size[%d]: 0x" LBAF "\n",
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100418 __func__, partitions[i].name, i,
419 offset, i, partitions[i].size);
420 }
421
422 return 0;
423}
424
425int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
426 char *str_guid, int parts_count)
427{
428 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
429 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
430 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
431 gpt_h->my_lba = cpu_to_le64(1);
432 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
433 gpt_h->first_usable_lba = cpu_to_le64(34);
434 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
435 gpt_h->partition_entry_lba = cpu_to_le64(2);
436 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
437 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
438 gpt_h->header_crc32 = 0;
439 gpt_h->partition_entry_array_crc32 = 0;
440
441 if (string_uuid(str_guid, gpt_h->disk_guid.b))
442 return -1;
443
444 return 0;
445}
446
447int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
448 disk_partition_t *partitions, int parts_count)
449{
450 int ret;
451
Egbert Eich071e4232013-04-09 06:03:36 +0000452 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
453 dev_desc));
454 gpt_entry *gpt_e;
455
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100456 if (gpt_h == NULL) {
457 printf("%s: calloc failed!\n", __func__);
458 return -1;
459 }
460
Egbert Eich071e4232013-04-09 06:03:36 +0000461 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
462 * sizeof(gpt_entry),
463 dev_desc));
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100464 if (gpt_e == NULL) {
465 printf("%s: calloc failed!\n", __func__);
466 free(gpt_h);
467 return -1;
468 }
469
470 /* Generate Primary GPT header (LBA1) */
471 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
472 if (ret)
473 goto err;
474
475 /* Generate partition entries */
476 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
477 if (ret)
478 goto err;
479
480 /* Write GPT partition table */
481 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
482
483err:
484 free(gpt_e);
485 free(gpt_h);
486 return ret;
487}
488#endif
489
richardretanubune6745592008-09-26 11:13:22 -0400490/*
491 * Private functions
492 */
493/*
494 * pmbr_part_valid(): Check for EFI partition signature
495 *
496 * Returns: 1 if EFI GPT partition type is found.
497 */
498static int pmbr_part_valid(struct partition *part)
499{
500 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich20ca3ad2013-03-29 07:57:10 +0000501 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubune6745592008-09-26 11:13:22 -0400502 return 1;
503 }
504
505 return 0;
506}
507
508/*
509 * is_pmbr_valid(): test Protective MBR for validity
510 *
511 * Returns: 1 if PMBR is valid, 0 otherwise.
512 * Validity depends on two things:
513 * 1) MSDOS signature is in the last two bytes of the MBR
514 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
515 */
516static int is_pmbr_valid(legacy_mbr * mbr)
517{
518 int i = 0;
519
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100520 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubune6745592008-09-26 11:13:22 -0400521 return 0;
richardretanubune6745592008-09-26 11:13:22 -0400522
523 for (i = 0; i < 4; i++) {
524 if (pmbr_part_valid(&mbr->partition_record[i])) {
525 return 1;
526 }
527 }
528 return 0;
529}
530
531/**
532 * is_gpt_valid() - tests one GPT header and PTEs for validity
533 *
534 * lba is the logical block address of the GPT header to test
535 * gpt is a GPT header ptr, filled on return.
536 * ptes is a PTEs ptr, filled on return.
537 *
538 * Description: returns 1 if valid, 0 on error.
539 * If valid, returns pointers to PTEs.
540 */
541static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
542 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
543{
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100544 u32 crc32_backup = 0;
545 u32 calc_crc32;
richardretanubune6745592008-09-26 11:13:22 -0400546 unsigned long long lastlba;
547
548 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000549 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400550 return 0;
551 }
552
553 /* Read GPT Header from device */
554 if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
555 printf("*** ERROR: Can't read GPT header ***\n");
556 return 0;
557 }
558
559 /* Check the GPT header signature */
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100560 if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
richardretanubune6745592008-09-26 11:13:22 -0400561 printf("GUID Partition Table Header signature is wrong:"
562 "0x%llX != 0x%llX\n",
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100563 le64_to_cpu(pgpt_head->signature),
564 GPT_HEADER_SIGNATURE);
richardretanubune6745592008-09-26 11:13:22 -0400565 return 0;
566 }
567
568 /* Check the GUID Partition Table CRC */
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100569 memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
570 memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
richardretanubune6745592008-09-26 11:13:22 -0400571
572 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100573 le32_to_cpu(pgpt_head->header_size));
richardretanubune6745592008-09-26 11:13:22 -0400574
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100575 memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
richardretanubune6745592008-09-26 11:13:22 -0400576
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100577 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
richardretanubune6745592008-09-26 11:13:22 -0400578 printf("GUID Partition Table Header CRC is wrong:"
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100579 "0x%x != 0x%x\n",
580 le32_to_cpu(crc32_backup), calc_crc32);
richardretanubune6745592008-09-26 11:13:22 -0400581 return 0;
582 }
583
584 /* Check that the my_lba entry points to the LBA that contains the GPT */
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100585 if (le64_to_cpu(pgpt_head->my_lba) != lba) {
richardretanubune6745592008-09-26 11:13:22 -0400586 printf("GPT: my_lba incorrect: %llX != %llX\n",
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100587 le64_to_cpu(pgpt_head->my_lba),
588 lba);
richardretanubune6745592008-09-26 11:13:22 -0400589 return 0;
590 }
591
592 /* Check the first_usable_lba and last_usable_lba are within the disk. */
593 lastlba = (unsigned long long)dev_desc->lba;
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100594 if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
richardretanubune6745592008-09-26 11:13:22 -0400595 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100596 le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
richardretanubune6745592008-09-26 11:13:22 -0400597 return 0;
598 }
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100599 if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
richardretanubune6745592008-09-26 11:13:22 -0400600 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100601 (u64) le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubune6745592008-09-26 11:13:22 -0400602 return 0;
603 }
604
605 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100606 le64_to_cpu(pgpt_head->first_usable_lba),
607 le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubune6745592008-09-26 11:13:22 -0400608
609 /* Read and allocate Partition Table Entries */
610 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
611 if (*pgpt_pte == NULL) {
612 printf("GPT: Failed to allocate memory for PTE\n");
613 return 0;
614 }
615
616 /* Check the GUID Partition Table Entry Array CRC */
617 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100618 le32_to_cpu(pgpt_head->num_partition_entries) *
619 le32_to_cpu(pgpt_head->sizeof_partition_entry));
richardretanubune6745592008-09-26 11:13:22 -0400620
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100621 if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
richardretanubune6745592008-09-26 11:13:22 -0400622 printf("GUID Partition Table Entry Array CRC is wrong:"
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100623 "0x%x != 0x%x\n",
624 le32_to_cpu(pgpt_head->partition_entry_array_crc32),
richardretanubune6745592008-09-26 11:13:22 -0400625 calc_crc32);
626
Doug Anderson698a51d2011-10-19 09:47:31 +0000627 free(*pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400628 return 0;
629 }
630
631 /* We're done, all's well */
632 return 1;
633}
634
635/**
636 * alloc_read_gpt_entries(): reads partition entries from disk
637 * @dev_desc
638 * @gpt - GPT header
639 *
640 * Description: Returns ptes on success, NULL on error.
641 * Allocates space for PTEs based on information found in @gpt.
642 * Notes: remember to free pte when you're done!
643 */
644static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
645 gpt_header * pgpt_head)
646{
Egbert Eich071e4232013-04-09 06:03:36 +0000647 size_t count = 0, blk_cnt;
richardretanubune6745592008-09-26 11:13:22 -0400648 gpt_entry *pte = NULL;
649
650 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000651 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400652 return NULL;
653 }
654
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100655 count = le32_to_cpu(pgpt_head->num_partition_entries) *
656 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubune6745592008-09-26 11:13:22 -0400657
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100658 debug("%s: count = %u * %u = %zu\n", __func__,
659 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
660 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
richardretanubune6745592008-09-26 11:13:22 -0400661
662 /* Allocate memory for PTE, remember to FREE */
663 if (count != 0) {
Egbert Eich071e4232013-04-09 06:03:36 +0000664 pte = memalign(ARCH_DMA_MINALIGN,
665 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubune6745592008-09-26 11:13:22 -0400666 }
667
668 if (count == 0 || pte == NULL) {
Taylor Hutt864074d2012-10-12 14:26:09 +0000669 printf("%s: ERROR: Can't allocate 0x%zX "
670 "bytes for GPT Entries\n",
Doug Andersondc036672011-10-19 08:04:46 +0000671 __func__, count);
richardretanubune6745592008-09-26 11:13:22 -0400672 return NULL;
673 }
674
675 /* Read GPT Entries from device */
Egbert Eich071e4232013-04-09 06:03:36 +0000676 blk_cnt = BLOCK_CNT(count, dev_desc);
richardretanubune6745592008-09-26 11:13:22 -0400677 if (dev_desc->block_read (dev_desc->dev,
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100678 le64_to_cpu(pgpt_head->partition_entry_lba),
Egbert Eich071e4232013-04-09 06:03:36 +0000679 (lbaint_t) (blk_cnt), pte)
680 != blk_cnt) {
richardretanubune6745592008-09-26 11:13:22 -0400681
682 printf("*** ERROR: Can't read GPT Entries ***\n");
683 free(pte);
684 return NULL;
685 }
686 return pte;
687}
688
689/**
690 * is_pte_valid(): validates a single Partition Table Entry
691 * @gpt_entry - Pointer to a single Partition Table Entry
692 *
693 * Description: returns 1 if valid, 0 on error.
694 */
695static int is_pte_valid(gpt_entry * pte)
696{
697 efi_guid_t unused_guid;
698
699 if (!pte) {
Doug Andersondc036672011-10-19 08:04:46 +0000700 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400701 return 0;
702 }
703
704 /* Only one validation for now:
705 * The GUID Partition Type != Unused Entry (ALL-ZERO)
706 */
707 memset(unused_guid.b, 0, sizeof(unused_guid.b));
708
709 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
710 sizeof(unused_guid.b)) == 0) {
711
Doug Andersondc036672011-10-19 08:04:46 +0000712 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt864074d2012-10-12 14:26:09 +0000713 (unsigned int)(uintptr_t)pte);
richardretanubune6745592008-09-26 11:13:22 -0400714
715 return 0;
716 } else {
717 return 1;
718 }
719}
720#endif