blob: c74b7b91705b8eb1b21385127d5fbbc8d45cbe28 [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/*
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02009 * Problems with CONFIG_SYS_64BIT_LBA:
richardretanubun07f3d782008-09-26 11:13:22 -040010 *
11 * struct disk_partition.start in include/part.h is sized as ulong.
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020012 * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t.
richardretanubun07f3d782008-09-26 11:13:22 -040013 * For now, it is cast back to ulong at assignment.
14 *
15 * This limits the maximum size of addressable storage to < 2 Terra Bytes
16 */
Marc Dietrich8faefad2013-03-29 07:57:10 +000017#include <asm/unaligned.h>
richardretanubun07f3d782008-09-26 11:13:22 -040018#include <common.h>
19#include <command.h>
20#include <ide.h>
21#include <malloc.h>
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010022#include <part_efi.h>
Lei Wen6eecc032011-09-07 18:11:19 +000023#include <linux/ctype.h>
richardretanubun07f3d782008-09-26 11:13:22 -040024
Lukasz Majewski40684dd2012-12-11 11:09:46 +010025DECLARE_GLOBAL_DATA_PTR;
26
Stephen Warren2c1af9d2013-02-28 15:03:46 +000027#ifdef HAVE_BLOCK_DEVICE
richardretanubun07f3d782008-09-26 11:13:22 -040028/**
29 * efi_crc32() - EFI version of crc32 function
30 * @buf: buffer to calculate crc32 of
31 * @len - length of buf
32 *
33 * Description: Returns EFI-style CRC32 value for @buf
34 */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +010035static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubun07f3d782008-09-26 11:13:22 -040036{
37 return crc32(0, buf, len);
38}
39
40/*
41 * Private function prototypes
42 */
43
44static int pmbr_part_valid(struct partition *part);
45static int is_pmbr_valid(legacy_mbr * mbr);
richardretanubun07f3d782008-09-26 11:13:22 -040046static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
47 gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -040048static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
49 gpt_header * pgpt_head);
richardretanubun07f3d782008-09-26 11:13:22 -040050static int is_pte_valid(gpt_entry * pte);
51
Lei Wen6eecc032011-09-07 18:11:19 +000052static char *print_efiname(gpt_entry *pte)
53{
54 static char name[PARTNAME_SZ + 1];
55 int i;
56 for (i = 0; i < PARTNAME_SZ; i++) {
57 u8 c;
58 c = pte->partition_name[i] & 0xff;
59 c = (c && !isprint(c)) ? '.' : c;
60 name[i] = c;
61 }
62 name[PARTNAME_SZ] = 0;
63 return name;
64}
65
Stephen Warrenb4414f42012-10-08 08:14:37 +000066static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
67
68static inline int is_bootable(gpt_entry *p)
69{
70 return p->attributes.fields.legacy_bios_bootable ||
71 !memcmp(&(p->partition_type_guid), &system_guid,
72 sizeof(efi_guid_t));
73}
74
Lukasz Majewski40684dd2012-12-11 11:09:46 +010075#ifdef CONFIG_EFI_PARTITION
Stephen Warrenf07cd2c2012-10-08 08:14:34 +000076/*
77 * Public Functions (include/part.h)
78 */
79
80void print_part_efi(block_dev_desc_t * dev_desc)
81{
Egbert Eichae1768a2013-04-09 06:03:36 +000082 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +000083 gpt_entry *gpt_pte = NULL;
84 int i = 0;
85 char uuid[37];
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020086 unsigned char *uuid_bin;
Stephen Warrenf07cd2c2012-10-08 08:14:34 +000087
88 if (!dev_desc) {
89 printf("%s: Invalid Argument(s)\n", __func__);
90 return;
91 }
92 /* This function validates AND fills in the GPT header and PTE */
93 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
94 gpt_head, &gpt_pte) != 1) {
95 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-05-05 13:00:08 -070096 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
97 gpt_head, &gpt_pte) != 1) {
98 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
99 __func__);
100 return;
101 } else {
102 printf("%s: *** Using Backup GPT ***\n",
103 __func__);
104 }
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000105 }
106
107 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
108
109 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren13bf2f52012-10-08 08:14:36 +0000110 printf("\tAttributes\n");
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200111 printf("\tType GUID\n");
112 printf("\tPartition GUID\n");
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000113
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100114 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000115 /* Stop at the first non valid PTE */
116 if (!is_pte_valid(&gpt_pte[i]))
117 break;
118
119 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100120 le64_to_cpu(gpt_pte[i].starting_lba),
121 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000122 print_efiname(&gpt_pte[i]));
Stephen Warren13bf2f52012-10-08 08:14:36 +0000123 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200124 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200125 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000126 printf("\ttype:\t%s\n", uuid);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200127 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200128 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
129 printf("\tguid:\t%s\n", uuid);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000130 }
131
132 /* Remember to free pte */
133 free(gpt_pte);
134 return;
135}
Stephen Warren894bfbb2012-09-21 09:50:59 +0000136
richardretanubun07f3d782008-09-26 11:13:22 -0400137int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
138 disk_partition_t * info)
139{
Egbert Eichae1768a2013-04-09 06:03:36 +0000140 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Andersondeb5ca82011-10-19 09:47:31 +0000141 gpt_entry *gpt_pte = NULL;
richardretanubun07f3d782008-09-26 11:13:22 -0400142
143 /* "part" argument must be at least 1 */
144 if (!dev_desc || !info || part < 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000145 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400146 return -1;
147 }
148
149 /* This function validates AND fills in the GPT header and PTE */
150 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren4715a812011-10-28 09:21:46 +0000151 gpt_head, &gpt_pte) != 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000152 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-05-05 13:00:08 -0700153 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
154 gpt_head, &gpt_pte) != 1) {
155 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
156 __func__);
157 return -1;
158 } else {
159 printf("%s: *** Using Backup GPT ***\n",
160 __func__);
161 }
richardretanubun07f3d782008-09-26 11:13:22 -0400162 }
163
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100164 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000165 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500166 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000167 __func__, part);
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500168 free(gpt_pte);
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000169 return -1;
170 }
171
richardretanubun07f3d782008-09-26 11:13:22 -0400172 /* The ulong casting limits the maximum disk size to 2 TB */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100173 info->start = (u64)le64_to_cpu(gpt_pte[part - 1].starting_lba);
Richard Retanubun50970832009-01-26 08:45:14 -0500174 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100175 info->size = ((u64)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1)
Richard Retanubun50970832009-01-26 08:45:14 -0500176 - info->start;
Egbert Eichae1768a2013-04-09 06:03:36 +0000177 info->blksz = dev_desc->blksz;
richardretanubun07f3d782008-09-26 11:13:22 -0400178
Lei Wen6eecc032011-09-07 18:11:19 +0000179 sprintf((char *)info->name, "%s",
Doug Andersondeb5ca82011-10-19 09:47:31 +0000180 print_efiname(&gpt_pte[part - 1]));
richardretanubun07f3d782008-09-26 11:13:22 -0400181 sprintf((char *)info->type, "U-Boot");
Stephen Warrenb4414f42012-10-08 08:14:37 +0000182 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000183#ifdef CONFIG_PARTITION_UUIDS
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200184 uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
185 UUID_STR_FORMAT_GUID);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000186#endif
richardretanubun07f3d782008-09-26 11:13:22 -0400187
Frederic Leroy04735e92013-06-26 18:11:25 +0200188 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s", __func__,
189 info->start, info->size, info->name);
richardretanubun07f3d782008-09-26 11:13:22 -0400190
191 /* Remember to free pte */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000192 free(gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400193 return 0;
194}
195
196int test_part_efi(block_dev_desc_t * dev_desc)
197{
Egbert Eichae1768a2013-04-09 06:03:36 +0000198 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubun07f3d782008-09-26 11:13:22 -0400199
200 /* Read legacy MBR from block 0 and validate it */
Anton staaff75dd582011-10-12 13:56:04 +0000201 if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
202 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400203 return -1;
204 }
205 return 0;
206}
207
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100208/**
209 * set_protective_mbr(): Set the EFI protective MBR
210 * @param dev_desc - block device descriptor
211 *
212 * @return - zero on success, otherwise error
213 */
214static int set_protective_mbr(block_dev_desc_t *dev_desc)
215{
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100216 /* Setup the Protective MBR */
Hector Palacios61fcc7d2014-02-13 09:48:24 +0100217 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
218 memset(p_mbr, 0, sizeof(*p_mbr));
219
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100220 if (p_mbr == NULL) {
221 printf("%s: calloc failed!\n", __func__);
222 return -1;
223 }
224 /* Append signature */
225 p_mbr->signature = MSDOS_MBR_SIGNATURE;
226 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
227 p_mbr->partition_record[0].start_sect = 1;
228 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba;
229
230 /* Write MBR sector to the MMC device */
231 if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
232 printf("** Can't write to device %d **\n",
233 dev_desc->dev);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100234 return -1;
235 }
236
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100237 return 0;
238}
239
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100240int write_gpt_table(block_dev_desc_t *dev_desc,
241 gpt_header *gpt_h, gpt_entry *gpt_e)
242{
Egbert Eichae1768a2013-04-09 06:03:36 +0000243 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
244 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100245 u32 calc_crc32;
246 u64 val;
247
248 debug("max lba: %x\n", (u32) dev_desc->lba);
249 /* Setup the Protective MBR */
250 if (set_protective_mbr(dev_desc) < 0)
251 goto err;
252
253 /* Generate CRC for the Primary GPT Header */
254 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
255 le32_to_cpu(gpt_h->num_partition_entries) *
256 le32_to_cpu(gpt_h->sizeof_partition_entry));
257 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
258
259 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
260 le32_to_cpu(gpt_h->header_size));
261 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
262
263 /* Write the First GPT to the block right after the Legacy MBR */
264 if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
265 goto err;
266
Egbert Eichae1768a2013-04-09 06:03:36 +0000267 if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e)
268 != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100269 goto err;
270
Steve Raeae95fad2014-05-05 13:00:08 -0700271 /* recalculate the values for the Backup GPT Header */
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100272 val = le64_to_cpu(gpt_h->my_lba);
273 gpt_h->my_lba = gpt_h->alternate_lba;
274 gpt_h->alternate_lba = cpu_to_le64(val);
275 gpt_h->header_crc32 = 0;
276
277 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
278 le32_to_cpu(gpt_h->header_size));
279 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
280
281 if (dev_desc->block_write(dev_desc->dev,
282 le32_to_cpu(gpt_h->last_usable_lba + 1),
Egbert Eichae1768a2013-04-09 06:03:36 +0000283 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100284 goto err;
285
286 if (dev_desc->block_write(dev_desc->dev,
287 le32_to_cpu(gpt_h->my_lba), 1, gpt_h) != 1)
288 goto err;
289
290 debug("GPT successfully written to block device!\n");
291 return 0;
292
293 err:
294 printf("** Can't write to device %d **\n", dev_desc->dev);
295 return -1;
296}
297
298int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
299 disk_partition_t *partitions, int parts)
300{
301 u32 offset = (u32)le32_to_cpu(gpt_h->first_usable_lba);
302 ulong start;
303 int i, k;
Marek Vasut67cd4a62013-05-19 12:53:34 +0000304 size_t efiname_len, dosname_len;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100305#ifdef CONFIG_PARTITION_UUIDS
306 char *str_uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200307 unsigned char *bin_uuid;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100308#endif
309
310 for (i = 0; i < parts; i++) {
311 /* partition starting lba */
312 start = partitions[i].start;
313 if (start && (start < offset)) {
314 printf("Partition overlap\n");
315 return -1;
316 }
317 if (start) {
318 gpt_e[i].starting_lba = cpu_to_le64(start);
319 offset = start + partitions[i].size;
320 } else {
321 gpt_e[i].starting_lba = cpu_to_le64(offset);
322 offset += partitions[i].size;
323 }
324 if (offset >= gpt_h->last_usable_lba) {
325 printf("Partitions layout exceds disk size\n");
326 return -1;
327 }
328 /* partition ending lba */
329 if ((i == parts - 1) && (partitions[i].size == 0))
330 /* extend the last partition to maximuim */
331 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
332 else
333 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
334
335 /* partition type GUID */
336 memcpy(gpt_e[i].partition_type_guid.b,
337 &PARTITION_BASIC_DATA_GUID, 16);
338
339#ifdef CONFIG_PARTITION_UUIDS
340 str_uuid = partitions[i].uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200341 bin_uuid = gpt_e[i].unique_partition_guid.b;
342
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200343 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100344 printf("Partition no. %d: invalid guid: %s\n",
345 i, str_uuid);
346 return -1;
347 }
348#endif
349
350 /* partition attributes */
351 memset(&gpt_e[i].attributes, 0,
352 sizeof(gpt_entry_attributes));
353
354 /* partition name */
Marek Vasut67cd4a62013-05-19 12:53:34 +0000355 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100356 / sizeof(efi_char16_t);
Marek Vasut67cd4a62013-05-19 12:53:34 +0000357 dosname_len = sizeof(partitions[i].name);
358
359 memset(gpt_e[i].partition_name, 0,
360 sizeof(gpt_e[i].partition_name));
361
362 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100363 gpt_e[i].partition_name[k] =
364 (efi_char16_t)(partitions[i].name[k]);
365
Frederic Leroy04735e92013-06-26 18:11:25 +0200366 debug("%s: name: %s offset[%d]: 0x%x size[%d]: 0x" LBAF "\n",
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100367 __func__, partitions[i].name, i,
368 offset, i, partitions[i].size);
369 }
370
371 return 0;
372}
373
374int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
375 char *str_guid, int parts_count)
376{
377 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
378 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
379 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
380 gpt_h->my_lba = cpu_to_le64(1);
381 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
382 gpt_h->first_usable_lba = cpu_to_le64(34);
383 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
384 gpt_h->partition_entry_lba = cpu_to_le64(2);
385 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
386 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
387 gpt_h->header_crc32 = 0;
388 gpt_h->partition_entry_array_crc32 = 0;
389
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200390 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100391 return -1;
392
393 return 0;
394}
395
396int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
397 disk_partition_t *partitions, int parts_count)
398{
399 int ret;
400
Egbert Eichae1768a2013-04-09 06:03:36 +0000401 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
402 dev_desc));
403 gpt_entry *gpt_e;
404
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100405 if (gpt_h == NULL) {
406 printf("%s: calloc failed!\n", __func__);
407 return -1;
408 }
409
Egbert Eichae1768a2013-04-09 06:03:36 +0000410 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
411 * sizeof(gpt_entry),
412 dev_desc));
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100413 if (gpt_e == NULL) {
414 printf("%s: calloc failed!\n", __func__);
415 free(gpt_h);
416 return -1;
417 }
418
419 /* Generate Primary GPT header (LBA1) */
420 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
421 if (ret)
422 goto err;
423
424 /* Generate partition entries */
425 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
426 if (ret)
427 goto err;
428
429 /* Write GPT partition table */
430 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
431
432err:
433 free(gpt_e);
434 free(gpt_h);
435 return ret;
436}
437#endif
438
richardretanubun07f3d782008-09-26 11:13:22 -0400439/*
440 * Private functions
441 */
442/*
443 * pmbr_part_valid(): Check for EFI partition signature
444 *
445 * Returns: 1 if EFI GPT partition type is found.
446 */
447static int pmbr_part_valid(struct partition *part)
448{
449 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich8faefad2013-03-29 07:57:10 +0000450 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubun07f3d782008-09-26 11:13:22 -0400451 return 1;
452 }
453
454 return 0;
455}
456
457/*
458 * is_pmbr_valid(): test Protective MBR for validity
459 *
460 * Returns: 1 if PMBR is valid, 0 otherwise.
461 * Validity depends on two things:
462 * 1) MSDOS signature is in the last two bytes of the MBR
463 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
464 */
465static int is_pmbr_valid(legacy_mbr * mbr)
466{
467 int i = 0;
468
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100469 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubun07f3d782008-09-26 11:13:22 -0400470 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400471
472 for (i = 0; i < 4; i++) {
473 if (pmbr_part_valid(&mbr->partition_record[i])) {
474 return 1;
475 }
476 }
477 return 0;
478}
479
480/**
481 * is_gpt_valid() - tests one GPT header and PTEs for validity
482 *
483 * lba is the logical block address of the GPT header to test
484 * gpt is a GPT header ptr, filled on return.
485 * ptes is a PTEs ptr, filled on return.
486 *
487 * Description: returns 1 if valid, 0 on error.
488 * If valid, returns pointers to PTEs.
489 */
490static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
491 gpt_header * pgpt_head, gpt_entry ** pgpt_pte)
492{
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100493 u32 crc32_backup = 0;
494 u32 calc_crc32;
richardretanubun07f3d782008-09-26 11:13:22 -0400495 unsigned long long lastlba;
496
497 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000498 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400499 return 0;
500 }
501
502 /* Read GPT Header from device */
503 if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) {
504 printf("*** ERROR: Can't read GPT header ***\n");
505 return 0;
506 }
507
508 /* Check the GPT header signature */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100509 if (le64_to_cpu(pgpt_head->signature) != GPT_HEADER_SIGNATURE) {
richardretanubun07f3d782008-09-26 11:13:22 -0400510 printf("GUID Partition Table Header signature is wrong:"
511 "0x%llX != 0x%llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100512 le64_to_cpu(pgpt_head->signature),
513 GPT_HEADER_SIGNATURE);
richardretanubun07f3d782008-09-26 11:13:22 -0400514 return 0;
515 }
516
517 /* Check the GUID Partition Table CRC */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100518 memcpy(&crc32_backup, &pgpt_head->header_crc32, sizeof(crc32_backup));
519 memset(&pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32));
richardretanubun07f3d782008-09-26 11:13:22 -0400520
521 calc_crc32 = efi_crc32((const unsigned char *)pgpt_head,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100522 le32_to_cpu(pgpt_head->header_size));
richardretanubun07f3d782008-09-26 11:13:22 -0400523
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100524 memcpy(&pgpt_head->header_crc32, &crc32_backup, sizeof(crc32_backup));
richardretanubun07f3d782008-09-26 11:13:22 -0400525
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100526 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400527 printf("GUID Partition Table Header CRC is wrong:"
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100528 "0x%x != 0x%x\n",
529 le32_to_cpu(crc32_backup), calc_crc32);
richardretanubun07f3d782008-09-26 11:13:22 -0400530 return 0;
531 }
532
533 /* Check that the my_lba entry points to the LBA that contains the GPT */
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100534 if (le64_to_cpu(pgpt_head->my_lba) != lba) {
richardretanubun07f3d782008-09-26 11:13:22 -0400535 printf("GPT: my_lba incorrect: %llX != %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100536 le64_to_cpu(pgpt_head->my_lba),
537 lba);
richardretanubun07f3d782008-09-26 11:13:22 -0400538 return 0;
539 }
540
541 /* Check the first_usable_lba and last_usable_lba are within the disk. */
542 lastlba = (unsigned long long)dev_desc->lba;
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100543 if (le64_to_cpu(pgpt_head->first_usable_lba) > lastlba) {
richardretanubun07f3d782008-09-26 11:13:22 -0400544 printf("GPT: first_usable_lba incorrect: %llX > %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100545 le64_to_cpu(pgpt_head->first_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400546 return 0;
547 }
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100548 if (le64_to_cpu(pgpt_head->last_usable_lba) > lastlba) {
richardretanubun07f3d782008-09-26 11:13:22 -0400549 printf("GPT: last_usable_lba incorrect: %llX > %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100550 (u64) le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400551 return 0;
552 }
553
554 debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n",
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100555 le64_to_cpu(pgpt_head->first_usable_lba),
556 le64_to_cpu(pgpt_head->last_usable_lba), lastlba);
richardretanubun07f3d782008-09-26 11:13:22 -0400557
558 /* Read and allocate Partition Table Entries */
559 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
560 if (*pgpt_pte == NULL) {
561 printf("GPT: Failed to allocate memory for PTE\n");
562 return 0;
563 }
564
565 /* Check the GUID Partition Table Entry Array CRC */
566 calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100567 le32_to_cpu(pgpt_head->num_partition_entries) *
568 le32_to_cpu(pgpt_head->sizeof_partition_entry));
richardretanubun07f3d782008-09-26 11:13:22 -0400569
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100570 if (calc_crc32 != le32_to_cpu(pgpt_head->partition_entry_array_crc32)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400571 printf("GUID Partition Table Entry Array CRC is wrong:"
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100572 "0x%x != 0x%x\n",
573 le32_to_cpu(pgpt_head->partition_entry_array_crc32),
richardretanubun07f3d782008-09-26 11:13:22 -0400574 calc_crc32);
575
Doug Andersondeb5ca82011-10-19 09:47:31 +0000576 free(*pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400577 return 0;
578 }
579
580 /* We're done, all's well */
581 return 1;
582}
583
584/**
585 * alloc_read_gpt_entries(): reads partition entries from disk
586 * @dev_desc
587 * @gpt - GPT header
588 *
589 * Description: Returns ptes on success, NULL on error.
590 * Allocates space for PTEs based on information found in @gpt.
591 * Notes: remember to free pte when you're done!
592 */
593static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
594 gpt_header * pgpt_head)
595{
Egbert Eichae1768a2013-04-09 06:03:36 +0000596 size_t count = 0, blk_cnt;
richardretanubun07f3d782008-09-26 11:13:22 -0400597 gpt_entry *pte = NULL;
598
599 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000600 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400601 return NULL;
602 }
603
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100604 count = le32_to_cpu(pgpt_head->num_partition_entries) *
605 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubun07f3d782008-09-26 11:13:22 -0400606
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100607 debug("%s: count = %u * %u = %zu\n", __func__,
608 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
609 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
richardretanubun07f3d782008-09-26 11:13:22 -0400610
611 /* Allocate memory for PTE, remember to FREE */
612 if (count != 0) {
Egbert Eichae1768a2013-04-09 06:03:36 +0000613 pte = memalign(ARCH_DMA_MINALIGN,
614 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubun07f3d782008-09-26 11:13:22 -0400615 }
616
617 if (count == 0 || pte == NULL) {
Taylor Hutt9936be32012-10-12 14:26:09 +0000618 printf("%s: ERROR: Can't allocate 0x%zX "
619 "bytes for GPT Entries\n",
Doug Andersondf70b1c2011-10-19 08:04:46 +0000620 __func__, count);
richardretanubun07f3d782008-09-26 11:13:22 -0400621 return NULL;
622 }
623
624 /* Read GPT Entries from device */
Egbert Eichae1768a2013-04-09 06:03:36 +0000625 blk_cnt = BLOCK_CNT(count, dev_desc);
richardretanubun07f3d782008-09-26 11:13:22 -0400626 if (dev_desc->block_read (dev_desc->dev,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100627 le64_to_cpu(pgpt_head->partition_entry_lba),
Egbert Eichae1768a2013-04-09 06:03:36 +0000628 (lbaint_t) (blk_cnt), pte)
629 != blk_cnt) {
richardretanubun07f3d782008-09-26 11:13:22 -0400630
631 printf("*** ERROR: Can't read GPT Entries ***\n");
632 free(pte);
633 return NULL;
634 }
635 return pte;
636}
637
638/**
639 * is_pte_valid(): validates a single Partition Table Entry
640 * @gpt_entry - Pointer to a single Partition Table Entry
641 *
642 * Description: returns 1 if valid, 0 on error.
643 */
644static int is_pte_valid(gpt_entry * pte)
645{
646 efi_guid_t unused_guid;
647
648 if (!pte) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000649 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400650 return 0;
651 }
652
653 /* Only one validation for now:
654 * The GUID Partition Type != Unused Entry (ALL-ZERO)
655 */
656 memset(unused_guid.b, 0, sizeof(unused_guid.b));
657
658 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
659 sizeof(unused_guid.b)) == 0) {
660
Doug Andersondf70b1c2011-10-19 08:04:46 +0000661 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt9936be32012-10-12 14:26:09 +0000662 (unsigned int)(uintptr_t)pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400663
664 return 0;
665 } else {
666 return 1;
667 }
668}
669#endif