part_efi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * Copyright (C) 2008 RuggedCom, Inc.
  3. * Richard Retanubun <RichardRetanubun@RuggedCom.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * NOTE:
  9. * when CONFIG_SYS_64BIT_LBA is not defined, lbaint_t is 32 bits; this
  10. * limits the maximum size of addressable storage to < 2 Terra Bytes
  11. */
  12. #include <asm/unaligned.h>
  13. #include <common.h>
  14. #include <command.h>
  15. #include <ide.h>
  16. #include <inttypes.h>
  17. #include <malloc.h>
  18. #include <memalign.h>
  19. #include <part_efi.h>
  20. #include <linux/ctype.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #ifdef HAVE_BLOCK_DEVICE
  23. /**
  24. * efi_crc32() - EFI version of crc32 function
  25. * @buf: buffer to calculate crc32 of
  26. * @len - length of buf
  27. *
  28. * Description: Returns EFI-style CRC32 value for @buf
  29. */
  30. static inline u32 efi_crc32(const void *buf, u32 len)
  31. {
  32. return crc32(0, buf, len);
  33. }
  34. /*
  35. * Private function prototypes
  36. */
  37. static int pmbr_part_valid(struct partition *part);
  38. static int is_pmbr_valid(legacy_mbr * mbr);
  39. static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
  40. gpt_header *pgpt_head, gpt_entry **pgpt_pte);
  41. static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
  42. gpt_header *pgpt_head);
  43. static int is_pte_valid(gpt_entry * pte);
  44. static char *print_efiname(gpt_entry *pte)
  45. {
  46. static char name[PARTNAME_SZ + 1];
  47. int i;
  48. for (i = 0; i < PARTNAME_SZ; i++) {
  49. u8 c;
  50. c = pte->partition_name[i] & 0xff;
  51. c = (c && !isprint(c)) ? '.' : c;
  52. name[i] = c;
  53. }
  54. name[PARTNAME_SZ] = 0;
  55. return name;
  56. }
  57. static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
  58. static inline int is_bootable(gpt_entry *p)
  59. {
  60. return p->attributes.fields.legacy_bios_bootable ||
  61. !memcmp(&(p->partition_type_guid), &system_guid,
  62. sizeof(efi_guid_t));
  63. }
  64. static int validate_gpt_header(gpt_header *gpt_h, lbaint_t lba,
  65. lbaint_t lastlba)
  66. {
  67. uint32_t crc32_backup = 0;
  68. uint32_t calc_crc32;
  69. /* Check the GPT header signature */
  70. if (le64_to_cpu(gpt_h->signature) != GPT_HEADER_SIGNATURE) {
  71. printf("%s signature is wrong: 0x%llX != 0x%llX\n",
  72. "GUID Partition Table Header",
  73. le64_to_cpu(gpt_h->signature),
  74. GPT_HEADER_SIGNATURE);
  75. return -1;
  76. }
  77. /* Check the GUID Partition Table CRC */
  78. memcpy(&crc32_backup, &gpt_h->header_crc32, sizeof(crc32_backup));
  79. memset(&gpt_h->header_crc32, 0, sizeof(gpt_h->header_crc32));
  80. calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
  81. le32_to_cpu(gpt_h->header_size));
  82. memcpy(&gpt_h->header_crc32, &crc32_backup, sizeof(crc32_backup));
  83. if (calc_crc32 != le32_to_cpu(crc32_backup)) {
  84. printf("%s CRC is wrong: 0x%x != 0x%x\n",
  85. "GUID Partition Table Header",
  86. le32_to_cpu(crc32_backup), calc_crc32);
  87. return -1;
  88. }
  89. /*
  90. * Check that the my_lba entry points to the LBA that contains the GPT
  91. */
  92. if (le64_to_cpu(gpt_h->my_lba) != lba) {
  93. printf("GPT: my_lba incorrect: %llX != " LBAF "\n",
  94. le64_to_cpu(gpt_h->my_lba),
  95. lba);
  96. return -1;
  97. }
  98. /*
  99. * Check that the first_usable_lba and that the last_usable_lba are
  100. * within the disk.
  101. */
  102. if (le64_to_cpu(gpt_h->first_usable_lba) > lastlba) {
  103. printf("GPT: first_usable_lba incorrect: %llX > " LBAF "\n",
  104. le64_to_cpu(gpt_h->first_usable_lba), lastlba);
  105. return -1;
  106. }
  107. if (le64_to_cpu(gpt_h->last_usable_lba) > lastlba) {
  108. printf("GPT: last_usable_lba incorrect: %llX > " LBAF "\n",
  109. le64_to_cpu(gpt_h->last_usable_lba), lastlba);
  110. return -1;
  111. }
  112. debug("GPT: first_usable_lba: %llX last_usable_lba: %llX last lba: "
  113. LBAF "\n", le64_to_cpu(gpt_h->first_usable_lba),
  114. le64_to_cpu(gpt_h->last_usable_lba), lastlba);
  115. return 0;
  116. }
  117. static int validate_gpt_entries(gpt_header *gpt_h, gpt_entry *gpt_e)
  118. {
  119. uint32_t calc_crc32;
  120. /* Check the GUID Partition Table Entry Array CRC */
  121. calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
  122. le32_to_cpu(gpt_h->num_partition_entries) *
  123. le32_to_cpu(gpt_h->sizeof_partition_entry));
  124. if (calc_crc32 != le32_to_cpu(gpt_h->partition_entry_array_crc32)) {
  125. printf("%s: 0x%x != 0x%x\n",
  126. "GUID Partition Table Entry Array CRC is wrong",
  127. le32_to_cpu(gpt_h->partition_entry_array_crc32),
  128. calc_crc32);
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. static void prepare_backup_gpt_header(gpt_header *gpt_h)
  134. {
  135. uint32_t calc_crc32;
  136. uint64_t val;
  137. /* recalculate the values for the Backup GPT Header */
  138. val = le64_to_cpu(gpt_h->my_lba);
  139. gpt_h->my_lba = gpt_h->alternate_lba;
  140. gpt_h->alternate_lba = cpu_to_le64(val);
  141. gpt_h->partition_entry_lba =
  142. cpu_to_le64(le64_to_cpu(gpt_h->last_usable_lba) + 1);
  143. gpt_h->header_crc32 = 0;
  144. calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
  145. le32_to_cpu(gpt_h->header_size));
  146. gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
  147. }
  148. #if CONFIG_IS_ENABLED(EFI_PARTITION)
  149. /*
  150. * Public Functions (include/part.h)
  151. */
  152. void part_print_efi(struct blk_desc *dev_desc)
  153. {
  154. ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
  155. gpt_entry *gpt_pte = NULL;
  156. int i = 0;
  157. char uuid[37];
  158. unsigned char *uuid_bin;
  159. /* This function validates AND fills in the GPT header and PTE */
  160. if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
  161. gpt_head, &gpt_pte) != 1) {
  162. printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
  163. if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
  164. gpt_head, &gpt_pte) != 1) {
  165. printf("%s: *** ERROR: Invalid Backup GPT ***\n",
  166. __func__);
  167. return;
  168. } else {
  169. printf("%s: *** Using Backup GPT ***\n",
  170. __func__);
  171. }
  172. }
  173. debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
  174. printf("Part\tStart LBA\tEnd LBA\t\tName\n");
  175. printf("\tAttributes\n");
  176. printf("\tType GUID\n");
  177. printf("\tPartition GUID\n");
  178. for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
  179. /* Stop at the first non valid PTE */
  180. if (!is_pte_valid(&gpt_pte[i]))
  181. break;
  182. printf("%3d\t0x%08llx\t0x%08llx\t\"%s\"\n", (i + 1),
  183. le64_to_cpu(gpt_pte[i].starting_lba),
  184. le64_to_cpu(gpt_pte[i].ending_lba),
  185. print_efiname(&gpt_pte[i]));
  186. printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
  187. uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
  188. uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
  189. printf("\ttype:\t%s\n", uuid);
  190. #ifdef CONFIG_PARTITION_TYPE_GUID
  191. if (!uuid_guid_get_str(uuid_bin, uuid))
  192. printf("\ttype:\t%s\n", uuid);
  193. #endif
  194. uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
  195. uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
  196. printf("\tguid:\t%s\n", uuid);
  197. }
  198. /* Remember to free pte */
  199. free(gpt_pte);
  200. return;
  201. }
  202. int part_get_info_efi(struct blk_desc *dev_desc, int part,
  203. disk_partition_t *info)
  204. {
  205. ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz);
  206. gpt_entry *gpt_pte = NULL;
  207. /* "part" argument must be at least 1 */
  208. if (part < 1) {
  209. printf("%s: Invalid Argument(s)\n", __func__);
  210. return -1;
  211. }
  212. /* This function validates AND fills in the GPT header and PTE */
  213. if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
  214. gpt_head, &gpt_pte) != 1) {
  215. printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
  216. if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
  217. gpt_head, &gpt_pte) != 1) {
  218. printf("%s: *** ERROR: Invalid Backup GPT ***\n",
  219. __func__);
  220. return -1;
  221. } else {
  222. printf("%s: *** Using Backup GPT ***\n",
  223. __func__);
  224. }
  225. }
  226. if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
  227. !is_pte_valid(&gpt_pte[part - 1])) {
  228. debug("%s: *** ERROR: Invalid partition number %d ***\n",
  229. __func__, part);
  230. free(gpt_pte);
  231. return -1;
  232. }
  233. /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
  234. info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
  235. /* The ending LBA is inclusive, to calculate size, add 1 to it */
  236. info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
  237. - info->start;
  238. info->blksz = dev_desc->blksz;
  239. sprintf((char *)info->name, "%s",
  240. print_efiname(&gpt_pte[part - 1]));
  241. strcpy((char *)info->type, "U-Boot");
  242. info->bootable = is_bootable(&gpt_pte[part - 1]);
  243. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  244. uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid,
  245. UUID_STR_FORMAT_GUID);
  246. #endif
  247. #ifdef CONFIG_PARTITION_TYPE_GUID
  248. uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
  249. info->type_guid, UUID_STR_FORMAT_GUID);
  250. #endif
  251. debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s\n", __func__,
  252. info->start, info->size, info->name);
  253. /* Remember to free pte */
  254. free(gpt_pte);
  255. return 0;
  256. }
  257. static int part_test_efi(struct blk_desc *dev_desc)
  258. {
  259. ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz);
  260. /* Read legacy MBR from block 0 and validate it */
  261. if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1)
  262. || (is_pmbr_valid(legacymbr) != 1)) {
  263. return -1;
  264. }
  265. return 0;
  266. }
  267. /**
  268. * set_protective_mbr(): Set the EFI protective MBR
  269. * @param dev_desc - block device descriptor
  270. *
  271. * @return - zero on success, otherwise error
  272. */
  273. static int set_protective_mbr(struct blk_desc *dev_desc)
  274. {
  275. /* Setup the Protective MBR */
  276. ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
  277. memset(p_mbr, 0, sizeof(*p_mbr));
  278. if (p_mbr == NULL) {
  279. printf("%s: calloc failed!\n", __func__);
  280. return -1;
  281. }
  282. /* Read MBR to backup boot code if it exists */
  283. if (blk_dread(dev_desc, 0, 1, p_mbr) != 1) {
  284. error("** Can't read from device %d **\n", dev_desc->devnum);
  285. return -1;
  286. }
  287. /* Append signature */
  288. p_mbr->signature = MSDOS_MBR_SIGNATURE;
  289. p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
  290. p_mbr->partition_record[0].start_sect = 1;
  291. p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1;
  292. /* Write MBR sector to the MMC device */
  293. if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) {
  294. printf("** Can't write to device %d **\n",
  295. dev_desc->devnum);
  296. return -1;
  297. }
  298. return 0;
  299. }
  300. int write_gpt_table(struct blk_desc *dev_desc,
  301. gpt_header *gpt_h, gpt_entry *gpt_e)
  302. {
  303. const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
  304. * sizeof(gpt_entry)), dev_desc);
  305. u32 calc_crc32;
  306. debug("max lba: %x\n", (u32) dev_desc->lba);
  307. /* Setup the Protective MBR */
  308. if (set_protective_mbr(dev_desc) < 0)
  309. goto err;
  310. /* Generate CRC for the Primary GPT Header */
  311. calc_crc32 = efi_crc32((const unsigned char *)gpt_e,
  312. le32_to_cpu(gpt_h->num_partition_entries) *
  313. le32_to_cpu(gpt_h->sizeof_partition_entry));
  314. gpt_h->partition_entry_array_crc32 = cpu_to_le32(calc_crc32);
  315. calc_crc32 = efi_crc32((const unsigned char *)gpt_h,
  316. le32_to_cpu(gpt_h->header_size));
  317. gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
  318. /* Write the First GPT to the block right after the Legacy MBR */
  319. if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1)
  320. goto err;
  321. if (blk_dwrite(dev_desc, 2, pte_blk_cnt, gpt_e)
  322. != pte_blk_cnt)
  323. goto err;
  324. prepare_backup_gpt_header(gpt_h);
  325. if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
  326. + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
  327. goto err;
  328. if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
  329. gpt_h) != 1)
  330. goto err;
  331. debug("GPT successfully written to block device!\n");
  332. return 0;
  333. err:
  334. printf("** Can't write to device %d **\n", dev_desc->devnum);
  335. return -1;
  336. }
  337. int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
  338. disk_partition_t *partitions, int parts)
  339. {
  340. lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
  341. lbaint_t start;
  342. lbaint_t last_usable_lba = (lbaint_t)
  343. le64_to_cpu(gpt_h->last_usable_lba);
  344. int i, k;
  345. size_t efiname_len, dosname_len;
  346. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  347. char *str_uuid;
  348. unsigned char *bin_uuid;
  349. #endif
  350. #ifdef CONFIG_PARTITION_TYPE_GUID
  351. char *str_type_guid;
  352. unsigned char *bin_type_guid;
  353. #endif
  354. for (i = 0; i < parts; i++) {
  355. /* partition starting lba */
  356. start = partitions[i].start;
  357. if (start && (start < offset)) {
  358. printf("Partition overlap\n");
  359. return -1;
  360. }
  361. if (start) {
  362. gpt_e[i].starting_lba = cpu_to_le64(start);
  363. offset = start + partitions[i].size;
  364. } else {
  365. gpt_e[i].starting_lba = cpu_to_le64(offset);
  366. offset += partitions[i].size;
  367. }
  368. if (offset > (last_usable_lba + 1)) {
  369. printf("Partitions layout exceds disk size\n");
  370. return -1;
  371. }
  372. /* partition ending lba */
  373. if ((i == parts - 1) && (partitions[i].size == 0))
  374. /* extend the last partition to maximuim */
  375. gpt_e[i].ending_lba = gpt_h->last_usable_lba;
  376. else
  377. gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
  378. #ifdef CONFIG_PARTITION_TYPE_GUID
  379. str_type_guid = partitions[i].type_guid;
  380. bin_type_guid = gpt_e[i].partition_type_guid.b;
  381. if (strlen(str_type_guid)) {
  382. if (uuid_str_to_bin(str_type_guid, bin_type_guid,
  383. UUID_STR_FORMAT_GUID)) {
  384. printf("Partition no. %d: invalid type guid: %s\n",
  385. i, str_type_guid);
  386. return -1;
  387. }
  388. } else {
  389. /* default partition type GUID */
  390. memcpy(bin_type_guid,
  391. &PARTITION_BASIC_DATA_GUID, 16);
  392. }
  393. #else
  394. /* partition type GUID */
  395. memcpy(gpt_e[i].partition_type_guid.b,
  396. &PARTITION_BASIC_DATA_GUID, 16);
  397. #endif
  398. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  399. str_uuid = partitions[i].uuid;
  400. bin_uuid = gpt_e[i].unique_partition_guid.b;
  401. if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
  402. printf("Partition no. %d: invalid guid: %s\n",
  403. i, str_uuid);
  404. return -1;
  405. }
  406. #endif
  407. /* partition attributes */
  408. memset(&gpt_e[i].attributes, 0,
  409. sizeof(gpt_entry_attributes));
  410. if (partitions[i].bootable)
  411. gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
  412. /* partition name */
  413. efiname_len = sizeof(gpt_e[i].partition_name)
  414. / sizeof(efi_char16_t);
  415. dosname_len = sizeof(partitions[i].name);
  416. memset(gpt_e[i].partition_name, 0,
  417. sizeof(gpt_e[i].partition_name));
  418. for (k = 0; k < min(dosname_len, efiname_len); k++)
  419. gpt_e[i].partition_name[k] =
  420. (efi_char16_t)(partitions[i].name[k]);
  421. debug("%s: name: %s offset[%d]: 0x" LBAF
  422. " size[%d]: 0x" LBAF "\n",
  423. __func__, partitions[i].name, i,
  424. offset, i, partitions[i].size);
  425. }
  426. return 0;
  427. }
  428. int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
  429. char *str_guid, int parts_count)
  430. {
  431. gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
  432. gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
  433. gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
  434. gpt_h->my_lba = cpu_to_le64(1);
  435. gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
  436. gpt_h->first_usable_lba = cpu_to_le64(34);
  437. gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
  438. gpt_h->partition_entry_lba = cpu_to_le64(2);
  439. gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
  440. gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
  441. gpt_h->header_crc32 = 0;
  442. gpt_h->partition_entry_array_crc32 = 0;
  443. if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
  444. return -1;
  445. return 0;
  446. }
  447. int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
  448. disk_partition_t *partitions, int parts_count)
  449. {
  450. int ret;
  451. gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
  452. dev_desc));
  453. gpt_entry *gpt_e;
  454. if (gpt_h == NULL) {
  455. printf("%s: calloc failed!\n", __func__);
  456. return -1;
  457. }
  458. gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
  459. * sizeof(gpt_entry),
  460. dev_desc));
  461. if (gpt_e == NULL) {
  462. printf("%s: calloc failed!\n", __func__);
  463. free(gpt_h);
  464. return -1;
  465. }
  466. /* Generate Primary GPT header (LBA1) */
  467. ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
  468. if (ret)
  469. goto err;
  470. /* Generate partition entries */
  471. ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
  472. if (ret)
  473. goto err;
  474. /* Write GPT partition table */
  475. ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
  476. err:
  477. free(gpt_e);
  478. free(gpt_h);
  479. return ret;
  480. }
  481. static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n)
  482. {
  483. char *ess = (char *)es;
  484. int i, j;
  485. memset(s, '\0', n);
  486. for (i = 0, j = 0; j < n; i += 2, j++) {
  487. s[j] = ess[i];
  488. if (!ess[i])
  489. return;
  490. }
  491. }
  492. int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
  493. gpt_entry **gpt_pte)
  494. {
  495. /*
  496. * This function validates AND
  497. * fills in the GPT header and PTE
  498. */
  499. if (is_gpt_valid(dev_desc,
  500. GPT_PRIMARY_PARTITION_TABLE_LBA,
  501. gpt_head, gpt_pte) != 1) {
  502. printf("%s: *** ERROR: Invalid GPT ***\n",
  503. __func__);
  504. return -1;
  505. }
  506. if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
  507. gpt_head, gpt_pte) != 1) {
  508. printf("%s: *** ERROR: Invalid Backup GPT ***\n",
  509. __func__);
  510. return -1;
  511. }
  512. return 0;
  513. }
  514. int gpt_verify_partitions(struct blk_desc *dev_desc,
  515. disk_partition_t *partitions, int parts,
  516. gpt_header *gpt_head, gpt_entry **gpt_pte)
  517. {
  518. char efi_str[PARTNAME_SZ + 1];
  519. u64 gpt_part_size;
  520. gpt_entry *gpt_e;
  521. int ret, i;
  522. ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
  523. if (ret)
  524. return ret;
  525. gpt_e = *gpt_pte;
  526. for (i = 0; i < parts; i++) {
  527. if (i == gpt_head->num_partition_entries) {
  528. error("More partitions than allowed!\n");
  529. return -1;
  530. }
  531. /* Check if GPT and ENV partition names match */
  532. gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
  533. PARTNAME_SZ + 1);
  534. debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
  535. __func__, i, efi_str, partitions[i].name);
  536. if (strncmp(efi_str, (char *)partitions[i].name,
  537. sizeof(partitions->name))) {
  538. error("Partition name: %s does not match %s!\n",
  539. efi_str, (char *)partitions[i].name);
  540. return -1;
  541. }
  542. /* Check if GPT and ENV sizes match */
  543. gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
  544. le64_to_cpu(gpt_e[i].starting_lba) + 1;
  545. debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
  546. (unsigned long long)gpt_part_size,
  547. (unsigned long long)partitions[i].size);
  548. if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
  549. /* We do not check the extend partition size */
  550. if ((i == parts - 1) && (partitions[i].size == 0))
  551. continue;
  552. error("Partition %s size: %llu does not match %llu!\n",
  553. efi_str, (unsigned long long)gpt_part_size,
  554. (unsigned long long)partitions[i].size);
  555. return -1;
  556. }
  557. /*
  558. * Start address is optional - check only if provided
  559. * in '$partition' variable
  560. */
  561. if (!partitions[i].start) {
  562. debug("\n");
  563. continue;
  564. }
  565. /* Check if GPT and ENV start LBAs match */
  566. debug("start LBA - GPT: %8llu, ENV: %8llu\n",
  567. le64_to_cpu(gpt_e[i].starting_lba),
  568. (unsigned long long)partitions[i].start);
  569. if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
  570. error("Partition %s start: %llu does not match %llu!\n",
  571. efi_str, le64_to_cpu(gpt_e[i].starting_lba),
  572. (unsigned long long)partitions[i].start);
  573. return -1;
  574. }
  575. }
  576. return 0;
  577. }
  578. int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf)
  579. {
  580. gpt_header *gpt_h;
  581. gpt_entry *gpt_e;
  582. /* determine start of GPT Header in the buffer */
  583. gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
  584. dev_desc->blksz);
  585. if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
  586. dev_desc->lba))
  587. return -1;
  588. /* determine start of GPT Entries in the buffer */
  589. gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
  590. dev_desc->blksz);
  591. if (validate_gpt_entries(gpt_h, gpt_e))
  592. return -1;
  593. return 0;
  594. }
  595. int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
  596. {
  597. gpt_header *gpt_h;
  598. gpt_entry *gpt_e;
  599. int gpt_e_blk_cnt;
  600. lbaint_t lba;
  601. int cnt;
  602. if (is_valid_gpt_buf(dev_desc, buf))
  603. return -1;
  604. /* determine start of GPT Header in the buffer */
  605. gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
  606. dev_desc->blksz);
  607. /* determine start of GPT Entries in the buffer */
  608. gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
  609. dev_desc->blksz);
  610. gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
  611. le32_to_cpu(gpt_h->sizeof_partition_entry)),
  612. dev_desc);
  613. /* write MBR */
  614. lba = 0; /* MBR is always at 0 */
  615. cnt = 1; /* MBR (1 block) */
  616. if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) {
  617. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  618. __func__, "MBR", cnt, lba);
  619. return 1;
  620. }
  621. /* write Primary GPT */
  622. lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
  623. cnt = 1; /* GPT Header (1 block) */
  624. if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
  625. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  626. __func__, "Primary GPT Header", cnt, lba);
  627. return 1;
  628. }
  629. lba = le64_to_cpu(gpt_h->partition_entry_lba);
  630. cnt = gpt_e_blk_cnt;
  631. if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
  632. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  633. __func__, "Primary GPT Entries", cnt, lba);
  634. return 1;
  635. }
  636. prepare_backup_gpt_header(gpt_h);
  637. /* write Backup GPT */
  638. lba = le64_to_cpu(gpt_h->partition_entry_lba);
  639. cnt = gpt_e_blk_cnt;
  640. if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) {
  641. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  642. __func__, "Backup GPT Entries", cnt, lba);
  643. return 1;
  644. }
  645. lba = le64_to_cpu(gpt_h->my_lba);
  646. cnt = 1; /* GPT Header (1 block) */
  647. if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) {
  648. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  649. __func__, "Backup GPT Header", cnt, lba);
  650. return 1;
  651. }
  652. return 0;
  653. }
  654. #endif
  655. /*
  656. * Private functions
  657. */
  658. /*
  659. * pmbr_part_valid(): Check for EFI partition signature
  660. *
  661. * Returns: 1 if EFI GPT partition type is found.
  662. */
  663. static int pmbr_part_valid(struct partition *part)
  664. {
  665. if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
  666. get_unaligned_le32(&part->start_sect) == 1UL) {
  667. return 1;
  668. }
  669. return 0;
  670. }
  671. /*
  672. * is_pmbr_valid(): test Protective MBR for validity
  673. *
  674. * Returns: 1 if PMBR is valid, 0 otherwise.
  675. * Validity depends on two things:
  676. * 1) MSDOS signature is in the last two bytes of the MBR
  677. * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
  678. */
  679. static int is_pmbr_valid(legacy_mbr * mbr)
  680. {
  681. int i = 0;
  682. if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
  683. return 0;
  684. for (i = 0; i < 4; i++) {
  685. if (pmbr_part_valid(&mbr->partition_record[i])) {
  686. return 1;
  687. }
  688. }
  689. return 0;
  690. }
  691. /**
  692. * is_gpt_valid() - tests one GPT header and PTEs for validity
  693. *
  694. * lba is the logical block address of the GPT header to test
  695. * gpt is a GPT header ptr, filled on return.
  696. * ptes is a PTEs ptr, filled on return.
  697. *
  698. * Description: returns 1 if valid, 0 on error.
  699. * If valid, returns pointers to PTEs.
  700. */
  701. static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
  702. gpt_header *pgpt_head, gpt_entry **pgpt_pte)
  703. {
  704. if (!dev_desc || !pgpt_head) {
  705. printf("%s: Invalid Argument(s)\n", __func__);
  706. return 0;
  707. }
  708. /* Read GPT Header from device */
  709. if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
  710. printf("*** ERROR: Can't read GPT header ***\n");
  711. return 0;
  712. }
  713. if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
  714. return 0;
  715. /* Read and allocate Partition Table Entries */
  716. *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
  717. if (*pgpt_pte == NULL) {
  718. printf("GPT: Failed to allocate memory for PTE\n");
  719. return 0;
  720. }
  721. if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
  722. free(*pgpt_pte);
  723. return 0;
  724. }
  725. /* We're done, all's well */
  726. return 1;
  727. }
  728. /**
  729. * alloc_read_gpt_entries(): reads partition entries from disk
  730. * @dev_desc
  731. * @gpt - GPT header
  732. *
  733. * Description: Returns ptes on success, NULL on error.
  734. * Allocates space for PTEs based on information found in @gpt.
  735. * Notes: remember to free pte when you're done!
  736. */
  737. static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
  738. gpt_header *pgpt_head)
  739. {
  740. size_t count = 0, blk_cnt;
  741. lbaint_t blk;
  742. gpt_entry *pte = NULL;
  743. if (!dev_desc || !pgpt_head) {
  744. printf("%s: Invalid Argument(s)\n", __func__);
  745. return NULL;
  746. }
  747. count = le32_to_cpu(pgpt_head->num_partition_entries) *
  748. le32_to_cpu(pgpt_head->sizeof_partition_entry);
  749. debug("%s: count = %u * %u = %lu\n", __func__,
  750. (u32) le32_to_cpu(pgpt_head->num_partition_entries),
  751. (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry),
  752. (ulong)count);
  753. /* Allocate memory for PTE, remember to FREE */
  754. if (count != 0) {
  755. pte = memalign(ARCH_DMA_MINALIGN,
  756. PAD_TO_BLOCKSIZE(count, dev_desc));
  757. }
  758. if (count == 0 || pte == NULL) {
  759. printf("%s: ERROR: Can't allocate %#lX bytes for GPT Entries\n",
  760. __func__, (ulong)count);
  761. return NULL;
  762. }
  763. /* Read GPT Entries from device */
  764. blk = le64_to_cpu(pgpt_head->partition_entry_lba);
  765. blk_cnt = BLOCK_CNT(count, dev_desc);
  766. if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
  767. printf("*** ERROR: Can't read GPT Entries ***\n");
  768. free(pte);
  769. return NULL;
  770. }
  771. return pte;
  772. }
  773. /**
  774. * is_pte_valid(): validates a single Partition Table Entry
  775. * @gpt_entry - Pointer to a single Partition Table Entry
  776. *
  777. * Description: returns 1 if valid, 0 on error.
  778. */
  779. static int is_pte_valid(gpt_entry * pte)
  780. {
  781. efi_guid_t unused_guid;
  782. if (!pte) {
  783. printf("%s: Invalid Argument(s)\n", __func__);
  784. return 0;
  785. }
  786. /* Only one validation for now:
  787. * The GUID Partition Type != Unused Entry (ALL-ZERO)
  788. */
  789. memset(unused_guid.b, 0, sizeof(unused_guid.b));
  790. if (memcmp(pte->partition_type_guid.b, unused_guid.b,
  791. sizeof(unused_guid.b)) == 0) {
  792. debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
  793. (unsigned int)(uintptr_t)pte);
  794. return 0;
  795. } else {
  796. return 1;
  797. }
  798. }
  799. /*
  800. * Add an 'a_' prefix so it comes before 'dos' in the linker list. We need to
  801. * check EFI first, since a DOS partition is often used as a 'protective MBR'
  802. * with EFI.
  803. */
  804. U_BOOT_PART_TYPE(a_efi) = {
  805. .name = "EFI",
  806. .part_type = PART_TYPE_EFI,
  807. .max_entries = GPT_ENTRY_NUMBERS,
  808. .get_info = part_get_info_ptr(part_get_info_efi),
  809. .print = part_print_ptr(part_print_efi),
  810. .test = part_test_efi,
  811. };
  812. #endif