Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Soby Mathew | 9fe8804 | 2018-03-26 12:43:37 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 7 | #include <arch.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 8 | #include <arch_helpers.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 9 | #include <assert.h> |
Juan Castillo | a08a5e7 | 2015-05-19 11:54:12 +0100 | [diff] [blame] | 10 | #include <auth_mod.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 11 | #include <bl_common.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 12 | #include <debug.h> |
Sandrine Bailleux | 467d057 | 2014-06-24 14:02:34 +0100 | [diff] [blame] | 13 | #include <errno.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 14 | #include <io_storage.h> |
| 15 | #include <platform.h> |
Juan Castillo | 97dbcf1 | 2015-08-17 10:43:27 +0100 | [diff] [blame] | 16 | #include <string.h> |
Sandrine Bailleux | 4ec7e2d | 2016-07-12 09:12:24 +0100 | [diff] [blame] | 17 | #include <utils.h> |
Antonio Nino Diaz | 4ef91f1 | 2017-02-20 14:22:22 +0000 | [diff] [blame] | 18 | #include <xlat_tables_defs.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 19 | |
Soby Mathew | 9fe8804 | 2018-03-26 12:43:37 +0100 | [diff] [blame] | 20 | #if TRUSTED_BOARD_BOOT |
| 21 | # ifdef DYN_DISABLE_AUTH |
| 22 | static int disable_auth; |
| 23 | |
| 24 | /****************************************************************************** |
| 25 | * API to dynamically disable authentication. Only meant for development |
Roberto Vargas | 025946a | 2018-09-24 17:20:48 +0100 | [diff] [blame] | 26 | * systems. This is only invoked if DYN_DISABLE_AUTH is defined. |
Soby Mathew | 9fe8804 | 2018-03-26 12:43:37 +0100 | [diff] [blame] | 27 | *****************************************************************************/ |
| 28 | void dyn_disable_auth(void) |
| 29 | { |
| 30 | INFO("Disabling authentication of images dynamically\n"); |
| 31 | disable_auth = 1; |
| 32 | } |
| 33 | # endif /* DYN_DISABLE_AUTH */ |
| 34 | |
| 35 | /****************************************************************************** |
| 36 | * Function to determine whether the authentication is disabled dynamically. |
| 37 | *****************************************************************************/ |
| 38 | static int dyn_is_auth_disabled(void) |
| 39 | { |
| 40 | # ifdef DYN_DISABLE_AUTH |
| 41 | return disable_auth; |
| 42 | # else |
| 43 | return 0; |
| 44 | # endif |
| 45 | } |
| 46 | #endif /* TRUSTED_BOARD_BOOT */ |
| 47 | |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 48 | uintptr_t page_align(uintptr_t value, unsigned dir) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 49 | { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 50 | /* Round up the limit to the next page boundary */ |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 51 | if (value & (PAGE_SIZE - 1)) { |
| 52 | value &= ~(PAGE_SIZE - 1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 53 | if (dir == UP) |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 54 | value += PAGE_SIZE; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return value; |
| 58 | } |
| 59 | |
Sandrine Bailleux | 467d057 | 2014-06-24 14:02:34 +0100 | [diff] [blame] | 60 | /****************************************************************************** |
| 61 | * Determine whether the memory region delimited by 'addr' and 'size' is free, |
| 62 | * given the extents of free memory. |
Sandrine Bailleux | 4ec7e2d | 2016-07-12 09:12:24 +0100 | [diff] [blame] | 63 | * Return 1 if it is free, 0 if it is not free or if the input values are |
| 64 | * invalid. |
Sandrine Bailleux | 467d057 | 2014-06-24 14:02:34 +0100 | [diff] [blame] | 65 | *****************************************************************************/ |
Sandrine Bailleux | b0e529b | 2016-11-08 14:27:10 +0000 | [diff] [blame] | 66 | int is_mem_free(uintptr_t free_base, size_t free_size, |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 67 | uintptr_t addr, size_t size) |
Sandrine Bailleux | 467d057 | 2014-06-24 14:02:34 +0100 | [diff] [blame] | 68 | { |
Sandrine Bailleux | 4ec7e2d | 2016-07-12 09:12:24 +0100 | [diff] [blame] | 69 | uintptr_t free_end, requested_end; |
| 70 | |
| 71 | /* |
| 72 | * Handle corner cases first. |
| 73 | * |
| 74 | * The order of the 2 tests is important, because if there's no space |
| 75 | * left (i.e. free_size == 0) but we don't ask for any memory |
| 76 | * (i.e. size == 0) then we should report that the memory is free. |
| 77 | */ |
| 78 | if (size == 0) |
| 79 | return 1; /* A zero-byte region is always free */ |
| 80 | if (free_size == 0) |
| 81 | return 0; |
| 82 | |
| 83 | /* |
| 84 | * Check that the end addresses don't overflow. |
| 85 | * If they do, consider that this memory region is not free, as this |
| 86 | * is an invalid scenario. |
| 87 | */ |
| 88 | if (check_uptr_overflow(free_base, free_size - 1)) |
| 89 | return 0; |
| 90 | free_end = free_base + (free_size - 1); |
| 91 | |
| 92 | if (check_uptr_overflow(addr, size - 1)) |
| 93 | return 0; |
| 94 | requested_end = addr + (size - 1); |
| 95 | |
| 96 | /* |
| 97 | * Finally, check that the requested memory region lies within the free |
| 98 | * region. |
| 99 | */ |
| 100 | return (addr >= free_base) && (requested_end <= free_end); |
Sandrine Bailleux | 467d057 | 2014-06-24 14:02:34 +0100 | [diff] [blame] | 101 | } |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 102 | |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 103 | /* Generic function to return the size of an image */ |
Daniel Boulby | 4f3b6ed | 2018-05-04 11:18:26 +0100 | [diff] [blame] | 104 | size_t get_image_size(unsigned int image_id) |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 105 | { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 106 | uintptr_t dev_handle; |
| 107 | uintptr_t image_handle; |
| 108 | uintptr_t image_spec; |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 109 | size_t image_size = 0; |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 110 | int io_result; |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 111 | |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 112 | /* Obtain a reference to the image by querying the platform layer */ |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 113 | io_result = plat_get_image_source(image_id, &dev_handle, &image_spec); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 114 | if (io_result != 0) { |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 115 | WARN("Failed to obtain reference to image id=%u (%i)\n", |
| 116 | image_id, io_result); |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /* Attempt to access the image */ |
| 121 | io_result = io_open(dev_handle, image_spec, &image_handle); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 122 | if (io_result != 0) { |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 123 | WARN("Failed to access image id=%u (%i)\n", |
| 124 | image_id, io_result); |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* Find the size of the image */ |
| 129 | io_result = io_size(image_handle, &image_size); |
Juan Castillo | 6e76206 | 2015-11-02 10:47:01 +0000 | [diff] [blame] | 130 | if ((io_result != 0) || (image_size == 0)) { |
Juan Castillo | 3a66aca | 2015-04-13 17:36:19 +0100 | [diff] [blame] | 131 | WARN("Failed to determine the size of the image id=%u (%i)\n", |
| 132 | image_id, io_result); |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 133 | } |
| 134 | io_result = io_close(image_handle); |
| 135 | /* Ignore improbable/unrecoverable error in 'close' */ |
| 136 | |
| 137 | /* TODO: Consider maintaining open device connection from this |
| 138 | * bootloader stage |
| 139 | */ |
| 140 | io_result = io_dev_close(dev_handle); |
| 141 | /* Ignore improbable/unrecoverable error in 'dev_close' */ |
| 142 | |
| 143 | return image_size; |
| 144 | } |
Sandrine Bailleux | 467d057 | 2014-06-24 14:02:34 +0100 | [diff] [blame] | 145 | |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 146 | /******************************************************************************* |
Soby Mathew | 7c8af06 | 2017-11-10 13:14:40 +0000 | [diff] [blame] | 147 | * Internal function to load an image at a specific address given |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 148 | * an image ID and extents of free memory. |
| 149 | * |
| 150 | * If the load is successful then the image information is updated. |
| 151 | * |
| 152 | * Returns 0 on success, a negative error code otherwise. |
| 153 | ******************************************************************************/ |
Soby Mathew | 7c8af06 | 2017-11-10 13:14:40 +0000 | [diff] [blame] | 154 | static int load_image(unsigned int image_id, image_info_t *image_data) |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 155 | { |
| 156 | uintptr_t dev_handle; |
| 157 | uintptr_t image_handle; |
| 158 | uintptr_t image_spec; |
| 159 | uintptr_t image_base; |
| 160 | size_t image_size; |
| 161 | size_t bytes_read; |
| 162 | int io_result; |
| 163 | |
| 164 | assert(image_data != NULL); |
| 165 | assert(image_data->h.version >= VERSION_2); |
| 166 | |
| 167 | image_base = image_data->image_base; |
| 168 | |
| 169 | /* Obtain a reference to the image by querying the platform layer */ |
| 170 | io_result = plat_get_image_source(image_id, &dev_handle, &image_spec); |
| 171 | if (io_result != 0) { |
| 172 | WARN("Failed to obtain reference to image id=%u (%i)\n", |
| 173 | image_id, io_result); |
| 174 | return io_result; |
| 175 | } |
| 176 | |
| 177 | /* Attempt to access the image */ |
| 178 | io_result = io_open(dev_handle, image_spec, &image_handle); |
| 179 | if (io_result != 0) { |
| 180 | WARN("Failed to access image id=%u (%i)\n", |
| 181 | image_id, io_result); |
| 182 | return io_result; |
| 183 | } |
| 184 | |
| 185 | INFO("Loading image id=%u at address %p\n", image_id, |
| 186 | (void *) image_base); |
| 187 | |
| 188 | /* Find the size of the image */ |
| 189 | io_result = io_size(image_handle, &image_size); |
| 190 | if ((io_result != 0) || (image_size == 0)) { |
| 191 | WARN("Failed to determine the size of the image id=%u (%i)\n", |
| 192 | image_id, io_result); |
| 193 | goto exit; |
| 194 | } |
| 195 | |
| 196 | /* Check that the image size to load is within limit */ |
| 197 | if (image_size > image_data->image_max_size) { |
| 198 | WARN("Image id=%u size out of bounds\n", image_id); |
| 199 | io_result = -EFBIG; |
| 200 | goto exit; |
| 201 | } |
| 202 | |
| 203 | image_data->image_size = image_size; |
| 204 | |
| 205 | /* We have enough space so load the image now */ |
| 206 | /* TODO: Consider whether to try to recover/retry a partially successful read */ |
| 207 | io_result = io_read(image_handle, image_base, image_size, &bytes_read); |
| 208 | if ((io_result != 0) || (bytes_read < image_size)) { |
| 209 | WARN("Failed to load image id=%u (%i)\n", image_id, io_result); |
| 210 | goto exit; |
| 211 | } |
| 212 | |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 213 | INFO("Image id=%u loaded: %p - %p\n", image_id, (void *) image_base, |
| 214 | (void *) (image_base + image_size)); |
| 215 | |
| 216 | exit: |
| 217 | io_close(image_handle); |
| 218 | /* Ignore improbable/unrecoverable error in 'close' */ |
| 219 | |
| 220 | /* TODO: Consider maintaining open device connection from this bootloader stage */ |
| 221 | io_dev_close(dev_handle); |
| 222 | /* Ignore improbable/unrecoverable error in 'dev_close' */ |
| 223 | |
| 224 | return io_result; |
| 225 | } |
| 226 | |
Antonio Nino Diaz | d7db870 | 2016-11-22 10:58:30 +0000 | [diff] [blame] | 227 | static int load_auth_image_internal(unsigned int image_id, |
| 228 | image_info_t *image_data, |
| 229 | int is_parent_image) |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 230 | { |
| 231 | int rc; |
| 232 | |
| 233 | #if TRUSTED_BOARD_BOOT |
Soby Mathew | 9fe8804 | 2018-03-26 12:43:37 +0100 | [diff] [blame] | 234 | if (dyn_is_auth_disabled() == 0) { |
| 235 | unsigned int parent_id; |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 236 | |
Soby Mathew | 9fe8804 | 2018-03-26 12:43:37 +0100 | [diff] [blame] | 237 | /* Use recursion to authenticate parent images */ |
| 238 | rc = auth_mod_get_parent_id(image_id, &parent_id); |
| 239 | if (rc == 0) { |
| 240 | rc = load_auth_image_internal(parent_id, image_data, 1); |
| 241 | if (rc != 0) { |
| 242 | return rc; |
| 243 | } |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | #endif /* TRUSTED_BOARD_BOOT */ |
| 247 | |
| 248 | /* Load the image */ |
| 249 | rc = load_image(image_id, image_data); |
| 250 | if (rc != 0) { |
| 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | #if TRUSTED_BOARD_BOOT |
Soby Mathew | 9fe8804 | 2018-03-26 12:43:37 +0100 | [diff] [blame] | 255 | if (dyn_is_auth_disabled() == 0) { |
| 256 | /* Authenticate it */ |
| 257 | rc = auth_mod_verify_img(image_id, |
| 258 | (void *)image_data->image_base, |
| 259 | image_data->image_size); |
| 260 | if (rc != 0) { |
| 261 | /* Authentication error, zero memory and flush it right away. */ |
| 262 | zero_normalmem((void *)image_data->image_base, |
| 263 | image_data->image_size); |
| 264 | flush_dcache_range(image_data->image_base, |
| 265 | image_data->image_size); |
| 266 | return -EAUTH; |
| 267 | } |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 268 | } |
Soby Mathew | 7c8af06 | 2017-11-10 13:14:40 +0000 | [diff] [blame] | 269 | #endif /* TRUSTED_BOARD_BOOT */ |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 270 | |
| 271 | /* |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 272 | * Flush the image to main memory so that it can be executed later by |
Soby Mathew | 7c8af06 | 2017-11-10 13:14:40 +0000 | [diff] [blame] | 273 | * any CPU, regardless of cache and MMU state. If TBB is enabled, then |
| 274 | * the file has been successfully loaded and authenticated and flush |
| 275 | * only for child images, not for the parents (certificates). |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 276 | */ |
Antonio Nino Diaz | d7db870 | 2016-11-22 10:58:30 +0000 | [diff] [blame] | 277 | if (!is_parent_image) { |
| 278 | flush_dcache_range(image_data->image_base, |
| 279 | image_data->image_size); |
| 280 | } |
Soby Mathew | 7c8af06 | 2017-11-10 13:14:40 +0000 | [diff] [blame] | 281 | |
Yatharth Kochar | 3345a8d | 2016-09-12 16:08:41 +0100 | [diff] [blame] | 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
Antonio Nino Diaz | d7db870 | 2016-11-22 10:58:30 +0000 | [diff] [blame] | 286 | /******************************************************************************* |
| 287 | * Generic function to load and authenticate an image. The image is actually |
| 288 | * loaded by calling the 'load_image()' function. Therefore, it returns the |
| 289 | * same error codes if the loading operation failed, or -EAUTH if the |
| 290 | * authentication failed. In addition, this function uses recursion to |
| 291 | * authenticate the parent images up to the root of trust. |
| 292 | ******************************************************************************/ |
| 293 | int load_auth_image(unsigned int image_id, image_info_t *image_data) |
| 294 | { |
Roberto Vargas | bc1ae1f | 2017-09-26 12:53:01 +0100 | [diff] [blame] | 295 | int err; |
| 296 | |
| 297 | do { |
| 298 | err = load_auth_image_internal(image_id, image_data, 0); |
| 299 | } while (err != 0 && plat_try_next_boot_source()); |
| 300 | |
| 301 | return err; |
Antonio Nino Diaz | d7db870 | 2016-11-22 10:58:30 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Sandrine Bailleux | b2e224c | 2015-09-28 17:03:06 +0100 | [diff] [blame] | 304 | /******************************************************************************* |
| 305 | * Print the content of an entry_point_info_t structure. |
| 306 | ******************************************************************************/ |
| 307 | void print_entry_point_info(const entry_point_info_t *ep_info) |
| 308 | { |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 309 | INFO("Entry point address = %p\n", (void *)ep_info->pc); |
| 310 | INFO("SPSR = 0x%x\n", ep_info->spsr); |
Sandrine Bailleux | b2e224c | 2015-09-28 17:03:06 +0100 | [diff] [blame] | 311 | |
| 312 | #define PRINT_IMAGE_ARG(n) \ |
| 313 | VERBOSE("Argument #" #n " = 0x%llx\n", \ |
| 314 | (unsigned long long) ep_info->args.arg##n) |
| 315 | |
| 316 | PRINT_IMAGE_ARG(0); |
| 317 | PRINT_IMAGE_ARG(1); |
| 318 | PRINT_IMAGE_ARG(2); |
| 319 | PRINT_IMAGE_ARG(3); |
Soby Mathew | cdf58cb | 2016-08-30 13:07:31 +0100 | [diff] [blame] | 320 | #ifndef AARCH32 |
Sandrine Bailleux | b2e224c | 2015-09-28 17:03:06 +0100 | [diff] [blame] | 321 | PRINT_IMAGE_ARG(4); |
| 322 | PRINT_IMAGE_ARG(5); |
| 323 | PRINT_IMAGE_ARG(6); |
| 324 | PRINT_IMAGE_ARG(7); |
Soby Mathew | cdf58cb | 2016-08-30 13:07:31 +0100 | [diff] [blame] | 325 | #endif |
Sandrine Bailleux | b2e224c | 2015-09-28 17:03:06 +0100 | [diff] [blame] | 326 | #undef PRINT_IMAGE_ARG |
| 327 | } |