blob: 4848a706812cecf07773bf974d931d857ea0e207 [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
Harry Liebel561cd332014-02-14 14:42:48 +000031#include <assert.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010032#include <debug.h>
33#include <errno.h>
Dan Handley714a0d22014-04-09 13:13:04 +010034#include <firmware_image_package.h>
Dan Handley714a0d22014-04-09 13:13:04 +010035#include <io_driver.h>
36#include <io_fip.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010037#include <io_storage.h>
38#include <platform.h>
39#include <stdint.h>
40#include <string.h>
41#include <uuid.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;
Dan Handleye2712bc2014-04-10 15:37:22 +010054} plat_fip_name_uuid_t;
Harry Liebel561cd332014-02-14 14:42:48 +000055
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;
Dan Handleye2712bc2014-04-10 15:37:22 +010062 fip_toc_entry_t entry;
63} file_state_t;
Harry Liebel561cd332014-02-14 14:42:48 +000064
Dan Handleya4cb68e2014-04-23 13:47:06 +010065static const plat_fip_name_uuid_t name_uuid[] = {
Harry Liebel561cd332014-02-14 14:42:48 +000066 {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};
Dan Handleye2712bc2014-04-10 15:37:22 +010073static file_state_t current_file = {0};
Dan Handleya4cb68e2014-04-23 13:47:06 +010074static uintptr_t backend_dev_handle;
75static uintptr_t backend_image_spec;
Harry Liebel561cd332014-02-14 14:42:48 +000076
77
78/* Firmware Image Package driver functions */
Dan Handleya4cb68e2014-04-23 13:47:06 +010079static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
80static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
Dan Handleye2712bc2014-04-10 15:37:22 +010081 io_entity_t *entity);
82static int fip_file_len(io_entity_t *entity, size_t *length);
Dan Handleya4cb68e2014-04-23 13:47:06 +010083static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
Harry Liebel561cd332014-02-14 14:42:48 +000084 size_t *length_read);
Dan Handleye2712bc2014-04-10 15:37:22 +010085static int fip_file_close(io_entity_t *entity);
Dan Handleya4cb68e2014-04-23 13:47:06 +010086static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
Dan Handleye2712bc2014-04-10 15:37:22 +010087static int fip_dev_close(io_dev_info_t *dev_info);
Harry Liebel561cd332014-02-14 14:42:48 +000088
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? */
Dan Handleye2712bc2014-04-10 15:37:22 +0100105static inline int is_valid_header(fip_toc_header_t *header)
Harry Liebel561cd332014-02-14 14:42:48 +0000106{
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 */
Dan Handleye2712bc2014-04-10 15:37:22 +0100132io_type_t device_type_fip(void)
Harry Liebel561cd332014-02-14 14:42:48 +0000133{
134 return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
135}
136
137
Dan Handleya4cb68e2014-04-23 13:47:06 +0100138static const io_dev_connector_t fip_dev_connector = {
Harry Liebel561cd332014-02-14 14:42:48 +0000139 .dev_open = fip_dev_open
140};
141
142
Dan Handleya4cb68e2014-04-23 13:47:06 +0100143static const io_dev_funcs_t fip_dev_funcs = {
Harry Liebel561cd332014-02-14 14:42:48 +0000144 .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
Dan Handleya4cb68e2014-04-23 13:47:06 +0100156/* No state associated with this device so structure can be const */
157static const io_dev_info_t fip_dev_info = {
Harry Liebel561cd332014-02-14 14:42:48 +0000158 .funcs = &fip_dev_funcs,
159 .info = (uintptr_t)NULL
160};
161
162
163/* Open a connection to the FIP device */
Dan Handleya4cb68e2014-04-23 13:47:06 +0100164static int fip_dev_open(const uintptr_t dev_spec __attribute__((unused)),
Dan Handleye2712bc2014-04-10 15:37:22 +0100165 io_dev_info_t **dev_info)
Harry Liebel561cd332014-02-14 14:42:48 +0000166{
167 assert(dev_info != NULL);
Dan Handleya4cb68e2014-04-23 13:47:06 +0100168 *dev_info = (io_dev_info_t *)&fip_dev_info; /* cast away const */
Harry Liebel561cd332014-02-14 14:42:48 +0000169
170 return IO_SUCCESS;
171}
172
173
174/* Do some basic package checks. */
Dan Handleya4cb68e2014-04-23 13:47:06 +0100175static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
Harry Liebel561cd332014-02-14 14:42:48 +0000176{
177 int result = IO_FAIL;
178 char *image_name = (char *)init_params;
Dan Handleya4cb68e2014-04-23 13:47:06 +0100179 uintptr_t backend_handle;
Dan Handleye2712bc2014-04-10 15:37:22 +0100180 fip_toc_header_t header;
Harry Liebel561cd332014-02-14 14:42:48 +0000181 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) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000187 WARN("Failed to obtain reference to image '%s' (%i)\n",
Harry Liebel561cd332014-02-14 14:42:48 +0000188 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) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000197 WARN("Failed to access image '%s' (%i)\n", image_name, result);
Harry Liebel561cd332014-02-14 14:42:48 +0000198 result = IO_FAIL;
199 goto fip_dev_init_exit;
200 }
201
Dan Handleya4cb68e2014-04-23 13:47:06 +0100202 result = io_read(backend_handle, (uintptr_t)&header, sizeof(header),
203 &bytes_read);
Harry Liebel561cd332014-02-14 14:42:48 +0000204 if (result == IO_SUCCESS) {
205 if (!is_valid_header(&header)) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000206 WARN("Firmware Image Package header check failed.\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000207 result = IO_FAIL;
208 } else {
209 INFO("FIP header looks OK.\n");
210 }
211 }
212
213 io_close(backend_handle);
214
215 fip_dev_init_exit:
216 return result;
217}
218
219/* Close a connection to the FIP device */
Dan Handleye2712bc2014-04-10 15:37:22 +0100220static int fip_dev_close(io_dev_info_t *dev_info)
Harry Liebel561cd332014-02-14 14:42:48 +0000221{
222 /* TODO: Consider tracking open files and cleaning them up here */
223
224 /* Clear the backend. */
Dan Handleya4cb68e2014-04-23 13:47:06 +0100225 backend_dev_handle = (uintptr_t)NULL;
226 backend_image_spec = (uintptr_t)NULL;
Harry Liebel561cd332014-02-14 14:42:48 +0000227
228 return IO_SUCCESS;
229}
230
231
232/* Open a file for access from package. */
Dan Handleya4cb68e2014-04-23 13:47:06 +0100233static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
Dan Handleye2712bc2014-04-10 15:37:22 +0100234 io_entity_t *entity)
Harry Liebel561cd332014-02-14 14:42:48 +0000235{
236 int result = IO_FAIL;
Dan Handleya4cb68e2014-04-23 13:47:06 +0100237 uintptr_t backend_handle;
Harry Liebel561cd332014-02-14 14:42:48 +0000238 uuid_t file_uuid;
Dan Handleye2712bc2014-04-10 15:37:22 +0100239 const io_file_spec_t *file_spec = (io_file_spec_t *)spec;
Harry Liebel561cd332014-02-14 14:42:48 +0000240 size_t bytes_read;
241 int found_file = 0;
242
243 assert(file_spec != NULL);
244 assert(entity != NULL);
245
246 /* Can only have one file open at a time for the moment. We need to
247 * track state like file cursor position. We know the header lives at
248 * offset zero, so this entry should never be zero for an active file.
249 * When the system supports dynamic memory allocation we can allow more
250 * than one open file at a time if needed.
251 */
252 if (current_file.entry.offset_address != 0) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000253 WARN("fip_file_open : Only one open file at a time.\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000254 return IO_RESOURCES_EXHAUSTED;
255 }
256
257 /* Attempt to access the FIP image */
258 result = io_open(backend_dev_handle, backend_image_spec,
259 &backend_handle);
260 if (result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000261 WARN("Failed to open Firmware Image Package (%i)\n", result);
Harry Liebel561cd332014-02-14 14:42:48 +0000262 result = IO_FAIL;
263 goto fip_file_open_exit;
264 }
265
266 /* Seek past the FIP header into the Table of Contents */
Dan Handleye2712bc2014-04-10 15:37:22 +0100267 result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t));
Harry Liebel561cd332014-02-14 14:42:48 +0000268 if (result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000269 WARN("fip_file_open: failed to seek\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000270 result = IO_FAIL;
271 goto fip_file_open_close;
272 }
273
274 file_to_uuid(file_spec->path, &file_uuid);
275
276 found_file = 0;
277 do {
Dan Handleya4cb68e2014-04-23 13:47:06 +0100278 result = io_read(backend_handle,
279 (uintptr_t)&current_file.entry,
Harry Liebel561cd332014-02-14 14:42:48 +0000280 sizeof(current_file.entry),
281 &bytes_read);
282 if (result == IO_SUCCESS) {
283 if (compare_uuids(&current_file.entry.uuid,
284 &file_uuid) == 0) {
285 found_file = 1;
286 break;
287 }
288 } else {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000289 WARN("Failed to read FIP (%i)\n", result);
Harry Liebel561cd332014-02-14 14:42:48 +0000290 goto fip_file_open_close;
291 }
292 } while (compare_uuids(&current_file.entry.uuid, &uuid_null) != 0);
293
294 if (found_file == 1) {
295 /* All fine. Update entity info with file state and return. Set
296 * the file position to 0. The 'current_file.entry' holds the
297 * base and size of the file.
298 */
299 current_file.file_pos = 0;
300 entity->info = (uintptr_t)&current_file;
301 } else {
302 /* Did not find the file in the FIP. */
Jeenu Viswambharandd3dc322014-02-20 11:51:00 +0000303 current_file.entry.offset_address = 0;
Harry Liebel561cd332014-02-14 14:42:48 +0000304 result = IO_FAIL;
305 }
306
307 fip_file_open_close:
308 io_close(backend_handle);
309
310 fip_file_open_exit:
311 return result;
312}
313
314
315/* Return the size of a file in package */
Dan Handleye2712bc2014-04-10 15:37:22 +0100316static int fip_file_len(io_entity_t *entity, size_t *length)
Harry Liebel561cd332014-02-14 14:42:48 +0000317{
318 assert(entity != NULL);
319 assert(length != NULL);
320
Dan Handleye2712bc2014-04-10 15:37:22 +0100321 *length = ((file_state_t *)entity->info)->entry.size;
Harry Liebel561cd332014-02-14 14:42:48 +0000322
323 return IO_SUCCESS;
324}
325
326
327/* Read data from a file in package */
Dan Handleya4cb68e2014-04-23 13:47:06 +0100328static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
Harry Liebel561cd332014-02-14 14:42:48 +0000329 size_t *length_read)
330{
331 int result = IO_FAIL;
Dan Handleye2712bc2014-04-10 15:37:22 +0100332 file_state_t *fp;
Harry Liebel561cd332014-02-14 14:42:48 +0000333 size_t file_offset;
334 size_t bytes_read;
Dan Handleya4cb68e2014-04-23 13:47:06 +0100335 uintptr_t backend_handle;
Harry Liebel561cd332014-02-14 14:42:48 +0000336
337 assert(entity != NULL);
Dan Handleya4cb68e2014-04-23 13:47:06 +0100338 assert(buffer != (uintptr_t)NULL);
Harry Liebel561cd332014-02-14 14:42:48 +0000339 assert(length_read != NULL);
Dan Handleya4cb68e2014-04-23 13:47:06 +0100340 assert(entity->info != (uintptr_t)NULL);
Harry Liebel561cd332014-02-14 14:42:48 +0000341
342 /* Open the backend, attempt to access the blob image */
343 result = io_open(backend_dev_handle, backend_image_spec,
344 &backend_handle);
345 if (result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000346 WARN("Failed to open FIP (%i)\n", result);
Harry Liebel561cd332014-02-14 14:42:48 +0000347 result = IO_FAIL;
348 goto fip_file_read_exit;
349 }
350
Dan Handleye2712bc2014-04-10 15:37:22 +0100351 fp = (file_state_t *)entity->info;
Harry Liebel561cd332014-02-14 14:42:48 +0000352
353 /* Seek to the position in the FIP where the payload lives */
354 file_offset = fp->entry.offset_address + fp->file_pos;
355 result = io_seek(backend_handle, IO_SEEK_SET, file_offset);
356 if (result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000357 WARN("fip_file_read: failed to seek\n");
Harry Liebel561cd332014-02-14 14:42:48 +0000358 result = IO_FAIL;
359 goto fip_file_read_close;
360 }
361
362 result = io_read(backend_handle, buffer, length, &bytes_read);
363 if (result != IO_SUCCESS) {
364 /* We cannot read our data. Fail. */
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000365 WARN("Failed to read payload (%i)\n", result);
Harry Liebel561cd332014-02-14 14:42:48 +0000366 result = IO_FAIL;
367 goto fip_file_read_close;
368 } else {
369 /* Set caller length and new file position. */
370 *length_read = bytes_read;
371 fp->file_pos += bytes_read;
372 }
373
374/* Close the backend. */
375 fip_file_read_close:
376 io_close(backend_handle);
377
378 fip_file_read_exit:
379 return result;
380}
381
382
383/* Close a file in package */
Dan Handleye2712bc2014-04-10 15:37:22 +0100384static int fip_file_close(io_entity_t *entity)
Harry Liebel561cd332014-02-14 14:42:48 +0000385{
386 /* Clear our current file pointer.
387 * If we had malloc() we would free() here.
388 */
389 if (current_file.entry.offset_address != 0) {
390 memset(&current_file, 0, sizeof(current_file));
391 }
392
393 /* Clear the Entity info. */
394 entity->info = 0;
395
396 return IO_SUCCESS;
397}
398
399/* Exported functions */
400
401/* Register the Firmware Image Package driver with the IO abstraction */
Dan Handleya4cb68e2014-04-23 13:47:06 +0100402int register_io_dev_fip(const io_dev_connector_t **dev_con)
Harry Liebel561cd332014-02-14 14:42:48 +0000403{
404 int result = IO_FAIL;
405 assert(dev_con != NULL);
406
407 result = io_register_device(&fip_dev_info);
408 if (result == IO_SUCCESS)
409 *dev_con = &fip_dev_connector;
410
411 return result;
412}