blob: da4107b6a6ce6064ba07e806feadca5541f0fc3c [file] [log] [blame]
Yatharth Kochar736a3bf2015-10-11 14:14:55 +01001/*
Yatharth Kocharf11b29a2016-02-01 11:04:46 +00002 * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
Yatharth Kochar736a3bf2015-10-11 14:14:55 +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
31#include <assert.h>
32#include <bl_common.h>
33#include <debug.h>
34#include <errno.h>
35#include <plat_arm.h>
Yatharth Kocharf11b29a2016-02-01 11:04:46 +000036#include <platform_def.h>
Yatharth Kochar736a3bf2015-10-11 14:14:55 +010037#include <tbbr_img_desc.h>
Sandrine Bailleuxb39d75f2016-11-11 16:44:37 +000038#include <utils.h>
Yatharth Kochar736a3bf2015-10-11 14:14:55 +010039
40/* Struct to keep track of usable memory */
Yatharth Kocharf11b29a2016-02-01 11:04:46 +000041typedef struct bl1_mem_info {
Yatharth Kochar736a3bf2015-10-11 14:14:55 +010042 uintptr_t mem_base;
43 unsigned int mem_size;
44} bl1_mem_info_t;
45
46bl1_mem_info_t fwu_addr_map_secure[] = {
47 {
48 .mem_base = ARM_SHARED_RAM_BASE,
49 .mem_size = ARM_SHARED_RAM_SIZE
50 },
51 {
52 .mem_size = 0
53 }
54};
55
56bl1_mem_info_t fwu_addr_map_non_secure[] = {
57 {
58 .mem_base = ARM_NS_DRAM1_BASE,
59 .mem_size = ARM_NS_DRAM1_SIZE
60 },
61 {
Yatharth Kocharf11b29a2016-02-01 11:04:46 +000062 .mem_base = PLAT_ARM_NVM_BASE,
63 .mem_size = PLAT_ARM_NVM_SIZE
Yatharth Kochar736a3bf2015-10-11 14:14:55 +010064 },
65 {
66 .mem_size = 0
67 }
68};
69
70int bl1_plat_mem_check(uintptr_t mem_base,
71 unsigned int mem_size,
72 unsigned int flags)
73{
74 unsigned int index = 0;
75 bl1_mem_info_t *mmap;
76
77 assert(mem_base);
78 assert(mem_size);
Sandrine Bailleuxb39d75f2016-11-11 16:44:37 +000079 /*
80 * The caller of this function is responsible for checking upfront that
81 * the end address doesn't overflow. We double-check this in debug
82 * builds.
83 */
84 assert(!check_uptr_overflow(mem_base, mem_size - 1));
Yatharth Kochar736a3bf2015-10-11 14:14:55 +010085
86 /*
87 * Check the given image source and size.
88 */
Yatharth Kocharf11b29a2016-02-01 11:04:46 +000089 if (GET_SECURITY_STATE(flags) == SECURE)
Yatharth Kochar736a3bf2015-10-11 14:14:55 +010090 mmap = fwu_addr_map_secure;
91 else
92 mmap = fwu_addr_map_non_secure;
93
94 while (mmap[index].mem_size) {
95 if ((mem_base >= mmap[index].mem_base) &&
96 ((mem_base + mem_size)
97 <= (mmap[index].mem_base +
98 mmap[index].mem_size)))
99 return 0;
100
101 index++;
102 }
103
104 return -ENOMEM;
105}
106
107/*******************************************************************************
108 * This function does linear search for image_id and returns image_desc.
109 ******************************************************************************/
110image_desc_t *bl1_plat_get_image_desc(unsigned int image_id)
111{
112 unsigned int index = 0;
113
114 while (bl1_tbbr_image_descs[index].image_id != INVALID_IMAGE_ID) {
115 if (bl1_tbbr_image_descs[index].image_id == image_id)
116 return &bl1_tbbr_image_descs[index];
117 index++;
118 }
119
120 return NULL;
121}