blob: db0c890d5b449987e2a4f1a530bfa8f2341476bc [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
Patrick Delaunaybd42a942017-01-27 11:00:41 +0100174#if CONFIG_IS_ENABLED(EFI_PARTITION)
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000175/*
176 * Public Functions (include/part.h)
177 */
178
Simon Glass084bf4c2016-02-29 15:26:04 -0700179void part_print_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
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000187 /* This function validates AND fills in the GPT header and PTE */
188 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
189 gpt_head, &gpt_pte) != 1) {
190 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raeae95fad2014-05-05 13:00:08 -0700191 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
192 gpt_head, &gpt_pte) != 1) {
193 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
194 __func__);
195 return;
196 } else {
197 printf("%s: *** Using Backup GPT ***\n",
198 __func__);
199 }
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000200 }
201
202 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
203
204 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren13bf2f52012-10-08 08:14:36 +0000205 printf("\tAttributes\n");
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200206 printf("\tType GUID\n");
207 printf("\tPartition GUID\n");
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000208
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100209 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000210 /* Stop at the first non valid PTE */
211 if (!is_pte_valid(&gpt_pte[i]))
212 break;
213
214 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100215 le64_to_cpu(gpt_pte[i].starting_lba),
216 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000217 print_efiname(&gpt_pte[i]));
Stephen Warren13bf2f52012-10-08 08:14:36 +0000218 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200219 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200220 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
Stephen Warrenf07cd2c2012-10-08 08:14:34 +0000221 printf("\ttype:\t%s\n", uuid);
Patrick Delaunaybcb41dc2015-10-27 11:00:28 +0100222#ifdef CONFIG_PARTITION_TYPE_GUID
223 if (!uuid_guid_get_str(uuid_bin, uuid))
224 printf("\ttype:\t%s\n", uuid);
225#endif
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
Simon Glass3e8bd462016-02-29 15:25:48 -0700236int part_get_info_efi(struct blk_desc *dev_desc, int part,
237 disk_partition_t *info)
richardretanubun07f3d782008-09-26 11:13:22 -0400238{
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 */
Simon Glass4708a072016-03-16 07:45:36 -0600243 if (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]));
Ben Whitten192bc692015-12-30 13:05:58 +0000280 strcpy((char *)info->type, "U-Boot");
Stephen Warrenb4414f42012-10-08 08:14:37 +0000281 info->bootable = is_bootable(&gpt_pte[part - 1]);
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100282#if CONFIG_IS_ENABLED(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
Simon Glass084bf4c2016-02-29 15:26:04 -0700299static int part_test_efi(struct blk_desc *dev_desc)
richardretanubun07f3d782008-09-26 11:13:22 -0400300{
Egbert Eichae1768a2013-04-09 06:03:36 +0000301 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubun07f3d782008-09-26 11:13:22 -0400302
303 /* Read legacy MBR from block 0 and validate it */
Simon Glass2a981dc2016-02-29 15:25:52 -0700304 if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1)
Anton staaff75dd582011-10-12 13:56:04 +0000305 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubun07f3d782008-09-26 11:13:22 -0400306 return -1;
307 }
308 return 0;
309}
310
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100311/**
312 * set_protective_mbr(): Set the EFI protective MBR
313 * @param dev_desc - block device descriptor
314 *
315 * @return - zero on success, otherwise error
316 */
Simon Glass4101f682016-02-29 15:25:34 -0700317static int set_protective_mbr(struct blk_desc *dev_desc)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100318{
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100319 /* Setup the Protective MBR */
Hector Palacios61fcc7d2014-02-13 09:48:24 +0100320 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
321 memset(p_mbr, 0, sizeof(*p_mbr));
322
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100323 if (p_mbr == NULL) {
324 printf("%s: calloc failed!\n", __func__);
325 return -1;
326 }
Vincent Tinellie163a932017-01-30 15:46:07 +0300327
328 /* Read MBR to backup boot code if it exists */
329 if (blk_dread(dev_desc, 0, 1, p_mbr) != 1) {
330 error("** Can't read from device %d **\n", dev_desc->devnum);
331 return -1;
332 }
333
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100334 /* Append signature */
335 p_mbr->signature = MSDOS_MBR_SIGNATURE;
336 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
337 p_mbr->partition_record[0].start_sect = 1;
Maxime Ripardb349abb2015-01-08 12:26:44 +0100338 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100339
340 /* Write MBR sector to the MMC device */
Simon Glass2a981dc2016-02-29 15:25:52 -0700341 if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100342 printf("** Can't write to device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700343 dev_desc->devnum);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100344 return -1;
345 }
346
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100347 return 0;
348}
349
Simon Glass4101f682016-02-29 15:25:34 -0700350int write_gpt_table(struct blk_desc *dev_desc,
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100351 gpt_header *gpt_h, gpt_entry *gpt_e)
352{
Egbert Eichae1768a2013-04-09 06:03:36 +0000353 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
354 * sizeof(gpt_entry)), dev_desc);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100355 u32 calc_crc32;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100356
357 debug("max lba: %x\n", (u32) dev_desc->lba);
358 /* Setup the Protective MBR */
359 if (set_protective_mbr(dev_desc) < 0)
360 goto err;
361
362 /* Generate CRC for the Primary GPT Header */
363 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
364 le32_to_cpu(gpt_h->num_partition_entries) *
365 le32_to_cpu(gpt_h->sizeof_partition_entry));
366 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
367
368 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
369 le32_to_cpu(gpt_h->header_size));
370 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
371
372 /* Write the First GPT to the block right after the Legacy MBR */
Simon Glass2a981dc2016-02-29 15:25:52 -0700373 if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100374 goto err;
375
Simon Glass2a981dc2016-02-29 15:25:52 -0700376 if (blk_dwrite(dev_desc, 2, pte_blk_cnt, gpt_e)
Egbert Eichae1768a2013-04-09 06:03:36 +0000377 != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100378 goto err;
379
Steve Raee1f6b0a2014-12-18 12:13:42 +0100380 prepare_backup_gpt_header(gpt_h);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100381
Simon Glass2a981dc2016-02-29 15:25:52 -0700382 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
383 + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100384 goto err;
385
Simon Glass2a981dc2016-02-29 15:25:52 -0700386 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
387 gpt_h) != 1)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100388 goto err;
389
390 debug("GPT successfully written to block device!\n");
391 return 0;
392
393 err:
Simon Glassbcce53d2016-02-29 15:25:51 -0700394 printf("** Can't write to device %d **\n", dev_desc->devnum);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100395 return -1;
396}
397
398int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
399 disk_partition_t *partitions, int parts)
400{
Steve Raee04350d2014-05-26 11:52:23 -0700401 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
402 lbaint_t start;
403 lbaint_t last_usable_lba = (lbaint_t)
404 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100405 int i, k;
Marek Vasut67cd4a62013-05-19 12:53:34 +0000406 size_t efiname_len, dosname_len;
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100407#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100408 char *str_uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200409 unsigned char *bin_uuid;
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100410#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100411#ifdef CONFIG_PARTITION_TYPE_GUID
412 char *str_type_guid;
413 unsigned char *bin_type_guid;
414#endif
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100415
416 for (i = 0; i < parts; i++) {
417 /* partition starting lba */
418 start = partitions[i].start;
419 if (start && (start < offset)) {
420 printf("Partition overlap\n");
421 return -1;
422 }
423 if (start) {
424 gpt_e[i].starting_lba = cpu_to_le64(start);
425 offset = start + partitions[i].size;
426 } else {
427 gpt_e[i].starting_lba = cpu_to_le64(offset);
428 offset += partitions[i].size;
429 }
Patrick Delaunaya5653862016-05-02 14:43:33 +0200430 if (offset > (last_usable_lba + 1)) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100431 printf("Partitions layout exceds disk size\n");
432 return -1;
433 }
434 /* partition ending lba */
435 if ((i == parts - 1) && (partitions[i].size == 0))
436 /* extend the last partition to maximuim */
437 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
438 else
439 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
440
Patrick Delaunay7561b252015-10-27 11:00:27 +0100441#ifdef CONFIG_PARTITION_TYPE_GUID
442 str_type_guid = partitions[i].type_guid;
443 bin_type_guid = gpt_e[i].partition_type_guid.b;
444 if (strlen(str_type_guid)) {
445 if (uuid_str_to_bin(str_type_guid, bin_type_guid,
446 UUID_STR_FORMAT_GUID)) {
447 printf("Partition no. %d: invalid type guid: %s\n",
448 i, str_type_guid);
449 return -1;
450 }
451 } else {
452 /* default partition type GUID */
453 memcpy(bin_type_guid,
454 &PARTITION_BASIC_DATA_GUID, 16);
455 }
456#else
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100457 /* partition type GUID */
458 memcpy(gpt_e[i].partition_type_guid.b,
459 &PARTITION_BASIC_DATA_GUID, 16);
Patrick Delaunay7561b252015-10-27 11:00:27 +0100460#endif
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100461
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100462#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100463 str_uuid = partitions[i].uuid;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200464 bin_uuid = gpt_e[i].unique_partition_guid.b;
465
Vincent Tinelli9da52f82017-02-27 16:11:15 +0200466 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_GUID)) {
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100467 printf("Partition no. %d: invalid guid: %s\n",
468 i, str_uuid);
469 return -1;
470 }
471#endif
472
473 /* partition attributes */
474 memset(&gpt_e[i].attributes, 0,
475 sizeof(gpt_entry_attributes));
476
Patrick Delaunaycfdaf4c2015-11-17 11:36:52 +0100477 if (partitions[i].bootable)
478 gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
479
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100480 /* partition name */
Marek Vasut67cd4a62013-05-19 12:53:34 +0000481 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100482 / sizeof(efi_char16_t);
Marek Vasut67cd4a62013-05-19 12:53:34 +0000483 dosname_len = sizeof(partitions[i].name);
484
485 memset(gpt_e[i].partition_name, 0,
486 sizeof(gpt_e[i].partition_name));
487
488 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100489 gpt_e[i].partition_name[k] =
490 (efi_char16_t)(partitions[i].name[k]);
491
Steve Raee04350d2014-05-26 11:52:23 -0700492 debug("%s: name: %s offset[%d]: 0x" LBAF
493 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100494 __func__, partitions[i].name, i,
495 offset, i, partitions[i].size);
496 }
497
498 return 0;
499}
500
Simon Glass4101f682016-02-29 15:25:34 -0700501int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100502 char *str_guid, int parts_count)
503{
504 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
505 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
506 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
507 gpt_h->my_lba = cpu_to_le64(1);
508 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
509 gpt_h->first_usable_lba = cpu_to_le64(34);
510 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
511 gpt_h->partition_entry_lba = cpu_to_le64(2);
512 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
513 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
514 gpt_h->header_crc32 = 0;
515 gpt_h->partition_entry_array_crc32 = 0;
516
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200517 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100518 return -1;
519
520 return 0;
521}
522
Simon Glass4101f682016-02-29 15:25:34 -0700523int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100524 disk_partition_t *partitions, int parts_count)
525{
526 int ret;
527
Egbert Eichae1768a2013-04-09 06:03:36 +0000528 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
529 dev_desc));
530 gpt_entry *gpt_e;
531
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100532 if (gpt_h == NULL) {
533 printf("%s: calloc failed!\n", __func__);
534 return -1;
535 }
536
Egbert Eichae1768a2013-04-09 06:03:36 +0000537 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
538 * sizeof(gpt_entry),
539 dev_desc));
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100540 if (gpt_e == NULL) {
541 printf("%s: calloc failed!\n", __func__);
542 free(gpt_h);
543 return -1;
544 }
545
546 /* Generate Primary GPT header (LBA1) */
547 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
548 if (ret)
549 goto err;
550
551 /* Generate partition entries */
552 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
553 if (ret)
554 goto err;
555
556 /* Write GPT partition table */
557 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
558
559err:
560 free(gpt_e);
561 free(gpt_h);
562 return ret;
563}
Steve Rae0ff7e582014-12-12 15:51:54 -0800564
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100565static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n)
566{
567 char *ess = (char *)es;
568 int i, j;
569
570 memset(s, '\0', n);
571
572 for (i = 0, j = 0; j < n; i += 2, j++) {
573 s[j] = ess[i];
574 if (!ess[i])
575 return;
576 }
577}
578
Simon Glass4101f682016-02-29 15:25:34 -0700579int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100580 gpt_entry **gpt_pte)
581{
582 /*
583 * This function validates AND
584 * fills in the GPT header and PTE
585 */
586 if (is_gpt_valid(dev_desc,
587 GPT_PRIMARY_PARTITION_TABLE_LBA,
588 gpt_head, gpt_pte) != 1) {
589 printf("%s: *** ERROR: Invalid GPT ***\n",
590 __func__);
591 return -1;
592 }
593 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
594 gpt_head, gpt_pte) != 1) {
595 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
596 __func__);
597 return -1;
598 }
599
600 return 0;
601}
602
Simon Glass4101f682016-02-29 15:25:34 -0700603int gpt_verify_partitions(struct blk_desc *dev_desc,
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100604 disk_partition_t *partitions, int parts,
605 gpt_header *gpt_head, gpt_entry **gpt_pte)
606{
607 char efi_str[PARTNAME_SZ + 1];
608 u64 gpt_part_size;
609 gpt_entry *gpt_e;
610 int ret, i;
611
612 ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
613 if (ret)
614 return ret;
615
616 gpt_e = *gpt_pte;
617
618 for (i = 0; i < parts; i++) {
619 if (i == gpt_head->num_partition_entries) {
620 error("More partitions than allowed!\n");
621 return -1;
622 }
623
624 /* Check if GPT and ENV partition names match */
625 gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
626 PARTNAME_SZ + 1);
627
628 debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
629 __func__, i, efi_str, partitions[i].name);
630
631 if (strncmp(efi_str, (char *)partitions[i].name,
632 sizeof(partitions->name))) {
633 error("Partition name: %s does not match %s!\n",
634 efi_str, (char *)partitions[i].name);
635 return -1;
636 }
637
638 /* Check if GPT and ENV sizes match */
639 gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
640 le64_to_cpu(gpt_e[i].starting_lba) + 1;
641 debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
Simon Glassf8d61652016-02-29 15:25:36 -0700642 (unsigned long long)gpt_part_size,
643 (unsigned long long)partitions[i].size);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100644
645 if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
Kever Yangc2fdd342016-07-29 11:12:18 +0800646 /* We do not check the extend partition size */
647 if ((i == parts - 1) && (partitions[i].size == 0))
648 continue;
649
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100650 error("Partition %s size: %llu does not match %llu!\n",
Simon Glassf8d61652016-02-29 15:25:36 -0700651 efi_str, (unsigned long long)gpt_part_size,
652 (unsigned long long)partitions[i].size);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100653 return -1;
654 }
655
656 /*
657 * Start address is optional - check only if provided
658 * in '$partition' variable
659 */
660 if (!partitions[i].start) {
661 debug("\n");
662 continue;
663 }
664
665 /* Check if GPT and ENV start LBAs match */
666 debug("start LBA - GPT: %8llu, ENV: %8llu\n",
667 le64_to_cpu(gpt_e[i].starting_lba),
Simon Glassf8d61652016-02-29 15:25:36 -0700668 (unsigned long long)partitions[i].start);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100669
670 if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
671 error("Partition %s start: %llu does not match %llu!\n",
672 efi_str, le64_to_cpu(gpt_e[i].starting_lba),
Simon Glassf8d61652016-02-29 15:25:36 -0700673 (unsigned long long)partitions[i].start);
Lukasz Majewskicef68bf2015-11-20 08:06:16 +0100674 return -1;
675 }
676 }
677
678 return 0;
679}
680
Simon Glass4101f682016-02-29 15:25:34 -0700681int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf)
Steve Rae0ff7e582014-12-12 15:51:54 -0800682{
683 gpt_header *gpt_h;
684 gpt_entry *gpt_e;
685
686 /* determine start of GPT Header in the buffer */
687 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
688 dev_desc->blksz);
689 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
690 dev_desc->lba))
691 return -1;
692
693 /* determine start of GPT Entries in the buffer */
694 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
695 dev_desc->blksz);
696 if (validate_gpt_entries(gpt_h, gpt_e))
697 return -1;
698
699 return 0;
700}
701
Simon Glass4101f682016-02-29 15:25:34 -0700702int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
Steve Rae0ff7e582014-12-12 15:51:54 -0800703{
704 gpt_header *gpt_h;
705 gpt_entry *gpt_e;
706 int gpt_e_blk_cnt;
707 lbaint_t lba;
708 int cnt;
709
710 if (is_valid_gpt_buf(dev_desc, buf))
711 return -1;
712
713 /* determine start of GPT Header in the buffer */
714 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
715 dev_desc->blksz);
716
717 /* determine start of GPT Entries in the buffer */
718 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
719 dev_desc->blksz);
720 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
721 le32_to_cpu(gpt_h->sizeof_partition_entry)),
722 dev_desc);
723
724 /* write MBR */
725 lba = 0; /* MBR is always at 0 */
726 cnt = 1; /* MBR (1 block) */
Simon Glass2a981dc2016-02-29 15:25:52 -0700727 if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800728 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
729 __func__, "MBR", cnt, lba);
730 return 1;
731 }
732
733 /* write Primary GPT */
734 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
735 cnt = 1; /* GPT Header (1 block) */
Simon Glass2a981dc2016-02-29 15:25:52 -0700736 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800737 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
738 __func__, "Primary GPT Header", cnt, lba);
739 return 1;
740 }
741
742 lba = le64_to_cpu(gpt_h->partition_entry_lba);
743 cnt = gpt_e_blk_cnt;
Simon Glass2a981dc2016-02-29 15:25:52 -0700744 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800745 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
746 __func__, "Primary GPT Entries", cnt, lba);
747 return 1;
748 }
749
750 prepare_backup_gpt_header(gpt_h);
751
752 /* write Backup GPT */
753 lba = le64_to_cpu(gpt_h->partition_entry_lba);
754 cnt = gpt_e_blk_cnt;
Simon Glass2a981dc2016-02-29 15:25:52 -0700755 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800756 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
757 __func__, "Backup GPT Entries", cnt, lba);
758 return 1;
759 }
760
761 lba = le64_to_cpu(gpt_h->my_lba);
762 cnt = 1; /* GPT Header (1 block) */
Simon Glass2a981dc2016-02-29 15:25:52 -0700763 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
Steve Rae0ff7e582014-12-12 15:51:54 -0800764 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
765 __func__, "Backup GPT Header", cnt, lba);
766 return 1;
767 }
768
769 return 0;
770}
Lukasz Majewski40684dd2012-12-11 11:09:46 +0100771#endif
772
richardretanubun07f3d782008-09-26 11:13:22 -0400773/*
774 * Private functions
775 */
776/*
777 * pmbr_part_valid(): Check for EFI partition signature
778 *
779 * Returns: 1 if EFI GPT partition type is found.
780 */
781static int pmbr_part_valid(struct partition *part)
782{
783 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich8faefad2013-03-29 07:57:10 +0000784 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubun07f3d782008-09-26 11:13:22 -0400785 return 1;
786 }
787
788 return 0;
789}
790
791/*
792 * is_pmbr_valid(): test Protective MBR for validity
793 *
794 * Returns: 1 if PMBR is valid, 0 otherwise.
795 * Validity depends on two things:
796 * 1) MSDOS signature is in the last two bytes of the MBR
797 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
798 */
799static int is_pmbr_valid(legacy_mbr * mbr)
800{
801 int i = 0;
802
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100803 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubun07f3d782008-09-26 11:13:22 -0400804 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400805
806 for (i = 0; i < 4; i++) {
807 if (pmbr_part_valid(&mbr->partition_record[i])) {
808 return 1;
809 }
810 }
811 return 0;
812}
813
814/**
815 * is_gpt_valid() - tests one GPT header and PTEs for validity
816 *
817 * lba is the logical block address of the GPT header to test
818 * gpt is a GPT header ptr, filled on return.
819 * ptes is a PTEs ptr, filled on return.
820 *
821 * Description: returns 1 if valid, 0 on error.
822 * If valid, returns pointers to PTEs.
823 */
Simon Glass4101f682016-02-29 15:25:34 -0700824static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
Steve Raee04350d2014-05-26 11:52:23 -0700825 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
richardretanubun07f3d782008-09-26 11:13:22 -0400826{
richardretanubun07f3d782008-09-26 11:13:22 -0400827 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000828 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400829 return 0;
830 }
831
832 /* Read GPT Header from device */
Simon Glass2a981dc2016-02-29 15:25:52 -0700833 if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
richardretanubun07f3d782008-09-26 11:13:22 -0400834 printf("*** ERROR: Can't read GPT header ***\n");
835 return 0;
836 }
837
Steve Raee1f6b0a2014-12-18 12:13:42 +0100838 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
richardretanubun07f3d782008-09-26 11:13:22 -0400839 return 0;
richardretanubun07f3d782008-09-26 11:13:22 -0400840
841 /* Read and allocate Partition Table Entries */
842 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
843 if (*pgpt_pte == NULL) {
844 printf("GPT: Failed to allocate memory for PTE\n");
845 return 0;
846 }
847
Steve Raee1f6b0a2014-12-18 12:13:42 +0100848 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
Doug Andersondeb5ca82011-10-19 09:47:31 +0000849 free(*pgpt_pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400850 return 0;
851 }
852
853 /* We're done, all's well */
854 return 1;
855}
856
857/**
858 * alloc_read_gpt_entries(): reads partition entries from disk
859 * @dev_desc
860 * @gpt - GPT header
861 *
862 * Description: Returns ptes on success, NULL on error.
863 * Allocates space for PTEs based on information found in @gpt.
864 * Notes: remember to free pte when you're done!
865 */
Simon Glass4101f682016-02-29 15:25:34 -0700866static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
867 gpt_header *pgpt_head)
richardretanubun07f3d782008-09-26 11:13:22 -0400868{
Egbert Eichae1768a2013-04-09 06:03:36 +0000869 size_t count = 0, blk_cnt;
Stephen Warren7c4213f2015-12-07 11:38:48 -0700870 lbaint_t blk;
richardretanubun07f3d782008-09-26 11:13:22 -0400871 gpt_entry *pte = NULL;
872
873 if (!dev_desc || !pgpt_head) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000874 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400875 return NULL;
876 }
877
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100878 count = le32_to_cpu(pgpt_head->num_partition_entries) *
879 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubun07f3d782008-09-26 11:13:22 -0400880
Simon Glass5afb8d12016-07-22 09:22:47 -0600881 debug("%s: count = %u * %u = %lu\n", __func__,
Chang Hyun Parkfae2bf22012-12-11 11:09:45 +0100882 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
Simon Glass5afb8d12016-07-22 09:22:47 -0600883 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry),
884 (ulong)count);
richardretanubun07f3d782008-09-26 11:13:22 -0400885
886 /* Allocate memory for PTE, remember to FREE */
887 if (count != 0) {
Egbert Eichae1768a2013-04-09 06:03:36 +0000888 pte = memalign(ARCH_DMA_MINALIGN,
889 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubun07f3d782008-09-26 11:13:22 -0400890 }
891
892 if (count == 0 || pte == NULL) {
Simon Glass5afb8d12016-07-22 09:22:47 -0600893 printf("%s: ERROR: Can't allocate %#lX bytes for GPT Entries\n",
894 __func__, (ulong)count);
richardretanubun07f3d782008-09-26 11:13:22 -0400895 return NULL;
896 }
897
898 /* Read GPT Entries from device */
Stephen Warren7c4213f2015-12-07 11:38:48 -0700899 blk = le64_to_cpu(pgpt_head->partition_entry_lba);
Egbert Eichae1768a2013-04-09 06:03:36 +0000900 blk_cnt = BLOCK_CNT(count, dev_desc);
Simon Glass2a981dc2016-02-29 15:25:52 -0700901 if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
richardretanubun07f3d782008-09-26 11:13:22 -0400902 printf("*** ERROR: Can't read GPT Entries ***\n");
903 free(pte);
904 return NULL;
905 }
906 return pte;
907}
908
909/**
910 * is_pte_valid(): validates a single Partition Table Entry
911 * @gpt_entry - Pointer to a single Partition Table Entry
912 *
913 * Description: returns 1 if valid, 0 on error.
914 */
915static int is_pte_valid(gpt_entry * pte)
916{
917 efi_guid_t unused_guid;
918
919 if (!pte) {
Doug Andersondf70b1c2011-10-19 08:04:46 +0000920 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubun07f3d782008-09-26 11:13:22 -0400921 return 0;
922 }
923
924 /* Only one validation for now:
925 * The GUID Partition Type != Unused Entry (ALL-ZERO)
926 */
927 memset(unused_guid.b, 0, sizeof(unused_guid.b));
928
929 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
930 sizeof(unused_guid.b)) == 0) {
931
Doug Andersondf70b1c2011-10-19 08:04:46 +0000932 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt9936be32012-10-12 14:26:09 +0000933 (unsigned int)(uintptr_t)pte);
richardretanubun07f3d782008-09-26 11:13:22 -0400934
935 return 0;
936 } else {
937 return 1;
938 }
939}
Simon Glass96e5b032016-02-29 15:25:47 -0700940
941/*
942 * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to
943 * check EFI first, since a DOS partition is often used as a 'protective MBR'
944 * with EFI.
945 */
946U_BOOT_PART_TYPE(a_efi) = {
947 .name = "EFI",
948 .part_type = PART_TYPE_EFI,
Petr Kulhavy87b85302016-09-09 10:27:15 +0200949 .max_entries = GPT_ENTRY_NUMBERS,
Simon Glass3e8bd462016-02-29 15:25:48 -0700950 .get_info = part_get_info_ptr(part_get_info_efi),
Simon Glass084bf4c2016-02-29 15:26:04 -0700951 .print = part_print_ptr(part_print_efi),
952 .test = part_test_efi,
Simon Glass96e5b032016-02-29 15:25:47 -0700953};
richardretanubun07f3d782008-09-26 11:13:22 -0400954#endif