Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 1 | /* |
| 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 Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 31 | #include <assert.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 32 | #include <debug.h> |
| 33 | #include <errno.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 34 | #include <firmware_image_package.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 35 | #include <io_driver.h> |
| 36 | #include <io_fip.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 37 | #include <io_storage.h> |
| 38 | #include <platform.h> |
| 39 | #include <stdint.h> |
| 40 | #include <string.h> |
| 41 | #include <uuid.h> |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 42 | |
| 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 | |
| 51 | typedef struct { |
| 52 | const char *name; |
| 53 | const uuid_t uuid; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 54 | } plat_fip_name_uuid_t; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 55 | |
| 56 | typedef 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 Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 62 | fip_toc_entry_t entry; |
| 63 | } file_state_t; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 64 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 65 | static const plat_fip_name_uuid_t name_uuid[] = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 66 | {BL2_IMAGE_NAME, UUID_TRUSTED_BOOT_FIRMWARE_BL2}, |
| 67 | {BL31_IMAGE_NAME, UUID_EL3_RUNTIME_FIRMWARE_BL31}, |
Dan Handley | 21a30ab | 2014-04-15 11:38:38 +0100 | [diff] [blame] | 68 | #ifdef BL32_IMAGE_NAME |
| 69 | /* BL3-2 is optional in the platform */ |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 70 | {BL32_IMAGE_NAME, UUID_SECURE_PAYLOAD_BL32}, |
Dan Handley | 21a30ab | 2014-04-15 11:38:38 +0100 | [diff] [blame] | 71 | #endif /* BL32_IMAGE_NAME */ |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 72 | {BL33_IMAGE_NAME, UUID_NON_TRUSTED_FIRMWARE_BL33}, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static const uuid_t uuid_null = {0}; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 76 | static file_state_t current_file = {0}; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 77 | static uintptr_t backend_dev_handle; |
| 78 | static uintptr_t backend_image_spec; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | /* Firmware Image Package driver functions */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 82 | static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); |
| 83 | static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 84 | io_entity_t *entity); |
| 85 | static int fip_file_len(io_entity_t *entity, size_t *length); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 86 | static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 87 | size_t *length_read); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 88 | static int fip_file_close(io_entity_t *entity); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 89 | static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 90 | static int fip_dev_close(io_dev_info_t *dev_info); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | static inline int copy_uuid(uuid_t *dst, const uuid_t *src) |
| 94 | { |
| 95 | memcpy(dst, src, sizeof(uuid_t)); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /* Return 0 for equal uuids. */ |
| 101 | static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2) |
| 102 | { |
| 103 | return memcmp(uuid1, uuid2, sizeof(uuid_t)); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* TODO: We could check version numbers or do a package checksum? */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 108 | static inline int is_valid_header(fip_toc_header_t *header) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 109 | { |
| 110 | if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) { |
| 111 | return 1; |
| 112 | } else { |
| 113 | return 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | static int file_to_uuid(const char *filename, uuid_t *uuid) |
| 119 | { |
| 120 | int i; |
| 121 | int status = -EINVAL; |
| 122 | |
Sandrine Bailleux | 0c51e07 | 2014-03-19 16:03:48 +0000 | [diff] [blame] | 123 | for (i = 0; i < (sizeof(name_uuid) / sizeof(name_uuid[0])); i++) { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 124 | if (strcmp(filename, name_uuid[i].name) == 0) { |
| 125 | copy_uuid(uuid, &name_uuid[i].uuid); |
| 126 | status = 0; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | return status; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* Identify the device type as a virtual driver */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 135 | io_type_t device_type_fip(void) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 136 | { |
| 137 | return IO_TYPE_FIRMWARE_IMAGE_PACKAGE; |
| 138 | } |
| 139 | |
| 140 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 141 | static const io_dev_connector_t fip_dev_connector = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 142 | .dev_open = fip_dev_open |
| 143 | }; |
| 144 | |
| 145 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 146 | static const io_dev_funcs_t fip_dev_funcs = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 147 | .type = device_type_fip, |
| 148 | .open = fip_file_open, |
| 149 | .seek = NULL, |
| 150 | .size = fip_file_len, |
| 151 | .read = fip_file_read, |
| 152 | .write = NULL, |
| 153 | .close = fip_file_close, |
| 154 | .dev_init = fip_dev_init, |
| 155 | .dev_close = fip_dev_close, |
| 156 | }; |
| 157 | |
| 158 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 159 | /* No state associated with this device so structure can be const */ |
| 160 | static const io_dev_info_t fip_dev_info = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 161 | .funcs = &fip_dev_funcs, |
| 162 | .info = (uintptr_t)NULL |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | /* Open a connection to the FIP device */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 167 | static int fip_dev_open(const uintptr_t dev_spec __attribute__((unused)), |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 168 | io_dev_info_t **dev_info) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 169 | { |
| 170 | assert(dev_info != NULL); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 171 | *dev_info = (io_dev_info_t *)&fip_dev_info; /* cast away const */ |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 172 | |
| 173 | return IO_SUCCESS; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /* Do some basic package checks. */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 178 | static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 179 | { |
| 180 | int result = IO_FAIL; |
| 181 | char *image_name = (char *)init_params; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 182 | uintptr_t backend_handle; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 183 | fip_toc_header_t header; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 184 | size_t bytes_read; |
| 185 | |
| 186 | /* Obtain a reference to the image by querying the platform layer */ |
| 187 | result = plat_get_image_source(image_name, &backend_dev_handle, |
| 188 | &backend_image_spec); |
| 189 | if (result != IO_SUCCESS) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 190 | WARN("Failed to obtain reference to image '%s' (%i)\n", |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 191 | image_name, result); |
| 192 | result = IO_FAIL; |
| 193 | goto fip_dev_init_exit; |
| 194 | } |
| 195 | |
| 196 | /* Attempt to access the FIP image */ |
| 197 | result = io_open(backend_dev_handle, backend_image_spec, |
| 198 | &backend_handle); |
| 199 | if (result != IO_SUCCESS) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 200 | WARN("Failed to access image '%s' (%i)\n", image_name, result); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 201 | result = IO_FAIL; |
| 202 | goto fip_dev_init_exit; |
| 203 | } |
| 204 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 205 | result = io_read(backend_handle, (uintptr_t)&header, sizeof(header), |
| 206 | &bytes_read); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 207 | if (result == IO_SUCCESS) { |
| 208 | if (!is_valid_header(&header)) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 209 | WARN("Firmware Image Package header check failed.\n"); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 210 | result = IO_FAIL; |
| 211 | } else { |
| 212 | INFO("FIP header looks OK.\n"); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | io_close(backend_handle); |
| 217 | |
| 218 | fip_dev_init_exit: |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | /* Close a connection to the FIP device */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 223 | static int fip_dev_close(io_dev_info_t *dev_info) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 224 | { |
| 225 | /* TODO: Consider tracking open files and cleaning them up here */ |
| 226 | |
| 227 | /* Clear the backend. */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 228 | backend_dev_handle = (uintptr_t)NULL; |
| 229 | backend_image_spec = (uintptr_t)NULL; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 230 | |
| 231 | return IO_SUCCESS; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* Open a file for access from package. */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 236 | static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 237 | io_entity_t *entity) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 238 | { |
| 239 | int result = IO_FAIL; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 240 | uintptr_t backend_handle; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 241 | uuid_t file_uuid; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 242 | const io_file_spec_t *file_spec = (io_file_spec_t *)spec; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 243 | size_t bytes_read; |
| 244 | int found_file = 0; |
| 245 | |
| 246 | assert(file_spec != NULL); |
| 247 | assert(entity != NULL); |
| 248 | |
| 249 | /* Can only have one file open at a time for the moment. We need to |
| 250 | * track state like file cursor position. We know the header lives at |
| 251 | * offset zero, so this entry should never be zero for an active file. |
| 252 | * When the system supports dynamic memory allocation we can allow more |
| 253 | * than one open file at a time if needed. |
| 254 | */ |
| 255 | if (current_file.entry.offset_address != 0) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 256 | WARN("fip_file_open : Only one open file at a time.\n"); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 257 | return IO_RESOURCES_EXHAUSTED; |
| 258 | } |
| 259 | |
| 260 | /* Attempt to access the FIP image */ |
| 261 | result = io_open(backend_dev_handle, backend_image_spec, |
| 262 | &backend_handle); |
| 263 | if (result != IO_SUCCESS) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 264 | WARN("Failed to open Firmware Image Package (%i)\n", result); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 265 | result = IO_FAIL; |
| 266 | goto fip_file_open_exit; |
| 267 | } |
| 268 | |
| 269 | /* Seek past the FIP header into the Table of Contents */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 270 | result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t)); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 271 | if (result != IO_SUCCESS) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 272 | WARN("fip_file_open: failed to seek\n"); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 273 | result = IO_FAIL; |
| 274 | goto fip_file_open_close; |
| 275 | } |
| 276 | |
| 277 | file_to_uuid(file_spec->path, &file_uuid); |
| 278 | |
| 279 | found_file = 0; |
| 280 | do { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 281 | result = io_read(backend_handle, |
| 282 | (uintptr_t)¤t_file.entry, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 283 | sizeof(current_file.entry), |
| 284 | &bytes_read); |
| 285 | if (result == IO_SUCCESS) { |
| 286 | if (compare_uuids(¤t_file.entry.uuid, |
| 287 | &file_uuid) == 0) { |
| 288 | found_file = 1; |
| 289 | break; |
| 290 | } |
| 291 | } else { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 292 | WARN("Failed to read FIP (%i)\n", result); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 293 | goto fip_file_open_close; |
| 294 | } |
| 295 | } while (compare_uuids(¤t_file.entry.uuid, &uuid_null) != 0); |
| 296 | |
| 297 | if (found_file == 1) { |
| 298 | /* All fine. Update entity info with file state and return. Set |
| 299 | * the file position to 0. The 'current_file.entry' holds the |
| 300 | * base and size of the file. |
| 301 | */ |
| 302 | current_file.file_pos = 0; |
| 303 | entity->info = (uintptr_t)¤t_file; |
| 304 | } else { |
| 305 | /* Did not find the file in the FIP. */ |
Jeenu Viswambharan | dd3dc32 | 2014-02-20 11:51:00 +0000 | [diff] [blame] | 306 | current_file.entry.offset_address = 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 307 | result = IO_FAIL; |
| 308 | } |
| 309 | |
| 310 | fip_file_open_close: |
| 311 | io_close(backend_handle); |
| 312 | |
| 313 | fip_file_open_exit: |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /* Return the size of a file in package */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 319 | static int fip_file_len(io_entity_t *entity, size_t *length) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 320 | { |
| 321 | assert(entity != NULL); |
| 322 | assert(length != NULL); |
| 323 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 324 | *length = ((file_state_t *)entity->info)->entry.size; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 325 | |
| 326 | return IO_SUCCESS; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | /* Read data from a file in package */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 331 | static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 332 | size_t *length_read) |
| 333 | { |
| 334 | int result = IO_FAIL; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 335 | file_state_t *fp; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 336 | size_t file_offset; |
| 337 | size_t bytes_read; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 338 | uintptr_t backend_handle; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 339 | |
| 340 | assert(entity != NULL); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 341 | assert(buffer != (uintptr_t)NULL); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 342 | assert(length_read != NULL); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 343 | assert(entity->info != (uintptr_t)NULL); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 344 | |
| 345 | /* Open the backend, attempt to access the blob image */ |
| 346 | result = io_open(backend_dev_handle, backend_image_spec, |
| 347 | &backend_handle); |
| 348 | if (result != IO_SUCCESS) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 349 | WARN("Failed to open FIP (%i)\n", result); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 350 | result = IO_FAIL; |
| 351 | goto fip_file_read_exit; |
| 352 | } |
| 353 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 354 | fp = (file_state_t *)entity->info; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 355 | |
| 356 | /* Seek to the position in the FIP where the payload lives */ |
| 357 | file_offset = fp->entry.offset_address + fp->file_pos; |
| 358 | result = io_seek(backend_handle, IO_SEEK_SET, file_offset); |
| 359 | if (result != IO_SUCCESS) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 360 | WARN("fip_file_read: failed to seek\n"); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 361 | result = IO_FAIL; |
| 362 | goto fip_file_read_close; |
| 363 | } |
| 364 | |
| 365 | result = io_read(backend_handle, buffer, length, &bytes_read); |
| 366 | if (result != IO_SUCCESS) { |
| 367 | /* We cannot read our data. Fail. */ |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 368 | WARN("Failed to read payload (%i)\n", result); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 369 | result = IO_FAIL; |
| 370 | goto fip_file_read_close; |
| 371 | } else { |
| 372 | /* Set caller length and new file position. */ |
| 373 | *length_read = bytes_read; |
| 374 | fp->file_pos += bytes_read; |
| 375 | } |
| 376 | |
| 377 | /* Close the backend. */ |
| 378 | fip_file_read_close: |
| 379 | io_close(backend_handle); |
| 380 | |
| 381 | fip_file_read_exit: |
| 382 | return result; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | /* Close a file in package */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 387 | static int fip_file_close(io_entity_t *entity) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 388 | { |
| 389 | /* Clear our current file pointer. |
| 390 | * If we had malloc() we would free() here. |
| 391 | */ |
| 392 | if (current_file.entry.offset_address != 0) { |
| 393 | memset(¤t_file, 0, sizeof(current_file)); |
| 394 | } |
| 395 | |
| 396 | /* Clear the Entity info. */ |
| 397 | entity->info = 0; |
| 398 | |
| 399 | return IO_SUCCESS; |
| 400 | } |
| 401 | |
| 402 | /* Exported functions */ |
| 403 | |
| 404 | /* Register the Firmware Image Package driver with the IO abstraction */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 405 | int register_io_dev_fip(const io_dev_connector_t **dev_con) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 406 | { |
| 407 | int result = IO_FAIL; |
| 408 | assert(dev_con != NULL); |
| 409 | |
| 410 | result = io_register_device(&fip_dev_info); |
| 411 | if (result == IO_SUCCESS) |
| 412 | *dev_con = &fip_dev_connector; |
| 413 | |
| 414 | return result; |
| 415 | } |