blob: 15627f29e8bd4ad33ba1688ad846d131e9c0cb8c [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>
Simon Glass2dd337a2015-09-02 17:24:58 -060019#include <memalign.h>
Chang Hyun Park823ffde2012-12-11 11:09:45 +010020#include <part_efi.h>
Lei Wen757ddfe2011-09-07 18:11:19 +000021#include <linux/ctype.h>
richardretanubune6745592008-09-26 11:13:22 -040022
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +010023DECLARE_GLOBAL_DATA_PTR;
24
Stephen Warren8ec730d2013-02-28 15:03:46 +000025#ifdef HAVE_BLOCK_DEVICE
richardretanubune6745592008-09-26 11:13:22 -040026/**
27 * efi_crc32() - EFI version of crc32 function
28 * @buf: buffer to calculate crc32 of
29 * @len - length of buf
30 *
31 * Description: Returns EFI-style CRC32 value for @buf
32 */
Chang Hyun Park823ffde2012-12-11 11:09:45 +010033static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubune6745592008-09-26 11:13:22 -040034{
35 return crc32(0, buf, len);
36}
37
38/*
39 * Private function prototypes
40 */
41
42static int pmbr_part_valid(struct partition *part);
43static int is_pmbr_valid(legacy_mbr * mbr);
Steve Raed30cbae2014-05-26 11:52:23 -070044static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
45 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -040046static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
47 gpt_header * pgpt_head);
richardretanubune6745592008-09-26 11:13:22 -040048static int is_pte_valid(gpt_entry * pte);
49
Lei Wen757ddfe2011-09-07 18:11:19 +000050static char *print_efiname(gpt_entry *pte)
51{
52 static char name[PARTNAME_SZ + 1];
53 int i;
54 for (i = 0; i < PARTNAME_SZ; i++) {
55 u8 c;
56 c = pte->partition_name[i] & 0xff;
57 c = (c && !isprint(c)) ? '.' : c;
58 name[i] = c;
59 }
60 name[PARTNAME_SZ] = 0;
61 return name;
62}
63
Stephen Warrend0cf3b62012-10-08 08:14:37 +000064static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
65
66static inline int is_bootable(gpt_entry *p)
67{
68 return p->attributes.fields.legacy_bios_bootable ||
69 !memcmp(&(p->partition_type_guid), &system_guid,
70 sizeof(efi_guid_t));
71}
72
Steve Rae6f5e17d2014-12-18 12:13:42 +010073static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba,
74 lbaint_t lastlba)
75{
76 uint32_t crc32_backup = 0;
77 uint32_t calc_crc32;
78
79 /* Check the GPT header signature */
80 if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE) {
81 printf("%s signature is wrong: 0x%llX != 0x%llX\n",
82 "GUID Partition Table Header",
83 le64_to_cpu(gpt_h->signature),
84 GPT_HEADER_SIGNATURE);
85 return -1;
86 }
87
88 /* Check the GUID Partition Table CRC */
89 memcpy(&crc32_backup, &gpt_h->header_crc32, sizeof(crc32_backup));
90 memset(&gpt_h->header_crc32, 0, sizeof(gpt_h->header_crc32));
91
92 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
93 le32_to_cpu(gpt_h->header_size));
94
95 memcpy(&gpt_h->header_crc32, &crc32_backup, sizeof(crc32_backup));
96
97 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
98 printf("%s CRC is wrong: 0x%x != 0x%x\n",
99 "GUID Partition Table Header",
100 le32_to_cpu(crc32_backup), calc_crc32);
101 return -1;
102 }
103
104 /*
105 * Check that the my_lba entry points to the LBA that contains the GPT
106 */
107 if (le64_to_cpu(gpt_h->my_lba) != lba) {
108 printf("GPT: my_lba incorrect: %llX != " LBAF "\n",
109 le64_to_cpu(gpt_h->my_lba),
110 lba);
111 return -1;
112 }
113
114 /*
115 * Check that the first_usable_lba and that the last_usable_lba are
116 * within the disk.
117 */
118 if (le64_to_cpu(gpt_h->first_usable_lba) > lastlba) {
119 printf("GPT: first_usable_lba incorrect: %llX > " LBAF "\n",
120 le64_to_cpu(gpt_h->first_usable_lba), lastlba);
121 return -1;
122 }
123 if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) {
124 printf("GPT: last_usable_lba incorrect: %llX > " LBAF "\n",
125 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
126 return -1;
127 }
128
129 debug("GPT: first_usable_lba: %llX last_usable_lba: %llX last lba: "
130 LBAF "\n", le64_to_cpu(gpt_h->first_usable_lba),
131 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
132
133 return 0;
134}
135
136static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
137{
138 uint32_t calc_crc32;
139
140 /* Check the GUID Partition Table Entry Array CRC */
141 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
142 le32_to_cpu(gpt_h->num_partition_entries) *
143 le32_to_cpu(gpt_h->sizeof_partition_entry));
144
145 if (calc_crc32 != le32_to_cpu(gpt_h->partition_entry_array_crc32)) {
146 printf("%s: 0x%x != 0x%x\n",
147 "GUID Partition Table Entry Array CRC is wrong",
148 le32_to_cpu(gpt_h->partition_entry_array_crc32),
149 calc_crc32);
150 return -1;
151 }
152
153 return 0;
154}
155
156static void prepare_backup_gpt_header(gpt_header *gpt_h)
157{
158 uint32_t calc_crc32;
159 uint64_t val;
160
161 /* recalculate the values for the Backup GPT Header */
162 val = le64_to_cpu(gpt_h->my_lba);
163 gpt_h->my_lba = gpt_h->alternate_lba;
164 gpt_h->alternate_lba = cpu_to_le64(val);
Steve Rae7d059342014-12-12 15:51:54 -0800165 gpt_h->partition_entry_lba =
166 cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
Steve Rae6f5e17d2014-12-18 12:13:42 +0100167 gpt_h->header_crc32 = 0;
168
169 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
170 le32_to_cpu(gpt_h->header_size));
171 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
172}
173
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100174#ifdef CONFIG_EFI_PARTITION
richardretanubune6745592008-09-26 11:13:22 -0400175/*
176 * Public Functions (include/part.h)
177 */
178
179void print_part_efi(block_dev_desc_t * dev_desc)
180{
Egbert Eich071e4232013-04-09 06:03:36 +0000181 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000182 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400183 int i = 0;
Stephen Warren8b855b52012-10-08 08:14:34 +0000184 char uuid[37];
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200185 unsigned char *uuid_bin;
richardretanubune6745592008-09-26 11:13:22 -0400186
187 if (!dev_desc) {
Doug Andersondc036672011-10-19 08:04:46 +0000188 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400189 return;
190 }
191 /* This function validates AND fills in the GPT header and PTE */
192 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren30f39de2011-10-28 09:21:46 +0000193 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000194 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raee1f4ac52014-05-05 13:00:08 -0700195 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
196 gpt_head, &gpt_pte) != 1) {
197 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
198 __func__);
199 return;
200 } else {
201 printf("%s: *** Using Backup GPT ***\n",
202 __func__);
203 }
richardretanubune6745592008-09-26 11:13:22 -0400204 }
205
Doug Anderson698a51d2011-10-19 09:47:31 +0000206 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400207
Stephen Warren26163262012-10-08 08:14:33 +0000208 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000209 printf("\tAttributes\n");
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200210 printf("\tType GUID\n");
211 printf("\tPartition GUID\n");
Stephen Warren8b855b52012-10-08 08:14:34 +0000212
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100213 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrene6df59e2012-10-08 08:14:32 +0000214 /* Stop at the first non valid PTE */
215 if (!is_pte_valid(&gpt_pte[i]))
216 break;
richardretanubune6745592008-09-26 11:13:22 -0400217
Stephen Warren26163262012-10-08 08:14:33 +0000218 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100219 le64_to_cpu(gpt_pte[i].starting_lba),
220 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warren26163262012-10-08 08:14:33 +0000221 print_efiname(&gpt_pte[i]));
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000222 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200223 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200224 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
Stephen Warren8b855b52012-10-08 08:14:34 +0000225 printf("\ttype:\t%s\n", uuid);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200226 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200227 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
228 printf("\tguid:\t%s\n", uuid);
richardretanubune6745592008-09-26 11:13:22 -0400229 }
230
231 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000232 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400233 return;
234}
235
236int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
237 disk_partition_t * info)
238{
Egbert Eich071e4232013-04-09 06:03:36 +0000239 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000240 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400241
242 /* "part" argument must be at least 1 */
243 if (!dev_desc || !info || part < 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000244 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400245 return -1;
246 }
247
248 /* This function validates AND fills in the GPT header and PTE */
249 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren30f39de2011-10-28 09:21:46 +0000250 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000251 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raee1f4ac52014-05-05 13:00:08 -0700252 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
253 gpt_head, &gpt_pte) != 1) {
254 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
255 __func__);
256 return -1;
257 } else {
258 printf("%s: *** Using Backup GPT ***\n",
259 __func__);
260 }
richardretanubune6745592008-09-26 11:13:22 -0400261 }
262
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100263 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrene47dbba2012-09-21 09:50:58 +0000264 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorfe7775312013-09-10 15:14:56 -0500265 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrene47dbba2012-09-21 09:50:58 +0000266 __func__, part);
Mark Langsdorfe7775312013-09-10 15:14:56 -0500267 free(gpt_pte);
Stephen Warrene47dbba2012-09-21 09:50:58 +0000268 return -1;
269 }
270
Steve Raed30cbae2014-05-26 11:52:23 -0700271 /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
272 info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
Richard Retanubun413dfc72009-01-26 08:45:14 -0500273 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Steve Raed30cbae2014-05-26 11:52:23 -0700274 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
Richard Retanubun413dfc72009-01-26 08:45:14 -0500275 - info->start;
Egbert Eich071e4232013-04-09 06:03:36 +0000276 info->blksz = dev_desc->blksz;
richardretanubune6745592008-09-26 11:13:22 -0400277
Lei Wen757ddfe2011-09-07 18:11:19 +0000278 sprintf((char *)info->name, "%s",
Doug Anderson698a51d2011-10-19 09:47:31 +0000279 print_efiname(&gpt_pte[part - 1]));
richardretanubune6745592008-09-26 11:13:22 -0400280 sprintf((char *)info->type, "U-Boot");
Stephen Warrend0cf3b62012-10-08 08:14:37 +0000281 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000282#ifdef CONFIG_PARTITION_UUIDS
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200283 uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
284 UUID_STR_FORMAT_GUID);
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000285#endif
richardretanubune6745592008-09-26 11:13:22 -0400286
Steve Raef7f10b82014-05-26 11:52:24 -0700287 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
Frederic Leroye7ee0282013-06-26 18:11:25 +0200288 info->start, info->size, info->name);
richardretanubune6745592008-09-26 11:13:22 -0400289
290 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000291 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400292 return 0;
293}
294
Steve Raef7f10b82014-05-26 11:52:24 -0700295int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc,
296 const char *name, disk_partition_t *info)
297{
298 int ret;
299 int i;
300 for (i = 1; i < GPT_ENTRY_NUMBERS; i++) {
301 ret = get_partition_info_efi(dev_desc, i, info);
302 if (ret != 0) {
303 /* no more entries in table */
304 return -1;
305 }
306 if (strcmp(name, (const char *)info->name) == 0) {
307 /* matched */
308 return 0;
309 }
310 }
311 return -2;
312}
313
richardretanubune6745592008-09-26 11:13:22 -0400314int test_part_efi(block_dev_desc_t * dev_desc)
315{
Egbert Eich071e4232013-04-09 06:03:36 +0000316 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubune6745592008-09-26 11:13:22 -0400317
318 /* Read legacy MBR from block 0 and validate it */
Anton staaf3b3df4e2011-10-12 13:56:04 +0000319 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
320 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubune6745592008-09-26 11:13:22 -0400321 return -1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100322 }
323 return 0;
324}
325
326/**
327 * set_protective_mbr(): Set the EFI protective MBR
328 * @param dev_desc - block device descriptor
329 *
330 * @return - zero on success, otherwise error
331 */
332static int set_protective_mbr(block_dev_desc_t *dev_desc)
333{
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100334 /* Setup the Protective MBR */
Hector Palaciosb0a54ff2014-02-13 09:48:24 +0100335 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
336 memset(p_mbr, 0, sizeof(*p_mbr));
337
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100338 if (p_mbr == NULL) {
339 printf("%s: calloc failed!\n", __func__);
340 return -1;
341 }
342 /* Append signature */
343 p_mbr->signature = MSDOS_MBR_SIGNATURE;
344 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
345 p_mbr->partition_record[0].start_sect = 1;
Maxime Ripard6d8a1522015-01-08 12:26:44 +0100346 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100347
348 /* Write MBR sector to the MMC device */
349 if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
350 printf("** Can't write to device %d **\n",
351 dev_desc->dev);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100352 return -1;
353 }
354
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100355 return 0;
356}
357
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100358int write_gpt_table(block_dev_desc_t *dev_desc,
359 gpt_header *gpt_h, gpt_entry *gpt_e)
360{
Egbert Eich071e4232013-04-09 06:03:36 +0000361 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
362 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100363 u32 calc_crc32;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100364
365 debug("max lba: %x\n", (u32) dev_desc->lba);
366 /* Setup the Protective MBR */
367 if (set_protective_mbr(dev_desc) < 0)
368 goto err;
369
370 /* Generate CRC for the Primary GPT Header */
371 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
372 le32_to_cpu(gpt_h->num_partition_entries) *
373 le32_to_cpu(gpt_h->sizeof_partition_entry));
374 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
375
376 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
377 le32_to_cpu(gpt_h->header_size));
378 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
379
380 /* Write the First GPT to the block right after the Legacy MBR */
381 if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
382 goto err;
383
Egbert Eich071e4232013-04-09 06:03:36 +0000384 if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
385 != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100386 goto err;
387
Steve Rae6f5e17d2014-12-18 12:13:42 +0100388 prepare_backup_gpt_header(gpt_h);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100389
390 if (dev_desc->block_write(dev_desc->dev,
Steve Raed30cbae2014-05-26 11:52:23 -0700391 (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
392 + 1,
Egbert Eich071e4232013-04-09 06:03:36 +0000393 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100394 goto err;
395
396 if (dev_desc->block_write(dev_desc->dev,
Steve Raed30cbae2014-05-26 11:52:23 -0700397 (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
398 gpt_h) != 1)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100399 goto err;
400
401 debug("GPT successfully written to block device!\n");
402 return 0;
403
404 err:
405 printf("** Can't write to device %d **\n", dev_desc->dev);
406 return -1;
407}
408
409int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
410 disk_partition_t *partitions, int parts)
411{
Steve Raed30cbae2014-05-26 11:52:23 -0700412 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
413 lbaint_t start;
414 lbaint_t last_usable_lba = (lbaint_t)
415 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100416 int i, k;
Marek Vasut941579e2013-05-19 12:53:34 +0000417 size_t efiname_len, dosname_len;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100418#ifdef CONFIG_PARTITION_UUIDS
419 char *str_uuid;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200420 unsigned char *bin_uuid;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100421#endif
422
423 for (i = 0; i < parts; i++) {
424 /* partition starting lba */
425 start = partitions[i].start;
426 if (start && (start < offset)) {
427 printf("Partition overlap\n");
428 return -1;
429 }
430 if (start) {
431 gpt_e[i].starting_lba = cpu_to_le64(start);
432 offset = start + partitions[i].size;
433 } else {
434 gpt_e[i].starting_lba = cpu_to_le64(offset);
435 offset += partitions[i].size;
436 }
Steve Rae6d0d1722014-05-26 11:52:22 -0700437 if (offset >= last_usable_lba) {
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100438 printf("Partitions layout exceds disk size\n");
439 return -1;
440 }
441 /* partition ending lba */
442 if ((i == parts - 1) && (partitions[i].size == 0))
443 /* extend the last partition to maximuim */
444 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
445 else
446 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
447
448 /* partition type GUID */
449 memcpy(gpt_e[i].partition_type_guid.b,
450 &PARTITION_BASIC_DATA_GUID, 16);
451
452#ifdef CONFIG_PARTITION_UUIDS
453 str_uuid = partitions[i].uuid;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200454 bin_uuid = gpt_e[i].unique_partition_guid.b;
455
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200456 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100457 printf("Partition no. %d: invalid guid: %s\n",
458 i, str_uuid);
459 return -1;
460 }
461#endif
462
463 /* partition attributes */
464 memset(&gpt_e[i].attributes, 0,
465 sizeof(gpt_entry_attributes));
466
467 /* partition name */
Marek Vasut941579e2013-05-19 12:53:34 +0000468 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100469 / sizeof(efi_char16_t);
Marek Vasut941579e2013-05-19 12:53:34 +0000470 dosname_len = sizeof(partitions[i].name);
471
472 memset(gpt_e[i].partition_name, 0,
473 sizeof(gpt_e[i].partition_name));
474
475 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100476 gpt_e[i].partition_name[k] =
477 (efi_char16_t)(partitions[i].name[k]);
478
Steve Raed30cbae2014-05-26 11:52:23 -0700479 debug("%s: name: %s offset[%d]: 0x" LBAF
480 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100481 __func__, partitions[i].name, i,
482 offset, i, partitions[i].size);
483 }
484
485 return 0;
486}
487
488int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
489 char *str_guid, int parts_count)
490{
491 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
492 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
493 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
494 gpt_h->my_lba = cpu_to_le64(1);
495 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
496 gpt_h->first_usable_lba = cpu_to_le64(34);
497 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
498 gpt_h->partition_entry_lba = cpu_to_le64(2);
499 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
500 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
501 gpt_h->header_crc32 = 0;
502 gpt_h->partition_entry_array_crc32 = 0;
503
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200504 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100505 return -1;
506
507 return 0;
508}
509
510int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
511 disk_partition_t *partitions, int parts_count)
512{
513 int ret;
514
Egbert Eich071e4232013-04-09 06:03:36 +0000515 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
516 dev_desc));
517 gpt_entry *gpt_e;
518
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100519 if (gpt_h == NULL) {
520 printf("%s: calloc failed!\n", __func__);
521 return -1;
522 }
523
Egbert Eich071e4232013-04-09 06:03:36 +0000524 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
525 * sizeof(gpt_entry),
526 dev_desc));
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100527 if (gpt_e == NULL) {
528 printf("%s: calloc failed!\n", __func__);
529 free(gpt_h);
530 return -1;
531 }
532
533 /* Generate Primary GPT header (LBA1) */
534 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
535 if (ret)
536 goto err;
537
538 /* Generate partition entries */
539 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
540 if (ret)
541 goto err;
542
543 /* Write GPT partition table */
544 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
545
546err:
547 free(gpt_e);
548 free(gpt_h);
549 return ret;
550}
Steve Rae7d059342014-12-12 15:51:54 -0800551
552int is_valid_gpt_buf(block_dev_desc_t *dev_desc, void *buf)
553{
554 gpt_header *gpt_h;
555 gpt_entry *gpt_e;
556
557 /* determine start of GPT Header in the buffer */
558 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
559 dev_desc->blksz);
560 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
561 dev_desc->lba))
562 return -1;
563
564 /* determine start of GPT Entries in the buffer */
565 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
566 dev_desc->blksz);
567 if (validate_gpt_entries(gpt_h, gpt_e))
568 return -1;
569
570 return 0;
571}
572
573int write_mbr_and_gpt_partitions(block_dev_desc_t *dev_desc, void *buf)
574{
575 gpt_header *gpt_h;
576 gpt_entry *gpt_e;
577 int gpt_e_blk_cnt;
578 lbaint_t lba;
579 int cnt;
580
581 if (is_valid_gpt_buf(dev_desc, buf))
582 return -1;
583
584 /* determine start of GPT Header in the buffer */
585 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
586 dev_desc->blksz);
587
588 /* determine start of GPT Entries in the buffer */
589 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
590 dev_desc->blksz);
591 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
592 le32_to_cpu(gpt_h->sizeof_partition_entry)),
593 dev_desc);
594
595 /* write MBR */
596 lba = 0; /* MBR is always at 0 */
597 cnt = 1; /* MBR (1 block) */
598 if (dev_desc->block_write(dev_desc->dev, lba, cnt, buf) != cnt) {
599 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
600 __func__, "MBR", cnt, lba);
601 return 1;
602 }
603
604 /* write Primary GPT */
605 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
606 cnt = 1; /* GPT Header (1 block) */
607 if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_h) != cnt) {
608 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
609 __func__, "Primary GPT Header", cnt, lba);
610 return 1;
611 }
612
613 lba = le64_to_cpu(gpt_h->partition_entry_lba);
614 cnt = gpt_e_blk_cnt;
615 if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_e) != cnt) {
616 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
617 __func__, "Primary GPT Entries", cnt, lba);
618 return 1;
619 }
620
621 prepare_backup_gpt_header(gpt_h);
622
623 /* write Backup GPT */
624 lba = le64_to_cpu(gpt_h->partition_entry_lba);
625 cnt = gpt_e_blk_cnt;
626 if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_e) != cnt) {
627 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
628 __func__, "Backup GPT Entries", cnt, lba);
629 return 1;
630 }
631
632 lba = le64_to_cpu(gpt_h->my_lba);
633 cnt = 1; /* GPT Header (1 block) */
634 if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_h) != cnt) {
635 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
636 __func__, "Backup GPT Header", cnt, lba);
637 return 1;
638 }
639
640 return 0;
641}
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100642#endif
643
richardretanubune6745592008-09-26 11:13:22 -0400644/*
645 * Private functions
646 */
647/*
648 * pmbr_part_valid(): Check for EFI partition signature
649 *
650 * Returns: 1 if EFI GPT partition type is found.
651 */
652static int pmbr_part_valid(struct partition *part)
653{
654 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich20ca3ad2013-03-29 07:57:10 +0000655 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubune6745592008-09-26 11:13:22 -0400656 return 1;
657 }
658
659 return 0;
660}
661
662/*
663 * is_pmbr_valid(): test Protective MBR for validity
664 *
665 * Returns: 1 if PMBR is valid, 0 otherwise.
666 * Validity depends on two things:
667 * 1) MSDOS signature is in the last two bytes of the MBR
668 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
669 */
670static int is_pmbr_valid(legacy_mbr * mbr)
671{
672 int i = 0;
673
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100674 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubune6745592008-09-26 11:13:22 -0400675 return 0;
richardretanubune6745592008-09-26 11:13:22 -0400676
677 for (i = 0; i < 4; i++) {
678 if (pmbr_part_valid(&mbr->partition_record[i])) {
679 return 1;
680 }
681 }
682 return 0;
683}
684
685/**
686 * is_gpt_valid() - tests one GPT header and PTEs for validity
687 *
688 * lba is the logical block address of the GPT header to test
689 * gpt is a GPT header ptr, filled on return.
690 * ptes is a PTEs ptr, filled on return.
691 *
692 * Description: returns 1 if valid, 0 on error.
693 * If valid, returns pointers to PTEs.
694 */
Steve Raed30cbae2014-05-26 11:52:23 -0700695static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
696 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
richardretanubune6745592008-09-26 11:13:22 -0400697{
richardretanubune6745592008-09-26 11:13:22 -0400698 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000699 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400700 return 0;
701 }
702
703 /* Read GPT Header from device */
Steve Raed30cbae2014-05-26 11:52:23 -0700704 if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head)
705 != 1) {
richardretanubune6745592008-09-26 11:13:22 -0400706 printf("*** ERROR: Can't read GPT header ***\n");
707 return 0;
708 }
709
Steve Rae6f5e17d2014-12-18 12:13:42 +0100710 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
richardretanubune6745592008-09-26 11:13:22 -0400711 return 0;
richardretanubune6745592008-09-26 11:13:22 -0400712
713 /* Read and allocate Partition Table Entries */
714 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
715 if (*pgpt_pte == NULL) {
716 printf("GPT: Failed to allocate memory for PTE\n");
717 return 0;
718 }
719
Steve Rae6f5e17d2014-12-18 12:13:42 +0100720 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
Doug Anderson698a51d2011-10-19 09:47:31 +0000721 free(*pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400722 return 0;
723 }
724
725 /* We're done, all's well */
726 return 1;
727}
728
729/**
730 * alloc_read_gpt_entries(): reads partition entries from disk
731 * @dev_desc
732 * @gpt - GPT header
733 *
734 * Description: Returns ptes on success, NULL on error.
735 * Allocates space for PTEs based on information found in @gpt.
736 * Notes: remember to free pte when you're done!
737 */
738static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
739 gpt_header * pgpt_head)
740{
Egbert Eich071e4232013-04-09 06:03:36 +0000741 size_t count = 0, blk_cnt;
richardretanubune6745592008-09-26 11:13:22 -0400742 gpt_entry *pte = NULL;
743
744 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000745 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400746 return NULL;
747 }
748
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100749 count = le32_to_cpu(pgpt_head->num_partition_entries) *
750 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubune6745592008-09-26 11:13:22 -0400751
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100752 debug("%s: count = %u * %u = %zu\n", __func__,
753 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
754 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
richardretanubune6745592008-09-26 11:13:22 -0400755
756 /* Allocate memory for PTE, remember to FREE */
757 if (count != 0) {
Egbert Eich071e4232013-04-09 06:03:36 +0000758 pte = memalign(ARCH_DMA_MINALIGN,
759 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubune6745592008-09-26 11:13:22 -0400760 }
761
762 if (count == 0 || pte == NULL) {
Taylor Hutt864074d2012-10-12 14:26:09 +0000763 printf("%s: ERROR: Can't allocate 0x%zX "
764 "bytes for GPT Entries\n",
Doug Andersondc036672011-10-19 08:04:46 +0000765 __func__, count);
richardretanubune6745592008-09-26 11:13:22 -0400766 return NULL;
767 }
768
769 /* Read GPT Entries from device */
Egbert Eich071e4232013-04-09 06:03:36 +0000770 blk_cnt = BLOCK_CNT(count, dev_desc);
richardretanubune6745592008-09-26 11:13:22 -0400771 if (dev_desc->block_read (dev_desc->dev,
Steve Raed30cbae2014-05-26 11:52:23 -0700772 (lbaint_t)le64_to_cpu(pgpt_head->partition_entry_lba),
Egbert Eich071e4232013-04-09 06:03:36 +0000773 (lbaint_t) (blk_cnt), pte)
774 != blk_cnt) {
richardretanubune6745592008-09-26 11:13:22 -0400775
776 printf("*** ERROR: Can't read GPT Entries ***\n");
777 free(pte);
778 return NULL;
779 }
780 return pte;
781}
782
783/**
784 * is_pte_valid(): validates a single Partition Table Entry
785 * @gpt_entry - Pointer to a single Partition Table Entry
786 *
787 * Description: returns 1 if valid, 0 on error.
788 */
789static int is_pte_valid(gpt_entry * pte)
790{
791 efi_guid_t unused_guid;
792
793 if (!pte) {
Doug Andersondc036672011-10-19 08:04:46 +0000794 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400795 return 0;
796 }
797
798 /* Only one validation for now:
799 * The GUID Partition Type != Unused Entry (ALL-ZERO)
800 */
801 memset(unused_guid.b, 0, sizeof(unused_guid.b));
802
803 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
804 sizeof(unused_guid.b)) == 0) {
805
Doug Andersondc036672011-10-19 08:04:46 +0000806 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt864074d2012-10-12 14:26:09 +0000807 (unsigned int)(uintptr_t)pte);
richardretanubune6745592008-09-26 11:13:22 -0400808
809 return 0;
810 } else {
811 return 1;
812 }
813}
814#endif