part_efi.c 17 KB

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