xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_fwu.c (revision 51faada71a219a8b94cd8d8e423f0f22e9da4d8f)
1 /*
2  * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
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 
31 #include <assert.h>
32 #include <bl_common.h>
33 #include <debug.h>
34 #include <errno.h>
35 #include <plat_arm.h>
36 #include <platform_def.h>
37 #include <tbbr_img_desc.h>
38 #include <utils.h>
39 
40 /* Struct to keep track of usable memory */
41 typedef struct bl1_mem_info {
42 	uintptr_t mem_base;
43 	unsigned int mem_size;
44 } bl1_mem_info_t;
45 
46 bl1_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 
56 bl1_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 	{
62 		.mem_base = PLAT_ARM_NVM_BASE,
63 		.mem_size = PLAT_ARM_NVM_SIZE
64 	},
65 	{
66 		.mem_size = 0
67 	}
68 };
69 
70 int 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);
79 	/*
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));
85 
86 	/*
87 	 * Check the given image source and size.
88 	 */
89 	if (GET_SECURITY_STATE(flags) == SECURE)
90 		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  ******************************************************************************/
110 image_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 }
122