xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_fwu.c (revision 949a52d24eea48a58608645b6536ab7158abcbbb)
1436223deSYatharth Kochar /*
2843ddee4SYatharth Kochar  * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
3436223deSYatharth Kochar  *
4436223deSYatharth Kochar  * Redistribution and use in source and binary forms, with or without
5436223deSYatharth Kochar  * modification, are permitted provided that the following conditions are met:
6436223deSYatharth Kochar  *
7436223deSYatharth Kochar  * Redistributions of source code must retain the above copyright notice, this
8436223deSYatharth Kochar  * list of conditions and the following disclaimer.
9436223deSYatharth Kochar  *
10436223deSYatharth Kochar  * Redistributions in binary form must reproduce the above copyright notice,
11436223deSYatharth Kochar  * this list of conditions and the following disclaimer in the documentation
12436223deSYatharth Kochar  * and/or other materials provided with the distribution.
13436223deSYatharth Kochar  *
14436223deSYatharth Kochar  * Neither the name of ARM nor the names of its contributors may be used
15436223deSYatharth Kochar  * to endorse or promote products derived from this software without specific
16436223deSYatharth Kochar  * prior written permission.
17436223deSYatharth Kochar  *
18436223deSYatharth Kochar  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19436223deSYatharth Kochar  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20436223deSYatharth Kochar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21436223deSYatharth Kochar  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22436223deSYatharth Kochar  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23436223deSYatharth Kochar  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24436223deSYatharth Kochar  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25436223deSYatharth Kochar  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26436223deSYatharth Kochar  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27436223deSYatharth Kochar  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28436223deSYatharth Kochar  * POSSIBILITY OF SUCH DAMAGE.
29436223deSYatharth Kochar  */
30436223deSYatharth Kochar 
31436223deSYatharth Kochar #include <assert.h>
32436223deSYatharth Kochar #include <bl_common.h>
33436223deSYatharth Kochar #include <debug.h>
34436223deSYatharth Kochar #include <errno.h>
35436223deSYatharth Kochar #include <plat_arm.h>
36843ddee4SYatharth Kochar #include <platform_def.h>
37436223deSYatharth Kochar #include <tbbr_img_desc.h>
38*949a52d2SSandrine Bailleux #include <utils.h>
39436223deSYatharth Kochar 
40436223deSYatharth Kochar /* Struct to keep track of usable memory */
41436223deSYatharth Kochar typedef struct bl1_mem_info {
42436223deSYatharth Kochar 	uintptr_t mem_base;
43436223deSYatharth Kochar 	unsigned int mem_size;
44436223deSYatharth Kochar } bl1_mem_info_t;
45436223deSYatharth Kochar 
46436223deSYatharth Kochar bl1_mem_info_t fwu_addr_map_secure[] = {
47436223deSYatharth Kochar 	{
48436223deSYatharth Kochar 		.mem_base = ARM_SHARED_RAM_BASE,
49436223deSYatharth Kochar 		.mem_size = ARM_SHARED_RAM_SIZE
50436223deSYatharth Kochar 	},
51436223deSYatharth Kochar 	{
52436223deSYatharth Kochar 		.mem_size = 0
53436223deSYatharth Kochar 	}
54436223deSYatharth Kochar };
55436223deSYatharth Kochar 
56436223deSYatharth Kochar bl1_mem_info_t fwu_addr_map_non_secure[] = {
57436223deSYatharth Kochar 	{
58436223deSYatharth Kochar 		.mem_base = ARM_NS_DRAM1_BASE,
59436223deSYatharth Kochar 		.mem_size = ARM_NS_DRAM1_SIZE
60436223deSYatharth Kochar 	},
61436223deSYatharth Kochar 	{
62843ddee4SYatharth Kochar 		.mem_base = PLAT_ARM_NVM_BASE,
63843ddee4SYatharth Kochar 		.mem_size = PLAT_ARM_NVM_SIZE
64436223deSYatharth Kochar 	},
65436223deSYatharth Kochar 	{
66436223deSYatharth Kochar 		.mem_size = 0
67436223deSYatharth Kochar 	}
68436223deSYatharth Kochar };
69436223deSYatharth Kochar 
70436223deSYatharth Kochar int bl1_plat_mem_check(uintptr_t mem_base,
71436223deSYatharth Kochar 		unsigned int mem_size,
72436223deSYatharth Kochar 		unsigned int flags)
73436223deSYatharth Kochar {
74436223deSYatharth Kochar 	unsigned int index = 0;
75436223deSYatharth Kochar 	bl1_mem_info_t *mmap;
76436223deSYatharth Kochar 
77436223deSYatharth Kochar 	assert(mem_base);
78436223deSYatharth Kochar 	assert(mem_size);
79*949a52d2SSandrine Bailleux 	/*
80*949a52d2SSandrine Bailleux 	 * The caller of this function is responsible for checking upfront that
81*949a52d2SSandrine Bailleux 	 * the end address doesn't overflow. We double-check this in debug
82*949a52d2SSandrine Bailleux 	 * builds.
83*949a52d2SSandrine Bailleux 	 */
84*949a52d2SSandrine Bailleux 	assert(!check_uptr_overflow(mem_base, mem_size - 1));
85436223deSYatharth Kochar 
86436223deSYatharth Kochar 	/*
87436223deSYatharth Kochar 	 * Check the given image source and size.
88436223deSYatharth Kochar 	 */
89843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == SECURE)
90436223deSYatharth Kochar 		mmap = fwu_addr_map_secure;
91436223deSYatharth Kochar 	else
92436223deSYatharth Kochar 		mmap = fwu_addr_map_non_secure;
93436223deSYatharth Kochar 
94436223deSYatharth Kochar 	while (mmap[index].mem_size) {
95436223deSYatharth Kochar 		if ((mem_base >= mmap[index].mem_base) &&
96436223deSYatharth Kochar 			((mem_base + mem_size)
97436223deSYatharth Kochar 			<= (mmap[index].mem_base +
98436223deSYatharth Kochar 			mmap[index].mem_size)))
99436223deSYatharth Kochar 			return 0;
100436223deSYatharth Kochar 
101436223deSYatharth Kochar 		index++;
102436223deSYatharth Kochar 	}
103436223deSYatharth Kochar 
104436223deSYatharth Kochar 	return -ENOMEM;
105436223deSYatharth Kochar }
106436223deSYatharth Kochar 
107436223deSYatharth Kochar /*******************************************************************************
108436223deSYatharth Kochar  * This function does linear search for image_id and returns image_desc.
109436223deSYatharth Kochar  ******************************************************************************/
110436223deSYatharth Kochar image_desc_t *bl1_plat_get_image_desc(unsigned int image_id)
111436223deSYatharth Kochar {
112436223deSYatharth Kochar 	unsigned int index = 0;
113436223deSYatharth Kochar 
114436223deSYatharth Kochar 	while (bl1_tbbr_image_descs[index].image_id != INVALID_IMAGE_ID) {
115436223deSYatharth Kochar 		if (bl1_tbbr_image_descs[index].image_id == image_id)
116436223deSYatharth Kochar 			return &bl1_tbbr_image_descs[index];
117436223deSYatharth Kochar 		index++;
118436223deSYatharth Kochar 	}
119436223deSYatharth Kochar 
120436223deSYatharth Kochar 	return NULL;
121436223deSYatharth Kochar }
122