14ecca339SDan Handley /* 2843ddee4SYatharth Kochar * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. 34ecca339SDan Handley * 44ecca339SDan Handley * Redistribution and use in source and binary forms, with or without 54ecca339SDan Handley * modification, are permitted provided that the following conditions are met: 64ecca339SDan Handley * 74ecca339SDan Handley * Redistributions of source code must retain the above copyright notice, this 84ecca339SDan Handley * list of conditions and the following disclaimer. 94ecca339SDan Handley * 104ecca339SDan Handley * Redistributions in binary form must reproduce the above copyright notice, 114ecca339SDan Handley * this list of conditions and the following disclaimer in the documentation 124ecca339SDan Handley * and/or other materials provided with the distribution. 134ecca339SDan Handley * 144ecca339SDan Handley * Neither the name of ARM nor the names of its contributors may be used 154ecca339SDan Handley * to endorse or promote products derived from this software without specific 164ecca339SDan Handley * prior written permission. 174ecca339SDan Handley * 184ecca339SDan Handley * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 194ecca339SDan Handley * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 204ecca339SDan Handley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 214ecca339SDan Handley * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 224ecca339SDan Handley * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 234ecca339SDan Handley * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 244ecca339SDan Handley * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 254ecca339SDan Handley * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 264ecca339SDan Handley * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 274ecca339SDan Handley * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 284ecca339SDan Handley * POSSIBILITY OF SUCH DAMAGE. 294ecca339SDan Handley */ 304ecca339SDan Handley 314ecca339SDan Handley #ifndef __BL_COMMON_H__ 324ecca339SDan Handley #define __BL_COMMON_H__ 334ecca339SDan Handley 344112bfa0SVikram Kanigiri #define SECURE 0x0 354112bfa0SVikram Kanigiri #define NON_SECURE 0x1 36d3280bebSJuan Castillo #define sec_state_is_valid(s) (((s) == SECURE) || ((s) == NON_SECURE)) 374ecca339SDan Handley 384ecca339SDan Handley #define UP 1 394ecca339SDan Handley #define DOWN 0 404ecca339SDan Handley 414ecca339SDan Handley /******************************************************************************* 428f55dfb4SSandrine Bailleux * Constants to identify the location of a memory region in a given memory 438f55dfb4SSandrine Bailleux * layout. 444ecca339SDan Handley ******************************************************************************/ 458f55dfb4SSandrine Bailleux #define TOP 0x1 468f55dfb4SSandrine Bailleux #define BOTTOM !TOP 474ecca339SDan Handley 4829fb905dSVikram Kanigiri /******************************************************************************* 4929fb905dSVikram Kanigiri * Constants that allow assembler code to access members of and the 504112bfa0SVikram Kanigiri * 'entry_point_info' structure at their correct offsets. 5129fb905dSVikram Kanigiri ******************************************************************************/ 524112bfa0SVikram Kanigiri #define ENTRY_POINT_INFO_PC_OFFSET 0x08 534112bfa0SVikram Kanigiri #define ENTRY_POINT_INFO_ARGS_OFFSET 0x18 544112bfa0SVikram Kanigiri 557baff11fSYatharth Kochar /* The following are used to set/get image attributes. */ 567baff11fSYatharth Kochar #define PARAM_EP_SECURITY_MASK (0x1) 577baff11fSYatharth Kochar 584112bfa0SVikram Kanigiri #define GET_SECURITY_STATE(x) (x & PARAM_EP_SECURITY_MASK) 594112bfa0SVikram Kanigiri #define SET_SECURITY_STATE(x, security) \ 604112bfa0SVikram Kanigiri ((x) = ((x) & ~PARAM_EP_SECURITY_MASK) | (security)) 614112bfa0SVikram Kanigiri 627baff11fSYatharth Kochar 637baff11fSYatharth Kochar /* 647baff11fSYatharth Kochar * The following are used for image state attributes. 657baff11fSYatharth Kochar * Image can only be in one of the following state. 667baff11fSYatharth Kochar */ 677baff11fSYatharth Kochar #define IMAGE_STATE_RESET 0 687baff11fSYatharth Kochar #define IMAGE_STATE_COPIED 1 697baff11fSYatharth Kochar #define IMAGE_STATE_COPYING 2 707baff11fSYatharth Kochar #define IMAGE_STATE_AUTHENTICATED 3 717baff11fSYatharth Kochar #define IMAGE_STATE_EXECUTED 4 727baff11fSYatharth Kochar #define IMAGE_STATE_INTERRUPTED 5 737baff11fSYatharth Kochar 74167a9357SAndrew Thoelke #define EP_EE_MASK 0x2 75167a9357SAndrew Thoelke #define EP_EE_LITTLE 0x0 76167a9357SAndrew Thoelke #define EP_EE_BIG 0x2 77167a9357SAndrew Thoelke #define EP_GET_EE(x) (x & EP_EE_MASK) 78167a9357SAndrew Thoelke #define EP_SET_EE(x, ee) ((x) = ((x) & ~EP_EE_MASK) | (ee)) 79167a9357SAndrew Thoelke 80167a9357SAndrew Thoelke #define EP_ST_MASK 0x4 81167a9357SAndrew Thoelke #define EP_ST_DISABLE 0x0 82167a9357SAndrew Thoelke #define EP_ST_ENABLE 0x4 83167a9357SAndrew Thoelke #define EP_GET_ST(x) (x & EP_ST_MASK) 84167a9357SAndrew Thoelke #define EP_SET_ST(x, ee) ((x) = ((x) & ~EP_ST_MASK) | (ee)) 85167a9357SAndrew Thoelke 86843ddee4SYatharth Kochar #define EP_EXE_MASK 0x8 87843ddee4SYatharth Kochar #define NON_EXECUTABLE 0x0 88843ddee4SYatharth Kochar #define EXECUTABLE 0x8 89843ddee4SYatharth Kochar #define EP_GET_EXE(x) (x & EP_EXE_MASK) 90843ddee4SYatharth Kochar #define EP_SET_EXE(x, ee) ((x) = ((x) & ~EP_EXE_MASK) | (ee)) 91843ddee4SYatharth Kochar 924112bfa0SVikram Kanigiri #define PARAM_EP 0x01 934112bfa0SVikram Kanigiri #define PARAM_IMAGE_BINARY 0x02 944112bfa0SVikram Kanigiri #define PARAM_BL31 0x03 954112bfa0SVikram Kanigiri 964112bfa0SVikram Kanigiri #define VERSION_1 0x01 974112bfa0SVikram Kanigiri 987baff11fSYatharth Kochar #define INVALID_IMAGE_ID (0xFFFFFFFF) 997baff11fSYatharth Kochar 1004112bfa0SVikram Kanigiri #define SET_PARAM_HEAD(_p, _type, _ver, _attr) do { \ 1014112bfa0SVikram Kanigiri (_p)->h.type = (uint8_t)(_type); \ 1024112bfa0SVikram Kanigiri (_p)->h.version = (uint8_t)(_ver); \ 1034112bfa0SVikram Kanigiri (_p)->h.size = (uint16_t)sizeof(*_p); \ 1044112bfa0SVikram Kanigiri (_p)->h.attr = (uint32_t)(_attr) ; \ 1054112bfa0SVikram Kanigiri } while (0) 1064ecca339SDan Handley 107843ddee4SYatharth Kochar /* Following is used for populating structure members statically. */ 108843ddee4SYatharth Kochar #define SET_STATIC_PARAM_HEAD(_p, _type, _ver, _p_type, _attr) \ 109843ddee4SYatharth Kochar ._p.h.type = (uint8_t)(_type), \ 110843ddee4SYatharth Kochar ._p.h.version = (uint8_t)(_ver), \ 111843ddee4SYatharth Kochar ._p.h.size = (uint16_t)sizeof(_p_type), \ 112843ddee4SYatharth Kochar ._p.h.attr = (uint32_t)(_attr) 113843ddee4SYatharth Kochar 114843ddee4SYatharth Kochar 115bbf8f6f9SYatharth Kochar /******************************************************************************* 116bbf8f6f9SYatharth Kochar * Constants to indicate type of exception to the common exception handler. 117bbf8f6f9SYatharth Kochar ******************************************************************************/ 118bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_SP_EL0 0x0 119bbf8f6f9SYatharth Kochar #define IRQ_SP_EL0 0x1 120bbf8f6f9SYatharth Kochar #define FIQ_SP_EL0 0x2 121bbf8f6f9SYatharth Kochar #define SERROR_SP_EL0 0x3 122bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_SP_ELX 0x4 123bbf8f6f9SYatharth Kochar #define IRQ_SP_ELX 0x5 124bbf8f6f9SYatharth Kochar #define FIQ_SP_ELX 0x6 125bbf8f6f9SYatharth Kochar #define SERROR_SP_ELX 0x7 126bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_AARCH64 0x8 127bbf8f6f9SYatharth Kochar #define IRQ_AARCH64 0x9 128bbf8f6f9SYatharth Kochar #define FIQ_AARCH64 0xa 129bbf8f6f9SYatharth Kochar #define SERROR_AARCH64 0xb 130bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_AARCH32 0xc 131bbf8f6f9SYatharth Kochar #define IRQ_AARCH32 0xd 132bbf8f6f9SYatharth Kochar #define FIQ_AARCH32 0xe 133bbf8f6f9SYatharth Kochar #define SERROR_AARCH32 0xf 134bbf8f6f9SYatharth Kochar 1354ecca339SDan Handley #ifndef __ASSEMBLY__ 13697043ac9SDan Handley #include <cdefs.h> /* For __dead2 */ 13729fb905dSVikram Kanigiri #include <cassert.h> 1384112bfa0SVikram Kanigiri #include <stdint.h> 1398f55dfb4SSandrine Bailleux #include <stddef.h> 140*4c0d0390SSoby Mathew #include <types.h> 141ed81f3ebSSandrine Bailleux #include <utils.h> /* To retain compatibility */ 142a7e98ad5SVikram Kanigiri 14390b3a6acSDan Handley /* 14490b3a6acSDan Handley * Declarations of linker defined symbols to help determine memory layout of 14590b3a6acSDan Handley * BL images 14690b3a6acSDan Handley */ 1475d1c104fSSandrine Bailleux #if SEPARATE_CODE_AND_RODATA 148*4c0d0390SSoby Mathew extern uintptr_t __TEXT_START__; 149*4c0d0390SSoby Mathew extern uintptr_t __TEXT_END__; 150*4c0d0390SSoby Mathew extern uintptr_t __RODATA_START__; 151*4c0d0390SSoby Mathew extern uintptr_t __RODATA_END__; 1525d1c104fSSandrine Bailleux #else 153*4c0d0390SSoby Mathew extern uintptr_t __RO_START__; 154*4c0d0390SSoby Mathew extern uintptr_t __RO_END__; 1555d1c104fSSandrine Bailleux #endif 1565d1c104fSSandrine Bailleux 15790b3a6acSDan Handley #if IMAGE_BL2 158*4c0d0390SSoby Mathew extern uintptr_t __BL2_END__; 1599003fa0bSYatharth Kochar #elif IMAGE_BL2U 160*4c0d0390SSoby Mathew extern uintptr_t __BL2U_END__; 16190b3a6acSDan Handley #elif IMAGE_BL31 162*4c0d0390SSoby Mathew extern uintptr_t __BL31_END__; 16390b3a6acSDan Handley #elif IMAGE_BL32 164*4c0d0390SSoby Mathew extern uintptr_t __BL32_END__; 16590b3a6acSDan Handley #endif /* IMAGE_BLX */ 16690b3a6acSDan Handley 16790b3a6acSDan Handley #if USE_COHERENT_MEM 168*4c0d0390SSoby Mathew extern uintptr_t __COHERENT_RAM_START__; 169*4c0d0390SSoby Mathew extern uintptr_t __COHERENT_RAM_END__; 17090b3a6acSDan Handley #endif 17190b3a6acSDan Handley 17290b3a6acSDan Handley 1734ecca339SDan Handley /******************************************************************************* 1744ecca339SDan Handley * Structure used for telling the next BL how much of a particular type of 1754ecca339SDan Handley * memory is available for its use and how much is already used. 1764ecca339SDan Handley ******************************************************************************/ 177fb037bfbSDan Handley typedef struct meminfo { 178*4c0d0390SSoby Mathew uintptr_t total_base; 1798f55dfb4SSandrine Bailleux size_t total_size; 180*4c0d0390SSoby Mathew uintptr_t free_base; 1818f55dfb4SSandrine Bailleux size_t free_size; 182fb037bfbSDan Handley } meminfo_t; 1834ecca339SDan Handley 184fb037bfbSDan Handley typedef struct aapcs64_params { 185*4c0d0390SSoby Mathew u_register_t arg0; 186*4c0d0390SSoby Mathew u_register_t arg1; 187*4c0d0390SSoby Mathew u_register_t arg2; 188*4c0d0390SSoby Mathew u_register_t arg3; 189*4c0d0390SSoby Mathew u_register_t arg4; 190*4c0d0390SSoby Mathew u_register_t arg5; 191*4c0d0390SSoby Mathew u_register_t arg6; 192*4c0d0390SSoby Mathew u_register_t arg7; 193fb037bfbSDan Handley } aapcs64_params_t; 1944ecca339SDan Handley 1954112bfa0SVikram Kanigiri /*************************************************************************** 1964112bfa0SVikram Kanigiri * This structure provides version information and the size of the 1974112bfa0SVikram Kanigiri * structure, attributes for the structure it represents 1984112bfa0SVikram Kanigiri ***************************************************************************/ 1994112bfa0SVikram Kanigiri typedef struct param_header { 2004112bfa0SVikram Kanigiri uint8_t type; /* type of the structure */ 2014112bfa0SVikram Kanigiri uint8_t version; /* version of this structure */ 2024112bfa0SVikram Kanigiri uint16_t size; /* size of this structure in bytes */ 2034112bfa0SVikram Kanigiri uint32_t attr; /* attributes: unused bits SBZ */ 2044112bfa0SVikram Kanigiri } param_header_t; 2054112bfa0SVikram Kanigiri 2064112bfa0SVikram Kanigiri /***************************************************************************** 2074112bfa0SVikram Kanigiri * This structure represents the superset of information needed while 2084112bfa0SVikram Kanigiri * switching exception levels. The only two mechanisms to do so are 2094112bfa0SVikram Kanigiri * ERET & SMC. Security state is indicated using bit zero of header 2104112bfa0SVikram Kanigiri * attribute 211b313d755SSoby Mathew * NOTE: BL1 expects entrypoint followed by spsr at an offset from the start 212b313d755SSoby Mathew * of this structure defined by the macro `ENTRY_POINT_INFO_PC_OFFSET` while 213b313d755SSoby Mathew * processing SMC to jump to BL31. 2144112bfa0SVikram Kanigiri *****************************************************************************/ 2154112bfa0SVikram Kanigiri typedef struct entry_point_info { 2164112bfa0SVikram Kanigiri param_header_t h; 2174112bfa0SVikram Kanigiri uintptr_t pc; 2184112bfa0SVikram Kanigiri uint32_t spsr; 219fb037bfbSDan Handley aapcs64_params_t args; 2204112bfa0SVikram Kanigiri } entry_point_info_t; 2214112bfa0SVikram Kanigiri 2224112bfa0SVikram Kanigiri /***************************************************************************** 2234112bfa0SVikram Kanigiri * Image info binary provides information from the image loader that 2244112bfa0SVikram Kanigiri * can be used by the firmware to manage available trusted RAM. 2254112bfa0SVikram Kanigiri * More advanced firmware image formats can provide additional 2264112bfa0SVikram Kanigiri * information that enables optimization or greater flexibility in the 2274112bfa0SVikram Kanigiri * common firmware code 2284112bfa0SVikram Kanigiri *****************************************************************************/ 2294112bfa0SVikram Kanigiri typedef struct image_info { 2304112bfa0SVikram Kanigiri param_header_t h; 2314112bfa0SVikram Kanigiri uintptr_t image_base; /* physical address of base of image */ 2324112bfa0SVikram Kanigiri uint32_t image_size; /* bytes read from image file */ 2334112bfa0SVikram Kanigiri } image_info_t; 2344ecca339SDan Handley 2357baff11fSYatharth Kochar /***************************************************************************** 2367baff11fSYatharth Kochar * The image descriptor struct definition. 2377baff11fSYatharth Kochar *****************************************************************************/ 2387baff11fSYatharth Kochar typedef struct image_desc { 2397baff11fSYatharth Kochar /* Contains unique image id for the image. */ 2407baff11fSYatharth Kochar unsigned int image_id; 2417baff11fSYatharth Kochar /* 2427baff11fSYatharth Kochar * This member contains Image state information. 2437baff11fSYatharth Kochar * Refer IMAGE_STATE_XXX defined above. 2447baff11fSYatharth Kochar */ 2457baff11fSYatharth Kochar unsigned int state; 246843ddee4SYatharth Kochar uint32_t copied_size; /* image size copied in blocks */ 247b313d755SSoby Mathew image_info_t image_info; 248b313d755SSoby Mathew entry_point_info_t ep_info; 2497baff11fSYatharth Kochar } image_desc_t; 2507baff11fSYatharth Kochar 2514ecca339SDan Handley /******************************************************************************* 2524ecca339SDan Handley * This structure represents the superset of information that can be passed to 2534ecca339SDan Handley * BL31 e.g. while passing control to it from BL2. The BL32 parameters will be 2544112bfa0SVikram Kanigiri * populated only if BL2 detects its presence. A pointer to a structure of this 255d178637dSJuan Castillo * type should be passed in X0 to BL31's cold boot entrypoint. 2564112bfa0SVikram Kanigiri * 257d178637dSJuan Castillo * Use of this structure and the X0 parameter is not mandatory: the BL31 2584112bfa0SVikram Kanigiri * platform code can use other mechanisms to provide the necessary information 259d178637dSJuan Castillo * about BL32 and BL33 to the common and SPD code. 2604112bfa0SVikram Kanigiri * 261d178637dSJuan Castillo * BL31 image information is mandatory if this structure is used. If either of 262d178637dSJuan Castillo * the optional BL32 and BL33 image information is not provided, this is 2634112bfa0SVikram Kanigiri * indicated by the respective image_info pointers being zero. 2644ecca339SDan Handley ******************************************************************************/ 2654112bfa0SVikram Kanigiri typedef struct bl31_params { 2664112bfa0SVikram Kanigiri param_header_t h; 2674112bfa0SVikram Kanigiri image_info_t *bl31_image_info; 2684112bfa0SVikram Kanigiri entry_point_info_t *bl32_ep_info; 2694112bfa0SVikram Kanigiri image_info_t *bl32_image_info; 2704112bfa0SVikram Kanigiri entry_point_info_t *bl33_ep_info; 2714112bfa0SVikram Kanigiri image_info_t *bl33_image_info; 2724112bfa0SVikram Kanigiri } bl31_params_t; 2734112bfa0SVikram Kanigiri 2744112bfa0SVikram Kanigiri 27529fb905dSVikram Kanigiri /* 2764112bfa0SVikram Kanigiri * Compile time assertions related to the 'entry_point_info' structure to 27729fb905dSVikram Kanigiri * ensure that the assembler and the compiler view of the offsets of 27829fb905dSVikram Kanigiri * the structure members is the same. 27929fb905dSVikram Kanigiri */ 2804112bfa0SVikram Kanigiri CASSERT(ENTRY_POINT_INFO_PC_OFFSET == 2814112bfa0SVikram Kanigiri __builtin_offsetof(entry_point_info_t, pc), \ 28229fb905dSVikram Kanigiri assert_BL31_pc_offset_mismatch); 28329fb905dSVikram Kanigiri 2844112bfa0SVikram Kanigiri CASSERT(ENTRY_POINT_INFO_ARGS_OFFSET == \ 2854112bfa0SVikram Kanigiri __builtin_offsetof(entry_point_info_t, args), \ 28629fb905dSVikram Kanigiri assert_BL31_args_offset_mismatch); 28729fb905dSVikram Kanigiri 288*4c0d0390SSoby Mathew CASSERT(sizeof(uintptr_t) == 2894112bfa0SVikram Kanigiri __builtin_offsetof(entry_point_info_t, spsr) - \ 2904112bfa0SVikram Kanigiri __builtin_offsetof(entry_point_info_t, pc), \ 29129fb905dSVikram Kanigiri assert_entrypoint_and_spsr_should_be_adjacent); 29229fb905dSVikram Kanigiri 2934ecca339SDan Handley /******************************************************************************* 2944ecca339SDan Handley * Function & variable prototypes 2954ecca339SDan Handley ******************************************************************************/ 296*4c0d0390SSoby Mathew uintptr_t page_align(uintptr_t, unsigned); 297*4c0d0390SSoby Mathew size_t image_size(unsigned int image_id); 2988f55dfb4SSandrine Bailleux int load_image(meminfo_t *mem_layout, 29916948ae1SJuan Castillo unsigned int image_id, 3001779ba6bSJuan Castillo uintptr_t image_base, 3011779ba6bSJuan Castillo image_info_t *image_data, 3021779ba6bSJuan Castillo entry_point_info_t *entry_point_info); 3031779ba6bSJuan Castillo int load_auth_image(meminfo_t *mem_layout, 3041779ba6bSJuan Castillo unsigned int image_name, 3051779ba6bSJuan Castillo uintptr_t image_base, 3068f55dfb4SSandrine Bailleux image_info_t *image_data, 3078f55dfb4SSandrine Bailleux entry_point_info_t *entry_point_info); 3084ecca339SDan Handley extern const char build_message[]; 309aaa3e722SJuan Castillo extern const char version_string[]; 3104ecca339SDan Handley 311*4c0d0390SSoby Mathew void reserve_mem(uintptr_t *free_base, size_t *free_size, 312*4c0d0390SSoby Mathew uintptr_t addr, size_t size); 3138f55dfb4SSandrine Bailleux 31468a68c92SSandrine Bailleux void print_entry_point_info(const entry_point_info_t *ep_info); 31568a68c92SSandrine Bailleux 3164ecca339SDan Handley #endif /*__ASSEMBLY__*/ 3174ecca339SDan Handley 3184ecca339SDan Handley #endif /* __BL_COMMON_H__ */ 319