part_efi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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(block_dev_desc_t *dev_desc, u64 lba,
  40. gpt_header *pgpt_head, gpt_entry **pgpt_pte);
  41. static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * 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 print_part_efi(block_dev_desc_t * 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 get_partition_info_efi(block_dev_desc_t * 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 get_partition_info_efi_by_name(block_dev_desc_t *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 = get_partition_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. int test_part_efi(block_dev_desc_t * 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 ((dev_desc->block_read(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(block_dev_desc_t *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 (dev_desc->block_write(dev_desc, 0, 1, p_mbr) != 1) {
  311. printf("** Can't write to device %d **\n",
  312. dev_desc->dev);
  313. return -1;
  314. }
  315. return 0;
  316. }
  317. int write_gpt_table(block_dev_desc_t *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 (dev_desc->block_write(dev_desc, 1, 1, gpt_h) != 1)
  337. goto err;
  338. if (dev_desc->block_write(dev_desc, 2, pte_blk_cnt, gpt_e)
  339. != pte_blk_cnt)
  340. goto err;
  341. prepare_backup_gpt_header(gpt_h);
  342. if (dev_desc->block_write(dev_desc,
  343. (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
  344. + 1,
  345. pte_blk_cnt, gpt_e) != pte_blk_cnt)
  346. goto err;
  347. if (dev_desc->block_write(dev_desc,
  348. (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
  349. gpt_h) != 1)
  350. goto err;
  351. debug("GPT successfully written to block device!\n");
  352. return 0;
  353. err:
  354. printf("** Can't write to device %d **\n", dev_desc->dev);
  355. return -1;
  356. }
  357. int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
  358. disk_partition_t *partitions, int parts)
  359. {
  360. lbaint_t offset = (lbaint_t)le64_to_cpu(gpt_h->first_usable_lba);
  361. lbaint_t start;
  362. lbaint_t last_usable_lba = (lbaint_t)
  363. le64_to_cpu(gpt_h->last_usable_lba);
  364. int i, k;
  365. size_t efiname_len, dosname_len;
  366. #ifdef CONFIG_PARTITION_UUIDS
  367. char *str_uuid;
  368. unsigned char *bin_uuid;
  369. #endif
  370. #ifdef CONFIG_PARTITION_TYPE_GUID
  371. char *str_type_guid;
  372. unsigned char *bin_type_guid;
  373. #endif
  374. for (i = 0; i < parts; i++) {
  375. /* partition starting lba */
  376. start = partitions[i].start;
  377. if (start && (start < offset)) {
  378. printf("Partition overlap\n");
  379. return -1;
  380. }
  381. if (start) {
  382. gpt_e[i].starting_lba = cpu_to_le64(start);
  383. offset = start + partitions[i].size;
  384. } else {
  385. gpt_e[i].starting_lba = cpu_to_le64(offset);
  386. offset += partitions[i].size;
  387. }
  388. if (offset >= last_usable_lba) {
  389. printf("Partitions layout exceds disk size\n");
  390. return -1;
  391. }
  392. /* partition ending lba */
  393. if ((i == parts - 1) && (partitions[i].size == 0))
  394. /* extend the last partition to maximuim */
  395. gpt_e[i].ending_lba = gpt_h->last_usable_lba;
  396. else
  397. gpt_e[i].ending_lba = cpu_to_le64(offset - 1);
  398. #ifdef CONFIG_PARTITION_TYPE_GUID
  399. str_type_guid = partitions[i].type_guid;
  400. bin_type_guid = gpt_e[i].partition_type_guid.b;
  401. if (strlen(str_type_guid)) {
  402. if (uuid_str_to_bin(str_type_guid, bin_type_guid,
  403. UUID_STR_FORMAT_GUID)) {
  404. printf("Partition no. %d: invalid type guid: %s\n",
  405. i, str_type_guid);
  406. return -1;
  407. }
  408. } else {
  409. /* default partition type GUID */
  410. memcpy(bin_type_guid,
  411. &PARTITION_BASIC_DATA_GUID, 16);
  412. }
  413. #else
  414. /* partition type GUID */
  415. memcpy(gpt_e[i].partition_type_guid.b,
  416. &PARTITION_BASIC_DATA_GUID, 16);
  417. #endif
  418. #ifdef CONFIG_PARTITION_UUIDS
  419. str_uuid = partitions[i].uuid;
  420. bin_uuid = gpt_e[i].unique_partition_guid.b;
  421. if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
  422. printf("Partition no. %d: invalid guid: %s\n",
  423. i, str_uuid);
  424. return -1;
  425. }
  426. #endif
  427. /* partition attributes */
  428. memset(&gpt_e[i].attributes, 0,
  429. sizeof(gpt_entry_attributes));
  430. if (partitions[i].bootable)
  431. gpt_e[i].attributes.fields.legacy_bios_bootable = 1;
  432. /* partition name */
  433. efiname_len = sizeof(gpt_e[i].partition_name)
  434. / sizeof(efi_char16_t);
  435. dosname_len = sizeof(partitions[i].name);
  436. memset(gpt_e[i].partition_name, 0,
  437. sizeof(gpt_e[i].partition_name));
  438. for (k = 0; k < min(dosname_len, efiname_len); k++)
  439. gpt_e[i].partition_name[k] =
  440. (efi_char16_t)(partitions[i].name[k]);
  441. debug("%s: name: %s offset[%d]: 0x" LBAF
  442. " size[%d]: 0x" LBAF "\n",
  443. __func__, partitions[i].name, i,
  444. offset, i, partitions[i].size);
  445. }
  446. return 0;
  447. }
  448. int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
  449. char *str_guid, int parts_count)
  450. {
  451. gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
  452. gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
  453. gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
  454. gpt_h->my_lba = cpu_to_le64(1);
  455. gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1);
  456. gpt_h->first_usable_lba = cpu_to_le64(34);
  457. gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34);
  458. gpt_h->partition_entry_lba = cpu_to_le64(2);
  459. gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
  460. gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
  461. gpt_h->header_crc32 = 0;
  462. gpt_h->partition_entry_array_crc32 = 0;
  463. if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
  464. return -1;
  465. return 0;
  466. }
  467. int gpt_restore(block_dev_desc_t *dev_desc, char *str_disk_guid,
  468. disk_partition_t *partitions, int parts_count)
  469. {
  470. int ret;
  471. gpt_header *gpt_h = calloc(1, PAD_TO_BLOCKSIZE(sizeof(gpt_header),
  472. dev_desc));
  473. gpt_entry *gpt_e;
  474. if (gpt_h == NULL) {
  475. printf("%s: calloc failed!\n", __func__);
  476. return -1;
  477. }
  478. gpt_e = calloc(1, PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS
  479. * sizeof(gpt_entry),
  480. dev_desc));
  481. if (gpt_e == NULL) {
  482. printf("%s: calloc failed!\n", __func__);
  483. free(gpt_h);
  484. return -1;
  485. }
  486. /* Generate Primary GPT header (LBA1) */
  487. ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count);
  488. if (ret)
  489. goto err;
  490. /* Generate partition entries */
  491. ret = gpt_fill_pte(gpt_h, gpt_e, partitions, parts_count);
  492. if (ret)
  493. goto err;
  494. /* Write GPT partition table */
  495. ret = write_gpt_table(dev_desc, gpt_h, gpt_e);
  496. err:
  497. free(gpt_e);
  498. free(gpt_h);
  499. return ret;
  500. }
  501. static void gpt_convert_efi_name_to_char(char *s, efi_char16_t *es, int n)
  502. {
  503. char *ess = (char *)es;
  504. int i, j;
  505. memset(s, '\0', n);
  506. for (i = 0, j = 0; j < n; i += 2, j++) {
  507. s[j] = ess[i];
  508. if (!ess[i])
  509. return;
  510. }
  511. }
  512. int gpt_verify_headers(block_dev_desc_t *dev_desc, gpt_header *gpt_head,
  513. gpt_entry **gpt_pte)
  514. {
  515. /*
  516. * This function validates AND
  517. * fills in the GPT header and PTE
  518. */
  519. if (is_gpt_valid(dev_desc,
  520. GPT_PRIMARY_PARTITION_TABLE_LBA,
  521. gpt_head, gpt_pte) != 1) {
  522. printf("%s: *** ERROR: Invalid GPT ***\n",
  523. __func__);
  524. return -1;
  525. }
  526. if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),
  527. gpt_head, gpt_pte) != 1) {
  528. printf("%s: *** ERROR: Invalid Backup GPT ***\n",
  529. __func__);
  530. return -1;
  531. }
  532. return 0;
  533. }
  534. int gpt_verify_partitions(block_dev_desc_t *dev_desc,
  535. disk_partition_t *partitions, int parts,
  536. gpt_header *gpt_head, gpt_entry **gpt_pte)
  537. {
  538. char efi_str[PARTNAME_SZ + 1];
  539. u64 gpt_part_size;
  540. gpt_entry *gpt_e;
  541. int ret, i;
  542. ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte);
  543. if (ret)
  544. return ret;
  545. gpt_e = *gpt_pte;
  546. for (i = 0; i < parts; i++) {
  547. if (i == gpt_head->num_partition_entries) {
  548. error("More partitions than allowed!\n");
  549. return -1;
  550. }
  551. /* Check if GPT and ENV partition names match */
  552. gpt_convert_efi_name_to_char(efi_str, gpt_e[i].partition_name,
  553. PARTNAME_SZ + 1);
  554. debug("%s: part: %2d name - GPT: %16s, ENV: %16s ",
  555. __func__, i, efi_str, partitions[i].name);
  556. if (strncmp(efi_str, (char *)partitions[i].name,
  557. sizeof(partitions->name))) {
  558. error("Partition name: %s does not match %s!\n",
  559. efi_str, (char *)partitions[i].name);
  560. return -1;
  561. }
  562. /* Check if GPT and ENV sizes match */
  563. gpt_part_size = le64_to_cpu(gpt_e[i].ending_lba) -
  564. le64_to_cpu(gpt_e[i].starting_lba) + 1;
  565. debug("size(LBA) - GPT: %8llu, ENV: %8llu ",
  566. gpt_part_size, (u64) partitions[i].size);
  567. if (le64_to_cpu(gpt_part_size) != partitions[i].size) {
  568. error("Partition %s size: %llu does not match %llu!\n",
  569. efi_str, gpt_part_size, (u64) partitions[i].size);
  570. return -1;
  571. }
  572. /*
  573. * Start address is optional - check only if provided
  574. * in '$partition' variable
  575. */
  576. if (!partitions[i].start) {
  577. debug("\n");
  578. continue;
  579. }
  580. /* Check if GPT and ENV start LBAs match */
  581. debug("start LBA - GPT: %8llu, ENV: %8llu\n",
  582. le64_to_cpu(gpt_e[i].starting_lba),
  583. (u64) partitions[i].start);
  584. if (le64_to_cpu(gpt_e[i].starting_lba) != partitions[i].start) {
  585. error("Partition %s start: %llu does not match %llu!\n",
  586. efi_str, le64_to_cpu(gpt_e[i].starting_lba),
  587. (u64) partitions[i].start);
  588. return -1;
  589. }
  590. }
  591. return 0;
  592. }
  593. int is_valid_gpt_buf(block_dev_desc_t *dev_desc, void *buf)
  594. {
  595. gpt_header *gpt_h;
  596. gpt_entry *gpt_e;
  597. /* determine start of GPT Header in the buffer */
  598. gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
  599. dev_desc->blksz);
  600. if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
  601. dev_desc->lba))
  602. return -1;
  603. /* determine start of GPT Entries in the buffer */
  604. gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
  605. dev_desc->blksz);
  606. if (validate_gpt_entries(gpt_h, gpt_e))
  607. return -1;
  608. return 0;
  609. }
  610. int write_mbr_and_gpt_partitions(block_dev_desc_t *dev_desc, void *buf)
  611. {
  612. gpt_header *gpt_h;
  613. gpt_entry *gpt_e;
  614. int gpt_e_blk_cnt;
  615. lbaint_t lba;
  616. int cnt;
  617. if (is_valid_gpt_buf(dev_desc, buf))
  618. return -1;
  619. /* determine start of GPT Header in the buffer */
  620. gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA *
  621. dev_desc->blksz);
  622. /* determine start of GPT Entries in the buffer */
  623. gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
  624. dev_desc->blksz);
  625. gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
  626. le32_to_cpu(gpt_h->sizeof_partition_entry)),
  627. dev_desc);
  628. /* write MBR */
  629. lba = 0; /* MBR is always at 0 */
  630. cnt = 1; /* MBR (1 block) */
  631. if (dev_desc->block_write(dev_desc, lba, cnt, buf) != cnt) {
  632. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  633. __func__, "MBR", cnt, lba);
  634. return 1;
  635. }
  636. /* write Primary GPT */
  637. lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
  638. cnt = 1; /* GPT Header (1 block) */
  639. if (dev_desc->block_write(dev_desc, lba, cnt, gpt_h) != cnt) {
  640. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  641. __func__, "Primary GPT Header", cnt, lba);
  642. return 1;
  643. }
  644. lba = le64_to_cpu(gpt_h->partition_entry_lba);
  645. cnt = gpt_e_blk_cnt;
  646. if (dev_desc->block_write(dev_desc, lba, cnt, gpt_e) != cnt) {
  647. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  648. __func__, "Primary GPT Entries", cnt, lba);
  649. return 1;
  650. }
  651. prepare_backup_gpt_header(gpt_h);
  652. /* write Backup GPT */
  653. lba = le64_to_cpu(gpt_h->partition_entry_lba);
  654. cnt = gpt_e_blk_cnt;
  655. if (dev_desc->block_write(dev_desc, lba, cnt, gpt_e) != cnt) {
  656. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  657. __func__, "Backup GPT Entries", cnt, lba);
  658. return 1;
  659. }
  660. lba = le64_to_cpu(gpt_h->my_lba);
  661. cnt = 1; /* GPT Header (1 block) */
  662. if (dev_desc->block_write(dev_desc, lba, cnt, gpt_h) != cnt) {
  663. printf("%s: failed writing '%s' (%d blks at 0x" LBAF ")\n",
  664. __func__, "Backup GPT Header", cnt, lba);
  665. return 1;
  666. }
  667. return 0;
  668. }
  669. #endif
  670. /*
  671. * Private functions
  672. */
  673. /*
  674. * pmbr_part_valid(): Check for EFI partition signature
  675. *
  676. * Returns: 1 if EFI GPT partition type is found.
  677. */
  678. static int pmbr_part_valid(struct partition *part)
  679. {
  680. if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT &&
  681. get_unaligned_le32(&part->start_sect) == 1UL) {
  682. return 1;
  683. }
  684. return 0;
  685. }
  686. /*
  687. * is_pmbr_valid(): test Protective MBR for validity
  688. *
  689. * Returns: 1 if PMBR is valid, 0 otherwise.
  690. * Validity depends on two things:
  691. * 1) MSDOS signature is in the last two bytes of the MBR
  692. * 2) One partition of type 0xEE is found, checked by pmbr_part_valid()
  693. */
  694. static int is_pmbr_valid(legacy_mbr * mbr)
  695. {
  696. int i = 0;
  697. if (!mbr || le16_to_cpu(mbr->signature) != MSDOS_MBR_SIGNATURE)
  698. return 0;
  699. for (i = 0; i < 4; i++) {
  700. if (pmbr_part_valid(&mbr->partition_record[i])) {
  701. return 1;
  702. }
  703. }
  704. return 0;
  705. }
  706. /**
  707. * is_gpt_valid() - tests one GPT header and PTEs for validity
  708. *
  709. * lba is the logical block address of the GPT header to test
  710. * gpt is a GPT header ptr, filled on return.
  711. * ptes is a PTEs ptr, filled on return.
  712. *
  713. * Description: returns 1 if valid, 0 on error.
  714. * If valid, returns pointers to PTEs.
  715. */
  716. static int is_gpt_valid(block_dev_desc_t *dev_desc, u64 lba,
  717. gpt_header *pgpt_head, gpt_entry **pgpt_pte)
  718. {
  719. if (!dev_desc || !pgpt_head) {
  720. printf("%s: Invalid Argument(s)\n", __func__);
  721. return 0;
  722. }
  723. /* Read GPT Header from device */
  724. if (dev_desc->block_read(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
  725. printf("*** ERROR: Can't read GPT header ***\n");
  726. return 0;
  727. }
  728. if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba))
  729. return 0;
  730. /* Read and allocate Partition Table Entries */
  731. *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head);
  732. if (*pgpt_pte == NULL) {
  733. printf("GPT: Failed to allocate memory for PTE\n");
  734. return 0;
  735. }
  736. if (validate_gpt_entries(pgpt_head, *pgpt_pte)) {
  737. free(*pgpt_pte);
  738. return 0;
  739. }
  740. /* We're done, all's well */
  741. return 1;
  742. }
  743. /**
  744. * alloc_read_gpt_entries(): reads partition entries from disk
  745. * @dev_desc
  746. * @gpt - GPT header
  747. *
  748. * Description: Returns ptes on success, NULL on error.
  749. * Allocates space for PTEs based on information found in @gpt.
  750. * Notes: remember to free pte when you're done!
  751. */
  752. static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
  753. gpt_header * pgpt_head)
  754. {
  755. size_t count = 0, blk_cnt;
  756. lbaint_t blk;
  757. gpt_entry *pte = NULL;
  758. if (!dev_desc || !pgpt_head) {
  759. printf("%s: Invalid Argument(s)\n", __func__);
  760. return NULL;
  761. }
  762. count = le32_to_cpu(pgpt_head->num_partition_entries) *
  763. le32_to_cpu(pgpt_head->sizeof_partition_entry);
  764. debug("%s: count = %u * %u = %zu\n", __func__,
  765. (u32) le32_to_cpu(pgpt_head->num_partition_entries),
  766. (u32) le32_to_cpu(pgpt_head->sizeof_partition_entry), count);
  767. /* Allocate memory for PTE, remember to FREE */
  768. if (count != 0) {
  769. pte = memalign(ARCH_DMA_MINALIGN,
  770. PAD_TO_BLOCKSIZE(count, dev_desc));
  771. }
  772. if (count == 0 || pte == NULL) {
  773. printf("%s: ERROR: Can't allocate 0x%zX "
  774. "bytes for GPT Entries\n",
  775. __func__, count);
  776. return NULL;
  777. }
  778. /* Read GPT Entries from device */
  779. blk = le64_to_cpu(pgpt_head->partition_entry_lba);
  780. blk_cnt = BLOCK_CNT(count, dev_desc);
  781. if (dev_desc->block_read(dev_desc, blk, (lbaint_t)blk_cnt, pte)
  782. != blk_cnt) {
  783. printf("*** ERROR: Can't read GPT Entries ***\n");
  784. free(pte);
  785. return NULL;
  786. }
  787. return pte;
  788. }
  789. /**
  790. * is_pte_valid(): validates a single Partition Table Entry
  791. * @gpt_entry - Pointer to a single Partition Table Entry
  792. *
  793. * Description: returns 1 if valid, 0 on error.
  794. */
  795. static int is_pte_valid(gpt_entry * pte)
  796. {
  797. efi_guid_t unused_guid;
  798. if (!pte) {
  799. printf("%s: Invalid Argument(s)\n", __func__);
  800. return 0;
  801. }
  802. /* Only one validation for now:
  803. * The GUID Partition Type != Unused Entry (ALL-ZERO)
  804. */
  805. memset(unused_guid.b, 0, sizeof(unused_guid.b));
  806. if (memcmp(pte->partition_type_guid.b, unused_guid.b,
  807. sizeof(unused_guid.b)) == 0) {
  808. debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
  809. (unsigned int)(uintptr_t)pte);
  810. return 0;
  811. } else {
  812. return 1;
  813. }
  814. }
  815. #endif