blob: 59bba8679ad124cc9dd4679a4e94c5d6bdc3d71a [file] [log] [blame]
Jens Wiklander52c798e2015-12-07 14:37:10 +01001/*
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +01002 * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
Jens Wiklander52c798e2015-12-07 14:37:10 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Jens Wiklander52c798e2015-12-07 14:37:10 +01005 */
6
7#include <assert.h>
Jens Wiklander52c798e2015-12-07 14:37:10 +01008#include <string.h>
9
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <platform_def.h>
11
12#include <common/bl_common.h>
13#include <common/debug.h>
Jens Wiklandera43c1282022-11-22 14:39:26 +010014#include <common/desc_image_load.h>
15#include <common/uuid.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <drivers/io/io_driver.h>
Sumit Garg4ac30342019-11-14 17:34:56 +053017#include <drivers/io/io_encrypted.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <drivers/io/io_fip.h>
19#include <drivers/io/io_memmap.h>
20#include <drivers/io/io_semihosting.h>
21#include <drivers/io/io_storage.h>
22#include <lib/semihosting.h>
23#include <tools_share/firmware_image_package.h>
24
Jens Wiklandera43c1282022-11-22 14:39:26 +010025#include "qemu_private.h"
26
Jens Wiklander52c798e2015-12-07 14:37:10 +010027/* Semihosting filenames */
28#define BL2_IMAGE_NAME "bl2.bin"
29#define BL31_IMAGE_NAME "bl31.bin"
30#define BL32_IMAGE_NAME "bl32.bin"
Jens Wiklandera43c1282022-11-22 14:39:26 +010031#define TB_FW_CONFIG_NAME "tb_fw_config.dtb"
Jens Wiklanderd4b84f02022-11-18 15:40:04 +010032#define TOS_FW_CONFIG_NAME "tos_fw_config.dtb"
Jens Wiklander0acbaaa2017-08-24 13:16:26 +020033#define BL32_EXTRA1_IMAGE_NAME "bl32_extra1.bin"
34#define BL32_EXTRA2_IMAGE_NAME "bl32_extra2.bin"
Jens Wiklander52c798e2015-12-07 14:37:10 +010035#define BL33_IMAGE_NAME "bl33.bin"
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +010036#define RMM_IMAGE_NAME "rmm.bin"
Jens Wiklander52c798e2015-12-07 14:37:10 +010037
38#if TRUSTED_BOARD_BOOT
Michalis Pappas3469cd02017-10-18 09:43:37 +080039#define TRUSTED_BOOT_FW_CERT_NAME "tb_fw.crt"
Jens Wiklander52c798e2015-12-07 14:37:10 +010040#define TRUSTED_KEY_CERT_NAME "trusted_key.crt"
Michalis Pappas3469cd02017-10-18 09:43:37 +080041#define SOC_FW_KEY_CERT_NAME "soc_fw_key.crt"
42#define TOS_FW_KEY_CERT_NAME "tos_fw_key.crt"
43#define NT_FW_KEY_CERT_NAME "nt_fw_key.crt"
44#define SOC_FW_CONTENT_CERT_NAME "soc_fw_content.crt"
45#define TOS_FW_CONTENT_CERT_NAME "tos_fw_content.crt"
46#define NT_FW_CONTENT_CERT_NAME "nt_fw_content.crt"
Jens Wiklander52c798e2015-12-07 14:37:10 +010047#endif /* TRUSTED_BOARD_BOOT */
48
49
50
51/* IO devices */
52static const io_dev_connector_t *fip_dev_con;
53static uintptr_t fip_dev_handle;
54static const io_dev_connector_t *memmap_dev_con;
55static uintptr_t memmap_dev_handle;
56static const io_dev_connector_t *sh_dev_con;
57static uintptr_t sh_dev_handle;
Sumit Garg4ac30342019-11-14 17:34:56 +053058#ifndef DECRYPTION_SUPPORT_none
59static const io_dev_connector_t *enc_dev_con;
60static uintptr_t enc_dev_handle;
61#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +010062
63static const io_block_spec_t fip_block_spec = {
64 .offset = PLAT_QEMU_FIP_BASE,
65 .length = PLAT_QEMU_FIP_MAX_SIZE
66};
67
68static const io_uuid_spec_t bl2_uuid_spec = {
69 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
70};
71
72static const io_uuid_spec_t bl31_uuid_spec = {
73 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
74};
75
76static const io_uuid_spec_t bl32_uuid_spec = {
77 .uuid = UUID_SECURE_PAYLOAD_BL32,
78};
79
Jens Wiklander0acbaaa2017-08-24 13:16:26 +020080static const io_uuid_spec_t bl32_extra1_uuid_spec = {
81 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
82};
83
84static const io_uuid_spec_t bl32_extra2_uuid_spec = {
85 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
86};
87
Jens Wiklandera43c1282022-11-22 14:39:26 +010088static const io_uuid_spec_t tb_fw_config_uuid_spec = {
89 .uuid = UUID_TB_FW_CONFIG,
90};
91
Jens Wiklanderd4b84f02022-11-18 15:40:04 +010092static const io_uuid_spec_t tos_fw_config_uuid_spec = {
93 .uuid = UUID_TOS_FW_CONFIG,
94};
95
Jens Wiklander52c798e2015-12-07 14:37:10 +010096static const io_uuid_spec_t bl33_uuid_spec = {
97 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
98};
99
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100100static const io_uuid_spec_t rmm_uuid_spec = {
101 .uuid = UUID_REALM_MONITOR_MGMT_FIRMWARE,
102};
103
Jens Wiklander52c798e2015-12-07 14:37:10 +0100104#if TRUSTED_BOARD_BOOT
Michalis Pappas3469cd02017-10-18 09:43:37 +0800105static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
106 .uuid = UUID_TRUSTED_BOOT_FW_CERT,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100107};
108
109static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
110 .uuid = UUID_TRUSTED_KEY_CERT,
111};
112
Michalis Pappas3469cd02017-10-18 09:43:37 +0800113static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
114 .uuid = UUID_SOC_FW_KEY_CERT,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100115};
116
Michalis Pappas3469cd02017-10-18 09:43:37 +0800117static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
118 .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100119};
120
Michalis Pappas3469cd02017-10-18 09:43:37 +0800121static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
122 .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100123};
124
Michalis Pappas3469cd02017-10-18 09:43:37 +0800125static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
126 .uuid = UUID_SOC_FW_CONTENT_CERT,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100127};
128
Michalis Pappas3469cd02017-10-18 09:43:37 +0800129static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
130 .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100131};
132
Michalis Pappas3469cd02017-10-18 09:43:37 +0800133static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
134 .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100135};
136#endif /* TRUSTED_BOARD_BOOT */
137
138static const io_file_spec_t sh_file_spec[] = {
139 [BL2_IMAGE_ID] = {
140 .path = BL2_IMAGE_NAME,
141 .mode = FOPEN_MODE_RB
142 },
143 [BL31_IMAGE_ID] = {
144 .path = BL31_IMAGE_NAME,
145 .mode = FOPEN_MODE_RB
146 },
147 [BL32_IMAGE_ID] = {
148 .path = BL32_IMAGE_NAME,
149 .mode = FOPEN_MODE_RB
150 },
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200151 [BL32_EXTRA1_IMAGE_ID] = {
152 .path = BL32_EXTRA1_IMAGE_NAME,
153 .mode = FOPEN_MODE_RB
154 },
155 [BL32_EXTRA2_IMAGE_ID] = {
156 .path = BL32_EXTRA2_IMAGE_NAME,
157 .mode = FOPEN_MODE_RB
158 },
Jens Wiklandera43c1282022-11-22 14:39:26 +0100159 [TB_FW_CONFIG_ID] = {
160 .path = TB_FW_CONFIG_NAME,
161 .mode = FOPEN_MODE_RB
162 },
Jens Wiklanderd4b84f02022-11-18 15:40:04 +0100163 [TOS_FW_CONFIG_ID] = {
164 .path = TOS_FW_CONFIG_NAME,
165 .mode = FOPEN_MODE_RB
166 },
Jens Wiklander52c798e2015-12-07 14:37:10 +0100167 [BL33_IMAGE_ID] = {
168 .path = BL33_IMAGE_NAME,
169 .mode = FOPEN_MODE_RB
170 },
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100171 [RMM_IMAGE_ID] = {
172 .path = RMM_IMAGE_NAME,
173 .mode = FOPEN_MODE_RB
174 },
Jens Wiklander52c798e2015-12-07 14:37:10 +0100175#if TRUSTED_BOARD_BOOT
Michalis Pappas3469cd02017-10-18 09:43:37 +0800176 [TRUSTED_BOOT_FW_CERT_ID] = {
177 .path = TRUSTED_BOOT_FW_CERT_NAME,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100178 .mode = FOPEN_MODE_RB
179 },
180 [TRUSTED_KEY_CERT_ID] = {
181 .path = TRUSTED_KEY_CERT_NAME,
182 .mode = FOPEN_MODE_RB
183 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800184 [SOC_FW_KEY_CERT_ID] = {
185 .path = SOC_FW_KEY_CERT_NAME,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100186 .mode = FOPEN_MODE_RB
187 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800188 [TRUSTED_OS_FW_KEY_CERT_ID] = {
189 .path = TOS_FW_KEY_CERT_NAME,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100190 .mode = FOPEN_MODE_RB
191 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800192 [NON_TRUSTED_FW_KEY_CERT_ID] = {
193 .path = NT_FW_KEY_CERT_NAME,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100194 .mode = FOPEN_MODE_RB
195 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800196 [SOC_FW_CONTENT_CERT_ID] = {
197 .path = SOC_FW_CONTENT_CERT_NAME,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100198 .mode = FOPEN_MODE_RB
199 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800200 [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
201 .path = TOS_FW_CONTENT_CERT_NAME,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100202 .mode = FOPEN_MODE_RB
203 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800204 [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
205 .path = NT_FW_CONTENT_CERT_NAME,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100206 .mode = FOPEN_MODE_RB
207 },
208#endif /* TRUSTED_BOARD_BOOT */
209};
210
Jens Wiklander52c798e2015-12-07 14:37:10 +0100211static int open_fip(const uintptr_t spec);
212static int open_memmap(const uintptr_t spec);
Sumit Garg4ac30342019-11-14 17:34:56 +0530213#ifndef DECRYPTION_SUPPORT_none
214static int open_enc_fip(const uintptr_t spec);
215#endif
Jens Wiklander52c798e2015-12-07 14:37:10 +0100216
217struct plat_io_policy {
218 uintptr_t *dev_handle;
219 uintptr_t image_spec;
220 int (*check)(const uintptr_t spec);
221};
222
223/* By default, ARM platforms load images from the FIP */
224static const struct plat_io_policy policies[] = {
225 [FIP_IMAGE_ID] = {
226 &memmap_dev_handle,
227 (uintptr_t)&fip_block_spec,
228 open_memmap
229 },
Sumit Garg4ac30342019-11-14 17:34:56 +0530230 [ENC_IMAGE_ID] = {
231 &fip_dev_handle,
232 (uintptr_t)NULL,
233 open_fip
234 },
Jens Wiklander52c798e2015-12-07 14:37:10 +0100235 [BL2_IMAGE_ID] = {
236 &fip_dev_handle,
237 (uintptr_t)&bl2_uuid_spec,
238 open_fip
239 },
Sumit Garg4ac30342019-11-14 17:34:56 +0530240#if ENCRYPT_BL31 && !defined(DECRYPTION_SUPPORT_none)
241 [BL31_IMAGE_ID] = {
242 &enc_dev_handle,
243 (uintptr_t)&bl31_uuid_spec,
244 open_enc_fip
245 },
246#else
Jens Wiklander52c798e2015-12-07 14:37:10 +0100247 [BL31_IMAGE_ID] = {
248 &fip_dev_handle,
249 (uintptr_t)&bl31_uuid_spec,
250 open_fip
251 },
Sumit Garg4ac30342019-11-14 17:34:56 +0530252#endif
253#if ENCRYPT_BL32 && !defined(DECRYPTION_SUPPORT_none)
254 [BL32_IMAGE_ID] = {
255 &enc_dev_handle,
256 (uintptr_t)&bl32_uuid_spec,
257 open_enc_fip
258 },
259 [BL32_EXTRA1_IMAGE_ID] = {
260 &enc_dev_handle,
261 (uintptr_t)&bl32_extra1_uuid_spec,
262 open_enc_fip
263 },
264 [BL32_EXTRA2_IMAGE_ID] = {
265 &enc_dev_handle,
266 (uintptr_t)&bl32_extra2_uuid_spec,
267 open_enc_fip
268 },
269#else
Jens Wiklander52c798e2015-12-07 14:37:10 +0100270 [BL32_IMAGE_ID] = {
271 &fip_dev_handle,
272 (uintptr_t)&bl32_uuid_spec,
273 open_fip
274 },
Jens Wiklander0acbaaa2017-08-24 13:16:26 +0200275 [BL32_EXTRA1_IMAGE_ID] = {
276 &fip_dev_handle,
277 (uintptr_t)&bl32_extra1_uuid_spec,
278 open_fip
279 },
280 [BL32_EXTRA2_IMAGE_ID] = {
281 &fip_dev_handle,
282 (uintptr_t)&bl32_extra2_uuid_spec,
283 open_fip
284 },
Sumit Garg4ac30342019-11-14 17:34:56 +0530285#endif
Jens Wiklandera43c1282022-11-22 14:39:26 +0100286 [TB_FW_CONFIG_ID] = {
287 &fip_dev_handle,
288 (uintptr_t)&tb_fw_config_uuid_spec,
289 open_fip
290 },
Jens Wiklanderd4b84f02022-11-18 15:40:04 +0100291 [TOS_FW_CONFIG_ID] = {
292 &fip_dev_handle,
293 (uintptr_t)&tos_fw_config_uuid_spec,
294 open_fip
295 },
Jens Wiklander52c798e2015-12-07 14:37:10 +0100296 [BL33_IMAGE_ID] = {
297 &fip_dev_handle,
298 (uintptr_t)&bl33_uuid_spec,
299 open_fip
300 },
Jean-Philippe Bruckerf304bd62023-09-07 17:33:22 +0100301 [RMM_IMAGE_ID] = {
302 &fip_dev_handle,
303 (uintptr_t)&rmm_uuid_spec,
304 open_fip
305 },
306
Jens Wiklander52c798e2015-12-07 14:37:10 +0100307#if TRUSTED_BOARD_BOOT
Michalis Pappas3469cd02017-10-18 09:43:37 +0800308 [TRUSTED_BOOT_FW_CERT_ID] = {
Jens Wiklander52c798e2015-12-07 14:37:10 +0100309 &fip_dev_handle,
Michalis Pappas3469cd02017-10-18 09:43:37 +0800310 (uintptr_t)&tb_fw_cert_uuid_spec,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100311 open_fip
312 },
313 [TRUSTED_KEY_CERT_ID] = {
314 &fip_dev_handle,
315 (uintptr_t)&trusted_key_cert_uuid_spec,
316 open_fip
317 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800318 [SOC_FW_KEY_CERT_ID] = {
Jens Wiklander52c798e2015-12-07 14:37:10 +0100319 &fip_dev_handle,
Michalis Pappas3469cd02017-10-18 09:43:37 +0800320 (uintptr_t)&soc_fw_key_cert_uuid_spec,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100321 open_fip
322 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800323 [TRUSTED_OS_FW_KEY_CERT_ID] = {
Jens Wiklander52c798e2015-12-07 14:37:10 +0100324 &fip_dev_handle,
Michalis Pappas3469cd02017-10-18 09:43:37 +0800325 (uintptr_t)&tos_fw_key_cert_uuid_spec,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100326 open_fip
327 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800328 [NON_TRUSTED_FW_KEY_CERT_ID] = {
Jens Wiklander52c798e2015-12-07 14:37:10 +0100329 &fip_dev_handle,
Michalis Pappas3469cd02017-10-18 09:43:37 +0800330 (uintptr_t)&nt_fw_key_cert_uuid_spec,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100331 open_fip
332 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800333 [SOC_FW_CONTENT_CERT_ID] = {
Jens Wiklander52c798e2015-12-07 14:37:10 +0100334 &fip_dev_handle,
Michalis Pappas3469cd02017-10-18 09:43:37 +0800335 (uintptr_t)&soc_fw_cert_uuid_spec,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100336 open_fip
337 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800338 [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
Jens Wiklander52c798e2015-12-07 14:37:10 +0100339 &fip_dev_handle,
Michalis Pappas3469cd02017-10-18 09:43:37 +0800340 (uintptr_t)&tos_fw_cert_uuid_spec,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100341 open_fip
342 },
Michalis Pappas3469cd02017-10-18 09:43:37 +0800343 [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
Jens Wiklander52c798e2015-12-07 14:37:10 +0100344 &fip_dev_handle,
Michalis Pappas3469cd02017-10-18 09:43:37 +0800345 (uintptr_t)&nt_fw_cert_uuid_spec,
Jens Wiklander52c798e2015-12-07 14:37:10 +0100346 open_fip
347 },
348#endif /* TRUSTED_BOARD_BOOT */
349};
350
Jens Wiklandera43c1282022-11-22 14:39:26 +0100351#if defined(SPD_spmd)
352static struct sp_pkg {
353 struct plat_io_policy policy;
354 io_file_spec_t sh_file_spec;
355 uint8_t uuid[UUID_BYTES_LENGTH];
356 char path[80];
357} sp_pkgs[MAX_SP_IDS];
358static unsigned int sp_pkg_count;
359
360int qemu_io_register_sp_pkg(const char *name, const char *uuid,
361 uintptr_t load_addr)
362{
363 struct sp_pkg *pkg;
364 bl_mem_params_node_t *mem_params;
365
366 if (sp_pkg_count == MAX_SP_IDS) {
367 INFO("Reached Max number of SPs\n");
368 return -1;
369 }
370 mem_params = get_bl_mem_params_node(SP_PKG1_ID + sp_pkg_count);
371 if (mem_params == NULL) {
372 ERROR("Can't find SP_PKG ID %u (SP_PKG%u_ID)\n",
373 SP_PKG1_ID + sp_pkg_count, sp_pkg_count);
374 return -1;
375 }
376 pkg = sp_pkgs + sp_pkg_count;
377
378 if (read_uuid(pkg->uuid, (char *)uuid)) {
379 return -1;
380 }
381
382 strlcpy(pkg->path, name, sizeof(pkg->path));
383 strlcat(pkg->path, ".pkg", sizeof(pkg->path));
384
385 pkg->policy.dev_handle = &fip_dev_handle;
386 pkg->policy.image_spec = (uintptr_t)&pkg->uuid;
387 pkg->policy.check = open_fip;
388 pkg->sh_file_spec.path = pkg->path;
389 pkg->sh_file_spec.mode = FOPEN_MODE_RB;
390
391 mem_params->image_info.image_base = load_addr;
392 mem_params->image_info.image_max_size = SZ_4M;
393 mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
394
395 sp_pkg_count++;
396
397 return 0;
398}
399#endif /*SPD_spmd*/
400
401static const io_file_spec_t *get_io_file_spec(unsigned int image_id)
402{
403#if defined(SPD_spmd)
404 if (image_id >= SP_PKG1_ID && image_id <= SP_PKG8_ID) {
405 return &sp_pkgs[image_id - SP_PKG1_ID].sh_file_spec;
406 }
407#endif
408
409 assert(image_id < ARRAY_SIZE(sh_file_spec));
410 return &sh_file_spec[image_id];
411}
412
413static const struct plat_io_policy *get_io_policy(unsigned int image_id)
414{
415#if defined(SPD_spmd)
416 if (image_id >= SP_PKG1_ID && image_id <= SP_PKG8_ID) {
417 return &sp_pkgs[image_id - SP_PKG1_ID].policy;
418 }
419#endif
420
421 assert(image_id < ARRAY_SIZE(policies));
422 return &policies[image_id];
423}
424
Jens Wiklander52c798e2015-12-07 14:37:10 +0100425static int open_fip(const uintptr_t spec)
426{
427 int result;
428 uintptr_t local_image_handle;
429
430 /* See if a Firmware Image Package is available */
431 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
Sumit Garg4ac30342019-11-14 17:34:56 +0530432 if (result == 0 && spec != (uintptr_t)NULL) {
Jens Wiklander52c798e2015-12-07 14:37:10 +0100433 result = io_open(fip_dev_handle, spec, &local_image_handle);
434 if (result == 0) {
435 VERBOSE("Using FIP\n");
436 io_close(local_image_handle);
437 }
438 }
439 return result;
440}
441
Sumit Garg4ac30342019-11-14 17:34:56 +0530442#ifndef DECRYPTION_SUPPORT_none
443static int open_enc_fip(const uintptr_t spec)
444{
445 int result;
446 uintptr_t local_image_handle;
447
448 /* See if an encrypted FIP is available */
449 result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID);
450 if (result == 0) {
451 result = io_open(enc_dev_handle, spec, &local_image_handle);
452 if (result == 0) {
453 VERBOSE("Using encrypted FIP\n");
454 io_close(local_image_handle);
455 }
456 }
457 return result;
458}
459#endif
460
Jens Wiklander52c798e2015-12-07 14:37:10 +0100461static int open_memmap(const uintptr_t spec)
462{
463 int result;
464 uintptr_t local_image_handle;
465
466 result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
467 if (result == 0) {
468 result = io_open(memmap_dev_handle, spec, &local_image_handle);
469 if (result == 0) {
470 VERBOSE("Using Memmap\n");
471 io_close(local_image_handle);
472 }
473 }
474 return result;
475}
476
477static int open_semihosting(const uintptr_t spec)
478{
479 int result;
480 uintptr_t local_image_handle;
481
482 /* See if the file exists on semi-hosting.*/
483 result = io_dev_init(sh_dev_handle, (uintptr_t)NULL);
484 if (result == 0) {
485 result = io_open(sh_dev_handle, spec, &local_image_handle);
486 if (result == 0) {
487 VERBOSE("Using Semi-hosting IO\n");
488 io_close(local_image_handle);
489 }
490 }
491 return result;
492}
493
494void plat_qemu_io_setup(void)
495{
496 int io_result;
497
498 io_result = register_io_dev_fip(&fip_dev_con);
499 assert(io_result == 0);
500
501 io_result = register_io_dev_memmap(&memmap_dev_con);
502 assert(io_result == 0);
503
504 /* Open connections to devices and cache the handles */
505 io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
506 &fip_dev_handle);
507 assert(io_result == 0);
508
509 io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
510 &memmap_dev_handle);
511 assert(io_result == 0);
512
Sumit Garg4ac30342019-11-14 17:34:56 +0530513#ifndef DECRYPTION_SUPPORT_none
514 io_result = register_io_dev_enc(&enc_dev_con);
515 assert(io_result == 0);
516
517 io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL,
518 &enc_dev_handle);
519 assert(io_result == 0);
520#endif
521
Jens Wiklander52c798e2015-12-07 14:37:10 +0100522 /* Register the additional IO devices on this platform */
523 io_result = register_io_dev_sh(&sh_dev_con);
524 assert(io_result == 0);
525
526 /* Open connections to devices and cache the handles */
527 io_result = io_dev_open(sh_dev_con, (uintptr_t)NULL, &sh_dev_handle);
528 assert(io_result == 0);
529
530 /* Ignore improbable errors in release builds */
531 (void)io_result;
532}
533
534static int get_alt_image_source(unsigned int image_id, uintptr_t *dev_handle,
535 uintptr_t *image_spec)
536{
Jens Wiklandera43c1282022-11-22 14:39:26 +0100537 const io_file_spec_t *spec = get_io_file_spec(image_id);
538 int result;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100539
Jens Wiklandera43c1282022-11-22 14:39:26 +0100540 result = open_semihosting((const uintptr_t)spec);
Jens Wiklander52c798e2015-12-07 14:37:10 +0100541 if (result == 0) {
542 *dev_handle = sh_dev_handle;
Jens Wiklandera43c1282022-11-22 14:39:26 +0100543 *image_spec = (uintptr_t)spec;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100544 }
545
546 return result;
547}
548
549/*
550 * Return an IO device handle and specification which can be used to access
551 * an image. Use this to enforce platform load policy
552 */
553int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
554 uintptr_t *image_spec)
555{
Jens Wiklandera43c1282022-11-22 14:39:26 +0100556 const struct plat_io_policy *policy = get_io_policy(image_id);
Jens Wiklander52c798e2015-12-07 14:37:10 +0100557 int result;
Jens Wiklander52c798e2015-12-07 14:37:10 +0100558
Jens Wiklander52c798e2015-12-07 14:37:10 +0100559 result = policy->check(policy->image_spec);
560 if (result == 0) {
561 *image_spec = policy->image_spec;
562 *dev_handle = *(policy->dev_handle);
563 } else {
564 VERBOSE("Trying alternative IO\n");
565 result = get_alt_image_source(image_id, dev_handle, image_spec);
566 }
567
568 return result;
569}