1
0

gpt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-05-05 linzhenxing first version
  9. */
  10. #include <rtthread.h>
  11. #include <dfs_fs.h>
  12. #include <drivers/gpt.h>
  13. #include <drivers/mmcsd_core.h>
  14. #define DBG_TAG "GPT"
  15. #ifdef RT_SDIO_DEBUG
  16. #define DBG_LVL DBG_LOG
  17. #else
  18. #define DBG_LVL DBG_INFO
  19. #endif /* RT_SDIO_DEBUG */
  20. #include <rtdbg.h>
  21. #define min(a, b) a < b ? a : b
  22. static int force_gpt = 0;
  23. static gpt_header *_gpt;
  24. static gpt_entry *_ptes;
  25. #define GPT_TYPE 1
  26. #define MBR_TYPE 0
  27. static inline int efi_guidcmp (gpt_guid_t left, gpt_guid_t right)
  28. {
  29. return rt_memcmp(&left, &right, sizeof (gpt_guid_t));
  30. }
  31. static uint32_t last_lba(struct rt_mmcsd_card *card)
  32. {
  33. RT_ASSERT(card != RT_NULL);
  34. return (card->card_sec_cnt) - 1;
  35. }
  36. static inline int pmbr_part_valid(gpt_mbr_record *part)
  37. {
  38. if (part->os_type != EFI_PMBR_OSTYPE_EFI_GPT)
  39. {
  40. goto invalid;
  41. }
  42. /* set to 0x00000001 (i.e., the LBA of the GPT Partition Header) */
  43. if ((uint32_t)(part->starting_lba) != GPT_PRIMARY_PARTITION_TABLE_LBA)
  44. {
  45. goto invalid;
  46. }
  47. return GPT_MBR_PROTECTIVE;
  48. invalid:
  49. return 0;
  50. }
  51. /*
  52. *
  53. * return ret
  54. * ret = 0, invalid mbr
  55. * ret = 1, protect mbr
  56. * ret = 2, hybrid mbr
  57. */
  58. int is_pmbr_valid(legacy_mbr *mbr, uint64_t total_sectors)
  59. {
  60. uint32_t sz = 0;
  61. int i, part = 0, ret = 0; /* invalid by default */
  62. if (!mbr || (uint16_t)(mbr->signature) != MSDOS_MBR_SIGNATURE)
  63. {
  64. goto done;
  65. }
  66. for (i = 0; i < 4; i++)
  67. {
  68. ret = pmbr_part_valid(&mbr->partition_record[i]);
  69. if (ret == GPT_MBR_PROTECTIVE)
  70. {
  71. part = i;
  72. /*
  73. * Ok, we at least know that there's a protective MBR,
  74. * now check if there are other partition types for
  75. * hybrid MBR.
  76. */
  77. goto check_hybrid;
  78. }
  79. }
  80. if (ret != GPT_MBR_PROTECTIVE)
  81. {
  82. goto done;
  83. }
  84. check_hybrid:
  85. for (i = 0; i < 4; i++)
  86. {
  87. if ((mbr->partition_record[i].os_type !=
  88. EFI_PMBR_OSTYPE_EFI_GPT) &&
  89. (mbr->partition_record[i].os_type != 0x00))
  90. {
  91. ret = GPT_MBR_HYBRID;
  92. }
  93. }
  94. /*
  95. * Protective MBRs take up the lesser of the whole disk
  96. * or 2 TiB (32bit LBA), ignoring the rest of the disk.
  97. * Some partitioning programs, nonetheless, choose to set
  98. * the size to the maximum 32-bit limitation, disregarding
  99. * the disk size.
  100. *
  101. * Hybrid MBRs do not necessarily comply with this.
  102. *
  103. * Consider a bad value here to be a warning to support dd'ing
  104. * an image from a smaller disk to a larger disk.
  105. */
  106. if (ret == GPT_MBR_PROTECTIVE)
  107. {
  108. sz = (uint32_t)(mbr->partition_record[part].size_in_lba);
  109. if (sz != (uint32_t) total_sectors - 1 && sz != 0xFFFFFFFF)
  110. {
  111. LOG_I("GPT: mbr size in lba (%u) different than whole disk (%u).",
  112. sz, min(total_sectors - 1, 0xFFFFFFFF));
  113. }
  114. }
  115. done:
  116. return ret;
  117. }
  118. static gpt_entry *alloc_read_gpt_entries(struct rt_mmcsd_card *card, gpt_header *gpt)
  119. {
  120. size_t count;
  121. gpt_entry *pte;
  122. if (!gpt)
  123. {
  124. return RT_NULL;
  125. }
  126. count = (size_t)(gpt->num_partition_entries) * (gpt->sizeof_partition_entry);
  127. if (!count)
  128. {
  129. return RT_NULL;
  130. }
  131. pte = rt_malloc(count);
  132. if (!pte)
  133. return RT_NULL;
  134. if (read_lba(card, (size_t)(gpt->partition_entry_lba),(uint8_t *)pte, count/512) != RT_EOK)
  135. {
  136. rt_free(pte);
  137. return RT_NULL;
  138. }
  139. return pte;
  140. }
  141. static gpt_header *alloc_read_gpt_header(struct rt_mmcsd_card *card, size_t lba)
  142. {
  143. gpt_header *gpt;
  144. void *buf;
  145. buf = rt_malloc(512);
  146. if (!buf)
  147. {
  148. return RT_NULL;
  149. }
  150. if (read_lba(card, lba, (uint8_t *)buf, 1) != RT_EOK)
  151. {
  152. rt_free(buf);
  153. return RT_NULL;
  154. }
  155. gpt = (gpt_header *)buf;
  156. return gpt;
  157. }
  158. static int is_gpt_valid(struct rt_mmcsd_card *card, size_t lba, gpt_header **gpt, gpt_entry **ptes)
  159. {
  160. size_t lastlba;
  161. if (!ptes)
  162. {
  163. return 0;
  164. }
  165. if (!(*gpt = alloc_read_gpt_header(card, lba)))
  166. {
  167. return 0;
  168. }
  169. /* Check the GUID Partition Table signature */
  170. if ((uint64_t)((*gpt)->signature) != GPT_HEADER_SIGNATURE)
  171. {
  172. LOG_E("GUID Partition Table Header signature is wrong:"
  173. "%ld != %ld",(uint64_t)((*gpt)->signature),(uint64_t)GPT_HEADER_SIGNATURE);
  174. goto fail;
  175. }
  176. /* Check the GUID Partition Table header size is too small */
  177. if ((uint32_t)((*gpt)->header_size) < sizeof(gpt_header))
  178. {
  179. LOG_E("GUID Partition Table Header size is too small: %u < %zu",
  180. (uint32_t)((*gpt)->header_size),sizeof(gpt_header));
  181. goto fail;
  182. }
  183. /* Check that the start_lba entry points to the LBA that contains
  184. * the GUID Partition Table */
  185. if ((uint64_t)((*gpt)->start_lba) != lba)
  186. {
  187. LOG_E("GPT start_lba incorrect: %ld != %ld",
  188. (uint64_t)((*gpt)->start_lba),
  189. (uint64_t)lba);
  190. goto fail;
  191. }
  192. /* Check the first_usable_lba and last_usable_lba are
  193. * within the disk.
  194. */
  195. lastlba = last_lba(card);
  196. if ((uint64_t)((*gpt)->first_usable_lba) > lastlba)
  197. {
  198. LOG_E("GPT: first_usable_lba incorrect: %ld > %ld",
  199. ((uint64_t)((*gpt)->first_usable_lba)),
  200. (size_t)lastlba);
  201. goto fail;
  202. }
  203. if ((uint64_t)((*gpt)->last_usable_lba) > lastlba)
  204. {
  205. LOG_E("GPT: last_usable_lba incorrect: %ld > %ld",
  206. (uint64_t)((*gpt)->last_usable_lba),
  207. (size_t)lastlba);
  208. goto fail;
  209. }
  210. if ((uint64_t)((*gpt)->last_usable_lba) < (uint64_t)((*gpt)->first_usable_lba))
  211. {
  212. LOG_E("GPT: last_usable_lba incorrect: %ld > %ld",
  213. (uint64_t)((*gpt)->last_usable_lba),
  214. (uint64_t)((*gpt)->first_usable_lba));
  215. goto fail;
  216. }
  217. /* Check that sizeof_partition_entry has the correct value */
  218. if ((uint32_t)((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
  219. LOG_E("GUID Partition Entry Size check failed.");
  220. goto fail;
  221. }
  222. if (!(*ptes = alloc_read_gpt_entries(card, *gpt)))
  223. {
  224. goto fail;
  225. }
  226. /* We're done, all's well */
  227. return 1;
  228. fail:
  229. rt_free(*gpt);
  230. *gpt = RT_NULL;
  231. return 0;
  232. }
  233. /**
  234. * is_pte_valid() - tests one PTE for validity
  235. * pte:pte to check
  236. * lastlba: last lba of the disk
  237. *
  238. * Description: returns 1 if valid, 0 on error.
  239. */
  240. static inline int is_pte_valid(const gpt_entry *pte, const size_t lastlba)
  241. {
  242. if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
  243. (uint64_t)(pte->starting_lba) > lastlba ||
  244. (uint64_t)(pte->ending_lba) > lastlba)
  245. {
  246. return 0;
  247. }
  248. return 1;
  249. }
  250. /**
  251. * compare_gpts() - Search disk for valid GPT headers and PTEs
  252. * pgpt: primary GPT header
  253. * agpt: alternate GPT header
  254. * lastlba: last LBA number
  255. *
  256. * Description: Returns nothing. Sanity checks pgpt and agpt fields
  257. * and prints warnings on discrepancies.
  258. *
  259. */
  260. static void compare_gpts(gpt_header *pgpt, gpt_header *agpt, size_t lastlba)
  261. {
  262. int error_found = 0;
  263. if (!pgpt || !agpt)
  264. {
  265. return;
  266. }
  267. if ((uint64_t)(pgpt->start_lba) != (uint64_t)(agpt->alternate_lba))
  268. {
  269. LOG_I("GPT:Primary header LBA != Alt. header alternate_lba");
  270. LOG_I("GPT:%lld != %lld",
  271. (uint64_t)(pgpt->start_lba),
  272. (uint64_t)(agpt->alternate_lba));
  273. error_found++;
  274. }
  275. if ((uint64_t)(pgpt->alternate_lba) != (uint64_t)(agpt->start_lba))
  276. {
  277. LOG_I("GPT:Primary header alternate_lba != Alt. header start_lba");
  278. LOG_I("GPT:%lld != %lld",
  279. (uint64_t)(pgpt->alternate_lba),
  280. (uint64_t)(agpt->start_lba));
  281. error_found++;
  282. }
  283. if ((uint64_t)(pgpt->first_usable_lba) != (uint64_t)(agpt->first_usable_lba))
  284. {
  285. LOG_I("GPT:first_usable_lbas don't match.");
  286. LOG_I("GPT:%lld != %lld",
  287. (uint64_t)(pgpt->first_usable_lba),
  288. (uint64_t)(agpt->first_usable_lba));
  289. error_found++;
  290. }
  291. if ((uint64_t)(pgpt->last_usable_lba) != (uint64_t)(agpt->last_usable_lba))
  292. {
  293. LOG_I("GPT:last_usable_lbas don't match.");
  294. LOG_I("GPT:%lld != %lld",
  295. (uint64_t)(pgpt->last_usable_lba),
  296. (uint64_t)(agpt->last_usable_lba));
  297. error_found++;
  298. }
  299. if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid))
  300. {
  301. LOG_I("GPT:disk_guids don't match.");
  302. error_found++;
  303. }
  304. if ((pgpt->num_partition_entries) != (agpt->num_partition_entries))
  305. {
  306. LOG_I("GPT:num_partition_entries don't match: "
  307. "0x%x != 0x%x",
  308. (pgpt->num_partition_entries),
  309. (agpt->num_partition_entries));
  310. error_found++;
  311. }
  312. if ((pgpt->sizeof_partition_entry) != (agpt->sizeof_partition_entry))
  313. {
  314. LOG_I("GPT:sizeof_partition_entry values don't match: "
  315. "0x%x != 0x%x",
  316. (pgpt->sizeof_partition_entry),
  317. (agpt->sizeof_partition_entry));
  318. error_found++;
  319. }
  320. if ((pgpt->partition_entry_array_crc32) != (agpt->partition_entry_array_crc32))
  321. {
  322. LOG_I("GPT:partition_entry_array_crc32 values don't match: "
  323. "0x%x != 0x%x",
  324. (pgpt->partition_entry_array_crc32),
  325. (agpt->partition_entry_array_crc32));
  326. error_found++;
  327. }
  328. if ((pgpt->alternate_lba) != lastlba)
  329. {
  330. LOG_I("GPT:Primary header thinks Alt. header is not at the end of the disk.");
  331. LOG_I("GPT:%lld != %lld",
  332. (uint64_t)(pgpt->alternate_lba),
  333. (size_t)lastlba);
  334. error_found++;
  335. }
  336. if ((agpt->start_lba) != lastlba)
  337. {
  338. LOG_I("GPT:Alternate GPT header not at the end of the disk.");
  339. LOG_I("GPT:%lld != %lld",
  340. (uint64_t)(agpt->start_lba),
  341. (size_t)lastlba);
  342. error_found++;
  343. }
  344. if (error_found)
  345. {
  346. LOG_I("GPT: Use GNU Parted to correct GPT errors.");
  347. }
  348. return;
  349. }
  350. /**
  351. * find_valid_gpt() - Search disk for valid GPT headers and PTEs
  352. * state: disk parsed partitions
  353. * gpt: GPT header ptr, filled on return.
  354. * ptes: PTEs ptr, filled on return.
  355. *
  356. * Description: Returns 1 if valid, 0 on error.
  357. * If valid, returns pointers to newly allocated GPT header and PTEs.
  358. * Validity depends on PMBR being valid (or being overridden by the
  359. * 'gpt' kernel command line option) and finding either the Primary
  360. * GPT header and PTEs valid, or the Alternate GPT header and PTEs
  361. * valid. If the Primary GPT header is not valid, the Alternate GPT header
  362. * is not checked unless the 'gpt' kernel command line option is passed.
  363. * This protects against devices which misreport their size, and forces
  364. * the user to decide to use the Alternate GPT.
  365. */
  366. static int find_valid_gpt(struct rt_mmcsd_card *card, gpt_header **gpt,
  367. gpt_entry **ptes)
  368. {
  369. int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
  370. gpt_header *pgpt = RT_NULL, *agpt = RT_NULL;
  371. gpt_entry *pptes = RT_NULL, *aptes = RT_NULL;
  372. legacy_mbr *legacymbr;
  373. size_t total_sectors = last_lba(card) + 1;
  374. size_t lastlba;
  375. int status = 0;
  376. if (!ptes)
  377. {
  378. return 0;
  379. }
  380. lastlba = last_lba(card);
  381. if (!force_gpt)
  382. {
  383. /* This will be added to the EFI Spec. per Intel after v1.02. */
  384. legacymbr = rt_malloc(512);
  385. if (!legacymbr)
  386. {
  387. goto fail;
  388. }
  389. status = read_lba(card, 0, (uint8_t *)legacymbr, 1);
  390. if (status)
  391. {
  392. LOG_I("status:%d", status);
  393. goto fail;
  394. }
  395. good_pmbr = is_pmbr_valid(legacymbr, total_sectors);
  396. rt_free(legacymbr);
  397. if (!good_pmbr)
  398. {
  399. goto fail;
  400. }
  401. rt_kprintf("Device has a %s MBR\n",
  402. good_pmbr == GPT_MBR_PROTECTIVE ?
  403. "protective" : "hybrid");
  404. }
  405. good_pgpt = is_gpt_valid(card, GPT_PRIMARY_PARTITION_TABLE_LBA,
  406. &pgpt, &pptes);
  407. if (good_pgpt)
  408. {
  409. good_agpt = is_gpt_valid(card, (pgpt->alternate_lba), &agpt, &aptes);
  410. if (!good_agpt && force_gpt)
  411. {
  412. good_agpt = is_gpt_valid(card, lastlba, &agpt, &aptes);
  413. }
  414. /* The obviously unsuccessful case */
  415. if (!good_pgpt && !good_agpt)
  416. {
  417. goto fail;
  418. }
  419. compare_gpts(pgpt, agpt, lastlba);
  420. /* The good cases */
  421. if (good_pgpt)
  422. {
  423. *gpt = pgpt;
  424. *ptes = pptes;
  425. rt_free(agpt);
  426. rt_free(aptes);
  427. if (!good_agpt)
  428. {
  429. LOG_D("Alternate GPT is invalid, using primary GPT.");
  430. }
  431. return 1;
  432. }
  433. else if (good_agpt)
  434. {
  435. *gpt = agpt;
  436. *ptes = aptes;
  437. rt_free(pgpt);
  438. rt_free(pptes);
  439. LOG_D("Primary GPT is invalid, using alternate GPT.");
  440. return 1;
  441. }
  442. }
  443. fail:
  444. rt_free(pgpt);
  445. rt_free(agpt);
  446. rt_free(pptes);
  447. rt_free(aptes);
  448. *gpt = RT_NULL;
  449. *ptes = RT_NULL;
  450. return 0;
  451. }
  452. int check_gpt(struct rt_mmcsd_card *card)
  453. {
  454. if (!find_valid_gpt(card, &_gpt, &_ptes) || !_gpt || !_ptes)
  455. {
  456. rt_free(_gpt);
  457. rt_free(_ptes);
  458. return MBR_TYPE;
  459. }
  460. return GPT_TYPE;
  461. }
  462. int gpt_get_partition_param(struct rt_mmcsd_card *card, struct dfs_partition *part, uint32_t pindex)
  463. {
  464. if (!is_pte_valid(&_ptes[pindex], last_lba(card)))
  465. {
  466. return -1;
  467. }
  468. part->offset = (off_t)(_ptes[pindex].starting_lba);
  469. part->size = (_ptes[pindex].ending_lba) - (_ptes[pindex].starting_lba) + 1ULL;
  470. rt_kprintf("found part[%d], begin(sector): %d, end(sector):%d size: ",
  471. pindex, _ptes[pindex].starting_lba, _ptes[pindex].ending_lba);
  472. if ((part->size >> 11) == 0)
  473. {
  474. rt_kprintf("%d%s", part->size >> 1, "KB\n"); /* KB */
  475. }
  476. else
  477. {
  478. unsigned int part_size;
  479. part_size = part->size >> 11; /* MB */
  480. if ((part_size >> 10) == 0)
  481. rt_kprintf("%d.%d%s", part_size, (part->size >> 1) & 0x3FF, "MB\n");
  482. else
  483. rt_kprintf("%d.%d%s", part_size >> 10, part_size & 0x3FF, "GB\n");
  484. }
  485. return 0;
  486. }
  487. void gpt_free(void)
  488. {
  489. rt_free(_ptes);
  490. rt_free(_gpt);
  491. }