1436223deSYatharth Kochar /* 2*3b94189aSRoberto Vargas * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 3436223deSYatharth Kochar * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5436223deSYatharth Kochar */ 6436223deSYatharth Kochar 7436223deSYatharth Kochar #include <assert.h> 8436223deSYatharth Kochar #include <bl_common.h> 9436223deSYatharth Kochar #include <debug.h> 10436223deSYatharth Kochar #include <errno.h> 11436223deSYatharth Kochar #include <plat_arm.h> 12*3b94189aSRoberto Vargas #include <platform.h> 13843ddee4SYatharth Kochar #include <platform_def.h> 14436223deSYatharth Kochar #include <tbbr_img_desc.h> 15949a52d2SSandrine Bailleux #include <utils.h> 16436223deSYatharth Kochar 17436223deSYatharth Kochar /* Struct to keep track of usable memory */ 18436223deSYatharth Kochar typedef struct bl1_mem_info { 19436223deSYatharth Kochar uintptr_t mem_base; 20436223deSYatharth Kochar unsigned int mem_size; 21436223deSYatharth Kochar } bl1_mem_info_t; 22436223deSYatharth Kochar 23*3b94189aSRoberto Vargas static bl1_mem_info_t fwu_addr_map_secure[] = { 24436223deSYatharth Kochar { 25436223deSYatharth Kochar .mem_base = ARM_SHARED_RAM_BASE, 26436223deSYatharth Kochar .mem_size = ARM_SHARED_RAM_SIZE 27436223deSYatharth Kochar }, 28436223deSYatharth Kochar { 29436223deSYatharth Kochar .mem_size = 0 30436223deSYatharth Kochar } 31436223deSYatharth Kochar }; 32436223deSYatharth Kochar 33*3b94189aSRoberto Vargas static bl1_mem_info_t fwu_addr_map_non_secure[] = { 34436223deSYatharth Kochar { 35436223deSYatharth Kochar .mem_base = ARM_NS_DRAM1_BASE, 36436223deSYatharth Kochar .mem_size = ARM_NS_DRAM1_SIZE 37436223deSYatharth Kochar }, 38436223deSYatharth Kochar { 39843ddee4SYatharth Kochar .mem_base = PLAT_ARM_NVM_BASE, 40843ddee4SYatharth Kochar .mem_size = PLAT_ARM_NVM_SIZE 41436223deSYatharth Kochar }, 42436223deSYatharth Kochar { 43436223deSYatharth Kochar .mem_size = 0 44436223deSYatharth Kochar } 45436223deSYatharth Kochar }; 46436223deSYatharth Kochar 47436223deSYatharth Kochar int bl1_plat_mem_check(uintptr_t mem_base, 48436223deSYatharth Kochar unsigned int mem_size, 49436223deSYatharth Kochar unsigned int flags) 50436223deSYatharth Kochar { 51436223deSYatharth Kochar unsigned int index = 0; 52436223deSYatharth Kochar bl1_mem_info_t *mmap; 53436223deSYatharth Kochar 54436223deSYatharth Kochar assert(mem_base); 55436223deSYatharth Kochar assert(mem_size); 56949a52d2SSandrine Bailleux /* 57949a52d2SSandrine Bailleux * The caller of this function is responsible for checking upfront that 58949a52d2SSandrine Bailleux * the end address doesn't overflow. We double-check this in debug 59949a52d2SSandrine Bailleux * builds. 60949a52d2SSandrine Bailleux */ 61949a52d2SSandrine Bailleux assert(!check_uptr_overflow(mem_base, mem_size - 1)); 62436223deSYatharth Kochar 63436223deSYatharth Kochar /* 64436223deSYatharth Kochar * Check the given image source and size. 65436223deSYatharth Kochar */ 66843ddee4SYatharth Kochar if (GET_SECURITY_STATE(flags) == SECURE) 67436223deSYatharth Kochar mmap = fwu_addr_map_secure; 68436223deSYatharth Kochar else 69436223deSYatharth Kochar mmap = fwu_addr_map_non_secure; 70436223deSYatharth Kochar 71436223deSYatharth Kochar while (mmap[index].mem_size) { 72436223deSYatharth Kochar if ((mem_base >= mmap[index].mem_base) && 73436223deSYatharth Kochar ((mem_base + mem_size) 74436223deSYatharth Kochar <= (mmap[index].mem_base + 75436223deSYatharth Kochar mmap[index].mem_size))) 76436223deSYatharth Kochar return 0; 77436223deSYatharth Kochar 78436223deSYatharth Kochar index++; 79436223deSYatharth Kochar } 80436223deSYatharth Kochar 81436223deSYatharth Kochar return -ENOMEM; 82436223deSYatharth Kochar } 83436223deSYatharth Kochar 84436223deSYatharth Kochar /******************************************************************************* 85436223deSYatharth Kochar * This function does linear search for image_id and returns image_desc. 86436223deSYatharth Kochar ******************************************************************************/ 87436223deSYatharth Kochar image_desc_t *bl1_plat_get_image_desc(unsigned int image_id) 88436223deSYatharth Kochar { 89436223deSYatharth Kochar unsigned int index = 0; 90436223deSYatharth Kochar 91436223deSYatharth Kochar while (bl1_tbbr_image_descs[index].image_id != INVALID_IMAGE_ID) { 92436223deSYatharth Kochar if (bl1_tbbr_image_descs[index].image_id == image_id) 93436223deSYatharth Kochar return &bl1_tbbr_image_descs[index]; 94436223deSYatharth Kochar index++; 95436223deSYatharth Kochar } 96436223deSYatharth Kochar 97436223deSYatharth Kochar return NULL; 98436223deSYatharth Kochar } 99