14f6ad66aSAchin Gupta /* 2e83b0cadSDan Handley * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. 34f6ad66aSAchin Gupta * 44f6ad66aSAchin Gupta * Redistribution and use in source and binary forms, with or without 54f6ad66aSAchin Gupta * modification, are permitted provided that the following conditions are met: 64f6ad66aSAchin Gupta * 74f6ad66aSAchin Gupta * Redistributions of source code must retain the above copyright notice, this 84f6ad66aSAchin Gupta * list of conditions and the following disclaimer. 94f6ad66aSAchin Gupta * 104f6ad66aSAchin Gupta * Redistributions in binary form must reproduce the above copyright notice, 114f6ad66aSAchin Gupta * this list of conditions and the following disclaimer in the documentation 124f6ad66aSAchin Gupta * and/or other materials provided with the distribution. 134f6ad66aSAchin Gupta * 144f6ad66aSAchin Gupta * Neither the name of ARM nor the names of its contributors may be used 154f6ad66aSAchin Gupta * to endorse or promote products derived from this software without specific 164f6ad66aSAchin Gupta * prior written permission. 174f6ad66aSAchin Gupta * 184f6ad66aSAchin Gupta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 194f6ad66aSAchin Gupta * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 204f6ad66aSAchin Gupta * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 214f6ad66aSAchin Gupta * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 224f6ad66aSAchin Gupta * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 234f6ad66aSAchin Gupta * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 244f6ad66aSAchin Gupta * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 254f6ad66aSAchin Gupta * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 264f6ad66aSAchin Gupta * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 274f6ad66aSAchin Gupta * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 284f6ad66aSAchin Gupta * POSSIBILITY OF SUCH DAMAGE. 294f6ad66aSAchin Gupta */ 304f6ad66aSAchin Gupta 3197043ac9SDan Handley #include <arch.h> 324f6ad66aSAchin Gupta #include <arch_helpers.h> 3397043ac9SDan Handley #include <assert.h> 34dec840afSJuan Castillo #include <auth.h> 354f6ad66aSAchin Gupta #include <bl_common.h> 3635e98e55SDan Handley #include <debug.h> 3797043ac9SDan Handley #include <platform.h> 385f0cdb05SDan Handley #include <platform_def.h> 39*16948ae1SJuan Castillo #include <stdint.h> 405b827a8fSDan Handley #include "bl2_private.h" 414f6ad66aSAchin Gupta 42dec840afSJuan Castillo #if TRUSTED_BOARD_BOOT 43dec840afSJuan Castillo 44dec840afSJuan Castillo #ifdef BL32_BASE 45dec840afSJuan Castillo static int bl32_cert_error; 46dec840afSJuan Castillo #endif 47dec840afSJuan Castillo 48dec840afSJuan Castillo /* 49*16948ae1SJuan Castillo * Load and authenticate the key and content certificates for a BL3-x image. 50*16948ae1SJuan Castillo * The _blob values identify the authentication objects (an object may be seen 51*16948ae1SJuan Castillo * as a single stage in the authentication process). See auth.h for the complete 52*16948ae1SJuan Castillo * list of objects. The _id values are passed to the IO framework to identify 53*16948ae1SJuan Castillo * the images to load. 54dec840afSJuan Castillo * 55dec840afSJuan Castillo * Parameters: 56dec840afSJuan Castillo * key_cert_blob: key certificate blob id (see auth.h) 57*16948ae1SJuan Castillo * key_cert_id: key certificate image identifier (for IO framework) 58dec840afSJuan Castillo * cont_cert_blob: content certificate blob id (see auth.h) 59*16948ae1SJuan Castillo * cont_cert_id: content certificate image identifier (for IO framework) 60dec840afSJuan Castillo * mem_layout: Trusted SRAM memory layout 61dec840afSJuan Castillo * load_addr: load the certificates at this address 62dec840afSJuan Castillo * 63dec840afSJuan Castillo * Return: 0 = success, Otherwise = error 64dec840afSJuan Castillo */ 65*16948ae1SJuan Castillo static int load_cert_bl3x(unsigned int key_cert_blob, unsigned int key_cert_id, 66*16948ae1SJuan Castillo unsigned int cont_cert_blob, unsigned int cont_cert_id, 67dec840afSJuan Castillo meminfo_t *mem_layout, uint64_t load_addr) 68dec840afSJuan Castillo { 69dec840afSJuan Castillo image_info_t image_info; 70dec840afSJuan Castillo int err; 71dec840afSJuan Castillo 72dec840afSJuan Castillo /* Load Key certificate */ 73dec840afSJuan Castillo image_info.h.version = VERSION_1; 74*16948ae1SJuan Castillo err = load_image(mem_layout, key_cert_id, load_addr, &image_info, NULL); 75dec840afSJuan Castillo if (err) { 76*16948ae1SJuan Castillo ERROR("Cannot load key certificate id=%u\n", key_cert_id); 77dec840afSJuan Castillo return err; 78dec840afSJuan Castillo } 79dec840afSJuan Castillo 80dec840afSJuan Castillo err = auth_verify_obj(key_cert_blob, image_info.image_base, 81dec840afSJuan Castillo image_info.image_size); 82dec840afSJuan Castillo if (err) { 83*16948ae1SJuan Castillo ERROR("Invalid key certificate id=%u\n", key_cert_id); 84dec840afSJuan Castillo return err; 85dec840afSJuan Castillo } 86dec840afSJuan Castillo 87dec840afSJuan Castillo /* Load Content certificate */ 88dec840afSJuan Castillo image_info.h.version = VERSION_1; 89*16948ae1SJuan Castillo err = load_image(mem_layout, cont_cert_id, load_addr, &image_info, NULL); 90dec840afSJuan Castillo if (err) { 91*16948ae1SJuan Castillo ERROR("Cannot load content certificate id=%u\n", 92*16948ae1SJuan Castillo cont_cert_id); 93dec840afSJuan Castillo return err; 94dec840afSJuan Castillo } 95dec840afSJuan Castillo 96dec840afSJuan Castillo err = auth_verify_obj(cont_cert_blob, image_info.image_base, 97dec840afSJuan Castillo image_info.image_size); 98dec840afSJuan Castillo if (err) { 99*16948ae1SJuan Castillo ERROR("Invalid content certificate id=%u\n", cont_cert_id); 100dec840afSJuan Castillo return err; 101dec840afSJuan Castillo } 102dec840afSJuan Castillo 103dec840afSJuan Castillo return 0; 104dec840afSJuan Castillo } 105dec840afSJuan Castillo 106dec840afSJuan Castillo /* 107dec840afSJuan Castillo * Load and authenticate the Trusted Key certificate the key and content 108dec840afSJuan Castillo * certificates for each of the BL3-x images. 109dec840afSJuan Castillo * 110dec840afSJuan Castillo * Return: 0 = success, Otherwise = error 111dec840afSJuan Castillo */ 112dec840afSJuan Castillo static int load_certs(void) 113dec840afSJuan Castillo { 114dec840afSJuan Castillo const uint64_t load_addr = BL31_BASE; 115dec840afSJuan Castillo image_info_t image_info; 116dec840afSJuan Castillo meminfo_t *mem_layout; 117dec840afSJuan Castillo int err; 118dec840afSJuan Castillo 119dec840afSJuan Castillo /* Find out how much free trusted ram remains after BL2 load */ 120dec840afSJuan Castillo mem_layout = bl2_plat_sec_mem_layout(); 121dec840afSJuan Castillo 122dec840afSJuan Castillo /* Load the Trusted Key certificate in the BL31 region */ 123dec840afSJuan Castillo image_info.h.version = VERSION_1; 124*16948ae1SJuan Castillo err = load_image(mem_layout, TRUSTED_KEY_CERT_ID, load_addr, 125dec840afSJuan Castillo &image_info, NULL); 126dec840afSJuan Castillo if (err) { 127dec840afSJuan Castillo ERROR("Failed to load Trusted Key certificate.\n"); 128dec840afSJuan Castillo return err; 129dec840afSJuan Castillo } 130dec840afSJuan Castillo 131dec840afSJuan Castillo /* Validate the certificate */ 132dec840afSJuan Castillo err = auth_verify_obj(AUTH_TRUSTED_KEY_CERT, image_info.image_base, 133dec840afSJuan Castillo image_info.image_size); 134dec840afSJuan Castillo if (err) { 135dec840afSJuan Castillo ERROR("Invalid Trusted Key certificate.\n"); 136dec840afSJuan Castillo return err; 137dec840afSJuan Castillo } 138dec840afSJuan Castillo 139dec840afSJuan Castillo /* Load and validate Key and Content certificates for BL3-x images */ 140dec840afSJuan Castillo #ifdef BL30_BASE 141*16948ae1SJuan Castillo err = load_cert_bl3x(AUTH_BL30_KEY_CERT, BL30_KEY_CERT_ID, 142*16948ae1SJuan Castillo AUTH_BL30_IMG_CERT, BL30_CERT_ID, 143dec840afSJuan Castillo mem_layout, load_addr); 144dec840afSJuan Castillo if (err) { 145dec840afSJuan Castillo ERROR("Failed to verify BL3-0 authenticity\n"); 146dec840afSJuan Castillo return err; 147dec840afSJuan Castillo } 148dec840afSJuan Castillo #endif /* BL30_BASE */ 149dec840afSJuan Castillo 150*16948ae1SJuan Castillo err = load_cert_bl3x(AUTH_BL31_KEY_CERT, BL31_KEY_CERT_ID, 151*16948ae1SJuan Castillo AUTH_BL31_IMG_CERT, BL31_CERT_ID, 152dec840afSJuan Castillo mem_layout, load_addr); 153dec840afSJuan Castillo if (err) { 154dec840afSJuan Castillo ERROR("Failed to verify BL3-1 authenticity\n"); 155dec840afSJuan Castillo return err; 156dec840afSJuan Castillo } 157dec840afSJuan Castillo 158dec840afSJuan Castillo #ifdef BL32_BASE 159dec840afSJuan Castillo /* BL3-2 image is optional, but keep the return value in case the 160dec840afSJuan Castillo * image is present but the certificate is missing */ 161*16948ae1SJuan Castillo err = load_cert_bl3x(AUTH_BL32_KEY_CERT, BL32_KEY_CERT_ID, 162*16948ae1SJuan Castillo AUTH_BL32_IMG_CERT, BL32_CERT_ID, 163dec840afSJuan Castillo mem_layout, load_addr); 164dec840afSJuan Castillo if (err) { 165dec840afSJuan Castillo WARN("Failed to verify BL3-2 authenticity\n"); 166dec840afSJuan Castillo } 167dec840afSJuan Castillo bl32_cert_error = err; 168dec840afSJuan Castillo #endif /* BL32_BASE */ 169dec840afSJuan Castillo 170*16948ae1SJuan Castillo err = load_cert_bl3x(AUTH_BL33_KEY_CERT, BL33_KEY_CERT_ID, 171*16948ae1SJuan Castillo AUTH_BL33_IMG_CERT, BL33_CERT_ID, 172dec840afSJuan Castillo mem_layout, load_addr); 173dec840afSJuan Castillo if (err) { 174dec840afSJuan Castillo ERROR("Failed to verify BL3-3 authenticity\n"); 175dec840afSJuan Castillo return err; 176dec840afSJuan Castillo } 177dec840afSJuan Castillo 178dec840afSJuan Castillo return 0; 179dec840afSJuan Castillo } 180dec840afSJuan Castillo 181dec840afSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */ 182dec840afSJuan Castillo 18393d81d64SSandrine Bailleux /******************************************************************************* 18493d81d64SSandrine Bailleux * Load the BL3-0 image if there's one. 18593d81d64SSandrine Bailleux * If a platform does not want to attempt to load BL3-0 image it must leave 18693d81d64SSandrine Bailleux * BL30_BASE undefined. 18793d81d64SSandrine Bailleux * Return 0 on success or if there's no BL3-0 image to load, a negative error 18893d81d64SSandrine Bailleux * code otherwise. 18993d81d64SSandrine Bailleux ******************************************************************************/ 19093d81d64SSandrine Bailleux static int load_bl30(void) 19193d81d64SSandrine Bailleux { 19293d81d64SSandrine Bailleux int e = 0; 19393d81d64SSandrine Bailleux #ifdef BL30_BASE 19493d81d64SSandrine Bailleux meminfo_t bl30_mem_info; 19593d81d64SSandrine Bailleux image_info_t bl30_image_info; 19693d81d64SSandrine Bailleux 19793d81d64SSandrine Bailleux /* 19893d81d64SSandrine Bailleux * It is up to the platform to specify where BL3-0 should be loaded if 19993d81d64SSandrine Bailleux * it exists. It could create space in the secure sram or point to a 20093d81d64SSandrine Bailleux * completely different memory. 20193d81d64SSandrine Bailleux * 20293d81d64SSandrine Bailleux * The entry point information is not relevant in this case as the AP 20393d81d64SSandrine Bailleux * won't execute the BL3-0 image. 20493d81d64SSandrine Bailleux */ 2056ad2e461SDan Handley INFO("BL2: Loading BL3-0\n"); 20693d81d64SSandrine Bailleux bl2_plat_get_bl30_meminfo(&bl30_mem_info); 20792de3565SJuan Castillo bl30_image_info.h.version = VERSION_1; 20893d81d64SSandrine Bailleux e = load_image(&bl30_mem_info, 209*16948ae1SJuan Castillo BL30_IMAGE_ID, 21093d81d64SSandrine Bailleux BL30_BASE, 21193d81d64SSandrine Bailleux &bl30_image_info, 21293d81d64SSandrine Bailleux NULL); 21393d81d64SSandrine Bailleux 214bcb79b90SSandrine Bailleux if (e) 215bcb79b90SSandrine Bailleux return e; 216bcb79b90SSandrine Bailleux 217dec840afSJuan Castillo #if TRUSTED_BOARD_BOOT 218dec840afSJuan Castillo e = auth_verify_obj(AUTH_BL30_IMG, 219dec840afSJuan Castillo bl30_image_info.image_base, 220dec840afSJuan Castillo bl30_image_info.image_size); 221dec840afSJuan Castillo if (e) { 222dec840afSJuan Castillo ERROR("Failed to authenticate BL3-0 image.\n"); 223bcb79b90SSandrine Bailleux return e; 224dec840afSJuan Castillo } 225dec840afSJuan Castillo 226dec840afSJuan Castillo /* After working with data, invalidate the data cache */ 227dec840afSJuan Castillo inv_dcache_range(bl30_image_info.image_base, 228dec840afSJuan Castillo (size_t)bl30_image_info.image_size); 229dec840afSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */ 230dec840afSJuan Castillo 23193d81d64SSandrine Bailleux /* The subsequent handling of BL3-0 is platform specific */ 232bcb79b90SSandrine Bailleux e = bl2_plat_handle_bl30(&bl30_image_info); 233bcb79b90SSandrine Bailleux if (e) { 234bcb79b90SSandrine Bailleux ERROR("Failure in platform-specific handling of BL3-0 image.\n"); 235bcb79b90SSandrine Bailleux return e; 23693d81d64SSandrine Bailleux } 23793d81d64SSandrine Bailleux #endif /* BL30_BASE */ 23893d81d64SSandrine Bailleux 23993d81d64SSandrine Bailleux return e; 24093d81d64SSandrine Bailleux } 24129fb905dSVikram Kanigiri 24229fb905dSVikram Kanigiri /******************************************************************************* 24393d81d64SSandrine Bailleux * Load the BL3-1 image. 24493d81d64SSandrine Bailleux * The bl2_to_bl31_params and bl31_ep_info params will be updated with the 24593d81d64SSandrine Bailleux * relevant BL3-1 information. 24693d81d64SSandrine Bailleux * Return 0 on success, a negative error code otherwise. 2474f6ad66aSAchin Gupta ******************************************************************************/ 24893d81d64SSandrine Bailleux static int load_bl31(bl31_params_t *bl2_to_bl31_params, 24993d81d64SSandrine Bailleux entry_point_info_t *bl31_ep_info) 2504f6ad66aSAchin Gupta { 251fb037bfbSDan Handley meminfo_t *bl2_tzram_layout; 2524112bfa0SVikram Kanigiri int e; 2534f6ad66aSAchin Gupta 2546ad2e461SDan Handley INFO("BL2: Loading BL3-1\n"); 25593d81d64SSandrine Bailleux assert(bl2_to_bl31_params != NULL); 25693d81d64SSandrine Bailleux assert(bl31_ep_info != NULL); 2574f6ad66aSAchin Gupta 2584f6ad66aSAchin Gupta /* Find out how much free trusted ram remains after BL2 load */ 259ee12f6f7SSandrine Bailleux bl2_tzram_layout = bl2_plat_sec_mem_layout(); 2604f6ad66aSAchin Gupta 26193d81d64SSandrine Bailleux /* Set the X0 parameter to BL3-1 */ 26203462671SAndrew Thoelke bl31_ep_info->args.arg0 = (unsigned long)bl2_to_bl31_params; 26303462671SAndrew Thoelke 2648f55dfb4SSandrine Bailleux /* Load the BL3-1 image */ 2654112bfa0SVikram Kanigiri e = load_image(bl2_tzram_layout, 266*16948ae1SJuan Castillo BL31_IMAGE_ID, 2674112bfa0SVikram Kanigiri BL31_BASE, 2684112bfa0SVikram Kanigiri bl2_to_bl31_params->bl31_image_info, 2694112bfa0SVikram Kanigiri bl31_ep_info); 270bcb79b90SSandrine Bailleux if (e) 271bcb79b90SSandrine Bailleux return e; 2724f6ad66aSAchin Gupta 273dec840afSJuan Castillo #if TRUSTED_BOARD_BOOT 274dec840afSJuan Castillo e = auth_verify_obj(AUTH_BL31_IMG, 275dec840afSJuan Castillo bl2_to_bl31_params->bl31_image_info->image_base, 276dec840afSJuan Castillo bl2_to_bl31_params->bl31_image_info->image_size); 277dec840afSJuan Castillo if (e) { 278dec840afSJuan Castillo ERROR("Failed to authenticate BL3-1 image.\n"); 279bcb79b90SSandrine Bailleux return e; 280dec840afSJuan Castillo } 281dec840afSJuan Castillo 282dec840afSJuan Castillo /* After working with data, invalidate the data cache */ 283dec840afSJuan Castillo inv_dcache_range(bl2_to_bl31_params->bl31_image_info->image_base, 284dec840afSJuan Castillo (size_t)bl2_to_bl31_params->bl31_image_info->image_size); 285dec840afSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */ 286dec840afSJuan Castillo 2874112bfa0SVikram Kanigiri bl2_plat_set_bl31_ep_info(bl2_to_bl31_params->bl31_image_info, 2884112bfa0SVikram Kanigiri bl31_ep_info); 289a3050ed5SAchin Gupta 29093d81d64SSandrine Bailleux return e; 291561cd33eSHarry Liebel } 292e4d084eaSAchin Gupta 29393d81d64SSandrine Bailleux /******************************************************************************* 29493d81d64SSandrine Bailleux * Load the BL3-2 image if there's one. 29593d81d64SSandrine Bailleux * The bl2_to_bl31_params param will be updated with the relevant BL3-2 29693d81d64SSandrine Bailleux * information. 29793d81d64SSandrine Bailleux * If a platform does not want to attempt to load BL3-2 image it must leave 29893d81d64SSandrine Bailleux * BL32_BASE undefined. 29993d81d64SSandrine Bailleux * Return 0 on success or if there's no BL3-2 image to load, a negative error 30093d81d64SSandrine Bailleux * code otherwise. 30193d81d64SSandrine Bailleux ******************************************************************************/ 30293d81d64SSandrine Bailleux static int load_bl32(bl31_params_t *bl2_to_bl31_params) 30393d81d64SSandrine Bailleux { 30493d81d64SSandrine Bailleux int e = 0; 3051151c821SDan Handley #ifdef BL32_BASE 30693d81d64SSandrine Bailleux meminfo_t bl32_mem_info; 30793d81d64SSandrine Bailleux 3086ad2e461SDan Handley INFO("BL2: Loading BL3-2\n"); 30993d81d64SSandrine Bailleux assert(bl2_to_bl31_params != NULL); 31093d81d64SSandrine Bailleux 31129fb905dSVikram Kanigiri /* 31293d81d64SSandrine Bailleux * It is up to the platform to specify where BL3-2 should be loaded if 31393d81d64SSandrine Bailleux * it exists. It could create space in the secure sram or point to a 3141151c821SDan Handley * completely different memory. 31529fb905dSVikram Kanigiri */ 3166871c5d3SVikram Kanigiri bl2_plat_get_bl32_meminfo(&bl32_mem_info); 3176871c5d3SVikram Kanigiri e = load_image(&bl32_mem_info, 318*16948ae1SJuan Castillo BL32_IMAGE_ID, 3194112bfa0SVikram Kanigiri BL32_BASE, 3204112bfa0SVikram Kanigiri bl2_to_bl31_params->bl32_image_info, 3214112bfa0SVikram Kanigiri bl2_to_bl31_params->bl32_ep_info); 32229fb905dSVikram Kanigiri 323bcb79b90SSandrine Bailleux if (e) 324bcb79b90SSandrine Bailleux return e; 325bcb79b90SSandrine Bailleux 326dec840afSJuan Castillo #if TRUSTED_BOARD_BOOT 327dec840afSJuan Castillo /* Image is present. Check if there is a valid certificate */ 328dec840afSJuan Castillo if (bl32_cert_error) { 329dec840afSJuan Castillo ERROR("Failed to authenticate BL3-2 certificates.\n"); 330bcb79b90SSandrine Bailleux return bl32_cert_error; 331dec840afSJuan Castillo } 332dec840afSJuan Castillo 333dec840afSJuan Castillo e = auth_verify_obj(AUTH_BL32_IMG, 334dec840afSJuan Castillo bl2_to_bl31_params->bl32_image_info->image_base, 335dec840afSJuan Castillo bl2_to_bl31_params->bl32_image_info->image_size); 336dec840afSJuan Castillo if (e) { 337dec840afSJuan Castillo ERROR("Failed to authenticate BL3-2 image.\n"); 338bcb79b90SSandrine Bailleux return e; 339dec840afSJuan Castillo } 340dec840afSJuan Castillo /* After working with data, invalidate the data cache */ 341dec840afSJuan Castillo inv_dcache_range(bl2_to_bl31_params->bl32_image_info->image_base, 342dec840afSJuan Castillo (size_t)bl2_to_bl31_params->bl32_image_info->image_size); 343dec840afSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */ 344dec840afSJuan Castillo 3454112bfa0SVikram Kanigiri bl2_plat_set_bl32_ep_info( 3464112bfa0SVikram Kanigiri bl2_to_bl31_params->bl32_image_info, 3474112bfa0SVikram Kanigiri bl2_to_bl31_params->bl32_ep_info); 3481151c821SDan Handley #endif /* BL32_BASE */ 3494112bfa0SVikram Kanigiri 35093d81d64SSandrine Bailleux return e; 35193d81d64SSandrine Bailleux } 35293d81d64SSandrine Bailleux 35393d81d64SSandrine Bailleux /******************************************************************************* 35493d81d64SSandrine Bailleux * Load the BL3-3 image. 35593d81d64SSandrine Bailleux * The bl2_to_bl31_params param will be updated with the relevant BL3-3 35693d81d64SSandrine Bailleux * information. 35793d81d64SSandrine Bailleux * Return 0 on success, a negative error code otherwise. 35893d81d64SSandrine Bailleux ******************************************************************************/ 35993d81d64SSandrine Bailleux static int load_bl33(bl31_params_t *bl2_to_bl31_params) 36093d81d64SSandrine Bailleux { 36193d81d64SSandrine Bailleux meminfo_t bl33_mem_info; 36293d81d64SSandrine Bailleux int e; 36393d81d64SSandrine Bailleux 3646ad2e461SDan Handley INFO("BL2: Loading BL3-3\n"); 36593d81d64SSandrine Bailleux assert(bl2_to_bl31_params != NULL); 36693d81d64SSandrine Bailleux 36793d81d64SSandrine Bailleux bl2_plat_get_bl33_meminfo(&bl33_mem_info); 36893d81d64SSandrine Bailleux 36993d81d64SSandrine Bailleux /* Load the BL3-3 image in non-secure memory provided by the platform */ 37093d81d64SSandrine Bailleux e = load_image(&bl33_mem_info, 371*16948ae1SJuan Castillo BL33_IMAGE_ID, 37293d81d64SSandrine Bailleux plat_get_ns_image_entrypoint(), 37393d81d64SSandrine Bailleux bl2_to_bl31_params->bl33_image_info, 37493d81d64SSandrine Bailleux bl2_to_bl31_params->bl33_ep_info); 37593d81d64SSandrine Bailleux 376bcb79b90SSandrine Bailleux if (e) 377bcb79b90SSandrine Bailleux return e; 378bcb79b90SSandrine Bailleux 379dec840afSJuan Castillo #if TRUSTED_BOARD_BOOT 380dec840afSJuan Castillo e = auth_verify_obj(AUTH_BL33_IMG, 381dec840afSJuan Castillo bl2_to_bl31_params->bl33_image_info->image_base, 382dec840afSJuan Castillo bl2_to_bl31_params->bl33_image_info->image_size); 383dec840afSJuan Castillo if (e) { 384dec840afSJuan Castillo ERROR("Failed to authenticate BL3-3 image.\n"); 385bcb79b90SSandrine Bailleux return e; 386dec840afSJuan Castillo } 387dec840afSJuan Castillo /* After working with data, invalidate the data cache */ 388dec840afSJuan Castillo inv_dcache_range(bl2_to_bl31_params->bl33_image_info->image_base, 389dec840afSJuan Castillo (size_t)bl2_to_bl31_params->bl33_image_info->image_size); 390dec840afSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */ 391dec840afSJuan Castillo 39293d81d64SSandrine Bailleux bl2_plat_set_bl33_ep_info(bl2_to_bl31_params->bl33_image_info, 39393d81d64SSandrine Bailleux bl2_to_bl31_params->bl33_ep_info); 39493d81d64SSandrine Bailleux 39593d81d64SSandrine Bailleux return e; 39693d81d64SSandrine Bailleux } 39793d81d64SSandrine Bailleux 39893d81d64SSandrine Bailleux /******************************************************************************* 39993d81d64SSandrine Bailleux * The only thing to do in BL2 is to load further images and pass control to 40093d81d64SSandrine Bailleux * BL3-1. The memory occupied by BL2 will be reclaimed by BL3-x stages. BL2 runs 40193d81d64SSandrine Bailleux * entirely in S-EL1. 40293d81d64SSandrine Bailleux ******************************************************************************/ 40393d81d64SSandrine Bailleux void bl2_main(void) 40493d81d64SSandrine Bailleux { 40593d81d64SSandrine Bailleux bl31_params_t *bl2_to_bl31_params; 40693d81d64SSandrine Bailleux entry_point_info_t *bl31_ep_info; 40793d81d64SSandrine Bailleux int e; 40893d81d64SSandrine Bailleux 4096ad2e461SDan Handley NOTICE("BL2: %s\n", version_string); 4106ad2e461SDan Handley NOTICE("BL2: %s\n", build_message); 4116ad2e461SDan Handley 41293d81d64SSandrine Bailleux /* Perform remaining generic architectural setup in S-EL1 */ 41393d81d64SSandrine Bailleux bl2_arch_setup(); 41493d81d64SSandrine Bailleux 415dec840afSJuan Castillo #if TRUSTED_BOARD_BOOT 416dec840afSJuan Castillo /* Initialize authentication module */ 417dec840afSJuan Castillo auth_init(); 418dec840afSJuan Castillo 419dec840afSJuan Castillo /* Validate the certificates involved in the Chain of Trust */ 420dec840afSJuan Castillo e = load_certs(); 421dec840afSJuan Castillo if (e) { 422dec840afSJuan Castillo ERROR("Chain of Trust invalid. Aborting...\n"); 423dec840afSJuan Castillo panic(); 424dec840afSJuan Castillo } 425dec840afSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */ 426dec840afSJuan Castillo 42793d81d64SSandrine Bailleux /* 42893d81d64SSandrine Bailleux * Load the subsequent bootloader images 42993d81d64SSandrine Bailleux */ 43093d81d64SSandrine Bailleux e = load_bl30(); 43193d81d64SSandrine Bailleux if (e) { 43293d81d64SSandrine Bailleux ERROR("Failed to load BL3-0 (%i)\n", e); 43393d81d64SSandrine Bailleux panic(); 43493d81d64SSandrine Bailleux } 43593d81d64SSandrine Bailleux 436ef538c6fSJuan Castillo /* Perform platform setup in BL2 after loading BL3-0 */ 437ef538c6fSJuan Castillo bl2_platform_setup(); 438ef538c6fSJuan Castillo 43993d81d64SSandrine Bailleux /* 44093d81d64SSandrine Bailleux * Get a pointer to the memory the platform has set aside to pass 44193d81d64SSandrine Bailleux * information to BL3-1. 44293d81d64SSandrine Bailleux */ 44393d81d64SSandrine Bailleux bl2_to_bl31_params = bl2_plat_get_bl31_params(); 44493d81d64SSandrine Bailleux bl31_ep_info = bl2_plat_get_bl31_ep_info(); 44593d81d64SSandrine Bailleux 44693d81d64SSandrine Bailleux e = load_bl31(bl2_to_bl31_params, bl31_ep_info); 44793d81d64SSandrine Bailleux if (e) { 44893d81d64SSandrine Bailleux ERROR("Failed to load BL3-1 (%i)\n", e); 44993d81d64SSandrine Bailleux panic(); 45093d81d64SSandrine Bailleux } 45193d81d64SSandrine Bailleux 45293d81d64SSandrine Bailleux e = load_bl32(bl2_to_bl31_params); 45393d81d64SSandrine Bailleux if (e) 45493d81d64SSandrine Bailleux WARN("Failed to load BL3-2 (%i)\n", e); 45593d81d64SSandrine Bailleux 45693d81d64SSandrine Bailleux e = load_bl33(bl2_to_bl31_params); 45793d81d64SSandrine Bailleux if (e) { 45893d81d64SSandrine Bailleux ERROR("Failed to load BL3-3 (%i)\n", e); 45993d81d64SSandrine Bailleux panic(); 46093d81d64SSandrine Bailleux } 46193d81d64SSandrine Bailleux 46203462671SAndrew Thoelke /* Flush the params to be passed to memory */ 46303462671SAndrew Thoelke bl2_plat_flush_bl31_params(); 46403462671SAndrew Thoelke 4654f6ad66aSAchin Gupta /* 46693d81d64SSandrine Bailleux * Run BL3-1 via an SMC to BL1. Information on how to pass control to 46793d81d64SSandrine Bailleux * the BL3-2 (if present) and BL3-3 software images will be passed to 46893d81d64SSandrine Bailleux * BL3-1 as an argument. 4694f6ad66aSAchin Gupta */ 47003462671SAndrew Thoelke smc(RUN_IMAGE, (unsigned long)bl31_ep_info, 0, 0, 0, 0, 0, 0); 4714f6ad66aSAchin Gupta } 472