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