hikey_io_storage.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <platform_def.h>
  10. #include <arch_helpers.h>
  11. #include <common/debug.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. #include <drivers/mmc.h>
  18. #include <drivers/partition/partition.h>
  19. #include <lib/mmio.h>
  20. #include <lib/semihosting.h>
  21. #include <tools_share/firmware_image_package.h>
  22. #include "hikey_private.h"
  23. #define EMMC_BLOCK_SHIFT 9
  24. /* Page 1024, since only a few pages before 2048 are used as partition table */
  25. #define SERIALNO_EMMC_OFFSET (1024 * 512)
  26. struct plat_io_policy {
  27. uintptr_t *dev_handle;
  28. uintptr_t image_spec;
  29. int (*check)(const uintptr_t spec);
  30. };
  31. static const io_dev_connector_t *emmc_dev_con;
  32. static uintptr_t emmc_dev_handle;
  33. static const io_dev_connector_t *fip_dev_con;
  34. static uintptr_t fip_dev_handle;
  35. static int check_emmc(const uintptr_t spec);
  36. static int check_fip(const uintptr_t spec);
  37. static io_block_spec_t emmc_fip_spec;
  38. static const io_block_spec_t emmc_gpt_spec = {
  39. .offset = 0,
  40. .length = PLAT_PARTITION_BLOCK_SIZE *
  41. (PLAT_PARTITION_MAX_ENTRIES / 4 + 2),
  42. };
  43. static const io_block_dev_spec_t emmc_dev_spec = {
  44. /* It's used as temp buffer in block driver. */
  45. #ifdef IMAGE_BL1
  46. .buffer = {
  47. .offset = HIKEY_BL1_MMC_DATA_BASE,
  48. .length = HIKEY_BL1_MMC_DATA_SIZE,
  49. },
  50. #else
  51. .buffer = {
  52. .offset = HIKEY_MMC_DATA_BASE,
  53. .length = HIKEY_MMC_DATA_SIZE,
  54. },
  55. #endif
  56. .ops = {
  57. .read = mmc_read_blocks,
  58. .write = mmc_write_blocks,
  59. },
  60. .block_size = MMC_BLOCK_SIZE,
  61. };
  62. static const io_uuid_spec_t bl31_uuid_spec = {
  63. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  64. };
  65. static const io_uuid_spec_t bl32_uuid_spec = {
  66. .uuid = UUID_SECURE_PAYLOAD_BL32,
  67. };
  68. static const io_uuid_spec_t bl32_extra1_uuid_spec = {
  69. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
  70. };
  71. static const io_uuid_spec_t bl32_extra2_uuid_spec = {
  72. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
  73. };
  74. static const io_uuid_spec_t bl33_uuid_spec = {
  75. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  76. };
  77. static const io_uuid_spec_t scp_bl2_uuid_spec = {
  78. .uuid = UUID_SCP_FIRMWARE_SCP_BL2,
  79. };
  80. #if TRUSTED_BOARD_BOOT
  81. static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
  82. .uuid = UUID_TRUSTED_KEY_CERT,
  83. };
  84. static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
  85. .uuid = UUID_SCP_FW_KEY_CERT,
  86. };
  87. static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
  88. .uuid = UUID_SOC_FW_KEY_CERT,
  89. };
  90. static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
  91. .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
  92. };
  93. static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
  94. .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
  95. };
  96. static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
  97. .uuid = UUID_SCP_FW_CONTENT_CERT,
  98. };
  99. static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
  100. .uuid = UUID_SOC_FW_CONTENT_CERT,
  101. };
  102. static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
  103. .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
  104. };
  105. static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
  106. .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
  107. };
  108. #endif /* TRUSTED_BOARD_BOOT */
  109. static const struct plat_io_policy policies[] = {
  110. [FIP_IMAGE_ID] = {
  111. &emmc_dev_handle,
  112. (uintptr_t)&emmc_fip_spec,
  113. check_emmc
  114. },
  115. [SCP_BL2_IMAGE_ID] = {
  116. &fip_dev_handle,
  117. (uintptr_t)&scp_bl2_uuid_spec,
  118. check_fip
  119. },
  120. [BL31_IMAGE_ID] = {
  121. &fip_dev_handle,
  122. (uintptr_t)&bl31_uuid_spec,
  123. check_fip
  124. },
  125. [BL32_IMAGE_ID] = {
  126. &fip_dev_handle,
  127. (uintptr_t)&bl32_uuid_spec,
  128. check_fip
  129. },
  130. [BL32_EXTRA1_IMAGE_ID] = {
  131. &fip_dev_handle,
  132. (uintptr_t)&bl32_extra1_uuid_spec,
  133. check_fip
  134. },
  135. [BL32_EXTRA2_IMAGE_ID] = {
  136. &fip_dev_handle,
  137. (uintptr_t)&bl32_extra2_uuid_spec,
  138. check_fip
  139. },
  140. [BL33_IMAGE_ID] = {
  141. &fip_dev_handle,
  142. (uintptr_t)&bl33_uuid_spec,
  143. check_fip
  144. },
  145. #if TRUSTED_BOARD_BOOT
  146. [TRUSTED_KEY_CERT_ID] = {
  147. &fip_dev_handle,
  148. (uintptr_t)&trusted_key_cert_uuid_spec,
  149. check_fip
  150. },
  151. [SCP_FW_KEY_CERT_ID] = {
  152. &fip_dev_handle,
  153. (uintptr_t)&scp_fw_key_cert_uuid_spec,
  154. check_fip
  155. },
  156. [SOC_FW_KEY_CERT_ID] = {
  157. &fip_dev_handle,
  158. (uintptr_t)&soc_fw_key_cert_uuid_spec,
  159. check_fip
  160. },
  161. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  162. &fip_dev_handle,
  163. (uintptr_t)&tos_fw_key_cert_uuid_spec,
  164. check_fip
  165. },
  166. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  167. &fip_dev_handle,
  168. (uintptr_t)&nt_fw_key_cert_uuid_spec,
  169. check_fip
  170. },
  171. [SCP_FW_CONTENT_CERT_ID] = {
  172. &fip_dev_handle,
  173. (uintptr_t)&scp_fw_cert_uuid_spec,
  174. check_fip
  175. },
  176. [SOC_FW_CONTENT_CERT_ID] = {
  177. &fip_dev_handle,
  178. (uintptr_t)&soc_fw_cert_uuid_spec,
  179. check_fip
  180. },
  181. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  182. &fip_dev_handle,
  183. (uintptr_t)&tos_fw_cert_uuid_spec,
  184. check_fip
  185. },
  186. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  187. &fip_dev_handle,
  188. (uintptr_t)&nt_fw_cert_uuid_spec,
  189. check_fip
  190. },
  191. #endif /* TRUSTED_BOARD_BOOT */
  192. [GPT_IMAGE_ID] = {
  193. &emmc_dev_handle,
  194. (uintptr_t)&emmc_gpt_spec,
  195. check_emmc
  196. },
  197. };
  198. static int check_emmc(const uintptr_t spec)
  199. {
  200. int result;
  201. uintptr_t local_handle;
  202. result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL);
  203. if (result == 0) {
  204. result = io_open(emmc_dev_handle, spec, &local_handle);
  205. if (result == 0)
  206. io_close(local_handle);
  207. }
  208. return result;
  209. }
  210. static int check_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. void hikey_io_setup(void)
  226. {
  227. int result;
  228. result = register_io_dev_block(&emmc_dev_con);
  229. assert(result == 0);
  230. result = register_io_dev_fip(&fip_dev_con);
  231. assert(result == 0);
  232. result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec,
  233. &emmc_dev_handle);
  234. assert(result == 0);
  235. result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
  236. assert(result == 0);
  237. /* Ignore improbable errors in release builds */
  238. (void)result;
  239. }
  240. int hikey_set_fip_addr(unsigned int image_id, const char *name)
  241. {
  242. const partition_entry_t *entry;
  243. if (emmc_fip_spec.length == 0) {
  244. partition_init(GPT_IMAGE_ID);
  245. entry = get_partition_entry(name);
  246. if (entry == NULL) {
  247. ERROR("Could NOT find the %s partition!\n", name);
  248. return -ENOENT;
  249. }
  250. emmc_fip_spec.offset = entry->start;
  251. emmc_fip_spec.length = entry->length;
  252. }
  253. return 0;
  254. }
  255. /* Return an IO device handle and specification which can be used to access
  256. * an image. Use this to enforce platform load policy
  257. */
  258. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  259. uintptr_t *image_spec)
  260. {
  261. int result;
  262. const struct plat_io_policy *policy;
  263. assert(image_id < ARRAY_SIZE(policies));
  264. policy = &policies[image_id];
  265. result = policy->check(policy->image_spec);
  266. assert(result == 0);
  267. *image_spec = policy->image_spec;
  268. *dev_handle = *(policy->dev_handle);
  269. return result;
  270. }