qemu_io_storage.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <platform_def.h>
  9. #include <common/bl_common.h>
  10. #include <common/debug.h>
  11. #include <common/desc_image_load.h>
  12. #include <common/uuid.h>
  13. #include <drivers/io/io_driver.h>
  14. #include <drivers/io/io_encrypted.h>
  15. #include <drivers/io/io_fip.h>
  16. #include <drivers/io/io_memmap.h>
  17. #include <drivers/io/io_semihosting.h>
  18. #include <drivers/io/io_storage.h>
  19. #include <lib/semihosting.h>
  20. #include <tools_share/firmware_image_package.h>
  21. #include "qemu_private.h"
  22. /* Semihosting filenames */
  23. #define BL2_IMAGE_NAME "bl2.bin"
  24. #define BL31_IMAGE_NAME "bl31.bin"
  25. #define BL32_IMAGE_NAME "bl32.bin"
  26. #define TB_FW_CONFIG_NAME "tb_fw_config.dtb"
  27. #define TOS_FW_CONFIG_NAME "tos_fw_config.dtb"
  28. #define BL32_EXTRA1_IMAGE_NAME "bl32_extra1.bin"
  29. #define BL32_EXTRA2_IMAGE_NAME "bl32_extra2.bin"
  30. #define BL33_IMAGE_NAME "bl33.bin"
  31. #define RMM_IMAGE_NAME "rmm.bin"
  32. #if TRUSTED_BOARD_BOOT
  33. #define TRUSTED_BOOT_FW_CERT_NAME "tb_fw.crt"
  34. #define TRUSTED_KEY_CERT_NAME "trusted_key.crt"
  35. #define SOC_FW_KEY_CERT_NAME "soc_fw_key.crt"
  36. #define TOS_FW_KEY_CERT_NAME "tos_fw_key.crt"
  37. #define NT_FW_KEY_CERT_NAME "nt_fw_key.crt"
  38. #define SOC_FW_CONTENT_CERT_NAME "soc_fw_content.crt"
  39. #define TOS_FW_CONTENT_CERT_NAME "tos_fw_content.crt"
  40. #define NT_FW_CONTENT_CERT_NAME "nt_fw_content.crt"
  41. #endif /* TRUSTED_BOARD_BOOT */
  42. /* IO devices */
  43. static const io_dev_connector_t *fip_dev_con;
  44. static uintptr_t fip_dev_handle;
  45. static const io_dev_connector_t *memmap_dev_con;
  46. static uintptr_t memmap_dev_handle;
  47. static const io_dev_connector_t *sh_dev_con;
  48. static uintptr_t sh_dev_handle;
  49. #ifndef DECRYPTION_SUPPORT_none
  50. static const io_dev_connector_t *enc_dev_con;
  51. static uintptr_t enc_dev_handle;
  52. #endif
  53. static const io_block_spec_t fip_block_spec = {
  54. .offset = PLAT_QEMU_FIP_BASE,
  55. .length = PLAT_QEMU_FIP_MAX_SIZE
  56. };
  57. static const io_uuid_spec_t bl2_uuid_spec = {
  58. .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
  59. };
  60. static const io_uuid_spec_t bl31_uuid_spec = {
  61. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  62. };
  63. static const io_uuid_spec_t bl32_uuid_spec = {
  64. .uuid = UUID_SECURE_PAYLOAD_BL32,
  65. };
  66. static const io_uuid_spec_t bl32_extra1_uuid_spec = {
  67. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
  68. };
  69. static const io_uuid_spec_t bl32_extra2_uuid_spec = {
  70. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
  71. };
  72. static const io_uuid_spec_t tb_fw_config_uuid_spec = {
  73. .uuid = UUID_TB_FW_CONFIG,
  74. };
  75. static const io_uuid_spec_t tos_fw_config_uuid_spec = {
  76. .uuid = UUID_TOS_FW_CONFIG,
  77. };
  78. static const io_uuid_spec_t bl33_uuid_spec = {
  79. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  80. };
  81. static const io_uuid_spec_t rmm_uuid_spec = {
  82. .uuid = UUID_REALM_MONITOR_MGMT_FIRMWARE,
  83. };
  84. #if TRUSTED_BOARD_BOOT
  85. static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
  86. .uuid = UUID_TRUSTED_BOOT_FW_CERT,
  87. };
  88. static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
  89. .uuid = UUID_TRUSTED_KEY_CERT,
  90. };
  91. static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
  92. .uuid = UUID_SOC_FW_KEY_CERT,
  93. };
  94. static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
  95. .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
  96. };
  97. static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
  98. .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
  99. };
  100. static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
  101. .uuid = UUID_SOC_FW_CONTENT_CERT,
  102. };
  103. static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
  104. .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
  105. };
  106. static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
  107. .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
  108. };
  109. #endif /* TRUSTED_BOARD_BOOT */
  110. static const io_file_spec_t sh_file_spec[] = {
  111. [BL2_IMAGE_ID] = {
  112. .path = BL2_IMAGE_NAME,
  113. .mode = FOPEN_MODE_RB
  114. },
  115. [BL31_IMAGE_ID] = {
  116. .path = BL31_IMAGE_NAME,
  117. .mode = FOPEN_MODE_RB
  118. },
  119. [BL32_IMAGE_ID] = {
  120. .path = BL32_IMAGE_NAME,
  121. .mode = FOPEN_MODE_RB
  122. },
  123. [BL32_EXTRA1_IMAGE_ID] = {
  124. .path = BL32_EXTRA1_IMAGE_NAME,
  125. .mode = FOPEN_MODE_RB
  126. },
  127. [BL32_EXTRA2_IMAGE_ID] = {
  128. .path = BL32_EXTRA2_IMAGE_NAME,
  129. .mode = FOPEN_MODE_RB
  130. },
  131. [TB_FW_CONFIG_ID] = {
  132. .path = TB_FW_CONFIG_NAME,
  133. .mode = FOPEN_MODE_RB
  134. },
  135. [TOS_FW_CONFIG_ID] = {
  136. .path = TOS_FW_CONFIG_NAME,
  137. .mode = FOPEN_MODE_RB
  138. },
  139. [BL33_IMAGE_ID] = {
  140. .path = BL33_IMAGE_NAME,
  141. .mode = FOPEN_MODE_RB
  142. },
  143. [RMM_IMAGE_ID] = {
  144. .path = RMM_IMAGE_NAME,
  145. .mode = FOPEN_MODE_RB
  146. },
  147. #if TRUSTED_BOARD_BOOT
  148. [TRUSTED_BOOT_FW_CERT_ID] = {
  149. .path = TRUSTED_BOOT_FW_CERT_NAME,
  150. .mode = FOPEN_MODE_RB
  151. },
  152. [TRUSTED_KEY_CERT_ID] = {
  153. .path = TRUSTED_KEY_CERT_NAME,
  154. .mode = FOPEN_MODE_RB
  155. },
  156. [SOC_FW_KEY_CERT_ID] = {
  157. .path = SOC_FW_KEY_CERT_NAME,
  158. .mode = FOPEN_MODE_RB
  159. },
  160. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  161. .path = TOS_FW_KEY_CERT_NAME,
  162. .mode = FOPEN_MODE_RB
  163. },
  164. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  165. .path = NT_FW_KEY_CERT_NAME,
  166. .mode = FOPEN_MODE_RB
  167. },
  168. [SOC_FW_CONTENT_CERT_ID] = {
  169. .path = SOC_FW_CONTENT_CERT_NAME,
  170. .mode = FOPEN_MODE_RB
  171. },
  172. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  173. .path = TOS_FW_CONTENT_CERT_NAME,
  174. .mode = FOPEN_MODE_RB
  175. },
  176. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  177. .path = NT_FW_CONTENT_CERT_NAME,
  178. .mode = FOPEN_MODE_RB
  179. },
  180. #endif /* TRUSTED_BOARD_BOOT */
  181. };
  182. static int open_fip(const uintptr_t spec);
  183. static int open_memmap(const uintptr_t spec);
  184. #ifndef DECRYPTION_SUPPORT_none
  185. static int open_enc_fip(const uintptr_t spec);
  186. #endif
  187. struct plat_io_policy {
  188. uintptr_t *dev_handle;
  189. uintptr_t image_spec;
  190. int (*check)(const uintptr_t spec);
  191. };
  192. /* By default, ARM platforms load images from the FIP */
  193. static const struct plat_io_policy policies[] = {
  194. [FIP_IMAGE_ID] = {
  195. &memmap_dev_handle,
  196. (uintptr_t)&fip_block_spec,
  197. open_memmap
  198. },
  199. [ENC_IMAGE_ID] = {
  200. &fip_dev_handle,
  201. (uintptr_t)NULL,
  202. open_fip
  203. },
  204. [BL2_IMAGE_ID] = {
  205. &fip_dev_handle,
  206. (uintptr_t)&bl2_uuid_spec,
  207. open_fip
  208. },
  209. #if ENCRYPT_BL31 && !defined(DECRYPTION_SUPPORT_none)
  210. [BL31_IMAGE_ID] = {
  211. &enc_dev_handle,
  212. (uintptr_t)&bl31_uuid_spec,
  213. open_enc_fip
  214. },
  215. #else
  216. [BL31_IMAGE_ID] = {
  217. &fip_dev_handle,
  218. (uintptr_t)&bl31_uuid_spec,
  219. open_fip
  220. },
  221. #endif
  222. #if ENCRYPT_BL32 && !defined(DECRYPTION_SUPPORT_none)
  223. [BL32_IMAGE_ID] = {
  224. &enc_dev_handle,
  225. (uintptr_t)&bl32_uuid_spec,
  226. open_enc_fip
  227. },
  228. [BL32_EXTRA1_IMAGE_ID] = {
  229. &enc_dev_handle,
  230. (uintptr_t)&bl32_extra1_uuid_spec,
  231. open_enc_fip
  232. },
  233. [BL32_EXTRA2_IMAGE_ID] = {
  234. &enc_dev_handle,
  235. (uintptr_t)&bl32_extra2_uuid_spec,
  236. open_enc_fip
  237. },
  238. #else
  239. [BL32_IMAGE_ID] = {
  240. &fip_dev_handle,
  241. (uintptr_t)&bl32_uuid_spec,
  242. open_fip
  243. },
  244. [BL32_EXTRA1_IMAGE_ID] = {
  245. &fip_dev_handle,
  246. (uintptr_t)&bl32_extra1_uuid_spec,
  247. open_fip
  248. },
  249. [BL32_EXTRA2_IMAGE_ID] = {
  250. &fip_dev_handle,
  251. (uintptr_t)&bl32_extra2_uuid_spec,
  252. open_fip
  253. },
  254. #endif
  255. [TB_FW_CONFIG_ID] = {
  256. &fip_dev_handle,
  257. (uintptr_t)&tb_fw_config_uuid_spec,
  258. open_fip
  259. },
  260. [TOS_FW_CONFIG_ID] = {
  261. &fip_dev_handle,
  262. (uintptr_t)&tos_fw_config_uuid_spec,
  263. open_fip
  264. },
  265. [BL33_IMAGE_ID] = {
  266. &fip_dev_handle,
  267. (uintptr_t)&bl33_uuid_spec,
  268. open_fip
  269. },
  270. [RMM_IMAGE_ID] = {
  271. &fip_dev_handle,
  272. (uintptr_t)&rmm_uuid_spec,
  273. open_fip
  274. },
  275. #if TRUSTED_BOARD_BOOT
  276. [TRUSTED_BOOT_FW_CERT_ID] = {
  277. &fip_dev_handle,
  278. (uintptr_t)&tb_fw_cert_uuid_spec,
  279. open_fip
  280. },
  281. [TRUSTED_KEY_CERT_ID] = {
  282. &fip_dev_handle,
  283. (uintptr_t)&trusted_key_cert_uuid_spec,
  284. open_fip
  285. },
  286. [SOC_FW_KEY_CERT_ID] = {
  287. &fip_dev_handle,
  288. (uintptr_t)&soc_fw_key_cert_uuid_spec,
  289. open_fip
  290. },
  291. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  292. &fip_dev_handle,
  293. (uintptr_t)&tos_fw_key_cert_uuid_spec,
  294. open_fip
  295. },
  296. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  297. &fip_dev_handle,
  298. (uintptr_t)&nt_fw_key_cert_uuid_spec,
  299. open_fip
  300. },
  301. [SOC_FW_CONTENT_CERT_ID] = {
  302. &fip_dev_handle,
  303. (uintptr_t)&soc_fw_cert_uuid_spec,
  304. open_fip
  305. },
  306. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  307. &fip_dev_handle,
  308. (uintptr_t)&tos_fw_cert_uuid_spec,
  309. open_fip
  310. },
  311. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  312. &fip_dev_handle,
  313. (uintptr_t)&nt_fw_cert_uuid_spec,
  314. open_fip
  315. },
  316. #endif /* TRUSTED_BOARD_BOOT */
  317. };
  318. #if defined(SPD_spmd)
  319. static struct sp_pkg {
  320. struct plat_io_policy policy;
  321. io_file_spec_t sh_file_spec;
  322. uint8_t uuid[UUID_BYTES_LENGTH];
  323. char path[80];
  324. } sp_pkgs[MAX_SP_IDS];
  325. static unsigned int sp_pkg_count;
  326. int qemu_io_register_sp_pkg(const char *name, const char *uuid,
  327. uintptr_t load_addr)
  328. {
  329. struct sp_pkg *pkg;
  330. bl_mem_params_node_t *mem_params;
  331. if (sp_pkg_count == MAX_SP_IDS) {
  332. INFO("Reached Max number of SPs\n");
  333. return -1;
  334. }
  335. mem_params = get_bl_mem_params_node(SP_PKG1_ID + sp_pkg_count);
  336. if (mem_params == NULL) {
  337. ERROR("Can't find SP_PKG ID %u (SP_PKG%u_ID)\n",
  338. SP_PKG1_ID + sp_pkg_count, sp_pkg_count);
  339. return -1;
  340. }
  341. pkg = sp_pkgs + sp_pkg_count;
  342. if (read_uuid(pkg->uuid, (char *)uuid)) {
  343. return -1;
  344. }
  345. strlcpy(pkg->path, name, sizeof(pkg->path));
  346. strlcat(pkg->path, ".pkg", sizeof(pkg->path));
  347. pkg->policy.dev_handle = &fip_dev_handle;
  348. pkg->policy.image_spec = (uintptr_t)&pkg->uuid;
  349. pkg->policy.check = open_fip;
  350. pkg->sh_file_spec.path = pkg->path;
  351. pkg->sh_file_spec.mode = FOPEN_MODE_RB;
  352. mem_params->image_info.image_base = load_addr;
  353. mem_params->image_info.image_max_size = SZ_4M;
  354. mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
  355. sp_pkg_count++;
  356. return 0;
  357. }
  358. #endif /*SPD_spmd*/
  359. static const io_file_spec_t *get_io_file_spec(unsigned int image_id)
  360. {
  361. #if defined(SPD_spmd)
  362. if (image_id >= SP_PKG1_ID && image_id <= SP_PKG8_ID) {
  363. return &sp_pkgs[image_id - SP_PKG1_ID].sh_file_spec;
  364. }
  365. #endif
  366. assert(image_id < ARRAY_SIZE(sh_file_spec));
  367. return &sh_file_spec[image_id];
  368. }
  369. static const struct plat_io_policy *get_io_policy(unsigned int image_id)
  370. {
  371. #if defined(SPD_spmd)
  372. if (image_id >= SP_PKG1_ID && image_id <= SP_PKG8_ID) {
  373. return &sp_pkgs[image_id - SP_PKG1_ID].policy;
  374. }
  375. #endif
  376. assert(image_id < ARRAY_SIZE(policies));
  377. return &policies[image_id];
  378. }
  379. static int open_fip(const uintptr_t spec)
  380. {
  381. int result;
  382. uintptr_t local_image_handle;
  383. /* See if a Firmware Image Package is available */
  384. result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  385. if (result == 0 && spec != (uintptr_t)NULL) {
  386. result = io_open(fip_dev_handle, spec, &local_image_handle);
  387. if (result == 0) {
  388. VERBOSE("Using FIP\n");
  389. io_close(local_image_handle);
  390. }
  391. }
  392. return result;
  393. }
  394. #ifndef DECRYPTION_SUPPORT_none
  395. static int open_enc_fip(const uintptr_t spec)
  396. {
  397. int result;
  398. uintptr_t local_image_handle;
  399. /* See if an encrypted FIP is available */
  400. result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID);
  401. if (result == 0) {
  402. result = io_open(enc_dev_handle, spec, &local_image_handle);
  403. if (result == 0) {
  404. VERBOSE("Using encrypted FIP\n");
  405. io_close(local_image_handle);
  406. }
  407. }
  408. return result;
  409. }
  410. #endif
  411. static int open_memmap(const uintptr_t spec)
  412. {
  413. int result;
  414. uintptr_t local_image_handle;
  415. result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
  416. if (result == 0) {
  417. result = io_open(memmap_dev_handle, spec, &local_image_handle);
  418. if (result == 0) {
  419. VERBOSE("Using Memmap\n");
  420. io_close(local_image_handle);
  421. }
  422. }
  423. return result;
  424. }
  425. static int open_semihosting(const uintptr_t spec)
  426. {
  427. int result;
  428. uintptr_t local_image_handle;
  429. /* See if the file exists on semi-hosting.*/
  430. result = io_dev_init(sh_dev_handle, (uintptr_t)NULL);
  431. if (result == 0) {
  432. result = io_open(sh_dev_handle, spec, &local_image_handle);
  433. if (result == 0) {
  434. VERBOSE("Using Semi-hosting IO\n");
  435. io_close(local_image_handle);
  436. }
  437. }
  438. return result;
  439. }
  440. void plat_qemu_io_setup(void)
  441. {
  442. int io_result;
  443. io_result = register_io_dev_fip(&fip_dev_con);
  444. assert(io_result == 0);
  445. io_result = register_io_dev_memmap(&memmap_dev_con);
  446. assert(io_result == 0);
  447. /* Open connections to devices and cache the handles */
  448. io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
  449. &fip_dev_handle);
  450. assert(io_result == 0);
  451. io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
  452. &memmap_dev_handle);
  453. assert(io_result == 0);
  454. #ifndef DECRYPTION_SUPPORT_none
  455. io_result = register_io_dev_enc(&enc_dev_con);
  456. assert(io_result == 0);
  457. io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL,
  458. &enc_dev_handle);
  459. assert(io_result == 0);
  460. #endif
  461. /* Register the additional IO devices on this platform */
  462. io_result = register_io_dev_sh(&sh_dev_con);
  463. assert(io_result == 0);
  464. /* Open connections to devices and cache the handles */
  465. io_result = io_dev_open(sh_dev_con, (uintptr_t)NULL, &sh_dev_handle);
  466. assert(io_result == 0);
  467. /* Ignore improbable errors in release builds */
  468. (void)io_result;
  469. }
  470. static int get_alt_image_source(unsigned int image_id, uintptr_t *dev_handle,
  471. uintptr_t *image_spec)
  472. {
  473. const io_file_spec_t *spec = get_io_file_spec(image_id);
  474. int result;
  475. result = open_semihosting((const uintptr_t)spec);
  476. if (result == 0) {
  477. *dev_handle = sh_dev_handle;
  478. *image_spec = (uintptr_t)spec;
  479. }
  480. return result;
  481. }
  482. /*
  483. * Return an IO device handle and specification which can be used to access
  484. * an image. Use this to enforce platform load policy
  485. */
  486. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  487. uintptr_t *image_spec)
  488. {
  489. const struct plat_io_policy *policy = get_io_policy(image_id);
  490. int result;
  491. result = policy->check(policy->image_spec);
  492. if (result == 0) {
  493. *image_spec = policy->image_spec;
  494. *dev_handle = *(policy->dev_handle);
  495. } else {
  496. VERBOSE("Trying alternative IO\n");
  497. result = get_alt_image_source(image_id, dev_handle, image_spec);
  498. }
  499. return result;
  500. }