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 53*e33b78a6SSoby Mathew #ifdef AARCH32 54*e33b78a6SSoby Mathew #define ENTRY_POINT_INFO_ARGS_OFFSET 0x10 55*e33b78a6SSoby Mathew #else 564112bfa0SVikram Kanigiri #define ENTRY_POINT_INFO_ARGS_OFFSET 0x18 57*e33b78a6SSoby Mathew #endif 584112bfa0SVikram Kanigiri 597baff11fSYatharth Kochar /* The following are used to set/get image attributes. */ 607baff11fSYatharth Kochar #define PARAM_EP_SECURITY_MASK (0x1) 617baff11fSYatharth Kochar 624112bfa0SVikram Kanigiri #define GET_SECURITY_STATE(x) (x & PARAM_EP_SECURITY_MASK) 634112bfa0SVikram Kanigiri #define SET_SECURITY_STATE(x, security) \ 644112bfa0SVikram Kanigiri ((x) = ((x) & ~PARAM_EP_SECURITY_MASK) | (security)) 654112bfa0SVikram Kanigiri 667baff11fSYatharth Kochar 677baff11fSYatharth Kochar /* 687baff11fSYatharth Kochar * The following are used for image state attributes. 697baff11fSYatharth Kochar * Image can only be in one of the following state. 707baff11fSYatharth Kochar */ 717baff11fSYatharth Kochar #define IMAGE_STATE_RESET 0 727baff11fSYatharth Kochar #define IMAGE_STATE_COPIED 1 737baff11fSYatharth Kochar #define IMAGE_STATE_COPYING 2 747baff11fSYatharth Kochar #define IMAGE_STATE_AUTHENTICATED 3 757baff11fSYatharth Kochar #define IMAGE_STATE_EXECUTED 4 767baff11fSYatharth Kochar #define IMAGE_STATE_INTERRUPTED 5 777baff11fSYatharth Kochar 78167a9357SAndrew Thoelke #define EP_EE_MASK 0x2 79167a9357SAndrew Thoelke #define EP_EE_LITTLE 0x0 80167a9357SAndrew Thoelke #define EP_EE_BIG 0x2 81167a9357SAndrew Thoelke #define EP_GET_EE(x) (x & EP_EE_MASK) 82167a9357SAndrew Thoelke #define EP_SET_EE(x, ee) ((x) = ((x) & ~EP_EE_MASK) | (ee)) 83167a9357SAndrew Thoelke 84167a9357SAndrew Thoelke #define EP_ST_MASK 0x4 85167a9357SAndrew Thoelke #define EP_ST_DISABLE 0x0 86167a9357SAndrew Thoelke #define EP_ST_ENABLE 0x4 87167a9357SAndrew Thoelke #define EP_GET_ST(x) (x & EP_ST_MASK) 88167a9357SAndrew Thoelke #define EP_SET_ST(x, ee) ((x) = ((x) & ~EP_ST_MASK) | (ee)) 89167a9357SAndrew Thoelke 90843ddee4SYatharth Kochar #define EP_EXE_MASK 0x8 91843ddee4SYatharth Kochar #define NON_EXECUTABLE 0x0 92843ddee4SYatharth Kochar #define EXECUTABLE 0x8 93843ddee4SYatharth Kochar #define EP_GET_EXE(x) (x & EP_EXE_MASK) 94843ddee4SYatharth Kochar #define EP_SET_EXE(x, ee) ((x) = ((x) & ~EP_EXE_MASK) | (ee)) 95843ddee4SYatharth Kochar 964112bfa0SVikram Kanigiri #define PARAM_EP 0x01 974112bfa0SVikram Kanigiri #define PARAM_IMAGE_BINARY 0x02 984112bfa0SVikram Kanigiri #define PARAM_BL31 0x03 994112bfa0SVikram Kanigiri 1004112bfa0SVikram Kanigiri #define VERSION_1 0x01 1014112bfa0SVikram Kanigiri 1027baff11fSYatharth Kochar #define INVALID_IMAGE_ID (0xFFFFFFFF) 1037baff11fSYatharth Kochar 1044112bfa0SVikram Kanigiri #define SET_PARAM_HEAD(_p, _type, _ver, _attr) do { \ 1054112bfa0SVikram Kanigiri (_p)->h.type = (uint8_t)(_type); \ 1064112bfa0SVikram Kanigiri (_p)->h.version = (uint8_t)(_ver); \ 1074112bfa0SVikram Kanigiri (_p)->h.size = (uint16_t)sizeof(*_p); \ 1084112bfa0SVikram Kanigiri (_p)->h.attr = (uint32_t)(_attr) ; \ 1094112bfa0SVikram Kanigiri } while (0) 1104ecca339SDan Handley 111843ddee4SYatharth Kochar /* Following is used for populating structure members statically. */ 112843ddee4SYatharth Kochar #define SET_STATIC_PARAM_HEAD(_p, _type, _ver, _p_type, _attr) \ 113843ddee4SYatharth Kochar ._p.h.type = (uint8_t)(_type), \ 114843ddee4SYatharth Kochar ._p.h.version = (uint8_t)(_ver), \ 115843ddee4SYatharth Kochar ._p.h.size = (uint16_t)sizeof(_p_type), \ 116843ddee4SYatharth Kochar ._p.h.attr = (uint32_t)(_attr) 117843ddee4SYatharth Kochar 118843ddee4SYatharth Kochar 119bbf8f6f9SYatharth Kochar /******************************************************************************* 120bbf8f6f9SYatharth Kochar * Constants to indicate type of exception to the common exception handler. 121bbf8f6f9SYatharth Kochar ******************************************************************************/ 122bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_SP_EL0 0x0 123bbf8f6f9SYatharth Kochar #define IRQ_SP_EL0 0x1 124bbf8f6f9SYatharth Kochar #define FIQ_SP_EL0 0x2 125bbf8f6f9SYatharth Kochar #define SERROR_SP_EL0 0x3 126bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_SP_ELX 0x4 127bbf8f6f9SYatharth Kochar #define IRQ_SP_ELX 0x5 128bbf8f6f9SYatharth Kochar #define FIQ_SP_ELX 0x6 129bbf8f6f9SYatharth Kochar #define SERROR_SP_ELX 0x7 130bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_AARCH64 0x8 131bbf8f6f9SYatharth Kochar #define IRQ_AARCH64 0x9 132bbf8f6f9SYatharth Kochar #define FIQ_AARCH64 0xa 133bbf8f6f9SYatharth Kochar #define SERROR_AARCH64 0xb 134bbf8f6f9SYatharth Kochar #define SYNC_EXCEPTION_AARCH32 0xc 135bbf8f6f9SYatharth Kochar #define IRQ_AARCH32 0xd 136bbf8f6f9SYatharth Kochar #define FIQ_AARCH32 0xe 137bbf8f6f9SYatharth Kochar #define SERROR_AARCH32 0xf 138bbf8f6f9SYatharth Kochar 1394ecca339SDan Handley #ifndef __ASSEMBLY__ 14097043ac9SDan Handley #include <cdefs.h> /* For __dead2 */ 14129fb905dSVikram Kanigiri #include <cassert.h> 1424112bfa0SVikram Kanigiri #include <stdint.h> 1438f55dfb4SSandrine Bailleux #include <stddef.h> 1444c0d0390SSoby Mathew #include <types.h> 145ed81f3ebSSandrine Bailleux #include <utils.h> /* To retain compatibility */ 146a7e98ad5SVikram Kanigiri 14790b3a6acSDan Handley /* 14890b3a6acSDan Handley * Declarations of linker defined symbols to help determine memory layout of 14990b3a6acSDan Handley * BL images 15090b3a6acSDan Handley */ 1515d1c104fSSandrine Bailleux #if SEPARATE_CODE_AND_RODATA 1524c0d0390SSoby Mathew extern uintptr_t __TEXT_START__; 1534c0d0390SSoby Mathew extern uintptr_t __TEXT_END__; 1544c0d0390SSoby Mathew extern uintptr_t __RODATA_START__; 1554c0d0390SSoby Mathew extern uintptr_t __RODATA_END__; 1565d1c104fSSandrine Bailleux #else 1574c0d0390SSoby Mathew extern uintptr_t __RO_START__; 1584c0d0390SSoby Mathew extern uintptr_t __RO_END__; 1595d1c104fSSandrine Bailleux #endif 1605d1c104fSSandrine Bailleux 16190b3a6acSDan Handley #if IMAGE_BL2 1624c0d0390SSoby Mathew extern uintptr_t __BL2_END__; 1639003fa0bSYatharth Kochar #elif IMAGE_BL2U 1644c0d0390SSoby Mathew extern uintptr_t __BL2U_END__; 16590b3a6acSDan Handley #elif IMAGE_BL31 1664c0d0390SSoby Mathew extern uintptr_t __BL31_END__; 16790b3a6acSDan Handley #elif IMAGE_BL32 1684c0d0390SSoby Mathew extern uintptr_t __BL32_END__; 16990b3a6acSDan Handley #endif /* IMAGE_BLX */ 17090b3a6acSDan Handley 17190b3a6acSDan Handley #if USE_COHERENT_MEM 1724c0d0390SSoby Mathew extern uintptr_t __COHERENT_RAM_START__; 1734c0d0390SSoby Mathew extern uintptr_t __COHERENT_RAM_END__; 17490b3a6acSDan Handley #endif 17590b3a6acSDan Handley 17690b3a6acSDan Handley 1774ecca339SDan Handley /******************************************************************************* 1784ecca339SDan Handley * Structure used for telling the next BL how much of a particular type of 1794ecca339SDan Handley * memory is available for its use and how much is already used. 1804ecca339SDan Handley ******************************************************************************/ 181fb037bfbSDan Handley typedef struct meminfo { 1824c0d0390SSoby Mathew uintptr_t total_base; 1838f55dfb4SSandrine Bailleux size_t total_size; 1844c0d0390SSoby Mathew uintptr_t free_base; 1858f55dfb4SSandrine Bailleux size_t free_size; 186fb037bfbSDan Handley } meminfo_t; 1874ecca339SDan Handley 188fb037bfbSDan Handley typedef struct aapcs64_params { 1894c0d0390SSoby Mathew u_register_t arg0; 1904c0d0390SSoby Mathew u_register_t arg1; 1914c0d0390SSoby Mathew u_register_t arg2; 1924c0d0390SSoby Mathew u_register_t arg3; 1934c0d0390SSoby Mathew u_register_t arg4; 1944c0d0390SSoby Mathew u_register_t arg5; 1954c0d0390SSoby Mathew u_register_t arg6; 1964c0d0390SSoby Mathew u_register_t arg7; 197fb037bfbSDan Handley } aapcs64_params_t; 1984ecca339SDan Handley 199*e33b78a6SSoby Mathew typedef struct aapcs32_params { 200*e33b78a6SSoby Mathew u_register_t arg0; 201*e33b78a6SSoby Mathew u_register_t arg1; 202*e33b78a6SSoby Mathew u_register_t arg2; 203*e33b78a6SSoby Mathew u_register_t arg3; 204*e33b78a6SSoby Mathew } aapcs32_params_t; 205*e33b78a6SSoby Mathew 2064112bfa0SVikram Kanigiri /*************************************************************************** 2074112bfa0SVikram Kanigiri * This structure provides version information and the size of the 2084112bfa0SVikram Kanigiri * structure, attributes for the structure it represents 2094112bfa0SVikram Kanigiri ***************************************************************************/ 2104112bfa0SVikram Kanigiri typedef struct param_header { 2114112bfa0SVikram Kanigiri uint8_t type; /* type of the structure */ 2124112bfa0SVikram Kanigiri uint8_t version; /* version of this structure */ 2134112bfa0SVikram Kanigiri uint16_t size; /* size of this structure in bytes */ 2144112bfa0SVikram Kanigiri uint32_t attr; /* attributes: unused bits SBZ */ 2154112bfa0SVikram Kanigiri } param_header_t; 2164112bfa0SVikram Kanigiri 2174112bfa0SVikram Kanigiri /***************************************************************************** 2184112bfa0SVikram Kanigiri * This structure represents the superset of information needed while 2194112bfa0SVikram Kanigiri * switching exception levels. The only two mechanisms to do so are 2204112bfa0SVikram Kanigiri * ERET & SMC. Security state is indicated using bit zero of header 2214112bfa0SVikram Kanigiri * attribute 222b313d755SSoby Mathew * NOTE: BL1 expects entrypoint followed by spsr at an offset from the start 223b313d755SSoby Mathew * of this structure defined by the macro `ENTRY_POINT_INFO_PC_OFFSET` while 224b313d755SSoby Mathew * processing SMC to jump to BL31. 2254112bfa0SVikram Kanigiri *****************************************************************************/ 2264112bfa0SVikram Kanigiri typedef struct entry_point_info { 2274112bfa0SVikram Kanigiri param_header_t h; 2284112bfa0SVikram Kanigiri uintptr_t pc; 2294112bfa0SVikram Kanigiri uint32_t spsr; 230*e33b78a6SSoby Mathew #ifdef AARCH32 231*e33b78a6SSoby Mathew aapcs32_params_t args; 232*e33b78a6SSoby Mathew #else 233fb037bfbSDan Handley aapcs64_params_t args; 234*e33b78a6SSoby Mathew #endif 2354112bfa0SVikram Kanigiri } entry_point_info_t; 2364112bfa0SVikram Kanigiri 2374112bfa0SVikram Kanigiri /***************************************************************************** 2384112bfa0SVikram Kanigiri * Image info binary provides information from the image loader that 2394112bfa0SVikram Kanigiri * can be used by the firmware to manage available trusted RAM. 2404112bfa0SVikram Kanigiri * More advanced firmware image formats can provide additional 2414112bfa0SVikram Kanigiri * information that enables optimization or greater flexibility in the 2424112bfa0SVikram Kanigiri * common firmware code 2434112bfa0SVikram Kanigiri *****************************************************************************/ 2444112bfa0SVikram Kanigiri typedef struct image_info { 2454112bfa0SVikram Kanigiri param_header_t h; 2464112bfa0SVikram Kanigiri uintptr_t image_base; /* physical address of base of image */ 2474112bfa0SVikram Kanigiri uint32_t image_size; /* bytes read from image file */ 2484112bfa0SVikram Kanigiri } image_info_t; 2494ecca339SDan Handley 2507baff11fSYatharth Kochar /***************************************************************************** 2517baff11fSYatharth Kochar * The image descriptor struct definition. 2527baff11fSYatharth Kochar *****************************************************************************/ 2537baff11fSYatharth Kochar typedef struct image_desc { 2547baff11fSYatharth Kochar /* Contains unique image id for the image. */ 2557baff11fSYatharth Kochar unsigned int image_id; 2567baff11fSYatharth Kochar /* 2577baff11fSYatharth Kochar * This member contains Image state information. 2587baff11fSYatharth Kochar * Refer IMAGE_STATE_XXX defined above. 2597baff11fSYatharth Kochar */ 2607baff11fSYatharth Kochar unsigned int state; 261843ddee4SYatharth Kochar uint32_t copied_size; /* image size copied in blocks */ 262b313d755SSoby Mathew image_info_t image_info; 263b313d755SSoby Mathew entry_point_info_t ep_info; 2647baff11fSYatharth Kochar } image_desc_t; 2657baff11fSYatharth Kochar 2664ecca339SDan Handley /******************************************************************************* 2674ecca339SDan Handley * This structure represents the superset of information that can be passed to 2684ecca339SDan Handley * BL31 e.g. while passing control to it from BL2. The BL32 parameters will be 2694112bfa0SVikram Kanigiri * populated only if BL2 detects its presence. A pointer to a structure of this 270d178637dSJuan Castillo * type should be passed in X0 to BL31's cold boot entrypoint. 2714112bfa0SVikram Kanigiri * 272d178637dSJuan Castillo * Use of this structure and the X0 parameter is not mandatory: the BL31 2734112bfa0SVikram Kanigiri * platform code can use other mechanisms to provide the necessary information 274d178637dSJuan Castillo * about BL32 and BL33 to the common and SPD code. 2754112bfa0SVikram Kanigiri * 276d178637dSJuan Castillo * BL31 image information is mandatory if this structure is used. If either of 277d178637dSJuan Castillo * the optional BL32 and BL33 image information is not provided, this is 2784112bfa0SVikram Kanigiri * indicated by the respective image_info pointers being zero. 2794ecca339SDan Handley ******************************************************************************/ 2804112bfa0SVikram Kanigiri typedef struct bl31_params { 2814112bfa0SVikram Kanigiri param_header_t h; 2824112bfa0SVikram Kanigiri image_info_t *bl31_image_info; 2834112bfa0SVikram Kanigiri entry_point_info_t *bl32_ep_info; 2844112bfa0SVikram Kanigiri image_info_t *bl32_image_info; 2854112bfa0SVikram Kanigiri entry_point_info_t *bl33_ep_info; 2864112bfa0SVikram Kanigiri image_info_t *bl33_image_info; 2874112bfa0SVikram Kanigiri } bl31_params_t; 2884112bfa0SVikram Kanigiri 2894112bfa0SVikram Kanigiri 29029fb905dSVikram Kanigiri /* 2914112bfa0SVikram Kanigiri * Compile time assertions related to the 'entry_point_info' structure to 29229fb905dSVikram Kanigiri * ensure that the assembler and the compiler view of the offsets of 29329fb905dSVikram Kanigiri * the structure members is the same. 29429fb905dSVikram Kanigiri */ 2954112bfa0SVikram Kanigiri CASSERT(ENTRY_POINT_INFO_PC_OFFSET == 2964112bfa0SVikram Kanigiri __builtin_offsetof(entry_point_info_t, pc), \ 29729fb905dSVikram Kanigiri assert_BL31_pc_offset_mismatch); 29829fb905dSVikram Kanigiri 2994112bfa0SVikram Kanigiri CASSERT(ENTRY_POINT_INFO_ARGS_OFFSET == \ 3004112bfa0SVikram Kanigiri __builtin_offsetof(entry_point_info_t, args), \ 30129fb905dSVikram Kanigiri assert_BL31_args_offset_mismatch); 30229fb905dSVikram Kanigiri 3034c0d0390SSoby Mathew CASSERT(sizeof(uintptr_t) == 3044112bfa0SVikram Kanigiri __builtin_offsetof(entry_point_info_t, spsr) - \ 3054112bfa0SVikram Kanigiri __builtin_offsetof(entry_point_info_t, pc), \ 30629fb905dSVikram Kanigiri assert_entrypoint_and_spsr_should_be_adjacent); 30729fb905dSVikram Kanigiri 3084ecca339SDan Handley /******************************************************************************* 3094ecca339SDan Handley * Function & variable prototypes 3104ecca339SDan Handley ******************************************************************************/ 3114c0d0390SSoby Mathew uintptr_t page_align(uintptr_t, unsigned); 3124c0d0390SSoby Mathew size_t image_size(unsigned int image_id); 3138f55dfb4SSandrine Bailleux int load_image(meminfo_t *mem_layout, 31416948ae1SJuan Castillo unsigned int image_id, 3151779ba6bSJuan Castillo uintptr_t image_base, 3161779ba6bSJuan Castillo image_info_t *image_data, 3171779ba6bSJuan Castillo entry_point_info_t *entry_point_info); 3181779ba6bSJuan Castillo int load_auth_image(meminfo_t *mem_layout, 3191779ba6bSJuan Castillo unsigned int image_name, 3201779ba6bSJuan Castillo uintptr_t image_base, 3218f55dfb4SSandrine Bailleux image_info_t *image_data, 3228f55dfb4SSandrine Bailleux entry_point_info_t *entry_point_info); 3234ecca339SDan Handley extern const char build_message[]; 324aaa3e722SJuan Castillo extern const char version_string[]; 3254ecca339SDan Handley 3264c0d0390SSoby Mathew void reserve_mem(uintptr_t *free_base, size_t *free_size, 3274c0d0390SSoby Mathew uintptr_t addr, size_t size); 3288f55dfb4SSandrine Bailleux 32968a68c92SSandrine Bailleux void print_entry_point_info(const entry_point_info_t *ep_info); 33068a68c92SSandrine Bailleux 3314ecca339SDan Handley #endif /*__ASSEMBLY__*/ 3324ecca339SDan Handley 3334ecca339SDan Handley #endif /* __BL_COMMON_H__ */ 334