sq_io_storage.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2022, Socionext Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdint.h>
  9. #include <drivers/io/io_block.h>
  10. #include <drivers/io/io_driver.h>
  11. #include <drivers/io/io_fip.h>
  12. #include <drivers/io/io_memmap.h>
  13. #include <lib/mmio.h>
  14. #include <lib/utils_def.h>
  15. #include <lib/xlat_tables/xlat_tables_v2.h>
  16. #include <tools_share/firmware_image_package.h>
  17. #include <platform_def.h>
  18. #include <sq_common.h>
  19. static const io_dev_connector_t *sq_fip_dev_con;
  20. static uintptr_t sq_fip_dev_handle;
  21. static const io_dev_connector_t *sq_backend_dev_con;
  22. static uintptr_t sq_backend_dev_handle;
  23. static io_block_spec_t sq_fip_spec = {
  24. .offset = PLAT_SQ_FIP_IOBASE, /* FIP Image is at 5MB offset on memory-mapped NOR flash */
  25. .length = PLAT_SQ_FIP_MAXSIZE, /* Expected maximum FIP image size */
  26. };
  27. static const io_uuid_spec_t sq_bl2_spec = {
  28. .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
  29. };
  30. static const io_uuid_spec_t sq_bl31_spec = {
  31. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  32. };
  33. static const io_uuid_spec_t sq_bl32_spec = {
  34. .uuid = UUID_SECURE_PAYLOAD_BL32,
  35. };
  36. static const io_uuid_spec_t sq_bl33_spec = {
  37. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  38. };
  39. #if TRUSTED_BOARD_BOOT
  40. static const io_uuid_spec_t sq_tb_fw_cert_spec = {
  41. .uuid = UUID_TRUSTED_BOOT_FW_CERT,
  42. };
  43. static const io_uuid_spec_t sq_trusted_key_cert_spec = {
  44. .uuid = UUID_TRUSTED_KEY_CERT,
  45. };
  46. static const io_uuid_spec_t sq_soc_fw_key_cert_spec = {
  47. .uuid = UUID_SOC_FW_KEY_CERT,
  48. };
  49. static const io_uuid_spec_t sq_tos_fw_key_cert_spec = {
  50. .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
  51. };
  52. static const io_uuid_spec_t sq_nt_fw_key_cert_spec = {
  53. .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
  54. };
  55. static const io_uuid_spec_t sq_soc_fw_cert_spec = {
  56. .uuid = UUID_SOC_FW_CONTENT_CERT,
  57. };
  58. static const io_uuid_spec_t sq_tos_fw_cert_spec = {
  59. .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
  60. };
  61. static const io_uuid_spec_t sq_nt_fw_cert_spec = {
  62. .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
  63. };
  64. #endif /* TRUSTED_BOARD_BOOT */
  65. struct sq_io_policy {
  66. uintptr_t *dev_handle;
  67. uintptr_t image_spec;
  68. uintptr_t init_params;
  69. };
  70. static const struct sq_io_policy sq_io_policies[] = {
  71. [FIP_IMAGE_ID] = {
  72. .dev_handle = &sq_backend_dev_handle,
  73. .image_spec = (uintptr_t)&sq_fip_spec,
  74. },
  75. [BL2_IMAGE_ID] = {
  76. .dev_handle = &sq_fip_dev_handle,
  77. .image_spec = (uintptr_t)&sq_bl2_spec,
  78. .init_params = FIP_IMAGE_ID,
  79. },
  80. [BL31_IMAGE_ID] = {
  81. .dev_handle = &sq_fip_dev_handle,
  82. .image_spec = (uintptr_t)&sq_bl31_spec,
  83. .init_params = FIP_IMAGE_ID,
  84. },
  85. [BL32_IMAGE_ID] = {
  86. .dev_handle = &sq_fip_dev_handle,
  87. .image_spec = (uintptr_t)&sq_bl32_spec,
  88. .init_params = FIP_IMAGE_ID,
  89. },
  90. [BL33_IMAGE_ID] = {
  91. .dev_handle = &sq_fip_dev_handle,
  92. .image_spec = (uintptr_t)&sq_bl33_spec,
  93. .init_params = FIP_IMAGE_ID,
  94. },
  95. #if TRUSTED_BOARD_BOOT
  96. [TRUSTED_BOOT_FW_CERT_ID] = {
  97. .dev_handle = &sq_fip_dev_handle,
  98. .image_spec = (uintptr_t)&sq_tb_fw_cert_spec,
  99. .init_params = FIP_IMAGE_ID,
  100. },
  101. [TRUSTED_KEY_CERT_ID] = {
  102. .dev_handle = &sq_fip_dev_handle,
  103. .image_spec = (uintptr_t)&sq_trusted_key_cert_spec,
  104. .init_params = FIP_IMAGE_ID,
  105. },
  106. [SOC_FW_KEY_CERT_ID] = {
  107. .dev_handle = &sq_fip_dev_handle,
  108. .image_spec = (uintptr_t)&sq_soc_fw_key_cert_spec,
  109. .init_params = FIP_IMAGE_ID,
  110. },
  111. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  112. .dev_handle = &sq_fip_dev_handle,
  113. .image_spec = (uintptr_t)&sq_tos_fw_key_cert_spec,
  114. .init_params = FIP_IMAGE_ID,
  115. },
  116. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  117. .dev_handle = &sq_fip_dev_handle,
  118. .image_spec = (uintptr_t)&sq_nt_fw_key_cert_spec,
  119. .init_params = FIP_IMAGE_ID,
  120. },
  121. [SOC_FW_CONTENT_CERT_ID] = {
  122. .dev_handle = &sq_fip_dev_handle,
  123. .image_spec = (uintptr_t)&sq_soc_fw_cert_spec,
  124. .init_params = FIP_IMAGE_ID,
  125. },
  126. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  127. .dev_handle = &sq_fip_dev_handle,
  128. .image_spec = (uintptr_t)&sq_tos_fw_cert_spec,
  129. .init_params = FIP_IMAGE_ID,
  130. },
  131. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  132. .dev_handle = &sq_fip_dev_handle,
  133. .image_spec = (uintptr_t)&sq_nt_fw_cert_spec,
  134. .init_params = FIP_IMAGE_ID,
  135. },
  136. #endif
  137. };
  138. static int sq_update_fip_spec(void)
  139. {
  140. uint32_t boot_index;
  141. int ret;
  142. ret = mmap_add_dynamic_region(PLAT_SQ_BOOTIDX_BASE, PLAT_SQ_BOOTIDX_BASE,
  143. PAGE_SIZE, MT_RO_DATA | MT_SECURE);
  144. if (ret) {
  145. return ret;
  146. }
  147. boot_index = mmio_read_32(PLAT_SQ_BOOTIDX_BASE);
  148. if (boot_index < PLAT_SQ_MAX_BOOT_INDEX) {
  149. sq_fip_spec.offset += PLAT_SQ_FIP_MAXSIZE * boot_index;
  150. INFO("FWU Enabled: boot_index %d\n", boot_index);
  151. } else {
  152. WARN("FWU Disabled: wrong boot_index value. Fallback to index 0.\n");
  153. }
  154. mmap_remove_dynamic_region(PLAT_SQ_BOOTIDX_BASE, PAGE_SIZE);
  155. return 0;
  156. }
  157. static int sq_io_memmap_setup(void)
  158. {
  159. int ret;
  160. ret = sq_update_fip_spec();
  161. if (ret) {
  162. return ret;
  163. }
  164. ret = mmap_add_dynamic_region(sq_fip_spec.offset, sq_fip_spec.offset,
  165. sq_fip_spec.length, MT_RO_DATA | MT_SECURE);
  166. if (ret) {
  167. return ret;
  168. }
  169. ret = register_io_dev_memmap(&sq_backend_dev_con);
  170. if (ret) {
  171. return ret;
  172. }
  173. return io_dev_open(sq_backend_dev_con, 0, &sq_backend_dev_handle);
  174. }
  175. static int sq_io_fip_setup(void)
  176. {
  177. int ret;
  178. ret = register_io_dev_fip(&sq_fip_dev_con);
  179. if (ret) {
  180. return ret;
  181. }
  182. return io_dev_open(sq_fip_dev_con, 0, &sq_fip_dev_handle);
  183. }
  184. int sq_io_setup(void)
  185. {
  186. int ret;
  187. ret = sq_io_memmap_setup();
  188. if (ret) {
  189. return ret;
  190. }
  191. ret = sq_io_fip_setup();
  192. if (ret) {
  193. return ret;
  194. }
  195. return 0;
  196. }
  197. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  198. uintptr_t *image_spec)
  199. {
  200. uintptr_t init_params;
  201. assert(image_id < ARRAY_SIZE(sq_io_policies));
  202. *dev_handle = *sq_io_policies[image_id].dev_handle;
  203. *image_spec = sq_io_policies[image_id].image_spec;
  204. init_params = sq_io_policies[image_id].init_params;
  205. return io_dev_init(*dev_handle, init_params);
  206. }