blob: 612f0926b62bbceb8b35ef9fc6aa259145c35c95 [file] [log] [blame]
richardretanubun07f3d782008-09-26 11:13:22 -04001/*
2 * Copyright (C) 2008 RuggedCom, Inc.
3 * Richard Retanubun <RichardRetanubun@RuggedCom.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
richardretanubun07f3d782008-09-26 11:13:22 -04006 */
7
8/*
Steve Raee04350d2014-05-26 11:52:23 -07009 * NOTE:
10 * when CONFIG_SYS_64BIT_LBA is not defined, lbaint_t is 32 bits; this
11 * limits the maximum size of addressable storage to < 2 Terra Bytes
richardretanubun07f3d782008-09-26 11:13:22 -040012 */
Marc Dietrich8faefad2013-03-29 07:57:10 +000013#include <asm/unaligned.h>
richardretanubun07f3d782008-09-26 11:13:22 -040014#include <common.h>
15#include <command.h>
16#include <ide.h>
17#include <malloc.h>
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010018#include <part_efi.h>
Lei Wen6eecc032011-09-07 18:11:19 +000019#include <linux/ctype.h>
richardretanubun07f3d782008-09-26 11:13:22 -040020
Lukasz Majewski40684dd2012-12-11 11:09:46 +010021DECLARE_GLOBAL_DATA_PTR;
22
Stephen Warren2c1af9d2013-02-28 15:03:46 +000023#ifdef HAVE_BLOCK_DEVICE
richardretanubun07f3d782008-09-26 11:13:22 -040024/**
25 * efi_crc32() - EFI version of crc32 function
26 * @buf: buffer to calculate crc32 of
27 * @len - length of buf
28 *
29 * Description: Returns EFI-style CRC32 value for @buf
30 */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010031static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubun07f3d782008-09-26 11:13:22 -040032{
33 return crc32(0, buf, len);
34}
35
36/*
37 * Private function prototypes
38 */
39
40static int pmbr_part_valid(struct partition *part);
41static int is_pmbr_valid(legacy_mbr * mbr);
Steve Raee04350d2014-05-26 11:52:23 -070042static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
43 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -040044static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
45 gpt_header * pgpt_head);
richardretanubun07f3d782008-09-26 11:13:22 -040046static int is_pte_valid(gpt_entry * pte);
47
Lei Wen6eecc032011-09-07 18:11:19 +000048static char *print_efiname(gpt_entry *pte)
49{
50 static char name[PARTNAME_SZ + 1];
51 int i;
52 for (i = 0; i < PARTNAME_SZ; i++) {
53 u8 c;
54 c = pte->partition_name[i] & 0xff;
55 c = (c && !isprint(c)) ? '.' : c;
56 name[i] = c;
57 }
58 name[PARTNAME_SZ] = 0;
59 return name;
60}
61
Stephen Warrenb4414f42012-10-08 08:14:37 +000062static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
63
64static inline int is_bootable(gpt_entry *p)
65{
66 return p->attributes.fields.legacy_bios_bootable ||
67 !memcmp(&(p->partition_type_guid), &system_guid,
68 sizeof(efi_guid_t));
69}
70
Lukasz Majewski40684dd2012-12-11 11:09:46 +010071#ifdef CONFIG_EFI_PARTITION
Stephen Warrenf07cd2c2012-10-08 08:14:34 +000072/*
73 * Public Functions (include/part.h)
74 */
75
76void print_part_efi(block_dev_desc_t * dev_desc)
77{
Egbert Eichae1768a2013-04-09 06:03:36 +000078 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +000079 gpt_entry *gpt_pte = NULL;
80 int i = 0;
81 char uuid[37];
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020082 unsigned char *uuid_bin;
Stephen Warrenf07cd2c2012-10-08 08:14:34 +000083
84 if (!dev_desc) {
85 printf("%s: Invalid Argument(s)\n", __func__);
86 return;
87 }
88 /* This function validates AND fills in the GPT header and PTE */
89 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
90 gpt_head, &gpt_pte) != 1) {
91 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-05-05 13:00:08 -070092 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
93 gpt_head, &gpt_pte) != 1) {
94 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
95 __func__);
96 return;
97 } else {
98 printf("%s: *** Using Backup GPT ***\n",
99 __func__);
100 }
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000101 }
102
103 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
104
105 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren13bf2f52012-10-08 08:14:36 +0000106 printf("\tAttributes\n");
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200107 printf("\tType GUID\n");
108 printf("\tPartition GUID\n");
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000109
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100110 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000111 /* Stop at the first non valid PTE */
112 if (!is_pte_valid(&gpt_pte[i]))
113 break;
114
115 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100116 le64_to_cpu(gpt_pte[i].starting_lba),
117 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000118 print_efiname(&gpt_pte[i]));
Stephen Warren13bf2f52012-10-08 08:14:36 +0000119 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200120 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200121 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000122 printf("\ttype:\t%s\n", uuid);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200123 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200124 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
125 printf("\tguid:\t%s\n", uuid);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000126 }
127
128 /* Remember to free pte */
129 free(gpt_pte);
130 return;
131}
Stephen Warren894bfbb2012-09-21 09:50:59 +0000132
richardretanubun07f3d782008-09-26 11:13:22 -0400133int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
134 disk_partition_t * info)
135{
Egbert Eichae1768a2013-04-09 06:03:36 +0000136 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Andersondeb5ca82011-10-19 09:47:31 +0000137 gpt_entry *gpt_pte = NULL;
richardretanubun07f3d782008-09-26 11:13:22 -0400138
139 /* "part" argument must be at least 1 */
140 if (!dev_desc || !info || part < 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000141 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400142 return -1;
143 }
144
145 /* This function validates AND fills in the GPT header and PTE */
146 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren4715a812011-10-28 09:21:46 +0000147 gpt_head, &gpt_pte) != 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000148 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-05-05 13:00:08 -0700149 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
150 gpt_head, &gpt_pte) != 1) {
151 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
152 __func__);
153 return -1;
154 } else {
155 printf("%s: *** Using Backup GPT ***\n",
156 __func__);
157 }
richardretanubun07f3d782008-09-26 11:13:22 -0400158 }
159
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100160 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000161 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500162 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000163 __func__, part);
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500164 free(gpt_pte);
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000165 return -1;
166 }
167
Steve Raee04350d2014-05-26 11:52:23 -0700168 /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
169 info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
Richard Retanubun50970832009-01-26 08:45:14 -0500170 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Steve Raee04350d2014-05-26 11:52:23 -0700171 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
Richard Retanubun50970832009-01-26 08:45:14 -0500172 - info->start;
Egbert Eichae1768a2013-04-09 06:03:36 +0000173 info->blksz = dev_desc->blksz;
richardretanubun07f3d782008-09-26 11:13:22 -0400174
Lei Wen6eecc032011-09-07 18:11:19 +0000175 sprintf((char *)info->name, "%s",
Doug Andersondeb5ca82011-10-19 09:47:31 +0000176 print_efiname(&gpt_pte[part - 1]));
richardretanubun07f3d782008-09-26 11:13:22 -0400177 sprintf((char *)info->type, "U-Boot");
Stephen Warrenb4414f42012-10-08 08:14:37 +0000178 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000179#ifdef CONFIG_PARTITION_UUIDS
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200180 uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
181 UUID_STR_FORMAT_GUID);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000182#endif
richardretanubun07f3d782008-09-26 11:13:22 -0400183
Steve Rae60bf9412014-05-26 11:52:24 -0700184 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
Frederic Leroy04735e92013-06-26 18:11:25 +0200185 info->start, info->size, info->name);
richardretanubun07f3d782008-09-26 11:13:22 -0400186
187 /* Remember to free pte */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000188 free(gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400189 return 0;
190}
191
Steve Rae60bf9412014-05-26 11:52:24 -0700192int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc,
193 const char *name, disk_partition_t *info)
194{
195 int ret;
196 int i;
197 for (i = 1; i < GPT_ENTRY_NUMBERS; i++) {
198 ret = get_partition_info_efi(dev_desc, i, info);
199 if (ret != 0) {
200 /* no more entries in table */
201 return -1;
202 }
203 if (strcmp(name, (const char *)info->name) == 0) {
204 /* matched */
205 return 0;
206 }
207 }
208 return -2;
209}
210
richardretanubun07f3d782008-09-26 11:13:22 -0400211int test_part_efi(block_dev_desc_t * dev_desc)
212{
Egbert Eichae1768a2013-04-09 06:03:36 +0000213 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
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{
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100231 /* Setup the Protective MBR */
Hector Palacios61fcc7d2014-02-13 09:48:24 +0100232 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
233 memset(p_mbr, 0, sizeof(*p_mbr));
234
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100235 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);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100249 return -1;
250 }
251
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100252 return 0;
253}
254
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100255int write_gpt_table(block_dev_desc_t *dev_desc,
256 gpt_header *gpt_h, gpt_entry *gpt_e)
257{
Egbert Eichae1768a2013-04-09 06:03:36 +0000258 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
259 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100260 u32 calc_crc32;
261 u64 val;
262
263 debug("max lba: %x\n", (u32) dev_desc->lba);
264 /* Setup the Protective MBR */
265 if (set_protective_mbr(dev_desc) < 0)
266 goto err;
267
268 /* Generate CRC for the Primary GPT Header */
269 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
270 le32_to_cpu(gpt_h->num_partition_entries) *
271 le32_to_cpu(gpt_h->sizeof_partition_entry));
272 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
273
274 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
275 le32_to_cpu(gpt_h->header_size));
276 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
277
278 /* Write the First GPT to the block right after the Legacy MBR */
279 if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
280 goto err;
281
Egbert Eichae1768a2013-04-09 06:03:36 +0000282 if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
283 != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100284 goto err;
285
Steve Raeae95fad2014-05-05 13:00:08 -0700286 /* recalculate the values for the Backup GPT Header */
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100287 val = le64_to_cpu(gpt_h->my_lba);
288 gpt_h->my_lba = gpt_h->alternate_lba;
289 gpt_h->alternate_lba = cpu_to_le64(val);
290 gpt_h->header_crc32 = 0;
291
292 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
293 le32_to_cpu(gpt_h->header_size));
294 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
295
296 if (dev_desc->block_write(dev_desc->dev,
Steve Raee04350d2014-05-26 11:52:23 -0700297 (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
298 + 1,
Egbert Eichae1768a2013-04-09 06:03:36 +0000299 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100300 goto err;
301
302 if (dev_desc->block_write(dev_desc->dev,
Steve Raee04350d2014-05-26 11:52:23 -0700303 (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
304 gpt_h) != 1)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100305 goto err;
306
307 debug("GPT successfully written to block device!\n");
308 return 0;
309
310 err:
311 printf("** Can't write to device %d **\n", dev_desc->dev);
312 return -1;
313}
314
315int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
316 disk_partition_t *partitions, int parts)
317{
Steve Raee04350d2014-05-26 11:52:23 -0700318 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
319 lbaint_t start;
320 lbaint_t last_usable_lba = (lbaint_t)
321 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100322 int i, k;
Marek Vasut67cd4a62013-05-19 12:53:34 +0000323 size_t efiname_len, dosname_len;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100324#ifdef CONFIG_PARTITION_UUIDS
325 char *str_uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200326 unsigned char *bin_uuid;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100327#endif
328
329 for (i = 0; i < parts; i++) {
330 /* partition starting lba */
331 start = partitions[i].start;
332 if (start && (start < offset)) {
333 printf("Partition overlap\n");
334 return -1;
335 }
336 if (start) {
337 gpt_e[i].starting_lba = cpu_to_le64(start);
338 offset = start + partitions[i].size;
339 } else {
340 gpt_e[i].starting_lba = cpu_to_le64(offset);
341 offset += partitions[i].size;
342 }
Steve Raededf37b2014-05-26 11:52:22 -0700343 if (offset >= last_usable_lba) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100344 printf("Partitions layout exceds disk size\n");
345 return -1;
346 }
347 /* partition ending lba */
348 if ((i == parts - 1) && (partitions[i].size == 0))
349 /* extend the last partition to maximuim */
350 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
351 else
352 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
353
354 /* partition type GUID */
355 memcpy(gpt_e[i].partition_type_guid.b,
356 &PARTITION_BASIC_DATA_GUID, 16);
357
358#ifdef CONFIG_PARTITION_UUIDS
359 str_uuid = partitions[i].uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200360 bin_uuid = gpt_e[i].unique_partition_guid.b;
361
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200362 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100363 printf("Partition no. %d: invalid guid: %s\n",
364 i, str_uuid);
365 return -1;
366 }
367#endif
368
369 /* partition attributes */
370 memset(&gpt_e[i].attributes, 0,
371 sizeof(gpt_entry_attributes));
372
373 /* partition name */
Marek Vasut67cd4a62013-05-19 12:53:34 +0000374 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100375 / sizeof(efi_char16_t);
Marek Vasut67cd4a62013-05-19 12:53:34 +0000376 dosname_len = sizeof(partitions[i].name);
377
378 memset(gpt_e[i].partition_name, 0,
379 sizeof(gpt_e[i].partition_name));
380
381 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100382 gpt_e[i].partition_name[k] =
383 (efi_char16_t)(partitions[i].name[k]);
384
Steve Raee04350d2014-05-26 11:52:23 -0700385 debug("%s: name: %s offset[%d]: 0x" LBAF
386 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100387 __func__, partitions[i].name, i,
388 offset, i, partitions[i].size);
389 }
390
391 return 0;
392}
393
394int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
395 char *str_guid, int parts_count)
396{
397 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
398 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
399 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
400 gpt_h->my_lba = cpu_to_le64(1);
401 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
402 gpt_h->first_usable_lba = cpu_to_le64(34);
403 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
404 gpt_h->partition_entry_lba = cpu_to_le64(2);
405 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
406 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
407 gpt_h->header_crc32 = 0;
408 gpt_h->partition_entry_array_crc32 = 0;
409
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200410 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100411 return -1;
412
413 return 0;
414}
415
416int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
417 disk_partition_t *partitions, int parts_count)
418{
419 int ret;
420
Egbert Eichae1768a2013-04-09 06:03:36 +0000421 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
422 dev_desc));
423 gpt_entry *gpt_e;
424
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100425 if (gpt_h == NULL) {
426 printf("%s: calloc failed!\n", __func__);
427 return -1;
428 }
429
Egbert Eichae1768a2013-04-09 06:03:36 +0000430 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
431 * sizeof(gpt_entry),
432 dev_desc));
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100433 if (gpt_e == NULL) {
434 printf("%s: calloc failed!\n", __func__);
435 free(gpt_h);
436 return -1;
437 }
438
439 /* Generate Primary GPT header (LBA1) */
440 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
441 if (ret)
442 goto err;
443
444 /* Generate partition entries */
445 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
446 if (ret)
447 goto err;
448
449 /* Write GPT partition table */
450 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
451
452err:
453 free(gpt_e);
454 free(gpt_h);
455 return ret;
456}
457#endif
458
richardretanubun07f3d782008-09-26 11:13:22 -0400459/*
460 * Private functions
461 */
462/*
463 * pmbr_part_valid(): Check for EFI partition signature
464 *
465 * Returns: 1 if EFI GPT partition type is found.
466 */
467static int pmbr_part_valid(struct partition *part)
468{
469 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich8faefad2013-03-29 07:57:10 +0000470 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubun07f3d782008-09-26 11:13:22 -0400471 return 1;
472 }
473
474 return 0;
475}
476
477/*
478 * is_pmbr_valid(): test Protective MBR for validity
479 *
480 * Returns: 1 if PMBR is valid, 0 otherwise.
481 * Validity depends on two things:
482 * 1) MSDOS signature is in the last two bytes of the MBR
483 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
484 */
485static int is_pmbr_valid(legacy_mbr * mbr)
486{
487 int i = 0;
488
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100489 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubun07f3d782008-09-26 11:13:22 -0400490 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400491
492 for (i = 0; i < 4; i++) {
493 if (pmbr_part_valid(&mbr->partition_record[i])) {
494 return 1;
495 }
496 }
497 return 0;
498}
499
500/**
501 * is_gpt_valid() - tests one GPT header and PTEs for validity
502 *
503 * lba is the logical block address of the GPT header to test
504 * gpt is a GPT header ptr, filled on return.
505 * ptes is a PTEs ptr, filled on return.
506 *
507 * Description: returns 1 if valid, 0 on error.
508 * If valid, returns pointers to PTEs.
509 */
Steve Raee04350d2014-05-26 11:52:23 -0700510static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
511 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
richardretanubun07f3d782008-09-26 11:13:22 -0400512{
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100513 u32 crc32_backup = 0;
514 u32 calc_crc32;
Steve Raee04350d2014-05-26 11:52:23 -0700515 u64 lastlba;
richardretanubun07f3d782008-09-26 11:13:22 -0400516
517 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000518 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400519 return 0;
520 }
521
522 /* Read GPT Header from device */
Steve Raee04350d2014-05-26 11:52:23 -0700523 if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head)
524 != 1) {
richardretanubun07f3d782008-09-26 11:13:22 -0400525 printf("*** ERROR: Can't read GPT header ***\n");
526 return 0;
527 }
528
529 /* Check the GPT header signature */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100530 if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
richardretanubun07f3d782008-09-26 11:13:22 -0400531 printf("GUID Partition Table Header signature is wrong:"
532 "0x%llX != 0x%llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100533 le64_to_cpu(pgpt_head->signature),
534 GPT_HEADER_SIGNATURE);
richardretanubun07f3d782008-09-26 11:13:22 -0400535 return 0;
536 }
537
538 /* Check the GUID Partition Table CRC */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100539 memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
540 memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
richardretanubun07f3d782008-09-26 11:13:22 -0400541
542 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100543 le32_to_cpu(pgpt_head->header_size));
richardretanubun07f3d782008-09-26 11:13:22 -0400544
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100545 memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
richardretanubun07f3d782008-09-26 11:13:22 -0400546
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100547 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400548 printf("GUID Partition Table Header CRC is wrong:"
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100549 "0x%x != 0x%x\n",
550 le32_to_cpu(crc32_backup), calc_crc32);
richardretanubun07f3d782008-09-26 11:13:22 -0400551 return 0;
552 }
553
554 /* Check that the my_lba entry points to the LBA that contains the GPT */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100555 if (le64_to_cpu(pgpt_head->my_lba) != lba) {
richardretanubun07f3d782008-09-26 11:13:22 -0400556 printf("GPT: my_lba incorrect: %llX != %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100557 le64_to_cpu(pgpt_head->my_lba),
558 lba);
richardretanubun07f3d782008-09-26 11:13:22 -0400559 return 0;
560 }
561
562 /* Check the first_usable_lba and last_usable_lba are within the disk. */
Steve Raee04350d2014-05-26 11:52:23 -0700563 lastlba = (u64)dev_desc->lba;
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100564 if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
richardretanubun07f3d782008-09-26 11:13:22 -0400565 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100566 le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400567 return 0;
568 }
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100569 if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
richardretanubun07f3d782008-09-26 11:13:22 -0400570 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
Steve Raee04350d2014-05-26 11:52:23 -0700571 le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400572 return 0;
573 }
574
575 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100576 le64_to_cpu(pgpt_head->first_usable_lba),
577 le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400578
579 /* Read and allocate Partition Table Entries */
580 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
581 if (*pgpt_pte == NULL) {
582 printf("GPT: Failed to allocate memory for PTE\n");
583 return 0;
584 }
585
586 /* Check the GUID Partition Table Entry Array CRC */
587 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100588 le32_to_cpu(pgpt_head->num_partition_entries) *
589 le32_to_cpu(pgpt_head->sizeof_partition_entry));
richardretanubun07f3d782008-09-26 11:13:22 -0400590
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100591 if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400592 printf("GUID Partition Table Entry Array CRC is wrong:"
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100593 "0x%x != 0x%x\n",
594 le32_to_cpu(pgpt_head->partition_entry_array_crc32),
richardretanubun07f3d782008-09-26 11:13:22 -0400595 calc_crc32);
596
Doug Andersondeb5ca82011-10-19 09:47:31 +0000597 free(*pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400598 return 0;
599 }
600
601 /* We're done, all's well */
602 return 1;
603}
604
605/**
606 * alloc_read_gpt_entries(): reads partition entries from disk
607 * @dev_desc
608 * @gpt - GPT header
609 *
610 * Description: Returns ptes on success, NULL on error.
611 * Allocates space for PTEs based on information found in @gpt.
612 * Notes: remember to free pte when you're done!
613 */
614static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
615 gpt_header * pgpt_head)
616{
Egbert Eichae1768a2013-04-09 06:03:36 +0000617 size_t count = 0, blk_cnt;
richardretanubun07f3d782008-09-26 11:13:22 -0400618 gpt_entry *pte = NULL;
619
620 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000621 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400622 return NULL;
623 }
624
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100625 count = le32_to_cpu(pgpt_head->num_partition_entries) *
626 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubun07f3d782008-09-26 11:13:22 -0400627
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100628 debug("%s: count = %u * %u = %zu\n", __func__,
629 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
630 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
richardretanubun07f3d782008-09-26 11:13:22 -0400631
632 /* Allocate memory for PTE, remember to FREE */
633 if (count != 0) {
Egbert Eichae1768a2013-04-09 06:03:36 +0000634 pte = memalign(ARCH_DMA_MINALIGN,
635 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubun07f3d782008-09-26 11:13:22 -0400636 }
637
638 if (count == 0 || pte == NULL) {
Taylor Hutt9936be32012-10-12 14:26:09 +0000639 printf("%s: ERROR: Can't allocate 0x%zX "
640 "bytes for GPT Entries\n",
Doug Andersondf70b1c2011-10-19 08:04:46 +0000641 __func__, count);
richardretanubun07f3d782008-09-26 11:13:22 -0400642 return NULL;
643 }
644
645 /* Read GPT Entries from device */
Egbert Eichae1768a2013-04-09 06:03:36 +0000646 blk_cnt = BLOCK_CNT(count, dev_desc);
richardretanubun07f3d782008-09-26 11:13:22 -0400647 if (dev_desc->block_read (dev_desc->dev,
Steve Raee04350d2014-05-26 11:52:23 -0700648 (lbaint_t)le64_to_cpu(pgpt_head->partition_entry_lba),
Egbert Eichae1768a2013-04-09 06:03:36 +0000649 (lbaint_t) (blk_cnt), pte)
650 != blk_cnt) {
richardretanubun07f3d782008-09-26 11:13:22 -0400651
652 printf("*** ERROR: Can't read GPT Entries ***\n");
653 free(pte);
654 return NULL;
655 }
656 return pte;
657}
658
659/**
660 * is_pte_valid(): validates a single Partition Table Entry
661 * @gpt_entry - Pointer to a single Partition Table Entry
662 *
663 * Description: returns 1 if valid, 0 on error.
664 */
665static int is_pte_valid(gpt_entry * pte)
666{
667 efi_guid_t unused_guid;
668
669 if (!pte) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000670 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400671 return 0;
672 }
673
674 /* Only one validation for now:
675 * The GUID Partition Type != Unused Entry (ALL-ZERO)
676 */
677 memset(unused_guid.b, 0, sizeof(unused_guid.b));
678
679 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
680 sizeof(unused_guid.b)) == 0) {
681
Doug Andersondf70b1c2011-10-19 08:04:46 +0000682 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt9936be32012-10-12 14:26:09 +0000683 (unsigned int)(uintptr_t)pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400684
685 return 0;
686 } else {
687 return 1;
688 }
689}
690#endif