part_efi.c 25 KB

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