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