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