part_efi.c 30 KB

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