ls_io_storage.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Copyright 2018-2021 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <assert.h>
  8. #include <endian.h>
  9. #include <string.h>
  10. #include <common/debug.h>
  11. #include <common/tbbr/tbbr_img_def.h>
  12. #include <drivers/io/io_block.h>
  13. #include <drivers/io/io_driver.h>
  14. #include <drivers/io/io_fip.h>
  15. #include <drivers/io/io_memmap.h>
  16. #include <drivers/io/io_storage.h>
  17. #ifdef FLEXSPI_NOR_BOOT
  18. #include <flexspi_nor.h>
  19. #endif
  20. #if defined(NAND_BOOT)
  21. #include <ifc_nand.h>
  22. #endif
  23. #if defined(NOR_BOOT)
  24. #include <ifc_nor.h>
  25. #endif
  26. #if defined(QSPI_BOOT)
  27. #include <qspi.h>
  28. #endif
  29. #if defined(SD_BOOT) || defined(EMMC_BOOT)
  30. #include <sd_mmc.h>
  31. #endif
  32. #include <tools_share/firmware_image_package.h>
  33. #ifdef CONFIG_DDR_FIP_IMAGE
  34. #include <ddr_io_storage.h>
  35. #endif
  36. #ifdef POLICY_FUSE_PROVISION
  37. #include <fuse_io.h>
  38. #endif
  39. #include "plat_common.h"
  40. #include "platform_def.h"
  41. uint32_t fip_device;
  42. /* IO devices */
  43. uintptr_t backend_dev_handle;
  44. static const io_dev_connector_t *fip_dev_con;
  45. static uintptr_t fip_dev_handle;
  46. static const io_dev_connector_t *backend_dev_con;
  47. static io_block_spec_t fip_block_spec = {
  48. .offset = PLAT_FIP_OFFSET,
  49. .length = PLAT_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 fuse_bl2_uuid_spec = {
  55. .uuid = UUID_SCP_FIRMWARE_SCP_BL2,
  56. };
  57. static const io_uuid_spec_t bl31_uuid_spec = {
  58. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  59. };
  60. static const io_uuid_spec_t bl32_uuid_spec = {
  61. .uuid = UUID_SECURE_PAYLOAD_BL32,
  62. };
  63. static const io_uuid_spec_t bl33_uuid_spec = {
  64. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  65. };
  66. static const io_uuid_spec_t tb_fw_config_uuid_spec = {
  67. .uuid = UUID_TB_FW_CONFIG,
  68. };
  69. static const io_uuid_spec_t hw_config_uuid_spec = {
  70. .uuid = UUID_HW_CONFIG,
  71. };
  72. #if TRUSTED_BOARD_BOOT
  73. static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
  74. .uuid = UUID_TRUSTED_BOOT_FW_CERT,
  75. };
  76. static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
  77. .uuid = UUID_TRUSTED_KEY_CERT,
  78. };
  79. static const io_uuid_spec_t fuse_key_cert_uuid_spec = {
  80. .uuid = UUID_SCP_FW_KEY_CERT,
  81. };
  82. static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
  83. .uuid = UUID_SOC_FW_KEY_CERT,
  84. };
  85. static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
  86. .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
  87. };
  88. static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
  89. .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
  90. };
  91. static const io_uuid_spec_t fuse_cert_uuid_spec = {
  92. .uuid = UUID_SCP_FW_CONTENT_CERT,
  93. };
  94. static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
  95. .uuid = UUID_SOC_FW_CONTENT_CERT,
  96. };
  97. static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
  98. .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
  99. };
  100. static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
  101. .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
  102. };
  103. #endif /* TRUSTED_BOARD_BOOT */
  104. static int open_fip(const uintptr_t spec);
  105. struct plat_io_policy {
  106. uintptr_t *dev_handle;
  107. uintptr_t image_spec;
  108. int (*check)(const uintptr_t spec);
  109. };
  110. /* By default, ARM platforms load images from the FIP */
  111. static const struct plat_io_policy policies[] = {
  112. [FIP_IMAGE_ID] = {
  113. &backend_dev_handle,
  114. (uintptr_t)&fip_block_spec,
  115. open_backend
  116. },
  117. [BL2_IMAGE_ID] = {
  118. &fip_dev_handle,
  119. (uintptr_t)&bl2_uuid_spec,
  120. open_fip
  121. },
  122. [SCP_BL2_IMAGE_ID] = {
  123. &fip_dev_handle,
  124. (uintptr_t)&fuse_bl2_uuid_spec,
  125. open_fip
  126. },
  127. [BL31_IMAGE_ID] = {
  128. &fip_dev_handle,
  129. (uintptr_t)&bl31_uuid_spec,
  130. open_fip
  131. },
  132. [BL32_IMAGE_ID] = {
  133. &fip_dev_handle,
  134. (uintptr_t)&bl32_uuid_spec,
  135. open_fip
  136. },
  137. [BL33_IMAGE_ID] = {
  138. &fip_dev_handle,
  139. (uintptr_t)&bl33_uuid_spec,
  140. open_fip
  141. },
  142. [TB_FW_CONFIG_ID] = {
  143. &fip_dev_handle,
  144. (uintptr_t)&tb_fw_config_uuid_spec,
  145. open_fip
  146. },
  147. [HW_CONFIG_ID] = {
  148. &fip_dev_handle,
  149. (uintptr_t)&hw_config_uuid_spec,
  150. open_fip
  151. },
  152. #if TRUSTED_BOARD_BOOT
  153. [TRUSTED_BOOT_FW_CERT_ID] = {
  154. &fip_dev_handle,
  155. (uintptr_t)&tb_fw_cert_uuid_spec,
  156. open_fip
  157. },
  158. [TRUSTED_KEY_CERT_ID] = {
  159. &fip_dev_handle,
  160. (uintptr_t)&trusted_key_cert_uuid_spec,
  161. open_fip
  162. },
  163. [SCP_FW_KEY_CERT_ID] = {
  164. &fip_dev_handle,
  165. (uintptr_t)&fuse_key_cert_uuid_spec,
  166. open_fip
  167. },
  168. [SOC_FW_KEY_CERT_ID] = {
  169. &fip_dev_handle,
  170. (uintptr_t)&soc_fw_key_cert_uuid_spec,
  171. open_fip
  172. },
  173. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  174. &fip_dev_handle,
  175. (uintptr_t)&tos_fw_key_cert_uuid_spec,
  176. open_fip
  177. },
  178. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  179. &fip_dev_handle,
  180. (uintptr_t)&nt_fw_key_cert_uuid_spec,
  181. open_fip
  182. },
  183. [SCP_FW_CONTENT_CERT_ID] = {
  184. &fip_dev_handle,
  185. (uintptr_t)&fuse_cert_uuid_spec,
  186. open_fip
  187. },
  188. [SOC_FW_CONTENT_CERT_ID] = {
  189. &fip_dev_handle,
  190. (uintptr_t)&soc_fw_cert_uuid_spec,
  191. open_fip
  192. },
  193. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  194. &fip_dev_handle,
  195. (uintptr_t)&tos_fw_cert_uuid_spec,
  196. open_fip
  197. },
  198. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  199. &fip_dev_handle,
  200. (uintptr_t)&nt_fw_cert_uuid_spec,
  201. open_fip
  202. },
  203. #endif /* TRUSTED_BOARD_BOOT */
  204. };
  205. /* Weak definitions may be overridden in specific ARM standard platform */
  206. #pragma weak plat_io_setup
  207. /*
  208. * Return an IO device handle and specification which can be used to access
  209. */
  210. static int open_fip(const uintptr_t spec)
  211. {
  212. int result;
  213. uintptr_t local_image_handle;
  214. /* See if a Firmware Image Package is available */
  215. result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  216. if (result == 0) {
  217. result = io_open(fip_dev_handle, spec, &local_image_handle);
  218. if (result == 0) {
  219. VERBOSE("Using FIP\n");
  220. io_close(local_image_handle);
  221. }
  222. }
  223. return result;
  224. }
  225. int open_backend(const uintptr_t spec)
  226. {
  227. int result;
  228. uintptr_t local_image_handle;
  229. result = io_dev_init(backend_dev_handle, (uintptr_t)NULL);
  230. if (result == 0) {
  231. result = io_open(backend_dev_handle, spec, &local_image_handle);
  232. if (result == 0) {
  233. io_close(local_image_handle);
  234. }
  235. }
  236. return result;
  237. }
  238. #if defined(SD_BOOT) || defined(EMMC_BOOT) || defined(NAND_BOOT)
  239. static int plat_io_block_setup(size_t fip_offset, uintptr_t block_dev_spec)
  240. {
  241. int io_result;
  242. fip_block_spec.offset = fip_offset;
  243. io_result = register_io_dev_block(&backend_dev_con);
  244. assert(io_result == 0);
  245. /* Open connections to devices and cache the handles */
  246. io_result = io_dev_open(backend_dev_con, block_dev_spec,
  247. &backend_dev_handle);
  248. assert(io_result == 0);
  249. return io_result;
  250. }
  251. #endif
  252. #if defined(FLEXSPI_NOR_BOOT) || defined(QSPI_BOOT) || defined(NOR_BOOT)
  253. static int plat_io_memmap_setup(size_t fip_offset)
  254. {
  255. int io_result;
  256. fip_block_spec.offset = fip_offset;
  257. io_result = register_io_dev_memmap(&backend_dev_con);
  258. assert(io_result == 0);
  259. /* Open connections to devices and cache the handles */
  260. io_result = io_dev_open(backend_dev_con, (uintptr_t)NULL,
  261. &backend_dev_handle);
  262. assert(io_result == 0);
  263. return io_result;
  264. }
  265. #endif
  266. static int ls_io_fip_setup(unsigned int boot_dev)
  267. {
  268. int io_result;
  269. io_result = register_io_dev_fip(&fip_dev_con);
  270. assert(io_result == 0);
  271. /* Open connections to devices and cache the handles */
  272. io_result = io_dev_open(fip_dev_con, (uintptr_t)&fip_device,
  273. &fip_dev_handle);
  274. assert(io_result == 0);
  275. #ifdef CONFIG_DDR_FIP_IMAGE
  276. /* Open connection to DDR FIP image if available */
  277. io_result = ddr_fip_setup(fip_dev_con, boot_dev);
  278. assert(io_result == 0);
  279. #endif
  280. #ifdef POLICY_FUSE_PROVISION
  281. /* Open connection to FUSE FIP image if available */
  282. io_result = fuse_fip_setup(fip_dev_con, boot_dev);
  283. assert(io_result == 0);
  284. #endif
  285. return io_result;
  286. }
  287. int ls_qspi_io_setup(void)
  288. {
  289. #ifdef QSPI_BOOT
  290. qspi_io_setup(NXP_QSPI_FLASH_ADDR,
  291. NXP_QSPI_FLASH_SIZE,
  292. PLAT_FIP_OFFSET);
  293. return plat_io_memmap_setup(NXP_QSPI_FLASH_ADDR + PLAT_FIP_OFFSET);
  294. #else
  295. ERROR("QSPI driver not present. Check your BUILD\n");
  296. /* Should never reach here */
  297. assert(false);
  298. return -1;
  299. #endif
  300. }
  301. int emmc_sdhc2_io_setup(void)
  302. {
  303. #if defined(EMMC_BOOT) && defined(NXP_ESDHC2_ADDR)
  304. uintptr_t block_dev_spec;
  305. int ret;
  306. ret = sd_emmc_init(&block_dev_spec,
  307. NXP_ESDHC2_ADDR,
  308. NXP_SD_BLOCK_BUF_ADDR,
  309. NXP_SD_BLOCK_BUF_SIZE,
  310. false);
  311. if (ret != 0) {
  312. return ret;
  313. }
  314. return plat_io_block_setup(PLAT_FIP_OFFSET, block_dev_spec);
  315. #else
  316. ERROR("EMMC driver not present. Check your BUILD\n");
  317. /* Should never reach here */
  318. assert(false);
  319. return -1;
  320. #endif
  321. }
  322. int emmc_io_setup(void)
  323. {
  324. /* On the platforms which only has one ESDHC controller,
  325. * eMMC-boot will use the first ESDHC controller.
  326. */
  327. #if defined(SD_BOOT) || defined(EMMC_BOOT)
  328. uintptr_t block_dev_spec;
  329. int ret;
  330. ret = sd_emmc_init(&block_dev_spec,
  331. NXP_ESDHC_ADDR,
  332. NXP_SD_BLOCK_BUF_ADDR,
  333. NXP_SD_BLOCK_BUF_SIZE,
  334. true);
  335. if (ret != 0) {
  336. return ret;
  337. }
  338. return plat_io_block_setup(PLAT_FIP_OFFSET, block_dev_spec);
  339. #else
  340. ERROR("SD driver not present. Check your BUILD\n");
  341. /* Should never reach here */
  342. assert(false);
  343. return -1;
  344. #endif
  345. }
  346. int ifc_nor_io_setup(void)
  347. {
  348. #if defined(NOR_BOOT)
  349. int ret;
  350. ret = ifc_nor_init(NXP_NOR_FLASH_ADDR,
  351. NXP_NOR_FLASH_SIZE);
  352. if (ret != 0) {
  353. return ret;
  354. }
  355. return plat_io_memmap_setup(NXP_NOR_FLASH_ADDR + PLAT_FIP_OFFSET);
  356. #else
  357. ERROR("NOR driver not present. Check your BUILD\n");
  358. /* Should never reach here */
  359. assert(false);
  360. return -1;
  361. #endif
  362. }
  363. int ifc_nand_io_setup(void)
  364. {
  365. #if defined(NAND_BOOT)
  366. uintptr_t block_dev_spec;
  367. int ret;
  368. ret = ifc_nand_init(&block_dev_spec,
  369. NXP_IFC_REGION_ADDR,
  370. NXP_IFC_ADDR,
  371. NXP_IFC_SRAM_BUFFER_SIZE,
  372. NXP_SD_BLOCK_BUF_ADDR,
  373. NXP_SD_BLOCK_BUF_SIZE);
  374. if (ret != 0) {
  375. return ret;
  376. }
  377. return plat_io_block_setup(PLAT_FIP_OFFSET, block_dev_spec);
  378. #else
  379. ERROR("NAND driver not present. Check your BUILD\n");
  380. /* Should never reach here */
  381. assert(false);
  382. return -1;
  383. #endif
  384. }
  385. int ls_flexspi_nor_io_setup(void)
  386. {
  387. #ifdef FLEXSPI_NOR_BOOT
  388. int ret = 0;
  389. ret = flexspi_nor_io_setup(NXP_FLEXSPI_FLASH_ADDR,
  390. NXP_FLEXSPI_FLASH_SIZE,
  391. NXP_FLEXSPI_ADDR);
  392. if (ret != 0) {
  393. ERROR("FlexSPI NOR driver initialization error.\n");
  394. /* Should never reach here */
  395. assert(0);
  396. panic();
  397. return -1;
  398. }
  399. return plat_io_memmap_setup(NXP_FLEXSPI_FLASH_ADDR + PLAT_FIP_OFFSET);
  400. #else
  401. ERROR("FlexSPI NOR driver not present. Check your BUILD\n");
  402. /* Should never reach here */
  403. assert(false);
  404. return -1;
  405. #endif
  406. }
  407. static int (* const ls_io_setup_table[])(void) = {
  408. [BOOT_DEVICE_IFC_NOR] = ifc_nor_io_setup,
  409. [BOOT_DEVICE_IFC_NAND] = ifc_nand_io_setup,
  410. [BOOT_DEVICE_QSPI] = ls_qspi_io_setup,
  411. [BOOT_DEVICE_EMMC] = emmc_io_setup,
  412. [BOOT_DEVICE_SDHC2_EMMC] = emmc_sdhc2_io_setup,
  413. [BOOT_DEVICE_FLEXSPI_NOR] = ls_flexspi_nor_io_setup,
  414. [BOOT_DEVICE_FLEXSPI_NAND] = ls_flexspi_nor_io_setup,
  415. };
  416. int plat_io_setup(void)
  417. {
  418. int (*io_setup)(void);
  419. unsigned int boot_dev = BOOT_DEVICE_NONE;
  420. int ret;
  421. boot_dev = get_boot_dev();
  422. if (boot_dev == BOOT_DEVICE_NONE) {
  423. ERROR("Boot Device detection failed, Check RCW_SRC\n");
  424. return -EINVAL;
  425. }
  426. io_setup = ls_io_setup_table[boot_dev];
  427. ret = io_setup();
  428. if (ret != 0) {
  429. return ret;
  430. }
  431. ret = ls_io_fip_setup(boot_dev);
  432. if (ret != 0) {
  433. return ret;
  434. }
  435. return 0;
  436. }
  437. /* Return an IO device handle and specification which can be used to access
  438. * an image. Use this to enforce platform load policy
  439. */
  440. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  441. uintptr_t *image_spec)
  442. {
  443. int result = -1;
  444. const struct plat_io_policy *policy;
  445. if (image_id < ARRAY_SIZE(policies)) {
  446. policy = &policies[image_id];
  447. result = policy->check(policy->image_spec);
  448. if (result == 0) {
  449. *image_spec = policy->image_spec;
  450. *dev_handle = *(policy->dev_handle);
  451. }
  452. }
  453. #ifdef CONFIG_DDR_FIP_IMAGE
  454. else {
  455. VERBOSE("Trying alternative IO\n");
  456. result = plat_get_ddr_fip_image_source(image_id, dev_handle,
  457. image_spec, open_backend);
  458. }
  459. #endif
  460. #ifdef POLICY_FUSE_PROVISION
  461. if (result != 0) {
  462. VERBOSE("Trying FUSE IO\n");
  463. result = plat_get_fuse_image_source(image_id, dev_handle,
  464. image_spec, open_backend);
  465. }
  466. #endif
  467. return result;
  468. }