Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Dan Handley | e83b0ca | 2014-01-14 18:17:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 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 | |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 31 | #include <arch.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 32 | #include <arch_helpers.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <assert.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 34 | #include <bl_common.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 35 | #include <debug.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 36 | #include <io_storage.h> |
| 37 | #include <platform.h> |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 38 | #include <errno.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 39 | #include <stdio.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 40 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 41 | unsigned long page_align(unsigned long value, unsigned dir) |
| 42 | { |
| 43 | unsigned long page_size = 1 << FOUR_KB_SHIFT; |
| 44 | |
| 45 | /* Round up the limit to the next page boundary */ |
| 46 | if (value & (page_size - 1)) { |
| 47 | value &= ~(page_size - 1); |
| 48 | if (dir == UP) |
| 49 | value += page_size; |
| 50 | } |
| 51 | |
| 52 | return value; |
| 53 | } |
| 54 | |
| 55 | static inline unsigned int is_page_aligned (unsigned long addr) { |
| 56 | const unsigned long page_size = 1 << FOUR_KB_SHIFT; |
| 57 | |
| 58 | return (addr & (page_size - 1)) == 0; |
| 59 | } |
| 60 | |
| 61 | void change_security_state(unsigned int target_security_state) |
| 62 | { |
| 63 | unsigned long scr = read_scr(); |
| 64 | |
| 65 | if (target_security_state == SECURE) |
| 66 | scr &= ~SCR_NS_BIT; |
| 67 | else if (target_security_state == NON_SECURE) |
| 68 | scr |= SCR_NS_BIT; |
| 69 | else |
| 70 | assert(0); |
| 71 | |
| 72 | write_scr(scr); |
| 73 | } |
| 74 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 75 | |
| 76 | /******************************************************************************* |
| 77 | * The next two functions are the weak definitions. Platform specific |
| 78 | * code can override them if it wishes to. |
| 79 | ******************************************************************************/ |
| 80 | |
| 81 | /******************************************************************************* |
| 82 | * Function that takes a memory layout into which BL31 has been either top or |
| 83 | * bottom loaded. Using this information, it populates bl31_mem_layout to tell |
| 84 | * BL31 how much memory it has access to and how much is available for use. It |
| 85 | * does not need the address where BL31 has been loaded as BL31 will reclaim |
| 86 | * all the memory used by BL2. |
| 87 | * TODO: Revisit if this and init_bl2_mem_layout can be replaced by a single |
| 88 | * routine. |
| 89 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 90 | void init_bl31_mem_layout(const meminfo_t *bl2_mem_layout, |
| 91 | meminfo_t *bl31_mem_layout, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 92 | unsigned int load_type) |
| 93 | { |
| 94 | if (load_type == BOT_LOAD) { |
| 95 | /* |
| 96 | * ------------ ^ |
| 97 | * | BL2 | | |
| 98 | * |----------| ^ | BL2 |
| 99 | * | | | BL2 free | total |
| 100 | * | | | size | size |
| 101 | * |----------| BL2 free base v | |
| 102 | * | BL31 | | |
| 103 | * ------------ BL2 total base v |
| 104 | */ |
| 105 | unsigned long bl31_size; |
| 106 | |
| 107 | bl31_mem_layout->free_base = bl2_mem_layout->free_base; |
| 108 | |
| 109 | bl31_size = bl2_mem_layout->free_base - bl2_mem_layout->total_base; |
| 110 | bl31_mem_layout->free_size = bl2_mem_layout->total_size - bl31_size; |
| 111 | } else { |
| 112 | /* |
| 113 | * ------------ ^ |
| 114 | * | BL31 | | |
| 115 | * |----------| ^ | BL2 |
| 116 | * | | | BL2 free | total |
| 117 | * | | | size | size |
| 118 | * |----------| BL2 free base v | |
| 119 | * | BL2 | | |
| 120 | * ------------ BL2 total base v |
| 121 | */ |
| 122 | unsigned long bl2_size; |
| 123 | |
| 124 | bl31_mem_layout->free_base = bl2_mem_layout->total_base; |
| 125 | |
| 126 | bl2_size = bl2_mem_layout->free_base - bl2_mem_layout->total_base; |
| 127 | bl31_mem_layout->free_size = bl2_mem_layout->free_size + bl2_size; |
| 128 | } |
| 129 | |
| 130 | bl31_mem_layout->total_base = bl2_mem_layout->total_base; |
| 131 | bl31_mem_layout->total_size = bl2_mem_layout->total_size; |
| 132 | bl31_mem_layout->attr = load_type; |
| 133 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 134 | flush_dcache_range((unsigned long) bl31_mem_layout, sizeof(meminfo_t)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | |
| 138 | /******************************************************************************* |
| 139 | * Function that takes a memory layout into which BL2 has been either top or |
| 140 | * bottom loaded along with the address where BL2 has been loaded in it. Using |
| 141 | * this information, it populates bl2_mem_layout to tell BL2 how much memory |
| 142 | * it has access to and how much is available for use. |
| 143 | ******************************************************************************/ |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 144 | void init_bl2_mem_layout(meminfo_t *bl1_mem_layout, |
| 145 | meminfo_t *bl2_mem_layout, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 146 | unsigned int load_type, |
| 147 | unsigned long bl2_base) |
| 148 | { |
| 149 | unsigned tmp; |
| 150 | |
| 151 | if (load_type == BOT_LOAD) { |
| 152 | bl2_mem_layout->total_base = bl2_base; |
| 153 | tmp = bl1_mem_layout->free_base - bl2_base; |
| 154 | bl2_mem_layout->total_size = bl1_mem_layout->free_size + tmp; |
| 155 | |
| 156 | } else { |
| 157 | bl2_mem_layout->total_base = bl1_mem_layout->free_base; |
| 158 | tmp = bl1_mem_layout->total_base + bl1_mem_layout->total_size; |
| 159 | bl2_mem_layout->total_size = tmp - bl1_mem_layout->free_base; |
| 160 | } |
| 161 | |
| 162 | bl2_mem_layout->free_base = bl1_mem_layout->free_base; |
| 163 | bl2_mem_layout->free_size = bl1_mem_layout->free_size; |
| 164 | bl2_mem_layout->attr = load_type; |
| 165 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 166 | flush_dcache_range((unsigned long) bl2_mem_layout, sizeof(meminfo_t)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | |
| 170 | static void dump_load_info(unsigned long image_load_addr, |
| 171 | unsigned long image_size, |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 172 | const meminfo_t *mem_layout) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 173 | { |
| 174 | #if DEBUG |
| 175 | printf("Trying to load image at address 0x%lx, size = 0x%lx\r\n", |
| 176 | image_load_addr, image_size); |
| 177 | printf("Current memory layout:\r\n"); |
| 178 | printf(" total region = [0x%lx, 0x%lx]\r\n", mem_layout->total_base, |
| 179 | mem_layout->total_base + mem_layout->total_size); |
| 180 | printf(" free region = [0x%lx, 0x%lx]\r\n", mem_layout->free_base, |
| 181 | mem_layout->free_base + mem_layout->free_size); |
| 182 | #endif |
| 183 | } |
| 184 | |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 185 | /* Generic function to return the size of an image */ |
| 186 | unsigned long image_size(const char *image_name) |
| 187 | { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 188 | uintptr_t dev_handle; |
| 189 | uintptr_t image_handle; |
| 190 | uintptr_t image_spec; |
Ryan Harkin | 87274c4 | 2014-02-04 11:43:57 +0000 | [diff] [blame] | 191 | size_t image_size = 0; |
| 192 | int io_result = IO_FAIL; |
| 193 | |
| 194 | assert(image_name != NULL); |
| 195 | |
| 196 | /* Obtain a reference to the image by querying the platform layer */ |
| 197 | io_result = plat_get_image_source(image_name, &dev_handle, &image_spec); |
| 198 | if (io_result != IO_SUCCESS) { |
| 199 | WARN("Failed to obtain reference to image '%s' (%i)\n", |
| 200 | image_name, io_result); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /* Attempt to access the image */ |
| 205 | io_result = io_open(dev_handle, image_spec, &image_handle); |
| 206 | if (io_result != IO_SUCCESS) { |
| 207 | WARN("Failed to access image '%s' (%i)\n", |
| 208 | image_name, io_result); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* Find the size of the image */ |
| 213 | io_result = io_size(image_handle, &image_size); |
| 214 | if ((io_result != IO_SUCCESS) || (image_size == 0)) { |
| 215 | WARN("Failed to determine the size of the image '%s' file (%i)\n", |
| 216 | image_name, io_result); |
| 217 | } |
| 218 | io_result = io_close(image_handle); |
| 219 | /* Ignore improbable/unrecoverable error in 'close' */ |
| 220 | |
| 221 | /* TODO: Consider maintaining open device connection from this |
| 222 | * bootloader stage |
| 223 | */ |
| 224 | io_result = io_dev_close(dev_handle); |
| 225 | /* Ignore improbable/unrecoverable error in 'dev_close' */ |
| 226 | |
| 227 | return image_size; |
| 228 | } |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 229 | /******************************************************************************* |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 230 | * Generic function to load an image into the trusted RAM, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 231 | * given a name, extents of free memory & whether the image should be loaded at |
| 232 | * the bottom or top of the free memory. It updates the memory layout if the |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 233 | * load is successful. It also updates the image information and the entry point |
| 234 | * information in the params passed |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 235 | ******************************************************************************/ |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 236 | int load_image(meminfo_t *mem_layout, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 237 | const char *image_name, |
| 238 | unsigned int load_type, |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 239 | unsigned long fixed_addr, |
| 240 | image_info_t *image_data, |
| 241 | entry_point_info_t *entry_point_info) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 242 | { |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 243 | uintptr_t dev_handle; |
| 244 | uintptr_t image_handle; |
| 245 | uintptr_t image_spec; |
James Morrissey | 40a6f64 | 2014-02-10 14:24:36 +0000 | [diff] [blame] | 246 | unsigned long temp_image_base = 0; |
| 247 | unsigned long image_base = 0; |
| 248 | long offset = 0; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 249 | size_t image_size = 0; |
| 250 | size_t bytes_read = 0; |
| 251 | int io_result = IO_FAIL; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 252 | |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 253 | assert(mem_layout != NULL); |
| 254 | assert(image_name != NULL); |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 255 | assert(image_data->h.version >= VERSION_1); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 256 | |
| 257 | /* Obtain a reference to the image by querying the platform layer */ |
| 258 | io_result = plat_get_image_source(image_name, &dev_handle, &image_spec); |
| 259 | if (io_result != IO_SUCCESS) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 260 | WARN("Failed to obtain reference to image '%s' (%i)\n", |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 261 | image_name, io_result); |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 262 | return io_result; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* Attempt to access the image */ |
| 266 | io_result = io_open(dev_handle, image_spec, &image_handle); |
| 267 | if (io_result != IO_SUCCESS) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 268 | WARN("Failed to access image '%s' (%i)\n", |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 269 | image_name, io_result); |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 270 | return io_result; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 271 | } |
| 272 | |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 273 | /* Find the size of the image */ |
| 274 | io_result = io_size(image_handle, &image_size); |
| 275 | if ((io_result != IO_SUCCESS) || (image_size == 0)) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 276 | WARN("Failed to determine the size of the image '%s' file (%i)\n", |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 277 | image_name, io_result); |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 278 | goto exit; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 281 | /* See if we have enough space */ |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 282 | if (image_size > mem_layout->free_size) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 283 | WARN("Cannot load '%s' file: Not enough space.\n", |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 284 | image_name); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 285 | dump_load_info(0, image_size, mem_layout); |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 286 | goto exit; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | switch (load_type) { |
| 290 | |
| 291 | case TOP_LOAD: |
| 292 | |
| 293 | /* Load the image in the top of free memory */ |
| 294 | temp_image_base = mem_layout->free_base + mem_layout->free_size; |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 295 | temp_image_base -= image_size; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 296 | |
| 297 | /* Page align base address and check whether the image still fits */ |
| 298 | image_base = page_align(temp_image_base, DOWN); |
| 299 | assert(image_base <= temp_image_base); |
| 300 | |
| 301 | if (image_base < mem_layout->free_base) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 302 | WARN("Cannot load '%s' file: Not enough space.\n", |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 303 | image_name); |
| 304 | dump_load_info(image_base, image_size, mem_layout); |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 305 | io_result = -ENOMEM; |
| 306 | goto exit; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* Calculate the amount of extra memory used due to alignment */ |
| 310 | offset = temp_image_base - image_base; |
| 311 | |
| 312 | break; |
| 313 | |
| 314 | case BOT_LOAD: |
| 315 | |
| 316 | /* Load the BL2 image in the bottom of free memory */ |
| 317 | temp_image_base = mem_layout->free_base; |
| 318 | image_base = page_align(temp_image_base, UP); |
| 319 | assert(image_base >= temp_image_base); |
| 320 | |
| 321 | /* Page align base address and check whether the image still fits */ |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 322 | if (image_base + image_size > |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 323 | mem_layout->free_base + mem_layout->free_size) { |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 324 | WARN("Cannot load '%s' file: Not enough space.\n", |
| 325 | image_name); |
| 326 | dump_load_info(image_base, image_size, mem_layout); |
| 327 | io_result = -ENOMEM; |
| 328 | goto exit; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* Calculate the amount of extra memory used due to alignment */ |
| 332 | offset = image_base - temp_image_base; |
| 333 | |
| 334 | break; |
| 335 | |
| 336 | default: |
| 337 | assert(0); |
| 338 | |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Some images must be loaded at a fixed address, not a dynamic one. |
| 343 | * |
| 344 | * This has been implemented as a hack on top of the existing dynamic |
| 345 | * loading mechanism, for the time being. If the 'fixed_addr' function |
| 346 | * argument is different from zero, then it will force the load address. |
| 347 | * So we still have this principle of top/bottom loading but the code |
| 348 | * determining the load address is bypassed and the load address is |
| 349 | * forced to the fixed one. |
| 350 | * |
| 351 | * This can result in quite a lot of wasted space because we still use |
| 352 | * 1 sole meminfo structure to represent the extents of free memory, |
| 353 | * where we should use some sort of linked list. |
| 354 | * |
| 355 | * E.g. we want to load BL2 at address 0x04020000, the resulting memory |
| 356 | * layout should look as follows: |
| 357 | * ------------ 0x04040000 |
| 358 | * | | <- Free space (1) |
| 359 | * |----------| |
| 360 | * | BL2 | |
| 361 | * |----------| 0x04020000 |
| 362 | * | | <- Free space (2) |
| 363 | * |----------| |
| 364 | * | BL1 | |
| 365 | * ------------ 0x04000000 |
| 366 | * |
| 367 | * But in the current hacky implementation, we'll need to specify |
| 368 | * whether BL2 is loaded at the top or bottom of the free memory. |
| 369 | * E.g. if BL2 is considered as top-loaded, the meminfo structure |
| 370 | * will give the following view of the memory, hiding the chunk of |
| 371 | * free memory above BL2: |
| 372 | * ------------ 0x04040000 |
| 373 | * | | |
| 374 | * | | |
| 375 | * | BL2 | |
| 376 | * |----------| 0x04020000 |
| 377 | * | | <- Free space (2) |
| 378 | * |----------| |
| 379 | * | BL1 | |
| 380 | * ------------ 0x04000000 |
| 381 | */ |
| 382 | if (fixed_addr != 0) { |
| 383 | /* Load the image at the given address. */ |
| 384 | image_base = fixed_addr; |
| 385 | |
| 386 | /* Check whether the image fits. */ |
| 387 | if ((image_base < mem_layout->free_base) || |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 388 | (image_base + image_size > |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 389 | mem_layout->free_base + mem_layout->free_size)) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 390 | WARN("Cannot load '%s' file: Not enough space.\n", |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 391 | image_name); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 392 | dump_load_info(image_base, image_size, mem_layout); |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 393 | io_result = -ENOMEM; |
| 394 | goto exit; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* Check whether the fixed load address is page-aligned. */ |
| 398 | if (!is_page_aligned(image_base)) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 399 | WARN("Cannot load '%s' file at unaligned address 0x%lx\n", |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 400 | image_name, fixed_addr); |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 401 | io_result = -ENOMEM; |
| 402 | goto exit; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Calculate the amount of extra memory used due to fixed |
| 407 | * loading. |
| 408 | */ |
| 409 | if (load_type == TOP_LOAD) { |
| 410 | unsigned long max_addr, space_used; |
| 411 | /* |
| 412 | * ------------ max_addr |
| 413 | * | /wasted/ | | offset |
| 414 | * |..........|.............................. |
| 415 | * | image | | image_flen |
| 416 | * |----------| fixed_addr |
| 417 | * | | |
| 418 | * | | |
| 419 | * ------------ total_base |
| 420 | */ |
| 421 | max_addr = mem_layout->total_base + mem_layout->total_size; |
| 422 | /* |
| 423 | * Compute the amount of memory used by the image. |
| 424 | * Corresponds to all space above the image load |
| 425 | * address. |
| 426 | */ |
| 427 | space_used = max_addr - fixed_addr; |
| 428 | /* |
| 429 | * Calculate the amount of wasted memory within the |
| 430 | * amount of memory used by the image. |
| 431 | */ |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 432 | offset = space_used - image_size; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 433 | } else /* BOT_LOAD */ |
| 434 | /* |
| 435 | * ------------ |
| 436 | * | | |
| 437 | * | | |
| 438 | * |----------| |
| 439 | * | image | |
| 440 | * |..........| fixed_addr |
| 441 | * | /wasted/ | | offset |
| 442 | * ------------ total_base |
| 443 | */ |
| 444 | offset = fixed_addr - mem_layout->total_base; |
| 445 | } |
| 446 | |
| 447 | /* We have enough space so load the image now */ |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 448 | /* TODO: Consider whether to try to recover/retry a partially successful read */ |
Dan Handley | a4cb68e | 2014-04-23 13:47:06 +0100 | [diff] [blame] | 449 | io_result = io_read(image_handle, image_base, image_size, &bytes_read); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 450 | if ((io_result != IO_SUCCESS) || (bytes_read < image_size)) { |
Jeenu Viswambharan | 08c28d5 | 2014-02-20 12:03:31 +0000 | [diff] [blame] | 451 | WARN("Failed to load '%s' file (%i)\n", image_name, io_result); |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 452 | goto exit; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 455 | image_data->image_base = image_base; |
| 456 | image_data->image_size = image_size; |
| 457 | |
| 458 | entry_point_info->pc = image_base; |
| 459 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 460 | /* |
| 461 | * File has been successfully loaded. Update the free memory |
| 462 | * data structure & flush the contents of the TZRAM so that |
| 463 | * the next EL can see it. |
| 464 | */ |
| 465 | /* Update the memory contents */ |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 466 | flush_dcache_range(image_base, image_size); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 467 | |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 468 | mem_layout->free_size -= image_size + offset; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 469 | |
| 470 | /* Update the base of free memory since its moved up */ |
| 471 | if (load_type == BOT_LOAD) |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 472 | mem_layout->free_base += offset + image_size; |
| 473 | |
| 474 | exit: |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 475 | io_close(image_handle); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 476 | /* Ignore improbable/unrecoverable error in 'close' */ |
| 477 | |
| 478 | /* TODO: Consider maintaining open device connection from this bootloader stage */ |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 479 | io_dev_close(dev_handle); |
James Morrissey | 9d72b4e | 2014-02-10 17:04:32 +0000 | [diff] [blame] | 480 | /* Ignore improbable/unrecoverable error in 'dev_close' */ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 481 | |
Vikram Kanigiri | da56743 | 2014-04-15 18:08:08 +0100 | [diff] [blame^] | 482 | return io_result; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 483 | } |