1561cd33eSHarry Liebel /* 2fb1198b1SAntonio Nino Diaz * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. 3561cd33eSHarry Liebel * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5561cd33eSHarry Liebel */ 6561cd33eSHarry Liebel 7561cd33eSHarry Liebel #include <assert.h> 897043ac9SDan Handley #include <errno.h> 997043ac9SDan Handley #include <stdint.h> 1097043ac9SDan Handley #include <string.h> 11*09d40e0eSAntonio Nino Diaz 12*09d40e0eSAntonio Nino Diaz #include <platform_def.h> 13*09d40e0eSAntonio Nino Diaz 14*09d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 15*09d40e0eSAntonio Nino Diaz #include <common/debug.h> 16*09d40e0eSAntonio Nino Diaz #include <drivers/io/io_driver.h> 17*09d40e0eSAntonio Nino Diaz #include <drivers/io/io_fip.h> 18*09d40e0eSAntonio Nino Diaz #include <drivers/io/io_storage.h> 19*09d40e0eSAntonio Nino Diaz #include <lib/utils.h> 20*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 21*09d40e0eSAntonio Nino Diaz #include <tools_share/firmware_image_package.h> 22*09d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h> 23561cd33eSHarry Liebel 24d4d598e9SRuchika Gupta #ifndef MAX_FIP_DEVICES 25d4d598e9SRuchika Gupta #define MAX_FIP_DEVICES 1 26d4d598e9SRuchika Gupta #endif 27d4d598e9SRuchika Gupta 28561cd33eSHarry Liebel /* Useful for printing UUIDs when debugging.*/ 29561cd33eSHarry Liebel #define PRINT_UUID2(x) \ 30561cd33eSHarry Liebel "%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", \ 31561cd33eSHarry Liebel x.time_low, x.time_mid, x.time_hi_and_version, \ 32561cd33eSHarry Liebel x.clock_seq_hi_and_reserved, x.clock_seq_low, \ 33561cd33eSHarry Liebel x.node[0], x.node[1], x.node[2], x.node[3], \ 34561cd33eSHarry Liebel x.node[4], x.node[5] 35561cd33eSHarry Liebel 36561cd33eSHarry Liebel typedef struct { 37561cd33eSHarry Liebel unsigned int file_pos; 38fb037bfbSDan Handley fip_toc_entry_t entry; 39fb037bfbSDan Handley } file_state_t; 40561cd33eSHarry Liebel 41d4d598e9SRuchika Gupta /* 42d4d598e9SRuchika Gupta * Maintain dev_spec per FIP Device 43d4d598e9SRuchika Gupta * TODO - Add backend handles and file state 44d4d598e9SRuchika Gupta * per FIP device here once backends like io_memmap 45d4d598e9SRuchika Gupta * can support multiple open files 46d4d598e9SRuchika Gupta */ 47d4d598e9SRuchika Gupta typedef struct { 48d4d598e9SRuchika Gupta uintptr_t dev_spec; 49d4d598e9SRuchika Gupta } fip_dev_state_t; 50d4d598e9SRuchika Gupta 5103364865SRoberto Vargas static const uuid_t uuid_null = { {0} }; 52d4d598e9SRuchika Gupta /* 53d4d598e9SRuchika Gupta * Only one file can be open across all FIP device 54d4d598e9SRuchika Gupta * as backends like io_memmap don't support 55d4d598e9SRuchika Gupta * multiple open files. The file state and 56d4d598e9SRuchika Gupta * backend handle should be maintained per FIP device 57d4d598e9SRuchika Gupta * if the same support is available in the backend 58d4d598e9SRuchika Gupta */ 59fb037bfbSDan Handley static file_state_t current_file = {0}; 60625de1d4SDan Handley static uintptr_t backend_dev_handle; 61625de1d4SDan Handley static uintptr_t backend_image_spec; 62561cd33eSHarry Liebel 63d4d598e9SRuchika Gupta static fip_dev_state_t state_pool[MAX_FIP_DEVICES]; 64d4d598e9SRuchika Gupta static io_dev_info_t dev_info_pool[MAX_FIP_DEVICES]; 65d4d598e9SRuchika Gupta 66d4d598e9SRuchika Gupta /* Track number of allocated fip devices */ 67d4d598e9SRuchika Gupta static unsigned int fip_dev_count; 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 */ 1007fabe1a8SRoberto Vargas static 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 123d4d598e9SRuchika Gupta /* Locate a file state in the pool, specified by address */ 124d4d598e9SRuchika Gupta static int find_first_fip_state(const uintptr_t dev_spec, 125d4d598e9SRuchika Gupta unsigned int *index_out) 126d4d598e9SRuchika Gupta { 127d4d598e9SRuchika Gupta int result = -ENOENT; 128d4d598e9SRuchika Gupta unsigned int index; 129561cd33eSHarry Liebel 130d4d598e9SRuchika Gupta for (index = 0; index < (unsigned int)MAX_FIP_DEVICES; ++index) { 131d4d598e9SRuchika Gupta /* dev_spec is used as identifier since it's unique */ 132d4d598e9SRuchika Gupta if (state_pool[index].dev_spec == dev_spec) { 133d4d598e9SRuchika Gupta result = 0; 134d4d598e9SRuchika Gupta *index_out = index; 135d4d598e9SRuchika Gupta break; 136d4d598e9SRuchika Gupta } 137d4d598e9SRuchika Gupta } 138d4d598e9SRuchika Gupta return result; 139d4d598e9SRuchika Gupta } 140561cd33eSHarry Liebel 141561cd33eSHarry Liebel 142d4d598e9SRuchika Gupta /* Allocate a device info from the pool and return a pointer to it */ 143d4d598e9SRuchika Gupta static int allocate_dev_info(io_dev_info_t **dev_info) 144d4d598e9SRuchika Gupta { 145d4d598e9SRuchika Gupta int result = -ENOMEM; 146d4d598e9SRuchika Gupta 147d4d598e9SRuchika Gupta assert(dev_info != NULL); 148d4d598e9SRuchika Gupta 149d4d598e9SRuchika Gupta if (fip_dev_count < (unsigned int)MAX_FIP_DEVICES) { 150d4d598e9SRuchika Gupta unsigned int index = 0; 151d4d598e9SRuchika Gupta 152d4d598e9SRuchika Gupta result = find_first_fip_state(0, &index); 153d4d598e9SRuchika Gupta assert(result == 0); 154d4d598e9SRuchika Gupta /* initialize dev_info */ 155d4d598e9SRuchika Gupta dev_info_pool[index].funcs = &fip_dev_funcs; 156d4d598e9SRuchika Gupta dev_info_pool[index].info = 157d4d598e9SRuchika Gupta (uintptr_t)&state_pool[index]; 158d4d598e9SRuchika Gupta *dev_info = &dev_info_pool[index]; 159d4d598e9SRuchika Gupta ++fip_dev_count; 160d4d598e9SRuchika Gupta } 161d4d598e9SRuchika Gupta 162d4d598e9SRuchika Gupta return result; 163d4d598e9SRuchika Gupta } 164d4d598e9SRuchika Gupta 165d4d598e9SRuchika Gupta /* Release a device info to the pool */ 166d4d598e9SRuchika Gupta static int free_dev_info(io_dev_info_t *dev_info) 167d4d598e9SRuchika Gupta { 168d4d598e9SRuchika Gupta int result; 169d4d598e9SRuchika Gupta unsigned int index = 0; 170d4d598e9SRuchika Gupta fip_dev_state_t *state; 171d4d598e9SRuchika Gupta 172d4d598e9SRuchika Gupta assert(dev_info != NULL); 173d4d598e9SRuchika Gupta 174d4d598e9SRuchika Gupta state = (fip_dev_state_t *)dev_info->info; 175d4d598e9SRuchika Gupta result = find_first_fip_state(state->dev_spec, &index); 176d4d598e9SRuchika Gupta if (result == 0) { 177d4d598e9SRuchika Gupta /* free if device info is valid */ 178d4d598e9SRuchika Gupta zeromem(state, sizeof(fip_dev_state_t)); 179d4d598e9SRuchika Gupta --fip_dev_count; 180d4d598e9SRuchika Gupta } 181d4d598e9SRuchika Gupta 182d4d598e9SRuchika Gupta return result; 183d4d598e9SRuchika Gupta } 184d4d598e9SRuchika Gupta 185d4d598e9SRuchika Gupta /* 186d4d598e9SRuchika Gupta * Multiple FIP devices can be opened depending on the value of 187d4d598e9SRuchika Gupta * MAX_FIP_DEVICES. Given that there is only one backend, only a 188d4d598e9SRuchika Gupta * single file can be open at a time by any FIP device. 189d4d598e9SRuchika Gupta */ 190d4d598e9SRuchika Gupta static int fip_dev_open(const uintptr_t dev_spec, 191fb037bfbSDan Handley io_dev_info_t **dev_info) 192561cd33eSHarry Liebel { 193d4d598e9SRuchika Gupta int result; 194d4d598e9SRuchika Gupta io_dev_info_t *info; 195d4d598e9SRuchika Gupta fip_dev_state_t *state; 196d4d598e9SRuchika Gupta 197561cd33eSHarry Liebel assert(dev_info != NULL); 198d4d598e9SRuchika Gupta #if MAX_FIP_DEVICES > 1 199d4d598e9SRuchika Gupta assert(dev_spec != (uintptr_t)NULL); 200d4d598e9SRuchika Gupta #endif 201d4d598e9SRuchika Gupta 202d4d598e9SRuchika Gupta result = allocate_dev_info(&info); 203d4d598e9SRuchika Gupta if (result != 0) 204d4d598e9SRuchika Gupta return -ENOMEM; 205d4d598e9SRuchika Gupta 206d4d598e9SRuchika Gupta state = (fip_dev_state_t *)info->info; 207d4d598e9SRuchika Gupta 208d4d598e9SRuchika Gupta state->dev_spec = dev_spec; 209d4d598e9SRuchika Gupta 210d4d598e9SRuchika Gupta *dev_info = info; 211561cd33eSHarry Liebel 212e098e244SJuan Castillo return 0; 213561cd33eSHarry Liebel } 214561cd33eSHarry Liebel 215561cd33eSHarry Liebel 216561cd33eSHarry Liebel /* Do some basic package checks. */ 217625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params) 218561cd33eSHarry Liebel { 219e098e244SJuan Castillo int result; 22016948ae1SJuan Castillo unsigned int image_id = (unsigned int)init_params; 221625de1d4SDan Handley uintptr_t backend_handle; 222fb037bfbSDan Handley fip_toc_header_t header; 223561cd33eSHarry Liebel size_t bytes_read; 224561cd33eSHarry Liebel 225561cd33eSHarry Liebel /* Obtain a reference to the image by querying the platform layer */ 22616948ae1SJuan Castillo result = plat_get_image_source(image_id, &backend_dev_handle, 227561cd33eSHarry Liebel &backend_image_spec); 228e098e244SJuan Castillo if (result != 0) { 22916948ae1SJuan Castillo WARN("Failed to obtain reference to image id=%u (%i)\n", 23016948ae1SJuan Castillo image_id, result); 231e098e244SJuan Castillo result = -ENOENT; 232561cd33eSHarry Liebel goto fip_dev_init_exit; 233561cd33eSHarry Liebel } 234561cd33eSHarry Liebel 235561cd33eSHarry Liebel /* Attempt to access the FIP image */ 236561cd33eSHarry Liebel result = io_open(backend_dev_handle, backend_image_spec, 237561cd33eSHarry Liebel &backend_handle); 238e098e244SJuan Castillo if (result != 0) { 23916948ae1SJuan Castillo WARN("Failed to access image id=%u (%i)\n", image_id, result); 240e098e244SJuan Castillo result = -ENOENT; 241561cd33eSHarry Liebel goto fip_dev_init_exit; 242561cd33eSHarry Liebel } 243561cd33eSHarry Liebel 244625de1d4SDan Handley result = io_read(backend_handle, (uintptr_t)&header, sizeof(header), 245625de1d4SDan Handley &bytes_read); 246e098e244SJuan Castillo if (result == 0) { 247561cd33eSHarry Liebel if (!is_valid_header(&header)) { 24808c28d53SJeenu Viswambharan WARN("Firmware Image Package header check failed.\n"); 249e098e244SJuan Castillo result = -ENOENT; 250561cd33eSHarry Liebel } else { 2516ad2e461SDan Handley VERBOSE("FIP header looks OK.\n"); 252561cd33eSHarry Liebel } 253561cd33eSHarry Liebel } 254561cd33eSHarry Liebel 255561cd33eSHarry Liebel io_close(backend_handle); 256561cd33eSHarry Liebel 257561cd33eSHarry Liebel fip_dev_init_exit: 258561cd33eSHarry Liebel return result; 259561cd33eSHarry Liebel } 260561cd33eSHarry Liebel 261561cd33eSHarry Liebel /* Close a connection to the FIP device */ 262fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info) 263561cd33eSHarry Liebel { 264561cd33eSHarry Liebel /* TODO: Consider tracking open files and cleaning them up here */ 265561cd33eSHarry Liebel 266561cd33eSHarry Liebel /* Clear the backend. */ 267625de1d4SDan Handley backend_dev_handle = (uintptr_t)NULL; 268625de1d4SDan Handley backend_image_spec = (uintptr_t)NULL; 269561cd33eSHarry Liebel 270d4d598e9SRuchika Gupta return free_dev_info(dev_info); 271561cd33eSHarry Liebel } 272561cd33eSHarry Liebel 273561cd33eSHarry Liebel 274561cd33eSHarry Liebel /* Open a file for access from package. */ 275625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, 276fb037bfbSDan Handley io_entity_t *entity) 277561cd33eSHarry Liebel { 278e098e244SJuan Castillo int result; 279625de1d4SDan Handley uintptr_t backend_handle; 28016948ae1SJuan Castillo const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec; 281561cd33eSHarry Liebel size_t bytes_read; 282561cd33eSHarry Liebel int found_file = 0; 283561cd33eSHarry Liebel 28416948ae1SJuan Castillo assert(uuid_spec != NULL); 285561cd33eSHarry Liebel assert(entity != NULL); 286561cd33eSHarry Liebel 287561cd33eSHarry Liebel /* Can only have one file open at a time for the moment. We need to 288561cd33eSHarry Liebel * track state like file cursor position. We know the header lives at 289561cd33eSHarry Liebel * offset zero, so this entry should never be zero for an active file. 290561cd33eSHarry Liebel * When the system supports dynamic memory allocation we can allow more 291561cd33eSHarry Liebel * than one open file at a time if needed. 292561cd33eSHarry Liebel */ 293561cd33eSHarry Liebel if (current_file.entry.offset_address != 0) { 29408c28d53SJeenu Viswambharan WARN("fip_file_open : Only one open file at a time.\n"); 295e098e244SJuan Castillo return -ENOMEM; 296561cd33eSHarry Liebel } 297561cd33eSHarry Liebel 298561cd33eSHarry Liebel /* Attempt to access the FIP image */ 299561cd33eSHarry Liebel result = io_open(backend_dev_handle, backend_image_spec, 300561cd33eSHarry Liebel &backend_handle); 301e098e244SJuan Castillo if (result != 0) { 30208c28d53SJeenu Viswambharan WARN("Failed to open Firmware Image Package (%i)\n", result); 303e098e244SJuan Castillo result = -ENOENT; 304561cd33eSHarry Liebel goto fip_file_open_exit; 305561cd33eSHarry Liebel } 306561cd33eSHarry Liebel 307561cd33eSHarry Liebel /* Seek past the FIP header into the Table of Contents */ 308fb037bfbSDan Handley result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t)); 309e098e244SJuan Castillo if (result != 0) { 31008c28d53SJeenu Viswambharan WARN("fip_file_open: failed to seek\n"); 311e098e244SJuan Castillo result = -ENOENT; 312561cd33eSHarry Liebel goto fip_file_open_close; 313561cd33eSHarry Liebel } 314561cd33eSHarry Liebel 315561cd33eSHarry Liebel found_file = 0; 316561cd33eSHarry Liebel do { 317625de1d4SDan Handley result = io_read(backend_handle, 318625de1d4SDan Handley (uintptr_t)¤t_file.entry, 319561cd33eSHarry Liebel sizeof(current_file.entry), 320561cd33eSHarry Liebel &bytes_read); 321e098e244SJuan Castillo if (result == 0) { 322561cd33eSHarry Liebel if (compare_uuids(¤t_file.entry.uuid, 32316948ae1SJuan Castillo &uuid_spec->uuid) == 0) { 324561cd33eSHarry Liebel found_file = 1; 325561cd33eSHarry Liebel break; 326561cd33eSHarry Liebel } 327561cd33eSHarry Liebel } else { 32808c28d53SJeenu Viswambharan WARN("Failed to read FIP (%i)\n", result); 329561cd33eSHarry Liebel goto fip_file_open_close; 330561cd33eSHarry Liebel } 331561cd33eSHarry Liebel } while (compare_uuids(¤t_file.entry.uuid, &uuid_null) != 0); 332561cd33eSHarry Liebel 333561cd33eSHarry Liebel if (found_file == 1) { 334561cd33eSHarry Liebel /* All fine. Update entity info with file state and return. Set 335561cd33eSHarry Liebel * the file position to 0. The 'current_file.entry' holds the 336561cd33eSHarry Liebel * base and size of the file. 337561cd33eSHarry Liebel */ 338561cd33eSHarry Liebel current_file.file_pos = 0; 339561cd33eSHarry Liebel entity->info = (uintptr_t)¤t_file; 340561cd33eSHarry Liebel } else { 341561cd33eSHarry Liebel /* Did not find the file in the FIP. */ 342dd3dc32fSJeenu Viswambharan current_file.entry.offset_address = 0; 343e098e244SJuan Castillo result = -ENOENT; 344561cd33eSHarry Liebel } 345561cd33eSHarry Liebel 346561cd33eSHarry Liebel fip_file_open_close: 347561cd33eSHarry Liebel io_close(backend_handle); 348561cd33eSHarry Liebel 349561cd33eSHarry Liebel fip_file_open_exit: 350561cd33eSHarry Liebel return result; 351561cd33eSHarry Liebel } 352561cd33eSHarry Liebel 353561cd33eSHarry Liebel 354561cd33eSHarry Liebel /* Return the size of a file in package */ 355fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length) 356561cd33eSHarry Liebel { 357561cd33eSHarry Liebel assert(entity != NULL); 358561cd33eSHarry Liebel assert(length != NULL); 359561cd33eSHarry Liebel 360fb037bfbSDan Handley *length = ((file_state_t *)entity->info)->entry.size; 361561cd33eSHarry Liebel 362e098e244SJuan Castillo return 0; 363561cd33eSHarry Liebel } 364561cd33eSHarry Liebel 365561cd33eSHarry Liebel 366561cd33eSHarry Liebel /* Read data from a file in package */ 367625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, 368561cd33eSHarry Liebel size_t *length_read) 369561cd33eSHarry Liebel { 370e098e244SJuan Castillo int result; 371fb037bfbSDan Handley file_state_t *fp; 372561cd33eSHarry Liebel size_t file_offset; 373561cd33eSHarry Liebel size_t bytes_read; 374625de1d4SDan Handley uintptr_t backend_handle; 375561cd33eSHarry Liebel 376561cd33eSHarry Liebel assert(entity != NULL); 377561cd33eSHarry Liebel assert(length_read != NULL); 378625de1d4SDan Handley assert(entity->info != (uintptr_t)NULL); 379561cd33eSHarry Liebel 380561cd33eSHarry Liebel /* Open the backend, attempt to access the blob image */ 381561cd33eSHarry Liebel result = io_open(backend_dev_handle, backend_image_spec, 382561cd33eSHarry Liebel &backend_handle); 383e098e244SJuan Castillo if (result != 0) { 38408c28d53SJeenu Viswambharan WARN("Failed to open FIP (%i)\n", result); 385e098e244SJuan Castillo result = -ENOENT; 386561cd33eSHarry Liebel goto fip_file_read_exit; 387561cd33eSHarry Liebel } 388561cd33eSHarry Liebel 389fb037bfbSDan Handley fp = (file_state_t *)entity->info; 390561cd33eSHarry Liebel 391561cd33eSHarry Liebel /* Seek to the position in the FIP where the payload lives */ 392561cd33eSHarry Liebel file_offset = fp->entry.offset_address + fp->file_pos; 393561cd33eSHarry Liebel result = io_seek(backend_handle, IO_SEEK_SET, file_offset); 394e098e244SJuan Castillo if (result != 0) { 39508c28d53SJeenu Viswambharan WARN("fip_file_read: failed to seek\n"); 396e098e244SJuan Castillo result = -ENOENT; 397561cd33eSHarry Liebel goto fip_file_read_close; 398561cd33eSHarry Liebel } 399561cd33eSHarry Liebel 400561cd33eSHarry Liebel result = io_read(backend_handle, buffer, length, &bytes_read); 401e098e244SJuan Castillo if (result != 0) { 402561cd33eSHarry Liebel /* We cannot read our data. Fail. */ 40308c28d53SJeenu Viswambharan WARN("Failed to read payload (%i)\n", result); 404e098e244SJuan Castillo result = -ENOENT; 405561cd33eSHarry Liebel goto fip_file_read_close; 406561cd33eSHarry Liebel } else { 407561cd33eSHarry Liebel /* Set caller length and new file position. */ 408561cd33eSHarry Liebel *length_read = bytes_read; 409561cd33eSHarry Liebel fp->file_pos += bytes_read; 410561cd33eSHarry Liebel } 411561cd33eSHarry Liebel 412561cd33eSHarry Liebel /* Close the backend. */ 413561cd33eSHarry Liebel fip_file_read_close: 414561cd33eSHarry Liebel io_close(backend_handle); 415561cd33eSHarry Liebel 416561cd33eSHarry Liebel fip_file_read_exit: 417561cd33eSHarry Liebel return result; 418561cd33eSHarry Liebel } 419561cd33eSHarry Liebel 420561cd33eSHarry Liebel 421561cd33eSHarry Liebel /* Close a file in package */ 422fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity) 423561cd33eSHarry Liebel { 424561cd33eSHarry Liebel /* Clear our current file pointer. 425561cd33eSHarry Liebel * If we had malloc() we would free() here. 426561cd33eSHarry Liebel */ 427561cd33eSHarry Liebel if (current_file.entry.offset_address != 0) { 42832f0d3c6SDouglas Raillard zeromem(¤t_file, sizeof(current_file)); 429561cd33eSHarry Liebel } 430561cd33eSHarry Liebel 431561cd33eSHarry Liebel /* Clear the Entity info. */ 432561cd33eSHarry Liebel entity->info = 0; 433561cd33eSHarry Liebel 434e098e244SJuan Castillo return 0; 435561cd33eSHarry Liebel } 436561cd33eSHarry Liebel 437561cd33eSHarry Liebel /* Exported functions */ 438561cd33eSHarry Liebel 439561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */ 440625de1d4SDan Handley int register_io_dev_fip(const io_dev_connector_t **dev_con) 441561cd33eSHarry Liebel { 442e098e244SJuan Castillo int result; 443561cd33eSHarry Liebel assert(dev_con != NULL); 444561cd33eSHarry Liebel 445d4d598e9SRuchika Gupta /* 446d4d598e9SRuchika Gupta * Since dev_info isn't really used in io_register_device, always 447d4d598e9SRuchika Gupta * use the same device info at here instead. 448d4d598e9SRuchika Gupta */ 449d4d598e9SRuchika Gupta result = io_register_device(&dev_info_pool[0]); 450e098e244SJuan Castillo if (result == 0) 451561cd33eSHarry Liebel *dev_con = &fip_dev_connector; 452561cd33eSHarry Liebel 453561cd33eSHarry Liebel return result; 454561cd33eSHarry Liebel } 455