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