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