part_efi.c 18 KB

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