part_efi.c 29 KB

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