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); |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 44 | static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba, |
| 45 | gpt_header *pgpt_head, gpt_entry **pgpt_pte); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 46 | static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * 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 | |
| 179 | void print_part_efi(block_dev_desc_t * dev_desc) |
| 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 | |
| 187 | if (!dev_desc) { |
Doug Anderson | dc03667 | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 188 | printf("%s: Invalid Argument(s)\n", __func__); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | /* This function validates AND fills in the GPT header and PTE */ |
| 192 | if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, |
Stephen Warren | 30f39de | 2011-10-28 09:21:46 +0000 | [diff] [blame] | 193 | gpt_head, &gpt_pte) != 1) { |
Doug Anderson | dc03667 | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 194 | printf("%s: *** ERROR: Invalid GPT ***\n", __func__); |
Steve Rae | e1f4ac5 | 2014-05-05 13:00:08 -0700 | [diff] [blame] | 195 | 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 | } |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Doug Anderson | 698a51d | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 206 | debug("%s: gpt-entry at %p\n", __func__, gpt_pte); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 207 | |
Stephen Warren | 2616326 | 2012-10-08 08:14:33 +0000 | [diff] [blame] | 208 | printf("Part\tStart LBA\tEnd LBA\t\tName\n"); |
Stephen Warren | 9fbc2b3 | 2012-10-08 08:14:36 +0000 | [diff] [blame] | 209 | printf("\tAttributes\n"); |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 210 | printf("\tType GUID\n"); |
| 211 | printf("\tPartition GUID\n"); |
Stephen Warren | 8b855b5 | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 212 | |
Chang Hyun Park | 823ffde | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 213 | 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] | 214 | /* Stop at the first non valid PTE */ |
| 215 | if (!is_pte_valid(&gpt_pte[i])) |
| 216 | break; |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 217 | |
Stephen Warren | 2616326 | 2012-10-08 08:14:33 +0000 | [diff] [blame] | 218 | printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1), |
Chang Hyun Park | 823ffde | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 219 | le64_to_cpu(gpt_pte[i].starting_lba), |
| 220 | le64_to_cpu(gpt_pte[i].ending_lba), |
Stephen Warren | 2616326 | 2012-10-08 08:14:33 +0000 | [diff] [blame] | 221 | print_efiname(&gpt_pte[i])); |
Stephen Warren | 9fbc2b3 | 2012-10-08 08:14:36 +0000 | [diff] [blame] | 222 | printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw); |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 223 | uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b; |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 224 | uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID); |
Stephen Warren | 8b855b5 | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 225 | printf("\ttype:\t%s\n", uuid); |
Patrick Delaunay | 65f94ed | 2015-10-27 11:00:28 +0100 | [diff] [blame] | 226 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 227 | if (!uuid_guid_get_str(uuid_bin, uuid)) |
| 228 | printf("\ttype:\t%s\n", uuid); |
| 229 | #endif |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 230 | uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b; |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 231 | uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID); |
| 232 | printf("\tguid:\t%s\n", uuid); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* Remember to free pte */ |
Doug Anderson | 698a51d | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 236 | free(gpt_pte); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | |
| 240 | int get_partition_info_efi(block_dev_desc_t * dev_desc, int part, |
| 241 | disk_partition_t * info) |
| 242 | { |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 243 | 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] | 244 | gpt_entry *gpt_pte = NULL; |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 245 | |
| 246 | /* "part" argument must be at least 1 */ |
| 247 | if (!dev_desc || !info || part < 1) { |
Doug Anderson | dc03667 | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 248 | printf("%s: Invalid Argument(s)\n", __func__); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 249 | 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 Warren | 30f39de | 2011-10-28 09:21:46 +0000 | [diff] [blame] | 254 | gpt_head, &gpt_pte) != 1) { |
Doug Anderson | dc03667 | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 255 | printf("%s: *** ERROR: Invalid GPT ***\n", __func__); |
Steve Rae | e1f4ac5 | 2014-05-05 13:00:08 -0700 | [diff] [blame] | 256 | 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 | } |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 265 | } |
| 266 | |
Chang Hyun Park | 823ffde | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 267 | if (part > le32_to_cpu(gpt_head->num_partition_entries) || |
Stephen Warren | e47dbba | 2012-09-21 09:50:58 +0000 | [diff] [blame] | 268 | !is_pte_valid(&gpt_pte[part - 1])) { |
Mark Langsdorf | e777531 | 2013-09-10 15:14:56 -0500 | [diff] [blame] | 269 | debug("%s: *** ERROR: Invalid partition number %d ***\n", |
Stephen Warren | e47dbba | 2012-09-21 09:50:58 +0000 | [diff] [blame] | 270 | __func__, part); |
Mark Langsdorf | e777531 | 2013-09-10 15:14:56 -0500 | [diff] [blame] | 271 | free(gpt_pte); |
Stephen Warren | e47dbba | 2012-09-21 09:50:58 +0000 | [diff] [blame] | 272 | return -1; |
| 273 | } |
| 274 | |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 275 | /* 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 Retanubun | 413dfc7 | 2009-01-26 08:45:14 -0500 | [diff] [blame] | 277 | /* The ending LBA is inclusive, to calculate size, add 1 to it */ |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 278 | 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] | 279 | - info->start; |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 280 | info->blksz = dev_desc->blksz; |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 281 | |
Lei Wen | 757ddfe | 2011-09-07 18:11:19 +0000 | [diff] [blame] | 282 | sprintf((char *)info->name, "%s", |
Doug Anderson | 698a51d | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 283 | print_efiname(&gpt_pte[part - 1])); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 284 | sprintf((char *)info->type, "U-Boot"); |
Stephen Warren | d0cf3b6 | 2012-10-08 08:14:37 +0000 | [diff] [blame] | 285 | info->bootable = is_bootable(&gpt_pte[part - 1]); |
Stephen Warren | c6c7b7a | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 286 | #ifdef CONFIG_PARTITION_UUIDS |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 287 | uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid, |
| 288 | UUID_STR_FORMAT_GUID); |
Stephen Warren | c6c7b7a | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 289 | #endif |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 290 | #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 |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 294 | |
Steve Rae | f7f10b8 | 2014-05-26 11:52:24 -0700 | [diff] [blame] | 295 | debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__, |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 296 | info->start, info->size, info->name); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 297 | |
| 298 | /* Remember to free pte */ |
Doug Anderson | 698a51d | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 299 | free(gpt_pte); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
Steve Rae | f7f10b8 | 2014-05-26 11:52:24 -0700 | [diff] [blame] | 303 | int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc, |
| 304 | const char *name, disk_partition_t *info) |
| 305 | { |
| 306 | int ret; |
| 307 | int i; |
| 308 | for (i = 1; i < GPT_ENTRY_NUMBERS; i++) { |
| 309 | ret = get_partition_info_efi(dev_desc, i, info); |
| 310 | if (ret != 0) { |
| 311 | /* no more entries in table */ |
| 312 | return -1; |
| 313 | } |
| 314 | if (strcmp(name, (const char *)info->name) == 0) { |
| 315 | /* matched */ |
| 316 | return 0; |
| 317 | } |
| 318 | } |
| 319 | return -2; |
| 320 | } |
| 321 | |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 322 | int test_part_efi(block_dev_desc_t * dev_desc) |
| 323 | { |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 324 | ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 325 | |
| 326 | /* Read legacy MBR from block 0 and validate it */ |
Anton staaf | 3b3df4e | 2011-10-12 13:56:04 +0000 | [diff] [blame] | 327 | if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1) |
| 328 | || (is_pmbr_valid(legacymbr) != 1)) { |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 329 | return -1; |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 330 | } |
| 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 | */ |
| 340 | static int set_protective_mbr(block_dev_desc_t *dev_desc) |
| 341 | { |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 342 | /* Setup the Protective MBR */ |
Hector Palacios | b0a54ff | 2014-02-13 09:48:24 +0100 | [diff] [blame] | 343 | ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1); |
| 344 | memset(p_mbr, 0, sizeof(*p_mbr)); |
| 345 | |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 346 | 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 Ripard | 6d8a152 | 2015-01-08 12:26:44 +0100 | [diff] [blame] | 354 | p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1; |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 355 | |
| 356 | /* Write MBR sector to the MMC device */ |
| 357 | if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) { |
| 358 | printf("** Can't write to device %d **\n", |
| 359 | dev_desc->dev); |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 360 | return -1; |
| 361 | } |
| 362 | |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 366 | int write_gpt_table(block_dev_desc_t *dev_desc, |
| 367 | gpt_header *gpt_h, gpt_entry *gpt_e) |
| 368 | { |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 369 | const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries |
| 370 | * sizeof(gpt_entry)), dev_desc); |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 371 | u32 calc_crc32; |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 372 | |
| 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 */ |
| 389 | if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1) |
| 390 | goto err; |
| 391 | |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 392 | if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e) |
| 393 | != pte_blk_cnt) |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 394 | goto err; |
| 395 | |
Steve Rae | 6f5e17d | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 396 | prepare_backup_gpt_header(gpt_h); |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 397 | |
| 398 | if (dev_desc->block_write(dev_desc->dev, |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 399 | (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba) |
| 400 | + 1, |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 401 | pte_blk_cnt, gpt_e) != pte_blk_cnt) |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 402 | goto err; |
| 403 | |
| 404 | if (dev_desc->block_write(dev_desc->dev, |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 405 | (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1, |
| 406 | gpt_h) != 1) |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 407 | goto err; |
| 408 | |
| 409 | debug("GPT successfully written to block device!\n"); |
| 410 | return 0; |
| 411 | |
| 412 | err: |
| 413 | printf("** Can't write to device %d **\n", dev_desc->dev); |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e, |
| 418 | disk_partition_t *partitions, int parts) |
| 419 | { |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 420 | lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba); |
| 421 | lbaint_t start; |
| 422 | lbaint_t last_usable_lba = (lbaint_t) |
| 423 | le64_to_cpu(gpt_h->last_usable_lba); |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 424 | int i, k; |
Marek Vasut | 941579e | 2013-05-19 12:53:34 +0000 | [diff] [blame] | 425 | size_t efiname_len, dosname_len; |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 426 | #ifdef CONFIG_PARTITION_UUIDS |
| 427 | char *str_uuid; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 428 | unsigned char *bin_uuid; |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 429 | #endif |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 430 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 431 | char *str_type_guid; |
| 432 | unsigned char *bin_type_guid; |
| 433 | #endif |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 434 | |
| 435 | for (i = 0; i < parts; i++) { |
| 436 | /* partition starting lba */ |
| 437 | start = partitions[i].start; |
| 438 | if (start && (start < offset)) { |
| 439 | printf("Partition overlap\n"); |
| 440 | return -1; |
| 441 | } |
| 442 | if (start) { |
| 443 | gpt_e[i].starting_lba = cpu_to_le64(start); |
| 444 | offset = start + partitions[i].size; |
| 445 | } else { |
| 446 | gpt_e[i].starting_lba = cpu_to_le64(offset); |
| 447 | offset += partitions[i].size; |
| 448 | } |
Steve Rae | 6d0d172 | 2014-05-26 11:52:22 -0700 | [diff] [blame] | 449 | if (offset >= last_usable_lba) { |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 450 | printf("Partitions layout exceds disk size\n"); |
| 451 | return -1; |
| 452 | } |
| 453 | /* partition ending lba */ |
| 454 | if ((i == parts - 1) && (partitions[i].size == 0)) |
| 455 | /* extend the last partition to maximuim */ |
| 456 | gpt_e[i].ending_lba = gpt_h->last_usable_lba; |
| 457 | else |
| 458 | gpt_e[i].ending_lba = cpu_to_le64(offset - 1); |
| 459 | |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 460 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 461 | str_type_guid = partitions[i].type_guid; |
| 462 | bin_type_guid = gpt_e[i].partition_type_guid.b; |
| 463 | if (strlen(str_type_guid)) { |
| 464 | if (uuid_str_to_bin(str_type_guid, bin_type_guid, |
| 465 | UUID_STR_FORMAT_GUID)) { |
| 466 | printf("Partition no. %d: invalid type guid: %s\n", |
| 467 | i, str_type_guid); |
| 468 | return -1; |
| 469 | } |
| 470 | } else { |
| 471 | /* default partition type GUID */ |
| 472 | memcpy(bin_type_guid, |
| 473 | &PARTITION_BASIC_DATA_GUID, 16); |
| 474 | } |
| 475 | #else |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 476 | /* partition type GUID */ |
| 477 | memcpy(gpt_e[i].partition_type_guid.b, |
| 478 | &PARTITION_BASIC_DATA_GUID, 16); |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 479 | #endif |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 480 | |
| 481 | #ifdef CONFIG_PARTITION_UUIDS |
| 482 | str_uuid = partitions[i].uuid; |
Przemyslaw Marczak | fa90734 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 483 | bin_uuid = gpt_e[i].unique_partition_guid.b; |
| 484 | |
Przemyslaw Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 485 | 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] | 486 | printf("Partition no. %d: invalid guid: %s\n", |
| 487 | i, str_uuid); |
| 488 | return -1; |
| 489 | } |
| 490 | #endif |
| 491 | |
| 492 | /* partition attributes */ |
| 493 | memset(&gpt_e[i].attributes, 0, |
| 494 | sizeof(gpt_entry_attributes)); |
| 495 | |
| 496 | /* partition name */ |
Marek Vasut | 941579e | 2013-05-19 12:53:34 +0000 | [diff] [blame] | 497 | efiname_len = sizeof(gpt_e[i].partition_name) |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 498 | / sizeof(efi_char16_t); |
Marek Vasut | 941579e | 2013-05-19 12:53:34 +0000 | [diff] [blame] | 499 | 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 Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 505 | gpt_e[i].partition_name[k] = |
| 506 | (efi_char16_t)(partitions[i].name[k]); |
| 507 | |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 508 | debug("%s: name: %s offset[%d]: 0x" LBAF |
| 509 | " size[%d]: 0x" LBAF "\n", |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 510 | __func__, partitions[i].name, i, |
| 511 | offset, i, partitions[i].size); |
| 512 | } |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h, |
| 518 | 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 Marczak | 0c81336 | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 533 | 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] | 534 | return -1; |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid, |
| 540 | disk_partition_t *partitions, int parts_count) |
| 541 | { |
| 542 | int ret; |
| 543 | |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 544 | gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header), |
| 545 | dev_desc)); |
| 546 | gpt_entry *gpt_e; |
| 547 | |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 548 | if (gpt_h == NULL) { |
| 549 | printf("%s: calloc failed!\n", __func__); |
| 550 | return -1; |
| 551 | } |
| 552 | |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 553 | gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS |
| 554 | * sizeof(gpt_entry), |
| 555 | dev_desc)); |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 556 | 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 | |
| 575 | err: |
| 576 | free(gpt_e); |
| 577 | free(gpt_h); |
| 578 | return ret; |
| 579 | } |
Steve Rae | 7d05934 | 2014-12-12 15:51:54 -0800 | [diff] [blame] | 580 | |
| 581 | int is_valid_gpt_buf(block_dev_desc_t *dev_desc, void *buf) |
| 582 | { |
| 583 | gpt_header *gpt_h; |
| 584 | gpt_entry *gpt_e; |
| 585 | |
| 586 | /* determine start of GPT Header in the buffer */ |
| 587 | gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * |
| 588 | dev_desc->blksz); |
| 589 | if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA, |
| 590 | dev_desc->lba)) |
| 591 | return -1; |
| 592 | |
| 593 | /* determine start of GPT Entries in the buffer */ |
| 594 | gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) * |
| 595 | dev_desc->blksz); |
| 596 | if (validate_gpt_entries(gpt_h, gpt_e)) |
| 597 | return -1; |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | int write_mbr_and_gpt_partitions(block_dev_desc_t *dev_desc, void *buf) |
| 603 | { |
| 604 | gpt_header *gpt_h; |
| 605 | gpt_entry *gpt_e; |
| 606 | int gpt_e_blk_cnt; |
| 607 | lbaint_t lba; |
| 608 | int cnt; |
| 609 | |
| 610 | if (is_valid_gpt_buf(dev_desc, buf)) |
| 611 | return -1; |
| 612 | |
| 613 | /* determine start of GPT Header in the buffer */ |
| 614 | gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * |
| 615 | dev_desc->blksz); |
| 616 | |
| 617 | /* determine start of GPT Entries in the buffer */ |
| 618 | gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) * |
| 619 | dev_desc->blksz); |
| 620 | gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) * |
| 621 | le32_to_cpu(gpt_h->sizeof_partition_entry)), |
| 622 | dev_desc); |
| 623 | |
| 624 | /* write MBR */ |
| 625 | lba = 0; /* MBR is always at 0 */ |
| 626 | cnt = 1; /* MBR (1 block) */ |
| 627 | if (dev_desc->block_write(dev_desc->dev, lba, cnt, buf) != cnt) { |
| 628 | printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 629 | __func__, "MBR", cnt, lba); |
| 630 | return 1; |
| 631 | } |
| 632 | |
| 633 | /* write Primary GPT */ |
| 634 | lba = GPT_PRIMARY_PARTITION_TABLE_LBA; |
| 635 | cnt = 1; /* GPT Header (1 block) */ |
| 636 | if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_h) != cnt) { |
| 637 | printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 638 | __func__, "Primary GPT Header", cnt, lba); |
| 639 | return 1; |
| 640 | } |
| 641 | |
| 642 | lba = le64_to_cpu(gpt_h->partition_entry_lba); |
| 643 | cnt = gpt_e_blk_cnt; |
| 644 | if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_e) != cnt) { |
| 645 | printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 646 | __func__, "Primary GPT Entries", cnt, lba); |
| 647 | return 1; |
| 648 | } |
| 649 | |
| 650 | prepare_backup_gpt_header(gpt_h); |
| 651 | |
| 652 | /* write Backup GPT */ |
| 653 | lba = le64_to_cpu(gpt_h->partition_entry_lba); |
| 654 | cnt = gpt_e_blk_cnt; |
| 655 | if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_e) != cnt) { |
| 656 | printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 657 | __func__, "Backup GPT Entries", cnt, lba); |
| 658 | return 1; |
| 659 | } |
| 660 | |
| 661 | lba = le64_to_cpu(gpt_h->my_lba); |
| 662 | cnt = 1; /* GPT Header (1 block) */ |
| 663 | if (dev_desc->block_write(dev_desc->dev, lba, cnt, gpt_h) != cnt) { |
| 664 | printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n", |
| 665 | __func__, "Backup GPT Header", cnt, lba); |
| 666 | return 1; |
| 667 | } |
| 668 | |
| 669 | return 0; |
| 670 | } |
Lukasz Majewski | 0dcfc5c | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 671 | #endif |
| 672 | |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 673 | /* |
| 674 | * Private functions |
| 675 | */ |
| 676 | /* |
| 677 | * pmbr_part_valid(): Check for EFI partition signature |
| 678 | * |
| 679 | * Returns: 1 if EFI GPT partition type is found. |
| 680 | */ |
| 681 | static int pmbr_part_valid(struct partition *part) |
| 682 | { |
| 683 | if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT && |
Marc Dietrich | 20ca3ad | 2013-03-29 07:57:10 +0000 | [diff] [blame] | 684 | get_unaligned_le32(&part->start_sect) == 1UL) { |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 685 | return 1; |
| 686 | } |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * is_pmbr_valid(): test Protective MBR for validity |
| 693 | * |
| 694 | * Returns: 1 if PMBR is valid, 0 otherwise. |
| 695 | * Validity depends on two things: |
| 696 | * 1) MSDOS signature is in the last two bytes of the MBR |
| 697 | * 2) One partition of type 0xEE is found, checked by pmbr_part_valid() |
| 698 | */ |
| 699 | static int is_pmbr_valid(legacy_mbr * mbr) |
| 700 | { |
| 701 | int i = 0; |
| 702 | |
Chang Hyun Park | 823ffde | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 703 | if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE) |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 704 | return 0; |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 705 | |
| 706 | for (i = 0; i < 4; i++) { |
| 707 | if (pmbr_part_valid(&mbr->partition_record[i])) { |
| 708 | return 1; |
| 709 | } |
| 710 | } |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * is_gpt_valid() - tests one GPT header and PTEs for validity |
| 716 | * |
| 717 | * lba is the logical block address of the GPT header to test |
| 718 | * gpt is a GPT header ptr, filled on return. |
| 719 | * ptes is a PTEs ptr, filled on return. |
| 720 | * |
| 721 | * Description: returns 1 if valid, 0 on error. |
| 722 | * If valid, returns pointers to PTEs. |
| 723 | */ |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 724 | static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba, |
| 725 | gpt_header *pgpt_head, gpt_entry **pgpt_pte) |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 726 | { |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 727 | if (!dev_desc || !pgpt_head) { |
Doug Anderson | dc03667 | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 728 | printf("%s: Invalid Argument(s)\n", __func__); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | /* Read GPT Header from device */ |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 733 | if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head) |
| 734 | != 1) { |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 735 | printf("*** ERROR: Can't read GPT header ***\n"); |
| 736 | return 0; |
| 737 | } |
| 738 | |
Steve Rae | 6f5e17d | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 739 | if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba)) |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 740 | return 0; |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 741 | |
| 742 | /* Read and allocate Partition Table Entries */ |
| 743 | *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head); |
| 744 | if (*pgpt_pte == NULL) { |
| 745 | printf("GPT: Failed to allocate memory for PTE\n"); |
| 746 | return 0; |
| 747 | } |
| 748 | |
Steve Rae | 6f5e17d | 2014-12-18 12:13:42 +0100 | [diff] [blame] | 749 | if (validate_gpt_entries(pgpt_head, *pgpt_pte)) { |
Doug Anderson | 698a51d | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 750 | free(*pgpt_pte); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /* We're done, all's well */ |
| 755 | return 1; |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * alloc_read_gpt_entries(): reads partition entries from disk |
| 760 | * @dev_desc |
| 761 | * @gpt - GPT header |
| 762 | * |
| 763 | * Description: Returns ptes on success, NULL on error. |
| 764 | * Allocates space for PTEs based on information found in @gpt. |
| 765 | * Notes: remember to free pte when you're done! |
| 766 | */ |
| 767 | static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc, |
| 768 | gpt_header * pgpt_head) |
| 769 | { |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 770 | size_t count = 0, blk_cnt; |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 771 | gpt_entry *pte = NULL; |
| 772 | |
| 773 | if (!dev_desc || !pgpt_head) { |
Doug Anderson | dc03667 | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 774 | printf("%s: Invalid Argument(s)\n", __func__); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 775 | return NULL; |
| 776 | } |
| 777 | |
Chang Hyun Park | 823ffde | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 778 | count = le32_to_cpu(pgpt_head->num_partition_entries) * |
| 779 | le32_to_cpu(pgpt_head->sizeof_partition_entry); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 780 | |
Chang Hyun Park | 823ffde | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 781 | debug("%s: count = %u * %u = %zu\n", __func__, |
| 782 | (u32) le32_to_cpu(pgpt_head->num_partition_entries), |
| 783 | (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 784 | |
| 785 | /* Allocate memory for PTE, remember to FREE */ |
| 786 | if (count != 0) { |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 787 | pte = memalign(ARCH_DMA_MINALIGN, |
| 788 | PAD_TO_BLOCKSIZE(count, dev_desc)); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | if (count == 0 || pte == NULL) { |
Taylor Hutt | 864074d | 2012-10-12 14:26:09 +0000 | [diff] [blame] | 792 | printf("%s: ERROR: Can't allocate 0x%zX " |
| 793 | "bytes for GPT Entries\n", |
Doug Anderson | dc03667 | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 794 | __func__, count); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 795 | return NULL; |
| 796 | } |
| 797 | |
| 798 | /* Read GPT Entries from device */ |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 799 | blk_cnt = BLOCK_CNT(count, dev_desc); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 800 | if (dev_desc->block_read (dev_desc->dev, |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 801 | (lbaint_t)le64_to_cpu(pgpt_head->partition_entry_lba), |
Egbert Eich | 071e423 | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 802 | (lbaint_t) (blk_cnt), pte) |
| 803 | != blk_cnt) { |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 804 | |
| 805 | printf("*** ERROR: Can't read GPT Entries ***\n"); |
| 806 | free(pte); |
| 807 | return NULL; |
| 808 | } |
| 809 | return pte; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * is_pte_valid(): validates a single Partition Table Entry |
| 814 | * @gpt_entry - Pointer to a single Partition Table Entry |
| 815 | * |
| 816 | * Description: returns 1 if valid, 0 on error. |
| 817 | */ |
| 818 | static int is_pte_valid(gpt_entry * pte) |
| 819 | { |
| 820 | efi_guid_t unused_guid; |
| 821 | |
| 822 | if (!pte) { |
Doug Anderson | dc03667 | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 823 | printf("%s: Invalid Argument(s)\n", __func__); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | /* Only one validation for now: |
| 828 | * The GUID Partition Type != Unused Entry (ALL-ZERO) |
| 829 | */ |
| 830 | memset(unused_guid.b, 0, sizeof(unused_guid.b)); |
| 831 | |
| 832 | if (memcmp(pte->partition_type_guid.b, unused_guid.b, |
| 833 | sizeof(unused_guid.b)) == 0) { |
| 834 | |
Doug Anderson | dc03667 | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 835 | debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__, |
Taylor Hutt | 864074d | 2012-10-12 14:26:09 +0000 | [diff] [blame] | 836 | (unsigned int)(uintptr_t)pte); |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 837 | |
| 838 | return 0; |
| 839 | } else { |
| 840 | return 1; |
| 841 | } |
| 842 | } |
| 843 | #endif |