plat_storage.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Copyright (c) 2015-2020, Renesas Electronics Corporation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <string.h>
  7. #include <common/debug.h>
  8. #include <drivers/io/io_driver.h>
  9. #include <drivers/io/io_storage.h>
  10. #include <drivers/io/io_semihosting.h>
  11. #include "io_common.h"
  12. #include "io_memdrv.h"
  13. #include "io_emmcdrv.h"
  14. #include "io_private.h"
  15. #include "io_rcar.h"
  16. #include <platform_def.h>
  17. static uintptr_t emmcdrv_dev_handle;
  18. static uintptr_t memdrv_dev_handle;
  19. static uintptr_t rcar_dev_handle;
  20. static uintptr_t boot_io_drv_id;
  21. static const io_block_spec_t rcar_block_spec = {
  22. .offset = FLASH0_BASE,
  23. .length = FLASH0_SIZE
  24. };
  25. static const io_block_spec_t bl2_file_spec = {
  26. .offset = BL2_IMAGE_ID,
  27. };
  28. static const io_block_spec_t bl31_file_spec = {
  29. .offset = BL31_IMAGE_ID,
  30. };
  31. static const io_block_spec_t bl32_file_spec = {
  32. .offset = BL32_IMAGE_ID,
  33. };
  34. static const io_block_spec_t bl33_file_spec = {
  35. .offset = BL33_IMAGE_ID,
  36. };
  37. static const io_block_spec_t bl332_file_spec = {
  38. .offset = BL332_IMAGE_ID,
  39. };
  40. static const io_block_spec_t bl333_file_spec = {
  41. .offset = BL333_IMAGE_ID,
  42. };
  43. static const io_block_spec_t bl334_file_spec = {
  44. .offset = BL334_IMAGE_ID,
  45. };
  46. static const io_block_spec_t bl335_file_spec = {
  47. .offset = BL335_IMAGE_ID,
  48. };
  49. static const io_block_spec_t bl336_file_spec = {
  50. .offset = BL336_IMAGE_ID,
  51. };
  52. static const io_block_spec_t bl337_file_spec = {
  53. .offset = BL337_IMAGE_ID,
  54. };
  55. static const io_block_spec_t bl338_file_spec = {
  56. .offset = BL338_IMAGE_ID,
  57. };
  58. #if TRUSTED_BOARD_BOOT
  59. static const io_block_spec_t trusted_key_cert_file_spec = {
  60. .offset = TRUSTED_KEY_CERT_ID,
  61. };
  62. static const io_block_spec_t bl31_key_cert_file_spec = {
  63. .offset = SOC_FW_KEY_CERT_ID,
  64. };
  65. static const io_block_spec_t bl32_key_cert_file_spec = {
  66. .offset = TRUSTED_OS_FW_KEY_CERT_ID,
  67. };
  68. static const io_block_spec_t bl33_key_cert_file_spec = {
  69. .offset = NON_TRUSTED_FW_KEY_CERT_ID,
  70. };
  71. static const io_block_spec_t bl332_key_cert_file_spec = {
  72. .offset = BL332_KEY_CERT_ID,
  73. };
  74. static const io_block_spec_t bl333_key_cert_file_spec = {
  75. .offset = BL333_KEY_CERT_ID,
  76. };
  77. static const io_block_spec_t bl334_key_cert_file_spec = {
  78. .offset = BL334_KEY_CERT_ID,
  79. };
  80. static const io_block_spec_t bl335_key_cert_file_spec = {
  81. .offset = BL335_KEY_CERT_ID,
  82. };
  83. static const io_block_spec_t bl336_key_cert_file_spec = {
  84. .offset = BL336_KEY_CERT_ID,
  85. };
  86. static const io_block_spec_t bl337_key_cert_file_spec = {
  87. .offset = BL337_KEY_CERT_ID,
  88. };
  89. static const io_block_spec_t bl338_key_cert_file_spec = {
  90. .offset = BL338_KEY_CERT_ID,
  91. };
  92. static const io_block_spec_t bl31_cert_file_spec = {
  93. .offset = SOC_FW_CONTENT_CERT_ID,
  94. };
  95. static const io_block_spec_t bl32_cert_file_spec = {
  96. .offset = TRUSTED_OS_FW_CONTENT_CERT_ID,
  97. };
  98. static const io_block_spec_t bl33_cert_file_spec = {
  99. .offset = NON_TRUSTED_FW_CONTENT_CERT_ID,
  100. };
  101. static const io_block_spec_t bl332_cert_file_spec = {
  102. .offset = BL332_CERT_ID,
  103. };
  104. static const io_block_spec_t bl333_cert_file_spec = {
  105. .offset = BL333_CERT_ID,
  106. };
  107. static const io_block_spec_t bl334_cert_file_spec = {
  108. .offset = BL334_CERT_ID,
  109. };
  110. static const io_block_spec_t bl335_cert_file_spec = {
  111. .offset = BL335_CERT_ID,
  112. };
  113. static const io_block_spec_t bl336_cert_file_spec = {
  114. .offset = BL336_CERT_ID,
  115. };
  116. static const io_block_spec_t bl337_cert_file_spec = {
  117. .offset = BL337_CERT_ID,
  118. };
  119. static const io_block_spec_t bl338_cert_file_spec = {
  120. .offset = BL338_CERT_ID,
  121. };
  122. #endif
  123. static int32_t open_emmcdrv(const uintptr_t spec);
  124. static int32_t open_memmap(const uintptr_t spec);
  125. static int32_t open_rcar(const uintptr_t spec);
  126. struct plat_io_policy {
  127. uintptr_t *dev_handle;
  128. uintptr_t image_spec;
  129. int32_t (*check)(const uintptr_t spec);
  130. };
  131. static const struct plat_io_policy policies[] = {
  132. [FIP_IMAGE_ID] = {
  133. &memdrv_dev_handle,
  134. (uintptr_t) &rcar_block_spec,
  135. &open_memmap},
  136. [BL2_IMAGE_ID] = {
  137. &rcar_dev_handle,
  138. (uintptr_t) &bl2_file_spec,
  139. &open_rcar},
  140. [BL31_IMAGE_ID] = {
  141. &rcar_dev_handle,
  142. (uintptr_t) &bl31_file_spec,
  143. &open_rcar},
  144. [BL32_IMAGE_ID] = {
  145. &rcar_dev_handle,
  146. (uintptr_t) &bl32_file_spec,
  147. &open_rcar},
  148. [BL33_IMAGE_ID] = {
  149. &rcar_dev_handle,
  150. (uintptr_t) &bl33_file_spec,
  151. &open_rcar},
  152. [BL332_IMAGE_ID] = {
  153. &rcar_dev_handle,
  154. (uintptr_t) &bl332_file_spec,
  155. &open_rcar},
  156. [BL333_IMAGE_ID] = {
  157. &rcar_dev_handle,
  158. (uintptr_t) &bl333_file_spec,
  159. &open_rcar},
  160. [BL334_IMAGE_ID] = {
  161. &rcar_dev_handle,
  162. (uintptr_t) &bl334_file_spec,
  163. &open_rcar},
  164. [BL335_IMAGE_ID] = {
  165. &rcar_dev_handle,
  166. (uintptr_t) &bl335_file_spec,
  167. &open_rcar},
  168. [BL336_IMAGE_ID] = {
  169. &rcar_dev_handle,
  170. (uintptr_t) &bl336_file_spec,
  171. &open_rcar},
  172. [BL337_IMAGE_ID] = {
  173. &rcar_dev_handle,
  174. (uintptr_t) &bl337_file_spec,
  175. &open_rcar},
  176. [BL338_IMAGE_ID] = {
  177. &rcar_dev_handle,
  178. (uintptr_t) &bl338_file_spec,
  179. &open_rcar},
  180. #if TRUSTED_BOARD_BOOT
  181. [TRUSTED_KEY_CERT_ID] = {
  182. &rcar_dev_handle,
  183. (uintptr_t) &trusted_key_cert_file_spec,
  184. &open_rcar},
  185. [SOC_FW_KEY_CERT_ID] = {
  186. &rcar_dev_handle,
  187. (uintptr_t) &bl31_key_cert_file_spec,
  188. &open_rcar},
  189. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  190. &rcar_dev_handle,
  191. (uintptr_t) &bl32_key_cert_file_spec,
  192. &open_rcar},
  193. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  194. &rcar_dev_handle,
  195. (uintptr_t) &bl33_key_cert_file_spec,
  196. &open_rcar},
  197. [BL332_KEY_CERT_ID] = {
  198. &rcar_dev_handle,
  199. (uintptr_t) &bl332_key_cert_file_spec,
  200. &open_rcar},
  201. [BL333_KEY_CERT_ID] = {
  202. &rcar_dev_handle,
  203. (uintptr_t) &bl333_key_cert_file_spec,
  204. &open_rcar},
  205. [BL334_KEY_CERT_ID] = {
  206. &rcar_dev_handle,
  207. (uintptr_t) &bl334_key_cert_file_spec,
  208. &open_rcar},
  209. [BL335_KEY_CERT_ID] = {
  210. &rcar_dev_handle,
  211. (uintptr_t) &bl335_key_cert_file_spec,
  212. &open_rcar},
  213. [BL336_KEY_CERT_ID] = {
  214. &rcar_dev_handle,
  215. (uintptr_t) &bl336_key_cert_file_spec,
  216. &open_rcar},
  217. [BL337_KEY_CERT_ID] = {
  218. &rcar_dev_handle,
  219. (uintptr_t) &bl337_key_cert_file_spec,
  220. &open_rcar},
  221. [BL338_KEY_CERT_ID] = {
  222. &rcar_dev_handle,
  223. (uintptr_t) &bl338_key_cert_file_spec,
  224. &open_rcar},
  225. [SOC_FW_CONTENT_CERT_ID] = {
  226. &rcar_dev_handle,
  227. (uintptr_t) &bl31_cert_file_spec,
  228. &open_rcar},
  229. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  230. &rcar_dev_handle,
  231. (uintptr_t) &bl32_cert_file_spec,
  232. &open_rcar},
  233. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  234. &rcar_dev_handle,
  235. (uintptr_t) &bl33_cert_file_spec,
  236. &open_rcar},
  237. [BL332_CERT_ID] = {
  238. &rcar_dev_handle,
  239. (uintptr_t) &bl332_cert_file_spec,
  240. &open_rcar},
  241. [BL333_CERT_ID] = {
  242. &rcar_dev_handle,
  243. (uintptr_t) &bl333_cert_file_spec,
  244. &open_rcar},
  245. [BL334_CERT_ID] = {
  246. &rcar_dev_handle,
  247. (uintptr_t) &bl334_cert_file_spec,
  248. &open_rcar},
  249. [BL335_CERT_ID] = {
  250. &rcar_dev_handle,
  251. (uintptr_t) &bl335_cert_file_spec,
  252. &open_rcar},
  253. [BL336_CERT_ID] = {
  254. &rcar_dev_handle,
  255. (uintptr_t) &bl336_cert_file_spec,
  256. &open_rcar},
  257. [BL337_CERT_ID] = {
  258. &rcar_dev_handle,
  259. (uintptr_t) &bl337_cert_file_spec,
  260. &open_rcar},
  261. [BL338_CERT_ID] = {
  262. &rcar_dev_handle,
  263. (uintptr_t) &bl338_cert_file_spec,
  264. &open_rcar}, {
  265. #else
  266. {
  267. #endif
  268. 0, 0, 0}
  269. };
  270. static io_drv_spec_t io_drv_spec_memdrv = {
  271. FLASH0_BASE,
  272. FLASH0_SIZE,
  273. 0,
  274. };
  275. static io_drv_spec_t io_drv_spec_emmcdrv = {
  276. 0,
  277. 0,
  278. 0,
  279. };
  280. static struct plat_io_policy drv_policies[] __attribute__ ((section(".data"))) = {
  281. /* FLASH_DEV_ID */
  282. { &memdrv_dev_handle, (uintptr_t) &io_drv_spec_memdrv, &open_memmap, },
  283. /* EMMC_DEV_ID */
  284. { &emmcdrv_dev_handle, (uintptr_t) &io_drv_spec_emmcdrv, &open_emmcdrv, }
  285. };
  286. static int32_t open_rcar(const uintptr_t spec)
  287. {
  288. return io_dev_init(rcar_dev_handle, boot_io_drv_id);
  289. }
  290. static int32_t open_memmap(const uintptr_t spec)
  291. {
  292. uintptr_t handle;
  293. int32_t result;
  294. result = io_dev_init(memdrv_dev_handle, 0);
  295. if (result != IO_SUCCESS)
  296. return result;
  297. result = io_open(memdrv_dev_handle, spec, &handle);
  298. if (result == IO_SUCCESS)
  299. io_close(handle);
  300. return result;
  301. }
  302. static int32_t open_emmcdrv(const uintptr_t spec)
  303. {
  304. return io_dev_init(emmcdrv_dev_handle, 0);
  305. }
  306. void rcar_io_setup(void)
  307. {
  308. const io_dev_connector_t *memmap;
  309. const io_dev_connector_t *rcar;
  310. boot_io_drv_id = FLASH_DEV_ID;
  311. rcar_register_io_dev(&rcar);
  312. rcar_register_io_dev_memdrv(&memmap);
  313. io_dev_open(rcar, 0, &rcar_dev_handle);
  314. io_dev_open(memmap, 0, &memdrv_dev_handle);
  315. }
  316. void rcar_io_emmc_setup(void)
  317. {
  318. const io_dev_connector_t *rcar;
  319. const io_dev_connector_t *emmc;
  320. boot_io_drv_id = EMMC_DEV_ID;
  321. rcar_register_io_dev(&rcar);
  322. rcar_register_io_dev_emmcdrv(&emmc);
  323. io_dev_open(rcar, 0, &rcar_dev_handle);
  324. io_dev_open(emmc, 0, &emmcdrv_dev_handle);
  325. }
  326. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  327. uintptr_t *image_spec)
  328. {
  329. const struct plat_io_policy *policy;
  330. int result;
  331. policy = &policies[image_id];
  332. result = policy->check(policy->image_spec);
  333. if (result != IO_SUCCESS)
  334. return result;
  335. *image_spec = policy->image_spec;
  336. *dev_handle = *(policy->dev_handle);
  337. return IO_SUCCESS;
  338. }
  339. int32_t plat_get_drv_source(uint32_t io_drv_id, uintptr_t *dev_handle,
  340. uintptr_t *image_spec)
  341. {
  342. const struct plat_io_policy *policy;
  343. int32_t result;
  344. policy = &drv_policies[io_drv_id];
  345. result = policy->check(policy->image_spec);
  346. if (result != IO_SUCCESS)
  347. return result;
  348. *image_spec = policy->image_spec;
  349. *dev_handle = *(policy->dev_handle);
  350. return IO_SUCCESS;
  351. }