blob: dd9f048af6c8b38188da3dae71cc28f373b15caa [file] [log] [blame]
Sandrine Bailleux798140d2014-07-17 16:06:39 +01001/*
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>
32#include <debug.h>
33#include <io_driver.h>
34#include <io_fip.h>
35#include <io_memmap.h>
36#include <io_storage.h>
37#include <platform_def.h>
38#include <semihosting.h> /* For FOPEN_MODE_... */
39#include <string.h>
40
41/* IO devices */
42static const io_dev_connector_t *fip_dev_con;
43static uintptr_t fip_dev_spec;
44static uintptr_t fip_dev_handle;
45static const io_dev_connector_t *memmap_dev_con;
46static uintptr_t memmap_dev_spec;
47static uintptr_t memmap_init_params;
48static uintptr_t memmap_dev_handle;
49
50static const io_block_spec_t fip_block_spec = {
51 .offset = FLASH_BASE,
52 .length = FLASH_SIZE
53};
54
55static const io_file_spec_t bl2_file_spec = {
56 .path = BL2_IMAGE_NAME,
57 .mode = FOPEN_MODE_RB
58};
59
60static const io_file_spec_t bl30_file_spec = {
61 .path = BL30_IMAGE_NAME,
62 .mode = FOPEN_MODE_RB
63};
64
65static const io_file_spec_t bl31_file_spec = {
66 .path = BL31_IMAGE_NAME,
67 .mode = FOPEN_MODE_RB
68};
69
Sandrine Bailleux1fe43362014-07-17 09:56:29 +010070static const io_file_spec_t bl32_file_spec = {
71 .path = BL32_IMAGE_NAME,
72 .mode = FOPEN_MODE_RB
73};
74
Sandrine Bailleux798140d2014-07-17 16:06:39 +010075static const io_file_spec_t bl33_file_spec = {
76 .path = BL33_IMAGE_NAME,
77 .mode = FOPEN_MODE_RB
78};
79
Juan Castillod227d8b2015-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
Sandrine Bailleux798140d2014-07-17 16:06:39 +010087static int open_fip(const uintptr_t spec);
88static int open_memmap(const uintptr_t spec);
89
90struct plat_io_policy {
91 const char *image_name;
92 uintptr_t *dev_handle;
93 uintptr_t image_spec;
94 int (*check)(const uintptr_t spec);
95};
96
97static 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 BL30_IMAGE_NAME,
110 &fip_dev_handle,
111 (uintptr_t)&bl30_file_spec,
112 open_fip
113 }, {
114 BL31_IMAGE_NAME,
115 &fip_dev_handle,
116 (uintptr_t)&bl31_file_spec,
117 open_fip
118 }, {
Sandrine Bailleux1fe43362014-07-17 09:56:29 +0100119 BL32_IMAGE_NAME,
120 &fip_dev_handle,
121 (uintptr_t)&bl32_file_spec,
122 open_fip
123 }, {
Sandrine Bailleux798140d2014-07-17 16:06:39 +0100124 BL33_IMAGE_NAME,
125 &fip_dev_handle,
126 (uintptr_t)&bl33_file_spec,
127 open_fip
128 }, {
Juan Castillod227d8b2015-01-07 13:49:59 +0000129#if TRUSTED_BOARD_BOOT
130 BL2_CERT_NAME,
131 &fip_dev_handle,
132 (uintptr_t)&bl2_cert_file_spec,
133 open_fip
134 }, {
135#endif /* TRUSTED_BOARD_BOOT */
Sandrine Bailleux798140d2014-07-17 16:06:39 +0100136 0, 0, 0
137 }
138};
139
140
141static int open_fip(const uintptr_t spec)
142{
143 int result = IO_FAIL;
144
145 /* See if a Firmware Image Package is available */
146 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_NAME);
147 if (result == IO_SUCCESS) {
148 INFO("Using FIP\n");
149 /*TODO: Check image defined in spec is present in FIP. */
150 }
151 return result;
152}
153
154
155static int open_memmap(const uintptr_t spec)
156{
157 int result = IO_FAIL;
158 uintptr_t local_image_handle;
159
160 result = io_dev_init(memmap_dev_handle, memmap_init_params);
161 if (result == IO_SUCCESS) {
162 result = io_open(memmap_dev_handle, spec, &local_image_handle);
163 if (result == IO_SUCCESS) {
164 /* INFO("Using Memmap IO\n"); */
165 io_close(local_image_handle);
166 }
167 }
168 return result;
169}
170
171void io_setup(void)
172{
173 int io_result = IO_FAIL;
174
175 /* Register the IO devices on this platform */
176 io_result = register_io_dev_fip(&fip_dev_con);
177 assert(io_result == IO_SUCCESS);
178
179 io_result = register_io_dev_memmap(&memmap_dev_con);
180 assert(io_result == IO_SUCCESS);
181
182 /* Open connections to devices and cache the handles */
183 io_result = io_dev_open(fip_dev_con, fip_dev_spec, &fip_dev_handle);
184 assert(io_result == IO_SUCCESS);
185
186 io_result = io_dev_open(memmap_dev_con, memmap_dev_spec,
187 &memmap_dev_handle);
188 assert(io_result == IO_SUCCESS);
189
190 /* Ignore improbable errors in release builds */
191 (void)io_result;
192}
193
194
195/* Return an IO device handle and specification which can be used to access
196 * an image. Use this to enforce platform load policy */
197int plat_get_image_source(const char *image_name, uintptr_t *dev_handle,
198 uintptr_t *image_spec)
199{
200 int result = IO_FAIL;
201 const struct plat_io_policy *policy;
202
203 if ((image_name != NULL) && (dev_handle != NULL) &&
204 (image_spec != NULL)) {
205 policy = policies;
206 while (policy->image_name != NULL) {
207 if (strcmp(policy->image_name, image_name) == 0) {
208 result = policy->check(policy->image_spec);
209 if (result == IO_SUCCESS) {
210 *image_spec = policy->image_spec;
211 *dev_handle = *(policy->dev_handle);
212 break;
213 }
214 }
215 policy++;
216 }
217 } else {
218 result = IO_FAIL;
219 }
220 return result;
221}