blob: 6a7f12395a75a838db42b09335ba0eb6087c9f3f [file] [log] [blame]
Harry Liebel561cd332014-02-14 14:42:48 +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 <stdint.h>
32#include <uuid.h>
33#include <errno.h>
34#include <string.h>
35#include <assert.h>
36#include "platform.h"
37#include "firmware_image_package.h"
38#include "io_storage.h"
39#include "io_driver.h"
40#include "io_fip.h"
41#include "debug.h"
42
43/* Useful for printing UUIDs when debugging.*/
44#define PRINT_UUID2(x) \
45 "%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", \
46 x.time_low, x.time_mid, x.time_hi_and_version, \
47 x.clock_seq_hi_and_reserved, x.clock_seq_low, \
48 x.node[0], x.node[1], x.node[2], x.node[3], \
49 x.node[4], x.node[5]
50
51typedef struct {
52 const char *name;
53 const uuid_t uuid;
54} plat_fip_name_uuid;
55
56typedef struct {
57 /* Put file_pos above the struct to allow {0} on static init.
58 * It is a workaround for a known bug in GCC
59 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
60 */
61 unsigned int file_pos;
62 fip_toc_entry entry;
63} file_state;
64
65static plat_fip_name_uuid name_uuid[] = {
66 {BL2_IMAGE_NAME, UUID_TRUSTED_BOOT_FIRMWARE_BL2},
67 {BL31_IMAGE_NAME, UUID_EL3_RUNTIME_FIRMWARE_BL31},
68 {BL32_IMAGE_NAME, UUID_SECURE_PAYLOAD_BL32},
69 {BL33_IMAGE_NAME, UUID_NON_TRUSTED_FIRMWARE_BL33},
70 {NULL, {0} }
71};
72
73static const uuid_t uuid_null = {0};
74static file_state current_file = {0};
75static io_dev_handle backend_dev_handle;
76static void *backend_image_spec;
77
78
79/* Firmware Image Package driver functions */
80static int fip_dev_open(void *spec, struct io_dev_info **dev_info);
81static int fip_file_open(struct io_dev_info *dev_info, const void *spec,
82 struct io_entity *entity);
83static int fip_file_len(struct io_entity *entity, size_t *length);
84static int fip_file_read(struct io_entity *entity, void *buffer, size_t length,
85 size_t *length_read);
86static int fip_file_close(struct io_entity *entity);
87static int fip_dev_init(struct io_dev_info *dev_info, const void *init_params);
88static int fip_dev_close(struct io_dev_info *dev_info);
89
90
91static inline int copy_uuid(uuid_t *dst, const uuid_t *src)
92{
93 memcpy(dst, src, sizeof(uuid_t));
94 return 0;
95}
96
97
98/* Return 0 for equal uuids. */
99static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
100{
101 return memcmp(uuid1, uuid2, sizeof(uuid_t));
102}
103
104
105/* TODO: We could check version numbers or do a package checksum? */
106static inline int is_valid_header(fip_toc_header *header)
107{
108 if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
109 return 1;
110 } else {
111 return 0;
112 }
113}
114
115
116static int file_to_uuid(const char *filename, uuid_t *uuid)
117{
118 int i;
119 int status = -EINVAL;
120
121 for (i = 0; i < (sizeof(name_uuid)/sizeof(plat_fip_name_uuid)); i++) {
122 if (strcmp(filename, name_uuid[i].name) == 0) {
123 copy_uuid(uuid, &name_uuid[i].uuid);
124 status = 0;
125 break;
126 }
127 }
128 return status;
129}
130
131
132/* Identify the device type as a virtual driver */
133io_type device_type_fip(void)
134{
135 return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
136}
137
138
139static struct io_dev_connector fip_dev_connector = {
140 .dev_open = fip_dev_open
141};
142
143
144static struct io_dev_funcs fip_dev_funcs = {
145 .type = device_type_fip,
146 .open = fip_file_open,
147 .seek = NULL,
148 .size = fip_file_len,
149 .read = fip_file_read,
150 .write = NULL,
151 .close = fip_file_close,
152 .dev_init = fip_dev_init,
153 .dev_close = fip_dev_close,
154};
155
156
157static struct io_dev_info fip_dev_info = {
158 .funcs = &fip_dev_funcs,
159 .info = (uintptr_t)NULL
160};
161
162
163/* Open a connection to the FIP device */
164static int fip_dev_open(void *spec __attribute__((unused)),
165 struct io_dev_info **dev_info)
166{
167 assert(dev_info != NULL);
168 *dev_info = &fip_dev_info;
169
170 return IO_SUCCESS;
171}
172
173
174/* Do some basic package checks. */
175static int fip_dev_init(struct io_dev_info *dev_info, const void *init_params)
176{
177 int result = IO_FAIL;
178 char *image_name = (char *)init_params;
179 io_handle backend_handle;
180 fip_toc_header header;
181 size_t bytes_read;
182
183 /* Obtain a reference to the image by querying the platform layer */
184 result = plat_get_image_source(image_name, &backend_dev_handle,
185 &backend_image_spec);
186 if (result != IO_SUCCESS) {
187 ERROR("Failed to obtain reference to image '%s' (%i)\n",
188 image_name, result);
189 result = IO_FAIL;
190 goto fip_dev_init_exit;
191 }
192
193 /* Attempt to access the FIP image */
194 result = io_open(backend_dev_handle, backend_image_spec,
195 &backend_handle);
196 if (result != IO_SUCCESS) {
197 ERROR("Failed to access image '%s' (%i)\n", image_name, result);
198 result = IO_FAIL;
199 goto fip_dev_init_exit;
200 }
201
202 result = io_read(backend_handle, &header, sizeof(header), &bytes_read);
203 if (result == IO_SUCCESS) {
204 if (!is_valid_header(&header)) {
205 ERROR("Firmware Image Package header check failed.\n");
206 result = IO_FAIL;
207 } else {
208 INFO("FIP header looks OK.\n");
209 }
210 }
211
212 io_close(backend_handle);
213
214 fip_dev_init_exit:
215 return result;
216}
217
218/* Close a connection to the FIP device */
219static int fip_dev_close(struct io_dev_info *dev_info)
220{
221 /* TODO: Consider tracking open files and cleaning them up here */
222
223 /* Clear the backend. */
224 backend_dev_handle = NULL;
225 backend_image_spec = NULL;
226
227 return IO_SUCCESS;
228}
229
230
231/* Open a file for access from package. */
232static int fip_file_open(struct io_dev_info *dev_info, const void *spec,
233 struct io_entity *entity)
234{
235 int result = IO_FAIL;
236 io_handle backend_handle;
237 uuid_t file_uuid;
238 const io_file_spec *file_spec = (io_file_spec *)spec;
239 size_t bytes_read;
240 int found_file = 0;
241
242 assert(file_spec != NULL);
243 assert(entity != NULL);
244
245 /* Can only have one file open at a time for the moment. We need to
246 * track state like file cursor position. We know the header lives at
247 * offset zero, so this entry should never be zero for an active file.
248 * When the system supports dynamic memory allocation we can allow more
249 * than one open file at a time if needed.
250 */
251 if (current_file.entry.offset_address != 0) {
252 ERROR("fip_file_open : Only one open file at a time.\n");
253 return IO_RESOURCES_EXHAUSTED;
254 }
255
256 /* Attempt to access the FIP image */
257 result = io_open(backend_dev_handle, backend_image_spec,
258 &backend_handle);
259 if (result != IO_SUCCESS) {
260 ERROR("Failed to open Firmware Image Package (%i)\n", result);
261 result = IO_FAIL;
262 goto fip_file_open_exit;
263 }
264
265 /* Seek past the FIP header into the Table of Contents */
266 result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header));
267 if (result != IO_SUCCESS) {
268 ERROR("fip_file_open: failed to seek\n");
269 result = IO_FAIL;
270 goto fip_file_open_close;
271 }
272
273 file_to_uuid(file_spec->path, &file_uuid);
274
275 found_file = 0;
276 do {
277 result = io_read(backend_handle, &current_file.entry,
278 sizeof(current_file.entry),
279 &bytes_read);
280 if (result == IO_SUCCESS) {
281 if (compare_uuids(&current_file.entry.uuid,
282 &file_uuid) == 0) {
283 found_file = 1;
284 break;
285 }
286 } else {
287 ERROR("Failed to read FIP (%i)\n", result);
288 goto fip_file_open_close;
289 }
290 } while (compare_uuids(&current_file.entry.uuid, &uuid_null) != 0);
291
292 if (found_file == 1) {
293 /* All fine. Update entity info with file state and return. Set
294 * the file position to 0. The 'current_file.entry' holds the
295 * base and size of the file.
296 */
297 current_file.file_pos = 0;
298 entity->info = (uintptr_t)&current_file;
299 } else {
300 /* Did not find the file in the FIP. */
301 result = IO_FAIL;
302 }
303
304 fip_file_open_close:
305 io_close(backend_handle);
306
307 fip_file_open_exit:
308 return result;
309}
310
311
312/* Return the size of a file in package */
313static int fip_file_len(struct io_entity *entity, size_t *length)
314{
315 assert(entity != NULL);
316 assert(length != NULL);
317
318 *length = ((file_state *)entity->info)->entry.size;
319
320 return IO_SUCCESS;
321}
322
323
324/* Read data from a file in package */
325static int fip_file_read(struct io_entity *entity, void *buffer, size_t length,
326 size_t *length_read)
327{
328 int result = IO_FAIL;
329 file_state *fp;
330 size_t file_offset;
331 size_t bytes_read;
332 io_handle backend_handle;
333
334 assert(entity != NULL);
335 assert(buffer != NULL);
336 assert(length_read != NULL);
337 assert((void *)entity->info != NULL);
338
339 /* Open the backend, attempt to access the blob image */
340 result = io_open(backend_dev_handle, backend_image_spec,
341 &backend_handle);
342 if (result != IO_SUCCESS) {
343 ERROR("Failed to open FIP (%i)\n", result);
344 result = IO_FAIL;
345 goto fip_file_read_exit;
346 }
347
348 fp = (file_state *)entity->info;
349
350 /* Seek to the position in the FIP where the payload lives */
351 file_offset = fp->entry.offset_address + fp->file_pos;
352 result = io_seek(backend_handle, IO_SEEK_SET, file_offset);
353 if (result != IO_SUCCESS) {
354 ERROR("fip_file_read: failed to seek\n");
355 result = IO_FAIL;
356 goto fip_file_read_close;
357 }
358
359 result = io_read(backend_handle, buffer, length, &bytes_read);
360 if (result != IO_SUCCESS) {
361 /* We cannot read our data. Fail. */
362 ERROR("Failed to read payload (%i)\n", result);
363 result = IO_FAIL;
364 goto fip_file_read_close;
365 } else {
366 /* Set caller length and new file position. */
367 *length_read = bytes_read;
368 fp->file_pos += bytes_read;
369 }
370
371/* Close the backend. */
372 fip_file_read_close:
373 io_close(backend_handle);
374
375 fip_file_read_exit:
376 return result;
377}
378
379
380/* Close a file in package */
381static int fip_file_close(struct io_entity *entity)
382{
383 /* Clear our current file pointer.
384 * If we had malloc() we would free() here.
385 */
386 if (current_file.entry.offset_address != 0) {
387 memset(&current_file, 0, sizeof(current_file));
388 }
389
390 /* Clear the Entity info. */
391 entity->info = 0;
392
393 return IO_SUCCESS;
394}
395
396/* Exported functions */
397
398/* Register the Firmware Image Package driver with the IO abstraction */
399int register_io_dev_fip(struct io_dev_connector **dev_con)
400{
401 int result = IO_FAIL;
402 assert(dev_con != NULL);
403
404 result = io_register_device(&fip_dev_info);
405 if (result == IO_SUCCESS)
406 *dev_con = &fip_dev_connector;
407
408 return result;
409}