/*******************************************************************************
 * DISCLAIMER
 * This software is supplied by Renesas Electronics Corporation and is only
 * intended for use with Renesas products. No other uses are authorized. This
 * software is owned by Renesas Electronics Corporation and is protected under
 * all applicable laws, including copyright laws.
 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * Renesas reserves the right, without notice, to make changes to this software
 * and to discontinue the availability of this software. By using this software,
 * you agree to the additional terms and conditions found by accessing the
 * following link:
 * http://www.renesas.com/disclaimer
 * Copyright 2021-2022 Renesas Electronics Corporation All rights reserved.
 *******************************************************************************/

/*******************************************************************************
 * DESCRIPTION   : Image load for emmc function
 ******************************************************************************/
/******************************************************************************
 * @file          image_load_emmc.c
 * - Version      : 0.04
 * @brief         Image load for emmc function.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 30.09.2021 0.01     First Release
 *         : 23.05.2022 0.02     Support for updating the memory map.
 *         : 05.08.2022 0.03     Add load_ver_tbl_cert_for_emmc function.
 *         : 30.09.2022 0.04     Modify size output in
 *                               load_ver_tbl_cert_for_emmc function.
 *****************************************************************************/

/* indelude */
#include <stdint.h>
#include <image_load.h>
#include <image_load_emmc.h>
#include <remap.h>
#include <log.h>
#include <mem_io.h>
#include <rom_api.h>
#include <emmc_multiboot.h>
#include <emmc_hal.h>
#include <emmc_std.h>

static void load_ver_tbl_cert_for_emmc(void);

uint32_t load_content_cert_for_emmc(void)
{
    uint32_t load_num;
    uint32_t phys_dst;
    uint32_t phys_src;
    uint32_t size;
    uint32_t part;

    /* source address.(0x00240000/sector:0x1200) */
    phys_src = EMMC_CONTENT_CERT_SECTOR_NUMBER;
    /* Get physical address of transfer destination. */
    phys_dst = remap_get_phys_addr(SA9_DEST_ADDR);
    /* transfer size(number of secters) */
    size = CONTENT_CERT_INFO_SIZE >> EMMC_SECTOR_SIZE_SHIFT;
    /* The partition that contains A. */
    part = (uint32_t)PARTITION_ID_BOOT_1;

    /* Load content cert header */
    (void)emmc_trans_data(part, phys_src, phys_dst, size);

    NOTICE(
        "======== content cert info ========\n"
        "destination address:0x%08x\n"
        "physical destination address:0x%08x\n"
        "source address:(p:%d)0x%08x\n"
        "size:0x%08x\n", SA9_DEST_ADDR, phys_dst,
        PARTITION_ID_BOOT_1, EMMC_CONTENT_CERT_ADDR, CONTENT_CERT_INFO_SIZE);

    load_num = mem_read32(SA9_DEST_ADDR);

    /* Check number of image load.
       In case of number of image load is 0, error of transfer parameter.
       In case of number of image loads is higher than 8,
       the transfer parameter error.                */
    if ((load_num == 0U) || (load_num > CA_MAX_IMAGE))
    {
        ERROR("Content cert info 'load image num' fault.\n");
        ERROR("load image num = %d\n",load_num);
        panic;
    }

    /* Increase forwarding address by the size of cert header */
    phys_src += (CONTENT_CERT_INFO_SIZE >> EMMC_SECTOR_SIZE_SHIFT);
    phys_dst += CONTENT_CERT_INFO_SIZE;

    /* Transfer size calculation for SA9                                               *
     * TFMV key + NTFMV key + minimum version table + (content cert * number of loads) */
    size = ((KEY_CERT_SIZE * 2U) + MIN_VER_TBL_SIZE
             + ((NUM_OF_ALWAYS_LOAD_CERT + load_num) * CONTENT_CERT_SIZE));

    (void)emmc_trans_data(part, phys_src, phys_dst, size >> EMMC_SECTOR_SIZE_SHIFT);

    NOTICE(
        "======== content cert ========\n"
        "address:0x%08x size:0x%08x\n", phys_dst, size);

    /* Load content cert of Software minimum version table */
    load_ver_tbl_cert_for_emmc();

    return load_num;
}
/* End of function load_content_cert_for_emmc(void) */

static void load_ver_tbl_cert_for_emmc(void)
{
#if (SW_VERSION_CHECK == OPT_VERSION_CHECK_ENABLE)
    uint32_t phys_dst;
    uint32_t phys_src;
    uint32_t size;
    uint32_t part;

    /* source address.(0x0024D000/sector:0x1268) */
    phys_src = EMMC_VER_TBL_CNT_CERT_SEC_NUM;
    /* Get physical address of transfer destination. */
    phys_dst = remap_get_phys_addr(SA9_DEST_ADDR + EMMC_VER_TBL_OFFSET);
    /* transfer size(number of secters) */
    size = (CONTENT_CERT_SIZE * 2U) >> EMMC_SECTOR_SIZE_SHIFT;
    /* The partition that contains content cert of Software minimum version table. */
    part = (uint32_t)PARTITION_ID_BOOT_1;

    /* Load content cert of Software minimum version table */
    (void)emmc_trans_data(part, phys_src, phys_dst, size);

    NOTICE("======== content cert of SW version table ========\n"
           "address:0x%08x size:0x%08x\n", phys_dst, (size << EMMC_SECTOR_SIZE_SHIFT));

#endif /* (SW_VERSION_CHECK == OPT_VERSION_CHECK_ENABLE) */
}
/* End of function load_ver_tbl_cert_for_emmc(void) */

