part_efi.c 30 KB

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