blob: 11dd97334c4e54022b8a7214cf75a7932b9e0063 [file] [log] [blame]
Haojian Zhuang5f281b32017-05-24 08:45:05 +08001/*
Haojian Zhuangb755da32018-01-25 16:10:14 +08002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Haojian Zhuang5f281b32017-05-24 08:45:05 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Haojian Zhuang5f281b32017-05-24 08:45:05 +08007#include <assert.h>
Haojian Zhuang5f281b32017-05-24 08:45:05 +08008#include <errno.h>
Haojian Zhuang5f281b32017-05-24 08:45:05 +08009#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
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
Haojian Zhuang5f281b32017-05-24 08:45:05 +080025#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
32struct plat_io_policy {
33 uintptr_t *dev_handle;
34 uintptr_t image_spec;
35 int (*check)(const uintptr_t spec);
36};
37
38static const io_dev_connector_t *emmc_dev_con;
39static uintptr_t emmc_dev_handle;
40static const io_dev_connector_t *fip_dev_con;
41static uintptr_t fip_dev_handle;
42
43static int check_emmc(const uintptr_t spec);
44static int check_fip(const uintptr_t spec);
45
46static const io_block_spec_t emmc_fip_spec = {
47 .offset = HIKEY_FIP_BASE,
48 .length = HIKEY_FIP_MAX_SIZE,
49};
50
51static const io_block_dev_spec_t emmc_dev_spec = {
52 /* It's used as temp buffer in block driver. */
Roberto Vargas82477962017-10-23 08:22:17 +010053#ifdef IMAGE_BL1
Haojian Zhuang5f281b32017-05-24 08:45:05 +080054 .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 = {
Haojian Zhuange9713772018-08-04 18:07:10 +080065 .read = mmc_read_blocks,
66 .write = mmc_write_blocks,
Haojian Zhuang5f281b32017-05-24 08:45:05 +080067 },
Haojian Zhuange9713772018-08-04 18:07:10 +080068 .block_size = MMC_BLOCK_SIZE,
Haojian Zhuang5f281b32017-05-24 08:45:05 +080069};
70
Haojian Zhuang5f281b32017-05-24 08:45:05 +080071static const io_uuid_spec_t bl31_uuid_spec = {
72 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
73};
74
Victor Chongb9a8db22017-05-28 00:14:25 +090075static const io_uuid_spec_t bl32_uuid_spec = {
76 .uuid = UUID_SECURE_PAYLOAD_BL32,
77};
78
Victor Chong7d787f52017-08-16 13:53:56 +090079static const io_uuid_spec_t bl32_extra1_uuid_spec = {
80 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
81};
82
83static const io_uuid_spec_t bl32_extra2_uuid_spec = {
84 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
85};
86
Haojian Zhuang5f281b32017-05-24 08:45:05 +080087static const io_uuid_spec_t bl33_uuid_spec = {
88 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
89};
90
91static const io_uuid_spec_t scp_bl2_uuid_spec = {
92 .uuid = UUID_SCP_FIRMWARE_SCP_BL2,
93};
94
Teddy Reed349cf892018-06-22 22:23:36 -040095#if TRUSTED_BOARD_BOOT
96static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
97 .uuid = UUID_TRUSTED_KEY_CERT,
98};
99
100static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
101 .uuid = UUID_SCP_FW_KEY_CERT,
102};
103
104static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
105 .uuid = UUID_SOC_FW_KEY_CERT,
106};
107
108static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
109 .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
110};
111
112static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
113 .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
114};
115
116static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
117 .uuid = UUID_SCP_FW_CONTENT_CERT,
118};
119
120static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
121 .uuid = UUID_SOC_FW_CONTENT_CERT,
122};
123
124static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
125 .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
126};
127
128static 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
Haojian Zhuang5f281b32017-05-24 08:45:05 +0800133static const struct plat_io_policy policies[] = {
134 [FIP_IMAGE_ID] = {
135 &emmc_dev_handle,
136 (uintptr_t)&emmc_fip_spec,
137 check_emmc
138 },
Haojian Zhuang5f281b32017-05-24 08:45:05 +0800139 [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 },
Victor Chongb9a8db22017-05-28 00:14:25 +0900149 [BL32_IMAGE_ID] = {
150 &fip_dev_handle,
151 (uintptr_t)&bl32_uuid_spec,
152 check_fip
153 },
Victor Chong7d787f52017-08-16 13:53:56 +0900154 [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 },
Haojian Zhuang5f281b32017-05-24 08:45:05 +0800164 [BL33_IMAGE_ID] = {
165 &fip_dev_handle,
166 (uintptr_t)&bl33_uuid_spec,
167 check_fip
Teddy Reed349cf892018-06-22 22:23:36 -0400168 },
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 */
Haojian Zhuang5f281b32017-05-24 08:45:05 +0800216};
217
218static 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
232static 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
249void 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 */
273int 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}