1 /*
2 * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8
9 #include <common/debug.h>
10 #include <drivers/io/io_driver.h>
11 #include <drivers/io/io_semihosting.h>
12 #include <drivers/io/io_storage.h>
13 #include <lib/semihosting.h>
14 #include <plat/arm/common/plat_arm.h>
15 #include <plat/common/common_def.h>
16
17 /* Semihosting filenames */
18 #define BL2_IMAGE_NAME "bl2.bin"
19 #define BL31_IMAGE_NAME "bl31.bin"
20 #define BL32_IMAGE_NAME "bl32.bin"
21 #define BL33_IMAGE_NAME "bl33.bin"
22 #define TB_FW_CONFIG_NAME "fvp_tb_fw_config.dtb"
23 #define HW_CONFIG_NAME "hw_config.dtb"
24
25 #if TRUSTED_BOARD_BOOT
26 #define TRUSTED_BOOT_FW_CERT_NAME "tb_fw.crt"
27 #define TRUSTED_KEY_CERT_NAME "trusted_key.crt"
28 #define SOC_FW_KEY_CERT_NAME "soc_fw_key.crt"
29 #define TOS_FW_KEY_CERT_NAME "tos_fw_key.crt"
30 #define NT_FW_KEY_CERT_NAME "nt_fw_key.crt"
31 #define SOC_FW_CONTENT_CERT_NAME "soc_fw_content.crt"
32 #define TOS_FW_CONTENT_CERT_NAME "tos_fw_content.crt"
33 #define NT_FW_CONTENT_CERT_NAME "nt_fw_content.crt"
34 #endif /* TRUSTED_BOARD_BOOT */
35
36 /* IO devices */
37 static const io_dev_connector_t *sh_dev_con;
38 static uintptr_t sh_dev_handle;
39
40 static const io_file_spec_t sh_file_spec[] = {
41 [BL2_IMAGE_ID] = {
42 .path = BL2_IMAGE_NAME,
43 .mode = FOPEN_MODE_RB
44 },
45 [BL31_IMAGE_ID] = {
46 .path = BL31_IMAGE_NAME,
47 .mode = FOPEN_MODE_RB
48 },
49 [BL32_IMAGE_ID] = {
50 .path = BL32_IMAGE_NAME,
51 .mode = FOPEN_MODE_RB
52 },
53 [BL33_IMAGE_ID] = {
54 .path = BL33_IMAGE_NAME,
55 .mode = FOPEN_MODE_RB
56 },
57 [TB_FW_CONFIG_ID] = {
58 .path = TB_FW_CONFIG_NAME,
59 .mode = FOPEN_MODE_RB
60 },
61 [HW_CONFIG_ID] = {
62 .path = HW_CONFIG_NAME,
63 .mode = FOPEN_MODE_RB
64 },
65 #if TRUSTED_BOARD_BOOT
66 [TRUSTED_BOOT_FW_CERT_ID] = {
67 .path = TRUSTED_BOOT_FW_CERT_NAME,
68 .mode = FOPEN_MODE_RB
69 },
70 [TRUSTED_KEY_CERT_ID] = {
71 .path = TRUSTED_KEY_CERT_NAME,
72 .mode = FOPEN_MODE_RB
73 },
74 [SOC_FW_KEY_CERT_ID] = {
75 .path = SOC_FW_KEY_CERT_NAME,
76 .mode = FOPEN_MODE_RB
77 },
78 [TRUSTED_OS_FW_KEY_CERT_ID] = {
79 .path = TOS_FW_KEY_CERT_NAME,
80 .mode = FOPEN_MODE_RB
81 },
82 [NON_TRUSTED_FW_KEY_CERT_ID] = {
83 .path = NT_FW_KEY_CERT_NAME,
84 .mode = FOPEN_MODE_RB
85 },
86 [SOC_FW_CONTENT_CERT_ID] = {
87 .path = SOC_FW_CONTENT_CERT_NAME,
88 .mode = FOPEN_MODE_RB
89 },
90 [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
91 .path = TOS_FW_CONTENT_CERT_NAME,
92 .mode = FOPEN_MODE_RB
93 },
94 [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
95 .path = NT_FW_CONTENT_CERT_NAME,
96 .mode = FOPEN_MODE_RB
97 },
98 #endif /* TRUSTED_BOARD_BOOT */
99 };
100
101
open_semihosting(const uintptr_t spec)102 static int open_semihosting(const uintptr_t spec)
103 {
104 int result;
105 uintptr_t local_image_handle;
106
107 /* See if the file exists on semi-hosting.*/
108 result = io_dev_init(sh_dev_handle, (uintptr_t)NULL);
109 if (result == 0) {
110 result = io_open(sh_dev_handle, spec, &local_image_handle);
111 if (result == 0) {
112 VERBOSE("Using Semi-hosting IO\n");
113 io_close(local_image_handle);
114 }
115 }
116 return result;
117 }
118
plat_arm_io_setup(void)119 void plat_arm_io_setup(void)
120 {
121 int io_result;
122
123 arm_io_setup();
124
125 /* Register the additional IO devices on this platform */
126 io_result = register_io_dev_sh(&sh_dev_con);
127 assert(io_result == 0);
128
129 /* Open connections to devices and cache the handles */
130 io_result = io_dev_open(sh_dev_con, (uintptr_t)NULL, &sh_dev_handle);
131 assert(io_result == 0);
132
133 /* Ignore improbable errors in release builds */
134 (void)io_result;
135 }
136
137 /*
138 * FVP provides semihosting as an alternative to load images
139 */
plat_arm_get_alt_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)140 int plat_arm_get_alt_image_source(unsigned int image_id, uintptr_t *dev_handle,
141 uintptr_t *image_spec)
142 {
143 int result = open_semihosting((const uintptr_t)&sh_file_spec[image_id]);
144 if (result == 0) {
145 *dev_handle = sh_dev_handle;
146 *image_spec = (uintptr_t)&sh_file_spec[image_id];
147 }
148
149 return result;
150 }
151