Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 5f73afb | 2018-02-14 11:41:26 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 7 | #include <assert.h> |
Dan Handley | ed6ff95 | 2014-05-14 17:44:19 +0100 | [diff] [blame] | 8 | #include <bl_common.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 9 | #include <debug.h> |
| 10 | #include <errno.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 11 | #include <firmware_image_package.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 12 | #include <io_driver.h> |
| 13 | #include <io_fip.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 14 | #include <io_storage.h> |
| 15 | #include <platform.h> |
Dan Handley | ed6ff95 | 2014-05-14 17:44:19 +0100 | [diff] [blame] | 16 | #include <platform_def.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | #include <string.h> |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 19 | #include <utils.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 20 | #include <uuid.h> |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 21 | |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 22 | #ifndef MAX_FIP_DEVICES |
| 23 | #define MAX_FIP_DEVICES 1 |
| 24 | #endif |
| 25 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 26 | /* Useful for printing UUIDs when debugging.*/ |
| 27 | #define PRINT_UUID2(x) \ |
| 28 | "%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", \ |
| 29 | x.time_low, x.time_mid, x.time_hi_and_version, \ |
| 30 | x.clock_seq_hi_and_reserved, x.clock_seq_low, \ |
| 31 | x.node[0], x.node[1], x.node[2], x.node[3], \ |
| 32 | x.node[4], x.node[5] |
| 33 | |
| 34 | typedef struct { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 35 | unsigned int file_pos; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 36 | fip_toc_entry_t entry; |
| 37 | } file_state_t; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 38 | |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 39 | /* |
| 40 | * Maintain dev_spec per FIP Device |
| 41 | * TODO - Add backend handles and file state |
| 42 | * per FIP device here once backends like io_memmap |
| 43 | * can support multiple open files |
| 44 | */ |
| 45 | typedef struct { |
| 46 | uintptr_t dev_spec; |
| 47 | } fip_dev_state_t; |
| 48 | |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 49 | static const uuid_t uuid_null = { {0} }; |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 50 | /* |
| 51 | * Only one file can be open across all FIP device |
| 52 | * as backends like io_memmap don't support |
| 53 | * multiple open files. The file state and |
| 54 | * backend handle should be maintained per FIP device |
| 55 | * if the same support is available in the backend |
| 56 | */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 57 | static file_state_t current_file = {0}; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 58 | static uintptr_t backend_dev_handle; |
| 59 | static uintptr_t backend_image_spec; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 60 | |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 61 | static fip_dev_state_t state_pool[MAX_FIP_DEVICES]; |
| 62 | static io_dev_info_t dev_info_pool[MAX_FIP_DEVICES]; |
| 63 | |
| 64 | /* Track number of allocated fip devices */ |
| 65 | static unsigned int fip_dev_count; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 66 | |
| 67 | /* Firmware Image Package driver functions */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 68 | static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); |
| 69 | 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] | 70 | io_entity_t *entity); |
| 71 | static int fip_file_len(io_entity_t *entity, size_t *length); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 72 | 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] | 73 | size_t *length_read); |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 74 | static int fip_file_close(io_entity_t *entity); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 75 | 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] | 76 | static int fip_dev_close(io_dev_info_t *dev_info); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 77 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 78 | |
| 79 | /* Return 0 for equal uuids. */ |
| 80 | static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2) |
| 81 | { |
| 82 | return memcmp(uuid1, uuid2, sizeof(uuid_t)); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* TODO: We could check version numbers or do a package checksum? */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 87 | static inline int is_valid_header(fip_toc_header_t *header) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 88 | { |
| 89 | if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) { |
| 90 | return 1; |
| 91 | } else { |
| 92 | return 0; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 97 | /* Identify the device type as a virtual driver */ |
Roberto Vargas | 0571270 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 98 | static io_type_t device_type_fip(void) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 99 | { |
| 100 | return IO_TYPE_FIRMWARE_IMAGE_PACKAGE; |
| 101 | } |
| 102 | |
| 103 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 104 | static const io_dev_connector_t fip_dev_connector = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 105 | .dev_open = fip_dev_open |
| 106 | }; |
| 107 | |
| 108 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 109 | static const io_dev_funcs_t fip_dev_funcs = { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 110 | .type = device_type_fip, |
| 111 | .open = fip_file_open, |
| 112 | .seek = NULL, |
| 113 | .size = fip_file_len, |
| 114 | .read = fip_file_read, |
| 115 | .write = NULL, |
| 116 | .close = fip_file_close, |
| 117 | .dev_init = fip_dev_init, |
| 118 | .dev_close = fip_dev_close, |
| 119 | }; |
| 120 | |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 121 | /* Locate a file state in the pool, specified by address */ |
| 122 | static int find_first_fip_state(const uintptr_t dev_spec, |
| 123 | unsigned int *index_out) |
| 124 | { |
| 125 | int result = -ENOENT; |
| 126 | unsigned int index; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 127 | |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 128 | for (index = 0; index < (unsigned int)MAX_FIP_DEVICES; ++index) { |
| 129 | /* dev_spec is used as identifier since it's unique */ |
| 130 | if (state_pool[index].dev_spec == dev_spec) { |
| 131 | result = 0; |
| 132 | *index_out = index; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | return result; |
| 137 | } |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 138 | |
| 139 | |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 140 | /* Allocate a device info from the pool and return a pointer to it */ |
| 141 | static int allocate_dev_info(io_dev_info_t **dev_info) |
| 142 | { |
| 143 | int result = -ENOMEM; |
| 144 | |
| 145 | assert(dev_info != NULL); |
| 146 | |
| 147 | if (fip_dev_count < (unsigned int)MAX_FIP_DEVICES) { |
| 148 | unsigned int index = 0; |
| 149 | |
| 150 | result = find_first_fip_state(0, &index); |
| 151 | assert(result == 0); |
| 152 | /* initialize dev_info */ |
| 153 | dev_info_pool[index].funcs = &fip_dev_funcs; |
| 154 | dev_info_pool[index].info = |
| 155 | (uintptr_t)&state_pool[index]; |
| 156 | *dev_info = &dev_info_pool[index]; |
| 157 | ++fip_dev_count; |
| 158 | } |
| 159 | |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | /* Release a device info to the pool */ |
| 164 | static int free_dev_info(io_dev_info_t *dev_info) |
| 165 | { |
| 166 | int result; |
| 167 | unsigned int index = 0; |
| 168 | fip_dev_state_t *state; |
| 169 | |
| 170 | assert(dev_info != NULL); |
| 171 | |
| 172 | state = (fip_dev_state_t *)dev_info->info; |
| 173 | result = find_first_fip_state(state->dev_spec, &index); |
| 174 | if (result == 0) { |
| 175 | /* free if device info is valid */ |
| 176 | zeromem(state, sizeof(fip_dev_state_t)); |
| 177 | --fip_dev_count; |
| 178 | } |
| 179 | |
| 180 | return result; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Multiple FIP devices can be opened depending on the value of |
| 185 | * MAX_FIP_DEVICES. Given that there is only one backend, only a |
| 186 | * single file can be open at a time by any FIP device. |
| 187 | */ |
| 188 | static int fip_dev_open(const uintptr_t dev_spec, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 189 | io_dev_info_t **dev_info) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 190 | { |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 191 | int result; |
| 192 | io_dev_info_t *info; |
| 193 | fip_dev_state_t *state; |
| 194 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 195 | assert(dev_info != NULL); |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 196 | #if MAX_FIP_DEVICES > 1 |
| 197 | assert(dev_spec != (uintptr_t)NULL); |
| 198 | #endif |
| 199 | |
| 200 | result = allocate_dev_info(&info); |
| 201 | if (result != 0) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | state = (fip_dev_state_t *)info->info; |
| 205 | |
| 206 | state->dev_spec = dev_spec; |
| 207 | |
| 208 | *dev_info = info; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 209 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 210 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | |
| 214 | /* Do some basic package checks. */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 215 | 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] | 216 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 217 | int result; |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 218 | unsigned int image_id = (unsigned int)init_params; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 219 | uintptr_t backend_handle; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 220 | fip_toc_header_t header; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 221 | size_t bytes_read; |
| 222 | |
| 223 | /* Obtain a reference to the image by querying the platform layer */ |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 224 | result = plat_get_image_source(image_id, &backend_dev_handle, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 225 | &backend_image_spec); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 226 | if (result != 0) { |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 227 | WARN("Failed to obtain reference to image id=%u (%i)\n", |
| 228 | image_id, result); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 229 | result = -ENOENT; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 230 | goto fip_dev_init_exit; |
| 231 | } |
| 232 | |
| 233 | /* Attempt to access the FIP image */ |
| 234 | result = io_open(backend_dev_handle, backend_image_spec, |
| 235 | &backend_handle); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 236 | if (result != 0) { |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 237 | WARN("Failed to access image id=%u (%i)\n", image_id, result); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 238 | result = -ENOENT; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 239 | goto fip_dev_init_exit; |
| 240 | } |
| 241 | |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 242 | result = io_read(backend_handle, (uintptr_t)&header, sizeof(header), |
| 243 | &bytes_read); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 244 | if (result == 0) { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 245 | if (!is_valid_header(&header)) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 246 | WARN("Firmware Image Package header check failed.\n"); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 247 | result = -ENOENT; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 248 | } else { |
Dan Handley | 91b624e | 2014-07-29 17:14:00 +0100 | [diff] [blame] | 249 | VERBOSE("FIP header looks OK.\n"); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | io_close(backend_handle); |
| 254 | |
| 255 | fip_dev_init_exit: |
| 256 | return result; |
| 257 | } |
| 258 | |
| 259 | /* Close a connection to the FIP device */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 260 | static int fip_dev_close(io_dev_info_t *dev_info) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 261 | { |
| 262 | /* TODO: Consider tracking open files and cleaning them up here */ |
| 263 | |
| 264 | /* Clear the backend. */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 265 | backend_dev_handle = (uintptr_t)NULL; |
| 266 | backend_image_spec = (uintptr_t)NULL; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 267 | |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 268 | return free_dev_info(dev_info); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | /* Open a file for access from package. */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 273 | 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] | 274 | io_entity_t *entity) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 275 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 276 | int result; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 277 | uintptr_t backend_handle; |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 278 | const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 279 | size_t bytes_read; |
| 280 | int found_file = 0; |
| 281 | |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 282 | assert(uuid_spec != NULL); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 283 | assert(entity != NULL); |
| 284 | |
| 285 | /* Can only have one file open at a time for the moment. We need to |
| 286 | * track state like file cursor position. We know the header lives at |
| 287 | * offset zero, so this entry should never be zero for an active file. |
| 288 | * When the system supports dynamic memory allocation we can allow more |
| 289 | * than one open file at a time if needed. |
| 290 | */ |
| 291 | if (current_file.entry.offset_address != 0) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 292 | WARN("fip_file_open : Only one open file at a time.\n"); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 293 | return -ENOMEM; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* Attempt to access the FIP image */ |
| 297 | result = io_open(backend_dev_handle, backend_image_spec, |
| 298 | &backend_handle); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 299 | if (result != 0) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 300 | WARN("Failed to open Firmware Image Package (%i)\n", result); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 301 | result = -ENOENT; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 302 | goto fip_file_open_exit; |
| 303 | } |
| 304 | |
| 305 | /* Seek past the FIP header into the Table of Contents */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 306 | result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t)); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 307 | if (result != 0) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 308 | WARN("fip_file_open: failed to seek\n"); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 309 | result = -ENOENT; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 310 | goto fip_file_open_close; |
| 311 | } |
| 312 | |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 313 | found_file = 0; |
| 314 | do { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 315 | result = io_read(backend_handle, |
| 316 | (uintptr_t)¤t_file.entry, |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 317 | sizeof(current_file.entry), |
| 318 | &bytes_read); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 319 | if (result == 0) { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 320 | if (compare_uuids(¤t_file.entry.uuid, |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 321 | &uuid_spec->uuid) == 0) { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 322 | found_file = 1; |
| 323 | break; |
| 324 | } |
| 325 | } else { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 326 | WARN("Failed to read FIP (%i)\n", result); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 327 | goto fip_file_open_close; |
| 328 | } |
| 329 | } while (compare_uuids(¤t_file.entry.uuid, &uuid_null) != 0); |
| 330 | |
| 331 | if (found_file == 1) { |
| 332 | /* All fine. Update entity info with file state and return. Set |
| 333 | * the file position to 0. The 'current_file.entry' holds the |
| 334 | * base and size of the file. |
| 335 | */ |
| 336 | current_file.file_pos = 0; |
| 337 | entity->info = (uintptr_t)¤t_file; |
| 338 | } else { |
| 339 | /* Did not find the file in the FIP. */ |
Jeenu Viswambharan | dd3dc32 | 2014-02-20 11:51:00 +0000 | [diff] [blame] | 340 | current_file.entry.offset_address = 0; |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 341 | result = -ENOENT; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | fip_file_open_close: |
| 345 | io_close(backend_handle); |
| 346 | |
| 347 | fip_file_open_exit: |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | /* Return the size of a file in package */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 353 | static int fip_file_len(io_entity_t *entity, size_t *length) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 354 | { |
| 355 | assert(entity != NULL); |
| 356 | assert(length != NULL); |
| 357 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 358 | *length = ((file_state_t *)entity->info)->entry.size; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 359 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 360 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
| 364 | /* Read data from a file in package */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 365 | 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] | 366 | size_t *length_read) |
| 367 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 368 | int result; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 369 | file_state_t *fp; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 370 | size_t file_offset; |
| 371 | size_t bytes_read; |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 372 | uintptr_t backend_handle; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 373 | |
| 374 | assert(entity != NULL); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 375 | assert(length_read != NULL); |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 376 | assert(entity->info != (uintptr_t)NULL); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 377 | |
| 378 | /* Open the backend, attempt to access the blob image */ |
| 379 | result = io_open(backend_dev_handle, backend_image_spec, |
| 380 | &backend_handle); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 381 | if (result != 0) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 382 | WARN("Failed to open FIP (%i)\n", result); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 383 | result = -ENOENT; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 384 | goto fip_file_read_exit; |
| 385 | } |
| 386 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 387 | fp = (file_state_t *)entity->info; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 388 | |
| 389 | /* Seek to the position in the FIP where the payload lives */ |
| 390 | file_offset = fp->entry.offset_address + fp->file_pos; |
| 391 | result = io_seek(backend_handle, IO_SEEK_SET, file_offset); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 392 | if (result != 0) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 393 | WARN("fip_file_read: failed to seek\n"); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 394 | result = -ENOENT; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 395 | goto fip_file_read_close; |
| 396 | } |
| 397 | |
| 398 | result = io_read(backend_handle, buffer, length, &bytes_read); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 399 | if (result != 0) { |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 400 | /* We cannot read our data. Fail. */ |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 401 | WARN("Failed to read payload (%i)\n", result); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 402 | result = -ENOENT; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 403 | goto fip_file_read_close; |
| 404 | } else { |
| 405 | /* Set caller length and new file position. */ |
| 406 | *length_read = bytes_read; |
| 407 | fp->file_pos += bytes_read; |
| 408 | } |
| 409 | |
| 410 | /* Close the backend. */ |
| 411 | fip_file_read_close: |
| 412 | io_close(backend_handle); |
| 413 | |
| 414 | fip_file_read_exit: |
| 415 | return result; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /* Close a file in package */ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 420 | static int fip_file_close(io_entity_t *entity) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 421 | { |
| 422 | /* Clear our current file pointer. |
| 423 | * If we had malloc() we would free() here. |
| 424 | */ |
| 425 | if (current_file.entry.offset_address != 0) { |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 426 | zeromem(¤t_file, sizeof(current_file)); |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* Clear the Entity info. */ |
| 430 | entity->info = 0; |
| 431 | |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 432 | return 0; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* Exported functions */ |
| 436 | |
| 437 | /* Register the Firmware Image Package driver with the IO abstraction */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 438 | int register_io_dev_fip(const io_dev_connector_t **dev_con) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 439 | { |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 440 | int result; |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 441 | assert(dev_con != NULL); |
| 442 | |
Ruchika Gupta | 246e45b | 2018-06-27 12:18:22 +0530 | [diff] [blame] | 443 | /* |
| 444 | * Since dev_info isn't really used in io_register_device, always |
| 445 | * use the same device info at here instead. |
| 446 | */ |
| 447 | result = io_register_device(&dev_info_pool[0]); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 448 | if (result == 0) |
Harry Liebel | 561cd33 | 2014-02-14 14:42:48 +0000 | [diff] [blame] | 449 | *dev_con = &fip_dev_connector; |
| 450 | |
| 451 | return result; |
| 452 | } |