/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2024. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V3.1.0  AC7840x  Demo
 ******************************************************************************/

#include "osif.h"
#include "clock_config.h"
#include "debugout_ac7840x.h"
#include "cse_functions.h"
#include "flash.h"
#include "cse_keys.h"

/*!
 *例程说明：
 *AC7840x CSE模块例程:
 *更新例程2加载的密钥
 */

/*! 秘钥属性说明
 * bit5 Write Protection enable: 1 disable: 0
 * bit4 Boot Protection  enable: 1 disable: 0
 * bit3 Debugger Protection enable:1 disable:0
 * bit2 Key Usage enable:1 generate & verify cmac  disable: 0 encryption and decryption
 * bit1 Wildcard enable:1 disable:0
 * bit0 Verify Only enable:1  disable:0
*/
#define CSE_ATTRIBUTE_WP_Msk            0x20
#define CSE_ATTRIBUTE_WP_Pos            5
#define CSE_ATTRIBUTE_BP_Msk            0x10
#define CSE_ATTRIBUTE_BP_Pos            4
#define CSE_ATTRIBUTE_DP_Msk            0x8
#define CSE_ATTRIBUTE_DP_Pos            3
#define CSE_ATTRIBUTE_KEYUSER_Msk       0x4
#define CSE_ATTRIBUTE_KEYUSER_Pos       2
#define CSE_ATTRIBUTE_Wildcard_Msk      0x2
#define CSE_ATTRIBUTE_Wildcard_Pos      1
#define CSE_ATTRIBUTE_VerifyOnly_Msk    0x1
#define CSE_ATTRIBUTE_VerifyOnly_Pos    0

flash_config_t  flash_Config = {0};
uint8_t         M1[16] = {0};
uint8_t         M2[32] = {0};
uint8_t         M3[16] = {0};
uint8_t         M4[32] = {0};
uint8_t         M5[16] = {0};
uint8_t         M4_out[32]= {0};
uint8_t         M5_out[16]= {0};
uint8_t RND_Numpp[16] = {0x00u};

cse_state_t cse_State;

/*!
 * @brief 时钟配置函数
 *
 * @param[in] none
 *
 * @return none
 */
static void SystemClock_Config(void);

void Numpy_Put(uint8_t* Numpy, uint8_t size)
{
    uint8_t i = 0;
    printf("Numpy = ");
    for (i = 0; i < size; i++)
    {
        printf("0x%x, ", Numpy[i]);
    }
    printf("\r\n");
}

/*!
 * @brief main
 *
 * @param[in]  none
 *
 * @return 0: success, other: error value
 */
int main(void)
{
    status_t Res;

    SystemClock_Config();

    InitDebug();

    printf("demo start...\r\n");

    FLASH_DRV_Init(&Flash1_InitConfig, &flash_Config);

    if (flash_Config.flexRAMSize == 0)
    {
        FLASH_DRV_UnlockCtrl();
        Res = FLASH_DRV_Partition(&flash_Config, 0x00, 0x03, true);
        printf("FLASH_DRV_Partition,Res = 0x%x \r\n", Res);
        OSIF_TimeDelay(5);
        NVIC_SystemReset();
    }

    /*initialize CSE*/
    CSE_DRV_Init(&cse_State);

    /*initialize RNG*/
    Res = CSE_DRV_InitRNG();
    printf("CSE_DRV_InitRNG,Res = 0x%x \r\n", Res);

    /*Update Key1*/
    Res = CalculateM1ToM5(MASTER_ECU_KEY, USER_KEY01, CSE_MASTER_ECU, CSE_KEY_1, 2u, 0u, M1, M2, M3, M4, M5); /* Calculate M1 to M5 in Software */
    printf("CalculateM1ToM5,Res = 0x%x \r\n", Res);

    Res = CSE_DRV_LoadKey(CSE_KEY_1, M1, M2, M3, M4_out, M5_out);
    printf("CSE_DRV_LoadKey,Res = 0x%x \r\n", Res);

    /*Update Key11*/
    Res = CalculateM1ToM5(MASTER_ECU_KEY, USER_KEY11, CSE_MASTER_ECU, CSE_KEY_11, 2u, 0u, M1, M2, M3, M4, M5); /* Calculate M1 to M5 in Software */
    printf("CalculateM1ToM5,Res = 0x%x \r\n", Res);

    Res = CSE_DRV_LoadKey(CSE_KEY_11, M1, M2, M3, M4_out, M5_out);
    printf("CSE_DRV_LoadKey,Res = 0x%x \r\n", Res);

    (void)Res;
    printf("demo end...\r\n");
    while (1);
}

/*!
 * @brief 时钟配置函数
 *
 * @param[in] none
 *
 * @return none
 */
static void SystemClock_Config(void)
{
    CKGEN_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
                   g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
    CKGEN_DRV_UpdateConfiguration(0, CLOCK_MANAGER_POLICY_AGREEMENT);
}
