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