hikey960_io_storage.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (c) 2017-2022, 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/ufs.h>
  13. #include <drivers/io/io_block.h>
  14. #include <drivers/io/io_driver.h>
  15. #include <drivers/io/io_fip.h>
  16. #include <drivers/io/io_memmap.h>
  17. #include <drivers/io/io_storage.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 "hikey960_def.h"
  23. #include "hikey960_private.h"
  24. struct plat_io_policy {
  25. uintptr_t *dev_handle;
  26. uintptr_t image_spec;
  27. int (*check)(const uintptr_t spec);
  28. };
  29. static const io_dev_connector_t *ufs_dev_con, *fip_dev_con;
  30. static uintptr_t ufs_dev_handle, fip_dev_handle;
  31. static int check_ufs(const uintptr_t spec);
  32. static int check_fip(const uintptr_t spec);
  33. size_t ufs_read_lun3_blks(int lba, uintptr_t buf, size_t size);
  34. size_t ufs_write_lun3_blks(int lba, const uintptr_t buf, size_t size);
  35. static io_block_spec_t ufs_fip_spec;
  36. static const io_block_spec_t ufs_gpt_spec = {
  37. .offset = 0,
  38. .length = PLAT_PARTITION_BLOCK_SIZE *
  39. (PLAT_PARTITION_MAX_ENTRIES / 4 + 2),
  40. };
  41. /* Fastboot serial number stored within first UFS device blocks */
  42. static const io_block_spec_t ufs_fastboot_spec = {
  43. .offset = UFS_BASE,
  44. .length = 1 << 20,
  45. };
  46. static const io_block_dev_spec_t ufs_dev_spec = {
  47. /* It's used as temp buffer in block driver. */
  48. .buffer = {
  49. .offset = HIKEY960_UFS_DATA_BASE,
  50. .length = HIKEY960_UFS_DATA_SIZE,
  51. },
  52. .ops = {
  53. .read = ufs_read_lun3_blks,
  54. .write = ufs_write_lun3_blks,
  55. },
  56. .block_size = UFS_BLOCK_SIZE,
  57. };
  58. static const io_uuid_spec_t scp_bl2_uuid_spec = {
  59. .uuid = UUID_SCP_FIRMWARE_SCP_BL2,
  60. };
  61. static const io_uuid_spec_t bl31_uuid_spec = {
  62. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  63. };
  64. static const io_uuid_spec_t bl32_uuid_spec = {
  65. .uuid = UUID_SECURE_PAYLOAD_BL32,
  66. };
  67. static const io_uuid_spec_t bl32_extra1_uuid_spec = {
  68. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
  69. };
  70. static const io_uuid_spec_t bl32_extra2_uuid_spec = {
  71. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
  72. };
  73. #ifdef SPD_spmd
  74. static const io_uuid_spec_t bl32_tos_fw_spec = {
  75. .uuid = UUID_TOS_FW_CONFIG,
  76. };
  77. #endif
  78. static const io_uuid_spec_t bl33_uuid_spec = {
  79. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  80. };
  81. #if TRUSTED_BOARD_BOOT
  82. static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
  83. .uuid = UUID_TRUSTED_KEY_CERT,
  84. };
  85. static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
  86. .uuid = UUID_SCP_FW_KEY_CERT,
  87. };
  88. static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
  89. .uuid = UUID_SOC_FW_KEY_CERT,
  90. };
  91. static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
  92. .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
  93. };
  94. static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
  95. .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
  96. };
  97. static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
  98. .uuid = UUID_SCP_FW_CONTENT_CERT,
  99. };
  100. static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
  101. .uuid = UUID_SOC_FW_CONTENT_CERT,
  102. };
  103. static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
  104. .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
  105. };
  106. static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
  107. .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
  108. };
  109. #endif /* TRUSTED_BOARD_BOOT */
  110. static const struct plat_io_policy policies[] = {
  111. [FIP_IMAGE_ID] = {
  112. &ufs_dev_handle,
  113. (uintptr_t)&ufs_fip_spec,
  114. check_ufs
  115. },
  116. [SCP_BL2_IMAGE_ID] = {
  117. &fip_dev_handle,
  118. (uintptr_t)&scp_bl2_uuid_spec,
  119. check_fip
  120. },
  121. [BL31_IMAGE_ID] = {
  122. &fip_dev_handle,
  123. (uintptr_t)&bl31_uuid_spec,
  124. check_fip
  125. },
  126. [BL32_IMAGE_ID] = {
  127. &fip_dev_handle,
  128. (uintptr_t)&bl32_uuid_spec,
  129. check_fip
  130. },
  131. [BL32_EXTRA1_IMAGE_ID] = {
  132. &fip_dev_handle,
  133. (uintptr_t)&bl32_extra1_uuid_spec,
  134. check_fip
  135. },
  136. [BL32_EXTRA2_IMAGE_ID] = {
  137. &fip_dev_handle,
  138. (uintptr_t)&bl32_extra2_uuid_spec,
  139. check_fip
  140. },
  141. #ifdef SPD_spmd
  142. [TOS_FW_CONFIG_ID] = {
  143. &fip_dev_handle,
  144. (uintptr_t)&bl32_tos_fw_spec,
  145. check_fip
  146. },
  147. #endif
  148. [BL33_IMAGE_ID] = {
  149. &fip_dev_handle,
  150. (uintptr_t)&bl33_uuid_spec,
  151. check_fip
  152. },
  153. #if TRUSTED_BOARD_BOOT
  154. [TRUSTED_KEY_CERT_ID] = {
  155. &fip_dev_handle,
  156. (uintptr_t)&trusted_key_cert_uuid_spec,
  157. check_fip
  158. },
  159. [SCP_FW_KEY_CERT_ID] = {
  160. &fip_dev_handle,
  161. (uintptr_t)&scp_fw_key_cert_uuid_spec,
  162. check_fip
  163. },
  164. [SOC_FW_KEY_CERT_ID] = {
  165. &fip_dev_handle,
  166. (uintptr_t)&soc_fw_key_cert_uuid_spec,
  167. check_fip
  168. },
  169. [TRUSTED_OS_FW_KEY_CERT_ID] = {
  170. &fip_dev_handle,
  171. (uintptr_t)&tos_fw_key_cert_uuid_spec,
  172. check_fip
  173. },
  174. [NON_TRUSTED_FW_KEY_CERT_ID] = {
  175. &fip_dev_handle,
  176. (uintptr_t)&nt_fw_key_cert_uuid_spec,
  177. check_fip
  178. },
  179. [SCP_FW_CONTENT_CERT_ID] = {
  180. &fip_dev_handle,
  181. (uintptr_t)&scp_fw_cert_uuid_spec,
  182. check_fip
  183. },
  184. [SOC_FW_CONTENT_CERT_ID] = {
  185. &fip_dev_handle,
  186. (uintptr_t)&soc_fw_cert_uuid_spec,
  187. check_fip
  188. },
  189. [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
  190. &fip_dev_handle,
  191. (uintptr_t)&tos_fw_cert_uuid_spec,
  192. check_fip
  193. },
  194. [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
  195. &fip_dev_handle,
  196. (uintptr_t)&nt_fw_cert_uuid_spec,
  197. check_fip
  198. },
  199. #endif /* TRUSTED_BOARD_BOOT */
  200. [GPT_IMAGE_ID] = {
  201. &ufs_dev_handle,
  202. (uintptr_t)&ufs_gpt_spec,
  203. check_ufs
  204. },
  205. };
  206. static int check_ufs(const uintptr_t spec)
  207. {
  208. int result;
  209. uintptr_t local_handle;
  210. result = io_dev_init(ufs_dev_handle, (uintptr_t)NULL);
  211. if (result == 0) {
  212. result = io_open(ufs_dev_handle, spec, &local_handle);
  213. if (result == 0)
  214. io_close(local_handle);
  215. }
  216. return result;
  217. }
  218. static int check_fip(const uintptr_t spec)
  219. {
  220. int result;
  221. uintptr_t local_image_handle;
  222. /* See if a Firmware Image Package is available */
  223. result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  224. if (result == 0) {
  225. result = io_open(fip_dev_handle, spec, &local_image_handle);
  226. if (result == 0) {
  227. VERBOSE("Using FIP\n");
  228. io_close(local_image_handle);
  229. }
  230. }
  231. return result;
  232. }
  233. int hikey960_load_serialno(uint64_t *serno)
  234. {
  235. int result;
  236. size_t len = 0;
  237. uintptr_t local_handle;
  238. uint64_t buf[HIKEY960_SERIAL_NUMBER_SIZE / sizeof(uint64_t)];
  239. if (serno == NULL) {
  240. return -1;
  241. }
  242. result = io_dev_init(ufs_dev_handle, (uintptr_t)NULL);
  243. if (result != 0) {
  244. return result;
  245. }
  246. result = io_open(ufs_dev_handle,
  247. (uintptr_t)&ufs_fastboot_spec, &local_handle);
  248. if (result != 0) {
  249. return result;
  250. }
  251. result = io_seek(local_handle, IO_SEEK_SET,
  252. HIKEY960_SERIAL_NUMBER_LBA * UFS_BLOCK_SIZE);
  253. if (result != 0) {
  254. goto closing;
  255. }
  256. result = io_read(local_handle, (uintptr_t)buf,
  257. HIKEY960_SERIAL_NUMBER_SIZE, &len);
  258. if (result != 0) {
  259. goto closing;
  260. }
  261. if (len != HIKEY960_SERIAL_NUMBER_SIZE) {
  262. result = -1;
  263. goto closing;
  264. }
  265. /* UEFI fastboot app stores a 16 bytes blob */
  266. /* We extract only relevant 8 bytes serial number */
  267. *serno = buf[1];
  268. closing:
  269. io_close(local_handle);
  270. return result;
  271. }
  272. void hikey960_io_setup(void)
  273. {
  274. int result;
  275. result = register_io_dev_block(&ufs_dev_con);
  276. assert(result == 0);
  277. result = register_io_dev_fip(&fip_dev_con);
  278. assert(result == 0);
  279. result = io_dev_open(ufs_dev_con, (uintptr_t)&ufs_dev_spec,
  280. &ufs_dev_handle);
  281. assert(result == 0);
  282. result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
  283. assert(result == 0);
  284. /* Ignore improbable errors in release builds */
  285. (void)result;
  286. }
  287. int hikey960_set_fip_addr(unsigned int image_id, const char *name)
  288. {
  289. const partition_entry_t *entry;
  290. if (ufs_fip_spec.length == 0) {
  291. partition_init(GPT_IMAGE_ID);
  292. entry = get_partition_entry(name);
  293. if (entry == NULL) {
  294. ERROR("Could NOT find the %s partition!\n", name);
  295. return -ENOENT;
  296. }
  297. ufs_fip_spec.offset = entry->start;
  298. ufs_fip_spec.length = entry->length;
  299. }
  300. return 0;
  301. }
  302. /* Return an IO device handle and specification which can be used to access
  303. * an image. Use this to enforce platform load policy
  304. */
  305. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  306. uintptr_t *image_spec)
  307. {
  308. int result;
  309. const struct plat_io_policy *policy;
  310. assert(image_id < ARRAY_SIZE(policies));
  311. policy = &policies[image_id];
  312. result = policy->check(policy->image_spec);
  313. assert(result == 0);
  314. *image_spec = policy->image_spec;
  315. *dev_handle = *(policy->dev_handle);
  316. return result;
  317. }
  318. size_t ufs_read_lun3_blks(int lba, uintptr_t buf, size_t size)
  319. {
  320. return ufs_read_blocks(3, lba, buf, size);
  321. }
  322. size_t ufs_write_lun3_blks(int lba, const uintptr_t buf, size_t size)
  323. {
  324. return ufs_write_blocks(3, lba, buf, size);
  325. }