/******************************************************************************/
/* Williams Advanced Engineering Limited                     Copyright(C) 2021*/
/* Grove                                                  All rights reserved */
/*                                                                            */
/******************************************************************************/


#ifndef MAINSOURCES_INCLUDE_IOS_CHECKBOOTLOADERSIGNATURE_H_
#define MAINSOURCES_INCLUDE_IOS_CHECKBOOTLOADERSIGNATURE_H_


/*------------------------------------------------------------*/
/*          Includes                                          */
/*------------------------------------------------------------*/
#include <crc_hal.h>
#include "hal_stdtypes.h"
#include "crc_tbl.h"
#include "sys_dma.h"
#include "rti.h"


/*------------------------------------------------------------*/
/*          Defines                                           */
/*------------------------------------------------------------*/
#define APP_INFO_ADDRESS        0x002BFED0
#define APP_CRC_TABLE_ADDRESS   0x002BFF50
#define CAL_INFO_ADDRESS        0x002FFF60
#define CAL_CRC_TABLE_ADDRESS   0x002FFFE0

#define COMPATIBILITY_MARKER_WAEBMUVX  0x574145424D555658ULL

#define NUM_RECORDS_BOOT_CRC_TABLE  9
#define NUM_RECORDS_APP_CRC_TABLE   3
#define NUM_RECORDS_CAL_CRC_TABLE   1

#define SIGNATURE_INVALID 0xFFFFFFFFFFFFFFFFUL // Max uint64 - invalid value

/*------------------------------------------------------------*/
/*          Type Definitions                                  */
/*------------------------------------------------------------*/
typedef enum
{
    RESERVE = 0,
    OFFICIAL_RELEASE_CLASS,
    DEV_RELEASE_OFFICIAL_CLASS,
    DEV_RELEASE_NOT_OFFICIAL_CLASS,
    DEV_RELEASE_NOT_OFFICIAL_CLASS_NON_VER
}   BUILD_TYPE;

typedef struct {
    uint64 CompatibilityTag;
    uint8 Hw_Revision;
    uint8 major_version;
    uint8 minor_version;
    uint8 revision;
    BUILD_TYPE build_type;
} MEM_BLOCK_INFO;


typedef enum
{
    VerifyJobReqstNone = 0,
    VerifyAppSecIntegrity,
    VerifyCalSecIntegrity,
    VerifyAppAndCalSecIntegrity
} IntegrityVerificationJobType;

typedef enum
{
    VerifyJobComplete = 0,
    VerifyJobInit,
    VerifyJobInProgress
} IntegrityVerificationJobStatusType;

typedef enum
{
    VerifStatus_Undetermined = 0,
    VerifStatus_Passed,
    VerifStatus_Failed
}SecVerificationStatusType;

typedef struct
{
    IntegrityVerificationJobType CurrentVerifJobReq;
    IntegrityVerificationJobStatusType CurrentVerifJobStatus;
} VerifyMngrStatusType;

typedef struct my_generic_crc_table
{
uint32_t         rec_size;
uint32_t         num_recs;
CRC_RECORD       recs[NUM_RECORDS_BOOT_CRC_TABLE];
} GENERIC_CRC_TABLE_TYPE;


typedef struct
{
    GENERIC_CRC_TABLE_TYPE  * g_crcTable;
    sint32 i32LastEntry;
    bool   bInitialized;
    enum   dmaCHANNEL eDmaChannel;
    uint32 u32DmaRequLine;
    uint64 ulSignatureValue;
    uint8 bSignatureStatus;
} crcSemiCpuCtrlType;


/*------------------------------------------------------------*/
/*          Global Variables definition                       */
/*------------------------------------------------------------*/
extern crcSemiCpuCtrlType gMyCrcCtrl;

/*------------------------------------------------------------*/
/*          Functions prototypes definition                   */
/*------------------------------------------------------------*/
/* Public */
extern uint8    fnIosCheckBootLoaderSignature     (void);
extern void     fnIosCheckBootLoaderSignatureFast (void);
extern uint8    fnIosGetSemiCPUSignatureStatus    (void);
extern void     fnIosSetBootLoaderSignatureValues (void);

extern void fnIosResetAppSectionVerificationStatus(void);
extern void fnIosResetCalSectionVerificationStatus(void);
extern void fnIosResetAllVerificationStatus(void);

extern void    fnIosSetVerifySectionIntegrityRqst(IntegrityVerificationJobType tSectionToVerify);
extern IntegrityVerificationJobType fnIosGetCurrentVerifyRqst(void);
extern IntegrityVerificationJobStatusType fnIosGetCurrentVerifyJobStatus(void);

extern SecVerificationStatusType fnIosGetAppSecVerificationStatus(void);
extern SecVerificationStatusType fnIosGetCalSecVerificationStatus(void);
extern void fnIosSetAppSecVerificationStatus(SecVerificationStatusType Status);
extern void fnIosSetCalSecVerificationStatus(SecVerificationStatusType Status);

extern void fnIosCheckApplicationCompatibility(void);
extern void fnIosCheckCalibrationCompatibility(void);
extern SecVerificationStatusType fnIosGetApplicationCompatibilityStatus(void);
extern SecVerificationStatusType fnIosGetCalibrationCompatibilityStatus(void);
extern void fnIosSetApplicationCompatibilityStatus(SecVerificationStatusType Status);
extern void fnIosSetCalibrationCompatibilityStatus(SecVerificationStatusType Status);


extern void     fnIosVerifySectionIntegrityMngr (void);

extern uint64   fnIosGetBootLoaderSignatureValue(void);

/* Private */
void crcSemiCpuMode(crcBASE_t *crc, crcSemiCpuCtrlType * crcSemiCpuCtrl);


#endif /* MAINSOURCES_INCLUDE_IOS_CHECKBOOTLOADERSIGNATURE_H_ */
