blob: fd610d8c0c667a24451bbbcc8b164aa8e89875f7 [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>
Haojian Zhuang53e70df2019-09-14 19:18:01 +080021#include <drivers/partition/partition.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000022#include <lib/mmio.h>
23#include <lib/semihosting.h>
24#include <tools_share/firmware_image_package.h>
25
Haojian Zhuang5f281b32017-05-24 08:45:05 +080026#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
33struct plat_io_policy {
34 uintptr_t *dev_handle;
35 uintptr_t image_spec;
36 int (*check)(const uintptr_t spec);
37};
38
39static const io_dev_connector_t *emmc_dev_con;
40static uintptr_t emmc_dev_handle;
41static const io_dev_connector_t *fip_dev_con;
42static uintptr_t fip_dev_handle;
43
44static int check_emmc(const uintptr_t spec);
45static int check_fip(const uintptr_t spec);
46
Haojian Zhuang53e70df2019-09-14 19:18:01 +080047static io_block_spec_t emmc_fip_spec;
48
49static const io_block_spec_t emmc_gpt_spec = {
50 .offset = 0,
51 .length = PLAT_PARTITION_BLOCK_SIZE *
52 (PLAT_PARTITION_MAX_ENTRIES / 4 + 2),
Haojian Zhuang5f281b32017-05-24 08:45:05 +080053};
54
55static const io_block_dev_spec_t emmc_dev_spec = {
56 /* It's used as temp buffer in block driver. */
Roberto Vargas82477962017-10-23 08:22:17 +010057#ifdef IMAGE_BL1
Haojian Zhuang5f281b32017-05-24 08:45:05 +080058 .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 = {
Haojian Zhuange9713772018-08-04 18:07:10 +080069 .read = mmc_read_blocks,
70 .write = mmc_write_blocks,
Haojian Zhuang5f281b32017-05-24 08:45:05 +080071 },
Haojian Zhuange9713772018-08-04 18:07:10 +080072 .block_size = MMC_BLOCK_SIZE,
Haojian Zhuang5f281b32017-05-24 08:45:05 +080073};
74
Haojian Zhuang5f281b32017-05-24 08:45:05 +080075static const io_uuid_spec_t bl31_uuid_spec = {
76 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
77};
78
Victor Chongb9a8db22017-05-28 00:14:25 +090079static const io_uuid_spec_t bl32_uuid_spec = {
80 .uuid = UUID_SECURE_PAYLOAD_BL32,
81};
82
Victor Chong7d787f52017-08-16 13:53:56 +090083static const io_uuid_spec_t bl32_extra1_uuid_spec = {
84 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
85};
86
87static const io_uuid_spec_t bl32_extra2_uuid_spec = {
88 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
89};
90
Haojian Zhuang5f281b32017-05-24 08:45:05 +080091static const io_uuid_spec_t bl33_uuid_spec = {
92 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
93};
94
95static const io_uuid_spec_t scp_bl2_uuid_spec = {
96 .uuid = UUID_SCP_FIRMWARE_SCP_BL2,
97};
98
Teddy Reed349cf892018-06-22 22:23:36 -040099#if TRUSTED_BOARD_BOOT
100static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
101 .uuid = UUID_TRUSTED_KEY_CERT,
102};
103
104static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
105 .uuid = UUID_SCP_FW_KEY_CERT,
106};
107
108static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
109 .uuid = UUID_SOC_FW_KEY_CERT,
110};
111
112static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
113 .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
114};
115
116static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
117 .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
118};
119
120static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
121 .uuid = UUID_SCP_FW_CONTENT_CERT,
122};
123
124static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
125 .uuid = UUID_SOC_FW_CONTENT_CERT,
126};
127
128static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
129 .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
130};
131
132static 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
Haojian Zhuang5f281b32017-05-24 08:45:05 +0800137static const struct plat_io_policy policies[] = {
138 [FIP_IMAGE_ID] = {
139 &emmc_dev_handle,
140 (uintptr_t)&emmc_fip_spec,
141 check_emmc
142 },
Haojian Zhuang5f281b32017-05-24 08:45:05 +0800143 [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 },
Victor Chongb9a8db22017-05-28 00:14:25 +0900153 [BL32_IMAGE_ID] = {
154 &fip_dev_handle,
155 (uintptr_t)&bl32_uuid_spec,
156 check_fip
157 },
Victor Chong7d787f52017-08-16 13:53:56 +0900158 [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 },
Haojian Zhuang5f281b32017-05-24 08:45:05 +0800168 [BL33_IMAGE_ID] = {
169 &fip_dev_handle,
170 (uintptr_t)&bl33_uuid_spec,
171 check_fip
Teddy Reed349cf892018-06-22 22:23:36 -0400172 },
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 */
Haojian Zhuang53e70df2019-09-14 19:18:01 +0800220 [GPT_IMAGE_ID] = {
221 &emmc_dev_handle,
222 (uintptr_t)&emmc_gpt_spec,
223 check_emmc
224 },
Haojian Zhuang5f281b32017-05-24 08:45:05 +0800225};
226
227static 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
241static 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
258void 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
Haojian Zhuang53e70df2019-09-14 19:18:01 +0800279int 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
Haojian Zhuang5f281b32017-05-24 08:45:05 +0800296/* Return an IO device handle and specification which can be used to access
297 * an image. Use this to enforce platform load policy
298 */
299int 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}