blob: efed58f81dac27ddcd1498a28ca8b80673138bda [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>
Simon Glasse48f3742014-11-11 12:47:07 -070017#include <inttypes.h>
richardretanubun07f3d782008-09-26 11:13:22 -040018#include <malloc.h>
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010019#include <part_efi.h>
Lei Wen6eecc032011-09-07 18:11:19 +000020#include <linux/ctype.h>
richardretanubun07f3d782008-09-26 11:13:22 -040021
Lukasz Majewski40684dd2012-12-11 11:09:46 +010022DECLARE_GLOBAL_DATA_PTR;
23
Stephen Warren2c1af9d2013-02-28 15:03:46 +000024#ifdef HAVE_BLOCK_DEVICE
richardretanubun07f3d782008-09-26 11:13:22 -040025/**
26 * efi_crc32() - EFI version of crc32 function
27 * @buf: buffer to calculate crc32 of
28 * @len - length of buf
29 *
30 * Description: Returns EFI-style CRC32 value for @buf
31 */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010032static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubun07f3d782008-09-26 11:13:22 -040033{
34 return crc32(0, buf, len);
35}
36
37/*
38 * Private function prototypes
39 */
40
41static int pmbr_part_valid(struct partition *part);
42static int is_pmbr_valid(legacy_mbr * mbr);
Steve Raee04350d2014-05-26 11:52:23 -070043static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
44 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -040045static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
46 gpt_header * pgpt_head);
richardretanubun07f3d782008-09-26 11:13:22 -040047static int is_pte_valid(gpt_entry * pte);
48
Lei Wen6eecc032011-09-07 18:11:19 +000049static char *print_efiname(gpt_entry *pte)
50{
51 static char name[PARTNAME_SZ + 1];
52 int i;
53 for (i = 0; i < PARTNAME_SZ; i++) {
54 u8 c;
55 c = pte->partition_name[i] & 0xff;
56 c = (c && !isprint(c)) ? '.' : c;
57 name[i] = c;
58 }
59 name[PARTNAME_SZ] = 0;
60 return name;
61}
62
Stephen Warrenb4414f42012-10-08 08:14:37 +000063static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
64
65static inline int is_bootable(gpt_entry *p)
66{
67 return p->attributes.fields.legacy_bios_bootable ||
68 !memcmp(&(p->partition_type_guid), &system_guid,
69 sizeof(efi_guid_t));
70}
71
Lukasz Majewski40684dd2012-12-11 11:09:46 +010072#ifdef CONFIG_EFI_PARTITION
Stephen Warrenf07cd2c2012-10-08 08:14:34 +000073/*
74 * Public Functions (include/part.h)
75 */
76
77void print_part_efi(block_dev_desc_t * dev_desc)
78{
Egbert Eichae1768a2013-04-09 06:03:36 +000079 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +000080 gpt_entry *gpt_pte = NULL;
81 int i = 0;
82 char uuid[37];
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020083 unsigned char *uuid_bin;
Stephen Warrenf07cd2c2012-10-08 08:14:34 +000084
85 if (!dev_desc) {
86 printf("%s: Invalid Argument(s)\n", __func__);
87 return;
88 }
89 /* This function validates AND fills in the GPT header and PTE */
90 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
91 gpt_head, &gpt_pte) != 1) {
92 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-05-05 13:00:08 -070093 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
94 gpt_head, &gpt_pte) != 1) {
95 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
96 __func__);
97 return;
98 } else {
99 printf("%s: *** Using Backup GPT ***\n",
100 __func__);
101 }
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000102 }
103
104 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
105
106 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren13bf2f52012-10-08 08:14:36 +0000107 printf("\tAttributes\n");
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200108 printf("\tType GUID\n");
109 printf("\tPartition GUID\n");
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000110
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100111 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000112 /* Stop at the first non valid PTE */
113 if (!is_pte_valid(&gpt_pte[i]))
114 break;
115
116 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100117 le64_to_cpu(gpt_pte[i].starting_lba),
118 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000119 print_efiname(&gpt_pte[i]));
Stephen Warren13bf2f52012-10-08 08:14:36 +0000120 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200121 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200122 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000123 printf("\ttype:\t%s\n", uuid);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200124 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200125 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
126 printf("\tguid:\t%s\n", uuid);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000127 }
128
129 /* Remember to free pte */
130 free(gpt_pte);
131 return;
132}
Stephen Warren894bfbb2012-09-21 09:50:59 +0000133
richardretanubun07f3d782008-09-26 11:13:22 -0400134int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
135 disk_partition_t * info)
136{
Egbert Eichae1768a2013-04-09 06:03:36 +0000137 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Andersondeb5ca82011-10-19 09:47:31 +0000138 gpt_entry *gpt_pte = NULL;
richardretanubun07f3d782008-09-26 11:13:22 -0400139
140 /* "part" argument must be at least 1 */
141 if (!dev_desc || !info || part < 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000142 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400143 return -1;
144 }
145
146 /* This function validates AND fills in the GPT header and PTE */
147 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren4715a812011-10-28 09:21:46 +0000148 gpt_head, &gpt_pte) != 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000149 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-05-05 13:00:08 -0700150 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
151 gpt_head, &gpt_pte) != 1) {
152 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
153 __func__);
154 return -1;
155 } else {
156 printf("%s: *** Using Backup GPT ***\n",
157 __func__);
158 }
richardretanubun07f3d782008-09-26 11:13:22 -0400159 }
160
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100161 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000162 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500163 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000164 __func__, part);
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500165 free(gpt_pte);
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000166 return -1;
167 }
168
Steve Raee04350d2014-05-26 11:52:23 -0700169 /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
170 info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
Richard Retanubun50970832009-01-26 08:45:14 -0500171 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Steve Raee04350d2014-05-26 11:52:23 -0700172 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
Richard Retanubun50970832009-01-26 08:45:14 -0500173 - info->start;
Egbert Eichae1768a2013-04-09 06:03:36 +0000174 info->blksz = dev_desc->blksz;
richardretanubun07f3d782008-09-26 11:13:22 -0400175
Lei Wen6eecc032011-09-07 18:11:19 +0000176 sprintf((char *)info->name, "%s",
Doug Andersondeb5ca82011-10-19 09:47:31 +0000177 print_efiname(&gpt_pte[part - 1]));
richardretanubun07f3d782008-09-26 11:13:22 -0400178 sprintf((char *)info->type, "U-Boot");
Stephen Warrenb4414f42012-10-08 08:14:37 +0000179 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000180#ifdef CONFIG_PARTITION_UUIDS
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200181 uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
182 UUID_STR_FORMAT_GUID);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000183#endif
richardretanubun07f3d782008-09-26 11:13:22 -0400184
Steve Rae60bf9412014-05-26 11:52:24 -0700185 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
Frederic Leroy04735e92013-06-26 18:11:25 +0200186 info->start, info->size, info->name);
richardretanubun07f3d782008-09-26 11:13:22 -0400187
188 /* Remember to free pte */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000189 free(gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400190 return 0;
191}
192
Steve Rae60bf9412014-05-26 11:52:24 -0700193int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc,
194 const char *name, disk_partition_t *info)
195{
196 int ret;
197 int i;
198 for (i = 1; i < GPT_ENTRY_NUMBERS; i++) {
199 ret = get_partition_info_efi(dev_desc, i, info);
200 if (ret != 0) {
201 /* no more entries in table */
202 return -1;
203 }
204 if (strcmp(name, (const char *)info->name) == 0) {
205 /* matched */
206 return 0;
207 }
208 }
209 return -2;
210}
211
richardretanubun07f3d782008-09-26 11:13:22 -0400212int test_part_efi(block_dev_desc_t * dev_desc)
213{
Egbert Eichae1768a2013-04-09 06:03:36 +0000214 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubun07f3d782008-09-26 11:13:22 -0400215
216 /* Read legacy MBR from block 0 and validate it */
Anton staaff75dd582011-10-12 13:56:04 +0000217 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
218 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400219 return -1;
220 }
221 return 0;
222}
223
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100224/**
225 * set_protective_mbr(): Set the EFI protective MBR
226 * @param dev_desc - block device descriptor
227 *
228 * @return - zero on success, otherwise error
229 */
230static int set_protective_mbr(block_dev_desc_t *dev_desc)
231{
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100232 /* Setup the Protective MBR */
Hector Palacios61fcc7d2014-02-13 09:48:24 +0100233 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
234 memset(p_mbr, 0, sizeof(*p_mbr));
235
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100236 if (p_mbr == NULL) {
237 printf("%s: calloc failed!\n", __func__);
238 return -1;
239 }
240 /* Append signature */
241 p_mbr->signature = MSDOS_MBR_SIGNATURE;
242 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
243 p_mbr->partition_record[0].start_sect = 1;
244 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba;
245
246 /* Write MBR sector to the MMC device */
247 if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
248 printf("** Can't write to device %d **\n",
249 dev_desc->dev);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100250 return -1;
251 }
252
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100253 return 0;
254}
255
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100256int write_gpt_table(block_dev_desc_t *dev_desc,
257 gpt_header *gpt_h, gpt_entry *gpt_e)
258{
Egbert Eichae1768a2013-04-09 06:03:36 +0000259 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
260 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100261 u32 calc_crc32;
262 u64 val;
263
264 debug("max lba: %x\n", (u32) dev_desc->lba);
265 /* Setup the Protective MBR */
266 if (set_protective_mbr(dev_desc) < 0)
267 goto err;
268
269 /* Generate CRC for the Primary GPT Header */
270 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
271 le32_to_cpu(gpt_h->num_partition_entries) *
272 le32_to_cpu(gpt_h->sizeof_partition_entry));
273 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
274
275 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
276 le32_to_cpu(gpt_h->header_size));
277 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
278
279 /* Write the First GPT to the block right after the Legacy MBR */
280 if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
281 goto err;
282
Egbert Eichae1768a2013-04-09 06:03:36 +0000283 if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
284 != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100285 goto err;
286
Steve Raeae95fad2014-05-05 13:00:08 -0700287 /* recalculate the values for the Backup GPT Header */
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100288 val = le64_to_cpu(gpt_h->my_lba);
289 gpt_h->my_lba = gpt_h->alternate_lba;
290 gpt_h->alternate_lba = cpu_to_le64(val);
291 gpt_h->header_crc32 = 0;
292
293 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
294 le32_to_cpu(gpt_h->header_size));
295 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
296
297 if (dev_desc->block_write(dev_desc->dev,
Steve Raee04350d2014-05-26 11:52:23 -0700298 (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
299 + 1,
Egbert Eichae1768a2013-04-09 06:03:36 +0000300 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100301 goto err;
302
303 if (dev_desc->block_write(dev_desc->dev,
Steve Raee04350d2014-05-26 11:52:23 -0700304 (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
305 gpt_h) != 1)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100306 goto err;
307
308 debug("GPT successfully written to block device!\n");
309 return 0;
310
311 err:
312 printf("** Can't write to device %d **\n", dev_desc->dev);
313 return -1;
314}
315
316int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
317 disk_partition_t *partitions, int parts)
318{
Steve Raee04350d2014-05-26 11:52:23 -0700319 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
320 lbaint_t start;
321 lbaint_t last_usable_lba = (lbaint_t)
322 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100323 int i, k;
Marek Vasut67cd4a62013-05-19 12:53:34 +0000324 size_t efiname_len, dosname_len;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100325#ifdef CONFIG_PARTITION_UUIDS
326 char *str_uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200327 unsigned char *bin_uuid;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100328#endif
329
330 for (i = 0; i < parts; i++) {
331 /* partition starting lba */
332 start = partitions[i].start;
333 if (start && (start < offset)) {
334 printf("Partition overlap\n");
335 return -1;
336 }
337 if (start) {
338 gpt_e[i].starting_lba = cpu_to_le64(start);
339 offset = start + partitions[i].size;
340 } else {
341 gpt_e[i].starting_lba = cpu_to_le64(offset);
342 offset += partitions[i].size;
343 }
Steve Raededf37b2014-05-26 11:52:22 -0700344 if (offset >= last_usable_lba) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100345 printf("Partitions layout exceds disk size\n");
346 return -1;
347 }
348 /* partition ending lba */
349 if ((i == parts - 1) && (partitions[i].size == 0))
350 /* extend the last partition to maximuim */
351 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
352 else
353 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
354
355 /* partition type GUID */
356 memcpy(gpt_e[i].partition_type_guid.b,
357 &PARTITION_BASIC_DATA_GUID, 16);
358
359#ifdef CONFIG_PARTITION_UUIDS
360 str_uuid = partitions[i].uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200361 bin_uuid = gpt_e[i].unique_partition_guid.b;
362
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200363 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100364 printf("Partition no. %d: invalid guid: %s\n",
365 i, str_uuid);
366 return -1;
367 }
368#endif
369
370 /* partition attributes */
371 memset(&gpt_e[i].attributes, 0,
372 sizeof(gpt_entry_attributes));
373
374 /* partition name */
Marek Vasut67cd4a62013-05-19 12:53:34 +0000375 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100376 / sizeof(efi_char16_t);
Marek Vasut67cd4a62013-05-19 12:53:34 +0000377 dosname_len = sizeof(partitions[i].name);
378
379 memset(gpt_e[i].partition_name, 0,
380 sizeof(gpt_e[i].partition_name));
381
382 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100383 gpt_e[i].partition_name[k] =
384 (efi_char16_t)(partitions[i].name[k]);
385
Steve Raee04350d2014-05-26 11:52:23 -0700386 debug("%s: name: %s offset[%d]: 0x" LBAF
387 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100388 __func__, partitions[i].name, i,
389 offset, i, partitions[i].size);
390 }
391
392 return 0;
393}
394
395int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
396 char *str_guid, int parts_count)
397{
398 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
399 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
400 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
401 gpt_h->my_lba = cpu_to_le64(1);
402 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
403 gpt_h->first_usable_lba = cpu_to_le64(34);
404 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
405 gpt_h->partition_entry_lba = cpu_to_le64(2);
406 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
407 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
408 gpt_h->header_crc32 = 0;
409 gpt_h->partition_entry_array_crc32 = 0;
410
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200411 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100412 return -1;
413
414 return 0;
415}
416
417int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
418 disk_partition_t *partitions, int parts_count)
419{
420 int ret;
421
Egbert Eichae1768a2013-04-09 06:03:36 +0000422 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
423 dev_desc));
424 gpt_entry *gpt_e;
425
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100426 if (gpt_h == NULL) {
427 printf("%s: calloc failed!\n", __func__);
428 return -1;
429 }
430
Egbert Eichae1768a2013-04-09 06:03:36 +0000431 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
432 * sizeof(gpt_entry),
433 dev_desc));
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100434 if (gpt_e == NULL) {
435 printf("%s: calloc failed!\n", __func__);
436 free(gpt_h);
437 return -1;
438 }
439
440 /* Generate Primary GPT header (LBA1) */
441 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
442 if (ret)
443 goto err;
444
445 /* Generate partition entries */
446 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
447 if (ret)
448 goto err;
449
450 /* Write GPT partition table */
451 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
452
453err:
454 free(gpt_e);
455 free(gpt_h);
456 return ret;
457}
458#endif
459
richardretanubun07f3d782008-09-26 11:13:22 -0400460/*
461 * Private functions
462 */
463/*
464 * pmbr_part_valid(): Check for EFI partition signature
465 *
466 * Returns: 1 if EFI GPT partition type is found.
467 */
468static int pmbr_part_valid(struct partition *part)
469{
470 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich8faefad2013-03-29 07:57:10 +0000471 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubun07f3d782008-09-26 11:13:22 -0400472 return 1;
473 }
474
475 return 0;
476}
477
478/*
479 * is_pmbr_valid(): test Protective MBR for validity
480 *
481 * Returns: 1 if PMBR is valid, 0 otherwise.
482 * Validity depends on two things:
483 * 1) MSDOS signature is in the last two bytes of the MBR
484 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
485 */
486static int is_pmbr_valid(legacy_mbr * mbr)
487{
488 int i = 0;
489
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100490 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubun07f3d782008-09-26 11:13:22 -0400491 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400492
493 for (i = 0; i < 4; i++) {
494 if (pmbr_part_valid(&mbr->partition_record[i])) {
495 return 1;
496 }
497 }
498 return 0;
499}
500
501/**
502 * is_gpt_valid() - tests one GPT header and PTEs for validity
503 *
504 * lba is the logical block address of the GPT header to test
505 * gpt is a GPT header ptr, filled on return.
506 * ptes is a PTEs ptr, filled on return.
507 *
508 * Description: returns 1 if valid, 0 on error.
509 * If valid, returns pointers to PTEs.
510 */
Steve Raee04350d2014-05-26 11:52:23 -0700511static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
512 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
richardretanubun07f3d782008-09-26 11:13:22 -0400513{
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100514 u32 crc32_backup = 0;
515 u32 calc_crc32;
Steve Raee04350d2014-05-26 11:52:23 -0700516 u64 lastlba;
richardretanubun07f3d782008-09-26 11:13:22 -0400517
518 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000519 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400520 return 0;
521 }
522
523 /* Read GPT Header from device */
Steve Raee04350d2014-05-26 11:52:23 -0700524 if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head)
525 != 1) {
richardretanubun07f3d782008-09-26 11:13:22 -0400526 printf("*** ERROR: Can't read GPT header ***\n");
527 return 0;
528 }
529
530 /* Check the GPT header signature */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100531 if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
richardretanubun07f3d782008-09-26 11:13:22 -0400532 printf("GUID Partition Table Header signature is wrong:"
533 "0x%llX != 0x%llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100534 le64_to_cpu(pgpt_head->signature),
535 GPT_HEADER_SIGNATURE);
richardretanubun07f3d782008-09-26 11:13:22 -0400536 return 0;
537 }
538
539 /* Check the GUID Partition Table CRC */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100540 memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
541 memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
richardretanubun07f3d782008-09-26 11:13:22 -0400542
543 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100544 le32_to_cpu(pgpt_head->header_size));
richardretanubun07f3d782008-09-26 11:13:22 -0400545
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100546 memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
richardretanubun07f3d782008-09-26 11:13:22 -0400547
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100548 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400549 printf("GUID Partition Table Header CRC is wrong:"
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100550 "0x%x != 0x%x\n",
551 le32_to_cpu(crc32_backup), calc_crc32);
richardretanubun07f3d782008-09-26 11:13:22 -0400552 return 0;
553 }
554
555 /* Check that the my_lba entry points to the LBA that contains the GPT */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100556 if (le64_to_cpu(pgpt_head->my_lba) != lba) {
Simon Glasse48f3742014-11-11 12:47:07 -0700557 printf("GPT: my_lba incorrect: %llX != %" PRIX64 "\n",
558 le64_to_cpu(pgpt_head->my_lba),
559 lba);
richardretanubun07f3d782008-09-26 11:13:22 -0400560 return 0;
561 }
562
563 /* Check the first_usable_lba and last_usable_lba are within the disk. */
Steve Raee04350d2014-05-26 11:52:23 -0700564 lastlba = (u64)dev_desc->lba;
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100565 if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
Simon Glasse48f3742014-11-11 12:47:07 -0700566 printf("GPT: first_usable_lba incorrect: %llX > %" PRIX64 "\n",
567 le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400568 return 0;
569 }
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100570 if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
Simon Glasse48f3742014-11-11 12:47:07 -0700571 printf("GPT: last_usable_lba incorrect: %llX > %" PRIX64 "\n",
572 le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400573 return 0;
574 }
575
Simon Glasse48f3742014-11-11 12:47:07 -0700576 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %"
577 PRIX64 "\n", le64_to_cpu(pgpt_head->first_usable_lba),
578 le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400579
580 /* Read and allocate Partition Table Entries */
581 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
582 if (*pgpt_pte == NULL) {
583 printf("GPT: Failed to allocate memory for PTE\n");
584 return 0;
585 }
586
587 /* Check the GUID Partition Table Entry Array CRC */
588 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100589 le32_to_cpu(pgpt_head->num_partition_entries) *
590 le32_to_cpu(pgpt_head->sizeof_partition_entry));
richardretanubun07f3d782008-09-26 11:13:22 -0400591
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100592 if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400593 printf("GUID Partition Table Entry Array CRC is wrong:"
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100594 "0x%x != 0x%x\n",
595 le32_to_cpu(pgpt_head->partition_entry_array_crc32),
richardretanubun07f3d782008-09-26 11:13:22 -0400596 calc_crc32);
597
Doug Andersondeb5ca82011-10-19 09:47:31 +0000598 free(*pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400599 return 0;
600 }
601
602 /* We're done, all's well */
603 return 1;
604}
605
606/**
607 * alloc_read_gpt_entries(): reads partition entries from disk
608 * @dev_desc
609 * @gpt - GPT header
610 *
611 * Description: Returns ptes on success, NULL on error.
612 * Allocates space for PTEs based on information found in @gpt.
613 * Notes: remember to free pte when you're done!
614 */
615static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
616 gpt_header * pgpt_head)
617{
Egbert Eichae1768a2013-04-09 06:03:36 +0000618 size_t count = 0, blk_cnt;
richardretanubun07f3d782008-09-26 11:13:22 -0400619 gpt_entry *pte = NULL;
620
621 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000622 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400623 return NULL;
624 }
625
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100626 count = 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 debug("%s: count = %u * %u = %zu\n", __func__,
630 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
631 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
richardretanubun07f3d782008-09-26 11:13:22 -0400632
633 /* Allocate memory for PTE, remember to FREE */
634 if (count != 0) {
Egbert Eichae1768a2013-04-09 06:03:36 +0000635 pte = memalign(ARCH_DMA_MINALIGN,
636 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubun07f3d782008-09-26 11:13:22 -0400637 }
638
639 if (count == 0 || pte == NULL) {
Taylor Hutt9936be32012-10-12 14:26:09 +0000640 printf("%s: ERROR: Can't allocate 0x%zX "
641 "bytes for GPT Entries\n",
Doug Andersondf70b1c2011-10-19 08:04:46 +0000642 __func__, count);
richardretanubun07f3d782008-09-26 11:13:22 -0400643 return NULL;
644 }
645
646 /* Read GPT Entries from device */
Egbert Eichae1768a2013-04-09 06:03:36 +0000647 blk_cnt = BLOCK_CNT(count, dev_desc);
richardretanubun07f3d782008-09-26 11:13:22 -0400648 if (dev_desc->block_read (dev_desc->dev,
Steve Raee04350d2014-05-26 11:52:23 -0700649 (lbaint_t)le64_to_cpu(pgpt_head->partition_entry_lba),
Egbert Eichae1768a2013-04-09 06:03:36 +0000650 (lbaint_t) (blk_cnt), pte)
651 != blk_cnt) {
richardretanubun07f3d782008-09-26 11:13:22 -0400652
653 printf("*** ERROR: Can't read GPT Entries ***\n");
654 free(pte);
655 return NULL;
656 }
657 return pte;
658}
659
660/**
661 * is_pte_valid(): validates a single Partition Table Entry
662 * @gpt_entry - Pointer to a single Partition Table Entry
663 *
664 * Description: returns 1 if valid, 0 on error.
665 */
666static int is_pte_valid(gpt_entry * pte)
667{
668 efi_guid_t unused_guid;
669
670 if (!pte) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000671 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400672 return 0;
673 }
674
675 /* Only one validation for now:
676 * The GUID Partition Type != Unused Entry (ALL-ZERO)
677 */
678 memset(unused_guid.b, 0, sizeof(unused_guid.b));
679
680 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
681 sizeof(unused_guid.b)) == 0) {
682
Doug Andersondf70b1c2011-10-19 08:04:46 +0000683 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt9936be32012-10-12 14:26:09 +0000684 (unsigned int)(uintptr_t)pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400685
686 return 0;
687 } else {
688 return 1;
689 }
690}
691#endif