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