]> git.ipfire.org Git - people/ms/u-boot.git/blame - disk/part_efi.c
cmd: disk: Fix unused variable warning
[people/ms/u-boot.git] / disk / part_efi.c
CommitLineData
07f3d789 1/*
2 * Copyright (C) 2008 RuggedCom, Inc.
3 * Richard Retanubun <RichardRetanubun@RuggedCom.com>
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
07f3d789 6 */
7
8/*
e04350d2
SR
9 * 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
07f3d789 12 */
8faefadb 13#include <asm/unaligned.h>
07f3d789 14#include <common.h>
15#include <command.h>
16#include <ide.h>
e48f3741 17#include <inttypes.h>
07f3d789 18#include <malloc.h>
cf92e05c 19#include <memalign.h>
fae2bf22 20#include <part_efi.h>
6eecc030 21#include <linux/ctype.h>
07f3d789 22
40684ddb
ŁM
23DECLARE_GLOBAL_DATA_PTR;
24
2c1af9dc 25#ifdef HAVE_BLOCK_DEVICE
07f3d789 26/**
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 */
fae2bf22 33static inline u32 efi_crc32(const void *buf, u32 len)
07f3d789 34{
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);
4101f687 44static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
e04350d2 45 gpt_header *pgpt_head, gpt_entry **pgpt_pte);
4101f687
SG
46static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
47 gpt_header *pgpt_head);
07f3d789 48static int is_pte_valid(gpt_entry * pte);
49
6eecc030
LW
50static 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
b4414f4a
SW
64static 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
e1f6b0a0
SR
73static 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);
0ff7e585
SR
165 gpt_h->partition_entry_lba =
166 cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
e1f6b0a0
SR
167 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
40684ddb 174#ifdef CONFIG_EFI_PARTITION
07f3d789 175/*
176 * Public Functions (include/part.h)
177 */
178
084bf4c2 179void part_print_efi(struct blk_desc *dev_desc)
07f3d789 180{
ae1768a7 181 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
deb5ca80 182 gpt_entry *gpt_pte = NULL;
07f3d789 183 int i = 0;
f07cd2c4 184 char uuid[37];
a96a0e61 185 unsigned char *uuid_bin;
07f3d789 186
07f3d789 187 /* This function validates AND fills in the GPT header and PTE */
188 if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
4715a811 189 gpt_head, &gpt_pte) != 1) {
df70b1c2 190 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
ae95fad5
SR
191 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 }
07f3d789 200 }
201
deb5ca80 202 debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
07f3d789 203
788a8c1f 204 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
13bf2f55 205 printf("\tAttributes\n");
d718ded0
PM
206 printf("\tType GUID\n");
207 printf("\tPartition GUID\n");
f07cd2c4 208
fae2bf22 209 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
38a3021e
SW
210 /* Stop at the first non valid PTE */
211 if (!is_pte_valid(&gpt_pte[i]))
212 break;
213
788a8c1f 214 printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
fae2bf22
CHP
215 le64_to_cpu(gpt_pte[i].starting_lba),
216 le64_to_cpu(gpt_pte[i].ending_lba),
788a8c1f 217 print_efiname(&gpt_pte[i]));
13bf2f55 218 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
a96a0e61 219 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
d718ded0 220 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
f07cd2c4 221 printf("\ttype:\t%s\n", uuid);
bcb41dca
PD
222#ifdef CONFIG_PARTITION_TYPE_GUID
223 if (!uuid_guid_get_str(uuid_bin, uuid))
224 printf("\ttype:\t%s\n", uuid);
225#endif
a96a0e61 226 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
d718ded0
PM
227 uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
228 printf("\tguid:\t%s\n", uuid);
07f3d789 229 }
230
231 /* Remember to free pte */
deb5ca80 232 free(gpt_pte);
07f3d789 233 return;
234}
235
3e8bd469
SG
236int part_get_info_efi(struct blk_desc *dev_desc, int part,
237 disk_partition_t *info)
07f3d789 238{
ae1768a7 239 ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
deb5ca80 240 gpt_entry *gpt_pte = NULL;
07f3d789 241
242 /* "part" argument must be at least 1 */
4708a07c 243 if (part < 1) {
df70b1c2 244 printf("%s: Invalid Argument(s)\n", __func__);
07f3d789 245 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,
4715a811 250 gpt_head, &gpt_pte) != 1) {
df70b1c2 251 printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
ae95fad5
SR
252 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 }
07f3d789 261 }
262
fae2bf22 263 if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
c04d68c6 264 !is_pte_valid(&gpt_pte[part - 1])) {
6d2ee5a3 265 debug("%s: *** ERROR: Invalid partition number %d ***\n",
c04d68c6 266 __func__, part);
6d2ee5a3 267 free(gpt_pte);
c04d68c6
SW
268 return -1;
269 }
270
e04350d2
SR
271 /* 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);
50970839 273 /* The ending LBA is inclusive, to calculate size, add 1 to it */
e04350d2 274 info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
50970839 275 - info->start;
ae1768a7 276 info->blksz = dev_desc->blksz;
07f3d789 277
6eecc030 278 sprintf((char *)info->name, "%s",
deb5ca80 279 print_efiname(&gpt_pte[part - 1]));
192bc694 280 strcpy((char *)info->type, "U-Boot");
b4414f4a 281 info->bootable = is_bootable(&gpt_pte[part - 1]);
894bfbbf 282#ifdef CONFIG_PARTITION_UUIDS
d718ded0
PM
283 uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
284 UUID_STR_FORMAT_GUID);
894bfbbf 285#endif
7561b258
PD
286#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
07f3d789 290
60bf9416 291 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
04735e9c 292 info->start, info->size, info->name);
07f3d789 293
294 /* Remember to free pte */
deb5ca80 295 free(gpt_pte);
07f3d789 296 return 0;
297}
298
3e8bd469 299int part_get_info_efi_by_name(struct blk_desc *dev_desc,
60bf9416
SR
300 const char *name, disk_partition_t *info)
301{
302 int ret;
303 int i;
304 for (i = 1; i < GPT_ENTRY_NUMBERS; i++) {
3e8bd469 305 ret = part_get_info_efi(dev_desc, i, info);
60bf9416
SR
306 if (ret != 0) {
307 /* no more entries in table */
308 return -1;
309 }
310 if (strcmp(name, (const char *)info->name) == 0) {
311 /* matched */
312 return 0;
313 }
314 }
315 return -2;
316}
317
084bf4c2 318static int part_test_efi(struct blk_desc *dev_desc)
07f3d789 319{
ae1768a7 320 ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
07f3d789 321
322 /* Read legacy MBR from block 0 and validate it */
2a981dc2 323 if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1)
f75dd584 324 || (is_pmbr_valid(legacymbr) != 1)) {
07f3d789 325 return -1;
326 }
327 return 0;
328}
329
40684ddb
ŁM
330/**
331 * set_protective_mbr(): Set the EFI protective MBR
332 * @param dev_desc - block device descriptor
333 *
334 * @return - zero on success, otherwise error
335 */
4101f687 336static int set_protective_mbr(struct blk_desc *dev_desc)
40684ddb 337{
40684ddb 338 /* Setup the Protective MBR */
61fcc7d2
HP
339 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
340 memset(p_mbr, 0, sizeof(*p_mbr));
341
40684ddb
ŁM
342 if (p_mbr == NULL) {
343 printf("%s: calloc failed!\n", __func__);
344 return -1;
345 }
346 /* Append signature */
347 p_mbr->signature = MSDOS_MBR_SIGNATURE;
348 p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
349 p_mbr->partition_record[0].start_sect = 1;
b349abbf 350 p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
40684ddb
ŁM
351
352 /* Write MBR sector to the MMC device */
2a981dc2 353 if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) {
40684ddb 354 printf("** Can't write to device %d **\n",
bcce53d0 355 dev_desc->devnum);
40684ddb
ŁM
356 return -1;
357 }
358
40684ddb
ŁM
359 return 0;
360}
361
4101f687 362int write_gpt_table(struct blk_desc *dev_desc,
40684ddb
ŁM
363 gpt_header *gpt_h, gpt_entry *gpt_e)
364{
ae1768a7
EE
365 const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
366 * sizeof(gpt_entry)), dev_desc);
40684ddb 367 u32 calc_crc32;
40684ddb
ŁM
368
369 debug("max lba: %x\n", (u32) dev_desc->lba);
370 /* Setup the Protective MBR */
371 if (set_protective_mbr(dev_desc) < 0)
372 goto err;
373
374 /* Generate CRC for the Primary GPT Header */
375 calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
376 le32_to_cpu(gpt_h->num_partition_entries) *
377 le32_to_cpu(gpt_h->sizeof_partition_entry));
378 gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
379
380 calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
381 le32_to_cpu(gpt_h->header_size));
382 gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
383
384 /* Write the First GPT to the block right after the Legacy MBR */
2a981dc2 385 if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1)
40684ddb
ŁM
386 goto err;
387
2a981dc2 388 if (blk_dwrite(dev_desc, 2, pte_blk_cnt, gpt_e)
ae1768a7 389 != pte_blk_cnt)
40684ddb
ŁM
390 goto err;
391
e1f6b0a0 392 prepare_backup_gpt_header(gpt_h);
40684ddb 393
2a981dc2
SG
394 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
395 + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
40684ddb
ŁM
396 goto err;
397
2a981dc2
SG
398 if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
399 gpt_h) != 1)
40684ddb
ŁM
400 goto err;
401
402 debug("GPT successfully written to block device!\n");
403 return 0;
404
405 err:
bcce53d0 406 printf("** Can't write to device %d **\n", dev_desc->devnum);
40684ddb
ŁM
407 return -1;
408}
409
410int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
411 disk_partition_t *partitions, int parts)
412{
e04350d2
SR
413 lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
414 lbaint_t start;
415 lbaint_t last_usable_lba = (lbaint_t)
416 le64_to_cpu(gpt_h->last_usable_lba);
40684ddb 417 int i, k;
67cd4a63 418 size_t efiname_len, dosname_len;
40684ddb
ŁM
419#ifdef CONFIG_PARTITION_UUIDS
420 char *str_uuid;
a96a0e61 421 unsigned char *bin_uuid;
40684ddb 422#endif
7561b258
PD
423#ifdef CONFIG_PARTITION_TYPE_GUID
424 char *str_type_guid;
425 unsigned char *bin_type_guid;
426#endif
40684ddb
ŁM
427
428 for (i = 0; i < parts; i++) {
429 /* partition starting lba */
430 start = partitions[i].start;
431 if (start && (start < offset)) {
432 printf("Partition overlap\n");
433 return -1;
434 }
435 if (start) {
436 gpt_e[i].starting_lba = cpu_to_le64(start);
437 offset = start + partitions[i].size;
438 } else {
439 gpt_e[i].starting_lba = cpu_to_le64(offset);
440 offset += partitions[i].size;
441 }
dedf37bb 442 if (offset >= last_usable_lba) {
40684ddb
ŁM
443 printf("Partitions layout exceds disk size\n");
444 return -1;
445 }
446 /* partition ending lba */
447 if ((i == parts - 1) && (partitions[i].size == 0))
448 /* extend the last partition to maximuim */
449 gpt_e[i].ending_lba = gpt_h->last_usable_lba;
450 else
451 gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
452
7561b258
PD
453#ifdef CONFIG_PARTITION_TYPE_GUID
454 str_type_guid = partitions[i].type_guid;
455 bin_type_guid = gpt_e[i].partition_type_guid.b;
456 if (strlen(str_type_guid)) {
457 if (uuid_str_to_bin(str_type_guid, bin_type_guid,
458 UUID_STR_FORMAT_GUID)) {
459 printf("Partition no. %d: invalid type guid: %s\n",
460 i, str_type_guid);
461 return -1;
462 }
463 } else {
464 /* default partition type GUID */
465 memcpy(bin_type_guid,
466 &PARTITION_BASIC_DATA_GUID, 16);
467 }
468#else
40684ddb
ŁM
469 /* partition type GUID */
470 memcpy(gpt_e[i].partition_type_guid.b,
471 &PARTITION_BASIC_DATA_GUID, 16);
7561b258 472#endif
40684ddb
ŁM
473
474#ifdef CONFIG_PARTITION_UUIDS
475 str_uuid = partitions[i].uuid;
a96a0e61
PM
476 bin_uuid = gpt_e[i].unique_partition_guid.b;
477
d718ded0 478 if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
40684ddb
ŁM
479 printf("Partition no. %d: invalid guid: %s\n",
480 i, str_uuid);
481 return -1;
482 }
483#endif
484
485 /* partition attributes */
486 memset(&gpt_e[i].attributes, 0,
487 sizeof(gpt_entry_attributes));
488
cfdaf4ca
PD
489 if (partitions[i].bootable)
490 gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
491
40684ddb 492 /* partition name */
67cd4a63 493 efiname_len = sizeof(gpt_e[i].partition_name)
40684ddb 494 / sizeof(efi_char16_t);
67cd4a63
MV
495 dosname_len = sizeof(partitions[i].name);
496
497 memset(gpt_e[i].partition_name, 0,
498 sizeof(gpt_e[i].partition_name));
499
500 for (k = 0; k < min(dosname_len, efiname_len); k++)
40684ddb
ŁM
501 gpt_e[i].partition_name[k] =
502 (efi_char16_t)(partitions[i].name[k]);
503
e04350d2
SR
504 debug("%s: name: %s offset[%d]: 0x" LBAF
505 " size[%d]: 0x" LBAF "\n",
40684ddb
ŁM
506 __func__, partitions[i].name, i,
507 offset, i, partitions[i].size);
508 }
509
510 return 0;
511}
512
4101f687 513int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
40684ddb
ŁM
514 char *str_guid, int parts_count)
515{
516 gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
517 gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
518 gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
519 gpt_h->my_lba = cpu_to_le64(1);
520 gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
521 gpt_h->first_usable_lba = cpu_to_le64(34);
522 gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
523 gpt_h->partition_entry_lba = cpu_to_le64(2);
524 gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
525 gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
526 gpt_h->header_crc32 = 0;
527 gpt_h->partition_entry_array_crc32 = 0;
528
d718ded0 529 if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
40684ddb
ŁM
530 return -1;
531
532 return 0;
533}
534
4101f687 535int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
40684ddb
ŁM
536 disk_partition_t *partitions, int parts_count)
537{
538 int ret;
539
ae1768a7
EE
540 gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
541 dev_desc));
542 gpt_entry *gpt_e;
543
40684ddb
ŁM
544 if (gpt_h == NULL) {
545 printf("%s: calloc failed!\n", __func__);
546 return -1;
547 }
548
ae1768a7
EE
549 gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
550 * sizeof(gpt_entry),
551 dev_desc));
40684ddb
ŁM
552 if (gpt_e == NULL) {
553 printf("%s: calloc failed!\n", __func__);
554 free(gpt_h);
555 return -1;
556 }
557
558 /* Generate Primary GPT header (LBA1) */
559 ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
560 if (ret)
561 goto err;
562
563 /* Generate partition entries */
564 ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
565 if (ret)
566 goto err;
567
568 /* Write GPT partition table */
569 ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
570
571err:
572 free(gpt_e);
573 free(gpt_h);
574 return ret;
575}
0ff7e585 576
cef68bf9
LM
577static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n)
578{
579 char *ess = (char *)es;
580 int i, j;
581
582 memset(s, '\0', n);
583
584 for (i = 0, j = 0; j < n; i += 2, j++) {
585 s[j] = ess[i];
586 if (!ess[i])
587 return;
588 }
589}
590
4101f687 591int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
cef68bf9
LM
592 gpt_entry **gpt_pte)
593{
594 /*
595 * This function validates AND
596 * fills in the GPT header and PTE
597 */
598 if (is_gpt_valid(dev_desc,
599 GPT_PRIMARY_PARTITION_TABLE_LBA,
600 gpt_head, gpt_pte) != 1) {
601 printf("%s: *** ERROR: Invalid GPT ***\n",
602 __func__);
603 return -1;
604 }
605 if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
606 gpt_head, gpt_pte) != 1) {
607 printf("%s: *** ERROR: Invalid Backup GPT ***\n",
608 __func__);
609 return -1;
610 }
611
612 return 0;
613}
614
4101f687 615int gpt_verify_partitions(struct blk_desc *dev_desc,
cef68bf9
LM
616 disk_partition_t *partitions, int parts,
617 gpt_header *gpt_head, gpt_entry **gpt_pte)
618{
619 char efi_str[PARTNAME_SZ + 1];
620 u64 gpt_part_size;
621 gpt_entry *gpt_e;
622 int ret, i;
623
624 ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
625 if (ret)
626 return ret;
627
628 gpt_e = *gpt_pte;
629
630 for (i = 0; i < parts; i++) {
631 if (i == gpt_head->num_partition_entries) {
632 error("More partitions than allowed!\n");
633 return -1;
634 }
635
636 /* Check if GPT and ENV partition names match */
637 gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
638 PARTNAME_SZ + 1);
639
640 debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
641 __func__, i, efi_str, partitions[i].name);
642
643 if (strncmp(efi_str, (char *)partitions[i].name,
644 sizeof(partitions->name))) {
645 error("Partition name: %s does not match %s!\n",
646 efi_str, (char *)partitions[i].name);
647 return -1;
648 }
649
650 /* Check if GPT and ENV sizes match */
651 gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
652 le64_to_cpu(gpt_e[i].starting_lba) + 1;
653 debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
f8d6165d
SG
654 (unsigned long long)gpt_part_size,
655 (unsigned long long)partitions[i].size);
cef68bf9
LM
656
657 if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
658 error("Partition %s size: %llu does not match %llu!\n",
f8d6165d
SG
659 efi_str, (unsigned long long)gpt_part_size,
660 (unsigned long long)partitions[i].size);
cef68bf9
LM
661 return -1;
662 }
663
664 /*
665 * Start address is optional - check only if provided
666 * in '$partition' variable
667 */
668 if (!partitions[i].start) {
669 debug("\n");
670 continue;
671 }
672
673 /* Check if GPT and ENV start LBAs match */
674 debug("start LBA - GPT: %8llu, ENV: %8llu\n",
675 le64_to_cpu(gpt_e[i].starting_lba),
f8d6165d 676 (unsigned long long)partitions[i].start);
cef68bf9
LM
677
678 if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
679 error("Partition %s start: %llu does not match %llu!\n",
680 efi_str, le64_to_cpu(gpt_e[i].starting_lba),
f8d6165d 681 (unsigned long long)partitions[i].start);
cef68bf9
LM
682 return -1;
683 }
684 }
685
686 return 0;
687}
688
4101f687 689int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf)
0ff7e585
SR
690{
691 gpt_header *gpt_h;
692 gpt_entry *gpt_e;
693
694 /* determine start of GPT Header in the buffer */
695 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
696 dev_desc->blksz);
697 if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
698 dev_desc->lba))
699 return -1;
700
701 /* determine start of GPT Entries in the buffer */
702 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
703 dev_desc->blksz);
704 if (validate_gpt_entries(gpt_h, gpt_e))
705 return -1;
706
707 return 0;
708}
709
4101f687 710int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
0ff7e585
SR
711{
712 gpt_header *gpt_h;
713 gpt_entry *gpt_e;
714 int gpt_e_blk_cnt;
715 lbaint_t lba;
716 int cnt;
717
718 if (is_valid_gpt_buf(dev_desc, buf))
719 return -1;
720
721 /* determine start of GPT Header in the buffer */
722 gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
723 dev_desc->blksz);
724
725 /* determine start of GPT Entries in the buffer */
726 gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
727 dev_desc->blksz);
728 gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
729 le32_to_cpu(gpt_h->sizeof_partition_entry)),
730 dev_desc);
731
732 /* write MBR */
733 lba = 0; /* MBR is always at 0 */
734 cnt = 1; /* MBR (1 block) */
2a981dc2 735 if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) {
0ff7e585
SR
736 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
737 __func__, "MBR", cnt, lba);
738 return 1;
739 }
740
741 /* write Primary GPT */
742 lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
743 cnt = 1; /* GPT Header (1 block) */
2a981dc2 744 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
0ff7e585
SR
745 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
746 __func__, "Primary GPT Header", cnt, lba);
747 return 1;
748 }
749
750 lba = le64_to_cpu(gpt_h->partition_entry_lba);
751 cnt = gpt_e_blk_cnt;
2a981dc2 752 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
0ff7e585
SR
753 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
754 __func__, "Primary GPT Entries", cnt, lba);
755 return 1;
756 }
757
758 prepare_backup_gpt_header(gpt_h);
759
760 /* write Backup GPT */
761 lba = le64_to_cpu(gpt_h->partition_entry_lba);
762 cnt = gpt_e_blk_cnt;
2a981dc2 763 if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
0ff7e585
SR
764 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
765 __func__, "Backup GPT Entries", cnt, lba);
766 return 1;
767 }
768
769 lba = le64_to_cpu(gpt_h->my_lba);
770 cnt = 1; /* GPT Header (1 block) */
2a981dc2 771 if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
0ff7e585
SR
772 printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
773 __func__, "Backup GPT Header", cnt, lba);
774 return 1;
775 }
776
777 return 0;
778}
40684ddb
ŁM
779#endif
780
07f3d789 781/*
782 * Private functions
783 */
784/*
785 * pmbr_part_valid(): Check for EFI partition signature
786 *
787 * Returns: 1 if EFI GPT partition type is found.
788 */
789static int pmbr_part_valid(struct partition *part)
790{
791 if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
8faefadb 792 get_unaligned_le32(&part->start_sect) == 1UL) {
07f3d789 793 return 1;
794 }
795
796 return 0;
797}
798
799/*
800 * is_pmbr_valid(): test Protective MBR for validity
801 *
802 * Returns: 1 if PMBR is valid, 0 otherwise.
803 * Validity depends on two things:
804 * 1) MSDOS signature is in the last two bytes of the MBR
805 * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
806 */
807static int is_pmbr_valid(legacy_mbr * mbr)
808{
809 int i = 0;
810
fae2bf22 811 if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
07f3d789 812 return 0;
07f3d789 813
814 for (i = 0; i < 4; i++) {
815 if (pmbr_part_valid(&mbr->partition_record[i])) {
816 return 1;
817 }
818 }
819 return 0;
820}
821
822/**
823 * is_gpt_valid() - tests one GPT header and PTEs for validity
824 *
825 * lba is the logical block address of the GPT header to test
826 * gpt is a GPT header ptr, filled on return.
827 * ptes is a PTEs ptr, filled on return.
828 *
829 * Description: returns 1 if valid, 0 on error.
830 * If valid, returns pointers to PTEs.
831 */
4101f687 832static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
e04350d2 833 gpt_header *pgpt_head, gpt_entry **pgpt_pte)
07f3d789 834{
07f3d789 835 if (!dev_desc || !pgpt_head) {
df70b1c2 836 printf("%s: Invalid Argument(s)\n", __func__);
07f3d789 837 return 0;
838 }
839
840 /* Read GPT Header from device */
2a981dc2 841 if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
07f3d789 842 printf("*** ERROR: Can't read GPT header ***\n");
843 return 0;
844 }
845
e1f6b0a0 846 if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
07f3d789 847 return 0;
07f3d789 848
849 /* Read and allocate Partition Table Entries */
850 *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
851 if (*pgpt_pte == NULL) {
852 printf("GPT: Failed to allocate memory for PTE\n");
853 return 0;
854 }
855
e1f6b0a0 856 if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
deb5ca80 857 free(*pgpt_pte);
07f3d789 858 return 0;
859 }
860
861 /* We're done, all's well */
862 return 1;
863}
864
865/**
866 * alloc_read_gpt_entries(): reads partition entries from disk
867 * @dev_desc
868 * @gpt - GPT header
869 *
870 * Description: Returns ptes on success, NULL on error.
871 * Allocates space for PTEs based on information found in @gpt.
872 * Notes: remember to free pte when you're done!
873 */
4101f687
SG
874static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
875 gpt_header *pgpt_head)
07f3d789 876{
ae1768a7 877 size_t count = 0, blk_cnt;
7c4213f6 878 lbaint_t blk;
07f3d789 879 gpt_entry *pte = NULL;
880
881 if (!dev_desc || !pgpt_head) {
df70b1c2 882 printf("%s: Invalid Argument(s)\n", __func__);
07f3d789 883 return NULL;
884 }
885
fae2bf22
CHP
886 count = le32_to_cpu(pgpt_head->num_partition_entries) *
887 le32_to_cpu(pgpt_head->sizeof_partition_entry);
07f3d789 888
fae2bf22
CHP
889 debug("%s: count = %u * %u = %zu\n", __func__,
890 (u32) le32_to_cpu(pgpt_head->num_partition_entries),
891 (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
07f3d789 892
893 /* Allocate memory for PTE, remember to FREE */
894 if (count != 0) {
ae1768a7
EE
895 pte = memalign(ARCH_DMA_MINALIGN,
896 PAD_TO_BLOCKSIZE(count, dev_desc));
07f3d789 897 }
898
899 if (count == 0 || pte == NULL) {
9936be31
TH
900 printf("%s: ERROR: Can't allocate 0x%zX "
901 "bytes for GPT Entries\n",
df70b1c2 902 __func__, count);
07f3d789 903 return NULL;
904 }
905
906 /* Read GPT Entries from device */
7c4213f6 907 blk = le64_to_cpu(pgpt_head->partition_entry_lba);
ae1768a7 908 blk_cnt = BLOCK_CNT(count, dev_desc);
2a981dc2 909 if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
07f3d789 910 printf("*** ERROR: Can't read GPT Entries ***\n");
911 free(pte);
912 return NULL;
913 }
914 return pte;
915}
916
917/**
918 * is_pte_valid(): validates a single Partition Table Entry
919 * @gpt_entry - Pointer to a single Partition Table Entry
920 *
921 * Description: returns 1 if valid, 0 on error.
922 */
923static int is_pte_valid(gpt_entry * pte)
924{
925 efi_guid_t unused_guid;
926
927 if (!pte) {
df70b1c2 928 printf("%s: Invalid Argument(s)\n", __func__);
07f3d789 929 return 0;
930 }
931
932 /* Only one validation for now:
933 * The GUID Partition Type != Unused Entry (ALL-ZERO)
934 */
935 memset(unused_guid.b, 0, sizeof(unused_guid.b));
936
937 if (memcmp(pte->partition_type_guid.b, unused_guid.b,
938 sizeof(unused_guid.b)) == 0) {
939
df70b1c2 940 debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
9936be31 941 (unsigned int)(uintptr_t)pte);
07f3d789 942
943 return 0;
944 } else {
945 return 1;
946 }
947}
96e5b03c
SG
948
949/*
950 * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to
951 * check EFI first, since a DOS partition is often used as a 'protective MBR'
952 * with EFI.
953 */
954U_BOOT_PART_TYPE(a_efi) = {
955 .name = "EFI",
956 .part_type = PART_TYPE_EFI,
3e8bd469 957 .get_info = part_get_info_ptr(part_get_info_efi),
084bf4c2
SG
958 .print = part_print_ptr(part_print_efi),
959 .test = part_test_efi,
96e5b03c 960};
07f3d789 961#endif