blob: efed58f81dac27ddcd1498a28ca8b80673138bda [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/*
Steve Raed30cbae2014-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
richardretanubune6745592008-09-26 11:13:22 -040012 */
Marc Dietrich20ca3ad2013-03-29 07:57:10 +000013#include <asm/unaligned.h>
richardretanubune6745592008-09-26 11:13:22 -040014#include <common.h>
15#include <command.h>
16#include <ide.h>
Simon Glasse9f343f2014-11-11 12:47:07 -070017#include <inttypes.h>
richardretanubune6745592008-09-26 11:13:22 -040018#include <malloc.h>
Chang Hyun Park823ffde2012-12-11 11:09:45 +010019#include <part_efi.h>
Lei Wen757ddfe2011-09-07 18:11:19 +000020#include <linux/ctype.h>
richardretanubune6745592008-09-26 11:13:22 -040021
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +010022DECLARE_GLOBAL_DATA_PTR;
23
Stephen Warren8ec730d2013-02-28 15:03:46 +000024#ifdef HAVE_BLOCK_DEVICE
richardretanubune6745592008-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 Park823ffde2012-12-11 11:09:45 +010032static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubune6745592008-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 Raed30cbae2014-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);
richardretanubune6745592008-09-26 11:13:22 -040045static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
46 gpt_header * pgpt_head);
richardretanubune6745592008-09-26 11:13:22 -040047static int is_pte_valid(gpt_entry * pte);
48
Lei Wen757ddfe2011-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 Warrend0cf3b62012-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 Majewski0dcfc5c2012-12-11 11:09:46 +010072#ifdef CONFIG_EFI_PARTITION
richardretanubune6745592008-09-26 11:13:22 -040073/*
74 * Public Functions (include/part.h)
75 */
76
77void print_part_efi(block_dev_desc_t * dev_desc)
78{
Egbert Eich071e4232013-04-09 06:03:36 +000079 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +000080 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -040081 int i = 0;
Stephen Warren8b855b52012-10-08 08:14:34 +000082 char uuid[37];
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020083 unsigned char *uuid_bin;
richardretanubune6745592008-09-26 11:13:22 -040084
85 if (!dev_desc) {
Doug Andersondc036672011-10-19 08:04:46 +000086 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -040087 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,
Stephen Warren30f39de2011-10-28 09:21:46 +000091 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +000092 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raee1f4ac52014-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 }
richardretanubune6745592008-09-26 11:13:22 -0400102 }
103
Doug Anderson698a51d2011-10-19 09:47:31 +0000104 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400105
Stephen Warren26163262012-10-08 08:14:33 +0000106 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000107 printf("\tAttributes\n");
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200108 printf("\tType GUID\n");
109 printf("\tPartition GUID\n");
Stephen Warren8b855b52012-10-08 08:14:34 +0000110
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100111 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrene6df59e2012-10-08 08:14:32 +0000112 /* Stop at the first non valid PTE */
113 if (!is_pte_valid(&gpt_pte[i]))
114 break;
richardretanubune6745592008-09-26 11:13:22 -0400115
Stephen Warren26163262012-10-08 08:14:33 +0000116 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100117 le64_to_cpu(gpt_pte[i].starting_lba),
118 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warren26163262012-10-08 08:14:33 +0000119 print_efiname(&gpt_pte[i]));
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000120 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200121 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200122 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
Stephen Warren8b855b52012-10-08 08:14:34 +0000123 printf("\ttype:\t%s\n", uuid);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200124 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200125 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
126 printf("\tguid:\t%s\n", uuid);
richardretanubune6745592008-09-26 11:13:22 -0400127 }
128
129 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000130 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400131 return;
132}
133
134int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
135 disk_partition_t * info)
136{
Egbert Eich071e4232013-04-09 06:03:36 +0000137 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000138 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400139
140 /* "part" argument must be at least 1 */
141 if (!dev_desc || !info || part < 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000142 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-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 Warren30f39de2011-10-28 09:21:46 +0000148 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000149 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raee1f4ac52014-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 }
richardretanubune6745592008-09-26 11:13:22 -0400159 }
160
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100161 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrene47dbba2012-09-21 09:50:58 +0000162 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorfe7775312013-09-10 15:14:56 -0500163 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrene47dbba2012-09-21 09:50:58 +0000164 __func__, part);
Mark Langsdorfe7775312013-09-10 15:14:56 -0500165 free(gpt_pte);
Stephen Warrene47dbba2012-09-21 09:50:58 +0000166 return -1;
167 }
168
Steve Raed30cbae2014-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 Retanubun413dfc72009-01-26 08:45:14 -0500171 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Steve Raed30cbae2014-05-26 11:52:23 -0700172 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
Richard Retanubun413dfc72009-01-26 08:45:14 -0500173 - info->start;
Egbert Eich071e4232013-04-09 06:03:36 +0000174 info->blksz = dev_desc->blksz;
richardretanubune6745592008-09-26 11:13:22 -0400175
Lei Wen757ddfe2011-09-07 18:11:19 +0000176 sprintf((char *)info->name, "%s",
Doug Anderson698a51d2011-10-19 09:47:31 +0000177 print_efiname(&gpt_pte[part - 1]));
richardretanubune6745592008-09-26 11:13:22 -0400178 sprintf((char *)info->type, "U-Boot");
Stephen Warrend0cf3b62012-10-08 08:14:37 +0000179 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000180#ifdef CONFIG_PARTITION_UUIDS
Przemyslaw Marczak0c813362014-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 Warrenc6c7b7a2012-09-21 09:50:59 +0000183#endif
richardretanubune6745592008-09-26 11:13:22 -0400184
Steve Raef7f10b82014-05-26 11:52:24 -0700185 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
Frederic Leroye7ee0282013-06-26 18:11:25 +0200186 info->start, info->size, info->name);
richardretanubune6745592008-09-26 11:13:22 -0400187
188 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000189 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400190 return 0;
191}
192
Steve Raef7f10b82014-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
richardretanubune6745592008-09-26 11:13:22 -0400212int test_part_efi(block_dev_desc_t * dev_desc)
213{
Egbert Eich071e4232013-04-09 06:03:36 +0000214 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubune6745592008-09-26 11:13:22 -0400215
216 /* Read legacy MBR from block 0 and validate it */
Anton staaf3b3df4e2011-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)) {
richardretanubune6745592008-09-26 11:13:22 -0400219 return -1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100220 }
221 return 0;
222}
223
224/**
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 Majewski0dcfc5c2012-12-11 11:09:46 +0100232 /* Setup the Protective MBR */
Hector Palaciosb0a54ff2014-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 Majewski0dcfc5c2012-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 Majewski0dcfc5c2012-12-11 11:09:46 +0100250 return -1;
251 }
252
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100253 return 0;
254}
255
Lukasz Majewski0dcfc5c2012-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 Eich071e4232013-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 Majewski0dcfc5c2012-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 Eich071e4232013-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 Majewski0dcfc5c2012-12-11 11:09:46 +0100285 goto err;
286
Steve Raee1f4ac52014-05-05 13:00:08 -0700287 /* recalculate the values for the Backup GPT Header */
Lukasz Majewski0dcfc5c2012-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 Raed30cbae2014-05-26 11:52:23 -0700298 (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
299 + 1,
Egbert Eich071e4232013-04-09 06:03:36 +0000300 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100301 goto err;
302
303 if (dev_desc->block_write(dev_desc->dev,
Steve Raed30cbae2014-05-26 11:52:23 -0700304 (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
305 gpt_h) != 1)
Lukasz Majewski0dcfc5c2012-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 Raed30cbae2014-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 Majewski0dcfc5c2012-12-11 11:09:46 +0100323 int i, k;
Marek Vasut941579e2013-05-19 12:53:34 +0000324 size_t efiname_len, dosname_len;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100325#ifdef CONFIG_PARTITION_UUIDS
326 char *str_uuid;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200327 unsigned char *bin_uuid;
Lukasz Majewski0dcfc5c2012-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 Rae6d0d1722014-05-26 11:52:22 -0700344 if (offset >= last_usable_lba) {
Lukasz Majewski0dcfc5c2012-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 Marczakfa907342014-04-02 10:20:02 +0200361 bin_uuid = gpt_e[i].unique_partition_guid.b;
362
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200363 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
Lukasz Majewski0dcfc5c2012-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 Vasut941579e2013-05-19 12:53:34 +0000375 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100376 / sizeof(efi_char16_t);
Marek Vasut941579e2013-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 Majewski0dcfc5c2012-12-11 11:09:46 +0100383 gpt_e[i].partition_name[k] =
384 (efi_char16_t)(partitions[i].name[k]);
385
Steve Raed30cbae2014-05-26 11:52:23 -0700386 debug("%s: name: %s offset[%d]: 0x" LBAF
387 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski0dcfc5c2012-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 Marczak0c813362014-04-02 10:20:03 +0200411 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski0dcfc5c2012-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 Eich071e4232013-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 Majewski0dcfc5c2012-12-11 11:09:46 +0100426 if (gpt_h == NULL) {
427 printf("%s: calloc failed!\n", __func__);
428 return -1;
429 }
430
Egbert Eich071e4232013-04-09 06:03:36 +0000431 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
432 * sizeof(gpt_entry),
433 dev_desc));
Lukasz Majewski0dcfc5c2012-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
richardretanubune6745592008-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 Dietrich20ca3ad2013-03-29 07:57:10 +0000471 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubune6745592008-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 Park823ffde2012-12-11 11:09:45 +0100490 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubune6745592008-09-26 11:13:22 -0400491 return 0;
richardretanubune6745592008-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 Raed30cbae2014-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)
richardretanubune6745592008-09-26 11:13:22 -0400513{
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100514 u32 crc32_backup = 0;
515 u32 calc_crc32;
Steve Raed30cbae2014-05-26 11:52:23 -0700516 u64 lastlba;
richardretanubune6745592008-09-26 11:13:22 -0400517
518 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000519 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400520 return 0;
521 }
522
523 /* Read GPT Header from device */
Steve Raed30cbae2014-05-26 11:52:23 -0700524 if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head)
525 != 1) {
richardretanubune6745592008-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 Park823ffde2012-12-11 11:09:45 +0100531 if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
richardretanubune6745592008-09-26 11:13:22 -0400532 printf("GUID Partition Table Header signature is wrong:"
533 "0x%llX != 0x%llX\n",
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100534 le64_to_cpu(pgpt_head->signature),
535 GPT_HEADER_SIGNATURE);
richardretanubune6745592008-09-26 11:13:22 -0400536 return 0;
537 }
538
539 /* Check the GUID Partition Table CRC */
Chang Hyun Park823ffde2012-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));
richardretanubune6745592008-09-26 11:13:22 -0400542
543 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100544 le32_to_cpu(pgpt_head->header_size));
richardretanubune6745592008-09-26 11:13:22 -0400545
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100546 memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
richardretanubune6745592008-09-26 11:13:22 -0400547
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100548 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
richardretanubune6745592008-09-26 11:13:22 -0400549 printf("GUID Partition Table Header CRC is wrong:"
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100550 "0x%x != 0x%x\n",
551 le32_to_cpu(crc32_backup), calc_crc32);
richardretanubune6745592008-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 Park823ffde2012-12-11 11:09:45 +0100556 if (le64_to_cpu(pgpt_head->my_lba) != lba) {
Simon Glasse9f343f2014-11-11 12:47:07 -0700557 printf("GPT: my_lba incorrect: %llX != %" PRIX64 "\n",
558 le64_to_cpu(pgpt_head->my_lba),
559 lba);
richardretanubune6745592008-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 Raed30cbae2014-05-26 11:52:23 -0700564 lastlba = (u64)dev_desc->lba;
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100565 if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
Simon Glasse9f343f2014-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);
richardretanubune6745592008-09-26 11:13:22 -0400568 return 0;
569 }
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100570 if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
Simon Glasse9f343f2014-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);
richardretanubune6745592008-09-26 11:13:22 -0400573 return 0;
574 }
575
Simon Glasse9f343f2014-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);
richardretanubune6745592008-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 Park823ffde2012-12-11 11:09:45 +0100589 le32_to_cpu(pgpt_head->num_partition_entries) *
590 le32_to_cpu(pgpt_head->sizeof_partition_entry));
richardretanubune6745592008-09-26 11:13:22 -0400591
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100592 if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
richardretanubune6745592008-09-26 11:13:22 -0400593 printf("GUID Partition Table Entry Array CRC is wrong:"
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100594 "0x%x != 0x%x\n",
595 le32_to_cpu(pgpt_head->partition_entry_array_crc32),
richardretanubune6745592008-09-26 11:13:22 -0400596 calc_crc32);
597
Doug Anderson698a51d2011-10-19 09:47:31 +0000598 free(*pgpt_pte);
richardretanubune6745592008-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 Eich071e4232013-04-09 06:03:36 +0000618 size_t count = 0, blk_cnt;
richardretanubune6745592008-09-26 11:13:22 -0400619 gpt_entry *pte = NULL;
620
621 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000622 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400623 return NULL;
624 }
625
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100626 count = le32_to_cpu(pgpt_head->num_partition_entries) *
627 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubune6745592008-09-26 11:13:22 -0400628
Chang Hyun Park823ffde2012-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);
richardretanubune6745592008-09-26 11:13:22 -0400632
633 /* Allocate memory for PTE, remember to FREE */
634 if (count != 0) {
Egbert Eich071e4232013-04-09 06:03:36 +0000635 pte = memalign(ARCH_DMA_MINALIGN,
636 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubune6745592008-09-26 11:13:22 -0400637 }
638
639 if (count == 0 || pte == NULL) {
Taylor Hutt864074d2012-10-12 14:26:09 +0000640 printf("%s: ERROR: Can't allocate 0x%zX "
641 "bytes for GPT Entries\n",
Doug Andersondc036672011-10-19 08:04:46 +0000642 __func__, count);
richardretanubune6745592008-09-26 11:13:22 -0400643 return NULL;
644 }
645
646 /* Read GPT Entries from device */
Egbert Eich071e4232013-04-09 06:03:36 +0000647 blk_cnt = BLOCK_CNT(count, dev_desc);
richardretanubune6745592008-09-26 11:13:22 -0400648 if (dev_desc->block_read (dev_desc->dev,
Steve Raed30cbae2014-05-26 11:52:23 -0700649 (lbaint_t)le64_to_cpu(pgpt_head->partition_entry_lba),
Egbert Eich071e4232013-04-09 06:03:36 +0000650 (lbaint_t) (blk_cnt), pte)
651 != blk_cnt) {
richardretanubune6745592008-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 Andersondc036672011-10-19 08:04:46 +0000671 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-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 Andersondc036672011-10-19 08:04:46 +0000683 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt864074d2012-10-12 14:26:09 +0000684 (unsigned int)(uintptr_t)pte);
richardretanubune6745592008-09-26 11:13:22 -0400685
686 return 0;
687 } else {
688 return 1;
689 }
690}
691#endif