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