/*******************************************************************************
 * Copyright (c) 2018-2021 Renesas Electronics Corporation. All rights reserved.
 *
 *     RENESAS ELECTRONICS CONFIDENTIAL AND PROPRIETARY
 *
 * This software is provided as reference/sample code under the license
 * agreement between Renesas Electronics Corporation and licensee (the
 * "License Agreement") and shall be treated as specified in the License
 * Agreement.
 * These instructions, statements, and software are the confidential
 * information of Renesas Electronics Corporation. They must be used and
 * modified solely for the purpose for which it was furnished by Renesas
 * Electronics Corporation. All or part of these instructions, statements and
 * software must not be reproduced nor disclosed to any third party in any
 * form, unless permitted by the License Agreement.
 *
 * THIS SOFTWARE IS PROVIDED BY RENESAS ELEOCTRONICS CORPORATION "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
 * SATISFACTORY QUALITY, ACCURACY, TITLE AND NON-INFRINGEMENT ARE DISCLAIMED.
 * IN NO EVENT SHALL RENESAS ELECTRONICS CORPORATION BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, PUNITIVE, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * 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.
 ******************************************************************************/

/*******************************************************************************
 * DESCRIPTION   : Image load function header
 ******************************************************************************/

#ifndef LOAD_IMAGE_H_
#define LOAD_IMAGE_H_

/* define */
/* Flash address */
#define FLASH_BASE      (0x08000000U)
#define FLASH_SIZE      (0x04000000U)
#define FLASH_END       ((FLASH_BASE + FLASH_SIZE) - 1U)
/* DRAM address */
#define DRAM_BASE       (0x40000000U)
#define DRAM_SIZE       (0x80000000U)
#define DRAM_END        ((DRAM_BASE + DRAM_SIZE) - 1U)
/* RT-SRAM */
#define RTSRAM_BASE     (0xEB200000U)
#define RTSRAM_SIZE     ((1024U-18U)*1024U)
#define RTSRAM_END      ((RTSRAM_BASE + RTSRAM_SIZE) - 1U)
/* ICUMXA Loader */
#define IPL_TOP         (0xEB2D8000U)
#define IPL_END         (0xEB2FFFFFU)

/* Flash address of content certificate */
#define CONTENT_CERT_SA         (6U)        /* Content Cert SA6 */
#define SA_SIZE                 (0x00040000U)
#define FLASH_CONTENT_CERT_ADDR (uint32_t)(FLASH_BASE + (SA_SIZE * CONTENT_CERT_SA))    /* FLASH Base + SA6 offset */

/* Certificate logical address */
extern char __ghsbegin_key_load[];
extern char __ghsbegin_cert_load[];

#define KEY_CERT_DEST_ADDR  (uint32_t)(&__ghsbegin_key_load[0])
#define CONTENT_CERT_DEST_ADDR  (uint32_t)(&__ghsbegin_cert_load[0])

/* Certificate size */
#define KEY_CERT_SRC_OFFSET         (0x00000F00U)
#define KEY_CERT_SIZE               (0x00000400U)   /* Key cert size */
#define CONTENT_CERT_INFO_SIZE      (0x00000400U)   /* CA53 program num */
#define CONTENT_CERT_SRC_OFFSET(x)  (CONTENT_CERT_INFO_SIZE + ((uint32_t)(x) * CONTENT_CERT_SEC_SIZE))  /* content cert src offsett */
#define CONTENT_CERT_DST_OFFSET(x)  (CONTENT_CERT_INFO_SIZE + ((uint32_t)(x) * CONTENT_CERT_DST_SIZE))  /* content cert dst offset */
#define CONTENT_CERT_SEC_SIZE       (0x00000800U)   /* content cert src size */
#define CONTENT_CERT_DST_SIZE       (0x00000400U)   /* content cert dst size */

/* Load ID */
#define SECURE_FW_ID        (0U)
#define RTOS_ID             (1U)
#define CA53_PROGRAM_ID     (2U)

/* Number of Max loading image */
#define CA53_MAX_IMAGE      (8U)        /* CA53 program MAX image num */
#define MAX_PLACED          (10U)

/* Cert Info Source Address Offset */
#define SRC_ADDR_OFFSET(x)  (((uint32_t)(x) * 0x10U) + 0x08U)

#define TARGET_MEM_DRAM     (0U)
#define TARGET_MEM_RTSRAM   (1U)

/* get info from cert address offset */
#define CERT_INFO_SIZE_OFFSET   (0x00000264U)   /* Offset Type1 */
#define CERT_INFO_DST_OFFSET    (0x00000154U)   /* Offset Type1 */
#define CERT_INFO_SIZE_OFFSET1  (0x00000364U)   /* Offset Type2 */
#define CERT_INFO_DST_OFFSET1   (0x000001D4U)   /* Offset Type2 */


/* struct */
/* load address range */
typedef struct {
    uint32_t    topadd;
    uint32_t    endadd;
} ADDRESS_RANGE;

/* load info */
typedef struct{
    const char *name;   /* store load image name */
    uint32_t boot_addr; /* store boot address of image */
    uint32_t cert_addr; /* store content cert address */
    uint32_t src_addr;  /* store source address */
} LOAD_INFO;


/* Prototype */
void load_key_cert(void);
uint32_t load_content_cert(void);
uint32_t load_image(uint32_t cert_addr, uint32_t flash_src_offset,
    const char *name);
#endif /* LOAD_IMAGE_H_ */

