part_efi.c 19 KB

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