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