part_efi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 <common.h>
  33. #include <command.h>
  34. #include <ide.h>
  35. #include <malloc.h>
  36. #include <part_efi.h>
  37. #include <linux/ctype.h>
  38. DECLARE_GLOBAL_DATA_PTR;
  39. #if defined(CONFIG_CMD_IDE) || \
  40. defined(CONFIG_CMD_SATA) || \
  41. defined(CONFIG_CMD_SCSI) || \
  42. defined(CONFIG_CMD_USB) || \
  43. defined(CONFIG_MMC) || \
  44. defined(CONFIG_SYSTEMACE)
  45. /**
  46. * efi_crc32() - EFI version of crc32 function
  47. * @buf: buffer to calculate crc32 of
  48. * @len - length of buf
  49. *
  50. * Description: Returns EFI-style CRC32 value for @buf
  51. */
  52. static inline u32 efi_crc32(const void *buf, u32 len)
  53. {
  54. return crc32(0, buf, len);
  55. }
  56. /*
  57. * Private function prototypes
  58. */
  59. static int pmbr_part_valid(struct partition *part);
  60. static int is_pmbr_valid(legacy_mbr * mbr);
  61. static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
  62. gpt_header * pgpt_head, gpt_entry ** pgpt_pte);
  63. static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
  64. gpt_header * pgpt_head);
  65. static int is_pte_valid(gpt_entry * pte);
  66. static char *print_efiname(gpt_entry *pte)
  67. {
  68. static char name[PARTNAME_SZ + 1];
  69. int i;
  70. for (i = 0; i < PARTNAME_SZ; i++) {
  71. u8 c;
  72. c = pte->partition_name[i] & 0xff;
  73. c = (c && !isprint(c)) ? '.' : c;
  74. name[i] = c;
  75. }
  76. name[PARTNAME_SZ] = 0;
  77. return name;
  78. }
  79. static void uuid_string(unsigned char *uuid, char *str)
  80. {
  81. static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
  82. 12, 13, 14, 15};
  83. int i;
  84. for (i = 0; i < 16; i++) {
  85. sprintf(str, "%02x", uuid[le[i]]);
  86. str += 2;
  87. switch (i) {
  88. case 3:
  89. case 5:
  90. case 7:
  91. case 9:
  92. *str++ = '-';
  93. break;
  94. }
  95. }
  96. }
  97. static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
  98. static inline int is_bootable(gpt_entry *p)
  99. {
  100. return p->attributes.fields.legacy_bios_bootable ||
  101. !memcmp(&(p->partition_type_guid), &system_guid,
  102. sizeof(efi_guid_t));
  103. }
  104. #ifdef CONFIG_EFI_PARTITION
  105. /*
  106. * Public Functions (include/part.h)
  107. */
  108. void print_part_efi(block_dev_desc_t * dev_desc)
  109. {
  110. ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
  111. gpt_entry *gpt_pte = NULL;
  112. int i = 0;
  113. char uuid[37];
  114. if (!dev_desc) {
  115. printf("%s: Invalid Argument(s)\n", __func__);
  116. return;
  117. }
  118. /* This function validates AND fills in the GPT header and PTE */
  119. if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
  120. gpt_head, &gpt_pte) != 1) {
  121. printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
  122. return;
  123. }
  124. debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
  125. printf("Part\tStart LBA\tEnd LBA\t\tName\n");
  126. printf("\tAttributes\n");
  127. printf("\tType UUID\n");
  128. printf("\tPartition UUID\n");
  129. for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
  130. /* Stop at the first non valid PTE */
  131. if (!is_pte_valid(&gpt_pte[i]))
  132. break;
  133. printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
  134. le64_to_cpu(gpt_pte[i].starting_lba),
  135. le64_to_cpu(gpt_pte[i].ending_lba),
  136. print_efiname(&gpt_pte[i]));
  137. printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
  138. uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
  139. printf("\ttype:\t%s\n", uuid);
  140. uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
  141. printf("\tuuid:\t%s\n", uuid);
  142. }
  143. /* Remember to free pte */
  144. free(gpt_pte);
  145. return;
  146. }
  147. int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
  148. disk_partition_t * info)
  149. {
  150. ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
  151. gpt_entry *gpt_pte = NULL;
  152. /* "part" argument must be at least 1 */
  153. if (!dev_desc || !info || part < 1) {
  154. printf("%s: Invalid Argument(s)\n", __func__);
  155. return -1;
  156. }
  157. /* This function validates AND fills in the GPT header and PTE */
  158. if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
  159. gpt_head, &gpt_pte) != 1) {
  160. printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
  161. return -1;
  162. }
  163. if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
  164. !is_pte_valid(&gpt_pte[part - 1])) {
  165. printf("%s: *** ERROR: Invalid partition number %d ***\n",
  166. __func__, part);
  167. return -1;
  168. }
  169. /* The ulong casting limits the maximum disk size to 2 TB */
  170. info->start = (u64)le64_to_cpu(gpt_pte[part - 1].starting_lba);
  171. /* The ending LBA is inclusive, to calculate size, add 1 to it */
  172. info->size = ((u64)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1)
  173. - info->start;
  174. info->blksz = GPT_BLOCK_SIZE;
  175. sprintf((char *)info->name, "%s",
  176. print_efiname(&gpt_pte[part - 1]));
  177. sprintf((char *)info->type, "U-Boot");
  178. info->bootable = is_bootable(&gpt_pte[part - 1]);
  179. #ifdef CONFIG_PARTITION_UUIDS
  180. uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
  181. #endif
  182. debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
  183. info->start, info->size, info->name);
  184. /* Remember to free pte */
  185. free(gpt_pte);
  186. return 0;
  187. }
  188. int test_part_efi(block_dev_desc_t * dev_desc)
  189. {
  190. ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
  191. /* Read legacy MBR from block 0 and validate it */
  192. if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
  193. || (is_pmbr_valid(legacymbr) != 1)) {
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. /**
  199. * set_protective_mbr(): Set the EFI protective MBR
  200. * @param dev_desc - block device descriptor
  201. *
  202. * @return - zero on success, otherwise error
  203. */
  204. static int set_protective_mbr(block_dev_desc_t *dev_desc)
  205. {
  206. legacy_mbr *p_mbr;
  207. /* Setup the Protective MBR */
  208. p_mbr = calloc(1, sizeof(p_mbr));
  209. if (p_mbr == NULL) {
  210. printf("%s: calloc failed!\n", __func__);
  211. return -1;
  212. }
  213. /* Append signature */
  214. p_mbr->signature = MSDOS_MBR_SIGNATURE;
  215. p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
  216. p_mbr->partition_record[0].start_sect = 1;
  217. p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba;
  218. /* Write MBR sector to the MMC device */
  219. if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
  220. printf("** Can't write to device %d **\n",
  221. dev_desc->dev);
  222. free(p_mbr);
  223. return -1;
  224. }
  225. free(p_mbr);
  226. return 0;
  227. }
  228. /**
  229. * string_uuid(); Convert UUID stored as string to bytes
  230. *
  231. * @param uuid - UUID represented as string
  232. * @param dst - GUID buffer
  233. *
  234. * @return return 0 on successful conversion
  235. */
  236. static int string_uuid(char *uuid, u8 *dst)
  237. {
  238. efi_guid_t guid;
  239. u16 b, c, d;
  240. u64 e;
  241. u32 a;
  242. u8 *p;
  243. u8 i;
  244. const u8 uuid_str_len = 36;
  245. /* The UUID is written in text: */
  246. /* 1 9 14 19 24 */
  247. /* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */
  248. debug("%s: uuid: %s\n", __func__, uuid);
  249. if (strlen(uuid) != uuid_str_len)
  250. return -1;
  251. for (i = 0; i < uuid_str_len; i++) {
  252. if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
  253. if (uuid[i] != '-')
  254. return -1;
  255. } else {
  256. if (!isxdigit(uuid[i]))
  257. return -1;
  258. }
  259. }
  260. a = (u32)simple_strtoul(uuid, NULL, 16);
  261. b = (u16)simple_strtoul(uuid + 9, NULL, 16);
  262. c = (u16)simple_strtoul(uuid + 14, NULL, 16);
  263. d = (u16)simple_strtoul(uuid + 19, NULL, 16);
  264. e = (u64)simple_strtoull(uuid + 24, NULL, 16);
  265. p = (u8 *) &e;
  266. guid = EFI_GUID(a, b, c, d >> 8, d & 0xFF,
  267. *(p + 5), *(p + 4), *(p + 3),
  268. *(p + 2), *(p + 1) , *p);
  269. memcpy(dst, guid.b, sizeof(efi_guid_t));
  270. return 0;
  271. }
  272. int write_gpt_table(block_dev_desc_t *dev_desc,
  273. gpt_header *gpt_h, gpt_entry *gpt_e)
  274. {
  275. const int pte_blk_num = (gpt_h->num_partition_entries
  276. * sizeof(gpt_entry)) / dev_desc->blksz;
  277. u32 calc_crc32;
  278. u64 val;
  279. debug("max lba: %x\n", (u32) dev_desc->lba);
  280. /* Setup the Protective MBR */
  281. if (set_protective_mbr(dev_desc) < 0)
  282. goto err;
  283. /* Generate CRC for the Primary GPT Header */
  284. calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
  285. le32_to_cpu(gpt_h->num_partition_entries) *
  286. le32_to_cpu(gpt_h->sizeof_partition_entry));
  287. gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
  288. calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
  289. le32_to_cpu(gpt_h->header_size));
  290. gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
  291. /* Write the First GPT to the block right after the Legacy MBR */
  292. if (dev_desc->block_write(dev_desc->dev, 1, 1, gpt_h) != 1)
  293. goto err;
  294. if (dev_desc->block_write(dev_desc->dev, 2, pte_blk_num, gpt_e)
  295. != pte_blk_num)
  296. goto err;
  297. /* recalculate the values for the Second GPT Header */
  298. val = le64_to_cpu(gpt_h->my_lba);
  299. gpt_h->my_lba = gpt_h->alternate_lba;
  300. gpt_h->alternate_lba = cpu_to_le64(val);
  301. gpt_h->header_crc32 = 0;
  302. calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
  303. le32_to_cpu(gpt_h->header_size));
  304. gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
  305. if (dev_desc->block_write(dev_desc->dev,
  306. le32_to_cpu(gpt_h->last_usable_lba + 1),
  307. pte_blk_num, gpt_e) != pte_blk_num)
  308. goto err;
  309. if (dev_desc->block_write(dev_desc->dev,
  310. le32_to_cpu(gpt_h->my_lba), 1, gpt_h) != 1)
  311. goto err;
  312. debug("GPT successfully written to block device!\n");
  313. return 0;
  314. err:
  315. printf("** Can't write to device %d **\n", dev_desc->dev);
  316. return -1;
  317. }
  318. int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
  319. disk_partition_t *partitions, int parts)
  320. {
  321. u32 offset = (u32)le32_to_cpu(gpt_h->first_usable_lba);
  322. ulong start;
  323. int i, k;
  324. size_t name_len;
  325. #ifdef CONFIG_PARTITION_UUIDS
  326. char *str_uuid;
  327. #endif
  328. for (i = 0; i < parts; i++) {
  329. /* partition starting lba */
  330. start = partitions[i].start;
  331. if (start && (start < offset)) {
  332. printf("Partition overlap\n");
  333. return -1;
  334. }
  335. if (start) {
  336. gpt_e[i].starting_lba = cpu_to_le64(start);
  337. offset = start + partitions[i].size;
  338. } else {
  339. gpt_e[i].starting_lba = cpu_to_le64(offset);
  340. offset += partitions[i].size;
  341. }
  342. if (offset >= gpt_h->last_usable_lba) {
  343. printf("Partitions layout exceds disk size\n");
  344. return -1;
  345. }
  346. /* partition ending lba */
  347. if ((i == parts - 1) && (partitions[i].size == 0))
  348. /* extend the last partition to maximuim */
  349. gpt_e[i].ending_lba = gpt_h->last_usable_lba;
  350. else
  351. gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
  352. /* partition type GUID */
  353. memcpy(gpt_e[i].partition_type_guid.b,
  354. &PARTITION_BASIC_DATA_GUID, 16);
  355. #ifdef CONFIG_PARTITION_UUIDS
  356. str_uuid = partitions[i].uuid;
  357. if (string_uuid(str_uuid, gpt_e[i].unique_partition_guid.b)) {
  358. printf("Partition no. %d: invalid guid: %s\n",
  359. i, str_uuid);
  360. return -1;
  361. }
  362. #endif
  363. /* partition attributes */
  364. memset(&gpt_e[i].attributes, 0,
  365. sizeof(gpt_entry_attributes));
  366. /* partition name */
  367. name_len = sizeof(gpt_e[i].partition_name)
  368. / sizeof(efi_char16_t);
  369. for (k = 0; k < name_len; k++)
  370. gpt_e[i].partition_name[k] =
  371. (efi_char16_t)(partitions[i].name[k]);
  372. debug("%s: name: %s offset[%d]: 0x%x size[%d]: 0x%lx\n",
  373. __func__, partitions[i].name, i,
  374. offset, i, partitions[i].size);
  375. }
  376. return 0;
  377. }
  378. int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
  379. char *str_guid, int parts_count)
  380. {
  381. gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
  382. gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
  383. gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
  384. gpt_h->my_lba = cpu_to_le64(1);
  385. gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
  386. gpt_h->first_usable_lba = cpu_to_le64(34);
  387. gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
  388. gpt_h->partition_entry_lba = cpu_to_le64(2);
  389. gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
  390. gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
  391. gpt_h->header_crc32 = 0;
  392. gpt_h->partition_entry_array_crc32 = 0;
  393. if (string_uuid(str_guid, gpt_h->disk_guid.b))
  394. return -1;
  395. return 0;
  396. }
  397. int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
  398. disk_partition_t *partitions, int parts_count)
  399. {
  400. int ret;
  401. gpt_header *gpt_h = calloc(1, sizeof(gpt_header));
  402. if (gpt_h == NULL) {
  403. printf("%s: calloc failed!\n", __func__);
  404. return -1;
  405. }
  406. gpt_entry *gpt_e = calloc(GPT_ENTRY_NUMBERS, sizeof(gpt_entry));
  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. le32_to_cpu(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;
  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, count);
  578. }
  579. if (count == 0 || pte == NULL) {
  580. printf("%s: ERROR: Can't allocate 0x%zX "
  581. "bytes for GPT Entries\n",
  582. __func__, count);
  583. return NULL;
  584. }
  585. /* Read GPT Entries from device */
  586. if (dev_desc->block_read (dev_desc->dev,
  587. le64_to_cpu(pgpt_head->partition_entry_lba),
  588. (lbaint_t) (count / GPT_BLOCK_SIZE), pte)
  589. != (count / GPT_BLOCK_SIZE)) {
  590. printf("*** ERROR: Can't read GPT Entries ***\n");
  591. free(pte);
  592. return NULL;
  593. }
  594. return pte;
  595. }
  596. /**
  597. * is_pte_valid(): validates a single Partition Table Entry
  598. * @gpt_entry - Pointer to a single Partition Table Entry
  599. *
  600. * Description: returns 1 if valid, 0 on error.
  601. */
  602. static int is_pte_valid(gpt_entry * pte)
  603. {
  604. efi_guid_t unused_guid;
  605. if (!pte) {
  606. printf("%s: Invalid Argument(s)\n", __func__);
  607. return 0;
  608. }
  609. /* Only one validation for now:
  610. * The GUID Partition Type != Unused Entry (ALL-ZERO)
  611. */
  612. memset(unused_guid.b, 0, sizeof(unused_guid.b));
  613. if (memcmp(pte->partition_type_guid.b, unused_guid.b,
  614. sizeof(unused_guid.b)) == 0) {
  615. debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
  616. (unsigned int)(uintptr_t)pte);
  617. return 0;
  618. } else {
  619. return 1;
  620. }
  621. }
  622. #endif