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