part_efi.c 30 KB

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