part_efi.c 29 KB

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