part_efi.c 30 KB

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