1 /* 2 * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <bl_common.h> 9 #include <debug.h> 10 #include <errno.h> 11 #include <firmware_image_package.h> 12 #include <io_driver.h> 13 #include <io_fip.h> 14 #include <io_storage.h> 15 #include <platform.h> 16 #include <platform_def.h> 17 #include <stdint.h> 18 #include <string.h> 19 #include <utils.h> 20 #include <uuid.h> 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 { 31 unsigned int file_pos; 32 fip_toc_entry_t entry; 33 } file_state_t; 34 35 static const uuid_t uuid_null = {0}; 36 static file_state_t current_file = {0}; 37 static uintptr_t backend_dev_handle; 38 static uintptr_t backend_image_spec; 39 40 41 /* Firmware Image Package driver functions */ 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, 44 io_entity_t *entity); 45 static int fip_file_len(io_entity_t *entity, size_t *length); 46 static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, 47 size_t *length_read); 48 static int fip_file_close(io_entity_t *entity); 49 static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params); 50 static int fip_dev_close(io_dev_info_t *dev_info); 51 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? */ 61 static inline int is_valid_header(fip_toc_header_t *header) 62 { 63 if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) { 64 return 1; 65 } else { 66 return 0; 67 } 68 } 69 70 71 /* Identify the device type as a virtual driver */ 72 static io_type_t device_type_fip(void) 73 { 74 return IO_TYPE_FIRMWARE_IMAGE_PACKAGE; 75 } 76 77 78 static const io_dev_connector_t fip_dev_connector = { 79 .dev_open = fip_dev_open 80 }; 81 82 83 static const io_dev_funcs_t fip_dev_funcs = { 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 96 /* No state associated with this device so structure can be const */ 97 static const io_dev_info_t fip_dev_info = { 98 .funcs = &fip_dev_funcs, 99 .info = (uintptr_t)NULL 100 }; 101 102 103 /* Open a connection to the FIP device */ 104 static int fip_dev_open(const uintptr_t dev_spec __unused, 105 io_dev_info_t **dev_info) 106 { 107 assert(dev_info != NULL); 108 *dev_info = (io_dev_info_t *)&fip_dev_info; /* cast away const */ 109 110 return 0; 111 } 112 113 114 /* Do some basic package checks. */ 115 static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params) 116 { 117 int result; 118 unsigned int image_id = (unsigned int)init_params; 119 uintptr_t backend_handle; 120 fip_toc_header_t header; 121 size_t bytes_read; 122 123 /* Obtain a reference to the image by querying the platform layer */ 124 result = plat_get_image_source(image_id, &backend_dev_handle, 125 &backend_image_spec); 126 if (result != 0) { 127 WARN("Failed to obtain reference to image id=%u (%i)\n", 128 image_id, result); 129 result = -ENOENT; 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); 136 if (result != 0) { 137 WARN("Failed to access image id=%u (%i)\n", image_id, result); 138 result = -ENOENT; 139 goto fip_dev_init_exit; 140 } 141 142 result = io_read(backend_handle, (uintptr_t)&header, sizeof(header), 143 &bytes_read); 144 if (result == 0) { 145 if (!is_valid_header(&header)) { 146 WARN("Firmware Image Package header check failed.\n"); 147 result = -ENOENT; 148 } else { 149 VERBOSE("FIP header looks OK.\n"); 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 */ 160 static int fip_dev_close(io_dev_info_t *dev_info) 161 { 162 /* TODO: Consider tracking open files and cleaning them up here */ 163 164 /* Clear the backend. */ 165 backend_dev_handle = (uintptr_t)NULL; 166 backend_image_spec = (uintptr_t)NULL; 167 168 return 0; 169 } 170 171 172 /* Open a file for access from package. */ 173 static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec, 174 io_entity_t *entity) 175 { 176 int result; 177 uintptr_t backend_handle; 178 const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec; 179 size_t bytes_read; 180 int found_file = 0; 181 182 assert(uuid_spec != NULL); 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) { 192 WARN("fip_file_open : Only one open file at a time.\n"); 193 return -ENOMEM; 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 != 0) { 200 WARN("Failed to open Firmware Image Package (%i)\n", result); 201 result = -ENOENT; 202 goto fip_file_open_exit; 203 } 204 205 /* Seek past the FIP header into the Table of Contents */ 206 result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t)); 207 if (result != 0) { 208 WARN("fip_file_open: failed to seek\n"); 209 result = -ENOENT; 210 goto fip_file_open_close; 211 } 212 213 found_file = 0; 214 do { 215 result = io_read(backend_handle, 216 (uintptr_t)¤t_file.entry, 217 sizeof(current_file.entry), 218 &bytes_read); 219 if (result == 0) { 220 if (compare_uuids(¤t_file.entry.uuid, 221 &uuid_spec->uuid) == 0) { 222 found_file = 1; 223 break; 224 } 225 } else { 226 WARN("Failed to read FIP (%i)\n", result); 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. */ 240 current_file.entry.offset_address = 0; 241 result = -ENOENT; 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 */ 253 static int fip_file_len(io_entity_t *entity, size_t *length) 254 { 255 assert(entity != NULL); 256 assert(length != NULL); 257 258 *length = ((file_state_t *)entity->info)->entry.size; 259 260 return 0; 261 } 262 263 264 /* Read data from a file in package */ 265 static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length, 266 size_t *length_read) 267 { 268 int result; 269 file_state_t *fp; 270 size_t file_offset; 271 size_t bytes_read; 272 uintptr_t backend_handle; 273 274 assert(entity != NULL); 275 assert(buffer != (uintptr_t)NULL); 276 assert(length_read != NULL); 277 assert(entity->info != (uintptr_t)NULL); 278 279 /* Open the backend, attempt to access the blob image */ 280 result = io_open(backend_dev_handle, backend_image_spec, 281 &backend_handle); 282 if (result != 0) { 283 WARN("Failed to open FIP (%i)\n", result); 284 result = -ENOENT; 285 goto fip_file_read_exit; 286 } 287 288 fp = (file_state_t *)entity->info; 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); 293 if (result != 0) { 294 WARN("fip_file_read: failed to seek\n"); 295 result = -ENOENT; 296 goto fip_file_read_close; 297 } 298 299 result = io_read(backend_handle, buffer, length, &bytes_read); 300 if (result != 0) { 301 /* We cannot read our data. Fail. */ 302 WARN("Failed to read payload (%i)\n", result); 303 result = -ENOENT; 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 */ 321 static int fip_file_close(io_entity_t *entity) 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) { 327 zeromem(¤t_file, sizeof(current_file)); 328 } 329 330 /* Clear the Entity info. */ 331 entity->info = 0; 332 333 return 0; 334 } 335 336 /* Exported functions */ 337 338 /* Register the Firmware Image Package driver with the IO abstraction */ 339 int register_io_dev_fip(const io_dev_connector_t **dev_con) 340 { 341 int result; 342 assert(dev_con != NULL); 343 344 result = io_register_device(&fip_dev_info); 345 if (result == 0) 346 *dev_con = &fip_dev_connector; 347 348 return result; 349 } 350