blob: 3471b755e1560dfb09f6cbde841ca30f351e793b [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);
Simon Glass4101f682016-02-29 15:25:34 -070044static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
Steve Raee04350d2014-05-26 11:52:23 -070045 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
Simon Glass4101f682016-02-29 15:25:34 -070046static gpt_entry *alloc_read_gpt_entries(struct blk_desc *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
Simon Glass4101f682016-02-29 15:25:34 -0700179void print_part_efi(struct blk_desc *dev_desc)
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000180{
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);
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100226#ifdef CONFIG_PARTITION_TYPE_GUID
227 if (!uuid_guid_get_str(uuid_bin, uuid))
228 printf("\ttype:\t%s\n", uuid);
229#endif
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200230 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200231 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
232 printf("\tguid:\t%s\n", uuid);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000233 }
234
235 /* Remember to free pte */
236 free(gpt_pte);
237 return;
238}
Stephen Warren894bfbb2012-09-21 09:50:59 +0000239
Simon Glass4101f682016-02-29 15:25:34 -0700240int get_partition_info_efi(struct blk_desc *dev_desc, int part,
241 disk_partition_t *info)
richardretanubun07f3d782008-09-26 11:13:22 -0400242{
Egbert Eichae1768a2013-04-09 06:03:36 +0000243 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Andersondeb5ca82011-10-19 09:47:31 +0000244 gpt_entry *gpt_pte = NULL;
richardretanubun07f3d782008-09-26 11:13:22 -0400245
246 /* "part" argument must be at least 1 */
247 if (!dev_desc || !info || part < 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000248 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400249 return -1;
250 }
251
252 /* This function validates AND fills in the GPT header and PTE */
253 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren4715a812011-10-28 09:21:46 +0000254 gpt_head, &gpt_pte) != 1) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000255 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-05-05 13:00:08 -0700256 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
257 gpt_head, &gpt_pte) != 1) {
258 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
259 __func__);
260 return -1;
261 } else {
262 printf("%s: *** Using Backup GPT ***\n",
263 __func__);
264 }
richardretanubun07f3d782008-09-26 11:13:22 -0400265 }
266
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100267 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000268 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500269 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000270 __func__, part);
Mark Langsdorf6d2ee5a2013-09-10 15:14:56 -0500271 free(gpt_pte);
Stephen Warrenc04d68c2012-09-21 09:50:58 +0000272 return -1;
273 }
274
Steve Raee04350d2014-05-26 11:52:23 -0700275 /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
276 info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
Richard Retanubun50970832009-01-26 08:45:14 -0500277 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Steve Raee04350d2014-05-26 11:52:23 -0700278 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
Richard Retanubun50970832009-01-26 08:45:14 -0500279 - info->start;
Egbert Eichae1768a2013-04-09 06:03:36 +0000280 info->blksz = dev_desc->blksz;
richardretanubun07f3d782008-09-26 11:13:22 -0400281
Lei Wen6eecc032011-09-07 18:11:19 +0000282 sprintf((char *)info->name, "%s",
Doug Andersondeb5ca82011-10-19 09:47:31 +0000283 print_efiname(&gpt_pte[part - 1]));
Ben Whitten192bc692015-12-30 13:05:58 +0000284 strcpy((char *)info->type, "U-Boot");
Stephen Warrenb4414f42012-10-08 08:14:37 +0000285 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000286#ifdef CONFIG_PARTITION_UUIDS
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200287 uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
288 UUID_STR_FORMAT_GUID);
Stephen Warren894bfbb2012-09-21 09:50:59 +0000289#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100290#ifdef CONFIG_PARTITION_TYPE_GUID
291 uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
292 info->type_guid, UUID_STR_FORMAT_GUID);
293#endif
richardretanubun07f3d782008-09-26 11:13:22 -0400294
Steve Rae60bf9412014-05-26 11:52:24 -0700295 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
Frederic Leroy04735e92013-06-26 18:11:25 +0200296 info->start, info->size, info->name);
richardretanubun07f3d782008-09-26 11:13:22 -0400297
298 /* Remember to free pte */
Doug Andersondeb5ca82011-10-19 09:47:31 +0000299 free(gpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400300 return 0;
301}
302
Simon Glass4101f682016-02-29 15:25:34 -0700303int get_partition_info_efi_by_name(struct blk_desc *dev_desc,
Steve Rae60bf9412014-05-26 11:52:24 -0700304 const char *name, disk_partition_t *info)
305{
306 int ret;
307 int i;
308 for (i = 1; i < GPT_ENTRY_NUMBERS; i++) {
309 ret = get_partition_info_efi(dev_desc, i, info);
310 if (ret != 0) {
311 /* no more entries in table */
312 return -1;
313 }
314 if (strcmp(name, (const char *)info->name) == 0) {
315 /* matched */
316 return 0;
317 }
318 }
319 return -2;
320}
321
Simon Glass96e5b032016-02-29 15:25:47 -0700322static int test_part_efi(struct blk_desc *dev_desc)
richardretanubun07f3d782008-09-26 11:13:22 -0400323{
Egbert Eichae1768a2013-04-09 06:03:36 +0000324 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubun07f3d782008-09-26 11:13:22 -0400325
326 /* Read legacy MBR from block 0 and validate it */
Stephen Warren7c4213f2015-12-07 11:38:48 -0700327 if ((dev_desc->block_read(dev_desc, 0, 1, (ulong *)legacymbr) != 1)
Anton staaff75dd582011-10-12 13:56:04 +0000328 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400329 return -1;
330 }
331 return 0;
332}
333
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100334/**
335 * set_protective_mbr(): Set the EFI protective MBR
336 * @param dev_desc - block device descriptor
337 *
338 * @return - zero on success, otherwise error
339 */
Simon Glass4101f682016-02-29 15:25:34 -0700340static int set_protective_mbr(struct blk_desc *dev_desc)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100341{
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100342 /* Setup the Protective MBR */
Hector Palacios61fcc7d2014-02-13 09:48:24 +0100343 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
344 memset(p_mbr, 0, sizeof(*p_mbr));
345
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100346 if (p_mbr == NULL) {
347 printf("%s: calloc failed!\n", __func__);
348 return -1;
349 }
350 /* Append signature */
351 p_mbr->signature = MSDOS_MBR_SIGNATURE;
352 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
353 p_mbr->partition_record[0].start_sect = 1;
Maxime Ripardb349abb2015-01-08 12:26:44 +0100354 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100355
356 /* Write MBR sector to the MMC device */
Stephen Warren7c4213f2015-12-07 11:38:48 -0700357 if (dev_desc->block_write(dev_desc, 0, 1, p_mbr) != 1) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100358 printf("** Can't write to device %d **\n",
359 dev_desc->dev);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100360 return -1;
361 }
362
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100363 return 0;
364}
365
Simon Glass4101f682016-02-29 15:25:34 -0700366int write_gpt_table(struct blk_desc *dev_desc,
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100367 gpt_header *gpt_h, gpt_entry *gpt_e)
368{
Egbert Eichae1768a2013-04-09 06:03:36 +0000369 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
370 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100371 u32 calc_crc32;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100372
373 debug("max lba: %x\n", (u32) dev_desc->lba);
374 /* Setup the Protective MBR */
375 if (set_protective_mbr(dev_desc) < 0)
376 goto err;
377
378 /* Generate CRC for the Primary GPT Header */
379 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
380 le32_to_cpu(gpt_h->num_partition_entries) *
381 le32_to_cpu(gpt_h->sizeof_partition_entry));
382 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
383
384 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
385 le32_to_cpu(gpt_h->header_size));
386 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
387
388 /* Write the First GPT to the block right after the Legacy MBR */
Stephen Warren7c4213f2015-12-07 11:38:48 -0700389 if (dev_desc->block_write(dev_desc, 1, 1, gpt_h) != 1)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100390 goto err;
391
Stephen Warren7c4213f2015-12-07 11:38:48 -0700392 if (dev_desc->block_write(dev_desc, 2, pte_blk_cnt, gpt_e)
Egbert Eichae1768a2013-04-09 06:03:36 +0000393 != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100394 goto err;
395
Steve Raee1f6b0a2014-12-18 12:13:42 +0100396 prepare_backup_gpt_header(gpt_h);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100397
Stephen Warren7c4213f2015-12-07 11:38:48 -0700398 if (dev_desc->block_write(dev_desc,
Steve Raee04350d2014-05-26 11:52:23 -0700399 (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
400 + 1,
Egbert Eichae1768a2013-04-09 06:03:36 +0000401 pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100402 goto err;
403
Stephen Warren7c4213f2015-12-07 11:38:48 -0700404 if (dev_desc->block_write(dev_desc,
Steve Raee04350d2014-05-26 11:52:23 -0700405 (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
406 gpt_h) != 1)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100407 goto err;
408
409 debug("GPT successfully written to block device!\n");
410 return 0;
411
412 err:
413 printf("** Can't write to device %d **\n", dev_desc->dev);
414 return -1;
415}
416
417int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
418 disk_partition_t *partitions, int parts)
419{
Steve Raee04350d2014-05-26 11:52:23 -0700420 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
421 lbaint_t start;
422 lbaint_t last_usable_lba = (lbaint_t)
423 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100424 int i, k;
Marek Vasut67cd4a62013-05-19 12:53:34 +0000425 size_t efiname_len, dosname_len;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100426#ifdef CONFIG_PARTITION_UUIDS
427 char *str_uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200428 unsigned char *bin_uuid;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100429#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100430#ifdef CONFIG_PARTITION_TYPE_GUID
431 char *str_type_guid;
432 unsigned char *bin_type_guid;
433#endif
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100434
435 for (i = 0; i < parts; i++) {
436 /* partition starting lba */
437 start = partitions[i].start;
438 if (start && (start < offset)) {
439 printf("Partition overlap\n");
440 return -1;
441 }
442 if (start) {
443 gpt_e[i].starting_lba = cpu_to_le64(start);
444 offset = start + partitions[i].size;
445 } else {
446 gpt_e[i].starting_lba = cpu_to_le64(offset);
447 offset += partitions[i].size;
448 }
Steve Raededf37b2014-05-26 11:52:22 -0700449 if (offset >= last_usable_lba) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100450 printf("Partitions layout exceds disk size\n");
451 return -1;
452 }
453 /* partition ending lba */
454 if ((i == parts - 1) && (partitions[i].size == 0))
455 /* extend the last partition to maximuim */
456 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
457 else
458 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
459
Patrick Delaunay7561b252015-10-27 11:00:27 +0100460#ifdef CONFIG_PARTITION_TYPE_GUID
461 str_type_guid = partitions[i].type_guid;
462 bin_type_guid = gpt_e[i].partition_type_guid.b;
463 if (strlen(str_type_guid)) {
464 if (uuid_str_to_bin(str_type_guid, bin_type_guid,
465 UUID_STR_FORMAT_GUID)) {
466 printf("Partition no. %d: invalid type guid: %s\n",
467 i, str_type_guid);
468 return -1;
469 }
470 } else {
471 /* default partition type GUID */
472 memcpy(bin_type_guid,
473 &PARTITION_BASIC_DATA_GUID, 16);
474 }
475#else
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100476 /* partition type GUID */
477 memcpy(gpt_e[i].partition_type_guid.b,
478 &PARTITION_BASIC_DATA_GUID, 16);
Patrick Delaunay7561b252015-10-27 11:00:27 +0100479#endif
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100480
481#ifdef CONFIG_PARTITION_UUIDS
482 str_uuid = partitions[i].uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200483 bin_uuid = gpt_e[i].unique_partition_guid.b;
484
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200485 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100486 printf("Partition no. %d: invalid guid: %s\n",
487 i, str_uuid);
488 return -1;
489 }
490#endif
491
492 /* partition attributes */
493 memset(&gpt_e[i].attributes, 0,
494 sizeof(gpt_entry_attributes));
495
Patrick Delaunaycfdaf4c2015-11-17 11:36:52 +0100496 if (partitions[i].bootable)
497 gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
498
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100499 /* partition name */
Marek Vasut67cd4a62013-05-19 12:53:34 +0000500 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100501 / sizeof(efi_char16_t);
Marek Vasut67cd4a62013-05-19 12:53:34 +0000502 dosname_len = sizeof(partitions[i].name);
503
504 memset(gpt_e[i].partition_name, 0,
505 sizeof(gpt_e[i].partition_name));
506
507 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100508 gpt_e[i].partition_name[k] =
509 (efi_char16_t)(partitions[i].name[k]);
510
Steve Raee04350d2014-05-26 11:52:23 -0700511 debug("%s: name: %s offset[%d]: 0x" LBAF
512 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100513 __func__, partitions[i].name, i,
514 offset, i, partitions[i].size);
515 }
516
517 return 0;
518}
519
Simon Glass4101f682016-02-29 15:25:34 -0700520int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100521 char *str_guid, int parts_count)
522{
523 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
524 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
525 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
526 gpt_h->my_lba = cpu_to_le64(1);
527 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
528 gpt_h->first_usable_lba = cpu_to_le64(34);
529 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
530 gpt_h->partition_entry_lba = cpu_to_le64(2);
531 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
532 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
533 gpt_h->header_crc32 = 0;
534 gpt_h->partition_entry_array_crc32 = 0;
535
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200536 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100537 return -1;
538
539 return 0;
540}
541
Simon Glass4101f682016-02-29 15:25:34 -0700542int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100543 disk_partition_t *partitions, int parts_count)
544{
545 int ret;
546
Egbert Eichae1768a2013-04-09 06:03:36 +0000547 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
548 dev_desc));
549 gpt_entry *gpt_e;
550
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100551 if (gpt_h == NULL) {
552 printf("%s: calloc failed!\n", __func__);
553 return -1;
554 }
555
Egbert Eichae1768a2013-04-09 06:03:36 +0000556 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
557 * sizeof(gpt_entry),
558 dev_desc));
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100559 if (gpt_e == NULL) {
560 printf("%s: calloc failed!\n", __func__);
561 free(gpt_h);
562 return -1;
563 }
564
565 /* Generate Primary GPT header (LBA1) */
566 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
567 if (ret)
568 goto err;
569
570 /* Generate partition entries */
571 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
572 if (ret)
573 goto err;
574
575 /* Write GPT partition table */
576 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
577
578err:
579 free(gpt_e);
580 free(gpt_h);
581 return ret;
582}
Steve Rae0ff7e582014-12-12 15:51:54 -0800583
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100584static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n)
585{
586 char *ess = (char *)es;
587 int i, j;
588
589 memset(s, '\0', n);
590
591 for (i = 0, j = 0; j < n; i += 2, j++) {
592 s[j] = ess[i];
593 if (!ess[i])
594 return;
595 }
596}
597
Simon Glass4101f682016-02-29 15:25:34 -0700598int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100599 gpt_entry **gpt_pte)
600{
601 /*
602 * This function validates AND
603 * fills in the GPT header and PTE
604 */
605 if (is_gpt_valid(dev_desc,
606 GPT_PRIMARY_PARTITION_TABLE_LBA,
607 gpt_head, gpt_pte) != 1) {
608 printf("%s: *** ERROR: Invalid GPT ***\n",
609 __func__);
610 return -1;
611 }
612 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
613 gpt_head, gpt_pte) != 1) {
614 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
615 __func__);
616 return -1;
617 }
618
619 return 0;
620}
621
Simon Glass4101f682016-02-29 15:25:34 -0700622int gpt_verify_partitions(struct blk_desc *dev_desc,
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100623 disk_partition_t *partitions, int parts,
624 gpt_header *gpt_head, gpt_entry **gpt_pte)
625{
626 char efi_str[PARTNAME_SZ + 1];
627 u64 gpt_part_size;
628 gpt_entry *gpt_e;
629 int ret, i;
630
631 ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
632 if (ret)
633 return ret;
634
635 gpt_e = *gpt_pte;
636
637 for (i = 0; i < parts; i++) {
638 if (i == gpt_head->num_partition_entries) {
639 error("More partitions than allowed!\n");
640 return -1;
641 }
642
643 /* Check if GPT and ENV partition names match */
644 gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
645 PARTNAME_SZ + 1);
646
647 debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
648 __func__, i, efi_str, partitions[i].name);
649
650 if (strncmp(efi_str, (char *)partitions[i].name,
651 sizeof(partitions->name))) {
652 error("Partition name: %s does not match %s!\n",
653 efi_str, (char *)partitions[i].name);
654 return -1;
655 }
656
657 /* Check if GPT and ENV sizes match */
658 gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
659 le64_to_cpu(gpt_e[i].starting_lba) + 1;
660 debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
Simon Glassf8d61652016-02-29 15:25:36 -0700661 (unsigned long long)gpt_part_size,
662 (unsigned long long)partitions[i].size);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100663
664 if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
665 error("Partition %s size: %llu does not match %llu!\n",
Simon Glassf8d61652016-02-29 15:25:36 -0700666 efi_str, (unsigned long long)gpt_part_size,
667 (unsigned long long)partitions[i].size);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100668 return -1;
669 }
670
671 /*
672 * Start address is optional - check only if provided
673 * in '$partition' variable
674 */
675 if (!partitions[i].start) {
676 debug("\n");
677 continue;
678 }
679
680 /* Check if GPT and ENV start LBAs match */
681 debug("start LBA - GPT: %8llu, ENV: %8llu\n",
682 le64_to_cpu(gpt_e[i].starting_lba),
Simon Glassf8d61652016-02-29 15:25:36 -0700683 (unsigned long long)partitions[i].start);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100684
685 if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
686 error("Partition %s start: %llu does not match %llu!\n",
687 efi_str, le64_to_cpu(gpt_e[i].starting_lba),
Simon Glassf8d61652016-02-29 15:25:36 -0700688 (unsigned long long)partitions[i].start);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100689 return -1;
690 }
691 }
692
693 return 0;
694}
695
Simon Glass4101f682016-02-29 15:25:34 -0700696int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf)
Steve Rae0ff7e582014-12-12 15:51:54 -0800697{
698 gpt_header *gpt_h;
699 gpt_entry *gpt_e;
700
701 /* determine start of GPT Header in the buffer */
702 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
703 dev_desc->blksz);
704 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
705 dev_desc->lba))
706 return -1;
707
708 /* determine start of GPT Entries in the buffer */
709 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
710 dev_desc->blksz);
711 if (validate_gpt_entries(gpt_h, gpt_e))
712 return -1;
713
714 return 0;
715}
716
Simon Glass4101f682016-02-29 15:25:34 -0700717int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
Steve Rae0ff7e582014-12-12 15:51:54 -0800718{
719 gpt_header *gpt_h;
720 gpt_entry *gpt_e;
721 int gpt_e_blk_cnt;
722 lbaint_t lba;
723 int cnt;
724
725 if (is_valid_gpt_buf(dev_desc, buf))
726 return -1;
727
728 /* determine start of GPT Header in the buffer */
729 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
730 dev_desc->blksz);
731
732 /* determine start of GPT Entries in the buffer */
733 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
734 dev_desc->blksz);
735 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
736 le32_to_cpu(gpt_h->sizeof_partition_entry)),
737 dev_desc);
738
739 /* write MBR */
740 lba = 0; /* MBR is always at 0 */
741 cnt = 1; /* MBR (1 block) */
Stephen Warren7c4213f2015-12-07 11:38:48 -0700742 if (dev_desc->block_write(dev_desc, lba, cnt, buf) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800743 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
744 __func__, "MBR", cnt, lba);
745 return 1;
746 }
747
748 /* write Primary GPT */
749 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
750 cnt = 1; /* GPT Header (1 block) */
Stephen Warren7c4213f2015-12-07 11:38:48 -0700751 if (dev_desc->block_write(dev_desc, lba, cnt, gpt_h) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800752 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
753 __func__, "Primary GPT Header", cnt, lba);
754 return 1;
755 }
756
757 lba = le64_to_cpu(gpt_h->partition_entry_lba);
758 cnt = gpt_e_blk_cnt;
Stephen Warren7c4213f2015-12-07 11:38:48 -0700759 if (dev_desc->block_write(dev_desc, lba, cnt, gpt_e) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800760 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
761 __func__, "Primary GPT Entries", cnt, lba);
762 return 1;
763 }
764
765 prepare_backup_gpt_header(gpt_h);
766
767 /* write Backup GPT */
768 lba = le64_to_cpu(gpt_h->partition_entry_lba);
769 cnt = gpt_e_blk_cnt;
Stephen Warren7c4213f2015-12-07 11:38:48 -0700770 if (dev_desc->block_write(dev_desc, lba, cnt, gpt_e) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800771 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
772 __func__, "Backup GPT Entries", cnt, lba);
773 return 1;
774 }
775
776 lba = le64_to_cpu(gpt_h->my_lba);
777 cnt = 1; /* GPT Header (1 block) */
Stephen Warren7c4213f2015-12-07 11:38:48 -0700778 if (dev_desc->block_write(dev_desc, lba, cnt, gpt_h) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800779 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
780 __func__, "Backup GPT Header", cnt, lba);
781 return 1;
782 }
783
784 return 0;
785}
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100786#endif
787
richardretanubun07f3d782008-09-26 11:13:22 -0400788/*
789 * Private functions
790 */
791/*
792 * pmbr_part_valid(): Check for EFI partition signature
793 *
794 * Returns: 1 if EFI GPT partition type is found.
795 */
796static int pmbr_part_valid(struct partition *part)
797{
798 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich8faefad2013-03-29 07:57:10 +0000799 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubun07f3d782008-09-26 11:13:22 -0400800 return 1;
801 }
802
803 return 0;
804}
805
806/*
807 * is_pmbr_valid(): test Protective MBR for validity
808 *
809 * Returns: 1 if PMBR is valid, 0 otherwise.
810 * Validity depends on two things:
811 * 1) MSDOS signature is in the last two bytes of the MBR
812 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
813 */
814static int is_pmbr_valid(legacy_mbr * mbr)
815{
816 int i = 0;
817
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100818 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubun07f3d782008-09-26 11:13:22 -0400819 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400820
821 for (i = 0; i < 4; i++) {
822 if (pmbr_part_valid(&mbr->partition_record[i])) {
823 return 1;
824 }
825 }
826 return 0;
827}
828
829/**
830 * is_gpt_valid() - tests one GPT header and PTEs for validity
831 *
832 * lba is the logical block address of the GPT header to test
833 * gpt is a GPT header ptr, filled on return.
834 * ptes is a PTEs ptr, filled on return.
835 *
836 * Description: returns 1 if valid, 0 on error.
837 * If valid, returns pointers to PTEs.
838 */
Simon Glass4101f682016-02-29 15:25:34 -0700839static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
Steve Raee04350d2014-05-26 11:52:23 -0700840 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
richardretanubun07f3d782008-09-26 11:13:22 -0400841{
richardretanubun07f3d782008-09-26 11:13:22 -0400842 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000843 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400844 return 0;
845 }
846
847 /* Read GPT Header from device */
Stephen Warren7c4213f2015-12-07 11:38:48 -0700848 if (dev_desc->block_read(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
richardretanubun07f3d782008-09-26 11:13:22 -0400849 printf("*** ERROR: Can't read GPT header ***\n");
850 return 0;
851 }
852
Steve Raee1f6b0a2014-12-18 12:13:42 +0100853 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
richardretanubun07f3d782008-09-26 11:13:22 -0400854 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400855
856 /* Read and allocate Partition Table Entries */
857 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
858 if (*pgpt_pte == NULL) {
859 printf("GPT: Failed to allocate memory for PTE\n");
860 return 0;
861 }
862
Steve Raee1f6b0a2014-12-18 12:13:42 +0100863 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
Doug Andersondeb5ca82011-10-19 09:47:31 +0000864 free(*pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400865 return 0;
866 }
867
868 /* We're done, all's well */
869 return 1;
870}
871
872/**
873 * alloc_read_gpt_entries(): reads partition entries from disk
874 * @dev_desc
875 * @gpt - GPT header
876 *
877 * Description: Returns ptes on success, NULL on error.
878 * Allocates space for PTEs based on information found in @gpt.
879 * Notes: remember to free pte when you're done!
880 */
Simon Glass4101f682016-02-29 15:25:34 -0700881static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
882 gpt_header *pgpt_head)
richardretanubun07f3d782008-09-26 11:13:22 -0400883{
Egbert Eichae1768a2013-04-09 06:03:36 +0000884 size_t count = 0, blk_cnt;
Stephen Warren7c4213f2015-12-07 11:38:48 -0700885 lbaint_t blk;
richardretanubun07f3d782008-09-26 11:13:22 -0400886 gpt_entry *pte = NULL;
887
888 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000889 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400890 return NULL;
891 }
892
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100893 count = le32_to_cpu(pgpt_head->num_partition_entries) *
894 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubun07f3d782008-09-26 11:13:22 -0400895
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100896 debug("%s: count = %u * %u = %zu\n", __func__,
897 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
898 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
richardretanubun07f3d782008-09-26 11:13:22 -0400899
900 /* Allocate memory for PTE, remember to FREE */
901 if (count != 0) {
Egbert Eichae1768a2013-04-09 06:03:36 +0000902 pte = memalign(ARCH_DMA_MINALIGN,
903 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubun07f3d782008-09-26 11:13:22 -0400904 }
905
906 if (count == 0 || pte == NULL) {
Taylor Hutt9936be32012-10-12 14:26:09 +0000907 printf("%s: ERROR: Can't allocate 0x%zX "
908 "bytes for GPT Entries\n",
Doug Andersondf70b1c2011-10-19 08:04:46 +0000909 __func__, count);
richardretanubun07f3d782008-09-26 11:13:22 -0400910 return NULL;
911 }
912
913 /* Read GPT Entries from device */
Stephen Warren7c4213f2015-12-07 11:38:48 -0700914 blk = le64_to_cpu(pgpt_head->partition_entry_lba);
Egbert Eichae1768a2013-04-09 06:03:36 +0000915 blk_cnt = BLOCK_CNT(count, dev_desc);
Stephen Warren7c4213f2015-12-07 11:38:48 -0700916 if (dev_desc->block_read(dev_desc, blk, (lbaint_t)blk_cnt, pte)
917 != blk_cnt) {
richardretanubun07f3d782008-09-26 11:13:22 -0400918 printf("*** ERROR: Can't read GPT Entries ***\n");
919 free(pte);
920 return NULL;
921 }
922 return pte;
923}
924
925/**
926 * is_pte_valid(): validates a single Partition Table Entry
927 * @gpt_entry - Pointer to a single Partition Table Entry
928 *
929 * Description: returns 1 if valid, 0 on error.
930 */
931static int is_pte_valid(gpt_entry * pte)
932{
933 efi_guid_t unused_guid;
934
935 if (!pte) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000936 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400937 return 0;
938 }
939
940 /* Only one validation for now:
941 * The GUID Partition Type != Unused Entry (ALL-ZERO)
942 */
943 memset(unused_guid.b, 0, sizeof(unused_guid.b));
944
945 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
946 sizeof(unused_guid.b)) == 0) {
947
Doug Andersondf70b1c2011-10-19 08:04:46 +0000948 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt9936be32012-10-12 14:26:09 +0000949 (unsigned int)(uintptr_t)pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400950
951 return 0;
952 } else {
953 return 1;
954 }
955}
Simon Glass96e5b032016-02-29 15:25:47 -0700956
957/*
958 * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to
959 * check EFI first, since a DOS partition is often used as a 'protective MBR'
960 * with EFI.
961 */
962U_BOOT_PART_TYPE(a_efi) = {
963 .name = "EFI",
964 .part_type = PART_TYPE_EFI,
965 .get_info = part_get_info_ptr(get_partition_info_efi),
966 .print = part_print_ptr(print_part_efi),
967 .test = test_part_efi,
968};
richardretanubun07f3d782008-09-26 11:13:22 -0400969#endif