/*******************************************************************************
 * 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 2022 Renesas Electronics Corporation All rights reserved.
 *******************************************************************************/

/*******************************************************************************
 * DESCRIPTION   : Image load function for MCU
 ******************************************************************************/
/******************************************************************************
 * @file          image_load_for_mcu.c
 * - Version      : 0.01
 * @brief         Loading image driver for MCU.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 04.07.2022 0.01     First Release
 *****************************************************************************/

/* indelude */
#include <stdint.h>
#include <sdmac.h>
#include <image_load_for_mcu.h>
#include <loader_main_mcu.h>
#include <rom_api.h>
#if (RCAR_SA9_TYPE == FLASH_BOOT)
#include <image_load_flash.h>
#elif (RCAR_SA9_TYPE == EMMC_BOOT)
#include <image_load_emmc.h>
#endif /* (RCAR_SA9_TYPE == FLASH_BOOT) */

void image_load_for_mcu(const LOAD_INFO *li, uint32_t is_verify)
{
    uint32_t aes_flg = MCU_IMG_NOT_ENCRYPTED;

    if (is_verify != NORMAL_BOOT)
    {
        /* A flag whether MCU image is encrypted */
        aes_flg = get_aes_flg_in_cert(li -> cnt_cert_addr);
    }

    if(aes_flg == MCU_IMG_NOT_ENCRYPTED)
    {
        /* Transfer by SDMAC on ICUMX */
        icu_sdmac_trans_start(li);
    }
    else
    {
        /* Transfer by AES driver on ICUMX */
        mcu_img_decrypt(li);
    }
}
/* End of function image_load_for_mcu(const LOAD_INFO *li) */

void load_end_for_mcu(const LOAD_INFO *li, uint32_t is_verify)
{
    uint32_t aes_flg = MCU_IMG_NOT_ENCRYPTED;

    if (is_verify != NORMAL_BOOT)
    {
        /* A flag whether MCU image is encrypted */
        aes_flg = get_aes_flg_in_cert(li -> cnt_cert_addr);
    }

    if(aes_flg == MCU_IMG_NOT_ENCRYPTED)
    {
        icu_sdmac_trans_end();
    }
    else
    {
        mcu_img_decrypt_end(li);
    }
}
/* End of function load_end_for_mcu(const LOAD_INFO *li) */

void load_image_info_print(const LOAD_INFO *li)
{
#if (RCAR_SA9_TYPE == FLASH_BOOT)
    load_image_info_print_for_flash(li);
#elif (RCAR_SA9_TYPE == EMMC_BOOT)
    load_image_info_print_for_emmc(li);
#endif /* (RCAR_SA9_TYPE == FLASH_BOOT) */
}
/* End of function load_image_info_print(const LOAD_INFO *li) */

void load_securedata_for_mcu(void)
{
    LOAD_INFO tmp_li;

    tmp_li.image_size = SECUREDATA_SIZE;
    tmp_li.src_addr   = RTVRAM_BASE;
    tmp_li.boot_addr  = DATA_SRAM_BASE;
#if (RCAR_SA9_TYPE == EMMC_BOOT)
    tmp_li.part_num  = EMMC_PARTITION_1;
#endif
    /*  */
    icu_sdmac_trans_start(&tmp_li);
    icu_sdmac_trans_end();
}/* End of function load_securedata(uint32_t target_id) */
