part_efi.c 19 KB

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