part_efi.c 30 KB

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