Commit 6d2ee5a33a802e6c59ba3148b3a111e4c41d43cb

Authored by Mark Langsdorf
Committed by Tom Rini
1 parent 8094972d59

part_efi: make sure the gpt_pte is freed

the gpt_pte wasn't being freed if it was checked against an invalid
partition. The resulting memory leakage could make it impossible
to repeatedly attempt to load non-existent files in a script.

Also, downgrade the message for not finding an invalid partition
from a printf() to a debug() so as to minimize message spam in
perfectly normal situations.

Signed-off-by: Mark Langsdorf <mark.langsdorf@calxeda.com>

Showing 1 changed file with 2 additions and 1 deletions Inline Diff

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