part_efi.c 30 KB

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