blob: 4affa76586366f839c4b864ae743ce36b4610bdd [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
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 Handley97043ac2014-04-09 13:14:54 +010031#include <arch.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010032#include <arch_helpers.h>
Dan Handley97043ac2014-04-09 13:14:54 +010033#include <assert.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010034#include <bl_common.h>
Dan Handley35e98e52014-04-09 13:13:04 +010035#include <debug.h>
Dan Handley97043ac2014-04-09 13:14:54 +010036#include <io_storage.h>
37#include <platform.h>
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +010038#include <errno.h>
Dan Handley97043ac2014-04-09 13:14:54 +010039#include <stdio.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010040
Achin Gupta4f6ad662013-10-25 09:08:21 +010041unsigned 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
55static 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
61void 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 Gupta4f6ad662013-10-25 09:08:21 +010075
76/*******************************************************************************
Dan Handleydec5e0d2014-05-15 14:11:36 +010077 * The next function has a weak definition. Platform specific code can override
78 * it if it wishes to.
Achin Gupta4f6ad662013-10-25 09:08:21 +010079 ******************************************************************************/
Dan Handleydec5e0d2014-05-15 14:11:36 +010080#pragma weak init_bl2_mem_layout
Achin Gupta4f6ad662013-10-25 09:08:21 +010081
82/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +010083 * Function that takes a memory layout into which BL2 has been either top or
84 * bottom loaded along with the address where BL2 has been loaded in it. Using
85 * this information, it populates bl2_mem_layout to tell BL2 how much memory
86 * it has access to and how much is available for use.
87 ******************************************************************************/
Dan Handleyfb037bf2014-04-10 15:37:22 +010088void init_bl2_mem_layout(meminfo_t *bl1_mem_layout,
89 meminfo_t *bl2_mem_layout,
Achin Gupta4f6ad662013-10-25 09:08:21 +010090 unsigned int load_type,
91 unsigned long bl2_base)
92{
93 unsigned tmp;
94
95 if (load_type == BOT_LOAD) {
96 bl2_mem_layout->total_base = bl2_base;
97 tmp = bl1_mem_layout->free_base - bl2_base;
98 bl2_mem_layout->total_size = bl1_mem_layout->free_size + tmp;
99
100 } else {
101 bl2_mem_layout->total_base = bl1_mem_layout->free_base;
102 tmp = bl1_mem_layout->total_base + bl1_mem_layout->total_size;
103 bl2_mem_layout->total_size = tmp - bl1_mem_layout->free_base;
104 }
105
106 bl2_mem_layout->free_base = bl1_mem_layout->free_base;
107 bl2_mem_layout->free_size = bl1_mem_layout->free_size;
108 bl2_mem_layout->attr = load_type;
109
Dan Handleyfb037bf2014-04-10 15:37:22 +0100110 flush_dcache_range((unsigned long) bl2_mem_layout, sizeof(meminfo_t));
Achin Gupta4f6ad662013-10-25 09:08:21 +0100111 return;
112}
113
114static void dump_load_info(unsigned long image_load_addr,
115 unsigned long image_size,
Dan Handleyfb037bf2014-04-10 15:37:22 +0100116 const meminfo_t *mem_layout)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100117{
118#if DEBUG
119 printf("Trying to load image at address 0x%lx, size = 0x%lx\r\n",
120 image_load_addr, image_size);
121 printf("Current memory layout:\r\n");
122 printf(" total region = [0x%lx, 0x%lx]\r\n", mem_layout->total_base,
123 mem_layout->total_base + mem_layout->total_size);
124 printf(" free region = [0x%lx, 0x%lx]\r\n", mem_layout->free_base,
125 mem_layout->free_base + mem_layout->free_size);
126#endif
127}
128
Ryan Harkinee9ad782014-02-04 11:43:57 +0000129/* Generic function to return the size of an image */
130unsigned long image_size(const char *image_name)
131{
Dan Handley625de1d2014-04-23 13:47:06 +0100132 uintptr_t dev_handle;
133 uintptr_t image_handle;
134 uintptr_t image_spec;
Ryan Harkinee9ad782014-02-04 11:43:57 +0000135 size_t image_size = 0;
136 int io_result = IO_FAIL;
137
138 assert(image_name != NULL);
139
140 /* Obtain a reference to the image by querying the platform layer */
141 io_result = plat_get_image_source(image_name, &dev_handle, &image_spec);
142 if (io_result != IO_SUCCESS) {
143 WARN("Failed to obtain reference to image '%s' (%i)\n",
144 image_name, io_result);
145 return 0;
146 }
147
148 /* Attempt to access the image */
149 io_result = io_open(dev_handle, image_spec, &image_handle);
150 if (io_result != IO_SUCCESS) {
151 WARN("Failed to access image '%s' (%i)\n",
152 image_name, io_result);
153 return 0;
154 }
155
156 /* Find the size of the image */
157 io_result = io_size(image_handle, &image_size);
158 if ((io_result != IO_SUCCESS) || (image_size == 0)) {
159 WARN("Failed to determine the size of the image '%s' file (%i)\n",
160 image_name, io_result);
161 }
162 io_result = io_close(image_handle);
163 /* Ignore improbable/unrecoverable error in 'close' */
164
165 /* TODO: Consider maintaining open device connection from this
166 * bootloader stage
167 */
168 io_result = io_dev_close(dev_handle);
169 /* Ignore improbable/unrecoverable error in 'dev_close' */
170
171 return image_size;
172}
Achin Gupta4f6ad662013-10-25 09:08:21 +0100173/*******************************************************************************
James Morrissey9d72b4e2014-02-10 17:04:32 +0000174 * Generic function to load an image into the trusted RAM,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100175 * given a name, extents of free memory & whether the image should be loaded at
176 * the bottom or top of the free memory. It updates the memory layout if the
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100177 * load is successful. It also updates the image information and the entry point
Sandrine Bailleux63db7ba2014-05-28 11:31:18 +0100178 * information in the params passed. The caller might pass a NULL pointer for
179 * the entry point if it is not interested in this information, e.g. because
180 * the image just needs to be loaded in memory but won't ever be executed.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100181 ******************************************************************************/
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100182int load_image(meminfo_t *mem_layout,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100183 const char *image_name,
184 unsigned int load_type,
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100185 unsigned long fixed_addr,
186 image_info_t *image_data,
187 entry_point_info_t *entry_point_info)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100188{
Dan Handley625de1d2014-04-23 13:47:06 +0100189 uintptr_t dev_handle;
190 uintptr_t image_handle;
191 uintptr_t image_spec;
James Morrissey40a6f642014-02-10 14:24:36 +0000192 unsigned long temp_image_base = 0;
193 unsigned long image_base = 0;
194 long offset = 0;
James Morrissey9d72b4e2014-02-10 17:04:32 +0000195 size_t image_size = 0;
196 size_t bytes_read = 0;
197 int io_result = IO_FAIL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100198
James Morrissey9d72b4e2014-02-10 17:04:32 +0000199 assert(mem_layout != NULL);
200 assert(image_name != NULL);
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100201 assert(image_data->h.version >= VERSION_1);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000202
203 /* Obtain a reference to the image by querying the platform layer */
204 io_result = plat_get_image_source(image_name, &dev_handle, &image_spec);
205 if (io_result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000206 WARN("Failed to obtain reference to image '%s' (%i)\n",
James Morrissey9d72b4e2014-02-10 17:04:32 +0000207 image_name, io_result);
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100208 return io_result;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100209 }
210
James Morrissey9d72b4e2014-02-10 17:04:32 +0000211 /* Attempt to access the image */
212 io_result = io_open(dev_handle, image_spec, &image_handle);
213 if (io_result != IO_SUCCESS) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000214 WARN("Failed to access image '%s' (%i)\n",
James Morrissey9d72b4e2014-02-10 17:04:32 +0000215 image_name, io_result);
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100216 return io_result;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100217 }
218
James Morrissey9d72b4e2014-02-10 17:04:32 +0000219 /* Find the size of the image */
220 io_result = io_size(image_handle, &image_size);
221 if ((io_result != IO_SUCCESS) || (image_size == 0)) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000222 WARN("Failed to determine the size of the image '%s' file (%i)\n",
James Morrissey9d72b4e2014-02-10 17:04:32 +0000223 image_name, io_result);
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100224 goto exit;
James Morrissey9d72b4e2014-02-10 17:04:32 +0000225 }
226
227 /* See if we have enough space */
228 if (image_size > mem_layout->free_size) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000229 WARN("Cannot load '%s' file: Not enough space.\n",
James Morrissey9d72b4e2014-02-10 17:04:32 +0000230 image_name);
231 dump_load_info(0, image_size, mem_layout);
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100232 goto exit;
James Morrissey9d72b4e2014-02-10 17:04:32 +0000233 }
234
Achin Gupta4f6ad662013-10-25 09:08:21 +0100235 switch (load_type) {
236
237 case TOP_LOAD:
238
239 /* Load the image in the top of free memory */
240 temp_image_base = mem_layout->free_base + mem_layout->free_size;
James Morrissey9d72b4e2014-02-10 17:04:32 +0000241 temp_image_base -= image_size;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100242
243 /* Page align base address and check whether the image still fits */
244 image_base = page_align(temp_image_base, DOWN);
245 assert(image_base <= temp_image_base);
246
247 if (image_base < mem_layout->free_base) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000248 WARN("Cannot load '%s' file: Not enough space.\n",
James Morrissey9d72b4e2014-02-10 17:04:32 +0000249 image_name);
250 dump_load_info(image_base, image_size, mem_layout);
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100251 io_result = -ENOMEM;
252 goto exit;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100253 }
254
255 /* Calculate the amount of extra memory used due to alignment */
256 offset = temp_image_base - image_base;
257
258 break;
259
260 case BOT_LOAD:
261
262 /* Load the BL2 image in the bottom of free memory */
263 temp_image_base = mem_layout->free_base;
264 image_base = page_align(temp_image_base, UP);
265 assert(image_base >= temp_image_base);
266
267 /* Page align base address and check whether the image still fits */
James Morrissey9d72b4e2014-02-10 17:04:32 +0000268 if (image_base + image_size >
Achin Gupta4f6ad662013-10-25 09:08:21 +0100269 mem_layout->free_base + mem_layout->free_size) {
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100270 WARN("Cannot load '%s' file: Not enough space.\n",
271 image_name);
272 dump_load_info(image_base, image_size, mem_layout);
273 io_result = -ENOMEM;
274 goto exit;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100275 }
276
277 /* Calculate the amount of extra memory used due to alignment */
278 offset = image_base - temp_image_base;
279
280 break;
281
282 default:
283 assert(0);
284
285 }
286
287 /*
288 * Some images must be loaded at a fixed address, not a dynamic one.
289 *
290 * This has been implemented as a hack on top of the existing dynamic
291 * loading mechanism, for the time being. If the 'fixed_addr' function
292 * argument is different from zero, then it will force the load address.
293 * So we still have this principle of top/bottom loading but the code
294 * determining the load address is bypassed and the load address is
295 * forced to the fixed one.
296 *
297 * This can result in quite a lot of wasted space because we still use
298 * 1 sole meminfo structure to represent the extents of free memory,
299 * where we should use some sort of linked list.
300 *
301 * E.g. we want to load BL2 at address 0x04020000, the resulting memory
302 * layout should look as follows:
303 * ------------ 0x04040000
304 * | | <- Free space (1)
305 * |----------|
306 * | BL2 |
307 * |----------| 0x04020000
308 * | | <- Free space (2)
309 * |----------|
310 * | BL1 |
311 * ------------ 0x04000000
312 *
313 * But in the current hacky implementation, we'll need to specify
314 * whether BL2 is loaded at the top or bottom of the free memory.
315 * E.g. if BL2 is considered as top-loaded, the meminfo structure
316 * will give the following view of the memory, hiding the chunk of
317 * free memory above BL2:
318 * ------------ 0x04040000
319 * | |
320 * | |
321 * | BL2 |
322 * |----------| 0x04020000
323 * | | <- Free space (2)
324 * |----------|
325 * | BL1 |
326 * ------------ 0x04000000
327 */
328 if (fixed_addr != 0) {
329 /* Load the image at the given address. */
330 image_base = fixed_addr;
331
332 /* Check whether the image fits. */
333 if ((image_base < mem_layout->free_base) ||
James Morrissey9d72b4e2014-02-10 17:04:32 +0000334 (image_base + image_size >
Achin Gupta4f6ad662013-10-25 09:08:21 +0100335 mem_layout->free_base + mem_layout->free_size)) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000336 WARN("Cannot load '%s' file: Not enough space.\n",
Achin Gupta4f6ad662013-10-25 09:08:21 +0100337 image_name);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000338 dump_load_info(image_base, image_size, mem_layout);
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100339 io_result = -ENOMEM;
340 goto exit;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100341 }
342
343 /* Check whether the fixed load address is page-aligned. */
344 if (!is_page_aligned(image_base)) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000345 WARN("Cannot load '%s' file at unaligned address 0x%lx\n",
Achin Gupta4f6ad662013-10-25 09:08:21 +0100346 image_name, fixed_addr);
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100347 io_result = -ENOMEM;
348 goto exit;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100349 }
350
351 /*
352 * Calculate the amount of extra memory used due to fixed
353 * loading.
354 */
355 if (load_type == TOP_LOAD) {
356 unsigned long max_addr, space_used;
357 /*
358 * ------------ max_addr
359 * | /wasted/ | | offset
360 * |..........|..............................
361 * | image | | image_flen
362 * |----------| fixed_addr
363 * | |
364 * | |
365 * ------------ total_base
366 */
367 max_addr = mem_layout->total_base + mem_layout->total_size;
368 /*
369 * Compute the amount of memory used by the image.
370 * Corresponds to all space above the image load
371 * address.
372 */
373 space_used = max_addr - fixed_addr;
374 /*
375 * Calculate the amount of wasted memory within the
376 * amount of memory used by the image.
377 */
James Morrissey9d72b4e2014-02-10 17:04:32 +0000378 offset = space_used - image_size;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100379 } else /* BOT_LOAD */
380 /*
381 * ------------
382 * | |
383 * | |
384 * |----------|
385 * | image |
386 * |..........| fixed_addr
387 * | /wasted/ | | offset
388 * ------------ total_base
389 */
390 offset = fixed_addr - mem_layout->total_base;
391 }
392
393 /* We have enough space so load the image now */
James Morrissey9d72b4e2014-02-10 17:04:32 +0000394 /* TODO: Consider whether to try to recover/retry a partially successful read */
Dan Handley625de1d2014-04-23 13:47:06 +0100395 io_result = io_read(image_handle, image_base, image_size, &bytes_read);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000396 if ((io_result != IO_SUCCESS) || (bytes_read < image_size)) {
Jeenu Viswambharan08c28d52014-02-20 12:03:31 +0000397 WARN("Failed to load '%s' file (%i)\n", image_name, io_result);
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100398 goto exit;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100399 }
400
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100401 image_data->image_base = image_base;
402 image_data->image_size = image_size;
403
Sandrine Bailleux63db7ba2014-05-28 11:31:18 +0100404 if (entry_point_info != NULL)
405 entry_point_info->pc = image_base;
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100406
Achin Gupta4f6ad662013-10-25 09:08:21 +0100407 /*
408 * File has been successfully loaded. Update the free memory
409 * data structure & flush the contents of the TZRAM so that
410 * the next EL can see it.
411 */
412 /* Update the memory contents */
James Morrissey9d72b4e2014-02-10 17:04:32 +0000413 flush_dcache_range(image_base, image_size);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100414
James Morrissey9d72b4e2014-02-10 17:04:32 +0000415 mem_layout->free_size -= image_size + offset;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100416
417 /* Update the base of free memory since its moved up */
418 if (load_type == BOT_LOAD)
James Morrissey9d72b4e2014-02-10 17:04:32 +0000419 mem_layout->free_base += offset + image_size;
420
421exit:
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100422 io_close(image_handle);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000423 /* Ignore improbable/unrecoverable error in 'close' */
424
425 /* TODO: Consider maintaining open device connection from this bootloader stage */
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100426 io_dev_close(dev_handle);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000427 /* Ignore improbable/unrecoverable error in 'dev_close' */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100428
Vikram Kanigiri4112bfa2014-04-15 18:08:08 +0100429 return io_result;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100430}