1 /* 2 * Copyright (c) 2013-2017, 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 #ifndef __BL_COMMON_H__ 32 #define __BL_COMMON_H__ 33 34 #include <ep_info.h> 35 #include <param_header.h> 36 37 #define UP 1 38 #define DOWN 0 39 40 /******************************************************************************* 41 * Constants to identify the location of a memory region in a given memory 42 * layout. 43 ******************************************************************************/ 44 #define TOP 0x1 45 #define BOTTOM !TOP 46 47 /* 48 * The following are used for image state attributes. 49 * Image can only be in one of the following state. 50 */ 51 #define IMAGE_STATE_RESET 0 52 #define IMAGE_STATE_COPIED 1 53 #define IMAGE_STATE_COPYING 2 54 #define IMAGE_STATE_AUTHENTICATED 3 55 #define IMAGE_STATE_EXECUTED 4 56 #define IMAGE_STATE_INTERRUPTED 5 57 58 #define IMAGE_ATTRIB_SKIP_LOADING 0x02 59 #define IMAGE_ATTRIB_PLAT_SETUP 0x04 60 61 #define INVALID_IMAGE_ID (0xFFFFFFFF) 62 63 /******************************************************************************* 64 * Constants to indicate type of exception to the common exception handler. 65 ******************************************************************************/ 66 #define SYNC_EXCEPTION_SP_EL0 0x0 67 #define IRQ_SP_EL0 0x1 68 #define FIQ_SP_EL0 0x2 69 #define SERROR_SP_EL0 0x3 70 #define SYNC_EXCEPTION_SP_ELX 0x4 71 #define IRQ_SP_ELX 0x5 72 #define FIQ_SP_ELX 0x6 73 #define SERROR_SP_ELX 0x7 74 #define SYNC_EXCEPTION_AARCH64 0x8 75 #define IRQ_AARCH64 0x9 76 #define FIQ_AARCH64 0xa 77 #define SERROR_AARCH64 0xb 78 #define SYNC_EXCEPTION_AARCH32 0xc 79 #define IRQ_AARCH32 0xd 80 #define FIQ_AARCH32 0xe 81 #define SERROR_AARCH32 0xf 82 83 #ifndef __ASSEMBLY__ 84 #include <cassert.h> 85 #include <stddef.h> 86 #include <stdint.h> 87 #include <types.h> 88 #include <utils_def.h> /* To retain compatibility */ 89 90 /* 91 * Declarations of linker defined symbols to help determine memory layout of 92 * BL images 93 */ 94 #if SEPARATE_CODE_AND_RODATA 95 extern uintptr_t __TEXT_START__; 96 extern uintptr_t __TEXT_END__; 97 extern uintptr_t __RODATA_START__; 98 extern uintptr_t __RODATA_END__; 99 #else 100 extern uintptr_t __RO_START__; 101 extern uintptr_t __RO_END__; 102 #endif 103 104 #if defined(IMAGE_BL2) 105 extern uintptr_t __BL2_END__; 106 #elif defined(IMAGE_BL2U) 107 extern uintptr_t __BL2U_END__; 108 #elif defined(IMAGE_BL31) 109 extern uintptr_t __BL31_END__; 110 #elif defined(IMAGE_BL32) 111 extern uintptr_t __BL32_END__; 112 #endif /* IMAGE_BLX */ 113 114 #if USE_COHERENT_MEM 115 extern uintptr_t __COHERENT_RAM_START__; 116 extern uintptr_t __COHERENT_RAM_END__; 117 #endif 118 119 /******************************************************************************* 120 * Structure used for telling the next BL how much of a particular type of 121 * memory is available for its use and how much is already used. 122 ******************************************************************************/ 123 typedef struct meminfo { 124 uintptr_t total_base; 125 size_t total_size; 126 #if !LOAD_IMAGE_V2 127 uintptr_t free_base; 128 size_t free_size; 129 #endif 130 } meminfo_t; 131 132 /***************************************************************************** 133 * Image info binary provides information from the image loader that 134 * can be used by the firmware to manage available trusted RAM. 135 * More advanced firmware image formats can provide additional 136 * information that enables optimization or greater flexibility in the 137 * common firmware code 138 *****************************************************************************/ 139 typedef struct image_info { 140 param_header_t h; 141 uintptr_t image_base; /* physical address of base of image */ 142 uint32_t image_size; /* bytes read from image file */ 143 #if LOAD_IMAGE_V2 144 uint32_t image_max_size; 145 #endif 146 } image_info_t; 147 148 /***************************************************************************** 149 * The image descriptor struct definition. 150 *****************************************************************************/ 151 typedef struct image_desc { 152 /* Contains unique image id for the image. */ 153 unsigned int image_id; 154 /* 155 * This member contains Image state information. 156 * Refer IMAGE_STATE_XXX defined above. 157 */ 158 unsigned int state; 159 uint32_t copied_size; /* image size copied in blocks */ 160 image_info_t image_info; 161 entry_point_info_t ep_info; 162 } image_desc_t; 163 164 #if LOAD_IMAGE_V2 165 /* BL image node in the BL image loading sequence */ 166 typedef struct bl_load_info_node { 167 unsigned int image_id; 168 image_info_t *image_info; 169 struct bl_load_info_node *next_load_info; 170 } bl_load_info_node_t; 171 172 /* BL image head node in the BL image loading sequence */ 173 typedef struct bl_load_info { 174 param_header_t h; 175 bl_load_info_node_t *head; 176 } bl_load_info_t; 177 178 /* BL image node in the BL image execution sequence */ 179 typedef struct bl_params_node { 180 unsigned int image_id; 181 image_info_t *image_info; 182 entry_point_info_t *ep_info; 183 struct bl_params_node *next_params_info; 184 } bl_params_node_t; 185 186 /* 187 * BL image head node in the BL image execution sequence 188 * It is also used to pass information to next BL image. 189 */ 190 typedef struct bl_params { 191 param_header_t h; 192 bl_params_node_t *head; 193 } bl_params_t; 194 195 #else /* LOAD_IMAGE_V2 */ 196 197 /******************************************************************************* 198 * This structure represents the superset of information that can be passed to 199 * BL31 e.g. while passing control to it from BL2. The BL32 parameters will be 200 * populated only if BL2 detects its presence. A pointer to a structure of this 201 * type should be passed in X0 to BL31's cold boot entrypoint. 202 * 203 * Use of this structure and the X0 parameter is not mandatory: the BL31 204 * platform code can use other mechanisms to provide the necessary information 205 * about BL32 and BL33 to the common and SPD code. 206 * 207 * BL31 image information is mandatory if this structure is used. If either of 208 * the optional BL32 and BL33 image information is not provided, this is 209 * indicated by the respective image_info pointers being zero. 210 ******************************************************************************/ 211 typedef struct bl31_params { 212 param_header_t h; 213 image_info_t *bl31_image_info; 214 entry_point_info_t *bl32_ep_info; 215 image_info_t *bl32_image_info; 216 entry_point_info_t *bl33_ep_info; 217 image_info_t *bl33_image_info; 218 } bl31_params_t; 219 220 #endif /* LOAD_IMAGE_V2 */ 221 222 /******************************************************************************* 223 * Function & variable prototypes 224 ******************************************************************************/ 225 size_t image_size(unsigned int image_id); 226 227 int is_mem_free(uintptr_t free_base, size_t free_size, 228 uintptr_t addr, size_t size); 229 230 #if LOAD_IMAGE_V2 231 232 int load_image(unsigned int image_id, image_info_t *image_data); 233 int load_auth_image(unsigned int image_id, image_info_t *image_data); 234 235 #else 236 237 uintptr_t page_align(uintptr_t, unsigned); 238 int load_image(meminfo_t *mem_layout, 239 unsigned int image_id, 240 uintptr_t image_base, 241 image_info_t *image_data, 242 entry_point_info_t *entry_point_info); 243 int load_auth_image(meminfo_t *mem_layout, 244 unsigned int image_id, 245 uintptr_t image_base, 246 image_info_t *image_data, 247 entry_point_info_t *entry_point_info); 248 void reserve_mem(uintptr_t *free_base, size_t *free_size, 249 uintptr_t addr, size_t size); 250 251 #endif /* LOAD_IMAGE_V2 */ 252 253 extern const char build_message[]; 254 extern const char version_string[]; 255 256 void print_entry_point_info(const entry_point_info_t *ep_info); 257 258 #endif /*__ASSEMBLY__*/ 259 260 #endif /* __BL_COMMON_H__ */ 261