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