part_efi.c 19 KB

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