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