1561cd33eSHarry Liebel /* 232f0d3c6SDouglas Raillard * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved. 3561cd33eSHarry Liebel * 4*82cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5561cd33eSHarry Liebel */ 6561cd33eSHarry Liebel 7561cd33eSHarry Liebel #include <assert.h> 85f0cdb05SDan Handley #include <bl_common.h> 997043ac9SDan Handley #include <debug.h> 1097043ac9SDan Handley #include <errno.h> 1135e98e55SDan Handley #include <firmware_image_package.h> 1235e98e55SDan Handley #include <io_driver.h> 1335e98e55SDan Handley #include <io_fip.h> 1497043ac9SDan Handley #include <io_storage.h> 1597043ac9SDan Handley #include <platform.h> 165f0cdb05SDan Handley #include <platform_def.h> 1797043ac9SDan Handley #include <stdint.h> 1897043ac9SDan Handley #include <string.h> 1932f0d3c6SDouglas Raillard #include <utils.h> 2097043ac9SDan Handley #include <uuid.h> 21561cd33eSHarry Liebel 22561cd33eSHarry Liebel /* Useful for printing UUIDs when debugging.*/ 23561cd33eSHarry Liebel #define PRINT_UUID2(x) \ 24561cd33eSHarry Liebel "%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", \ 25561cd33eSHarry Liebel x.time_low, x.time_mid, x.time_hi_and_version, \ 26561cd33eSHarry Liebel x.clock_seq_hi_and_reserved, x.clock_seq_low, \ 27561cd33eSHarry Liebel x.node[0], x.node[1], x.node[2], x.node[3], \ 28561cd33eSHarry Liebel x.node[4], x.node[5] 29561cd33eSHarry Liebel 30561cd33eSHarry Liebel typedef struct { 31561cd33eSHarry Liebel /* Put file_pos above the struct to allow {0} on static init. 32561cd33eSHarry Liebel * It is a workaround for a known bug in GCC 33561cd33eSHarry Liebel * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119 34561cd33eSHarry Liebel */ 35561cd33eSHarry Liebel unsigned int file_pos; 36fb037bfbSDan Handley fip_toc_entry_t entry; 37fb037bfbSDan Handley } file_state_t; 38561cd33eSHarry Liebel 39561cd33eSHarry Liebel static const uuid_t uuid_null = {0}; 40fb037bfbSDan Handley static file_state_t current_file = {0}; 41625de1d4SDan Handley static uintptr_t backend_dev_handle; 42625de1d4SDan Handley static uintptr_t backend_image_spec; 43561cd33eSHarry Liebel 44561cd33eSHarry Liebel 45561cd33eSHarry Liebel /* Firmware Image Package driver functions */ 46625de1d4SDan Handley static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); 47625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, 48fb037bfbSDan Handley io_entity_t *entity); 49fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length); 50625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, 51561cd33eSHarry Liebel size_t *length_read); 52fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity); 53625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params); 54fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info); 55561cd33eSHarry Liebel 56561cd33eSHarry Liebel 57561cd33eSHarry Liebel /* Return 0 for equal uuids. */ 58561cd33eSHarry Liebel static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2) 59561cd33eSHarry Liebel { 60561cd33eSHarry Liebel return memcmp(uuid1, uuid2, sizeof(uuid_t)); 61561cd33eSHarry Liebel } 62561cd33eSHarry Liebel 63561cd33eSHarry Liebel 64561cd33eSHarry Liebel /* TODO: We could check version numbers or do a package checksum? */ 65fb037bfbSDan Handley static inline int is_valid_header(fip_toc_header_t *header) 66561cd33eSHarry Liebel { 67561cd33eSHarry Liebel if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) { 68561cd33eSHarry Liebel return 1; 69561cd33eSHarry Liebel } else { 70561cd33eSHarry Liebel return 0; 71561cd33eSHarry Liebel } 72561cd33eSHarry Liebel } 73561cd33eSHarry Liebel 74561cd33eSHarry Liebel 75561cd33eSHarry Liebel /* Identify the device type as a virtual driver */ 76fb037bfbSDan Handley io_type_t device_type_fip(void) 77561cd33eSHarry Liebel { 78561cd33eSHarry Liebel return IO_TYPE_FIRMWARE_IMAGE_PACKAGE; 79561cd33eSHarry Liebel } 80561cd33eSHarry Liebel 81561cd33eSHarry Liebel 82625de1d4SDan Handley static const io_dev_connector_t fip_dev_connector = { 83561cd33eSHarry Liebel .dev_open = fip_dev_open 84561cd33eSHarry Liebel }; 85561cd33eSHarry Liebel 86561cd33eSHarry Liebel 87625de1d4SDan Handley static const io_dev_funcs_t fip_dev_funcs = { 88561cd33eSHarry Liebel .type = device_type_fip, 89561cd33eSHarry Liebel .open = fip_file_open, 90561cd33eSHarry Liebel .seek = NULL, 91561cd33eSHarry Liebel .size = fip_file_len, 92561cd33eSHarry Liebel .read = fip_file_read, 93561cd33eSHarry Liebel .write = NULL, 94561cd33eSHarry Liebel .close = fip_file_close, 95561cd33eSHarry Liebel .dev_init = fip_dev_init, 96561cd33eSHarry Liebel .dev_close = fip_dev_close, 97561cd33eSHarry Liebel }; 98561cd33eSHarry Liebel 99561cd33eSHarry Liebel 100625de1d4SDan Handley /* No state associated with this device so structure can be const */ 101625de1d4SDan Handley static const io_dev_info_t fip_dev_info = { 102561cd33eSHarry Liebel .funcs = &fip_dev_funcs, 103561cd33eSHarry Liebel .info = (uintptr_t)NULL 104561cd33eSHarry Liebel }; 105561cd33eSHarry Liebel 106561cd33eSHarry Liebel 107561cd33eSHarry Liebel /* Open a connection to the FIP device */ 10865cd299fSSoren Brinkmann static int fip_dev_open(const uintptr_t dev_spec __unused, 109fb037bfbSDan Handley io_dev_info_t **dev_info) 110561cd33eSHarry Liebel { 111561cd33eSHarry Liebel assert(dev_info != NULL); 112625de1d4SDan Handley *dev_info = (io_dev_info_t *)&fip_dev_info; /* cast away const */ 113561cd33eSHarry Liebel 114e098e244SJuan Castillo return 0; 115561cd33eSHarry Liebel } 116561cd33eSHarry Liebel 117561cd33eSHarry Liebel 118561cd33eSHarry Liebel /* Do some basic package checks. */ 119625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params) 120561cd33eSHarry Liebel { 121e098e244SJuan Castillo int result; 12216948ae1SJuan Castillo unsigned int image_id = (unsigned int)init_params; 123625de1d4SDan Handley uintptr_t backend_handle; 124fb037bfbSDan Handley fip_toc_header_t header; 125561cd33eSHarry Liebel size_t bytes_read; 126561cd33eSHarry Liebel 127561cd33eSHarry Liebel /* Obtain a reference to the image by querying the platform layer */ 12816948ae1SJuan Castillo result = plat_get_image_source(image_id, &backend_dev_handle, 129561cd33eSHarry Liebel &backend_image_spec); 130e098e244SJuan Castillo if (result != 0) { 13116948ae1SJuan Castillo WARN("Failed to obtain reference to image id=%u (%i)\n", 13216948ae1SJuan Castillo image_id, result); 133e098e244SJuan Castillo result = -ENOENT; 134561cd33eSHarry Liebel goto fip_dev_init_exit; 135561cd33eSHarry Liebel } 136561cd33eSHarry Liebel 137561cd33eSHarry Liebel /* Attempt to access the FIP image */ 138561cd33eSHarry Liebel result = io_open(backend_dev_handle, backend_image_spec, 139561cd33eSHarry Liebel &backend_handle); 140e098e244SJuan Castillo if (result != 0) { 14116948ae1SJuan Castillo WARN("Failed to access image id=%u (%i)\n", image_id, result); 142e098e244SJuan Castillo result = -ENOENT; 143561cd33eSHarry Liebel goto fip_dev_init_exit; 144561cd33eSHarry Liebel } 145561cd33eSHarry Liebel 146625de1d4SDan Handley result = io_read(backend_handle, (uintptr_t)&header, sizeof(header), 147625de1d4SDan Handley &bytes_read); 148e098e244SJuan Castillo if (result == 0) { 149561cd33eSHarry Liebel if (!is_valid_header(&header)) { 15008c28d53SJeenu Viswambharan WARN("Firmware Image Package header check failed.\n"); 151e098e244SJuan Castillo result = -ENOENT; 152561cd33eSHarry Liebel } else { 1536ad2e461SDan Handley VERBOSE("FIP header looks OK.\n"); 154561cd33eSHarry Liebel } 155561cd33eSHarry Liebel } 156561cd33eSHarry Liebel 157561cd33eSHarry Liebel io_close(backend_handle); 158561cd33eSHarry Liebel 159561cd33eSHarry Liebel fip_dev_init_exit: 160561cd33eSHarry Liebel return result; 161561cd33eSHarry Liebel } 162561cd33eSHarry Liebel 163561cd33eSHarry Liebel /* Close a connection to the FIP device */ 164fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info) 165561cd33eSHarry Liebel { 166561cd33eSHarry Liebel /* TODO: Consider tracking open files and cleaning them up here */ 167561cd33eSHarry Liebel 168561cd33eSHarry Liebel /* Clear the backend. */ 169625de1d4SDan Handley backend_dev_handle = (uintptr_t)NULL; 170625de1d4SDan Handley backend_image_spec = (uintptr_t)NULL; 171561cd33eSHarry Liebel 172e098e244SJuan Castillo return 0; 173561cd33eSHarry Liebel } 174561cd33eSHarry Liebel 175561cd33eSHarry Liebel 176561cd33eSHarry Liebel /* Open a file for access from package. */ 177625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, 178fb037bfbSDan Handley io_entity_t *entity) 179561cd33eSHarry Liebel { 180e098e244SJuan Castillo int result; 181625de1d4SDan Handley uintptr_t backend_handle; 18216948ae1SJuan Castillo const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec; 183561cd33eSHarry Liebel size_t bytes_read; 184561cd33eSHarry Liebel int found_file = 0; 185561cd33eSHarry Liebel 18616948ae1SJuan Castillo assert(uuid_spec != NULL); 187561cd33eSHarry Liebel assert(entity != NULL); 188561cd33eSHarry Liebel 189561cd33eSHarry Liebel /* Can only have one file open at a time for the moment. We need to 190561cd33eSHarry Liebel * track state like file cursor position. We know the header lives at 191561cd33eSHarry Liebel * offset zero, so this entry should never be zero for an active file. 192561cd33eSHarry Liebel * When the system supports dynamic memory allocation we can allow more 193561cd33eSHarry Liebel * than one open file at a time if needed. 194561cd33eSHarry Liebel */ 195561cd33eSHarry Liebel if (current_file.entry.offset_address != 0) { 19608c28d53SJeenu Viswambharan WARN("fip_file_open : Only one open file at a time.\n"); 197e098e244SJuan Castillo return -ENOMEM; 198561cd33eSHarry Liebel } 199561cd33eSHarry Liebel 200561cd33eSHarry Liebel /* Attempt to access the FIP image */ 201561cd33eSHarry Liebel result = io_open(backend_dev_handle, backend_image_spec, 202561cd33eSHarry Liebel &backend_handle); 203e098e244SJuan Castillo if (result != 0) { 20408c28d53SJeenu Viswambharan WARN("Failed to open Firmware Image Package (%i)\n", result); 205e098e244SJuan Castillo result = -ENOENT; 206561cd33eSHarry Liebel goto fip_file_open_exit; 207561cd33eSHarry Liebel } 208561cd33eSHarry Liebel 209561cd33eSHarry Liebel /* Seek past the FIP header into the Table of Contents */ 210fb037bfbSDan Handley result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t)); 211e098e244SJuan Castillo if (result != 0) { 21208c28d53SJeenu Viswambharan WARN("fip_file_open: failed to seek\n"); 213e098e244SJuan Castillo result = -ENOENT; 214561cd33eSHarry Liebel goto fip_file_open_close; 215561cd33eSHarry Liebel } 216561cd33eSHarry Liebel 217561cd33eSHarry Liebel found_file = 0; 218561cd33eSHarry Liebel do { 219625de1d4SDan Handley result = io_read(backend_handle, 220625de1d4SDan Handley (uintptr_t)¤t_file.entry, 221561cd33eSHarry Liebel sizeof(current_file.entry), 222561cd33eSHarry Liebel &bytes_read); 223e098e244SJuan Castillo if (result == 0) { 224561cd33eSHarry Liebel if (compare_uuids(¤t_file.entry.uuid, 22516948ae1SJuan Castillo &uuid_spec->uuid) == 0) { 226561cd33eSHarry Liebel found_file = 1; 227561cd33eSHarry Liebel break; 228561cd33eSHarry Liebel } 229561cd33eSHarry Liebel } else { 23008c28d53SJeenu Viswambharan WARN("Failed to read FIP (%i)\n", result); 231561cd33eSHarry Liebel goto fip_file_open_close; 232561cd33eSHarry Liebel } 233561cd33eSHarry Liebel } while (compare_uuids(¤t_file.entry.uuid, &uuid_null) != 0); 234561cd33eSHarry Liebel 235561cd33eSHarry Liebel if (found_file == 1) { 236561cd33eSHarry Liebel /* All fine. Update entity info with file state and return. Set 237561cd33eSHarry Liebel * the file position to 0. The 'current_file.entry' holds the 238561cd33eSHarry Liebel * base and size of the file. 239561cd33eSHarry Liebel */ 240561cd33eSHarry Liebel current_file.file_pos = 0; 241561cd33eSHarry Liebel entity->info = (uintptr_t)¤t_file; 242561cd33eSHarry Liebel } else { 243561cd33eSHarry Liebel /* Did not find the file in the FIP. */ 244dd3dc32fSJeenu Viswambharan current_file.entry.offset_address = 0; 245e098e244SJuan Castillo result = -ENOENT; 246561cd33eSHarry Liebel } 247561cd33eSHarry Liebel 248561cd33eSHarry Liebel fip_file_open_close: 249561cd33eSHarry Liebel io_close(backend_handle); 250561cd33eSHarry Liebel 251561cd33eSHarry Liebel fip_file_open_exit: 252561cd33eSHarry Liebel return result; 253561cd33eSHarry Liebel } 254561cd33eSHarry Liebel 255561cd33eSHarry Liebel 256561cd33eSHarry Liebel /* Return the size of a file in package */ 257fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length) 258561cd33eSHarry Liebel { 259561cd33eSHarry Liebel assert(entity != NULL); 260561cd33eSHarry Liebel assert(length != NULL); 261561cd33eSHarry Liebel 262fb037bfbSDan Handley *length = ((file_state_t *)entity->info)->entry.size; 263561cd33eSHarry Liebel 264e098e244SJuan Castillo return 0; 265561cd33eSHarry Liebel } 266561cd33eSHarry Liebel 267561cd33eSHarry Liebel 268561cd33eSHarry Liebel /* Read data from a file in package */ 269625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, 270561cd33eSHarry Liebel size_t *length_read) 271561cd33eSHarry Liebel { 272e098e244SJuan Castillo int result; 273fb037bfbSDan Handley file_state_t *fp; 274561cd33eSHarry Liebel size_t file_offset; 275561cd33eSHarry Liebel size_t bytes_read; 276625de1d4SDan Handley uintptr_t backend_handle; 277561cd33eSHarry Liebel 278561cd33eSHarry Liebel assert(entity != NULL); 279625de1d4SDan Handley assert(buffer != (uintptr_t)NULL); 280561cd33eSHarry Liebel assert(length_read != NULL); 281625de1d4SDan Handley assert(entity->info != (uintptr_t)NULL); 282561cd33eSHarry Liebel 283561cd33eSHarry Liebel /* Open the backend, attempt to access the blob image */ 284561cd33eSHarry Liebel result = io_open(backend_dev_handle, backend_image_spec, 285561cd33eSHarry Liebel &backend_handle); 286e098e244SJuan Castillo if (result != 0) { 28708c28d53SJeenu Viswambharan WARN("Failed to open FIP (%i)\n", result); 288e098e244SJuan Castillo result = -ENOENT; 289561cd33eSHarry Liebel goto fip_file_read_exit; 290561cd33eSHarry Liebel } 291561cd33eSHarry Liebel 292fb037bfbSDan Handley fp = (file_state_t *)entity->info; 293561cd33eSHarry Liebel 294561cd33eSHarry Liebel /* Seek to the position in the FIP where the payload lives */ 295561cd33eSHarry Liebel file_offset = fp->entry.offset_address + fp->file_pos; 296561cd33eSHarry Liebel result = io_seek(backend_handle, IO_SEEK_SET, file_offset); 297e098e244SJuan Castillo if (result != 0) { 29808c28d53SJeenu Viswambharan WARN("fip_file_read: failed to seek\n"); 299e098e244SJuan Castillo result = -ENOENT; 300561cd33eSHarry Liebel goto fip_file_read_close; 301561cd33eSHarry Liebel } 302561cd33eSHarry Liebel 303561cd33eSHarry Liebel result = io_read(backend_handle, buffer, length, &bytes_read); 304e098e244SJuan Castillo if (result != 0) { 305561cd33eSHarry Liebel /* We cannot read our data. Fail. */ 30608c28d53SJeenu Viswambharan WARN("Failed to read payload (%i)\n", result); 307e098e244SJuan Castillo result = -ENOENT; 308561cd33eSHarry Liebel goto fip_file_read_close; 309561cd33eSHarry Liebel } else { 310561cd33eSHarry Liebel /* Set caller length and new file position. */ 311561cd33eSHarry Liebel *length_read = bytes_read; 312561cd33eSHarry Liebel fp->file_pos += bytes_read; 313561cd33eSHarry Liebel } 314561cd33eSHarry Liebel 315561cd33eSHarry Liebel /* Close the backend. */ 316561cd33eSHarry Liebel fip_file_read_close: 317561cd33eSHarry Liebel io_close(backend_handle); 318561cd33eSHarry Liebel 319561cd33eSHarry Liebel fip_file_read_exit: 320561cd33eSHarry Liebel return result; 321561cd33eSHarry Liebel } 322561cd33eSHarry Liebel 323561cd33eSHarry Liebel 324561cd33eSHarry Liebel /* Close a file in package */ 325fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity) 326561cd33eSHarry Liebel { 327561cd33eSHarry Liebel /* Clear our current file pointer. 328561cd33eSHarry Liebel * If we had malloc() we would free() here. 329561cd33eSHarry Liebel */ 330561cd33eSHarry Liebel if (current_file.entry.offset_address != 0) { 33132f0d3c6SDouglas Raillard zeromem(¤t_file, sizeof(current_file)); 332561cd33eSHarry Liebel } 333561cd33eSHarry Liebel 334561cd33eSHarry Liebel /* Clear the Entity info. */ 335561cd33eSHarry Liebel entity->info = 0; 336561cd33eSHarry Liebel 337e098e244SJuan Castillo return 0; 338561cd33eSHarry Liebel } 339561cd33eSHarry Liebel 340561cd33eSHarry Liebel /* Exported functions */ 341561cd33eSHarry Liebel 342561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */ 343625de1d4SDan Handley int register_io_dev_fip(const io_dev_connector_t **dev_con) 344561cd33eSHarry Liebel { 345e098e244SJuan Castillo int result; 346561cd33eSHarry Liebel assert(dev_con != NULL); 347561cd33eSHarry Liebel 348561cd33eSHarry Liebel result = io_register_device(&fip_dev_info); 349e098e244SJuan Castillo if (result == 0) 350561cd33eSHarry Liebel *dev_con = &fip_dev_connector; 351561cd33eSHarry Liebel 352561cd33eSHarry Liebel return result; 353561cd33eSHarry Liebel } 354