part_efi.c 19 KB

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