1 /*
2  * Copyright (c) 2021, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <common/debug.h>
10 #include <drivers/io/io_driver.h>
11 #include <drivers/io/io_semihosting.h>
12 #include <drivers/io/io_storage.h>
13 #include <lib/semihosting.h>
14 #include <plat/arm/common/plat_arm.h>
15 #include <plat/common/common_def.h>
16 
17 /* Semihosting filenames */
18 #define BL33_IMAGE_NAME			"bl33.bin"
19 
20 #if TRUSTED_BOARD_BOOT
21 #define TRUSTED_KEY_CERT_NAME		"trusted_key.crt"
22 #define NT_FW_KEY_CERT_NAME		"nt_fw_key.crt"
23 #define NT_FW_CONTENT_CERT_NAME		"nt_fw_content.crt"
24 #endif /* TRUSTED_BOARD_BOOT */
25 
26 /* IO devices */
27 static const io_dev_connector_t *sh_dev_con;
28 static uintptr_t sh_dev_handle;
29 
30 static const io_file_spec_t sh_file_spec[] = {
31 	[BL33_IMAGE_ID] = {
32 		.path = BL33_IMAGE_NAME,
33 		.mode = FOPEN_MODE_RB
34 	},
35 #if TRUSTED_BOARD_BOOT
36 	[TRUSTED_KEY_CERT_ID] = {
37 		.path = TRUSTED_KEY_CERT_NAME,
38 		.mode = FOPEN_MODE_RB
39 	},
40 	[NON_TRUSTED_FW_KEY_CERT_ID] = {
41 		.path = NT_FW_KEY_CERT_NAME,
42 		.mode = FOPEN_MODE_RB
43 	},
44 	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
45 		.path = NT_FW_CONTENT_CERT_NAME,
46 		.mode = FOPEN_MODE_RB
47 	},
48 #endif /* TRUSTED_BOARD_BOOT */
49 };
50 
51 
open_semihosting(const uintptr_t spec)52 static int open_semihosting(const uintptr_t spec)
53 {
54 	int result;
55 	uintptr_t local_image_handle;
56 
57 	/* See if the file exists on semi-hosting.*/
58 	result = io_dev_init(sh_dev_handle, (uintptr_t)NULL);
59 	if (result == 0) {
60 		result = io_open(sh_dev_handle, spec, &local_image_handle);
61 		if (result == 0) {
62 			VERBOSE("Using Semi-hosting IO\n");
63 			io_close(local_image_handle);
64 		}
65 	}
66 	return result;
67 }
68 
plat_arm_io_setup(void)69 void plat_arm_io_setup(void)
70 {
71 	int io_result;
72 
73 	io_result = arm_io_setup();
74 	if (io_result < 0) {
75 		panic();
76 	}
77 
78 	/* Register the additional IO devices on this platform */
79 	io_result = register_io_dev_sh(&sh_dev_con);
80 	if (io_result < 0) {
81 		panic();
82 	}
83 
84 	/* Open connections to devices and cache the handles */
85 	io_result = io_dev_open(sh_dev_con, (uintptr_t)NULL, &sh_dev_handle);
86 	if (io_result < 0) {
87 		panic();
88 	}
89 }
90 
91 /*
92  * FVP_R provides semihosting as an alternative to load images
93  */
plat_arm_get_alt_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)94 int plat_arm_get_alt_image_source(unsigned int image_id, uintptr_t *dev_handle,
95 				  uintptr_t *image_spec)
96 {
97 	int result = open_semihosting((const uintptr_t)&sh_file_spec[image_id]);
98 
99 	if (result == 0) {
100 		*dev_handle = sh_dev_handle;
101 		*image_spec = (uintptr_t)&sh_file_spec[image_id];
102 	}
103 
104 	return result;
105 }
106