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