part_efi.c 31 KB

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