1561cd33eSHarry Liebel /* 2*32f0d3c6SDouglas Raillard * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved. 3561cd33eSHarry Liebel * 4561cd33eSHarry Liebel * Redistribution and use in source and binary forms, with or without 5561cd33eSHarry Liebel * modification, are permitted provided that the following conditions are met: 6561cd33eSHarry Liebel * 7561cd33eSHarry Liebel * Redistributions of source code must retain the above copyright notice, this 8561cd33eSHarry Liebel * list of conditions and the following disclaimer. 9561cd33eSHarry Liebel * 10561cd33eSHarry Liebel * Redistributions in binary form must reproduce the above copyright notice, 11561cd33eSHarry Liebel * this list of conditions and the following disclaimer in the documentation 12561cd33eSHarry Liebel * and/or other materials provided with the distribution. 13561cd33eSHarry Liebel * 14561cd33eSHarry Liebel * Neither the name of ARM nor the names of its contributors may be used 15561cd33eSHarry Liebel * to endorse or promote products derived from this software without specific 16561cd33eSHarry Liebel * prior written permission. 17561cd33eSHarry Liebel * 18561cd33eSHarry Liebel * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19561cd33eSHarry Liebel * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20561cd33eSHarry Liebel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21561cd33eSHarry Liebel * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22561cd33eSHarry Liebel * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23561cd33eSHarry Liebel * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24561cd33eSHarry Liebel * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25561cd33eSHarry Liebel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26561cd33eSHarry Liebel * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27561cd33eSHarry Liebel * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28561cd33eSHarry Liebel * POSSIBILITY OF SUCH DAMAGE. 29561cd33eSHarry Liebel */ 30561cd33eSHarry Liebel 31561cd33eSHarry Liebel #include <assert.h> 325f0cdb05SDan Handley #include <bl_common.h> 3397043ac9SDan Handley #include <debug.h> 3497043ac9SDan Handley #include <errno.h> 3535e98e55SDan Handley #include <firmware_image_package.h> 3635e98e55SDan Handley #include <io_driver.h> 3735e98e55SDan Handley #include <io_fip.h> 3897043ac9SDan Handley #include <io_storage.h> 3997043ac9SDan Handley #include <platform.h> 405f0cdb05SDan Handley #include <platform_def.h> 4197043ac9SDan Handley #include <stdint.h> 4297043ac9SDan Handley #include <string.h> 43*32f0d3c6SDouglas Raillard #include <utils.h> 4497043ac9SDan Handley #include <uuid.h> 45561cd33eSHarry Liebel 46561cd33eSHarry Liebel /* Useful for printing UUIDs when debugging.*/ 47561cd33eSHarry Liebel #define PRINT_UUID2(x) \ 48561cd33eSHarry Liebel "%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", \ 49561cd33eSHarry Liebel x.time_low, x.time_mid, x.time_hi_and_version, \ 50561cd33eSHarry Liebel x.clock_seq_hi_and_reserved, x.clock_seq_low, \ 51561cd33eSHarry Liebel x.node[0], x.node[1], x.node[2], x.node[3], \ 52561cd33eSHarry Liebel x.node[4], x.node[5] 53561cd33eSHarry Liebel 54561cd33eSHarry Liebel typedef struct { 55561cd33eSHarry Liebel /* Put file_pos above the struct to allow {0} on static init. 56561cd33eSHarry Liebel * It is a workaround for a known bug in GCC 57561cd33eSHarry Liebel * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119 58561cd33eSHarry Liebel */ 59561cd33eSHarry Liebel unsigned int file_pos; 60fb037bfbSDan Handley fip_toc_entry_t entry; 61fb037bfbSDan Handley } file_state_t; 62561cd33eSHarry Liebel 63561cd33eSHarry Liebel static const uuid_t uuid_null = {0}; 64fb037bfbSDan Handley static file_state_t current_file = {0}; 65625de1d4SDan Handley static uintptr_t backend_dev_handle; 66625de1d4SDan Handley static uintptr_t backend_image_spec; 67561cd33eSHarry Liebel 68561cd33eSHarry Liebel 69561cd33eSHarry Liebel /* Firmware Image Package driver functions */ 70625de1d4SDan Handley static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); 71625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, 72fb037bfbSDan Handley io_entity_t *entity); 73fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length); 74625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, 75561cd33eSHarry Liebel size_t *length_read); 76fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity); 77625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params); 78fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info); 79561cd33eSHarry Liebel 80561cd33eSHarry Liebel 81561cd33eSHarry Liebel /* Return 0 for equal uuids. */ 82561cd33eSHarry Liebel static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2) 83561cd33eSHarry Liebel { 84561cd33eSHarry Liebel return memcmp(uuid1, uuid2, sizeof(uuid_t)); 85561cd33eSHarry Liebel } 86561cd33eSHarry Liebel 87561cd33eSHarry Liebel 88561cd33eSHarry Liebel /* TODO: We could check version numbers or do a package checksum? */ 89fb037bfbSDan Handley static inline int is_valid_header(fip_toc_header_t *header) 90561cd33eSHarry Liebel { 91561cd33eSHarry Liebel if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) { 92561cd33eSHarry Liebel return 1; 93561cd33eSHarry Liebel } else { 94561cd33eSHarry Liebel return 0; 95561cd33eSHarry Liebel } 96561cd33eSHarry Liebel } 97561cd33eSHarry Liebel 98561cd33eSHarry Liebel 99561cd33eSHarry Liebel /* Identify the device type as a virtual driver */ 100fb037bfbSDan Handley io_type_t device_type_fip(void) 101561cd33eSHarry Liebel { 102561cd33eSHarry Liebel return IO_TYPE_FIRMWARE_IMAGE_PACKAGE; 103561cd33eSHarry Liebel } 104561cd33eSHarry Liebel 105561cd33eSHarry Liebel 106625de1d4SDan Handley static const io_dev_connector_t fip_dev_connector = { 107561cd33eSHarry Liebel .dev_open = fip_dev_open 108561cd33eSHarry Liebel }; 109561cd33eSHarry Liebel 110561cd33eSHarry Liebel 111625de1d4SDan Handley static const io_dev_funcs_t fip_dev_funcs = { 112561cd33eSHarry Liebel .type = device_type_fip, 113561cd33eSHarry Liebel .open = fip_file_open, 114561cd33eSHarry Liebel .seek = NULL, 115561cd33eSHarry Liebel .size = fip_file_len, 116561cd33eSHarry Liebel .read = fip_file_read, 117561cd33eSHarry Liebel .write = NULL, 118561cd33eSHarry Liebel .close = fip_file_close, 119561cd33eSHarry Liebel .dev_init = fip_dev_init, 120561cd33eSHarry Liebel .dev_close = fip_dev_close, 121561cd33eSHarry Liebel }; 122561cd33eSHarry Liebel 123561cd33eSHarry Liebel 124625de1d4SDan Handley /* No state associated with this device so structure can be const */ 125625de1d4SDan Handley static const io_dev_info_t fip_dev_info = { 126561cd33eSHarry Liebel .funcs = &fip_dev_funcs, 127561cd33eSHarry Liebel .info = (uintptr_t)NULL 128561cd33eSHarry Liebel }; 129561cd33eSHarry Liebel 130561cd33eSHarry Liebel 131561cd33eSHarry Liebel /* Open a connection to the FIP device */ 13265cd299fSSoren Brinkmann static int fip_dev_open(const uintptr_t dev_spec __unused, 133fb037bfbSDan Handley io_dev_info_t **dev_info) 134561cd33eSHarry Liebel { 135561cd33eSHarry Liebel assert(dev_info != NULL); 136625de1d4SDan Handley *dev_info = (io_dev_info_t *)&fip_dev_info; /* cast away const */ 137561cd33eSHarry Liebel 138e098e244SJuan Castillo return 0; 139561cd33eSHarry Liebel } 140561cd33eSHarry Liebel 141561cd33eSHarry Liebel 142561cd33eSHarry Liebel /* Do some basic package checks. */ 143625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params) 144561cd33eSHarry Liebel { 145e098e244SJuan Castillo int result; 14616948ae1SJuan Castillo unsigned int image_id = (unsigned int)init_params; 147625de1d4SDan Handley uintptr_t backend_handle; 148fb037bfbSDan Handley fip_toc_header_t header; 149561cd33eSHarry Liebel size_t bytes_read; 150561cd33eSHarry Liebel 151561cd33eSHarry Liebel /* Obtain a reference to the image by querying the platform layer */ 15216948ae1SJuan Castillo result = plat_get_image_source(image_id, &backend_dev_handle, 153561cd33eSHarry Liebel &backend_image_spec); 154e098e244SJuan Castillo if (result != 0) { 15516948ae1SJuan Castillo WARN("Failed to obtain reference to image id=%u (%i)\n", 15616948ae1SJuan Castillo image_id, result); 157e098e244SJuan Castillo result = -ENOENT; 158561cd33eSHarry Liebel goto fip_dev_init_exit; 159561cd33eSHarry Liebel } 160561cd33eSHarry Liebel 161561cd33eSHarry Liebel /* Attempt to access the FIP image */ 162561cd33eSHarry Liebel result = io_open(backend_dev_handle, backend_image_spec, 163561cd33eSHarry Liebel &backend_handle); 164e098e244SJuan Castillo if (result != 0) { 16516948ae1SJuan Castillo WARN("Failed to access image id=%u (%i)\n", image_id, result); 166e098e244SJuan Castillo result = -ENOENT; 167561cd33eSHarry Liebel goto fip_dev_init_exit; 168561cd33eSHarry Liebel } 169561cd33eSHarry Liebel 170625de1d4SDan Handley result = io_read(backend_handle, (uintptr_t)&header, sizeof(header), 171625de1d4SDan Handley &bytes_read); 172e098e244SJuan Castillo if (result == 0) { 173561cd33eSHarry Liebel if (!is_valid_header(&header)) { 17408c28d53SJeenu Viswambharan WARN("Firmware Image Package header check failed.\n"); 175e098e244SJuan Castillo result = -ENOENT; 176561cd33eSHarry Liebel } else { 1776ad2e461SDan Handley VERBOSE("FIP header looks OK.\n"); 178561cd33eSHarry Liebel } 179561cd33eSHarry Liebel } 180561cd33eSHarry Liebel 181561cd33eSHarry Liebel io_close(backend_handle); 182561cd33eSHarry Liebel 183561cd33eSHarry Liebel fip_dev_init_exit: 184561cd33eSHarry Liebel return result; 185561cd33eSHarry Liebel } 186561cd33eSHarry Liebel 187561cd33eSHarry Liebel /* Close a connection to the FIP device */ 188fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info) 189561cd33eSHarry Liebel { 190561cd33eSHarry Liebel /* TODO: Consider tracking open files and cleaning them up here */ 191561cd33eSHarry Liebel 192561cd33eSHarry Liebel /* Clear the backend. */ 193625de1d4SDan Handley backend_dev_handle = (uintptr_t)NULL; 194625de1d4SDan Handley backend_image_spec = (uintptr_t)NULL; 195561cd33eSHarry Liebel 196e098e244SJuan Castillo return 0; 197561cd33eSHarry Liebel } 198561cd33eSHarry Liebel 199561cd33eSHarry Liebel 200561cd33eSHarry Liebel /* Open a file for access from package. */ 201625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, 202fb037bfbSDan Handley io_entity_t *entity) 203561cd33eSHarry Liebel { 204e098e244SJuan Castillo int result; 205625de1d4SDan Handley uintptr_t backend_handle; 20616948ae1SJuan Castillo const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec; 207561cd33eSHarry Liebel size_t bytes_read; 208561cd33eSHarry Liebel int found_file = 0; 209561cd33eSHarry Liebel 21016948ae1SJuan Castillo assert(uuid_spec != NULL); 211561cd33eSHarry Liebel assert(entity != NULL); 212561cd33eSHarry Liebel 213561cd33eSHarry Liebel /* Can only have one file open at a time for the moment. We need to 214561cd33eSHarry Liebel * track state like file cursor position. We know the header lives at 215561cd33eSHarry Liebel * offset zero, so this entry should never be zero for an active file. 216561cd33eSHarry Liebel * When the system supports dynamic memory allocation we can allow more 217561cd33eSHarry Liebel * than one open file at a time if needed. 218561cd33eSHarry Liebel */ 219561cd33eSHarry Liebel if (current_file.entry.offset_address != 0) { 22008c28d53SJeenu Viswambharan WARN("fip_file_open : Only one open file at a time.\n"); 221e098e244SJuan Castillo return -ENOMEM; 222561cd33eSHarry Liebel } 223561cd33eSHarry Liebel 224561cd33eSHarry Liebel /* Attempt to access the FIP image */ 225561cd33eSHarry Liebel result = io_open(backend_dev_handle, backend_image_spec, 226561cd33eSHarry Liebel &backend_handle); 227e098e244SJuan Castillo if (result != 0) { 22808c28d53SJeenu Viswambharan WARN("Failed to open Firmware Image Package (%i)\n", result); 229e098e244SJuan Castillo result = -ENOENT; 230561cd33eSHarry Liebel goto fip_file_open_exit; 231561cd33eSHarry Liebel } 232561cd33eSHarry Liebel 233561cd33eSHarry Liebel /* Seek past the FIP header into the Table of Contents */ 234fb037bfbSDan Handley result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t)); 235e098e244SJuan Castillo if (result != 0) { 23608c28d53SJeenu Viswambharan WARN("fip_file_open: failed to seek\n"); 237e098e244SJuan Castillo result = -ENOENT; 238561cd33eSHarry Liebel goto fip_file_open_close; 239561cd33eSHarry Liebel } 240561cd33eSHarry Liebel 241561cd33eSHarry Liebel found_file = 0; 242561cd33eSHarry Liebel do { 243625de1d4SDan Handley result = io_read(backend_handle, 244625de1d4SDan Handley (uintptr_t)¤t_file.entry, 245561cd33eSHarry Liebel sizeof(current_file.entry), 246561cd33eSHarry Liebel &bytes_read); 247e098e244SJuan Castillo if (result == 0) { 248561cd33eSHarry Liebel if (compare_uuids(¤t_file.entry.uuid, 24916948ae1SJuan Castillo &uuid_spec->uuid) == 0) { 250561cd33eSHarry Liebel found_file = 1; 251561cd33eSHarry Liebel break; 252561cd33eSHarry Liebel } 253561cd33eSHarry Liebel } else { 25408c28d53SJeenu Viswambharan WARN("Failed to read FIP (%i)\n", result); 255561cd33eSHarry Liebel goto fip_file_open_close; 256561cd33eSHarry Liebel } 257561cd33eSHarry Liebel } while (compare_uuids(¤t_file.entry.uuid, &uuid_null) != 0); 258561cd33eSHarry Liebel 259561cd33eSHarry Liebel if (found_file == 1) { 260561cd33eSHarry Liebel /* All fine. Update entity info with file state and return. Set 261561cd33eSHarry Liebel * the file position to 0. The 'current_file.entry' holds the 262561cd33eSHarry Liebel * base and size of the file. 263561cd33eSHarry Liebel */ 264561cd33eSHarry Liebel current_file.file_pos = 0; 265561cd33eSHarry Liebel entity->info = (uintptr_t)¤t_file; 266561cd33eSHarry Liebel } else { 267561cd33eSHarry Liebel /* Did not find the file in the FIP. */ 268dd3dc32fSJeenu Viswambharan current_file.entry.offset_address = 0; 269e098e244SJuan Castillo result = -ENOENT; 270561cd33eSHarry Liebel } 271561cd33eSHarry Liebel 272561cd33eSHarry Liebel fip_file_open_close: 273561cd33eSHarry Liebel io_close(backend_handle); 274561cd33eSHarry Liebel 275561cd33eSHarry Liebel fip_file_open_exit: 276561cd33eSHarry Liebel return result; 277561cd33eSHarry Liebel } 278561cd33eSHarry Liebel 279561cd33eSHarry Liebel 280561cd33eSHarry Liebel /* Return the size of a file in package */ 281fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length) 282561cd33eSHarry Liebel { 283561cd33eSHarry Liebel assert(entity != NULL); 284561cd33eSHarry Liebel assert(length != NULL); 285561cd33eSHarry Liebel 286fb037bfbSDan Handley *length = ((file_state_t *)entity->info)->entry.size; 287561cd33eSHarry Liebel 288e098e244SJuan Castillo return 0; 289561cd33eSHarry Liebel } 290561cd33eSHarry Liebel 291561cd33eSHarry Liebel 292561cd33eSHarry Liebel /* Read data from a file in package */ 293625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, 294561cd33eSHarry Liebel size_t *length_read) 295561cd33eSHarry Liebel { 296e098e244SJuan Castillo int result; 297fb037bfbSDan Handley file_state_t *fp; 298561cd33eSHarry Liebel size_t file_offset; 299561cd33eSHarry Liebel size_t bytes_read; 300625de1d4SDan Handley uintptr_t backend_handle; 301561cd33eSHarry Liebel 302561cd33eSHarry Liebel assert(entity != NULL); 303625de1d4SDan Handley assert(buffer != (uintptr_t)NULL); 304561cd33eSHarry Liebel assert(length_read != NULL); 305625de1d4SDan Handley assert(entity->info != (uintptr_t)NULL); 306561cd33eSHarry Liebel 307561cd33eSHarry Liebel /* Open the backend, attempt to access the blob image */ 308561cd33eSHarry Liebel result = io_open(backend_dev_handle, backend_image_spec, 309561cd33eSHarry Liebel &backend_handle); 310e098e244SJuan Castillo if (result != 0) { 31108c28d53SJeenu Viswambharan WARN("Failed to open FIP (%i)\n", result); 312e098e244SJuan Castillo result = -ENOENT; 313561cd33eSHarry Liebel goto fip_file_read_exit; 314561cd33eSHarry Liebel } 315561cd33eSHarry Liebel 316fb037bfbSDan Handley fp = (file_state_t *)entity->info; 317561cd33eSHarry Liebel 318561cd33eSHarry Liebel /* Seek to the position in the FIP where the payload lives */ 319561cd33eSHarry Liebel file_offset = fp->entry.offset_address + fp->file_pos; 320561cd33eSHarry Liebel result = io_seek(backend_handle, IO_SEEK_SET, file_offset); 321e098e244SJuan Castillo if (result != 0) { 32208c28d53SJeenu Viswambharan WARN("fip_file_read: failed to seek\n"); 323e098e244SJuan Castillo result = -ENOENT; 324561cd33eSHarry Liebel goto fip_file_read_close; 325561cd33eSHarry Liebel } 326561cd33eSHarry Liebel 327561cd33eSHarry Liebel result = io_read(backend_handle, buffer, length, &bytes_read); 328e098e244SJuan Castillo if (result != 0) { 329561cd33eSHarry Liebel /* We cannot read our data. Fail. */ 33008c28d53SJeenu Viswambharan WARN("Failed to read payload (%i)\n", result); 331e098e244SJuan Castillo result = -ENOENT; 332561cd33eSHarry Liebel goto fip_file_read_close; 333561cd33eSHarry Liebel } else { 334561cd33eSHarry Liebel /* Set caller length and new file position. */ 335561cd33eSHarry Liebel *length_read = bytes_read; 336561cd33eSHarry Liebel fp->file_pos += bytes_read; 337561cd33eSHarry Liebel } 338561cd33eSHarry Liebel 339561cd33eSHarry Liebel /* Close the backend. */ 340561cd33eSHarry Liebel fip_file_read_close: 341561cd33eSHarry Liebel io_close(backend_handle); 342561cd33eSHarry Liebel 343561cd33eSHarry Liebel fip_file_read_exit: 344561cd33eSHarry Liebel return result; 345561cd33eSHarry Liebel } 346561cd33eSHarry Liebel 347561cd33eSHarry Liebel 348561cd33eSHarry Liebel /* Close a file in package */ 349fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity) 350561cd33eSHarry Liebel { 351561cd33eSHarry Liebel /* Clear our current file pointer. 352561cd33eSHarry Liebel * If we had malloc() we would free() here. 353561cd33eSHarry Liebel */ 354561cd33eSHarry Liebel if (current_file.entry.offset_address != 0) { 355*32f0d3c6SDouglas Raillard zeromem(¤t_file, sizeof(current_file)); 356561cd33eSHarry Liebel } 357561cd33eSHarry Liebel 358561cd33eSHarry Liebel /* Clear the Entity info. */ 359561cd33eSHarry Liebel entity->info = 0; 360561cd33eSHarry Liebel 361e098e244SJuan Castillo return 0; 362561cd33eSHarry Liebel } 363561cd33eSHarry Liebel 364561cd33eSHarry Liebel /* Exported functions */ 365561cd33eSHarry Liebel 366561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */ 367625de1d4SDan Handley int register_io_dev_fip(const io_dev_connector_t **dev_con) 368561cd33eSHarry Liebel { 369e098e244SJuan Castillo int result; 370561cd33eSHarry Liebel assert(dev_con != NULL); 371561cd33eSHarry Liebel 372561cd33eSHarry Liebel result = io_register_device(&fip_dev_info); 373e098e244SJuan Castillo if (result == 0) 374561cd33eSHarry Liebel *dev_con = &fip_dev_connector; 375561cd33eSHarry Liebel 376561cd33eSHarry Liebel return result; 377561cd33eSHarry Liebel } 378