part_efi.c 30 KB

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