brcm_io_storage.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) 2019-2020, 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 <common/debug.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/io/io_storage.h>
  13. #include <tools_share/firmware_image_package.h>
  14. #include <cmn_plat_def.h>
  15. #include <cmn_plat_util.h>
  16. #include <plat_brcm.h>
  17. #include <platform_def.h>
  18. /* IO devices */
  19. static const io_dev_connector_t *fip_dev_con;
  20. static uintptr_t fip_dev_handle;
  21. static const io_dev_connector_t *memmap_dev_con;
  22. static uintptr_t memmap_dev_handle;
  23. static const io_block_spec_t fip_block_spec = {
  24. .offset = PLAT_BRCM_FIP_BASE,
  25. .length = PLAT_BRCM_FIP_MAX_SIZE
  26. };
  27. static const io_block_spec_t qspi_fip_block_spec = {
  28. .offset = PLAT_BRCM_FIP_QSPI_BASE,
  29. .length = PLAT_BRCM_FIP_MAX_SIZE
  30. };
  31. static const io_block_spec_t nand_fip_block_spec = {
  32. .offset = PLAT_BRCM_FIP_NAND_BASE,
  33. .length = PLAT_BRCM_FIP_MAX_SIZE
  34. };
  35. static const io_uuid_spec_t bl2_uuid_spec = {
  36. .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
  37. };
  38. static const io_uuid_spec_t scp_bl2_uuid_spec = {
  39. .uuid = UUID_SCP_FIRMWARE_SCP_BL2,
  40. };
  41. static const io_uuid_spec_t bl31_uuid_spec = {
  42. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  43. };
  44. static const io_uuid_spec_t bl32_uuid_spec = {
  45. .uuid = UUID_SECURE_PAYLOAD_BL32,
  46. };
  47. static const io_uuid_spec_t bl32_extra1_uuid_spec = {
  48. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
  49. };
  50. static const io_uuid_spec_t bl32_extra2_uuid_spec = {
  51. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
  52. };
  53. static const io_uuid_spec_t bl33_uuid_spec = {
  54. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  55. };
  56. static const io_uuid_spec_t tb_fw_config_uuid_spec = {
  57. .uuid = UUID_TB_FW_CONFIG,
  58. };
  59. static const io_uuid_spec_t hw_config_uuid_spec = {
  60. .uuid = UUID_HW_CONFIG,
  61. };
  62. static const io_uuid_spec_t soc_fw_config_uuid_spec = {
  63. .uuid = UUID_SOC_FW_CONFIG,
  64. };
  65. static const io_uuid_spec_t tos_fw_config_uuid_spec = {
  66. .uuid = UUID_TOS_FW_CONFIG,
  67. };
  68. static const io_uuid_spec_t nt_fw_config_uuid_spec = {
  69. .uuid = UUID_NT_FW_CONFIG,
  70. };
  71. #if TRUSTED_BOARD_BOOT
  72. static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
  73. .uuid = UUID_TRUSTED_BOOT_FW_CERT,
  74. };
  75. static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
  76. .uuid = UUID_TRUSTED_KEY_CERT,
  77. };
  78. static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
  79. .uuid = UUID_SCP_FW_KEY_CERT,
  80. };
  81. static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
  82. .uuid = UUID_SOC_FW_KEY_CERT,
  83. };
  84. static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
  85. .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
  86. };
  87. static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
  88. .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
  89. };
  90. static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
  91. .uuid = UUID_SCP_FW_CONTENT_CERT,
  92. };
  93. static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
  94. .uuid = UUID_SOC_FW_CONTENT_CERT,
  95. };
  96. static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
  97. .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
  98. };
  99. static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
  100. .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
  101. };
  102. #endif /* TRUSTED_BOARD_BOOT */
  103. static int open_fip(const uintptr_t spec);
  104. static int open_memmap(const uintptr_t spec);
  105. static int open_qspi(const uintptr_t spec);
  106. static int open_nand(const uintptr_t spec);
  107. struct plat_io_policy {
  108. uintptr_t *dev_handle;
  109. uintptr_t image_spec;
  110. int (*check)(const uintptr_t spec);
  111. };
  112. /* By default, BRCM platforms load images from the FIP */
  113. static const struct plat_io_policy policies[] = {
  114. [FIP_IMAGE_ID] = {
  115. &memmap_dev_handle,
  116. (uintptr_t)&fip_block_spec,
  117. open_memmap
  118. },
  119. [BL2_IMAGE_ID] = {
  120. &fip_dev_handle,
  121. (uintptr_t)&bl2_uuid_spec,
  122. open_fip
  123. },
  124. [SCP_BL2_IMAGE_ID] = {
  125. &fip_dev_handle,
  126. (uintptr_t)&scp_bl2_uuid_spec,
  127. open_fip
  128. },
  129. [BL31_IMAGE_ID] = {
  130. &fip_dev_handle,
  131. (uintptr_t)&bl31_uuid_spec,
  132. open_fip
  133. },
  134. [BL32_IMAGE_ID] = {
  135. &fip_dev_handle,
  136. (uintptr_t)&bl32_uuid_spec,
  137. open_fip
  138. },
  139. [BL32_EXTRA1_IMAGE_ID] = {
  140. &fip_dev_handle,
  141. (uintptr_t)&bl32_extra1_uuid_spec,
  142. open_fip
  143. },
  144. [BL32_EXTRA2_IMAGE_ID] = {
  145. &fip_dev_handle,
  146. (uintptr_t)&bl32_extra2_uuid_spec,
  147. open_fip
  148. },
  149. [BL33_IMAGE_ID] = {
  150. &fip_dev_handle,
  151. (uintptr_t)&bl33_uuid_spec,
  152. open_fip
  153. },
  154. [TB_FW_CONFIG_ID] = {
  155. &fip_dev_handle,
  156. (uintptr_t)&tb_fw_config_uuid_spec,
  157. open_fip
  158. },
  159. [HW_CONFIG_ID] = {
  160. &fip_dev_handle,
  161. (uintptr_t)&hw_config_uuid_spec,
  162. open_fip
  163. },
  164. [SOC_FW_CONFIG_ID] = {
  165. &fip_dev_handle,
  166. (uintptr_t)&soc_fw_config_uuid_spec,
  167. open_fip
  168. },
  169. [TOS_FW_CONFIG_ID] = {
  170. &fip_dev_handle,
  171. (uintptr_t)&tos_fw_config_uuid_spec,
  172. open_fip
  173. },
  174. [NT_FW_CONFIG_ID] = {
  175. &fip_dev_handle,
  176. (uintptr_t)&nt_fw_config_uuid_spec,
  177. open_fip
  178. },
  179. #if TRUSTED_BOARD_BOOT
  180. [TRUSTED_BOOT_FW_CERT_ID] = {
  181. &fip_dev_handle,
  182. (uintptr_t)&tb_fw_cert_uuid_spec,
  183. open_fip
  184. },
  185. [TRUSTED_KEY_CERT_ID] = {
  186. &fip_dev_handle,
  187. (uintptr_t)&trusted_key_cert_uuid_spec,
  188. open_fip
  189. },
  190. [SCP_FW_KEY_CERT_ID] = {
  191. &fip_dev_handle,
  192. (uintptr_t)&scp_fw_key_cert_uuid_spec,
  193. open_fip
  194. },
  195. [SOC_FW_KEY_CERT_ID] = {
  196. &fip_dev_handle,
  197. (uintptr_t)&soc_fw_key_cert_uuid_spec,
  198. open_fip
  199. },
  200. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  201. &fip_dev_handle,
  202. (uintptr_t)&tos_fw_key_cert_uuid_spec,
  203. open_fip
  204. },
  205. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  206. &fip_dev_handle,
  207. (uintptr_t)&nt_fw_key_cert_uuid_spec,
  208. open_fip
  209. },
  210. [SCP_FW_CONTENT_CERT_ID] = {
  211. &fip_dev_handle,
  212. (uintptr_t)&scp_fw_cert_uuid_spec,
  213. open_fip
  214. },
  215. [SOC_FW_CONTENT_CERT_ID] = {
  216. &fip_dev_handle,
  217. (uintptr_t)&soc_fw_cert_uuid_spec,
  218. open_fip
  219. },
  220. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  221. &fip_dev_handle,
  222. (uintptr_t)&tos_fw_cert_uuid_spec,
  223. open_fip
  224. },
  225. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  226. &fip_dev_handle,
  227. (uintptr_t)&nt_fw_cert_uuid_spec,
  228. open_fip
  229. },
  230. #endif /* TRUSTED_BOARD_BOOT */
  231. };
  232. /* By default, BRCM platforms load images from the FIP */
  233. static const struct plat_io_policy boot_source_policies[] = {
  234. [BOOT_SOURCE_QSPI] = {
  235. &memmap_dev_handle,
  236. (uintptr_t)&qspi_fip_block_spec,
  237. open_qspi
  238. },
  239. [BOOT_SOURCE_NAND] = {
  240. &memmap_dev_handle,
  241. (uintptr_t)&nand_fip_block_spec,
  242. open_nand
  243. },
  244. };
  245. /* Weak definitions may be overridden in specific brcm platform */
  246. #pragma weak plat_brcm_io_setup
  247. #pragma weak plat_brcm_process_flags
  248. static int open_fip(const uintptr_t spec)
  249. {
  250. int result;
  251. uintptr_t local_image_handle;
  252. /* See if a Firmware Image Package is available */
  253. result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  254. if (result == 0) {
  255. result = io_open(fip_dev_handle, spec, &local_image_handle);
  256. if (result == 0) {
  257. VERBOSE("Using FIP\n");
  258. io_close(local_image_handle);
  259. }
  260. }
  261. return result;
  262. }
  263. static int open_memmap(const uintptr_t spec)
  264. {
  265. int result;
  266. uintptr_t local_image_handle;
  267. result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
  268. if (result == 0) {
  269. result = io_open(memmap_dev_handle, spec, &local_image_handle);
  270. if (result == 0) {
  271. VERBOSE("Using Memmap\n");
  272. io_close(local_image_handle);
  273. }
  274. }
  275. return result;
  276. }
  277. static int open_qspi(const uintptr_t spec)
  278. {
  279. return open_memmap(spec);
  280. }
  281. static int open_nand(const uintptr_t spec)
  282. {
  283. return open_memmap(spec);
  284. }
  285. void brcm_io_setup(void)
  286. {
  287. int io_result;
  288. uint32_t boot_source;
  289. io_result = register_io_dev_fip(&fip_dev_con);
  290. assert(io_result == 0);
  291. io_result = register_io_dev_memmap(&memmap_dev_con);
  292. assert(io_result == 0);
  293. /* Open connections to devices and cache the handles */
  294. io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
  295. &fip_dev_handle);
  296. assert(io_result == 0);
  297. boot_source = boot_source_get();
  298. switch (boot_source) {
  299. case BOOT_SOURCE_QSPI:
  300. case BOOT_SOURCE_NAND:
  301. default:
  302. io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
  303. &memmap_dev_handle);
  304. break;
  305. }
  306. assert(io_result == 0);
  307. /* Ignore improbable errors in release builds */
  308. (void)io_result;
  309. }
  310. void plat_brcm_io_setup(void)
  311. {
  312. brcm_io_setup();
  313. }
  314. void plat_brcm_process_flags(uint16_t plat_toc_flags __unused)
  315. {
  316. WARN("%s not implemented\n", __func__);
  317. }
  318. /*
  319. * Return an IO device handle and specification which can be used to access
  320. * an image. Use this to enforce platform load policy
  321. */
  322. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  323. uintptr_t *image_spec)
  324. {
  325. int result;
  326. const struct plat_io_policy *policy;
  327. uint32_t boot_source;
  328. uint16_t lcl_plat_toc_flg;
  329. assert(image_id < ARRAY_SIZE(policies));
  330. boot_source = boot_source_get();
  331. if (image_id == FIP_IMAGE_ID)
  332. policy = &boot_source_policies[boot_source];
  333. else
  334. policy = &policies[image_id];
  335. result = policy->check(policy->image_spec);
  336. if (result == 0) {
  337. *image_spec = policy->image_spec;
  338. *dev_handle = *(policy->dev_handle);
  339. if (image_id == TRUSTED_BOOT_FW_CERT_ID) {
  340. /*
  341. * Process the header flags to perform
  342. * such custom actions as speeding up PLL.
  343. * CERT seems to be the first image accessed
  344. * by BL1 so this is where we process the flags.
  345. */
  346. fip_dev_get_plat_toc_flag((io_dev_info_t *)fip_dev_handle,
  347. &lcl_plat_toc_flg);
  348. plat_brcm_process_flags(lcl_plat_toc_flg);
  349. }
  350. }
  351. return result;
  352. }