/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * 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 the copyright holder 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.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 * Description:
 *
 *---------------------------------------------------------------------------*/

#ifndef X_LDR_ENV_H
#define X_LDR_ENV_H

#include "c_model.h"
#include "x_bim.h"

#ifndef CC_LDR_ENV_OFFSET
#define CC_LDR_ENV_OFFSET            (DRAM_START + 0xf000) // use 60k~64k to save loader env.
#endif /* CC_LDR_ENV_OFFSET */

#ifndef LDR_ENV_CUSTOM_SIZE
#if defined(CC_S_PLATFORM) && (defined(CC_HK_CUST) || defined(CC_CP_CUST))
#define LDR_ENV_CUSTOM_SIZE (1024)
#else
#define LDR_ENV_CUSTOM_SIZE (0)
#endif
#endif // LDR_ENV_CUSTOM_SIZE

#define LDR_TIMESTAMP_SIZE 32

// for loader use
#define LDR_SetDramTimeLog(x)   ((LDR_ENV_T*)CC_LDR_ENV_OFFSET)->au4TimeStamp[(x)] = BIM_READ32(REG_RW_TIMER2_LOW)
#define LDR_GetDramTimeLog(x)   ((LDR_ENV_T*)CC_LDR_ENV_OFFSET)->au4TimeStamp[(x)]

#define USIG_VERIFY_FAIL 0
#define USIG_DO_UPGRADE '1'
#define USIG_RUN_ON_USB '2'
#define USIG_DO_DIAG    '3'

typedef struct
{
    CHAR    szCustomer[16];
    CHAR    szMagic[16];
    CHAR    szModel[32];
    CHAR    szVersion[64];
} LDR_ENV_HDR_T;

typedef union
{
    UINT8           au1Raw[128];
    LDR_ENV_HDR_T   rHdr;
} LDR_ENV_HDR_UNION_T;

typedef struct
{
    UINT32 u4LoaderPlainSize;
    UINT32 u4LoaderLzhsSize;
    UINT32 u4LoaderLzhsAddress;
    UINT32 u4CfgFlag;
    //0x010
    LDR_ENV_HDR_UNION_T u; //128byte
    //0x090
    UINT32 au4TimeStamp[LDR_TIMESTAMP_SIZE]; //128byte
    //0x110
    UINT8 au1DtvCfg[16];        // DTVCFG_T use 9bytes
    //0x120
    UINT32 au4CustKey[64];      // 2048bit key
    //0x220
    CHAR szUenv[128];           // additional uboot env
    //0x2a0
#if defined(CC_EMMC_BOOT)
    UINT8 szMSDCenv[72];        // preloader -> loader
    //0x2E0
    UINT8 szMSDCenv1[64];       // loader -> uboot
    //0x320
    UINT32 szMSDCAutoK[18];     // loader -> uboot
    //
    UINT32 szMSDCETT[5];        // loader -> uboot
#endif
#if defined(CC_LOADER_CONSOLE_KEY_SUPPORT)
    UINT8 console_key[16];      // preloader -> loader
    UINT8 console_iv[16];       // preloader -> loader
#endif
#if defined(CC_SECURE_BOOT_SCRAMBLE) || (defined(CC_DLM_V2) && defined(CC_SECURE_BOOT_V3))
    UINT8 aes_key2[16];         // preloader -> loader
    UINT8 aes_iv2[16];          // preloader -> loader
#endif
#if defined(CC_S_PLATFORM)
    UINT8 AndroidSerailNo[16];  // customized android serial no.
#endif
#ifdef CC_CMDLINE_UPGRADE_STATUS
    UINT32 u4UpgradeStatus;     // loader -> uboot -> kernel cmdline
#endif
    UINT8 au1CustArea[LDR_ENV_CUSTOM_SIZE];
#ifdef CC_SUPPORT_AB_UPDATE
    UINT8 RPMB_KEY[32];         // loader -> uboot
#endif // CC_SUPPORT_AB_UPDATE
#ifdef CC_MAPLE_LOADER_UPGRADE_VERSION_CTRL_FLOW
    INT32 Isconsolelocked;
    UINT8 versionHmacKey[32];
#endif
#ifdef CC_FBM_DYNMJC
    UINT32 u4SupportMJC;      // loader -> uboot
#endif
} LDR_ENV_T;

#ifdef CC_SUPPORT_AB_UPDATE
#include <x_tz.h>
#define STRD_RB_HASH_SIZE 4
//#define FLAG_RST_STRD_RB_INDEX_BASE (DRAM_START + 0x10000)
#define FLAG_RST_STRD_RB_INDEX_BASE (TRUSTZONE_ENTRY + 10000)
//#define FLAG_RST_STRD_RB_INDEX_BASE 0xfa710000
#define FLAG_RST_STRD_RB_INDEX_MAGIC 0x46697368

typedef struct
{
    UINT8   magic[4];
    UINT8   flag;
    UINT8   pad[3];
} LDR_RST_STRD_RB_INDEX_IN_T;

typedef struct
{
    LDR_RST_STRD_RB_INDEX_IN_T   rFlgRstStrdRBIndx;
    UINT8   hash[STRD_RB_HASH_SIZE];
} LDR_RST_STRD_RB_INDEX_T;
#endif /* CC_SUPPORT_AB_UPDATE */

#endif // X_LDR_ENV_H
