blob: 3a5d2204e9858013df70cea287a67db122669bb5 [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>
Dan Handley714a0d22014-04-09 13:13:04 +010036#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>
Harry Liebel561cd332014-02-14 14:42:48 +000042
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},
Harry Liebel561cd332014-02-14 14:42:48 +000070};
71
72static const uuid_t uuid_null = {0};
73static file_state current_file = {0};
74static io_dev_handle backend_dev_handle;
75static void *backend_image_spec;
76
77
78/* Firmware Image Package driver functions */
79static int fip_dev_open(void *spec, struct io_dev_info **dev_info);
80static int fip_file_open(struct io_dev_info *dev_info, const void *spec,
81 struct io_entity *entity);
82static int fip_file_len(struct io_entity *entity, size_t *length);
83static int fip_file_read(struct io_entity *entity, void *buffer, size_t length,
84 size_t *length_read);
85static int fip_file_close(struct io_entity *entity);
86static int fip_dev_init(struct io_dev_info *dev_info, const void *init_params);
87static int fip_dev_close(struct io_dev_info *dev_info);
88
89
90static inline int copy_uuid(uuid_t *dst, const uuid_t *src)
91{
92 memcpy(dst, src, sizeof(uuid_t));
93 return 0;
94}
95
96
97/* Return 0 for equal uuids. */
98static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
99{
100 return memcmp(uuid1, uuid2, sizeof(uuid_t));
101}
102
103
104/* TODO: We could check version numbers or do a package checksum? */
105static inline int is_valid_header(fip_toc_header *header)
106{
107 if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
108 return 1;
109 } else {
110 return 0;
111 }
112}
113
114
115static int file_to_uuid(const char *filename, uuid_t *uuid)
116{
117 int i;
118 int status = -EINVAL;
119
Sandrine Bailleux0c51e072014-03-19 16:03:48 +0000120 for (i = 0; i < (sizeof(name_uuid) / sizeof(name_uuid[0])); i++) {
Harry Liebel561cd332014-02-14 14:42:48 +0000121 if (strcmp(filename, name_uuid[i].name) == 0) {
122 copy_uuid(uuid, &name_uuid[i].uuid);
123 status = 0;
124 break;
125 }
126 }
127 return status;
128}
129
130
131/* Identify the device type as a virtual driver */
132io_type device_type_fip(void)
133{
134 return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
135}
136
137
138static struct io_dev_connector fip_dev_connector = {
139 .dev_open = fip_dev_open
140};
141
142
143static struct io_dev_funcs fip_dev_funcs = {
144 .type = device_type_fip,
145 .open = fip_file_open,
146 .seek = NULL,
147 .size = fip_file_len,
148 .read = fip_file_read,
149 .write = NULL,
150 .close = fip_file_close,
151 .dev_init = fip_dev_init,
152 .dev_close = fip_dev_close,
153};
154
155
156static struct io_dev_info fip_dev_info = {
157 .funcs = &fip_dev_funcs,
158 .info = (uintptr_t)NULL
159};
160
161
162/* Open a connection to the FIP device */
163static int fip_dev_open(void *spec __attribute__((unused)),
164 struct io_dev_info **dev_info)
165{
166 assert(dev_info != NULL);
167 *dev_info = &fip_dev_info;
168
169 return IO_SUCCESS;
170}
171
172
173/* Do some basic package checks. */
174static int fip_dev_init(struct io_dev_info *dev_info, const void *init_params)
175{
176 int result = IO_FAIL;
177 char *image_name = (char *)init_params;
178 io_handle backend_handle;
179 fip_toc_header header;
180 size_t bytes_read;
181
182 /* Obtain a reference to the image by querying the platform layer */
183 result = plat_get_image_source(image_name, &backend_dev_handle,
184 &backend_image_spec);
185 if (result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000186 WARN("Failed to obtain reference to image '%s' (%i)\n",
Harry Liebel561cd332014-02-14 14:42:48 +0000187 image_name, result);
188 result = IO_FAIL;
189 goto fip_dev_init_exit;
190 }
191
192 /* Attempt to access the FIP image */
193 result = io_open(backend_dev_handle, backend_image_spec,
194 &backend_handle);
195 if (result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000196 WARN("Failed to access image '%s' (%i)\n", image_name, result);
Harry Liebel561cd332014-02-14 14:42:48 +0000197 result = IO_FAIL;
198 goto fip_dev_init_exit;
199 }
200
201 result = io_read(backend_handle, &header, sizeof(header), &bytes_read);
202 if (result == IO_SUCCESS) {
203 if (!is_valid_header(&header)) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000204 WARN("Firmware Image Package header check failed.\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000205 result = IO_FAIL;
206 } else {
207 INFO("FIP header looks OK.\n");
208 }
209 }
210
211 io_close(backend_handle);
212
213 fip_dev_init_exit:
214 return result;
215}
216
217/* Close a connection to the FIP device */
218static int fip_dev_close(struct io_dev_info *dev_info)
219{
220 /* TODO: Consider tracking open files and cleaning them up here */
221
222 /* Clear the backend. */
223 backend_dev_handle = NULL;
224 backend_image_spec = NULL;
225
226 return IO_SUCCESS;
227}
228
229
230/* Open a file for access from package. */
231static int fip_file_open(struct io_dev_info *dev_info, const void *spec,
232 struct io_entity *entity)
233{
234 int result = IO_FAIL;
235 io_handle backend_handle;
236 uuid_t file_uuid;
237 const io_file_spec *file_spec = (io_file_spec *)spec;
238 size_t bytes_read;
239 int found_file = 0;
240
241 assert(file_spec != NULL);
242 assert(entity != NULL);
243
244 /* Can only have one file open at a time for the moment. We need to
245 * track state like file cursor position. We know the header lives at
246 * offset zero, so this entry should never be zero for an active file.
247 * When the system supports dynamic memory allocation we can allow more
248 * than one open file at a time if needed.
249 */
250 if (current_file.entry.offset_address != 0) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000251 WARN("fip_file_open : Only one open file at a time.\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000252 return IO_RESOURCES_EXHAUSTED;
253 }
254
255 /* Attempt to access the FIP image */
256 result = io_open(backend_dev_handle, backend_image_spec,
257 &backend_handle);
258 if (result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000259 WARN("Failed to open Firmware Image Package (%i)\n", result);
Harry Liebel561cd332014-02-14 14:42:48 +0000260 result = IO_FAIL;
261 goto fip_file_open_exit;
262 }
263
264 /* Seek past the FIP header into the Table of Contents */
265 result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header));
266 if (result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000267 WARN("fip_file_open: failed to seek\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000268 result = IO_FAIL;
269 goto fip_file_open_close;
270 }
271
272 file_to_uuid(file_spec->path, &file_uuid);
273
274 found_file = 0;
275 do {
276 result = io_read(backend_handle, &current_file.entry,
277 sizeof(current_file.entry),
278 &bytes_read);
279 if (result == IO_SUCCESS) {
280 if (compare_uuids(&current_file.entry.uuid,
281 &file_uuid) == 0) {
282 found_file = 1;
283 break;
284 }
285 } else {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000286 WARN("Failed to read FIP (%i)\n", result);
Harry Liebel561cd332014-02-14 14:42:48 +0000287 goto fip_file_open_close;
288 }
289 } while (compare_uuids(&current_file.entry.uuid, &uuid_null) != 0);
290
291 if (found_file == 1) {
292 /* All fine. Update entity info with file state and return. Set
293 * the file position to 0. The 'current_file.entry' holds the
294 * base and size of the file.
295 */
296 current_file.file_pos = 0;
297 entity->info = (uintptr_t)&current_file;
298 } else {
299 /* Did not find the file in the FIP. */
Jeenu Viswambharandd3dc322014-02-20 11:51:00 +0000300 current_file.entry.offset_address = 0;
Harry Liebel561cd332014-02-14 14:42:48 +0000301 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) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000343 WARN("Failed to open FIP (%i)\n", result);
Harry Liebel561cd332014-02-14 14:42:48 +0000344 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) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000354 WARN("fip_file_read: failed to seek\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000355 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. */
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000362 WARN("Failed to read payload (%i)\n", result);
Harry Liebel561cd332014-02-14 14:42:48 +0000363 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}