part_efi.c 29 KB

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