part_efi.c 29 KB

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