part_efi.c 21 KB

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