part_efi.c 17 KB

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