part_efi.c 30 KB

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