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