blob: 77bdfcbce5eafa694f15e67f1bda009597d579c7 [file] [log] [blame]
richardretanubune6745592008-09-26 11:13:22 -04001/*
2 * Copyright (C) 2008 RuggedCom, Inc.
3 * Richard Retanubun <RichardRetanubun@RuggedCom.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
richardretanubune6745592008-09-26 11:13:22 -04006 */
7
8/*
Steve Raed30cbae2014-05-26 11:52:23 -07009 * NOTE:
10 * when CONFIG_SYS_64BIT_LBA is not defined, lbaint_t is 32 bits; this
11 * limits the maximum size of addressable storage to < 2 Terra Bytes
richardretanubune6745592008-09-26 11:13:22 -040012 */
Marc Dietrich20ca3ad2013-03-29 07:57:10 +000013#include <asm/unaligned.h>
richardretanubune6745592008-09-26 11:13:22 -040014#include <common.h>
15#include <command.h>
16#include <ide.h>
Simon Glasse9f343f2014-11-11 12:47:07 -070017#include <inttypes.h>
richardretanubune6745592008-09-26 11:13:22 -040018#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060019#include <memalign.h>
Chang Hyun Park823ffde2012-12-11 11:09:45 +010020#include <part_efi.h>
Lei Wen757ddfe2011-09-07 18:11:19 +000021#include <linux/ctype.h>
richardretanubune6745592008-09-26 11:13:22 -040022
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +010023DECLARE_GLOBAL_DATA_PTR;
24
Stephen Warren8ec730d2013-02-28 15:03:46 +000025#ifdef HAVE_BLOCK_DEVICE
richardretanubune6745592008-09-26 11:13:22 -040026/**
27 * efi_crc32() - EFI version of crc32 function
28 * @buf: buffer to calculate crc32 of
29 * @len - length of buf
30 *
31 * Description: Returns EFI-style CRC32 value for @buf
32 */
Chang Hyun Park823ffde2012-12-11 11:09:45 +010033static inline u32 efi_crc32(const void *buf, u32 len)
richardretanubune6745592008-09-26 11:13:22 -040034{
35 return crc32(0, buf, len);
36}
37
38/*
39 * Private function prototypes
40 */
41
42static int pmbr_part_valid(struct partition *part);
43static int is_pmbr_valid(legacy_mbr * mbr);
Simon Glasse3394752016-02-29 15:25:34 -070044static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
Steve Raed30cbae2014-05-26 11:52:23 -070045 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
Simon Glasse3394752016-02-29 15:25:34 -070046static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
47 gpt_header *pgpt_head);
richardretanubune6745592008-09-26 11:13:22 -040048static int is_pte_valid(gpt_entry * pte);
49
Lei Wen757ddfe2011-09-07 18:11:19 +000050static char *print_efiname(gpt_entry *pte)
51{
52 static char name[PARTNAME_SZ + 1];
53 int i;
54 for (i = 0; i < PARTNAME_SZ; i++) {
55 u8 c;
56 c = pte->partition_name[i] & 0xff;
57 c = (c && !isprint(c)) ? '.' : c;
58 name[i] = c;
59 }
60 name[PARTNAME_SZ] = 0;
61 return name;
62}
63
Stephen Warrend0cf3b62012-10-08 08:14:37 +000064static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
65
66static inline int is_bootable(gpt_entry *p)
67{
68 return p->attributes.fields.legacy_bios_bootable ||
69 !memcmp(&(p->partition_type_guid), &system_guid,
70 sizeof(efi_guid_t));
71}
72
Steve Rae6f5e17d2014-12-18 12:13:42 +010073static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba,
74 lbaint_t lastlba)
75{
76 uint32_t crc32_backup = 0;
77 uint32_t calc_crc32;
78
79 /* Check the GPT header signature */
80 if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE) {
81 printf("%s signature is wrong: 0x%llX != 0x%llX\n",
82 "GUID Partition Table Header",
83 le64_to_cpu(gpt_h->signature),
84 GPT_HEADER_SIGNATURE);
85 return -1;
86 }
87
88 /* Check the GUID Partition Table CRC */
89 memcpy(&crc32_backup, &gpt_h->header_crc32, sizeof(crc32_backup));
90 memset(&gpt_h->header_crc32, 0, sizeof(gpt_h->header_crc32));
91
92 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
93 le32_to_cpu(gpt_h->header_size));
94
95 memcpy(&gpt_h->header_crc32, &crc32_backup, sizeof(crc32_backup));
96
97 if (calc_crc32 != le32_to_cpu(crc32_backup)) {
98 printf("%s CRC is wrong: 0x%x != 0x%x\n",
99 "GUID Partition Table Header",
100 le32_to_cpu(crc32_backup), calc_crc32);
101 return -1;
102 }
103
104 /*
105 * Check that the my_lba entry points to the LBA that contains the GPT
106 */
107 if (le64_to_cpu(gpt_h->my_lba) != lba) {
108 printf("GPT: my_lba incorrect: %llX != " LBAF "\n",
109 le64_to_cpu(gpt_h->my_lba),
110 lba);
111 return -1;
112 }
113
114 /*
115 * Check that the first_usable_lba and that the last_usable_lba are
116 * within the disk.
117 */
118 if (le64_to_cpu(gpt_h->first_usable_lba) > lastlba) {
119 printf("GPT: first_usable_lba incorrect: %llX > " LBAF "\n",
120 le64_to_cpu(gpt_h->first_usable_lba), lastlba);
121 return -1;
122 }
123 if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) {
124 printf("GPT: last_usable_lba incorrect: %llX > " LBAF "\n",
125 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
126 return -1;
127 }
128
129 debug("GPT: first_usable_lba: %llX last_usable_lba: %llX last lba: "
130 LBAF "\n", le64_to_cpu(gpt_h->first_usable_lba),
131 le64_to_cpu(gpt_h->last_usable_lba), lastlba);
132
133 return 0;
134}
135
136static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
137{
138 uint32_t calc_crc32;
139
140 /* Check the GUID Partition Table Entry Array CRC */
141 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
142 le32_to_cpu(gpt_h->num_partition_entries) *
143 le32_to_cpu(gpt_h->sizeof_partition_entry));
144
145 if (calc_crc32 != le32_to_cpu(gpt_h->partition_entry_array_crc32)) {
146 printf("%s: 0x%x != 0x%x\n",
147 "GUID Partition Table Entry Array CRC is wrong",
148 le32_to_cpu(gpt_h->partition_entry_array_crc32),
149 calc_crc32);
150 return -1;
151 }
152
153 return 0;
154}
155
156static void prepare_backup_gpt_header(gpt_header *gpt_h)
157{
158 uint32_t calc_crc32;
159 uint64_t val;
160
161 /* recalculate the values for the Backup GPT Header */
162 val = le64_to_cpu(gpt_h->my_lba);
163 gpt_h->my_lba = gpt_h->alternate_lba;
164 gpt_h->alternate_lba = cpu_to_le64(val);
Steve Rae7d059342014-12-12 15:51:54 -0800165 gpt_h->partition_entry_lba =
166 cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
Steve Rae6f5e17d2014-12-18 12:13:42 +0100167 gpt_h->header_crc32 = 0;
168
169 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
170 le32_to_cpu(gpt_h->header_size));
171 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
172}
173
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100174#ifdef CONFIG_EFI_PARTITION
richardretanubune6745592008-09-26 11:13:22 -0400175/*
176 * Public Functions (include/part.h)
177 */
178
Simon Glass0f1c9522016-02-29 15:26:04 -0700179void part_print_efi(struct blk_desc *dev_desc)
richardretanubune6745592008-09-26 11:13:22 -0400180{
Egbert Eich071e4232013-04-09 06:03:36 +0000181 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000182 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400183 int i = 0;
Stephen Warren8b855b52012-10-08 08:14:34 +0000184 char uuid[37];
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200185 unsigned char *uuid_bin;
richardretanubune6745592008-09-26 11:13:22 -0400186
187 if (!dev_desc) {
Doug Andersondc036672011-10-19 08:04:46 +0000188 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400189 return;
190 }
191 /* This function validates AND fills in the GPT header and PTE */
192 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
Stephen Warren30f39de2011-10-28 09:21:46 +0000193 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000194 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raee1f4ac52014-05-05 13:00:08 -0700195 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
196 gpt_head, &gpt_pte) != 1) {
197 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
198 __func__);
199 return;
200 } else {
201 printf("%s: *** Using Backup GPT ***\n",
202 __func__);
203 }
richardretanubune6745592008-09-26 11:13:22 -0400204 }
205
Doug Anderson698a51d2011-10-19 09:47:31 +0000206 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400207
Stephen Warren26163262012-10-08 08:14:33 +0000208 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000209 printf("\tAttributes\n");
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200210 printf("\tType GUID\n");
211 printf("\tPartition GUID\n");
Stephen Warren8b855b52012-10-08 08:14:34 +0000212
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100213 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
Stephen Warrene6df59e2012-10-08 08:14:32 +0000214 /* Stop at the first non valid PTE */
215 if (!is_pte_valid(&gpt_pte[i]))
216 break;
richardretanubune6745592008-09-26 11:13:22 -0400217
Stephen Warren26163262012-10-08 08:14:33 +0000218 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100219 le64_to_cpu(gpt_pte[i].starting_lba),
220 le64_to_cpu(gpt_pte[i].ending_lba),
Stephen Warren26163262012-10-08 08:14:33 +0000221 print_efiname(&gpt_pte[i]));
Stephen Warren9fbc2b32012-10-08 08:14:36 +0000222 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200223 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200224 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
Stephen Warren8b855b52012-10-08 08:14:34 +0000225 printf("\ttype:\t%s\n", uuid);
Patrick Delaunay65f94ed2015-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 Marczakfa907342014-04-02 10:20:02 +0200230 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200231 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
232 printf("\tguid:\t%s\n", uuid);
richardretanubune6745592008-09-26 11:13:22 -0400233 }
234
235 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000236 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400237 return;
238}
239
Simon Glassb89a8442016-02-29 15:25:48 -0700240int part_get_info_efi(struct blk_desc *dev_desc, int part,
241 disk_partition_t *info)
richardretanubune6745592008-09-26 11:13:22 -0400242{
Egbert Eich071e4232013-04-09 06:03:36 +0000243 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
Doug Anderson698a51d2011-10-19 09:47:31 +0000244 gpt_entry *gpt_pte = NULL;
richardretanubune6745592008-09-26 11:13:22 -0400245
246 /* "part" argument must be at least 1 */
247 if (!dev_desc || !info || part < 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000248 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-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 Warren30f39de2011-10-28 09:21:46 +0000254 gpt_head, &gpt_pte) != 1) {
Doug Andersondc036672011-10-19 08:04:46 +0000255 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
Steve Raee1f4ac52014-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 }
richardretanubune6745592008-09-26 11:13:22 -0400265 }
266
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100267 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
Stephen Warrene47dbba2012-09-21 09:50:58 +0000268 !is_pte_valid(&gpt_pte[part - 1])) {
Mark Langsdorfe7775312013-09-10 15:14:56 -0500269 debug("%s: *** ERROR: Invalid partition number %d ***\n",
Stephen Warrene47dbba2012-09-21 09:50:58 +0000270 __func__, part);
Mark Langsdorfe7775312013-09-10 15:14:56 -0500271 free(gpt_pte);
Stephen Warrene47dbba2012-09-21 09:50:58 +0000272 return -1;
273 }
274
Steve Raed30cbae2014-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 Retanubun413dfc72009-01-26 08:45:14 -0500277 /* The ending LBA is inclusive, to calculate size, add 1 to it */
Steve Raed30cbae2014-05-26 11:52:23 -0700278 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
Richard Retanubun413dfc72009-01-26 08:45:14 -0500279 - info->start;
Egbert Eich071e4232013-04-09 06:03:36 +0000280 info->blksz = dev_desc->blksz;
richardretanubune6745592008-09-26 11:13:22 -0400281
Lei Wen757ddfe2011-09-07 18:11:19 +0000282 sprintf((char *)info->name, "%s",
Doug Anderson698a51d2011-10-19 09:47:31 +0000283 print_efiname(&gpt_pte[part - 1]));
Ben Whitten34fd6c92015-12-30 13:05:58 +0000284 strcpy((char *)info->type, "U-Boot");
Stephen Warrend0cf3b62012-10-08 08:14:37 +0000285 info->bootable = is_bootable(&gpt_pte[part - 1]);
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000286#ifdef CONFIG_PARTITION_UUIDS
Przemyslaw Marczak0c813362014-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 Warrenc6c7b7a2012-09-21 09:50:59 +0000289#endif
Patrick Delaunay8248c8d2015-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
richardretanubune6745592008-09-26 11:13:22 -0400294
Steve Raef7f10b82014-05-26 11:52:24 -0700295 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
Frederic Leroye7ee0282013-06-26 18:11:25 +0200296 info->start, info->size, info->name);
richardretanubune6745592008-09-26 11:13:22 -0400297
298 /* Remember to free pte */
Doug Anderson698a51d2011-10-19 09:47:31 +0000299 free(gpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400300 return 0;
301}
302
Simon Glassb89a8442016-02-29 15:25:48 -0700303int part_get_info_efi_by_name(struct blk_desc *dev_desc,
Steve Raef7f10b82014-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++) {
Simon Glassb89a8442016-02-29 15:25:48 -0700309 ret = part_get_info_efi(dev_desc, i, info);
Steve Raef7f10b82014-05-26 11:52:24 -0700310 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 Glass0f1c9522016-02-29 15:26:04 -0700322static int part_test_efi(struct blk_desc *dev_desc)
richardretanubune6745592008-09-26 11:13:22 -0400323{
Egbert Eich071e4232013-04-09 06:03:36 +0000324 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
richardretanubune6745592008-09-26 11:13:22 -0400325
326 /* Read legacy MBR from block 0 and validate it */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700327 if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1)
Anton staaf3b3df4e2011-10-12 13:56:04 +0000328 || (is_pmbr_valid(legacymbr) != 1)) {
richardretanubune6745592008-09-26 11:13:22 -0400329 return -1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100330 }
331 return 0;
332}
333
334/**
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 Glasse3394752016-02-29 15:25:34 -0700340static int set_protective_mbr(struct blk_desc *dev_desc)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100341{
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100342 /* Setup the Protective MBR */
Hector Palaciosb0a54ff2014-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 Majewski0dcfc5c2012-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 Ripard6d8a1522015-01-08 12:26:44 +0100354 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100355
356 /* Write MBR sector to the MMC device */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700357 if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) {
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100358 printf("** Can't write to device %d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700359 dev_desc->devnum);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100360 return -1;
361 }
362
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100363 return 0;
364}
365
Simon Glasse3394752016-02-29 15:25:34 -0700366int write_gpt_table(struct blk_desc *dev_desc,
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100367 gpt_header *gpt_h, gpt_entry *gpt_e)
368{
Egbert Eich071e4232013-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 Majewski0dcfc5c2012-12-11 11:09:46 +0100371 u32 calc_crc32;
Lukasz Majewski0dcfc5c2012-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 */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700389 if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100390 goto err;
391
Simon Glass2ee8ada2016-02-29 15:25:52 -0700392 if (blk_dwrite(dev_desc, 2, pte_blk_cnt, gpt_e)
Egbert Eich071e4232013-04-09 06:03:36 +0000393 != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100394 goto err;
395
Steve Rae6f5e17d2014-12-18 12:13:42 +0100396 prepare_backup_gpt_header(gpt_h);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100397
Simon Glass2ee8ada2016-02-29 15:25:52 -0700398 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
399 + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100400 goto err;
401
Simon Glass2ee8ada2016-02-29 15:25:52 -0700402 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
403 gpt_h) != 1)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100404 goto err;
405
406 debug("GPT successfully written to block device!\n");
407 return 0;
408
409 err:
Simon Glass2f26fff2016-02-29 15:25:51 -0700410 printf("** Can't write to device %d **\n", dev_desc->devnum);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100411 return -1;
412}
413
414int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
415 disk_partition_t *partitions, int parts)
416{
Steve Raed30cbae2014-05-26 11:52:23 -0700417 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
418 lbaint_t start;
419 lbaint_t last_usable_lba = (lbaint_t)
420 le64_to_cpu(gpt_h->last_usable_lba);
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100421 int i, k;
Marek Vasut941579e2013-05-19 12:53:34 +0000422 size_t efiname_len, dosname_len;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100423#ifdef CONFIG_PARTITION_UUIDS
424 char *str_uuid;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200425 unsigned char *bin_uuid;
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100426#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100427#ifdef CONFIG_PARTITION_TYPE_GUID
428 char *str_type_guid;
429 unsigned char *bin_type_guid;
430#endif
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100431
432 for (i = 0; i < parts; i++) {
433 /* partition starting lba */
434 start = partitions[i].start;
435 if (start && (start < offset)) {
436 printf("Partition overlap\n");
437 return -1;
438 }
439 if (start) {
440 gpt_e[i].starting_lba = cpu_to_le64(start);
441 offset = start + partitions[i].size;
442 } else {
443 gpt_e[i].starting_lba = cpu_to_le64(offset);
444 offset += partitions[i].size;
445 }
Steve Rae6d0d1722014-05-26 11:52:22 -0700446 if (offset >= last_usable_lba) {
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100447 printf("Partitions layout exceds disk size\n");
448 return -1;
449 }
450 /* partition ending lba */
451 if ((i == parts - 1) && (partitions[i].size == 0))
452 /* extend the last partition to maximuim */
453 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
454 else
455 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
456
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100457#ifdef CONFIG_PARTITION_TYPE_GUID
458 str_type_guid = partitions[i].type_guid;
459 bin_type_guid = gpt_e[i].partition_type_guid.b;
460 if (strlen(str_type_guid)) {
461 if (uuid_str_to_bin(str_type_guid, bin_type_guid,
462 UUID_STR_FORMAT_GUID)) {
463 printf("Partition no. %d: invalid type guid: %s\n",
464 i, str_type_guid);
465 return -1;
466 }
467 } else {
468 /* default partition type GUID */
469 memcpy(bin_type_guid,
470 &PARTITION_BASIC_DATA_GUID, 16);
471 }
472#else
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100473 /* partition type GUID */
474 memcpy(gpt_e[i].partition_type_guid.b,
475 &PARTITION_BASIC_DATA_GUID, 16);
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100476#endif
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100477
478#ifdef CONFIG_PARTITION_UUIDS
479 str_uuid = partitions[i].uuid;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200480 bin_uuid = gpt_e[i].unique_partition_guid.b;
481
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200482 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100483 printf("Partition no. %d: invalid guid: %s\n",
484 i, str_uuid);
485 return -1;
486 }
487#endif
488
489 /* partition attributes */
490 memset(&gpt_e[i].attributes, 0,
491 sizeof(gpt_entry_attributes));
492
Patrick Delaunay7840c4a2015-11-17 11:36:52 +0100493 if (partitions[i].bootable)
494 gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
495
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100496 /* partition name */
Marek Vasut941579e2013-05-19 12:53:34 +0000497 efiname_len = sizeof(gpt_e[i].partition_name)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100498 / sizeof(efi_char16_t);
Marek Vasut941579e2013-05-19 12:53:34 +0000499 dosname_len = sizeof(partitions[i].name);
500
501 memset(gpt_e[i].partition_name, 0,
502 sizeof(gpt_e[i].partition_name));
503
504 for (k = 0; k < min(dosname_len, efiname_len); k++)
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100505 gpt_e[i].partition_name[k] =
506 (efi_char16_t)(partitions[i].name[k]);
507
Steve Raed30cbae2014-05-26 11:52:23 -0700508 debug("%s: name: %s offset[%d]: 0x" LBAF
509 " size[%d]: 0x" LBAF "\n",
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100510 __func__, partitions[i].name, i,
511 offset, i, partitions[i].size);
512 }
513
514 return 0;
515}
516
Simon Glasse3394752016-02-29 15:25:34 -0700517int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100518 char *str_guid, int parts_count)
519{
520 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
521 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
522 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
523 gpt_h->my_lba = cpu_to_le64(1);
524 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
525 gpt_h->first_usable_lba = cpu_to_le64(34);
526 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
527 gpt_h->partition_entry_lba = cpu_to_le64(2);
528 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
529 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
530 gpt_h->header_crc32 = 0;
531 gpt_h->partition_entry_array_crc32 = 0;
532
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200533 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100534 return -1;
535
536 return 0;
537}
538
Simon Glasse3394752016-02-29 15:25:34 -0700539int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100540 disk_partition_t *partitions, int parts_count)
541{
542 int ret;
543
Egbert Eich071e4232013-04-09 06:03:36 +0000544 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
545 dev_desc));
546 gpt_entry *gpt_e;
547
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100548 if (gpt_h == NULL) {
549 printf("%s: calloc failed!\n", __func__);
550 return -1;
551 }
552
Egbert Eich071e4232013-04-09 06:03:36 +0000553 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
554 * sizeof(gpt_entry),
555 dev_desc));
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100556 if (gpt_e == NULL) {
557 printf("%s: calloc failed!\n", __func__);
558 free(gpt_h);
559 return -1;
560 }
561
562 /* Generate Primary GPT header (LBA1) */
563 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
564 if (ret)
565 goto err;
566
567 /* Generate partition entries */
568 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
569 if (ret)
570 goto err;
571
572 /* Write GPT partition table */
573 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
574
575err:
576 free(gpt_e);
577 free(gpt_h);
578 return ret;
579}
Steve Rae7d059342014-12-12 15:51:54 -0800580
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100581static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n)
582{
583 char *ess = (char *)es;
584 int i, j;
585
586 memset(s, '\0', n);
587
588 for (i = 0, j = 0; j < n; i += 2, j++) {
589 s[j] = ess[i];
590 if (!ess[i])
591 return;
592 }
593}
594
Simon Glasse3394752016-02-29 15:25:34 -0700595int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100596 gpt_entry **gpt_pte)
597{
598 /*
599 * This function validates AND
600 * fills in the GPT header and PTE
601 */
602 if (is_gpt_valid(dev_desc,
603 GPT_PRIMARY_PARTITION_TABLE_LBA,
604 gpt_head, gpt_pte) != 1) {
605 printf("%s: *** ERROR: Invalid GPT ***\n",
606 __func__);
607 return -1;
608 }
609 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
610 gpt_head, gpt_pte) != 1) {
611 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
612 __func__);
613 return -1;
614 }
615
616 return 0;
617}
618
Simon Glasse3394752016-02-29 15:25:34 -0700619int gpt_verify_partitions(struct blk_desc *dev_desc,
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100620 disk_partition_t *partitions, int parts,
621 gpt_header *gpt_head, gpt_entry **gpt_pte)
622{
623 char efi_str[PARTNAME_SZ + 1];
624 u64 gpt_part_size;
625 gpt_entry *gpt_e;
626 int ret, i;
627
628 ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
629 if (ret)
630 return ret;
631
632 gpt_e = *gpt_pte;
633
634 for (i = 0; i < parts; i++) {
635 if (i == gpt_head->num_partition_entries) {
636 error("More partitions than allowed!\n");
637 return -1;
638 }
639
640 /* Check if GPT and ENV partition names match */
641 gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
642 PARTNAME_SZ + 1);
643
644 debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
645 __func__, i, efi_str, partitions[i].name);
646
647 if (strncmp(efi_str, (char *)partitions[i].name,
648 sizeof(partitions->name))) {
649 error("Partition name: %s does not match %s!\n",
650 efi_str, (char *)partitions[i].name);
651 return -1;
652 }
653
654 /* Check if GPT and ENV sizes match */
655 gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
656 le64_to_cpu(gpt_e[i].starting_lba) + 1;
657 debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
Simon Glass03d7b602016-02-29 15:25:36 -0700658 (unsigned long long)gpt_part_size,
659 (unsigned long long)partitions[i].size);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100660
661 if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
662 error("Partition %s size: %llu does not match %llu!\n",
Simon Glass03d7b602016-02-29 15:25:36 -0700663 efi_str, (unsigned long long)gpt_part_size,
664 (unsigned long long)partitions[i].size);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100665 return -1;
666 }
667
668 /*
669 * Start address is optional - check only if provided
670 * in '$partition' variable
671 */
672 if (!partitions[i].start) {
673 debug("\n");
674 continue;
675 }
676
677 /* Check if GPT and ENV start LBAs match */
678 debug("start LBA - GPT: %8llu, ENV: %8llu\n",
679 le64_to_cpu(gpt_e[i].starting_lba),
Simon Glass03d7b602016-02-29 15:25:36 -0700680 (unsigned long long)partitions[i].start);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100681
682 if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
683 error("Partition %s start: %llu does not match %llu!\n",
684 efi_str, le64_to_cpu(gpt_e[i].starting_lba),
Simon Glass03d7b602016-02-29 15:25:36 -0700685 (unsigned long long)partitions[i].start);
Lukasz Majewski7b9839c2015-11-20 08:06:16 +0100686 return -1;
687 }
688 }
689
690 return 0;
691}
692
Simon Glasse3394752016-02-29 15:25:34 -0700693int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf)
Steve Rae7d059342014-12-12 15:51:54 -0800694{
695 gpt_header *gpt_h;
696 gpt_entry *gpt_e;
697
698 /* determine start of GPT Header in the buffer */
699 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
700 dev_desc->blksz);
701 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
702 dev_desc->lba))
703 return -1;
704
705 /* determine start of GPT Entries in the buffer */
706 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
707 dev_desc->blksz);
708 if (validate_gpt_entries(gpt_h, gpt_e))
709 return -1;
710
711 return 0;
712}
713
Simon Glasse3394752016-02-29 15:25:34 -0700714int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
Steve Rae7d059342014-12-12 15:51:54 -0800715{
716 gpt_header *gpt_h;
717 gpt_entry *gpt_e;
718 int gpt_e_blk_cnt;
719 lbaint_t lba;
720 int cnt;
721
722 if (is_valid_gpt_buf(dev_desc, buf))
723 return -1;
724
725 /* determine start of GPT Header in the buffer */
726 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
727 dev_desc->blksz);
728
729 /* determine start of GPT Entries in the buffer */
730 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
731 dev_desc->blksz);
732 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
733 le32_to_cpu(gpt_h->sizeof_partition_entry)),
734 dev_desc);
735
736 /* write MBR */
737 lba = 0; /* MBR is always at 0 */
738 cnt = 1; /* MBR (1 block) */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700739 if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800740 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
741 __func__, "MBR", cnt, lba);
742 return 1;
743 }
744
745 /* write Primary GPT */
746 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
747 cnt = 1; /* GPT Header (1 block) */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700748 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800749 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
750 __func__, "Primary GPT Header", cnt, lba);
751 return 1;
752 }
753
754 lba = le64_to_cpu(gpt_h->partition_entry_lba);
755 cnt = gpt_e_blk_cnt;
Simon Glass2ee8ada2016-02-29 15:25:52 -0700756 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800757 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
758 __func__, "Primary GPT Entries", cnt, lba);
759 return 1;
760 }
761
762 prepare_backup_gpt_header(gpt_h);
763
764 /* write Backup GPT */
765 lba = le64_to_cpu(gpt_h->partition_entry_lba);
766 cnt = gpt_e_blk_cnt;
Simon Glass2ee8ada2016-02-29 15:25:52 -0700767 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800768 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
769 __func__, "Backup GPT Entries", cnt, lba);
770 return 1;
771 }
772
773 lba = le64_to_cpu(gpt_h->my_lba);
774 cnt = 1; /* GPT Header (1 block) */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700775 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
Steve Rae7d059342014-12-12 15:51:54 -0800776 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
777 __func__, "Backup GPT Header", cnt, lba);
778 return 1;
779 }
780
781 return 0;
782}
Lukasz Majewski0dcfc5c2012-12-11 11:09:46 +0100783#endif
784
richardretanubune6745592008-09-26 11:13:22 -0400785/*
786 * Private functions
787 */
788/*
789 * pmbr_part_valid(): Check for EFI partition signature
790 *
791 * Returns: 1 if EFI GPT partition type is found.
792 */
793static int pmbr_part_valid(struct partition *part)
794{
795 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
Marc Dietrich20ca3ad2013-03-29 07:57:10 +0000796 get_unaligned_le32(&part->start_sect) == 1UL) {
richardretanubune6745592008-09-26 11:13:22 -0400797 return 1;
798 }
799
800 return 0;
801}
802
803/*
804 * is_pmbr_valid(): test Protective MBR for validity
805 *
806 * Returns: 1 if PMBR is valid, 0 otherwise.
807 * Validity depends on two things:
808 * 1) MSDOS signature is in the last two bytes of the MBR
809 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
810 */
811static int is_pmbr_valid(legacy_mbr * mbr)
812{
813 int i = 0;
814
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100815 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
richardretanubune6745592008-09-26 11:13:22 -0400816 return 0;
richardretanubune6745592008-09-26 11:13:22 -0400817
818 for (i = 0; i < 4; i++) {
819 if (pmbr_part_valid(&mbr->partition_record[i])) {
820 return 1;
821 }
822 }
823 return 0;
824}
825
826/**
827 * is_gpt_valid() - tests one GPT header and PTEs for validity
828 *
829 * lba is the logical block address of the GPT header to test
830 * gpt is a GPT header ptr, filled on return.
831 * ptes is a PTEs ptr, filled on return.
832 *
833 * Description: returns 1 if valid, 0 on error.
834 * If valid, returns pointers to PTEs.
835 */
Simon Glasse3394752016-02-29 15:25:34 -0700836static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
Steve Raed30cbae2014-05-26 11:52:23 -0700837 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
richardretanubune6745592008-09-26 11:13:22 -0400838{
richardretanubune6745592008-09-26 11:13:22 -0400839 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000840 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400841 return 0;
842 }
843
844 /* Read GPT Header from device */
Simon Glass2ee8ada2016-02-29 15:25:52 -0700845 if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
richardretanubune6745592008-09-26 11:13:22 -0400846 printf("*** ERROR: Can't read GPT header ***\n");
847 return 0;
848 }
849
Steve Rae6f5e17d2014-12-18 12:13:42 +0100850 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
richardretanubune6745592008-09-26 11:13:22 -0400851 return 0;
richardretanubune6745592008-09-26 11:13:22 -0400852
853 /* Read and allocate Partition Table Entries */
854 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
855 if (*pgpt_pte == NULL) {
856 printf("GPT: Failed to allocate memory for PTE\n");
857 return 0;
858 }
859
Steve Rae6f5e17d2014-12-18 12:13:42 +0100860 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
Doug Anderson698a51d2011-10-19 09:47:31 +0000861 free(*pgpt_pte);
richardretanubune6745592008-09-26 11:13:22 -0400862 return 0;
863 }
864
865 /* We're done, all's well */
866 return 1;
867}
868
869/**
870 * alloc_read_gpt_entries(): reads partition entries from disk
871 * @dev_desc
872 * @gpt - GPT header
873 *
874 * Description: Returns ptes on success, NULL on error.
875 * Allocates space for PTEs based on information found in @gpt.
876 * Notes: remember to free pte when you're done!
877 */
Simon Glasse3394752016-02-29 15:25:34 -0700878static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
879 gpt_header *pgpt_head)
richardretanubune6745592008-09-26 11:13:22 -0400880{
Egbert Eich071e4232013-04-09 06:03:36 +0000881 size_t count = 0, blk_cnt;
Stephen Warrene73f2962015-12-07 11:38:48 -0700882 lbaint_t blk;
richardretanubune6745592008-09-26 11:13:22 -0400883 gpt_entry *pte = NULL;
884
885 if (!dev_desc || !pgpt_head) {
Doug Andersondc036672011-10-19 08:04:46 +0000886 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400887 return NULL;
888 }
889
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100890 count = le32_to_cpu(pgpt_head->num_partition_entries) *
891 le32_to_cpu(pgpt_head->sizeof_partition_entry);
richardretanubune6745592008-09-26 11:13:22 -0400892
Chang Hyun Park823ffde2012-12-11 11:09:45 +0100893 debug("%s: count = %u * %u = %zu\n", __func__,
894 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
895 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
richardretanubune6745592008-09-26 11:13:22 -0400896
897 /* Allocate memory for PTE, remember to FREE */
898 if (count != 0) {
Egbert Eich071e4232013-04-09 06:03:36 +0000899 pte = memalign(ARCH_DMA_MINALIGN,
900 PAD_TO_BLOCKSIZE(count, dev_desc));
richardretanubune6745592008-09-26 11:13:22 -0400901 }
902
903 if (count == 0 || pte == NULL) {
Taylor Hutt864074d2012-10-12 14:26:09 +0000904 printf("%s: ERROR: Can't allocate 0x%zX "
905 "bytes for GPT Entries\n",
Doug Andersondc036672011-10-19 08:04:46 +0000906 __func__, count);
richardretanubune6745592008-09-26 11:13:22 -0400907 return NULL;
908 }
909
910 /* Read GPT Entries from device */
Stephen Warrene73f2962015-12-07 11:38:48 -0700911 blk = le64_to_cpu(pgpt_head->partition_entry_lba);
Egbert Eich071e4232013-04-09 06:03:36 +0000912 blk_cnt = BLOCK_CNT(count, dev_desc);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700913 if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
richardretanubune6745592008-09-26 11:13:22 -0400914 printf("*** ERROR: Can't read GPT Entries ***\n");
915 free(pte);
916 return NULL;
917 }
918 return pte;
919}
920
921/**
922 * is_pte_valid(): validates a single Partition Table Entry
923 * @gpt_entry - Pointer to a single Partition Table Entry
924 *
925 * Description: returns 1 if valid, 0 on error.
926 */
927static int is_pte_valid(gpt_entry * pte)
928{
929 efi_guid_t unused_guid;
930
931 if (!pte) {
Doug Andersondc036672011-10-19 08:04:46 +0000932 printf("%s: Invalid Argument(s)\n", __func__);
richardretanubune6745592008-09-26 11:13:22 -0400933 return 0;
934 }
935
936 /* Only one validation for now:
937 * The GUID Partition Type != Unused Entry (ALL-ZERO)
938 */
939 memset(unused_guid.b, 0, sizeof(unused_guid.b));
940
941 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
942 sizeof(unused_guid.b)) == 0) {
943
Doug Andersondc036672011-10-19 08:04:46 +0000944 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
Taylor Hutt864074d2012-10-12 14:26:09 +0000945 (unsigned int)(uintptr_t)pte);
richardretanubune6745592008-09-26 11:13:22 -0400946
947 return 0;
948 } else {
949 return 1;
950 }
951}
Simon Glass83ce5632016-02-29 15:25:47 -0700952
953/*
954 * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to
955 * check EFI first, since a DOS partition is often used as a 'protective MBR'
956 * with EFI.
957 */
958U_BOOT_PART_TYPE(a_efi) = {
959 .name = "EFI",
960 .part_type = PART_TYPE_EFI,
Simon Glassb89a8442016-02-29 15:25:48 -0700961 .get_info = part_get_info_ptr(part_get_info_efi),
Simon Glass0f1c9522016-02-29 15:26:04 -0700962 .print = part_print_ptr(part_print_efi),
963 .test = part_test_efi,
Simon Glass83ce5632016-02-29 15:25:47 -0700964};
richardretanubune6745592008-09-26 11:13:22 -0400965#endif