richardretanubun | 07f3d78 | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
Steve Rae | e04350d | 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 | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 12 | */ |
Marc Dietrich | 8faefad | 2013-03-29 07:57:10 +0000 | [diff] [blame] | 13 | #include <asm/unaligned.h> |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 14 | #include <common.h> |
| 15 | #include <command.h> |
| 16 | #include <ide.h> |
Simon Glass | e48f374 | 2014-11-11 12:47:07 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 18 | #include <malloc.h> |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 19 | #include <part_efi.h> |
Lei Wen | 6eecc03 | 2011-09-07 18:11:19 +0000 | [diff] [blame] | 20 | #include <linux/ctype.h> |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 21 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Stephen Warren | 2c1af9d | 2013-02-28 15:03:46 +0000 | [diff] [blame] | 24 | #ifdef HAVE_BLOCK_DEVICE |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 25 | /** |
| 26 | * efi_crc32() - EFI version of crc32 function |
| 27 | * @buf: buffer to calculate crc32 of |
| 28 | * @len - length of buf |
| 29 | * |
| 30 | * Description: Returns EFI-style CRC32 value for @buf |
| 31 | */ |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 32 | static inline u32 efi_crc32(const void *buf, u32 len) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 33 | { |
| 34 | return crc32(0, buf, len); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Private function prototypes |
| 39 | */ |
| 40 | |
| 41 | static int pmbr_part_valid(struct partition *part); |
| 42 | static int is_pmbr_valid(legacy_mbr * mbr); |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 43 | static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba, |
| 44 | gpt_header *pgpt_head, gpt_entry **pgpt_pte); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 45 | static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc, |
| 46 | gpt_header * pgpt_head); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 47 | static int is_pte_valid(gpt_entry * pte); |
| 48 | |
Lei Wen | 6eecc03 | 2011-09-07 18:11:19 +0000 | [diff] [blame] | 49 | static char *print_efiname(gpt_entry *pte) |
| 50 | { |
| 51 | static char name[PARTNAME_SZ + 1]; |
| 52 | int i; |
| 53 | for (i = 0; i < PARTNAME_SZ; i++) { |
| 54 | u8 c; |
| 55 | c = pte->partition_name[i] & 0xff; |
| 56 | c = (c && !isprint(c)) ? '.' : c; |
| 57 | name[i] = c; |
| 58 | } |
| 59 | name[PARTNAME_SZ] = 0; |
| 60 | return name; |
| 61 | } |
| 62 | |
Stephen Warren | b4414f4 | 2012-10-08 08:14:37 +0000 | [diff] [blame] | 63 | static efi_guid_t system_guid = PARTITION_SYSTEM_GUID; |
| 64 | |
| 65 | static inline int is_bootable(gpt_entry *p) |
| 66 | { |
| 67 | return p->attributes.fields.legacy_bios_bootable || |
| 68 | !memcmp(&(p->partition_type_guid), &system_guid, |
| 69 | sizeof(efi_guid_t)); |
| 70 | } |
| 71 | |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame^] | 72 | static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba, |
| 73 | lbaint_t lastlba) |
| 74 | { |
| 75 | uint32_t crc32_backup = 0; |
| 76 | uint32_t calc_crc32; |
| 77 | |
| 78 | /* Check the GPT header signature */ |
| 79 | if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE) { |
| 80 | printf("%s signature is wrong: 0x%llX != 0x%llX\n", |
| 81 | "GUID Partition Table Header", |
| 82 | le64_to_cpu(gpt_h->signature), |
| 83 | GPT_HEADER_SIGNATURE); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | /* Check the GUID Partition Table CRC */ |
| 88 | memcpy(&crc32_backup, &gpt_h->header_crc32, sizeof(crc32_backup)); |
| 89 | memset(&gpt_h->header_crc32, 0, sizeof(gpt_h->header_crc32)); |
| 90 | |
| 91 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, |
| 92 | le32_to_cpu(gpt_h->header_size)); |
| 93 | |
| 94 | memcpy(&gpt_h->header_crc32, &crc32_backup, sizeof(crc32_backup)); |
| 95 | |
| 96 | if (calc_crc32 != le32_to_cpu(crc32_backup)) { |
| 97 | printf("%s CRC is wrong: 0x%x != 0x%x\n", |
| 98 | "GUID Partition Table Header", |
| 99 | le32_to_cpu(crc32_backup), calc_crc32); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Check that the my_lba entry points to the LBA that contains the GPT |
| 105 | */ |
| 106 | if (le64_to_cpu(gpt_h->my_lba) != lba) { |
| 107 | printf("GPT: my_lba incorrect: %llX != " LBAF "\n", |
| 108 | le64_to_cpu(gpt_h->my_lba), |
| 109 | lba); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Check that the first_usable_lba and that the last_usable_lba are |
| 115 | * within the disk. |
| 116 | */ |
| 117 | if (le64_to_cpu(gpt_h->first_usable_lba) > lastlba) { |
| 118 | printf("GPT: first_usable_lba incorrect: %llX > " LBAF "\n", |
| 119 | le64_to_cpu(gpt_h->first_usable_lba), lastlba); |
| 120 | return -1; |
| 121 | } |
| 122 | if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) { |
| 123 | printf("GPT: last_usable_lba incorrect: %llX > " LBAF "\n", |
| 124 | le64_to_cpu(gpt_h->last_usable_lba), lastlba); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | debug("GPT: first_usable_lba: %llX last_usable_lba: %llX last lba: " |
| 129 | LBAF "\n", le64_to_cpu(gpt_h->first_usable_lba), |
| 130 | le64_to_cpu(gpt_h->last_usable_lba), lastlba); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e) |
| 136 | { |
| 137 | uint32_t calc_crc32; |
| 138 | |
| 139 | /* Check the GUID Partition Table Entry Array CRC */ |
| 140 | calc_crc32 = efi_crc32((const unsigned char *)gpt_e, |
| 141 | le32_to_cpu(gpt_h->num_partition_entries) * |
| 142 | le32_to_cpu(gpt_h->sizeof_partition_entry)); |
| 143 | |
| 144 | if (calc_crc32 != le32_to_cpu(gpt_h->partition_entry_array_crc32)) { |
| 145 | printf("%s: 0x%x != 0x%x\n", |
| 146 | "GUID Partition Table Entry Array CRC is wrong", |
| 147 | le32_to_cpu(gpt_h->partition_entry_array_crc32), |
| 148 | calc_crc32); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static void prepare_backup_gpt_header(gpt_header *gpt_h) |
| 156 | { |
| 157 | uint32_t calc_crc32; |
| 158 | uint64_t val; |
| 159 | |
| 160 | /* recalculate the values for the Backup GPT Header */ |
| 161 | val = le64_to_cpu(gpt_h->my_lba); |
| 162 | gpt_h->my_lba = gpt_h->alternate_lba; |
| 163 | gpt_h->alternate_lba = cpu_to_le64(val); |
| 164 | gpt_h->header_crc32 = 0; |
| 165 | |
| 166 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, |
| 167 | le32_to_cpu(gpt_h->header_size)); |
| 168 | gpt_h->header_crc32 = cpu_to_le32(calc_crc32); |
| 169 | } |
| 170 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 171 | #ifdef CONFIG_EFI_PARTITION |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 172 | /* |
| 173 | * Public Functions (include/part.h) |
| 174 | */ |
| 175 | |
| 176 | void print_part_efi(block_dev_desc_t * dev_desc) |
| 177 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 178 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 179 | gpt_entry *gpt_pte = NULL; |
| 180 | int i = 0; |
| 181 | char uuid[37]; |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 182 | unsigned char *uuid_bin; |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 183 | |
| 184 | if (!dev_desc) { |
| 185 | printf("%s: Invalid Argument(s)\n", __func__); |
| 186 | return; |
| 187 | } |
| 188 | /* This function validates AND fills in the GPT header and PTE */ |
| 189 | if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, |
| 190 | gpt_head, &gpt_pte) != 1) { |
| 191 | printf("%s: *** ERROR: Invalid GPT ***\n", __func__); |
Steve Rae | ae95fad | 2014-05-05 13:00:08 -0700 | [diff] [blame] | 192 | if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), |
| 193 | gpt_head, &gpt_pte) != 1) { |
| 194 | printf("%s: *** ERROR: Invalid Backup GPT ***\n", |
| 195 | __func__); |
| 196 | return; |
| 197 | } else { |
| 198 | printf("%s: *** Using Backup GPT ***\n", |
| 199 | __func__); |
| 200 | } |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | debug("%s: gpt-entry at %p\n", __func__, gpt_pte); |
| 204 | |
| 205 | printf("Part\tStart LBA\tEnd LBA\t\tName\n"); |
Stephen Warren | 13bf2f5 | 2012-10-08 08:14:36 +0000 | [diff] [blame] | 206 | printf("\tAttributes\n"); |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 207 | printf("\tType GUID\n"); |
| 208 | printf("\tPartition GUID\n"); |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 209 | |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 210 | for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) { |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 211 | /* Stop at the first non valid PTE */ |
| 212 | if (!is_pte_valid(&gpt_pte[i])) |
| 213 | break; |
| 214 | |
| 215 | printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1), |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 216 | le64_to_cpu(gpt_pte[i].starting_lba), |
| 217 | le64_to_cpu(gpt_pte[i].ending_lba), |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 218 | print_efiname(&gpt_pte[i])); |
Stephen Warren | 13bf2f5 | 2012-10-08 08:14:36 +0000 | [diff] [blame] | 219 | printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw); |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 220 | uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b; |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 221 | uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID); |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 222 | printf("\ttype:\t%s\n", uuid); |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 223 | uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b; |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 224 | uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID); |
| 225 | printf("\tguid:\t%s\n", uuid); |
Stephen Warren | f07cd2c | 2012-10-08 08:14:34 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* Remember to free pte */ |
| 229 | free(gpt_pte); |
| 230 | return; |
| 231 | } |
Stephen Warren | 894bfbb | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 232 | |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 233 | int get_partition_info_efi(block_dev_desc_t * dev_desc, int part, |
| 234 | disk_partition_t * info) |
| 235 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 236 | ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); |
Doug Anderson | deb5ca8 | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 237 | gpt_entry *gpt_pte = NULL; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 238 | |
| 239 | /* "part" argument must be at least 1 */ |
| 240 | if (!dev_desc || !info || part < 1) { |
Doug Anderson | df70b1c | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 241 | printf("%s: Invalid Argument(s)\n", __func__); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | /* This function validates AND fills in the GPT header and PTE */ |
| 246 | if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, |
Stephen Warren | 4715a81 | 2011-10-28 09:21:46 +0000 | [diff] [blame] | 247 | gpt_head, &gpt_pte) != 1) { |
Doug Anderson | df70b1c | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 248 | printf("%s: *** ERROR: Invalid GPT ***\n", __func__); |
Steve Rae | ae95fad | 2014-05-05 13:00:08 -0700 | [diff] [blame] | 249 | if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), |
| 250 | gpt_head, &gpt_pte) != 1) { |
| 251 | printf("%s: *** ERROR: Invalid Backup GPT ***\n", |
| 252 | __func__); |
| 253 | return -1; |
| 254 | } else { |
| 255 | printf("%s: *** Using Backup GPT ***\n", |
| 256 | __func__); |
| 257 | } |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 260 | if (part > le32_to_cpu(gpt_head->num_partition_entries) || |
Stephen Warren | c04d68c | 2012-09-21 09:50:58 +0000 | [diff] [blame] | 261 | !is_pte_valid(&gpt_pte[part - 1])) { |
Mark Langsdorf | 6d2ee5a | 2013-09-10 15:14:56 -0500 | [diff] [blame] | 262 | debug("%s: *** ERROR: Invalid partition number %d ***\n", |
Stephen Warren | c04d68c | 2012-09-21 09:50:58 +0000 | [diff] [blame] | 263 | __func__, part); |
Mark Langsdorf | 6d2ee5a | 2013-09-10 15:14:56 -0500 | [diff] [blame] | 264 | free(gpt_pte); |
Stephen Warren | c04d68c | 2012-09-21 09:50:58 +0000 | [diff] [blame] | 265 | return -1; |
| 266 | } |
| 267 | |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 268 | /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */ |
| 269 | info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba); |
Richard Retanubun | 5097083 | 2009-01-26 08:45:14 -0500 | [diff] [blame] | 270 | /* The ending LBA is inclusive, to calculate size, add 1 to it */ |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 271 | info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1 |
Richard Retanubun | 5097083 | 2009-01-26 08:45:14 -0500 | [diff] [blame] | 272 | - info->start; |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 273 | info->blksz = dev_desc->blksz; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 274 | |
Lei Wen | 6eecc03 | 2011-09-07 18:11:19 +0000 | [diff] [blame] | 275 | sprintf((char *)info->name, "%s", |
Doug Anderson | deb5ca8 | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 276 | print_efiname(&gpt_pte[part - 1])); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 277 | sprintf((char *)info->type, "U-Boot"); |
Stephen Warren | b4414f4 | 2012-10-08 08:14:37 +0000 | [diff] [blame] | 278 | info->bootable = is_bootable(&gpt_pte[part - 1]); |
Stephen Warren | 894bfbb | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 279 | #ifdef CONFIG_PARTITION_UUIDS |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 280 | uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid, |
| 281 | UUID_STR_FORMAT_GUID); |
Stephen Warren | 894bfbb | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 282 | #endif |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 283 | |
Steve Rae | 60bf941 | 2014-05-26 11:52:24 -0700 | [diff] [blame] | 284 | debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__, |
Frederic Leroy | 04735e9 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 285 | info->start, info->size, info->name); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 286 | |
| 287 | /* Remember to free pte */ |
Doug Anderson | deb5ca8 | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 288 | free(gpt_pte); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
Steve Rae | 60bf941 | 2014-05-26 11:52:24 -0700 | [diff] [blame] | 292 | int get_partition_info_efi_by_name(block_dev_desc_t *dev_desc, |
| 293 | const char *name, disk_partition_t *info) |
| 294 | { |
| 295 | int ret; |
| 296 | int i; |
| 297 | for (i = 1; i < GPT_ENTRY_NUMBERS; i++) { |
| 298 | ret = get_partition_info_efi(dev_desc, i, info); |
| 299 | if (ret != 0) { |
| 300 | /* no more entries in table */ |
| 301 | return -1; |
| 302 | } |
| 303 | if (strcmp(name, (const char *)info->name) == 0) { |
| 304 | /* matched */ |
| 305 | return 0; |
| 306 | } |
| 307 | } |
| 308 | return -2; |
| 309 | } |
| 310 | |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 311 | int test_part_efi(block_dev_desc_t * dev_desc) |
| 312 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 313 | ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 314 | |
| 315 | /* Read legacy MBR from block 0 and validate it */ |
Anton staaf | f75dd58 | 2011-10-12 13:56:04 +0000 | [diff] [blame] | 316 | if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1) |
| 317 | || (is_pmbr_valid(legacymbr) != 1)) { |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 318 | return -1; |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 323 | /** |
| 324 | * set_protective_mbr(): Set the EFI protective MBR |
| 325 | * @param dev_desc - block device descriptor |
| 326 | * |
| 327 | * @return - zero on success, otherwise error |
| 328 | */ |
| 329 | static int set_protective_mbr(block_dev_desc_t *dev_desc) |
| 330 | { |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 331 | /* Setup the Protective MBR */ |
Hector Palacios | 61fcc7d | 2014-02-13 09:48:24 +0100 | [diff] [blame] | 332 | ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1); |
| 333 | memset(p_mbr, 0, sizeof(*p_mbr)); |
| 334 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 335 | if (p_mbr == NULL) { |
| 336 | printf("%s: calloc failed!\n", __func__); |
| 337 | return -1; |
| 338 | } |
| 339 | /* Append signature */ |
| 340 | p_mbr->signature = MSDOS_MBR_SIGNATURE; |
| 341 | p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT; |
| 342 | p_mbr->partition_record[0].start_sect = 1; |
| 343 | p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba; |
| 344 | |
| 345 | /* Write MBR sector to the MMC device */ |
| 346 | if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) { |
| 347 | printf("** Can't write to device %d **\n", |
| 348 | dev_desc->dev); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 349 | return -1; |
| 350 | } |
| 351 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 355 | int write_gpt_table(block_dev_desc_t *dev_desc, |
| 356 | gpt_header *gpt_h, gpt_entry *gpt_e) |
| 357 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 358 | const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries |
| 359 | * sizeof(gpt_entry)), dev_desc); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 360 | u32 calc_crc32; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 361 | |
| 362 | debug("max lba: %x\n", (u32) dev_desc->lba); |
| 363 | /* Setup the Protective MBR */ |
| 364 | if (set_protective_mbr(dev_desc) < 0) |
| 365 | goto err; |
| 366 | |
| 367 | /* Generate CRC for the Primary GPT Header */ |
| 368 | calc_crc32 = efi_crc32((const unsigned char *)gpt_e, |
| 369 | le32_to_cpu(gpt_h->num_partition_entries) * |
| 370 | le32_to_cpu(gpt_h->sizeof_partition_entry)); |
| 371 | gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32); |
| 372 | |
| 373 | calc_crc32 = efi_crc32((const unsigned char *)gpt_h, |
| 374 | le32_to_cpu(gpt_h->header_size)); |
| 375 | gpt_h->header_crc32 = cpu_to_le32(calc_crc32); |
| 376 | |
| 377 | /* Write the First GPT to the block right after the Legacy MBR */ |
| 378 | if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1) |
| 379 | goto err; |
| 380 | |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 381 | if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_cnt, gpt_e) |
| 382 | != pte_blk_cnt) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 383 | goto err; |
| 384 | |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame^] | 385 | prepare_backup_gpt_header(gpt_h); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 386 | |
| 387 | if (dev_desc->block_write(dev_desc->dev, |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 388 | (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba) |
| 389 | + 1, |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 390 | pte_blk_cnt, gpt_e) != pte_blk_cnt) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 391 | goto err; |
| 392 | |
| 393 | if (dev_desc->block_write(dev_desc->dev, |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 394 | (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1, |
| 395 | gpt_h) != 1) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 396 | goto err; |
| 397 | |
| 398 | debug("GPT successfully written to block device!\n"); |
| 399 | return 0; |
| 400 | |
| 401 | err: |
| 402 | printf("** Can't write to device %d **\n", dev_desc->dev); |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e, |
| 407 | disk_partition_t *partitions, int parts) |
| 408 | { |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 409 | lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba); |
| 410 | lbaint_t start; |
| 411 | lbaint_t last_usable_lba = (lbaint_t) |
| 412 | le64_to_cpu(gpt_h->last_usable_lba); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 413 | int i, k; |
Marek Vasut | 67cd4a6 | 2013-05-19 12:53:34 +0000 | [diff] [blame] | 414 | size_t efiname_len, dosname_len; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 415 | #ifdef CONFIG_PARTITION_UUIDS |
| 416 | char *str_uuid; |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 417 | unsigned char *bin_uuid; |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 418 | #endif |
| 419 | |
| 420 | for (i = 0; i < parts; i++) { |
| 421 | /* partition starting lba */ |
| 422 | start = partitions[i].start; |
| 423 | if (start && (start < offset)) { |
| 424 | printf("Partition overlap\n"); |
| 425 | return -1; |
| 426 | } |
| 427 | if (start) { |
| 428 | gpt_e[i].starting_lba = cpu_to_le64(start); |
| 429 | offset = start + partitions[i].size; |
| 430 | } else { |
| 431 | gpt_e[i].starting_lba = cpu_to_le64(offset); |
| 432 | offset += partitions[i].size; |
| 433 | } |
Steve Rae | dedf37b | 2014-05-26 11:52:22 -0700 | [diff] [blame] | 434 | if (offset >= last_usable_lba) { |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 435 | printf("Partitions layout exceds disk size\n"); |
| 436 | return -1; |
| 437 | } |
| 438 | /* partition ending lba */ |
| 439 | if ((i == parts - 1) && (partitions[i].size == 0)) |
| 440 | /* extend the last partition to maximuim */ |
| 441 | gpt_e[i].ending_lba = gpt_h->last_usable_lba; |
| 442 | else |
| 443 | gpt_e[i].ending_lba = cpu_to_le64(offset - 1); |
| 444 | |
| 445 | /* partition type GUID */ |
| 446 | memcpy(gpt_e[i].partition_type_guid.b, |
| 447 | &PARTITION_BASIC_DATA_GUID, 16); |
| 448 | |
| 449 | #ifdef CONFIG_PARTITION_UUIDS |
| 450 | str_uuid = partitions[i].uuid; |
Przemyslaw Marczak | a96a0e6 | 2014-04-02 10:20:02 +0200 | [diff] [blame] | 451 | bin_uuid = gpt_e[i].unique_partition_guid.b; |
| 452 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 453 | if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) { |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 454 | printf("Partition no. %d: invalid guid: %s\n", |
| 455 | i, str_uuid); |
| 456 | return -1; |
| 457 | } |
| 458 | #endif |
| 459 | |
| 460 | /* partition attributes */ |
| 461 | memset(&gpt_e[i].attributes, 0, |
| 462 | sizeof(gpt_entry_attributes)); |
| 463 | |
| 464 | /* partition name */ |
Marek Vasut | 67cd4a6 | 2013-05-19 12:53:34 +0000 | [diff] [blame] | 465 | efiname_len = sizeof(gpt_e[i].partition_name) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 466 | / sizeof(efi_char16_t); |
Marek Vasut | 67cd4a6 | 2013-05-19 12:53:34 +0000 | [diff] [blame] | 467 | dosname_len = sizeof(partitions[i].name); |
| 468 | |
| 469 | memset(gpt_e[i].partition_name, 0, |
| 470 | sizeof(gpt_e[i].partition_name)); |
| 471 | |
| 472 | for (k = 0; k < min(dosname_len, efiname_len); k++) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 473 | gpt_e[i].partition_name[k] = |
| 474 | (efi_char16_t)(partitions[i].name[k]); |
| 475 | |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 476 | debug("%s: name: %s offset[%d]: 0x" LBAF |
| 477 | " size[%d]: 0x" LBAF "\n", |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 478 | __func__, partitions[i].name, i, |
| 479 | offset, i, partitions[i].size); |
| 480 | } |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h, |
| 486 | char *str_guid, int parts_count) |
| 487 | { |
| 488 | gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE); |
| 489 | gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1); |
| 490 | gpt_h->header_size = cpu_to_le32(sizeof(gpt_header)); |
| 491 | gpt_h->my_lba = cpu_to_le64(1); |
| 492 | gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1); |
| 493 | gpt_h->first_usable_lba = cpu_to_le64(34); |
| 494 | gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34); |
| 495 | gpt_h->partition_entry_lba = cpu_to_le64(2); |
| 496 | gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS); |
| 497 | gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry)); |
| 498 | gpt_h->header_crc32 = 0; |
| 499 | gpt_h->partition_entry_array_crc32 = 0; |
| 500 | |
Przemyslaw Marczak | d718ded | 2014-04-02 10:20:03 +0200 | [diff] [blame] | 501 | if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID)) |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 502 | return -1; |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid, |
| 508 | disk_partition_t *partitions, int parts_count) |
| 509 | { |
| 510 | int ret; |
| 511 | |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 512 | gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header), |
| 513 | dev_desc)); |
| 514 | gpt_entry *gpt_e; |
| 515 | |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 516 | if (gpt_h == NULL) { |
| 517 | printf("%s: calloc failed!\n", __func__); |
| 518 | return -1; |
| 519 | } |
| 520 | |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 521 | gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS |
| 522 | * sizeof(gpt_entry), |
| 523 | dev_desc)); |
Lukasz Majewski | 40684dd | 2012-12-11 11:09:46 +0100 | [diff] [blame] | 524 | if (gpt_e == NULL) { |
| 525 | printf("%s: calloc failed!\n", __func__); |
| 526 | free(gpt_h); |
| 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | /* Generate Primary GPT header (LBA1) */ |
| 531 | ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count); |
| 532 | if (ret) |
| 533 | goto err; |
| 534 | |
| 535 | /* Generate partition entries */ |
| 536 | ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count); |
| 537 | if (ret) |
| 538 | goto err; |
| 539 | |
| 540 | /* Write GPT partition table */ |
| 541 | ret = write_gpt_table(dev_desc, gpt_h, gpt_e); |
| 542 | |
| 543 | err: |
| 544 | free(gpt_e); |
| 545 | free(gpt_h); |
| 546 | return ret; |
| 547 | } |
| 548 | #endif |
| 549 | |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 550 | /* |
| 551 | * Private functions |
| 552 | */ |
| 553 | /* |
| 554 | * pmbr_part_valid(): Check for EFI partition signature |
| 555 | * |
| 556 | * Returns: 1 if EFI GPT partition type is found. |
| 557 | */ |
| 558 | static int pmbr_part_valid(struct partition *part) |
| 559 | { |
| 560 | if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT && |
Marc Dietrich | 8faefad | 2013-03-29 07:57:10 +0000 | [diff] [blame] | 561 | get_unaligned_le32(&part->start_sect) == 1UL) { |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 562 | return 1; |
| 563 | } |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * is_pmbr_valid(): test Protective MBR for validity |
| 570 | * |
| 571 | * Returns: 1 if PMBR is valid, 0 otherwise. |
| 572 | * Validity depends on two things: |
| 573 | * 1) MSDOS signature is in the last two bytes of the MBR |
| 574 | * 2) One partition of type 0xEE is found, checked by pmbr_part_valid() |
| 575 | */ |
| 576 | static int is_pmbr_valid(legacy_mbr * mbr) |
| 577 | { |
| 578 | int i = 0; |
| 579 | |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 580 | if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 581 | return 0; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 582 | |
| 583 | for (i = 0; i < 4; i++) { |
| 584 | if (pmbr_part_valid(&mbr->partition_record[i])) { |
| 585 | return 1; |
| 586 | } |
| 587 | } |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * is_gpt_valid() - tests one GPT header and PTEs for validity |
| 593 | * |
| 594 | * lba is the logical block address of the GPT header to test |
| 595 | * gpt is a GPT header ptr, filled on return. |
| 596 | * ptes is a PTEs ptr, filled on return. |
| 597 | * |
| 598 | * Description: returns 1 if valid, 0 on error. |
| 599 | * If valid, returns pointers to PTEs. |
| 600 | */ |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 601 | static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba, |
| 602 | gpt_header *pgpt_head, gpt_entry **pgpt_pte) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 603 | { |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 604 | if (!dev_desc || !pgpt_head) { |
Doug Anderson | df70b1c | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 605 | printf("%s: Invalid Argument(s)\n", __func__); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* Read GPT Header from device */ |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 610 | if (dev_desc->block_read(dev_desc->dev, (lbaint_t)lba, 1, pgpt_head) |
| 611 | != 1) { |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 612 | printf("*** ERROR: Can't read GPT header ***\n"); |
| 613 | return 0; |
| 614 | } |
| 615 | |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame^] | 616 | if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba)) |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 617 | return 0; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 618 | |
| 619 | /* Read and allocate Partition Table Entries */ |
| 620 | *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head); |
| 621 | if (*pgpt_pte == NULL) { |
| 622 | printf("GPT: Failed to allocate memory for PTE\n"); |
| 623 | return 0; |
| 624 | } |
| 625 | |
Steve Rae | e1f6b0a | 2014-12-18 12:13:42 +0100 | [diff] [blame^] | 626 | if (validate_gpt_entries(pgpt_head, *pgpt_pte)) { |
Doug Anderson | deb5ca8 | 2011-10-19 09:47:31 +0000 | [diff] [blame] | 627 | free(*pgpt_pte); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | /* We're done, all's well */ |
| 632 | return 1; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * alloc_read_gpt_entries(): reads partition entries from disk |
| 637 | * @dev_desc |
| 638 | * @gpt - GPT header |
| 639 | * |
| 640 | * Description: Returns ptes on success, NULL on error. |
| 641 | * Allocates space for PTEs based on information found in @gpt. |
| 642 | * Notes: remember to free pte when you're done! |
| 643 | */ |
| 644 | static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc, |
| 645 | gpt_header * pgpt_head) |
| 646 | { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 647 | size_t count = 0, blk_cnt; |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 648 | gpt_entry *pte = NULL; |
| 649 | |
| 650 | if (!dev_desc || !pgpt_head) { |
Doug Anderson | df70b1c | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 651 | printf("%s: Invalid Argument(s)\n", __func__); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 652 | return NULL; |
| 653 | } |
| 654 | |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 655 | count = le32_to_cpu(pgpt_head->num_partition_entries) * |
| 656 | le32_to_cpu(pgpt_head->sizeof_partition_entry); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 657 | |
Chang Hyun Park | fae2bf2 | 2012-12-11 11:09:45 +0100 | [diff] [blame] | 658 | debug("%s: count = %u * %u = %zu\n", __func__, |
| 659 | (u32) le32_to_cpu(pgpt_head->num_partition_entries), |
| 660 | (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 661 | |
| 662 | /* Allocate memory for PTE, remember to FREE */ |
| 663 | if (count != 0) { |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 664 | pte = memalign(ARCH_DMA_MINALIGN, |
| 665 | PAD_TO_BLOCKSIZE(count, dev_desc)); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | if (count == 0 || pte == NULL) { |
Taylor Hutt | 9936be3 | 2012-10-12 14:26:09 +0000 | [diff] [blame] | 669 | printf("%s: ERROR: Can't allocate 0x%zX " |
| 670 | "bytes for GPT Entries\n", |
Doug Anderson | df70b1c | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 671 | __func__, count); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 672 | return NULL; |
| 673 | } |
| 674 | |
| 675 | /* Read GPT Entries from device */ |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 676 | blk_cnt = BLOCK_CNT(count, dev_desc); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 677 | if (dev_desc->block_read (dev_desc->dev, |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 678 | (lbaint_t)le64_to_cpu(pgpt_head->partition_entry_lba), |
Egbert Eich | ae1768a | 2013-04-09 06:03:36 +0000 | [diff] [blame] | 679 | (lbaint_t) (blk_cnt), pte) |
| 680 | != blk_cnt) { |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 681 | |
| 682 | printf("*** ERROR: Can't read GPT Entries ***\n"); |
| 683 | free(pte); |
| 684 | return NULL; |
| 685 | } |
| 686 | return pte; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * is_pte_valid(): validates a single Partition Table Entry |
| 691 | * @gpt_entry - Pointer to a single Partition Table Entry |
| 692 | * |
| 693 | * Description: returns 1 if valid, 0 on error. |
| 694 | */ |
| 695 | static int is_pte_valid(gpt_entry * pte) |
| 696 | { |
| 697 | efi_guid_t unused_guid; |
| 698 | |
| 699 | if (!pte) { |
Doug Anderson | df70b1c | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 700 | printf("%s: Invalid Argument(s)\n", __func__); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | /* Only one validation for now: |
| 705 | * The GUID Partition Type != Unused Entry (ALL-ZERO) |
| 706 | */ |
| 707 | memset(unused_guid.b, 0, sizeof(unused_guid.b)); |
| 708 | |
| 709 | if (memcmp(pte->partition_type_guid.b, unused_guid.b, |
| 710 | sizeof(unused_guid.b)) == 0) { |
| 711 | |
Doug Anderson | df70b1c | 2011-10-19 08:04:46 +0000 | [diff] [blame] | 712 | debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__, |
Taylor Hutt | 9936be3 | 2012-10-12 14:26:09 +0000 | [diff] [blame] | 713 | (unsigned int)(uintptr_t)pte); |
richardretanubun | 07f3d78 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 714 | |
| 715 | return 0; |
| 716 | } else { |
| 717 | return 1; |
| 718 | } |
| 719 | } |
| 720 | #endif |