xref: /rk3399_ARM-atf/plat/arm/common/arm_bl1_fwu.c (revision a65007fee6131f372b66cdb6e6544e73cf2cff94)
1 /*
2  * Copyright (c) 2015-2026, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <errno.h>
9 #include <inttypes.h>
10 
11 #include <platform_def.h>
12 
13 #include <bl1/tbbr/tbbr_img_desc.h>
14 #include <common/bl_common.h>
15 #include <common/debug.h>
16 #include <lib/cassert.h>
17 #include <lib/utils.h>
18 #include <plat/arm/common/plat_arm.h>
19 #include <plat/common/platform.h>
20 
21 #pragma weak bl1_plat_get_image_desc
22 
23 /* Compile-time checks for static FWU map entries. */
24 CASSERT(!check_uptr_overflow(ARM_SHARED_RAM_BASE, ARM_SHARED_RAM_SIZE),
25 	assert_fwu_secure_ram_overflow);
26 CASSERT(!check_uptr_overflow(ARM_NS_DRAM1_BASE, ARM_NS_DRAM1_SIZE),
27 	assert_fwu_ns_dram1_overflow);
28 CASSERT(!check_uptr_overflow(PLAT_ARM_NVM_BASE, PLAT_ARM_NVM_SIZE),
29 	assert_fwu_ns_nvm_overflow);
30 
31 /* Struct to keep track of usable memory */
32 typedef struct bl1_mem_info {
33 	uintptr_t mem_base;
34 	unsigned int mem_size;
35 } bl1_mem_info_t;
36 
37 static const bl1_mem_info_t fwu_addr_map_secure[] = {
38 	{
39 		.mem_base = ARM_SHARED_RAM_BASE,
40 		.mem_size = ARM_SHARED_RAM_SIZE
41 	},
42 	{
43 		.mem_size = 0
44 	}
45 };
46 
47 static const bl1_mem_info_t fwu_addr_map_non_secure[] = {
48 	{
49 		.mem_base = ARM_NS_DRAM1_BASE,
50 		.mem_size = ARM_NS_DRAM1_SIZE
51 	},
52 	{
53 		.mem_base = PLAT_ARM_NVM_BASE,
54 		.mem_size = PLAT_ARM_NVM_SIZE
55 	},
56 	{
57 		.mem_size = 0
58 	}
59 };
60 
bl1_plat_mem_check(uintptr_t mem_base,unsigned int mem_size,unsigned int flags)61 int bl1_plat_mem_check(uintptr_t mem_base,
62 		unsigned int mem_size,
63 		unsigned int flags)
64 {
65 	unsigned int index = 0;
66 	const bl1_mem_info_t *mmap;
67 	uintptr_t end;
68 	uintptr_t region_end;
69 
70 	/*
71 	 * The caller of this function is responsible for checking upfront that
72 	 * the end address doesn't overflow builds. We double check this.
73 	 */
74 	if ((mem_base == 0U) || (mem_size == 0U) ||
75 	    check_uptr_overflow(mem_base, mem_size)) {
76 		return -ENOMEM;
77 	}
78 
79 	end = mem_base + mem_size;
80 
81 	/*
82 	 * Check the given image source and size.
83 	 */
84 	if (GET_SECURITY_STATE(flags) == SECURE) {
85 		mmap = fwu_addr_map_secure;
86 	} else {
87 		mmap = fwu_addr_map_non_secure;
88 	}
89 
90 	while (mmap[index].mem_size > 0U) {
91 		region_end = mmap[index].mem_base + mmap[index].mem_size;
92 
93 		if ((mem_base >= mmap[index].mem_base) &&
94 		    (end <= region_end)) {
95 			return 0;
96 		}
97 
98 		index++;
99 	}
100 
101 	return -ENOMEM;
102 }
103 
104 /*******************************************************************************
105  * This function does linear search for image_id and returns image_desc.
106  ******************************************************************************/
bl1_plat_get_image_desc(unsigned int image_id)107 image_desc_t *bl1_plat_get_image_desc(unsigned int image_id)
108 {
109 	unsigned int index = 0;
110 
111 	while (bl1_tbbr_image_descs[index].image_id != INVALID_IMAGE_ID) {
112 		if (bl1_tbbr_image_descs[index].image_id == image_id)
113 			return &bl1_tbbr_image_descs[index];
114 		index++;
115 	}
116 
117 	return NULL;
118 }
119