• Yatharth Kochar's avatar
    Fix the inconsistencies in bl1_tbbr_image_descs[] · 843ddee4
    Yatharth Kochar authored
    This patch fixes inconsistencies in bl1_tbbr_image_descs[]
    and miscellaneous fixes in Firmware Update code.
    
    Following are the changes:
    * As part of the original FWU changes, a `copied_size`
      field was added to `image_info_t`. This was a subtle binary
      compatibility break because it changed the size of the
      `bl31_params_t` struct, which could cause problems if
      somebody used different versions of BL2 or BL31, one with
      the old `image_info_t` and one with the new version.
      This patch put the `copied_size` within the `image_desc_t`.
    * EXECUTABLE flag is now stored in `ep_info.h.attr` in place
      of `image_info.h.attr`, associating it to an entrypoint.
    * The `image_info.image_base` is only relevant for secure
      images that are copied from non-secure memory into secure
      memory. This patch removes initializing `image_base` for
      non secure images in the bl1_tbbr_image_descs[].
    * A new macro `SET_STATIC_PARAM_HEAD` is added for populating
      bl1_tbbr_image_descs[].ep_info/image_info.h members statically.
      The version, image_type and image attributes are now
      populated using this new macro.
    * Added PLAT_ARM_NVM_BASE and PLAT_ARM_NVM_SIZE to avoid direct
      usage of V2M_FLASH0_XXX in plat/arm/common/arm_bl1_fwu.c.
    * Refactoring of code/macros related to SECURE and EXECUTABLE flags.
    
    NOTE: PLATFORM PORTS THAT RELY ON THE SIZE OF `image_info_t`
          OR USE the "EXECUTABLE" BIT WITHIN `image_info_t.h.attr`
          OR USE THEIR OWN `image_desc_t` ARRAY IN BL1, MAY BE
          BROKEN BY THIS CHANGE. THIS IS CONSIDERED UNLIKELY.
    
    Change-Id: Id4e5989af7bf0ed263d19d3751939da1169b561d
    843ddee4
bl1_context_mgmt.c 3.86 KB
/*
 * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <arch_helpers.h>
#include <assert.h>
#include <context.h>
#include <context_mgmt.h>
#include <platform.h>

/*
 * Following array will be used for context management.
 * There are 2 instances, for the Secure and Non-Secure contexts.
 */
static cpu_context_t bl1_cpu_context[2];

/* Following contains the cpu context pointers. */
static void *bl1_cpu_context_ptr[2];


void *cm_get_context(uint32_t security_state)
{
	assert(sec_state_is_valid(security_state));
	return bl1_cpu_context_ptr[security_state];
}

void cm_set_context(void *context, uint32_t security_state)
{
	assert(sec_state_is_valid(security_state));
	bl1_cpu_context_ptr[security_state] = context;
}

/*******************************************************************************
 * This function prepares the context for Secure/Normal world images.
 * Normal world images are transitioned to EL2(if supported) else EL1.
 ******************************************************************************/
void bl1_prepare_next_image(unsigned int image_id)
{
	unsigned int security_state;
	image_desc_t *image_desc;
	entry_point_info_t *next_bl_ep;

	/* Get the image descriptor. */
	image_desc = bl1_plat_get_image_desc(image_id);
	assert(image_desc);

	/* Get the entry point info. */
	next_bl_ep = &image_desc->ep_info;

	/* Get the image security state. */
	security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);

	/* Setup the Secure/Non-Secure context if not done already. */
	if (!cm_get_context(security_state))
		cm_set_context(&bl1_cpu_context[security_state], security_state);

	/* Prepare the SPSR for the next BL image. */
	if (security_state == SECURE) {
		next_bl_ep->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
				   DISABLE_ALL_EXCEPTIONS);
	} else {
		/* Use EL2 if supported else use EL1. */
		if (read_id_aa64pfr0_el1() &
			(ID_AA64PFR0_ELX_MASK << ID_AA64PFR0_EL2_SHIFT)) {
			next_bl_ep->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
				DISABLE_ALL_EXCEPTIONS);
		} else {
			next_bl_ep->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
			   DISABLE_ALL_EXCEPTIONS);
		}
	}

	/* Allow platform to make change */
	bl1_plat_set_ep_info(image_id, next_bl_ep);

	/* Prepare the context for the next BL image. */
	cm_init_my_context(next_bl_ep);
	cm_prepare_el3_exit(security_state);

	/* Indicate that image is in execution state. */
	image_desc->state = IMAGE_STATE_EXECUTED;

	print_entry_point_info(next_bl_ep);
}