part_efi.c 30 KB

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