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