Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <debug.h> |
Antonio Nino Diaz | 09a00ef | 2019-01-11 13:12:58 +0000 | [diff] [blame^] | 9 | #include <drivers/io/io_driver.h> |
| 10 | #include <drivers/io/io_fip.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | #include <firmware_image_package.h> |
| 13 | #include <image_loader.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 14 | #include <io_storage.h> |
| 15 | #include <platform.h> |
| 16 | #include <platform_def.h> |
| 17 | #include <stdint.h> |
| 18 | #include <string.h> |
| 19 | #include <uuid.h> |
| 20 | #include <uuid_utils.h> |
| 21 | |
| 22 | |
| 23 | typedef struct { |
| 24 | unsigned int file_pos; |
| 25 | fip_toc_entry_t entry; |
| 26 | } file_state_t; |
| 27 | |
| 28 | static file_state_t current_file = {0}; |
| 29 | static uintptr_t backend_dev_handle; |
| 30 | static uintptr_t backend_image_spec; |
| 31 | |
| 32 | |
| 33 | /* Firmware Image Package driver functions */ |
| 34 | static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info); |
| 35 | static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, |
| 36 | io_entity_t *entity); |
| 37 | static int fip_file_len(io_entity_t *entity, size_t *length); |
| 38 | static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, |
| 39 | size_t *length_read); |
| 40 | static int fip_file_close(io_entity_t *entity); |
| 41 | static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params); |
| 42 | static int fip_dev_close(io_dev_info_t *dev_info); |
| 43 | |
| 44 | |
| 45 | /* TODO: We could check version numbers or do a package checksum? */ |
| 46 | static inline int is_valid_header(fip_toc_header_t *header) |
| 47 | { |
| 48 | if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) { |
| 49 | return 1; |
| 50 | } else { |
| 51 | return 0; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* Identify the device type as a virtual driver */ |
| 57 | io_type_t device_type_fip(void) |
| 58 | { |
| 59 | return IO_TYPE_FIRMWARE_IMAGE_PACKAGE; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static const io_dev_connector_t fip_dev_connector = { |
| 64 | .dev_open = fip_dev_open |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | static const io_dev_funcs_t fip_dev_funcs = { |
| 69 | .type = device_type_fip, |
| 70 | .open = fip_file_open, |
| 71 | .seek = NULL, |
| 72 | .size = fip_file_len, |
| 73 | .read = fip_file_read, |
| 74 | .write = NULL, |
| 75 | .close = fip_file_close, |
| 76 | .dev_init = fip_dev_init, |
| 77 | .dev_close = fip_dev_close, |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | /* No state associated with this device so structure can be const */ |
| 82 | static const io_dev_info_t fip_dev_info = { |
| 83 | .funcs = &fip_dev_funcs, |
| 84 | .info = (uintptr_t)NULL |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | /* Open a connection to the FIP device */ |
| 89 | static int fip_dev_open(const uintptr_t dev_spec __attribute__((unused)), |
| 90 | io_dev_info_t **dev_info) |
| 91 | { |
| 92 | assert(dev_info != NULL); |
| 93 | *dev_info = (io_dev_info_t *)&fip_dev_info; /* cast away const */ |
| 94 | |
| 95 | return IO_SUCCESS; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /* Do some basic package checks. */ |
| 100 | static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params) |
| 101 | { |
| 102 | int result = IO_FAIL; |
| 103 | unsigned int image_id = (unsigned int)init_params; |
| 104 | uintptr_t backend_handle; |
| 105 | fip_toc_header_t header; |
| 106 | size_t bytes_read; |
| 107 | |
| 108 | /* Obtain a reference to the image by querying the platform layer */ |
| 109 | result = plat_get_image_source(image_id, &backend_dev_handle, |
| 110 | &backend_image_spec); |
| 111 | if (result != IO_SUCCESS) { |
| 112 | WARN("Failed to obtain reference to image id=%u (%i)\n", |
| 113 | image_id, result); |
| 114 | result = IO_FAIL; |
| 115 | goto fip_dev_init_exit; |
| 116 | } |
| 117 | |
| 118 | /* Attempt to access the FIP image */ |
| 119 | result = io_open(backend_dev_handle, backend_image_spec, |
| 120 | &backend_handle); |
| 121 | if (result != IO_SUCCESS) { |
| 122 | WARN("Failed to access image id=%u (%i)\n", image_id, result); |
| 123 | result = IO_FAIL; |
| 124 | goto fip_dev_init_exit; |
| 125 | } |
| 126 | |
| 127 | result = io_read(backend_handle, (uintptr_t)&header, sizeof(header), |
| 128 | &bytes_read); |
| 129 | if (result == IO_SUCCESS) { |
| 130 | if (!is_valid_header(&header)) { |
| 131 | WARN("Firmware Image Package header check failed.\n"); |
| 132 | result = IO_FAIL; |
| 133 | } else { |
| 134 | VERBOSE("FIP header looks OK.\n"); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | io_close(backend_handle); |
| 139 | |
| 140 | fip_dev_init_exit: |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | /* Close a connection to the FIP device */ |
| 145 | static int fip_dev_close(io_dev_info_t *dev_info) |
| 146 | { |
| 147 | /* TODO: Consider tracking open files and cleaning them up here */ |
| 148 | |
| 149 | /* Clear the backend. */ |
| 150 | backend_dev_handle = (uintptr_t)NULL; |
| 151 | backend_image_spec = (uintptr_t)NULL; |
| 152 | |
| 153 | return IO_SUCCESS; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /* Open a file for access from package. */ |
| 158 | static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, |
| 159 | io_entity_t *entity) |
| 160 | { |
| 161 | int result = IO_FAIL; |
| 162 | uintptr_t backend_handle; |
| 163 | const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec; |
| 164 | size_t bytes_read; |
| 165 | int found_file = 0; |
| 166 | |
| 167 | assert(uuid_spec != NULL); |
| 168 | assert(entity != NULL); |
| 169 | |
| 170 | /* Can only have one file open at a time for the moment. We need to |
| 171 | * track state like file cursor position. We know the header lives at |
| 172 | * offset zero, so this entry should never be zero for an active file. |
| 173 | * When the system supports dynamic memory allocation we can allow more |
| 174 | * than one open file at a time if needed. |
| 175 | */ |
| 176 | if (current_file.entry.offset_address != 0) { |
| 177 | WARN("fip_file_open : Only one open file at a time.\n"); |
| 178 | return IO_RESOURCES_EXHAUSTED; |
| 179 | } |
| 180 | |
| 181 | /* Attempt to access the FIP image */ |
| 182 | result = io_open(backend_dev_handle, backend_image_spec, |
| 183 | &backend_handle); |
| 184 | if (result != IO_SUCCESS) { |
| 185 | WARN("Failed to open Firmware Image Package (%i)\n", result); |
| 186 | result = IO_FAIL; |
| 187 | goto fip_file_open_exit; |
| 188 | } |
| 189 | |
| 190 | /* Seek past the FIP header into the Table of Contents */ |
| 191 | result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t)); |
| 192 | if (result != IO_SUCCESS) { |
| 193 | WARN("fip_file_open: failed to seek\n"); |
| 194 | result = IO_FAIL; |
| 195 | goto fip_file_open_close; |
| 196 | } |
| 197 | |
| 198 | found_file = 0; |
| 199 | do { |
| 200 | result = io_read(backend_handle, |
| 201 | (uintptr_t)¤t_file.entry, |
| 202 | sizeof(current_file.entry), |
| 203 | &bytes_read); |
| 204 | if (result == IO_SUCCESS) { |
| 205 | if (uuid_equal(¤t_file.entry.uuid, |
| 206 | &uuid_spec->uuid)) { |
| 207 | found_file = 1; |
| 208 | break; |
| 209 | } |
| 210 | } else { |
| 211 | WARN("Failed to read FIP (%i)\n", result); |
| 212 | goto fip_file_open_close; |
| 213 | } |
| 214 | } while (!is_uuid_null(¤t_file.entry.uuid)); |
| 215 | |
| 216 | if (found_file == 1) { |
| 217 | /* All fine. Update entity info with file state and return. Set |
| 218 | * the file position to 0. The 'current_file.entry' holds the |
| 219 | * base and size of the file. |
| 220 | */ |
| 221 | current_file.file_pos = 0; |
| 222 | entity->info = (uintptr_t)¤t_file; |
| 223 | } else { |
| 224 | /* Did not find the file in the FIP. */ |
| 225 | current_file.entry.offset_address = 0; |
| 226 | result = IO_FAIL; |
| 227 | } |
| 228 | |
| 229 | fip_file_open_close: |
| 230 | io_close(backend_handle); |
| 231 | |
| 232 | fip_file_open_exit: |
| 233 | return result; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /* Return the size of a file in package */ |
| 238 | static int fip_file_len(io_entity_t *entity, size_t *length) |
| 239 | { |
| 240 | assert(entity != NULL); |
| 241 | assert(length != NULL); |
| 242 | |
| 243 | *length = ((file_state_t *)entity->info)->entry.size; |
| 244 | |
| 245 | return IO_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /* Read data from a file in package */ |
| 250 | static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, |
| 251 | size_t *length_read) |
| 252 | { |
| 253 | int result = IO_FAIL; |
| 254 | file_state_t *fp; |
| 255 | size_t file_offset; |
| 256 | size_t bytes_read; |
| 257 | uintptr_t backend_handle; |
| 258 | |
| 259 | assert(entity != NULL); |
| 260 | assert(buffer != (uintptr_t)NULL); |
| 261 | assert(length_read != NULL); |
| 262 | assert(entity->info != (uintptr_t)NULL); |
| 263 | |
| 264 | /* Open the backend, attempt to access the blob image */ |
| 265 | result = io_open(backend_dev_handle, backend_image_spec, |
| 266 | &backend_handle); |
| 267 | if (result != IO_SUCCESS) { |
| 268 | WARN("Failed to open FIP (%i)\n", result); |
| 269 | result = IO_FAIL; |
| 270 | goto fip_file_read_exit; |
| 271 | } |
| 272 | |
| 273 | fp = (file_state_t *)entity->info; |
| 274 | |
| 275 | /* Seek to the position in the FIP where the payload lives */ |
| 276 | file_offset = fp->entry.offset_address + fp->file_pos; |
| 277 | result = io_seek(backend_handle, IO_SEEK_SET, file_offset); |
| 278 | if (result != IO_SUCCESS) { |
| 279 | WARN("fip_file_read: failed to seek\n"); |
| 280 | result = IO_FAIL; |
| 281 | goto fip_file_read_close; |
| 282 | } |
| 283 | |
| 284 | result = io_read(backend_handle, buffer, length, &bytes_read); |
| 285 | if (result != IO_SUCCESS) { |
| 286 | /* We cannot read our data. Fail. */ |
| 287 | WARN("Failed to read payload (%i)\n", result); |
| 288 | result = IO_FAIL; |
| 289 | goto fip_file_read_close; |
| 290 | } else { |
| 291 | /* Set caller length and new file position. */ |
| 292 | *length_read = bytes_read; |
| 293 | fp->file_pos += bytes_read; |
| 294 | } |
| 295 | |
| 296 | /* Close the backend. */ |
| 297 | fip_file_read_close: |
| 298 | io_close(backend_handle); |
| 299 | |
| 300 | fip_file_read_exit: |
| 301 | return result; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | /* Close a file in package */ |
| 306 | static int fip_file_close(io_entity_t *entity) |
| 307 | { |
| 308 | /* Clear our current file pointer. |
| 309 | * If we had malloc() we would free() here. |
| 310 | */ |
| 311 | if (current_file.entry.offset_address != 0) { |
| 312 | memset(¤t_file, 0, sizeof(current_file)); |
| 313 | } |
| 314 | |
| 315 | /* Clear the Entity info. */ |
| 316 | entity->info = 0; |
| 317 | |
| 318 | return IO_SUCCESS; |
| 319 | } |
| 320 | |
| 321 | /* Exported functions */ |
| 322 | |
| 323 | /* Register the Firmware Image Package driver with the IO abstraction */ |
| 324 | int register_io_dev_fip(const io_dev_connector_t **dev_con) |
| 325 | { |
| 326 | int result = IO_FAIL; |
| 327 | assert(dev_con != NULL); |
| 328 | |
| 329 | result = io_register_device(&fip_dev_info); |
| 330 | if (result == IO_SUCCESS) |
| 331 | *dev_con = &fip_dev_connector; |
| 332 | |
| 333 | return result; |
| 334 | } |