part_efi.c 19 KB

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