part_efi.c 29 KB

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