qemu_io_storage.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright (c) 2015-2016, 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 <drivers/io/io_driver.h>
  12. #include <drivers/io/io_encrypted.h>
  13. #include <drivers/io/io_fip.h>
  14. #include <drivers/io/io_memmap.h>
  15. #include <drivers/io/io_semihosting.h>
  16. #include <drivers/io/io_storage.h>
  17. #include <lib/semihosting.h>
  18. #include <tools_share/firmware_image_package.h>
  19. /* Semihosting filenames */
  20. #define BL2_IMAGE_NAME "bl2.bin"
  21. #define BL31_IMAGE_NAME "bl31.bin"
  22. #define BL32_IMAGE_NAME "bl32.bin"
  23. #define BL32_EXTRA1_IMAGE_NAME "bl32_extra1.bin"
  24. #define BL32_EXTRA2_IMAGE_NAME "bl32_extra2.bin"
  25. #define BL33_IMAGE_NAME "bl33.bin"
  26. #if TRUSTED_BOARD_BOOT
  27. #define TRUSTED_BOOT_FW_CERT_NAME "tb_fw.crt"
  28. #define TRUSTED_KEY_CERT_NAME "trusted_key.crt"
  29. #define SOC_FW_KEY_CERT_NAME "soc_fw_key.crt"
  30. #define TOS_FW_KEY_CERT_NAME "tos_fw_key.crt"
  31. #define NT_FW_KEY_CERT_NAME "nt_fw_key.crt"
  32. #define SOC_FW_CONTENT_CERT_NAME "soc_fw_content.crt"
  33. #define TOS_FW_CONTENT_CERT_NAME "tos_fw_content.crt"
  34. #define NT_FW_CONTENT_CERT_NAME "nt_fw_content.crt"
  35. #endif /* TRUSTED_BOARD_BOOT */
  36. /* IO devices */
  37. static const io_dev_connector_t *fip_dev_con;
  38. static uintptr_t fip_dev_handle;
  39. static const io_dev_connector_t *memmap_dev_con;
  40. static uintptr_t memmap_dev_handle;
  41. static const io_dev_connector_t *sh_dev_con;
  42. static uintptr_t sh_dev_handle;
  43. #ifndef DECRYPTION_SUPPORT_none
  44. static const io_dev_connector_t *enc_dev_con;
  45. static uintptr_t enc_dev_handle;
  46. #endif
  47. static const io_block_spec_t fip_block_spec = {
  48. .offset = PLAT_QEMU_FIP_BASE,
  49. .length = PLAT_QEMU_FIP_MAX_SIZE
  50. };
  51. static const io_uuid_spec_t bl2_uuid_spec = {
  52. .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
  53. };
  54. static const io_uuid_spec_t bl31_uuid_spec = {
  55. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  56. };
  57. static const io_uuid_spec_t bl32_uuid_spec = {
  58. .uuid = UUID_SECURE_PAYLOAD_BL32,
  59. };
  60. static const io_uuid_spec_t bl32_extra1_uuid_spec = {
  61. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
  62. };
  63. static const io_uuid_spec_t bl32_extra2_uuid_spec = {
  64. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
  65. };
  66. static const io_uuid_spec_t bl33_uuid_spec = {
  67. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  68. };
  69. #if TRUSTED_BOARD_BOOT
  70. static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
  71. .uuid = UUID_TRUSTED_BOOT_FW_CERT,
  72. };
  73. static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
  74. .uuid = UUID_TRUSTED_KEY_CERT,
  75. };
  76. static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
  77. .uuid = UUID_SOC_FW_KEY_CERT,
  78. };
  79. static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
  80. .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
  81. };
  82. static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
  83. .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
  84. };
  85. static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
  86. .uuid = UUID_SOC_FW_CONTENT_CERT,
  87. };
  88. static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
  89. .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
  90. };
  91. static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
  92. .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
  93. };
  94. #endif /* TRUSTED_BOARD_BOOT */
  95. static const io_file_spec_t sh_file_spec[] = {
  96. [BL2_IMAGE_ID] = {
  97. .path = BL2_IMAGE_NAME,
  98. .mode = FOPEN_MODE_RB
  99. },
  100. [BL31_IMAGE_ID] = {
  101. .path = BL31_IMAGE_NAME,
  102. .mode = FOPEN_MODE_RB
  103. },
  104. [BL32_IMAGE_ID] = {
  105. .path = BL32_IMAGE_NAME,
  106. .mode = FOPEN_MODE_RB
  107. },
  108. [BL32_EXTRA1_IMAGE_ID] = {
  109. .path = BL32_EXTRA1_IMAGE_NAME,
  110. .mode = FOPEN_MODE_RB
  111. },
  112. [BL32_EXTRA2_IMAGE_ID] = {
  113. .path = BL32_EXTRA2_IMAGE_NAME,
  114. .mode = FOPEN_MODE_RB
  115. },
  116. [BL33_IMAGE_ID] = {
  117. .path = BL33_IMAGE_NAME,
  118. .mode = FOPEN_MODE_RB
  119. },
  120. #if TRUSTED_BOARD_BOOT
  121. [TRUSTED_BOOT_FW_CERT_ID] = {
  122. .path = TRUSTED_BOOT_FW_CERT_NAME,
  123. .mode = FOPEN_MODE_RB
  124. },
  125. [TRUSTED_KEY_CERT_ID] = {
  126. .path = TRUSTED_KEY_CERT_NAME,
  127. .mode = FOPEN_MODE_RB
  128. },
  129. [SOC_FW_KEY_CERT_ID] = {
  130. .path = SOC_FW_KEY_CERT_NAME,
  131. .mode = FOPEN_MODE_RB
  132. },
  133. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  134. .path = TOS_FW_KEY_CERT_NAME,
  135. .mode = FOPEN_MODE_RB
  136. },
  137. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  138. .path = NT_FW_KEY_CERT_NAME,
  139. .mode = FOPEN_MODE_RB
  140. },
  141. [SOC_FW_CONTENT_CERT_ID] = {
  142. .path = SOC_FW_CONTENT_CERT_NAME,
  143. .mode = FOPEN_MODE_RB
  144. },
  145. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  146. .path = TOS_FW_CONTENT_CERT_NAME,
  147. .mode = FOPEN_MODE_RB
  148. },
  149. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  150. .path = NT_FW_CONTENT_CERT_NAME,
  151. .mode = FOPEN_MODE_RB
  152. },
  153. #endif /* TRUSTED_BOARD_BOOT */
  154. };
  155. static int open_fip(const uintptr_t spec);
  156. static int open_memmap(const uintptr_t spec);
  157. #ifndef DECRYPTION_SUPPORT_none
  158. static int open_enc_fip(const uintptr_t spec);
  159. #endif
  160. struct plat_io_policy {
  161. uintptr_t *dev_handle;
  162. uintptr_t image_spec;
  163. int (*check)(const uintptr_t spec);
  164. };
  165. /* By default, ARM platforms load images from the FIP */
  166. static const struct plat_io_policy policies[] = {
  167. [FIP_IMAGE_ID] = {
  168. &memmap_dev_handle,
  169. (uintptr_t)&fip_block_spec,
  170. open_memmap
  171. },
  172. [ENC_IMAGE_ID] = {
  173. &fip_dev_handle,
  174. (uintptr_t)NULL,
  175. open_fip
  176. },
  177. [BL2_IMAGE_ID] = {
  178. &fip_dev_handle,
  179. (uintptr_t)&bl2_uuid_spec,
  180. open_fip
  181. },
  182. #if ENCRYPT_BL31 && !defined(DECRYPTION_SUPPORT_none)
  183. [BL31_IMAGE_ID] = {
  184. &enc_dev_handle,
  185. (uintptr_t)&bl31_uuid_spec,
  186. open_enc_fip
  187. },
  188. #else
  189. [BL31_IMAGE_ID] = {
  190. &fip_dev_handle,
  191. (uintptr_t)&bl31_uuid_spec,
  192. open_fip
  193. },
  194. #endif
  195. #if ENCRYPT_BL32 && !defined(DECRYPTION_SUPPORT_none)
  196. [BL32_IMAGE_ID] = {
  197. &enc_dev_handle,
  198. (uintptr_t)&bl32_uuid_spec,
  199. open_enc_fip
  200. },
  201. [BL32_EXTRA1_IMAGE_ID] = {
  202. &enc_dev_handle,
  203. (uintptr_t)&bl32_extra1_uuid_spec,
  204. open_enc_fip
  205. },
  206. [BL32_EXTRA2_IMAGE_ID] = {
  207. &enc_dev_handle,
  208. (uintptr_t)&bl32_extra2_uuid_spec,
  209. open_enc_fip
  210. },
  211. #else
  212. [BL32_IMAGE_ID] = {
  213. &fip_dev_handle,
  214. (uintptr_t)&bl32_uuid_spec,
  215. open_fip
  216. },
  217. [BL32_EXTRA1_IMAGE_ID] = {
  218. &fip_dev_handle,
  219. (uintptr_t)&bl32_extra1_uuid_spec,
  220. open_fip
  221. },
  222. [BL32_EXTRA2_IMAGE_ID] = {
  223. &fip_dev_handle,
  224. (uintptr_t)&bl32_extra2_uuid_spec,
  225. open_fip
  226. },
  227. #endif
  228. [BL33_IMAGE_ID] = {
  229. &fip_dev_handle,
  230. (uintptr_t)&bl33_uuid_spec,
  231. open_fip
  232. },
  233. #if TRUSTED_BOARD_BOOT
  234. [TRUSTED_BOOT_FW_CERT_ID] = {
  235. &fip_dev_handle,
  236. (uintptr_t)&tb_fw_cert_uuid_spec,
  237. open_fip
  238. },
  239. [TRUSTED_KEY_CERT_ID] = {
  240. &fip_dev_handle,
  241. (uintptr_t)&trusted_key_cert_uuid_spec,
  242. open_fip
  243. },
  244. [SOC_FW_KEY_CERT_ID] = {
  245. &fip_dev_handle,
  246. (uintptr_t)&soc_fw_key_cert_uuid_spec,
  247. open_fip
  248. },
  249. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  250. &fip_dev_handle,
  251. (uintptr_t)&tos_fw_key_cert_uuid_spec,
  252. open_fip
  253. },
  254. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  255. &fip_dev_handle,
  256. (uintptr_t)&nt_fw_key_cert_uuid_spec,
  257. open_fip
  258. },
  259. [SOC_FW_CONTENT_CERT_ID] = {
  260. &fip_dev_handle,
  261. (uintptr_t)&soc_fw_cert_uuid_spec,
  262. open_fip
  263. },
  264. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  265. &fip_dev_handle,
  266. (uintptr_t)&tos_fw_cert_uuid_spec,
  267. open_fip
  268. },
  269. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  270. &fip_dev_handle,
  271. (uintptr_t)&nt_fw_cert_uuid_spec,
  272. open_fip
  273. },
  274. #endif /* TRUSTED_BOARD_BOOT */
  275. };
  276. static int open_fip(const uintptr_t spec)
  277. {
  278. int result;
  279. uintptr_t local_image_handle;
  280. /* See if a Firmware Image Package is available */
  281. result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  282. if (result == 0 && spec != (uintptr_t)NULL) {
  283. result = io_open(fip_dev_handle, spec, &local_image_handle);
  284. if (result == 0) {
  285. VERBOSE("Using FIP\n");
  286. io_close(local_image_handle);
  287. }
  288. }
  289. return result;
  290. }
  291. #ifndef DECRYPTION_SUPPORT_none
  292. static int open_enc_fip(const uintptr_t spec)
  293. {
  294. int result;
  295. uintptr_t local_image_handle;
  296. /* See if an encrypted FIP is available */
  297. result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID);
  298. if (result == 0) {
  299. result = io_open(enc_dev_handle, spec, &local_image_handle);
  300. if (result == 0) {
  301. VERBOSE("Using encrypted FIP\n");
  302. io_close(local_image_handle);
  303. }
  304. }
  305. return result;
  306. }
  307. #endif
  308. static int open_memmap(const uintptr_t spec)
  309. {
  310. int result;
  311. uintptr_t local_image_handle;
  312. result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
  313. if (result == 0) {
  314. result = io_open(memmap_dev_handle, spec, &local_image_handle);
  315. if (result == 0) {
  316. VERBOSE("Using Memmap\n");
  317. io_close(local_image_handle);
  318. }
  319. }
  320. return result;
  321. }
  322. static int open_semihosting(const uintptr_t spec)
  323. {
  324. int result;
  325. uintptr_t local_image_handle;
  326. /* See if the file exists on semi-hosting.*/
  327. result = io_dev_init(sh_dev_handle, (uintptr_t)NULL);
  328. if (result == 0) {
  329. result = io_open(sh_dev_handle, spec, &local_image_handle);
  330. if (result == 0) {
  331. VERBOSE("Using Semi-hosting IO\n");
  332. io_close(local_image_handle);
  333. }
  334. }
  335. return result;
  336. }
  337. void plat_qemu_io_setup(void)
  338. {
  339. int io_result;
  340. io_result = register_io_dev_fip(&fip_dev_con);
  341. assert(io_result == 0);
  342. io_result = register_io_dev_memmap(&memmap_dev_con);
  343. assert(io_result == 0);
  344. /* Open connections to devices and cache the handles */
  345. io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
  346. &fip_dev_handle);
  347. assert(io_result == 0);
  348. io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
  349. &memmap_dev_handle);
  350. assert(io_result == 0);
  351. #ifndef DECRYPTION_SUPPORT_none
  352. io_result = register_io_dev_enc(&enc_dev_con);
  353. assert(io_result == 0);
  354. io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL,
  355. &enc_dev_handle);
  356. assert(io_result == 0);
  357. #endif
  358. /* Register the additional IO devices on this platform */
  359. io_result = register_io_dev_sh(&sh_dev_con);
  360. assert(io_result == 0);
  361. /* Open connections to devices and cache the handles */
  362. io_result = io_dev_open(sh_dev_con, (uintptr_t)NULL, &sh_dev_handle);
  363. assert(io_result == 0);
  364. /* Ignore improbable errors in release builds */
  365. (void)io_result;
  366. }
  367. static int get_alt_image_source(unsigned int image_id, uintptr_t *dev_handle,
  368. uintptr_t *image_spec)
  369. {
  370. int result = open_semihosting((const uintptr_t)&sh_file_spec[image_id]);
  371. if (result == 0) {
  372. *dev_handle = sh_dev_handle;
  373. *image_spec = (uintptr_t)&sh_file_spec[image_id];
  374. }
  375. return result;
  376. }
  377. /*
  378. * Return an IO device handle and specification which can be used to access
  379. * an image. Use this to enforce platform load policy
  380. */
  381. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  382. uintptr_t *image_spec)
  383. {
  384. int result;
  385. const struct plat_io_policy *policy;
  386. assert(image_id < ARRAY_SIZE(policies));
  387. policy = &policies[image_id];
  388. result = policy->check(policy->image_spec);
  389. if (result == 0) {
  390. *image_spec = policy->image_spec;
  391. *dev_handle = *(policy->dev_handle);
  392. } else {
  393. VERBOSE("Trying alternative IO\n");
  394. result = get_alt_image_source(image_id, dev_handle, image_spec);
  395. }
  396. return result;
  397. }