partition.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <inttypes.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <common/debug.h>
  11. #include <common/tf_crc32.h>
  12. #include <drivers/io/io_storage.h>
  13. #include <drivers/partition/efi.h>
  14. #include <drivers/partition/partition.h>
  15. #include <drivers/partition/gpt.h>
  16. #include <drivers/partition/mbr.h>
  17. #include <plat/common/platform.h>
  18. static uint8_t mbr_sector[PLAT_PARTITION_BLOCK_SIZE];
  19. static partition_entry_list_t list;
  20. #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
  21. static void dump_entries(int num)
  22. {
  23. char name[EFI_NAMELEN];
  24. int i, j, len;
  25. VERBOSE("Partition table with %d entries:\n", num);
  26. for (i = 0; i < num; i++) {
  27. len = snprintf(name, EFI_NAMELEN, "%s", list.list[i].name);
  28. for (j = 0; j < EFI_NAMELEN - len - 1; j++) {
  29. name[len + j] = ' ';
  30. }
  31. name[EFI_NAMELEN - 1] = '\0';
  32. VERBOSE("%d: %s %" PRIx64 "-%" PRIx64 "\n", i + 1, name, list.list[i].start,
  33. list.list[i].start + list.list[i].length - 4);
  34. }
  35. }
  36. #else
  37. #define dump_entries(num) ((void)num)
  38. #endif
  39. /*
  40. * Load the first sector that carries MBR header.
  41. * The MBR boot signature should be always valid whether it's MBR or GPT.
  42. */
  43. static int load_mbr_header(uintptr_t image_handle, mbr_entry_t *mbr_entry)
  44. {
  45. size_t bytes_read;
  46. int result;
  47. mbr_entry_t tmp;
  48. assert(mbr_entry != NULL);
  49. /* MBR partition table is in LBA0. */
  50. result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET);
  51. if (result != 0) {
  52. VERBOSE("Failed to seek (%i)\n", result);
  53. return result;
  54. }
  55. result = io_read(image_handle, (uintptr_t)&mbr_sector,
  56. PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
  57. if ((result != 0) || (bytes_read != PLAT_PARTITION_BLOCK_SIZE)) {
  58. VERBOSE("Failed to read data (%i)\n", result);
  59. return result;
  60. }
  61. /* Check MBR boot signature. */
  62. if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
  63. (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
  64. VERBOSE("MBR boot signature failure\n");
  65. return -ENOENT;
  66. }
  67. memcpy(&tmp, mbr_sector + MBR_PRIMARY_ENTRY_OFFSET, sizeof(tmp));
  68. if (tmp.first_lba != 1) {
  69. VERBOSE("MBR header may have an invalid first LBA\n");
  70. return -EINVAL;
  71. }
  72. if ((tmp.sector_nums == 0) || (tmp.sector_nums == UINT32_MAX)) {
  73. VERBOSE("MBR header entry has an invalid number of sectors\n");
  74. return -EINVAL;
  75. }
  76. memcpy(mbr_entry, &tmp, sizeof(mbr_entry_t));
  77. return 0;
  78. }
  79. /*
  80. * Load GPT header and check the GPT signature and header CRC.
  81. * If partition numbers could be found, check & update it.
  82. */
  83. static int load_gpt_header(uintptr_t image_handle, size_t header_offset,
  84. gpt_header_t *header)
  85. {
  86. size_t bytes_read;
  87. int result;
  88. uint32_t header_crc, calc_crc;
  89. result = io_seek(image_handle, IO_SEEK_SET, header_offset);
  90. if (result != 0) {
  91. VERBOSE("Failed to seek into the GPT image at offset (%zu)\n",
  92. header_offset);
  93. return result;
  94. }
  95. result = io_read(image_handle, (uintptr_t)header,
  96. sizeof(gpt_header_t), &bytes_read);
  97. if ((result != 0) || (sizeof(gpt_header_t) != bytes_read)) {
  98. VERBOSE("GPT header read error(%i) or read mismatch occurred,"
  99. "expected(%zu) and actual(%zu)\n", result,
  100. sizeof(gpt_header_t), bytes_read);
  101. return result;
  102. }
  103. if (memcmp(header->signature, GPT_SIGNATURE,
  104. sizeof(header->signature)) != 0) {
  105. VERBOSE("GPT header signature failure\n");
  106. return -EINVAL;
  107. }
  108. /*
  109. * UEFI Spec 2.8 March 2019 Page 119: HeaderCRC32 value is
  110. * computed by setting this field to 0, and computing the
  111. * 32-bit CRC for HeaderSize bytes.
  112. */
  113. header_crc = header->header_crc;
  114. header->header_crc = 0U;
  115. calc_crc = tf_crc32(0U, (uint8_t *)header, sizeof(gpt_header_t));
  116. if (header_crc != calc_crc) {
  117. ERROR("Invalid GPT Header CRC: Expected 0x%x but got 0x%x.\n",
  118. header_crc, calc_crc);
  119. return -EINVAL;
  120. }
  121. header->header_crc = header_crc;
  122. /* partition numbers can't exceed PLAT_PARTITION_MAX_ENTRIES */
  123. list.entry_count = header->list_num;
  124. if (list.entry_count > PLAT_PARTITION_MAX_ENTRIES) {
  125. list.entry_count = PLAT_PARTITION_MAX_ENTRIES;
  126. }
  127. return 0;
  128. }
  129. /*
  130. * Load a single MBR entry based on details from MBR header.
  131. */
  132. static int load_mbr_entry(uintptr_t image_handle, mbr_entry_t *mbr_entry,
  133. int part_number)
  134. {
  135. size_t bytes_read;
  136. uintptr_t offset;
  137. int result;
  138. assert(mbr_entry != NULL);
  139. /* MBR partition table is in LBA0. */
  140. result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET);
  141. if (result != 0) {
  142. VERBOSE("Failed to seek (%i)\n", result);
  143. return result;
  144. }
  145. result = io_read(image_handle, (uintptr_t)&mbr_sector,
  146. PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
  147. if (result != 0) {
  148. VERBOSE("Failed to read data (%i)\n", result);
  149. return result;
  150. }
  151. /* Check MBR boot signature. */
  152. if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
  153. (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
  154. VERBOSE("MBR Entry boot signature failure\n");
  155. return -ENOENT;
  156. }
  157. offset = (uintptr_t)&mbr_sector +
  158. MBR_PRIMARY_ENTRY_OFFSET +
  159. MBR_PRIMARY_ENTRY_SIZE * part_number;
  160. memcpy(mbr_entry, (void *)offset, sizeof(mbr_entry_t));
  161. return 0;
  162. }
  163. /*
  164. * Load MBR entries based on max number of partition entries.
  165. */
  166. static int load_mbr_entries(uintptr_t image_handle)
  167. {
  168. mbr_entry_t mbr_entry;
  169. unsigned int i;
  170. list.entry_count = MBR_PRIMARY_ENTRY_NUMBER;
  171. for (i = 0U; i < list.entry_count; i++) {
  172. load_mbr_entry(image_handle, &mbr_entry, i);
  173. list.list[i].start = mbr_entry.first_lba * 512;
  174. list.list[i].length = mbr_entry.sector_nums * 512;
  175. list.list[i].name[0] = mbr_entry.type;
  176. }
  177. return 0;
  178. }
  179. /*
  180. * Try to read and load a single GPT entry.
  181. */
  182. static int load_gpt_entry(uintptr_t image_handle, gpt_entry_t *entry)
  183. {
  184. size_t bytes_read = 0U;
  185. int result;
  186. assert(entry != NULL);
  187. result = io_read(image_handle, (uintptr_t)entry, sizeof(gpt_entry_t),
  188. &bytes_read);
  189. if ((result != 0) || (sizeof(gpt_entry_t) != bytes_read)) {
  190. VERBOSE("GPT Entry read error(%i) or read mismatch occurred,"
  191. "expected(%zu) and actual(%zu)\n", result,
  192. sizeof(gpt_entry_t), bytes_read);
  193. return -EINVAL;
  194. }
  195. return result;
  196. }
  197. /*
  198. * Retrieve each entry in the partition table, parse the data from each
  199. * entry and store them in the list of partition table entries.
  200. */
  201. static int load_partition_gpt(uintptr_t image_handle, gpt_header_t header)
  202. {
  203. const signed long long gpt_entry_offset = LBA(header.part_lba);
  204. gpt_entry_t entry;
  205. int result;
  206. unsigned int i;
  207. uint32_t calc_crc = 0U;
  208. result = io_seek(image_handle, IO_SEEK_SET, gpt_entry_offset);
  209. if (result != 0) {
  210. VERBOSE("Failed to seek (%i), Failed loading GPT partition"
  211. "table entries\n", result);
  212. return result;
  213. }
  214. for (i = 0U; i < list.entry_count; i++) {
  215. result = load_gpt_entry(image_handle, &entry);
  216. if (result != 0) {
  217. VERBOSE("Failed to load gpt entry data(%u) error is (%i)\n",
  218. i, result);
  219. return result;
  220. }
  221. result = parse_gpt_entry(&entry, &list.list[i]);
  222. if (result != 0) {
  223. result = io_seek(image_handle, IO_SEEK_SET,
  224. (gpt_entry_offset + (i * sizeof(gpt_entry_t))));
  225. if (result != 0) {
  226. VERBOSE("Failed to seek (%i)\n", result);
  227. return result;
  228. }
  229. break;
  230. }
  231. /*
  232. * Calculate CRC of Partition entry array to compare with CRC
  233. * value in header
  234. */
  235. calc_crc = tf_crc32(calc_crc, (uint8_t *)&entry, sizeof(gpt_entry_t));
  236. }
  237. if (i == 0) {
  238. VERBOSE("No Valid GPT Entries found\n");
  239. return -EINVAL;
  240. }
  241. /*
  242. * Only records the valid partition number that is loaded from
  243. * partition table.
  244. */
  245. list.entry_count = i;
  246. dump_entries(list.entry_count);
  247. /*
  248. * If there are less valid entries than the possible number of entries
  249. * from the header, continue to load the partition entry table to
  250. * calculate the full CRC in order to check against the partition CRC
  251. * from the header for validation.
  252. */
  253. for (; i < header.list_num; i++) {
  254. result = load_gpt_entry(image_handle, &entry);
  255. if (result != 0) {
  256. VERBOSE("Failed to load gpt entry data(%u) error is (%i)\n",
  257. i, result);
  258. return result;
  259. }
  260. calc_crc = tf_crc32(calc_crc, (uint8_t *)&entry, sizeof(gpt_entry_t));
  261. }
  262. if (header.part_crc != calc_crc) {
  263. ERROR("Invalid GPT Partition Array Entry CRC: Expected 0x%x"
  264. " but got 0x%x.\n", header.part_crc, calc_crc);
  265. return -EINVAL;
  266. }
  267. return 0;
  268. }
  269. /*
  270. * Try retrieving and parsing the backup-GPT header and backup GPT entries.
  271. * Last 33 blocks contains the backup-GPT entries and header.
  272. */
  273. static int load_backup_gpt(unsigned int image_id, unsigned int sector_nums)
  274. {
  275. int result;
  276. gpt_header_t header;
  277. size_t gpt_header_offset;
  278. uintptr_t dev_handle, image_spec, image_handle;
  279. io_block_spec_t *block_spec;
  280. int part_num_entries;
  281. result = plat_get_image_source(image_id, &dev_handle, &image_spec);
  282. if (result != 0) {
  283. VERBOSE("Failed to obtain reference to image id=%u (%i)\n",
  284. image_id, result);
  285. return result;
  286. }
  287. block_spec = (io_block_spec_t *)image_spec;
  288. /*
  289. * We need to read 32 blocks of GPT entries and one block of GPT header
  290. * try mapping only last 33 last blocks from the image to read the
  291. * Backup-GPT header and its entries.
  292. */
  293. part_num_entries = (PLAT_PARTITION_MAX_ENTRIES / 4);
  294. /* Move the offset base to LBA-33 */
  295. block_spec->offset += LBA(sector_nums - part_num_entries);
  296. /*
  297. * Set length as LBA-33, 32 blocks of backup-GPT entries and one
  298. * block of backup-GPT header.
  299. */
  300. block_spec->length = LBA(part_num_entries + 1);
  301. result = io_open(dev_handle, image_spec, &image_handle);
  302. if (result != 0) {
  303. VERBOSE("Failed to access image id (%i)\n", result);
  304. return result;
  305. }
  306. INFO("Trying to retrieve back-up GPT header\n");
  307. /* Last block is backup-GPT header, after the end of GPT entries */
  308. gpt_header_offset = LBA(part_num_entries);
  309. result = load_gpt_header(image_handle, gpt_header_offset, &header);
  310. if ((result != 0) || (header.part_lba == 0)) {
  311. ERROR("Failed to retrieve Backup GPT header,"
  312. "Partition maybe corrupted\n");
  313. goto out;
  314. }
  315. /*
  316. * Note we mapped last 33 blocks(LBA-33), first block here starts with
  317. * entries while last block was header.
  318. */
  319. header.part_lba = 0;
  320. result = load_partition_gpt(image_handle, header);
  321. out:
  322. io_close(image_handle);
  323. return result;
  324. }
  325. /*
  326. * Load a GPT partition, Try retrieving and parsing the primary GPT header,
  327. * if its corrupted try loading backup GPT header and then retrieve list
  328. * of partition table entries found from the GPT.
  329. */
  330. static int load_primary_gpt(uintptr_t image_handle, unsigned int first_lba)
  331. {
  332. int result;
  333. size_t gpt_header_offset;
  334. gpt_header_t header;
  335. /* Try to load Primary GPT header from LBA1 */
  336. gpt_header_offset = LBA(first_lba);
  337. result = load_gpt_header(image_handle, gpt_header_offset, &header);
  338. if ((result != 0) || (header.part_lba == 0)) {
  339. VERBOSE("Failed to retrieve Primary GPT header,"
  340. "trying to retrieve back-up GPT header\n");
  341. return result;
  342. }
  343. return load_partition_gpt(image_handle, header);
  344. }
  345. /*
  346. * Load the partition table info based on the image id provided.
  347. */
  348. int load_partition_table(unsigned int image_id)
  349. {
  350. uintptr_t dev_handle, image_handle, image_spec = 0;
  351. mbr_entry_t mbr_entry;
  352. int result;
  353. result = plat_get_image_source(image_id, &dev_handle, &image_spec);
  354. if (result != 0) {
  355. VERBOSE("Failed to obtain reference to image id=%u (%i)\n",
  356. image_id, result);
  357. return result;
  358. }
  359. result = io_open(dev_handle, image_spec, &image_handle);
  360. if (result != 0) {
  361. VERBOSE("Failed to access image id=%u (%i)\n", image_id, result);
  362. return result;
  363. }
  364. result = load_mbr_header(image_handle, &mbr_entry);
  365. if (result != 0) {
  366. VERBOSE("Failed to access image id=%u (%i)\n", image_id, result);
  367. goto out;
  368. }
  369. if (mbr_entry.type == PARTITION_TYPE_GPT) {
  370. result = load_primary_gpt(image_handle, mbr_entry.first_lba);
  371. if (result != 0) {
  372. io_close(image_handle);
  373. return load_backup_gpt(BKUP_GPT_IMAGE_ID,
  374. mbr_entry.sector_nums);
  375. }
  376. } else {
  377. result = load_mbr_entries(image_handle);
  378. }
  379. out:
  380. io_close(image_handle);
  381. return result;
  382. }
  383. /*
  384. * Try retrieving a partition table entry based on the name of the partition.
  385. */
  386. const partition_entry_t *get_partition_entry(const char *name)
  387. {
  388. unsigned int i;
  389. for (i = 0U; i < list.entry_count; i++) {
  390. if (strcmp(name, list.list[i].name) == 0) {
  391. return &list.list[i];
  392. }
  393. }
  394. return NULL;
  395. }
  396. /*
  397. * Try retrieving a partition table entry based on the partition type GUID.
  398. */
  399. const partition_entry_t *get_partition_entry_by_type(
  400. const struct efi_guid *type_guid)
  401. {
  402. unsigned int i;
  403. for (i = 0U; i < list.entry_count; i++) {
  404. if (guidcmp(type_guid, &list.list[i].type_guid) == 0) {
  405. return &list.list[i];
  406. }
  407. }
  408. return NULL;
  409. }
  410. /*
  411. * Try retrieving a partition table entry based on the unique partition GUID.
  412. */
  413. const partition_entry_t *get_partition_entry_by_guid(
  414. const struct efi_guid *part_guid)
  415. {
  416. unsigned int i;
  417. for (i = 0U; i < list.entry_count; i++) {
  418. if (guidcmp(part_guid, &list.list[i].part_guid) == 0) {
  419. return &list.list[i];
  420. }
  421. }
  422. return NULL;
  423. }
  424. /*
  425. * Return entry to the list of partition table entries.
  426. */
  427. const partition_entry_list_t *get_partition_entry_list(void)
  428. {
  429. return &list;
  430. }
  431. /*
  432. * Try loading partition table info for the given image ID.
  433. */
  434. void partition_init(unsigned int image_id)
  435. {
  436. int ret;
  437. ret = load_partition_table(image_id);
  438. if (ret != 0) {
  439. ERROR("Failed to parse partition with image id = %u\n",
  440. image_id);
  441. }
  442. }
  443. /*
  444. * Load a GPT based image.
  445. */
  446. int gpt_partition_init(void)
  447. {
  448. return load_partition_table(GPT_IMAGE_ID);
  449. }