1 /*
2 * Copyright (c) 2014-2015, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8 #include <common_def.h>
9 #include <debug.h>
10 #include <io_driver.h>
11 #include <io_semihosting.h>
12 #include <io_storage.h>
13 #include <plat_arm.h>
14 #include <semihosting.h> /* For FOPEN_MODE_... */
15
16 /* Semihosting filenames */
17 #define BL2_IMAGE_NAME "bl2.bin"
18 #define BL31_IMAGE_NAME "bl31.bin"
19 #define BL32_IMAGE_NAME "bl32.bin"
20 #define BL33_IMAGE_NAME "bl33.bin"
21
22 #if TRUSTED_BOARD_BOOT
23 #define TRUSTED_BOOT_FW_CERT_NAME "tb_fw.crt"
24 #define TRUSTED_KEY_CERT_NAME "trusted_key.crt"
25 #define SOC_FW_KEY_CERT_NAME "soc_fw_key.crt"
26 #define TOS_FW_KEY_CERT_NAME "tos_fw_key.crt"
27 #define NT_FW_KEY_CERT_NAME "nt_fw_key.crt"
28 #define SOC_FW_CONTENT_CERT_NAME "soc_fw_content.crt"
29 #define TOS_FW_CONTENT_CERT_NAME "tos_fw_content.crt"
30 #define NT_FW_CONTENT_CERT_NAME "nt_fw_content.crt"
31 #endif /* TRUSTED_BOARD_BOOT */
32
33 /* IO devices */
34 static const io_dev_connector_t *sh_dev_con;
35 static uintptr_t sh_dev_handle;
36
37 static const io_file_spec_t sh_file_spec[] = {
38 [BL2_IMAGE_ID] = {
39 .path = BL2_IMAGE_NAME,
40 .mode = FOPEN_MODE_RB
41 },
42 [BL31_IMAGE_ID] = {
43 .path = BL31_IMAGE_NAME,
44 .mode = FOPEN_MODE_RB
45 },
46 [BL32_IMAGE_ID] = {
47 .path = BL32_IMAGE_NAME,
48 .mode = FOPEN_MODE_RB
49 },
50 [BL33_IMAGE_ID] = {
51 .path = BL33_IMAGE_NAME,
52 .mode = FOPEN_MODE_RB
53 },
54 #if TRUSTED_BOARD_BOOT
55 [TRUSTED_BOOT_FW_CERT_ID] = {
56 .path = TRUSTED_BOOT_FW_CERT_NAME,
57 .mode = FOPEN_MODE_RB
58 },
59 [TRUSTED_KEY_CERT_ID] = {
60 .path = TRUSTED_KEY_CERT_NAME,
61 .mode = FOPEN_MODE_RB
62 },
63 [SOC_FW_KEY_CERT_ID] = {
64 .path = SOC_FW_KEY_CERT_NAME,
65 .mode = FOPEN_MODE_RB
66 },
67 [TRUSTED_OS_FW_KEY_CERT_ID] = {
68 .path = TOS_FW_KEY_CERT_NAME,
69 .mode = FOPEN_MODE_RB
70 },
71 [NON_TRUSTED_FW_KEY_CERT_ID] = {
72 .path = NT_FW_KEY_CERT_NAME,
73 .mode = FOPEN_MODE_RB
74 },
75 [SOC_FW_CONTENT_CERT_ID] = {
76 .path = SOC_FW_CONTENT_CERT_NAME,
77 .mode = FOPEN_MODE_RB
78 },
79 [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
80 .path = TOS_FW_CONTENT_CERT_NAME,
81 .mode = FOPEN_MODE_RB
82 },
83 [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
84 .path = NT_FW_CONTENT_CERT_NAME,
85 .mode = FOPEN_MODE_RB
86 },
87 #endif /* TRUSTED_BOARD_BOOT */
88 };
89
90
open_semihosting(const uintptr_t spec)91 static int open_semihosting(const uintptr_t spec)
92 {
93 int result;
94 uintptr_t local_image_handle;
95
96 /* See if the file exists on semi-hosting.*/
97 result = io_dev_init(sh_dev_handle, (uintptr_t)NULL);
98 if (result == 0) {
99 result = io_open(sh_dev_handle, spec, &local_image_handle);
100 if (result == 0) {
101 VERBOSE("Using Semi-hosting IO\n");
102 io_close(local_image_handle);
103 }
104 }
105 return result;
106 }
107
plat_arm_io_setup(void)108 void plat_arm_io_setup(void)
109 {
110 int io_result;
111
112 arm_io_setup();
113
114 /* Register the additional IO devices on this platform */
115 io_result = register_io_dev_sh(&sh_dev_con);
116 assert(io_result == 0);
117
118 /* Open connections to devices and cache the handles */
119 io_result = io_dev_open(sh_dev_con, (uintptr_t)NULL, &sh_dev_handle);
120 assert(io_result == 0);
121
122 /* Ignore improbable errors in release builds */
123 (void)io_result;
124 }
125
126 /*
127 * FVP provides semihosting as an alternative to load images
128 */
plat_arm_get_alt_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)129 int plat_arm_get_alt_image_source(unsigned int image_id, uintptr_t *dev_handle,
130 uintptr_t *image_spec)
131 {
132 int result = open_semihosting((const uintptr_t)&sh_file_spec[image_id]);
133 if (result == 0) {
134 *dev_handle = sh_dev_handle;
135 *image_spec = (uintptr_t)&sh_file_spec[image_id];
136 }
137
138 return result;
139 }
140