1 /*
2 * Copyright (c) 2015-2018, 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
10 #include <platform_def.h>
11
12 #include <bl1/tbbr/tbbr_img_desc.h>
13 #include <common/bl_common.h>
14 #include <common/debug.h>
15 #include <lib/utils.h>
16 #include <plat/arm/common/plat_arm.h>
17 #include <plat/common/platform.h>
18
19 /* Struct to keep track of usable memory */
20 typedef struct bl1_mem_info {
21 uintptr_t mem_base;
22 unsigned int mem_size;
23 } bl1_mem_info_t;
24
25 static bl1_mem_info_t fwu_addr_map_secure[] = {
26 {
27 .mem_base = ARM_SHARED_RAM_BASE,
28 .mem_size = ARM_SHARED_RAM_SIZE
29 },
30 {
31 .mem_size = 0
32 }
33 };
34
35 static bl1_mem_info_t fwu_addr_map_non_secure[] = {
36 {
37 .mem_base = ARM_NS_DRAM1_BASE,
38 .mem_size = ARM_NS_DRAM1_SIZE
39 },
40 {
41 .mem_base = PLAT_ARM_NVM_BASE,
42 .mem_size = PLAT_ARM_NVM_SIZE
43 },
44 {
45 .mem_size = 0
46 }
47 };
48
bl1_plat_mem_check(uintptr_t mem_base,unsigned int mem_size,unsigned int flags)49 int bl1_plat_mem_check(uintptr_t mem_base,
50 unsigned int mem_size,
51 unsigned int flags)
52 {
53 unsigned int index = 0;
54 bl1_mem_info_t *mmap;
55
56 assert(mem_base);
57 assert(mem_size);
58 /*
59 * The caller of this function is responsible for checking upfront that
60 * the end address doesn't overflow. We double-check this in debug
61 * builds.
62 */
63 assert(!check_uptr_overflow(mem_base, mem_size - 1));
64
65 /*
66 * Check the given image source and size.
67 */
68 if (GET_SECURITY_STATE(flags) == SECURE)
69 mmap = fwu_addr_map_secure;
70 else
71 mmap = fwu_addr_map_non_secure;
72
73 while (mmap[index].mem_size) {
74 if ((mem_base >= mmap[index].mem_base) &&
75 ((mem_base + mem_size)
76 <= (mmap[index].mem_base +
77 mmap[index].mem_size)))
78 return 0;
79
80 index++;
81 }
82
83 return -ENOMEM;
84 }
85
86 /*******************************************************************************
87 * This function does linear search for image_id and returns image_desc.
88 ******************************************************************************/
bl1_plat_get_image_desc(unsigned int image_id)89 image_desc_t *bl1_plat_get_image_desc(unsigned int image_id)
90 {
91 unsigned int index = 0;
92
93 while (bl1_tbbr_image_descs[index].image_id != INVALID_IMAGE_ID) {
94 if (bl1_tbbr_image_descs[index].image_id == image_id)
95 return &bl1_tbbr_image_descs[index];
96 index++;
97 }
98
99 return NULL;
100 }
101