xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_fwu.c (revision 3105f7ba9a3a9f6f0e78761e8bdd4da621254730)
1 /*
2  * Copyright (c) 2015, 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 <tbbr_img_desc.h>
37 
38 
39 /* Struct to keep track of usable memory */
40 typedef struct bl1_mem_info{
41 	uintptr_t mem_base;
42 	unsigned int mem_size;
43 } bl1_mem_info_t;
44 
45 bl1_mem_info_t fwu_addr_map_secure[] = {
46 	{
47 		.mem_base = ARM_SHARED_RAM_BASE,
48 		.mem_size = ARM_SHARED_RAM_SIZE
49 	},
50 	{
51 		.mem_size = 0
52 	}
53 };
54 
55 bl1_mem_info_t fwu_addr_map_non_secure[] = {
56 	{
57 		.mem_base = ARM_NS_DRAM1_BASE,
58 		.mem_size = ARM_NS_DRAM1_SIZE
59 	},
60 	{
61 		.mem_base = V2M_FLASH0_BASE,
62 		.mem_size = V2M_FLASH0_SIZE
63 	},
64 	{
65 		.mem_size = 0
66 	}
67 };
68 
69 int bl1_plat_mem_check(uintptr_t mem_base,
70 		unsigned int mem_size,
71 		unsigned int flags)
72 {
73 	unsigned int index = 0;
74 	bl1_mem_info_t *mmap;
75 
76 	assert(mem_base);
77 	assert(mem_size);
78 
79 	/*
80 	 * Check the given image source and size.
81 	 */
82 	if (GET_SEC_STATE(flags) == SECURE)
83 		mmap = fwu_addr_map_secure;
84 	else
85 		mmap = fwu_addr_map_non_secure;
86 
87 	while (mmap[index].mem_size) {
88 		if ((mem_base >= mmap[index].mem_base) &&
89 			((mem_base + mem_size)
90 			<= (mmap[index].mem_base +
91 			mmap[index].mem_size)))
92 			return 0;
93 
94 		index++;
95 	}
96 
97 	return -ENOMEM;
98 }
99 
100 /*******************************************************************************
101  * This function does linear search for image_id and returns image_desc.
102  ******************************************************************************/
103 image_desc_t *bl1_plat_get_image_desc(unsigned int image_id)
104 {
105 	unsigned int index = 0;
106 
107 	while (bl1_tbbr_image_descs[index].image_id != INVALID_IMAGE_ID) {
108 		if (bl1_tbbr_image_descs[index].image_id == image_id)
109 			return &bl1_tbbr_image_descs[index];
110 		index++;
111 	}
112 
113 	return NULL;
114 }
115