1 /*
2  * Copyright (c) 2014-2020, 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 SOC_FW_CONFIG_NAME		"fvp_soc_fw_config.dtb"
24 #define TOS_FW_CONFIG_NAME		"fvp_tsp_fw_config.dtb"
25 #define NT_FW_CONFIG_NAME		"fvp_nt_fw_config.dtb"
26 #define FW_CONFIG_NAME			"fvp_fw_config.dtb"
27 #define HW_CONFIG_NAME			"hw_config.dtb"
28 
29 #if TRUSTED_BOARD_BOOT
30 #define TRUSTED_BOOT_FW_CERT_NAME	"tb_fw.crt"
31 #define TRUSTED_KEY_CERT_NAME		"trusted_key.crt"
32 #define SOC_FW_KEY_CERT_NAME		"soc_fw_key.crt"
33 #define TOS_FW_KEY_CERT_NAME		"tos_fw_key.crt"
34 #define NT_FW_KEY_CERT_NAME		"nt_fw_key.crt"
35 #define SOC_FW_CONTENT_CERT_NAME	"soc_fw_content.crt"
36 #define TOS_FW_CONTENT_CERT_NAME	"tos_fw_content.crt"
37 #define NT_FW_CONTENT_CERT_NAME		"nt_fw_content.crt"
38 #endif /* TRUSTED_BOARD_BOOT */
39 
40 /* IO devices */
41 static const io_dev_connector_t *sh_dev_con;
42 static uintptr_t sh_dev_handle;
43 
44 static const io_file_spec_t sh_file_spec[] = {
45 	[BL2_IMAGE_ID] = {
46 		.path = BL2_IMAGE_NAME,
47 		.mode = FOPEN_MODE_RB
48 	},
49 	[BL31_IMAGE_ID] = {
50 		.path = BL31_IMAGE_NAME,
51 		.mode = FOPEN_MODE_RB
52 	},
53 	[BL32_IMAGE_ID] = {
54 		.path = BL32_IMAGE_NAME,
55 		.mode = FOPEN_MODE_RB
56 	},
57 	[BL33_IMAGE_ID] = {
58 		.path = BL33_IMAGE_NAME,
59 		.mode = FOPEN_MODE_RB
60 	},
61 	[TB_FW_CONFIG_ID] = {
62 		.path = TB_FW_CONFIG_NAME,
63 		.mode = FOPEN_MODE_RB
64 	},
65 	[SOC_FW_CONFIG_ID] = {
66 		.path = SOC_FW_CONFIG_NAME,
67 		.mode = FOPEN_MODE_RB
68 	},
69 	[TOS_FW_CONFIG_ID] = {
70 		.path = TOS_FW_CONFIG_NAME,
71 		.mode = FOPEN_MODE_RB
72 	},
73 	[NT_FW_CONFIG_ID] = {
74 		.path = NT_FW_CONFIG_NAME,
75 		.mode = FOPEN_MODE_RB
76 	},
77 	[FW_CONFIG_ID] = {
78 		.path = FW_CONFIG_NAME,
79 		.mode = FOPEN_MODE_RB
80 	},
81 	[HW_CONFIG_ID] = {
82 		.path = HW_CONFIG_NAME,
83 		.mode = FOPEN_MODE_RB
84 	},
85 #if TRUSTED_BOARD_BOOT
86 	[TRUSTED_BOOT_FW_CERT_ID] = {
87 		.path = TRUSTED_BOOT_FW_CERT_NAME,
88 		.mode = FOPEN_MODE_RB
89 	},
90 	[TRUSTED_KEY_CERT_ID] = {
91 		.path = TRUSTED_KEY_CERT_NAME,
92 		.mode = FOPEN_MODE_RB
93 	},
94 	[SOC_FW_KEY_CERT_ID] = {
95 		.path = SOC_FW_KEY_CERT_NAME,
96 		.mode = FOPEN_MODE_RB
97 	},
98 	[TRUSTED_OS_FW_KEY_CERT_ID] = {
99 		.path = TOS_FW_KEY_CERT_NAME,
100 		.mode = FOPEN_MODE_RB
101 	},
102 	[NON_TRUSTED_FW_KEY_CERT_ID] = {
103 		.path = NT_FW_KEY_CERT_NAME,
104 		.mode = FOPEN_MODE_RB
105 	},
106 	[SOC_FW_CONTENT_CERT_ID] = {
107 		.path = SOC_FW_CONTENT_CERT_NAME,
108 		.mode = FOPEN_MODE_RB
109 	},
110 	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {
111 		.path = TOS_FW_CONTENT_CERT_NAME,
112 		.mode = FOPEN_MODE_RB
113 	},
114 	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
115 		.path = NT_FW_CONTENT_CERT_NAME,
116 		.mode = FOPEN_MODE_RB
117 	},
118 #endif /* TRUSTED_BOARD_BOOT */
119 };
120 
121 
open_semihosting(const uintptr_t spec)122 static int open_semihosting(const uintptr_t spec)
123 {
124 	int result;
125 	uintptr_t local_image_handle;
126 
127 	/* See if the file exists on semi-hosting.*/
128 	result = io_dev_init(sh_dev_handle, (uintptr_t)NULL);
129 	if (result == 0) {
130 		result = io_open(sh_dev_handle, spec, &local_image_handle);
131 		if (result == 0) {
132 			VERBOSE("Using Semi-hosting IO\n");
133 			io_close(local_image_handle);
134 		}
135 	}
136 	return result;
137 }
138 
plat_arm_io_setup(void)139 void plat_arm_io_setup(void)
140 {
141 	int io_result;
142 
143 	io_result = arm_io_setup();
144 	if (io_result < 0) {
145 		panic();
146 	}
147 
148 	/* Register the additional IO devices on this platform */
149 	io_result = register_io_dev_sh(&sh_dev_con);
150 	if (io_result < 0) {
151 		panic();
152 	}
153 
154 	/* Open connections to devices and cache the handles */
155 	io_result = io_dev_open(sh_dev_con, (uintptr_t)NULL, &sh_dev_handle);
156 	if (io_result < 0) {
157 		panic();
158 	}
159 }
160 
161 /*
162  * FVP provides semihosting as an alternative to load images
163  */
plat_arm_get_alt_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)164 int plat_arm_get_alt_image_source(unsigned int image_id, uintptr_t *dev_handle,
165 				  uintptr_t *image_spec)
166 {
167 	int result = open_semihosting((const uintptr_t)&sh_file_spec[image_id]);
168 	if (result == 0) {
169 		*dev_handle = sh_dev_handle;
170 		*image_spec = (uintptr_t)&sh_file_spec[image_id];
171 	}
172 
173 	return result;
174 }
175