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