imx_io_storage.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2021, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <common/debug.h>
  8. #include <drivers/io/io_block.h>
  9. #include <drivers/io/io_driver.h>
  10. #include <drivers/io/io_fip.h>
  11. #include <drivers/io/io_memmap.h>
  12. #include <drivers/mmc.h>
  13. #include <lib/utils_def.h>
  14. #include <tbbr_img_def.h>
  15. #include <tools_share/firmware_image_package.h>
  16. #include <platform_def.h>
  17. static const io_dev_connector_t *fip_dev_con;
  18. static uintptr_t fip_dev_handle;
  19. #ifndef IMX_FIP_MMAP
  20. static const io_dev_connector_t *mmc_dev_con;
  21. static uintptr_t mmc_dev_handle;
  22. static const io_block_spec_t mmc_fip_spec = {
  23. .offset = IMX_FIP_MMC_BASE,
  24. .length = IMX_FIP_SIZE
  25. };
  26. static const io_block_dev_spec_t mmc_dev_spec = {
  27. /* It's used as temp buffer in block driver. */
  28. .buffer = {
  29. .offset = IMX_FIP_BASE,
  30. /* do we need a new value? */
  31. .length = IMX_FIP_SIZE
  32. },
  33. .ops = {
  34. .read = mmc_read_blocks,
  35. .write = mmc_write_blocks,
  36. },
  37. .block_size = MMC_BLOCK_SIZE,
  38. };
  39. static int open_mmc(const uintptr_t spec);
  40. #else
  41. static const io_dev_connector_t *memmap_dev_con;
  42. static uintptr_t memmap_dev_handle;
  43. static const io_block_spec_t fip_block_spec = {
  44. .offset = IMX_FIP_BASE,
  45. .length = IMX_FIP_SIZE
  46. };
  47. static int open_memmap(const uintptr_t spec);
  48. #endif
  49. static int open_fip(const uintptr_t spec);
  50. static const io_uuid_spec_t bl31_uuid_spec = {
  51. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  52. };
  53. static const io_uuid_spec_t bl32_uuid_spec = {
  54. .uuid = UUID_SECURE_PAYLOAD_BL32,
  55. };
  56. static const io_uuid_spec_t bl32_extra1_uuid_spec = {
  57. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
  58. };
  59. static const io_uuid_spec_t bl32_extra2_uuid_spec = {
  60. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
  61. };
  62. static const io_uuid_spec_t bl33_uuid_spec = {
  63. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  64. };
  65. #if TRUSTED_BOARD_BOOT
  66. static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
  67. .uuid = UUID_TRUSTED_BOOT_FW_CERT,
  68. };
  69. static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
  70. .uuid = UUID_TRUSTED_KEY_CERT,
  71. };
  72. static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
  73. .uuid = UUID_SOC_FW_KEY_CERT,
  74. };
  75. static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
  76. .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
  77. };
  78. static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
  79. .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
  80. };
  81. static const io_uuid_spec_t soc_fw_content_cert_uuid_spec = {
  82. .uuid = UUID_SOC_FW_CONTENT_CERT,
  83. };
  84. static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
  85. .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
  86. };
  87. static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
  88. .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
  89. };
  90. #endif /* TRUSTED_BOARD_BOOT */
  91. /* TODO: this structure is replicated multiple times. rationalize it ! */
  92. struct plat_io_policy {
  93. uintptr_t *dev_handle;
  94. uintptr_t image_spec;
  95. int (*check)(const uintptr_t spec);
  96. };
  97. static const struct plat_io_policy policies[] = {
  98. #ifndef IMX_FIP_MMAP
  99. [FIP_IMAGE_ID] = {
  100. &mmc_dev_handle,
  101. (uintptr_t)&mmc_fip_spec,
  102. open_mmc
  103. },
  104. #else
  105. [FIP_IMAGE_ID] = {
  106. &memmap_dev_handle,
  107. (uintptr_t)&fip_block_spec,
  108. open_memmap
  109. },
  110. #endif
  111. [BL31_IMAGE_ID] = {
  112. &fip_dev_handle,
  113. (uintptr_t)&bl31_uuid_spec,
  114. open_fip
  115. },
  116. [BL32_IMAGE_ID] = {
  117. &fip_dev_handle,
  118. (uintptr_t)&bl32_uuid_spec,
  119. open_fip
  120. },
  121. [BL32_EXTRA1_IMAGE_ID] = {
  122. &fip_dev_handle,
  123. (uintptr_t)&bl32_extra1_uuid_spec,
  124. open_fip
  125. },
  126. [BL32_EXTRA2_IMAGE_ID] = {
  127. &fip_dev_handle,
  128. (uintptr_t)&bl32_extra2_uuid_spec,
  129. open_fip
  130. },
  131. [BL33_IMAGE_ID] = {
  132. &fip_dev_handle,
  133. (uintptr_t)&bl33_uuid_spec,
  134. open_fip
  135. },
  136. #if TRUSTED_BOARD_BOOT
  137. [TRUSTED_BOOT_FW_CERT_ID] = {
  138. &fip_dev_handle,
  139. (uintptr_t)&tb_fw_cert_uuid_spec,
  140. open_fip
  141. },
  142. [SOC_FW_KEY_CERT_ID] = {
  143. &fip_dev_handle,
  144. (uintptr_t)&soc_fw_key_cert_uuid_spec,
  145. open_fip
  146. },
  147. [TRUSTED_KEY_CERT_ID] = {
  148. &fip_dev_handle,
  149. (uintptr_t)&trusted_key_cert_uuid_spec,
  150. open_fip
  151. },
  152. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  153. &fip_dev_handle,
  154. (uintptr_t)&tos_fw_key_cert_uuid_spec,
  155. open_fip
  156. },
  157. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  158. &fip_dev_handle,
  159. (uintptr_t)&nt_fw_key_cert_uuid_spec,
  160. open_fip
  161. },
  162. [SOC_FW_CONTENT_CERT_ID] = {
  163. &fip_dev_handle,
  164. (uintptr_t)&soc_fw_content_cert_uuid_spec,
  165. open_fip
  166. },
  167. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  168. &fip_dev_handle,
  169. (uintptr_t)&tos_fw_cert_uuid_spec,
  170. open_fip
  171. },
  172. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  173. &fip_dev_handle,
  174. (uintptr_t)&nt_fw_cert_uuid_spec,
  175. open_fip
  176. },
  177. #endif /* TRUSTED_BOARD_BOOT */
  178. };
  179. static int open_fip(const uintptr_t spec)
  180. {
  181. int result;
  182. uintptr_t local_image_handle;
  183. /* See if a Firmware Image Package is available */
  184. result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  185. if (result == 0) {
  186. result = io_open(fip_dev_handle, spec, &local_image_handle);
  187. if (result == 0) {
  188. VERBOSE("Using FIP\n");
  189. io_close(local_image_handle);
  190. }
  191. }
  192. return result;
  193. }
  194. #ifndef IMX_FIP_MMAP
  195. static int open_mmc(const uintptr_t spec)
  196. {
  197. int result;
  198. uintptr_t local_handle;
  199. result = io_dev_init(mmc_dev_handle, (uintptr_t)NULL);
  200. if (result == 0) {
  201. result = io_open(mmc_dev_handle, spec, &local_handle);
  202. if (result == 0) {
  203. io_close(local_handle);
  204. }
  205. }
  206. return result;
  207. }
  208. #else
  209. static int open_memmap(const uintptr_t spec)
  210. {
  211. int result;
  212. uintptr_t local_image_handle;
  213. result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
  214. if (result == 0) {
  215. result = io_open(memmap_dev_handle, spec, &local_image_handle);
  216. if (result == 0) {
  217. VERBOSE("Using Memmap\n");
  218. io_close(local_image_handle);
  219. }
  220. }
  221. return result;
  222. }
  223. #endif
  224. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  225. uintptr_t *image_spec)
  226. {
  227. int result;
  228. const struct plat_io_policy *policy;
  229. assert(image_id < ARRAY_SIZE(policies));
  230. policy = &policies[image_id];
  231. result = policy->check(policy->image_spec);
  232. assert(result == 0);
  233. *image_spec = policy->image_spec;
  234. *dev_handle = *policy->dev_handle;
  235. return result;
  236. }
  237. void plat_imx_io_setup(void)
  238. {
  239. int result __unused;
  240. #ifndef IMX_FIP_MMAP
  241. result = register_io_dev_block(&mmc_dev_con);
  242. assert(result == 0);
  243. result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_dev_spec,
  244. &mmc_dev_handle);
  245. assert(result == 0);
  246. #else
  247. result = register_io_dev_memmap(&memmap_dev_con);
  248. assert(result == 0);
  249. result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
  250. &memmap_dev_handle);
  251. assert(result == 0);
  252. #endif
  253. result = register_io_dev_fip(&fip_dev_con);
  254. assert(result == 0);
  255. result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
  256. &fip_dev_handle);
  257. assert(result == 0);
  258. }