part_efi.c 25 KB

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