1 /* 2 * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved. 3 * Copyright (c) 2021-2025, Renesas Electronics Corporation. All rights reserved. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 #include <assert.h> 9 #include <errno.h> 10 #include <string.h> 11 12 #include <arch.h> 13 #include <arch_features.h> 14 #include <arch_helpers.h> 15 #include <common/bl_common.h> 16 #include <common/build_message.h> 17 #include <common/debug.h> 18 #include <drivers/auth/auth_mod.h> 19 #include <drivers/io/io_storage.h> 20 #include <lib/utils.h> 21 #include <lib/xlat_tables/xlat_tables_defs.h> 22 #include <plat/common/platform.h> 23 24 #if TRUSTED_BOARD_BOOT 25 # ifdef DYN_DISABLE_AUTH 26 static int disable_auth; 27 28 /****************************************************************************** 29 * API to dynamically disable authentication. Only meant for development 30 * systems. This is only invoked if DYN_DISABLE_AUTH is defined. 31 *****************************************************************************/ 32 void dyn_disable_auth(void) 33 { 34 INFO("Disabling authentication of images dynamically\n"); 35 disable_auth = 1; 36 } 37 # endif /* DYN_DISABLE_AUTH */ 38 39 /****************************************************************************** 40 * Function to determine whether the authentication is disabled dynamically. 41 *****************************************************************************/ 42 static int dyn_is_auth_disabled(void) 43 { 44 # ifdef DYN_DISABLE_AUTH 45 return disable_auth; 46 # else 47 return 0; 48 # endif 49 } 50 #endif /* TRUSTED_BOARD_BOOT */ 51 52 uintptr_t page_align(uintptr_t value, unsigned dir) 53 { 54 /* Round up the limit to the next page boundary */ 55 if ((value & PAGE_SIZE_MASK) != 0U) { 56 value &= ~PAGE_SIZE_MASK; 57 if (dir == UP) { 58 value += PAGE_SIZE; 59 } 60 } 61 62 return value; 63 } 64 65 /******************************************************************************* 66 * Internal function to load an image at a specific address given 67 * an image ID and extents of free memory. 68 * 69 * If the load is successful then the image information is updated. 70 * 71 * Returns 0 on success, a negative error code otherwise. 72 ******************************************************************************/ 73 static int load_image(unsigned int image_id, image_info_t *image_data) 74 { 75 uintptr_t dev_handle = 0ULL; 76 uintptr_t image_handle = 0ULL; 77 uintptr_t image_spec = 0ULL; 78 uintptr_t image_base; 79 size_t image_size = 0ULL; 80 size_t bytes_read = 0ULL; 81 int io_result; 82 83 assert(image_data != NULL); 84 assert(image_data->h.version >= VERSION_2); 85 86 image_base = image_data->image_base; 87 88 /* Obtain a reference to the image by querying the platform layer */ 89 io_result = plat_get_image_source(image_id, &dev_handle, &image_spec); 90 if (io_result != 0) { 91 WARN("Failed to obtain reference to image id=%u (%i)\n", 92 image_id, io_result); 93 return io_result; 94 } 95 96 /* Attempt to access the image */ 97 io_result = io_open(dev_handle, image_spec, &image_handle); 98 if (io_result != 0) { 99 WARN("Failed to access image id=%u (%i)\n", 100 image_id, io_result); 101 return io_result; 102 } 103 104 INFO("Loading image id=%u at address 0x%lx\n", image_id, image_base); 105 106 /* Find the size of the image */ 107 io_result = io_size(image_handle, &image_size); 108 if (io_result != 0) { 109 WARN("Failed to determine the size of the image id=%u (%i)\n", 110 image_id, io_result); 111 goto exit_load_image; 112 } 113 114 if (image_size == 0U) { 115 WARN("image id=%u size is zero\n", image_id); 116 io_result = -EIO; 117 goto exit_load_image; 118 } 119 120 /* Check that the image size to load is within limit */ 121 if (image_size > image_data->image_max_size) { 122 WARN("Image id=%u size out of bounds\n", image_id); 123 io_result = -EFBIG; 124 goto exit_load_image; 125 } 126 127 /* 128 * image_data->image_max_size is a uint32_t so image_size will always 129 * fit in image_data->image_size. 130 */ 131 image_data->image_size = (uint32_t)image_size; 132 133 /* We have enough space so load the image now */ 134 /* TODO: Consider whether to try to recover/retry a partially successful read */ 135 io_result = io_read(image_handle, image_base, image_size, &bytes_read); 136 if ((io_result != 0) || (bytes_read < image_size)) { 137 WARN("Failed to load image id=%u (%i)\n", image_id, io_result); 138 goto exit_load_image; 139 } 140 141 INFO("Image id=%u loaded: 0x%lx - 0x%lx\n", image_id, image_base, 142 (uintptr_t)(image_base + image_size)); 143 144 exit_load_image: 145 (void)io_close(image_handle); 146 /* Ignore improbable/unrecoverable error in 'close' */ 147 148 /* TODO: Consider maintaining open device connection from this bootloader stage */ 149 (void)io_dev_close(dev_handle); 150 /* Ignore improbable/unrecoverable error in 'dev_close' */ 151 152 return io_result; 153 } 154 155 #if TRUSTED_BOARD_BOOT 156 /* 157 * This function uses recursion to authenticate the parent images up to the root 158 * of trust. 159 */ 160 static int load_auth_image_recursive(unsigned int image_id, 161 image_info_t *image_data) 162 { 163 int rc; 164 unsigned int parent_id; 165 166 /* Use recursion to authenticate parent images */ 167 rc = auth_mod_get_parent_id(image_id, &parent_id); 168 if (rc == 0) { 169 rc = load_auth_image_recursive(parent_id, image_data); 170 if (rc != 0) { 171 return rc; 172 } 173 } 174 175 /* Load the image */ 176 rc = load_image(image_id, image_data); 177 if (rc != 0) { 178 return rc; 179 } 180 181 /* Authenticate it */ 182 rc = auth_mod_verify_img(image_id, 183 (void *)image_data->image_base, 184 image_data->image_size); 185 if (rc != 0) { 186 /* Authentication error, zero memory and flush it right away. */ 187 zero_normalmem((void *)image_data->image_base, 188 image_data->image_size); 189 flush_dcache_range(image_data->image_base, 190 image_data->image_size); 191 return -EAUTH; 192 } 193 194 return 0; 195 } 196 #endif /* TRUSTED_BOARD_BOOT */ 197 198 static int load_auth_image_internal(unsigned int image_id, 199 image_info_t *image_data) 200 { 201 #if TRUSTED_BOARD_BOOT 202 if (dyn_is_auth_disabled() == 0) { 203 return load_auth_image_recursive(image_id, image_data); 204 } 205 #endif 206 207 return load_image(image_id, image_data); 208 } 209 210 /******************************************************************************* 211 * Generic function to load and authenticate an image. The image is actually 212 * loaded by calling the 'load_image()' function. Therefore, it returns the 213 * same error codes if the loading operation failed, or -EAUTH if the 214 * authentication failed. In addition, this function uses recursion to 215 * authenticate the parent images up to the root of trust (if TBB is enabled). 216 ******************************************************************************/ 217 int load_auth_image(unsigned int image_id, image_info_t *image_data) 218 { 219 int err; 220 221 if ((plat_try_img_ops == NULL) || (plat_try_img_ops->next_instance == NULL)) { 222 err = load_auth_image_internal(image_id, image_data); 223 } else { 224 do { 225 err = load_auth_image_internal(image_id, image_data); 226 if (err != 0) { 227 if (plat_try_img_ops->next_instance(image_id) != 0) { 228 return err; 229 } 230 } 231 } while (err != 0); 232 } 233 234 if (err == 0) { 235 /* 236 * If loading of the image gets passed (along with its 237 * authentication in case of Trusted-Boot flow) then measure 238 * it (if MEASURED_BOOT flag is enabled). 239 */ 240 err = plat_mboot_measure_image(image_id, image_data); 241 if (err != 0) { 242 return err; 243 } 244 245 /* 246 * Flush the image to main memory so that it can be executed 247 * later by any CPU, regardless of cache and MMU state. 248 */ 249 flush_dcache_range(image_data->image_base, 250 image_data->image_size); 251 } 252 253 return err; 254 } 255 256 /******************************************************************************* 257 * Print the content of an entry_point_info_t structure. 258 ******************************************************************************/ 259 void print_entry_point_info(const entry_point_info_t *ep_info) 260 { 261 INFO("Entry point address = 0x%lx\n", ep_info->pc); 262 INFO("SPSR = 0x%x\n", ep_info->spsr); 263 264 #define PRINT_IMAGE_ARG(n) \ 265 VERBOSE("Argument #" #n " = 0x%llx\n", \ 266 (unsigned long long) ep_info->args.arg##n) 267 268 PRINT_IMAGE_ARG(0); 269 PRINT_IMAGE_ARG(1); 270 PRINT_IMAGE_ARG(2); 271 PRINT_IMAGE_ARG(3); 272 #ifdef __aarch64__ 273 PRINT_IMAGE_ARG(4); 274 PRINT_IMAGE_ARG(5); 275 PRINT_IMAGE_ARG(6); 276 PRINT_IMAGE_ARG(7); 277 #endif 278 #undef PRINT_IMAGE_ARG 279 } 280 281 /* 282 * This function is for returning the TF-A version 283 */ 284 const char *get_version(void) 285 { 286 return build_version; 287 } 288