blob: c124143bd9f9b87ba32dbf7ab3b037f42a4f5c38 [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>
Simon Glasscf92e052015-09-02 17:24:58 -060019#include <memalign.h>
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010020#include <part_efi.h>
Lei Wen6eecc032011-09-07 18:11:19 +000021#include <linux/ctype.h>
richardretanubun07f3d782008-09-26 11:13:22 -040022
Lukasz Majewski40684dd2012-12-11 11:09:46 +010023DECLARE_GLOBAL_DATA_PTR;
24
Stephen Warren2c1af9d2013-02-28 15:03:46 +000025#ifdef HAVE_BLOCK_DEVICE
richardretanubun07f3d782008-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 Parkfae2bf22012-12-11 11:09:45 +010033static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubun07f3d782008-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 Raee04350d2014-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);
richardretanubun07f3d782008-09-26 11:13:22 -040046static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
47 gpt_header * pgpt_head);
richardretanubun07f3d782008-09-26 11:13:22 -040048static int is_pte_valid(gpt_entry * pte);
49
Lei Wen6eecc032011-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 Warrenb4414f42012-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 Raee1f6b0a2014-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 Rae0ff7e582014-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 Raee1f6b0a2014-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 Majewski40684dd2012-12-11 11:09:46 +0100174#ifdef CONFIG_EFI_PARTITION
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000175/*
176 * Public Functions (include/part.h)
177 */
178
179void print_part_efi(block_dev_desc_t * dev_desc)
180{
Egbert Eichae1768a2013-04-09 06:03:36 +0000181 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000182 gpt_entry *gpt_pte = NULL;
183 int i = 0;
184 char uuid[37];
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200185 unsigned char *uuid_bin;
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000186
187 if (!dev_desc) {
188 printf("%s: Invalid Argument(s)\n", __func__);
189 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,
193 gpt_head, &gpt_pte) != 1) {
194 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-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 }
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000204 }
205
206 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
207
208 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren13bf2f52012-10-08 08:14:36 +0000209 printf("\tAttributes\n");
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200210 printf("\tType GUID\n");
211 printf("\tPartition GUID\n");
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000212
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100213 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000214 /* Stop at the first non valid PTE */
215 if (!is_pte_valid(&gpt_pte[i]))
216 break;
217
218 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100219 le64_to_cpu(gpt_pte[i].starting_lba),
220 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000221 print_efiname(&gpt_pte[i]));
Stephen Warren13bf2f52012-10-08 08:14:36 +0000222 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200223 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200224 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000225 printf("\ttype:\t%s\n", uuid);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200226 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200227 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
228 printf("\tguid:\t%s\n", uuid);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000229 }
230
231 /* Remember to free pte */
232 free(gpt_pte);
233 return;
234}
Stephen Warren894bfbb2012-09-21 09:50:59 +0000235
richardretanubun07f3d782008-09-26 11:13:22 -0400236int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
237 disk_partition_t * info)
238{
Egbert Eichae1768a2013-04-09 06:03:36 +0000239 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Andersondeb5ca82011-10-19 09:47:31 +0000240 gpt_entry *gpt_pte = NULL;
richardretanubun07f3d782008-09-26 11:13:22 -0400241
242 /* "part" argument must be at least 1 */
243 if (!dev_desc || !info || part < 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000244 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-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 Warren4715a812011-10-28 09:21:46 +0000250 gpt_head, &gpt_pte) != 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000251 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-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 }
richardretanubun07f3d782008-09-26 11:13:22 -0400261 }
262
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100263 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000264 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500265 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000266 __func__, part);
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500267 free(gpt_pte);
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000268 return -1;
269 }
270
Steve Raee04350d2014-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 Retanubun50970832009-01-26 08:45:14 -0500273 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Steve Raee04350d2014-05-26 11:52:23 -0700274 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
Richard Retanubun50970832009-01-26 08:45:14 -0500275 - info->start;
Egbert Eichae1768a2013-04-09 06:03:36 +0000276 info->blksz = dev_desc->blksz;
richardretanubun07f3d782008-09-26 11:13:22 -0400277
Lei Wen6eecc032011-09-07 18:11:19 +0000278 sprintf((char *)info->name, "%s",
Doug Andersondeb5ca82011-10-19 09:47:31 +0000279 print_efiname(&gpt_pte[part - 1]));
richardretanubun07f3d782008-09-26 11:13:22 -0400280 sprintf((char *)info->type, "U-Boot");
Stephen Warrenb4414f42012-10-08 08:14:37 +0000281 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000282#ifdef CONFIG_PARTITION_UUIDS
Przemyslaw Marczakd718ded2014-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 Warren894bfbb2012-09-21 09:50:59 +0000285#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100286#ifdef CONFIG_PARTITION_TYPE_GUID
287 uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
288 info->type_guid, UUID_STR_FORMAT_GUID);
289#endif
richardretanubun07f3d782008-09-26 11:13:22 -0400290
Steve Rae60bf9412014-05-26 11:52:24 -0700291 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
Frederic Leroy04735e92013-06-26 18:11:25 +0200292 info->start, info->size, info->name);
richardretanubun07f3d782008-09-26 11:13:22 -0400293
294 /* Remember to free pte */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000295 free(gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400296 return 0;
297}
298
Steve Rae60bf9412014-05-26 11:52:24 -0700299int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc,
300 const char *name, disk_partition_t *info)
301{
302 int ret;
303 int i;
304 for (i = 1; i < GPT_ENTRY_NUMBERS; i++) {
305 ret = get_partition_info_efi(dev_desc, i, info);
306 if (ret != 0) {
307 /* no more entries in table */
308 return -1;
309 }
310 if (strcmp(name, (const char *)info->name) == 0) {
311 /* matched */
312 return 0;
313 }
314 }
315 return -2;
316}
317
richardretanubun07f3d782008-09-26 11:13:22 -0400318int test_part_efi(block_dev_desc_t * dev_desc)
319{
Egbert Eichae1768a2013-04-09 06:03:36 +0000320 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubun07f3d782008-09-26 11:13:22 -0400321
322 /* Read legacy MBR from block 0 and validate it */
Anton staaff75dd582011-10-12 13:56:04 +0000323 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
324 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400325 return -1;
326 }
327 return 0;
328}
329
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100330/**
331 * set_protective_mbr(): Set the EFI protective MBR
332 * @param dev_desc - block device descriptor
333 *
334 * @return - zero on success, otherwise error
335 */
336static int set_protective_mbr(block_dev_desc_t *dev_desc)
337{
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100338 /* Setup the Protective MBR */
Hector Palacios61fcc7d2014-02-13 09:48:24 +0100339 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
340 memset(p_mbr, 0, sizeof(*p_mbr));
341
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100342 if (p_mbr == NULL) {
343 printf("%s: calloc failed!\n", __func__);
344 return -1;
345 }
346 /* Append signature */
347 p_mbr->signature = MSDOS_MBR_SIGNATURE;
348 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
349 p_mbr->partition_record[0].start_sect = 1;
Maxime Ripardb349abb2015-01-08 12:26:44 +0100350 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100351
352 /* Write MBR sector to the MMC device */
353 if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
354 printf("** Can't write to device %d **\n",
355 dev_desc->dev);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100356 return -1;
357 }
358
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100359 return 0;
360}
361
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100362int write_gpt_table(block_dev_desc_t *dev_desc,
363 gpt_header *gpt_h, gpt_entry *gpt_e)
364{
Egbert Eichae1768a2013-04-09 06:03:36 +0000365 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
366 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100367 u32 calc_crc32;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100368
369 debug("max lba: %x\n", (u32) dev_desc->lba);
370 /* Setup the Protective MBR */
371 if (set_protective_mbr(dev_desc) < 0)
372 goto err;
373
374 /* Generate CRC for the Primary GPT Header */
375 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
376 le32_to_cpu(gpt_h->num_partition_entries) *
377 le32_to_cpu(gpt_h->sizeof_partition_entry));
378 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
379
380 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
381 le32_to_cpu(gpt_h->header_size));
382 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
383
384 /* Write the First GPT to the block right after the Legacy MBR */
385 if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
386 goto err;
387
Egbert Eichae1768a2013-04-09 06:03:36 +0000388 if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
389 != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100390 goto err;
391
Steve Raee1f6b0a2014-12-18 12:13:42 +0100392 prepare_backup_gpt_header(gpt_h);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100393
394 if (dev_desc->block_write(dev_desc->dev,
Steve Raee04350d2014-05-26 11:52:23 -0700395 (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
396 + 1,
Egbert Eichae1768a2013-04-09 06:03:36 +0000397 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100398 goto err;
399
400 if (dev_desc->block_write(dev_desc->dev,
Steve Raee04350d2014-05-26 11:52:23 -0700401 (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
402 gpt_h) != 1)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100403 goto err;
404
405 debug("GPT successfully written to block device!\n");
406 return 0;
407
408 err:
409 printf("** Can't write to device %d **\n", dev_desc->dev);
410 return -1;
411}
412
413int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
414 disk_partition_t *partitions, int parts)
415{
Steve Raee04350d2014-05-26 11:52:23 -0700416 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
417 lbaint_t start;
418 lbaint_t last_usable_lba = (lbaint_t)
419 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100420 int i, k;
Marek Vasut67cd4a62013-05-19 12:53:34 +0000421 size_t efiname_len, dosname_len;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100422#ifdef CONFIG_PARTITION_UUIDS
423 char *str_uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200424 unsigned char *bin_uuid;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100425#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100426#ifdef CONFIG_PARTITION_TYPE_GUID
427 char *str_type_guid;
428 unsigned char *bin_type_guid;
429#endif
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100430
431 for (i = 0; i < parts; i++) {
432 /* partition starting lba */
433 start = partitions[i].start;
434 if (start && (start < offset)) {
435 printf("Partition overlap\n");
436 return -1;
437 }
438 if (start) {
439 gpt_e[i].starting_lba = cpu_to_le64(start);
440 offset = start + partitions[i].size;
441 } else {
442 gpt_e[i].starting_lba = cpu_to_le64(offset);
443 offset += partitions[i].size;
444 }
Steve Raededf37b2014-05-26 11:52:22 -0700445 if (offset >= last_usable_lba) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100446 printf("Partitions layout exceds disk size\n");
447 return -1;
448 }
449 /* partition ending lba */
450 if ((i == parts - 1) && (partitions[i].size == 0))
451 /* extend the last partition to maximuim */
452 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
453 else
454 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
455
Patrick Delaunay7561b252015-10-27 11:00:27 +0100456#ifdef CONFIG_PARTITION_TYPE_GUID
457 str_type_guid = partitions[i].type_guid;
458 bin_type_guid = gpt_e[i].partition_type_guid.b;
459 if (strlen(str_type_guid)) {
460 if (uuid_str_to_bin(str_type_guid, bin_type_guid,
461 UUID_STR_FORMAT_GUID)) {
462 printf("Partition no. %d: invalid type guid: %s\n",
463 i, str_type_guid);
464 return -1;
465 }
466 } else {
467 /* default partition type GUID */
468 memcpy(bin_type_guid,
469 &PARTITION_BASIC_DATA_GUID, 16);
470 }
471#else
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100472 /* partition type GUID */
473 memcpy(gpt_e[i].partition_type_guid.b,
474 &PARTITION_BASIC_DATA_GUID, 16);
Patrick Delaunay7561b252015-10-27 11:00:27 +0100475#endif
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100476
477#ifdef CONFIG_PARTITION_UUIDS
478 str_uuid = partitions[i].uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200479 bin_uuid = gpt_e[i].unique_partition_guid.b;
480
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200481 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100482 printf("Partition no. %d: invalid guid: %s\n",
483 i, str_uuid);
484 return -1;
485 }
486#endif
487
488 /* partition attributes */
489 memset(&gpt_e[i].attributes, 0,
490 sizeof(gpt_entry_attributes));
491
492 /* partition name */
Marek Vasut67cd4a62013-05-19 12:53:34 +0000493 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100494 / sizeof(efi_char16_t);
Marek Vasut67cd4a62013-05-19 12:53:34 +0000495 dosname_len = sizeof(partitions[i].name);
496
497 memset(gpt_e[i].partition_name, 0,
498 sizeof(gpt_e[i].partition_name));
499
500 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100501 gpt_e[i].partition_name[k] =
502 (efi_char16_t)(partitions[i].name[k]);
503
Steve Raee04350d2014-05-26 11:52:23 -0700504 debug("%s: name: %s offset[%d]: 0x" LBAF
505 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100506 __func__, partitions[i].name, i,
507 offset, i, partitions[i].size);
508 }
509
510 return 0;
511}
512
513int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
514 char *str_guid, int parts_count)
515{
516 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
517 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
518 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
519 gpt_h->my_lba = cpu_to_le64(1);
520 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
521 gpt_h->first_usable_lba = cpu_to_le64(34);
522 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
523 gpt_h->partition_entry_lba = cpu_to_le64(2);
524 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
525 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
526 gpt_h->header_crc32 = 0;
527 gpt_h->partition_entry_array_crc32 = 0;
528
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200529 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100530 return -1;
531
532 return 0;
533}
534
535int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
536 disk_partition_t *partitions, int parts_count)
537{
538 int ret;
539
Egbert Eichae1768a2013-04-09 06:03:36 +0000540 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
541 dev_desc));
542 gpt_entry *gpt_e;
543
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100544 if (gpt_h == NULL) {
545 printf("%s: calloc failed!\n", __func__);
546 return -1;
547 }
548
Egbert Eichae1768a2013-04-09 06:03:36 +0000549 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
550 * sizeof(gpt_entry),
551 dev_desc));
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100552 if (gpt_e == NULL) {
553 printf("%s: calloc failed!\n", __func__);
554 free(gpt_h);
555 return -1;
556 }
557
558 /* Generate Primary GPT header (LBA1) */
559 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
560 if (ret)
561 goto err;
562
563 /* Generate partition entries */
564 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
565 if (ret)
566 goto err;
567
568 /* Write GPT partition table */
569 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
570
571err:
572 free(gpt_e);
573 free(gpt_h);
574 return ret;
575}
Steve Rae0ff7e582014-12-12 15:51:54 -0800576
577int is_valid_gpt_buf(block_dev_desc_t *dev_desc, void *buf)
578{
579 gpt_header *gpt_h;
580 gpt_entry *gpt_e;
581
582 /* determine start of GPT Header in the buffer */
583 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
584 dev_desc->blksz);
585 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
586 dev_desc->lba))
587 return -1;
588
589 /* determine start of GPT Entries in the buffer */
590 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
591 dev_desc->blksz);
592 if (validate_gpt_entries(gpt_h, gpt_e))
593 return -1;
594
595 return 0;
596}
597
598int write_mbr_and_gpt_partitions(block_dev_desc_t *dev_desc, void *buf)
599{
600 gpt_header *gpt_h;
601 gpt_entry *gpt_e;
602 int gpt_e_blk_cnt;
603 lbaint_t lba;
604 int cnt;
605
606 if (is_valid_gpt_buf(dev_desc, buf))
607 return -1;
608
609 /* determine start of GPT Header in the buffer */
610 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
611 dev_desc->blksz);
612
613 /* determine start of GPT Entries in the buffer */
614 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
615 dev_desc->blksz);
616 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
617 le32_to_cpu(gpt_h->sizeof_partition_entry)),
618 dev_desc);
619
620 /* write MBR */
621 lba = 0; /* MBR is always at 0 */
622 cnt = 1; /* MBR (1 block) */
623 if (dev_desc->block_write(dev_desc->dev, lba, cnt, buf) != cnt) {
624 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
625 __func__, "MBR", cnt, lba);
626 return 1;
627 }
628
629 /* write Primary GPT */
630 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
631 cnt = 1; /* GPT Header (1 block) */
632 if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_h) != cnt) {
633 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
634 __func__, "Primary GPT Header", cnt, lba);
635 return 1;
636 }
637
638 lba = le64_to_cpu(gpt_h->partition_entry_lba);
639 cnt = gpt_e_blk_cnt;
640 if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_e) != cnt) {
641 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
642 __func__, "Primary GPT Entries", cnt, lba);
643 return 1;
644 }
645
646 prepare_backup_gpt_header(gpt_h);
647
648 /* write Backup GPT */
649 lba = le64_to_cpu(gpt_h->partition_entry_lba);
650 cnt = gpt_e_blk_cnt;
651 if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_e) != cnt) {
652 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
653 __func__, "Backup GPT Entries", cnt, lba);
654 return 1;
655 }
656
657 lba = le64_to_cpu(gpt_h->my_lba);
658 cnt = 1; /* GPT Header (1 block) */
659 if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_h) != cnt) {
660 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
661 __func__, "Backup GPT Header", cnt, lba);
662 return 1;
663 }
664
665 return 0;
666}
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100667#endif
668
richardretanubun07f3d782008-09-26 11:13:22 -0400669/*
670 * Private functions
671 */
672/*
673 * pmbr_part_valid(): Check for EFI partition signature
674 *
675 * Returns: 1 if EFI GPT partition type is found.
676 */
677static int pmbr_part_valid(struct partition *part)
678{
679 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich8faefad2013-03-29 07:57:10 +0000680 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubun07f3d782008-09-26 11:13:22 -0400681 return 1;
682 }
683
684 return 0;
685}
686
687/*
688 * is_pmbr_valid(): test Protective MBR for validity
689 *
690 * Returns: 1 if PMBR is valid, 0 otherwise.
691 * Validity depends on two things:
692 * 1) MSDOS signature is in the last two bytes of the MBR
693 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
694 */
695static int is_pmbr_valid(legacy_mbr * mbr)
696{
697 int i = 0;
698
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100699 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubun07f3d782008-09-26 11:13:22 -0400700 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400701
702 for (i = 0; i < 4; i++) {
703 if (pmbr_part_valid(&mbr->partition_record[i])) {
704 return 1;
705 }
706 }
707 return 0;
708}
709
710/**
711 * is_gpt_valid() - tests one GPT header and PTEs for validity
712 *
713 * lba is the logical block address of the GPT header to test
714 * gpt is a GPT header ptr, filled on return.
715 * ptes is a PTEs ptr, filled on return.
716 *
717 * Description: returns 1 if valid, 0 on error.
718 * If valid, returns pointers to PTEs.
719 */
Steve Raee04350d2014-05-26 11:52:23 -0700720static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
721 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
richardretanubun07f3d782008-09-26 11:13:22 -0400722{
richardretanubun07f3d782008-09-26 11:13:22 -0400723 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000724 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400725 return 0;
726 }
727
728 /* Read GPT Header from device */
Steve Raee04350d2014-05-26 11:52:23 -0700729 if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head)
730 != 1) {
richardretanubun07f3d782008-09-26 11:13:22 -0400731 printf("*** ERROR: Can't read GPT header ***\n");
732 return 0;
733 }
734
Steve Raee1f6b0a2014-12-18 12:13:42 +0100735 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
richardretanubun07f3d782008-09-26 11:13:22 -0400736 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400737
738 /* Read and allocate Partition Table Entries */
739 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
740 if (*pgpt_pte == NULL) {
741 printf("GPT: Failed to allocate memory for PTE\n");
742 return 0;
743 }
744
Steve Raee1f6b0a2014-12-18 12:13:42 +0100745 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
Doug Andersondeb5ca82011-10-19 09:47:31 +0000746 free(*pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400747 return 0;
748 }
749
750 /* We're done, all's well */
751 return 1;
752}
753
754/**
755 * alloc_read_gpt_entries(): reads partition entries from disk
756 * @dev_desc
757 * @gpt - GPT header
758 *
759 * Description: Returns ptes on success, NULL on error.
760 * Allocates space for PTEs based on information found in @gpt.
761 * Notes: remember to free pte when you're done!
762 */
763static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
764 gpt_header * pgpt_head)
765{
Egbert Eichae1768a2013-04-09 06:03:36 +0000766 size_t count = 0, blk_cnt;
richardretanubun07f3d782008-09-26 11:13:22 -0400767 gpt_entry *pte = NULL;
768
769 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000770 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400771 return NULL;
772 }
773
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100774 count = le32_to_cpu(pgpt_head->num_partition_entries) *
775 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubun07f3d782008-09-26 11:13:22 -0400776
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100777 debug("%s: count = %u * %u = %zu\n", __func__,
778 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
779 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
richardretanubun07f3d782008-09-26 11:13:22 -0400780
781 /* Allocate memory for PTE, remember to FREE */
782 if (count != 0) {
Egbert Eichae1768a2013-04-09 06:03:36 +0000783 pte = memalign(ARCH_DMA_MINALIGN,
784 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubun07f3d782008-09-26 11:13:22 -0400785 }
786
787 if (count == 0 || pte == NULL) {
Taylor Hutt9936be32012-10-12 14:26:09 +0000788 printf("%s: ERROR: Can't allocate 0x%zX "
789 "bytes for GPT Entries\n",
Doug Andersondf70b1c2011-10-19 08:04:46 +0000790 __func__, count);
richardretanubun07f3d782008-09-26 11:13:22 -0400791 return NULL;
792 }
793
794 /* Read GPT Entries from device */
Egbert Eichae1768a2013-04-09 06:03:36 +0000795 blk_cnt = BLOCK_CNT(count, dev_desc);
richardretanubun07f3d782008-09-26 11:13:22 -0400796 if (dev_desc->block_read (dev_desc->dev,
Steve Raee04350d2014-05-26 11:52:23 -0700797 (lbaint_t)le64_to_cpu(pgpt_head->partition_entry_lba),
Egbert Eichae1768a2013-04-09 06:03:36 +0000798 (lbaint_t) (blk_cnt), pte)
799 != blk_cnt) {
richardretanubun07f3d782008-09-26 11:13:22 -0400800
801 printf("*** ERROR: Can't read GPT Entries ***\n");
802 free(pte);
803 return NULL;
804 }
805 return pte;
806}
807
808/**
809 * is_pte_valid(): validates a single Partition Table Entry
810 * @gpt_entry - Pointer to a single Partition Table Entry
811 *
812 * Description: returns 1 if valid, 0 on error.
813 */
814static int is_pte_valid(gpt_entry * pte)
815{
816 efi_guid_t unused_guid;
817
818 if (!pte) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000819 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400820 return 0;
821 }
822
823 /* Only one validation for now:
824 * The GUID Partition Type != Unused Entry (ALL-ZERO)
825 */
826 memset(unused_guid.b, 0, sizeof(unused_guid.b));
827
828 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
829 sizeof(unused_guid.b)) == 0) {
830
Doug Andersondf70b1c2011-10-19 08:04:46 +0000831 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt9936be32012-10-12 14:26:09 +0000832 (unsigned int)(uintptr_t)pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400833
834 return 0;
835 } else {
836 return 1;
837 }
838}
839#endif