blob: b1e033ee46aba31fe51ce05fab18ae5188bad33b [file] [log] [blame]
James Morrissey9d72b4e2014-02-10 17:04:32 +00001/*
2 * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <assert.h>
Dan Handley97043ac2014-04-09 13:14:54 +010032#include <debug.h>
Dan Handley35e98e52014-04-09 13:13:04 +010033#include <io_driver.h>
Dan Handley35e98e52014-04-09 13:13:04 +010034#include <io_fip.h>
35#include <io_memmap.h>
Dan Handley97043ac2014-04-09 13:14:54 +010036#include <io_storage.h>
37#include <io_semihosting.h>
Dan Handley6d16ce02014-08-04 18:31:43 +010038#include <platform_def.h>
Dan Handley97043ac2014-04-09 13:14:54 +010039#include <semihosting.h> /* For FOPEN_MODE_... */
40#include <string.h>
James Morrissey9d72b4e2014-02-10 17:04:32 +000041
James Morrissey9d72b4e2014-02-10 17:04:32 +000042/* IO devices */
Dan Handley625de1d2014-04-23 13:47:06 +010043static const io_dev_connector_t *sh_dev_con;
44static uintptr_t sh_dev_spec;
45static uintptr_t sh_init_params;
46static uintptr_t sh_dev_handle;
47static const io_dev_connector_t *fip_dev_con;
48static uintptr_t fip_dev_spec;
49static uintptr_t fip_dev_handle;
50static const io_dev_connector_t *memmap_dev_con;
51static uintptr_t memmap_dev_spec;
52static uintptr_t memmap_init_params;
53static uintptr_t memmap_dev_handle;
James Morrissey9d72b4e2014-02-10 17:04:32 +000054
Dan Handley625de1d2014-04-23 13:47:06 +010055static const io_block_spec_t fip_block_spec = {
Harry Liebel561cd332014-02-14 14:42:48 +000056 .offset = FLASH0_BASE,
57 .length = FLASH0_SIZE
58};
59
Dan Handley625de1d2014-04-23 13:47:06 +010060static const io_file_spec_t bl2_file_spec = {
James Morrissey9d72b4e2014-02-10 17:04:32 +000061 .path = BL2_IMAGE_NAME,
Sandrine Bailleux886278e2014-03-20 15:51:02 +000062 .mode = FOPEN_MODE_RB
James Morrissey9d72b4e2014-02-10 17:04:32 +000063};
64
Dan Handley625de1d2014-04-23 13:47:06 +010065static const io_file_spec_t bl31_file_spec = {
James Morrissey9d72b4e2014-02-10 17:04:32 +000066 .path = BL31_IMAGE_NAME,
Sandrine Bailleux886278e2014-03-20 15:51:02 +000067 .mode = FOPEN_MODE_RB
James Morrissey9d72b4e2014-02-10 17:04:32 +000068};
69
Dan Handley625de1d2014-04-23 13:47:06 +010070static const io_file_spec_t bl32_file_spec = {
Achin Gupta375f5382014-02-18 18:12:48 +000071 .path = BL32_IMAGE_NAME,
Sandrine Bailleux886278e2014-03-20 15:51:02 +000072 .mode = FOPEN_MODE_RB
Achin Gupta375f5382014-02-18 18:12:48 +000073};
74
Dan Handley625de1d2014-04-23 13:47:06 +010075static const io_file_spec_t bl33_file_spec = {
Harry Liebel561cd332014-02-14 14:42:48 +000076 .path = BL33_IMAGE_NAME,
Sandrine Bailleux886278e2014-03-20 15:51:02 +000077 .mode = FOPEN_MODE_RB
Harry Liebel561cd332014-02-14 14:42:48 +000078};
James Morrissey9d72b4e2014-02-10 17:04:32 +000079
Juan Castillo01df3c12015-01-07 13:49:59 +000080#if TRUSTED_BOARD_BOOT
81static const io_file_spec_t bl2_cert_file_spec = {
82 .path = BL2_CERT_NAME,
83 .mode = FOPEN_MODE_RB
84};
85#endif /* TRUSTED_BOARD_BOOT */
86
Dan Handley625de1d2014-04-23 13:47:06 +010087static int open_fip(const uintptr_t spec);
88static int open_memmap(const uintptr_t spec);
Ryan Harkin48e2ca72014-02-18 17:40:24 +000089
90struct plat_io_policy {
91 char *image_name;
Dan Handley625de1d2014-04-23 13:47:06 +010092 uintptr_t *dev_handle;
93 uintptr_t image_spec;
94 int (*check)(const uintptr_t spec);
Ryan Harkin48e2ca72014-02-18 17:40:24 +000095};
96
Dan Handley625de1d2014-04-23 13:47:06 +010097static const struct plat_io_policy policies[] = {
98 {
99 FIP_IMAGE_NAME,
100 &memmap_dev_handle,
101 (uintptr_t)&fip_block_spec,
102 open_memmap
103 }, {
104 BL2_IMAGE_NAME,
105 &fip_dev_handle,
106 (uintptr_t)&bl2_file_spec,
107 open_fip
108 }, {
109 BL31_IMAGE_NAME,
110 &fip_dev_handle,
111 (uintptr_t)&bl31_file_spec,
112 open_fip
113 }, {
114 BL32_IMAGE_NAME,
115 &fip_dev_handle,
116 (uintptr_t)&bl32_file_spec,
117 open_fip
118 }, {
119 BL33_IMAGE_NAME,
120 &fip_dev_handle,
121 (uintptr_t)&bl33_file_spec,
122 open_fip
123 }, {
Juan Castillo01df3c12015-01-07 13:49:59 +0000124#if TRUSTED_BOARD_BOOT
125 BL2_CERT_NAME,
126 &fip_dev_handle,
127 (uintptr_t)&bl2_cert_file_spec,
128 open_fip
129 }, {
130#endif /* TRUSTED_BOARD_BOOT */
Dan Handley625de1d2014-04-23 13:47:06 +0100131 0, 0, 0
132 }
Harry Liebel561cd332014-02-14 14:42:48 +0000133};
134
135
Dan Handley625de1d2014-04-23 13:47:06 +0100136static int open_fip(const uintptr_t spec)
James Morrissey9d72b4e2014-02-10 17:04:32 +0000137{
Harry Liebel561cd332014-02-14 14:42:48 +0000138 int result = IO_FAIL;
139
140 /* See if a Firmware Image Package is available */
Dan Handley625de1d2014-04-23 13:47:06 +0100141 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_NAME);
Harry Liebel561cd332014-02-14 14:42:48 +0000142 if (result == IO_SUCCESS) {
Dan Handley6ad2e462014-07-29 17:14:00 +0100143 VERBOSE("Using FIP\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000144 /*TODO: Check image defined in spec is present in FIP. */
145 }
146 return result;
147}
148
149
Dan Handley625de1d2014-04-23 13:47:06 +0100150static int open_memmap(const uintptr_t spec)
Harry Liebel561cd332014-02-14 14:42:48 +0000151{
152 int result = IO_FAIL;
Dan Handley625de1d2014-04-23 13:47:06 +0100153 uintptr_t local_image_handle;
Harry Liebel561cd332014-02-14 14:42:48 +0000154
155 result = io_dev_init(memmap_dev_handle, memmap_init_params);
156 if (result == IO_SUCCESS) {
157 result = io_open(memmap_dev_handle, spec, &local_image_handle);
158 if (result == IO_SUCCESS) {
Dan Handley6ad2e462014-07-29 17:14:00 +0100159 VERBOSE("Using Memmap IO\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000160 io_close(local_image_handle);
161 }
162 }
163 return result;
164}
165
166
Dan Handley625de1d2014-04-23 13:47:06 +0100167static int open_semihosting(const uintptr_t spec)
Harry Liebel561cd332014-02-14 14:42:48 +0000168{
169 int result = IO_FAIL;
Dan Handley625de1d2014-04-23 13:47:06 +0100170 uintptr_t local_image_handle;
Harry Liebel561cd332014-02-14 14:42:48 +0000171
172 /* See if the file exists on semi-hosting.*/
173 result = io_dev_init(sh_dev_handle, sh_init_params);
174 if (result == IO_SUCCESS) {
175 result = io_open(sh_dev_handle, spec, &local_image_handle);
176 if (result == IO_SUCCESS) {
Dan Handley6ad2e462014-07-29 17:14:00 +0100177 VERBOSE("Using Semi-hosting IO\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000178 io_close(local_image_handle);
179 }
180 }
181 return result;
182}
183
Dan Handley17a387a2014-05-15 14:53:30 +0100184void fvp_io_setup (void)
Harry Liebel561cd332014-02-14 14:42:48 +0000185{
186 int io_result = IO_FAIL;
187
Harry Liebel561cd332014-02-14 14:42:48 +0000188 /* Register the IO devices on this platform */
189 io_result = register_io_dev_sh(&sh_dev_con);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000190 assert(io_result == IO_SUCCESS);
191
Harry Liebel561cd332014-02-14 14:42:48 +0000192 io_result = register_io_dev_fip(&fip_dev_con);
193 assert(io_result == IO_SUCCESS);
194
195 io_result = register_io_dev_memmap(&memmap_dev_con);
196 assert(io_result == IO_SUCCESS);
197
198 /* Open connections to devices and cache the handles */
James Morrissey9d72b4e2014-02-10 17:04:32 +0000199 io_result = io_dev_open(sh_dev_con, sh_dev_spec, &sh_dev_handle);
200 assert(io_result == IO_SUCCESS);
201
Harry Liebel561cd332014-02-14 14:42:48 +0000202 io_result = io_dev_open(fip_dev_con, fip_dev_spec, &fip_dev_handle);
203 assert(io_result == IO_SUCCESS);
204
205 io_result = io_dev_open(memmap_dev_con, memmap_dev_spec,
206 &memmap_dev_handle);
207 assert(io_result == IO_SUCCESS);
208
James Morrissey9d72b4e2014-02-10 17:04:32 +0000209 /* Ignore improbable errors in release builds */
210 (void)io_result;
211}
212
213
214/* Return an IO device handle and specification which can be used to access
Harry Liebel561cd332014-02-14 14:42:48 +0000215 * an image. Use this to enforce platform load policy */
Dan Handley625de1d2014-04-23 13:47:06 +0100216int plat_get_image_source(const char *image_name, uintptr_t *dev_handle,
217 uintptr_t *image_spec)
James Morrissey9d72b4e2014-02-10 17:04:32 +0000218{
219 int result = IO_FAIL;
Dan Handley625de1d2014-04-23 13:47:06 +0100220 const struct plat_io_policy *policy;
James Morrissey9d72b4e2014-02-10 17:04:32 +0000221
Ryan Harkin48e2ca72014-02-18 17:40:24 +0000222 if ((image_name != NULL) && (dev_handle != NULL) &&
223 (image_spec != NULL)) {
224 policy = policies;
225 while (policy->image_name != NULL) {
226 if (strcmp(policy->image_name, image_name) == 0) {
227 result = policy->check(policy->image_spec);
228 if (result == IO_SUCCESS) {
Dan Handley625de1d2014-04-23 13:47:06 +0100229 *image_spec = policy->image_spec;
Ryan Harkin48e2ca72014-02-18 17:40:24 +0000230 *dev_handle = *(policy->dev_handle);
231 break;
232 } else {
233 result = open_semihosting(
234 policy->image_spec);
235 if (result == IO_SUCCESS) {
236 *dev_handle = sh_dev_handle;
Dan Handley625de1d2014-04-23 13:47:06 +0100237 *image_spec =
Ryan Harkin48e2ca72014-02-18 17:40:24 +0000238 policy->image_spec;
239 }
240 }
241 }
242 policy++;
Harry Liebel561cd332014-02-14 14:42:48 +0000243 }
Ryan Harkin48e2ca72014-02-18 17:40:24 +0000244 } else {
245 result = IO_FAIL;
Harry Liebel561cd332014-02-14 14:42:48 +0000246 }
James Morrissey9d72b4e2014-02-10 17:04:32 +0000247 return result;
248}