part_efi.c 29 KB

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