blob: 83d7e43bab96275c7a0e8581ef319950db0fbed0 [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
80static int open_fip(const uintptr_t spec);
81static int open_memmap(const uintptr_t spec);
82
83struct plat_io_policy {
84 const char *image_name;
85 uintptr_t *dev_handle;
86 uintptr_t image_spec;
87 int (*check)(const uintptr_t spec);
88};
89
90static const struct plat_io_policy policies[] = {
91 {
92 FIP_IMAGE_NAME,
93 &memmap_dev_handle,
94 (uintptr_t)&fip_block_spec,
95 open_memmap
96 }, {
97 BL2_IMAGE_NAME,
98 &fip_dev_handle,
99 (uintptr_t)&bl2_file_spec,
100 open_fip
101 }, {
102 BL30_IMAGE_NAME,
103 &fip_dev_handle,
104 (uintptr_t)&bl30_file_spec,
105 open_fip
106 }, {
107 BL31_IMAGE_NAME,
108 &fip_dev_handle,
109 (uintptr_t)&bl31_file_spec,
110 open_fip
111 }, {
Sandrine Bailleux1fe43362014-07-17 09:56:29 +0100112 BL32_IMAGE_NAME,
113 &fip_dev_handle,
114 (uintptr_t)&bl32_file_spec,
115 open_fip
116 }, {
Sandrine Bailleux798140d2014-07-17 16:06:39 +0100117 BL33_IMAGE_NAME,
118 &fip_dev_handle,
119 (uintptr_t)&bl33_file_spec,
120 open_fip
121 }, {
122 0, 0, 0
123 }
124};
125
126
127static int open_fip(const uintptr_t spec)
128{
129 int result = IO_FAIL;
130
131 /* See if a Firmware Image Package is available */
132 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_NAME);
133 if (result == IO_SUCCESS) {
134 INFO("Using FIP\n");
135 /*TODO: Check image defined in spec is present in FIP. */
136 }
137 return result;
138}
139
140
141static int open_memmap(const uintptr_t spec)
142{
143 int result = IO_FAIL;
144 uintptr_t local_image_handle;
145
146 result = io_dev_init(memmap_dev_handle, memmap_init_params);
147 if (result == IO_SUCCESS) {
148 result = io_open(memmap_dev_handle, spec, &local_image_handle);
149 if (result == IO_SUCCESS) {
150 /* INFO("Using Memmap IO\n"); */
151 io_close(local_image_handle);
152 }
153 }
154 return result;
155}
156
157void io_setup(void)
158{
159 int io_result = IO_FAIL;
160
161 /* Register the IO devices on this platform */
162 io_result = register_io_dev_fip(&fip_dev_con);
163 assert(io_result == IO_SUCCESS);
164
165 io_result = register_io_dev_memmap(&memmap_dev_con);
166 assert(io_result == IO_SUCCESS);
167
168 /* Open connections to devices and cache the handles */
169 io_result = io_dev_open(fip_dev_con, fip_dev_spec, &fip_dev_handle);
170 assert(io_result == IO_SUCCESS);
171
172 io_result = io_dev_open(memmap_dev_con, memmap_dev_spec,
173 &memmap_dev_handle);
174 assert(io_result == IO_SUCCESS);
175
176 /* Ignore improbable errors in release builds */
177 (void)io_result;
178}
179
180
181/* Return an IO device handle and specification which can be used to access
182 * an image. Use this to enforce platform load policy */
183int plat_get_image_source(const char *image_name, uintptr_t *dev_handle,
184 uintptr_t *image_spec)
185{
186 int result = IO_FAIL;
187 const struct plat_io_policy *policy;
188
189 if ((image_name != NULL) && (dev_handle != NULL) &&
190 (image_spec != NULL)) {
191 policy = policies;
192 while (policy->image_name != NULL) {
193 if (strcmp(policy->image_name, image_name) == 0) {
194 result = policy->check(policy->image_spec);
195 if (result == IO_SUCCESS) {
196 *image_spec = policy->image_spec;
197 *dev_handle = *(policy->dev_handle);
198 break;
199 }
200 }
201 policy++;
202 }
203 } else {
204 result = IO_FAIL;
205 }
206 return result;
207}