]>
Commit | Line | Data |
---|---|---|
07f3d789 | 1 | /* |
2 | * Copyright (C) 2008 RuggedCom, Inc. | |
3 | * Richard Retanubun <[email protected]> | |
4 | * | |
5 | * See file CREDITS for list of people who contributed to this | |
6 | * project. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License as | |
10 | * published by the Free Software Foundation; either version 2 of | |
11 | * the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
21 | * MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | /* | |
6d0f6bcf | 25 | * Problems with CONFIG_SYS_64BIT_LBA: |
07f3d789 | 26 | * |
27 | * struct disk_partition.start in include/part.h is sized as ulong. | |
6d0f6bcf | 28 | * When CONFIG_SYS_64BIT_LBA is activated, lbaint_t changes from ulong to uint64_t. |
07f3d789 | 29 | * For now, it is cast back to ulong at assignment. |
30 | * | |
31 | * This limits the maximum size of addressable storage to < 2 Terra Bytes | |
32 | */ | |
33 | #include <common.h> | |
34 | #include <command.h> | |
35 | #include <ide.h> | |
36 | #include <malloc.h> | |
37 | #include "part_efi.h" | |
38 | ||
39 | #if (defined(CONFIG_CMD_IDE) || \ | |
40 | defined(CONFIG_CMD_SATA) || \ | |
41 | defined(CONFIG_CMD_SCSI) || \ | |
42 | defined(CONFIG_CMD_USB) || \ | |
43 | defined(CONFIG_MMC) || \ | |
44 | defined(CONFIG_SYSTEMACE) ) && defined(CONFIG_EFI_PARTITION) | |
45 | ||
46 | /* Convert char[2] in little endian format to the host format integer | |
47 | */ | |
48 | static inline unsigned short le16_to_int(unsigned char *le16) | |
49 | { | |
50 | return ((le16[1] << 8) + le16[0]); | |
51 | } | |
52 | ||
53 | /* Convert char[4] in little endian format to the host format integer | |
54 | */ | |
55 | static inline unsigned long le32_to_int(unsigned char *le32) | |
56 | { | |
57 | return ((le32[3] << 24) + (le32[2] << 16) + (le32[1] << 8) + le32[0]); | |
58 | } | |
59 | ||
60 | /* Convert char[8] in little endian format to the host format integer | |
61 | */ | |
62 | static inline unsigned long long le64_to_int(unsigned char *le64) | |
63 | { | |
64 | return (((unsigned long long)le64[7] << 56) + | |
65 | ((unsigned long long)le64[6] << 48) + | |
66 | ((unsigned long long)le64[5] << 40) + | |
67 | ((unsigned long long)le64[4] << 32) + | |
68 | ((unsigned long long)le64[3] << 24) + | |
69 | ((unsigned long long)le64[2] << 16) + | |
70 | ((unsigned long long)le64[1] << 8) + | |
71 | (unsigned long long)le64[0]); | |
72 | } | |
73 | ||
74 | /** | |
75 | * efi_crc32() - EFI version of crc32 function | |
76 | * @buf: buffer to calculate crc32 of | |
77 | * @len - length of buf | |
78 | * | |
79 | * Description: Returns EFI-style CRC32 value for @buf | |
80 | */ | |
81 | static inline unsigned long efi_crc32(const void *buf, unsigned long len) | |
82 | { | |
83 | return crc32(0, buf, len); | |
84 | } | |
85 | ||
86 | /* | |
87 | * Private function prototypes | |
88 | */ | |
89 | ||
90 | static int pmbr_part_valid(struct partition *part); | |
91 | static int is_pmbr_valid(legacy_mbr * mbr); | |
92 | ||
93 | static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba, | |
94 | gpt_header * pgpt_head, gpt_entry ** pgpt_pte); | |
95 | ||
96 | static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc, | |
97 | gpt_header * pgpt_head); | |
98 | ||
99 | static int is_pte_valid(gpt_entry * pte); | |
100 | ||
101 | /* | |
102 | * Public Functions (include/part.h) | |
103 | */ | |
104 | ||
105 | void print_part_efi(block_dev_desc_t * dev_desc) | |
106 | { | |
107 | gpt_header gpt_head; | |
108 | gpt_entry **pgpt_pte = NULL; | |
109 | int i = 0; | |
110 | ||
111 | if (!dev_desc) { | |
112 | printf("%s: Invalid Argument(s)\n", __FUNCTION__); | |
113 | return; | |
114 | } | |
115 | /* This function validates AND fills in the GPT header and PTE */ | |
116 | if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, | |
117 | &(gpt_head), pgpt_pte) != 1) { | |
118 | printf("%s: *** ERROR: Invalid GPT ***\n", __FUNCTION__); | |
119 | return; | |
120 | } | |
121 | ||
122 | debug("%s: gpt-entry at 0x%08X\n", __FUNCTION__, (unsigned int)*pgpt_pte); | |
123 | ||
124 | printf("Part Start LBA End LBA\n"); | |
125 | for (i = 0; i < le32_to_int(gpt_head.num_partition_entries); i++) { | |
126 | ||
127 | if (is_pte_valid(&(*pgpt_pte)[i])) { | |
128 | printf("%s%d 0x%llX 0x%llX\n", GPT_ENTRY_NAME, | |
129 | (i + 1), | |
130 | le64_to_int((*pgpt_pte)[i].starting_lba), | |
131 | le64_to_int((*pgpt_pte)[i].ending_lba)); | |
132 | } else { | |
133 | break; /* Stop at the first non valid PTE */ | |
134 | } | |
135 | } | |
136 | ||
137 | /* Remember to free pte */ | |
138 | if (*pgpt_pte != NULL) { | |
139 | debug("%s: Freeing pgpt_pte\n", __FUNCTION__); | |
140 | free(*pgpt_pte); | |
141 | } | |
142 | return; | |
143 | } | |
144 | ||
145 | int get_partition_info_efi(block_dev_desc_t * dev_desc, int part, | |
146 | disk_partition_t * info) | |
147 | { | |
148 | gpt_header gpt_head; | |
149 | gpt_entry **pgpt_pte = NULL; | |
150 | ||
151 | /* "part" argument must be at least 1 */ | |
152 | if (!dev_desc || !info || part < 1) { | |
153 | printf("%s: Invalid Argument(s)\n", __FUNCTION__); | |
154 | return -1; | |
155 | } | |
156 | ||
157 | /* This function validates AND fills in the GPT header and PTE */ | |
158 | if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, | |
159 | &(gpt_head), pgpt_pte) != 1) { | |
160 | printf("%s: *** ERROR: Invalid GPT ***\n", __FUNCTION__); | |
161 | return -1; | |
162 | } | |
163 | ||
164 | /* The ulong casting limits the maximum disk size to 2 TB */ | |
165 | info->start = (ulong) le64_to_int((*pgpt_pte)[part - 1].starting_lba); | |
166 | info->size = (ulong) le64_to_int((*pgpt_pte)[part - 1].ending_lba) - info->start; | |
167 | info->blksz = GPT_BLOCK_SIZE; | |
168 | ||
169 | sprintf((char *)info->name, "%s%d\n", GPT_ENTRY_NAME, part); | |
170 | sprintf((char *)info->type, "U-Boot"); | |
171 | ||
172 | debug("%s: start 0x%lX, size 0x%lX, name %s", __FUNCTION__, | |
173 | info->start, info->size, info->name); | |
174 | ||
175 | /* Remember to free pte */ | |
176 | if (*pgpt_pte != NULL) { | |
177 | debug("%s: Freeing pgpt_pte\n", __FUNCTION__); | |
178 | free(*pgpt_pte); | |
179 | } | |
180 | return 0; | |
181 | } | |
182 | ||
183 | int test_part_efi(block_dev_desc_t * dev_desc) | |
184 | { | |
185 | legacy_mbr legacymbr; | |
186 | ||
187 | /* Read legacy MBR from block 0 and validate it */ | |
188 | if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) & legacymbr) != 1) | |
189 | || (is_pmbr_valid(&legacymbr) != 1)) { | |
190 | return -1; | |
191 | } | |
192 | return 0; | |
193 | } | |
194 | ||
195 | /* | |
196 | * Private functions | |
197 | */ | |
198 | /* | |
199 | * pmbr_part_valid(): Check for EFI partition signature | |
200 | * | |
201 | * Returns: 1 if EFI GPT partition type is found. | |
202 | */ | |
203 | static int pmbr_part_valid(struct partition *part) | |
204 | { | |
205 | if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT && | |
206 | le32_to_int(part->start_sect) == 1UL) { | |
207 | return 1; | |
208 | } | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
213 | /* | |
214 | * is_pmbr_valid(): test Protective MBR for validity | |
215 | * | |
216 | * Returns: 1 if PMBR is valid, 0 otherwise. | |
217 | * Validity depends on two things: | |
218 | * 1) MSDOS signature is in the last two bytes of the MBR | |
219 | * 2) One partition of type 0xEE is found, checked by pmbr_part_valid() | |
220 | */ | |
221 | static int is_pmbr_valid(legacy_mbr * mbr) | |
222 | { | |
223 | int i = 0; | |
224 | ||
225 | if (!mbr || le16_to_int(mbr->signature) != MSDOS_MBR_SIGNATURE) { | |
226 | return 0; | |
227 | } | |
228 | ||
229 | for (i = 0; i < 4; i++) { | |
230 | if (pmbr_part_valid(&mbr->partition_record[i])) { | |
231 | return 1; | |
232 | } | |
233 | } | |
234 | return 0; | |
235 | } | |
236 | ||
237 | /** | |
238 | * is_gpt_valid() - tests one GPT header and PTEs for validity | |
239 | * | |
240 | * lba is the logical block address of the GPT header to test | |
241 | * gpt is a GPT header ptr, filled on return. | |
242 | * ptes is a PTEs ptr, filled on return. | |
243 | * | |
244 | * Description: returns 1 if valid, 0 on error. | |
245 | * If valid, returns pointers to PTEs. | |
246 | */ | |
247 | static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba, | |
248 | gpt_header * pgpt_head, gpt_entry ** pgpt_pte) | |
249 | { | |
250 | unsigned char crc32_backup[4] = { 0 }; | |
251 | unsigned long calc_crc32; | |
252 | unsigned long long lastlba; | |
253 | ||
254 | if (!dev_desc || !pgpt_head) { | |
255 | printf("%s: Invalid Argument(s)\n", __FUNCTION__); | |
256 | return 0; | |
257 | } | |
258 | ||
259 | /* Read GPT Header from device */ | |
260 | if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) { | |
261 | printf("*** ERROR: Can't read GPT header ***\n"); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | /* Check the GPT header signature */ | |
266 | if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) { | |
267 | printf("GUID Partition Table Header signature is wrong:" | |
268 | "0x%llX != 0x%llX\n", | |
269 | (unsigned long long)le64_to_int(pgpt_head->signature), | |
270 | (unsigned long long)GPT_HEADER_SIGNATURE); | |
271 | return 0; | |
272 | } | |
273 | ||
274 | /* Check the GUID Partition Table CRC */ | |
275 | memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup)); | |
276 | memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32)); | |
277 | ||
278 | calc_crc32 = efi_crc32((const unsigned char *)pgpt_head, | |
279 | le32_to_int(pgpt_head->header_size)); | |
280 | ||
281 | memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup)); | |
282 | ||
283 | if (calc_crc32 != le32_to_int(crc32_backup)) { | |
284 | printf("GUID Partition Table Header CRC is wrong:" | |
285 | "0x%08lX != 0x%08lX\n", | |
286 | le32_to_int(crc32_backup), calc_crc32); | |
287 | return 0; | |
288 | } | |
289 | ||
290 | /* Check that the my_lba entry points to the LBA that contains the GPT */ | |
291 | if (le64_to_int(pgpt_head->my_lba) != lba) { | |
292 | printf("GPT: my_lba incorrect: %llX != %llX\n", | |
293 | (unsigned long long)le64_to_int(pgpt_head->my_lba), | |
294 | (unsigned long long)lba); | |
295 | return 0; | |
296 | } | |
297 | ||
298 | /* Check the first_usable_lba and last_usable_lba are within the disk. */ | |
299 | lastlba = (unsigned long long)dev_desc->lba; | |
300 | if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) { | |
301 | printf("GPT: first_usable_lba incorrect: %llX > %llX\n", | |
302 | le64_to_int(pgpt_head->first_usable_lba), lastlba); | |
303 | return 0; | |
304 | } | |
305 | if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) { | |
306 | printf("GPT: last_usable_lba incorrect: %llX > %llX\n", | |
307 | le64_to_int(pgpt_head->last_usable_lba), lastlba); | |
308 | return 0; | |
309 | } | |
310 | ||
311 | debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n", | |
312 | le64_to_int(pgpt_head->first_usable_lba), | |
313 | le64_to_int(pgpt_head->last_usable_lba), lastlba); | |
314 | ||
315 | /* Read and allocate Partition Table Entries */ | |
316 | *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head); | |
317 | if (*pgpt_pte == NULL) { | |
318 | printf("GPT: Failed to allocate memory for PTE\n"); | |
319 | return 0; | |
320 | } | |
321 | ||
322 | /* Check the GUID Partition Table Entry Array CRC */ | |
323 | calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte, | |
324 | le32_to_int(pgpt_head->num_partition_entries) * | |
325 | le32_to_int(pgpt_head->sizeof_partition_entry)); | |
326 | ||
327 | if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) { | |
328 | printf("GUID Partition Table Entry Array CRC is wrong:" | |
329 | "0x%08lX != 0x%08lX\n", | |
330 | le32_to_int(pgpt_head->partition_entry_array_crc32), | |
331 | calc_crc32); | |
332 | ||
333 | if (*pgpt_pte != NULL) { | |
334 | free(*pgpt_pte); | |
335 | } | |
336 | return 0; | |
337 | } | |
338 | ||
339 | /* We're done, all's well */ | |
340 | return 1; | |
341 | } | |
342 | ||
343 | /** | |
344 | * alloc_read_gpt_entries(): reads partition entries from disk | |
345 | * @dev_desc | |
346 | * @gpt - GPT header | |
347 | * | |
348 | * Description: Returns ptes on success, NULL on error. | |
349 | * Allocates space for PTEs based on information found in @gpt. | |
350 | * Notes: remember to free pte when you're done! | |
351 | */ | |
352 | static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc, | |
353 | gpt_header * pgpt_head) | |
354 | { | |
355 | size_t count = 0; | |
356 | gpt_entry *pte = NULL; | |
357 | ||
358 | if (!dev_desc || !pgpt_head) { | |
359 | printf("%s: Invalid Argument(s)\n", __FUNCTION__); | |
360 | return NULL; | |
361 | } | |
362 | ||
363 | count = le32_to_int(pgpt_head->num_partition_entries) * | |
364 | le32_to_int(pgpt_head->sizeof_partition_entry); | |
365 | ||
366 | debug("%s: count = %lu * %lu = %u\n", __FUNCTION__, | |
367 | le32_to_int(pgpt_head->num_partition_entries), | |
368 | le32_to_int(pgpt_head->sizeof_partition_entry), count); | |
369 | ||
370 | /* Allocate memory for PTE, remember to FREE */ | |
371 | if (count != 0) { | |
372 | pte = malloc(count); | |
373 | } | |
374 | ||
375 | if (count == 0 || pte == NULL) { | |
376 | printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n", | |
377 | __FUNCTION__, count); | |
378 | return NULL; | |
379 | } | |
380 | ||
381 | /* Read GPT Entries from device */ | |
382 | if (dev_desc->block_read (dev_desc->dev, | |
383 | (unsigned long)le64_to_int(pgpt_head->partition_entry_lba), | |
384 | (lbaint_t) (count / GPT_BLOCK_SIZE), pte) | |
385 | != (count / GPT_BLOCK_SIZE)) { | |
386 | ||
387 | printf("*** ERROR: Can't read GPT Entries ***\n"); | |
388 | free(pte); | |
389 | return NULL; | |
390 | } | |
391 | return pte; | |
392 | } | |
393 | ||
394 | /** | |
395 | * is_pte_valid(): validates a single Partition Table Entry | |
396 | * @gpt_entry - Pointer to a single Partition Table Entry | |
397 | * | |
398 | * Description: returns 1 if valid, 0 on error. | |
399 | */ | |
400 | static int is_pte_valid(gpt_entry * pte) | |
401 | { | |
402 | efi_guid_t unused_guid; | |
403 | ||
404 | if (!pte) { | |
405 | printf("%s: Invalid Argument(s)\n", __FUNCTION__); | |
406 | return 0; | |
407 | } | |
408 | ||
409 | /* Only one validation for now: | |
410 | * The GUID Partition Type != Unused Entry (ALL-ZERO) | |
411 | */ | |
412 | memset(unused_guid.b, 0, sizeof(unused_guid.b)); | |
413 | ||
414 | if (memcmp(pte->partition_type_guid.b, unused_guid.b, | |
415 | sizeof(unused_guid.b)) == 0) { | |
416 | ||
417 | debug("%s: Found an unused PTE GUID at 0x%08X\n", __FUNCTION__, | |
418 | (unsigned int)pte); | |
419 | ||
420 | return 0; | |
421 | } else { | |
422 | return 1; | |
423 | } | |
424 | } | |
425 | #endif |