/**
*   @file    global_variables.h
*
*   @brief   Contains the global variables.
*   @details This file contains the definitions for global variables
             used to install the security features on a device.
*
*   @addtogroup security_installer
*   @{
*/
/*==================================================================================================
*
*   Copyright 2020-2021 NXP.*
*   This software is owned or controlled by NXP and may only be used strictly in accordance with
*   the applicable license terms. By expressly accepting such terms or by downloading, installing,
*   activating and/or otherwise using the software, you are agreeing that you have read, and that
*   you agree to comply with and are bound by, such license terms. If you do not agree to
*   be bound by the applicable license terms, then you may not retain, install, activate or
*   otherwise use the software.
==================================================================================================*/

#ifndef GLOBAL_VARIABLES_H
#define GLOBAL_VARIABLES_H

#ifdef __cplusplus
extern "C"{
#endif

/*=============================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
=============================================================================*/
#include "global_defs.h"
#include "global_types.h"
#include "demo_app_services.h"
/*=============================================================================
*                              SOURCE FILE VERSION INFORMATION
=============================================================================*/

/*=============================================================================
*                                     FILE VERSION CHECKS
=============================================================================*/

/*=============================================================================
*                                          CONSTANTS
=============================================================================*/

/*=============================================================================
*                                      DEFINES AND MACROS
=============================================================================*/
#define HEADER_APP_TAG                          0xD5U
#define APP_HEADER_LENGTH                       0x40U

/* the code satrt address must 128Byte(0x80u) align
 * when the starting address of AppBL is at 0x00504000,
 * even if the AppBL structure itself only occupies 64Byte(0x40u).
 * the App code address must start at 0x00504080
 * */
#define APP_START_ADDR_OFFSET                   (0x80U)

#define APPBL_START_ADDR_OFFSET                   (0xC0U)

/* 0x00400000 0x00600000 */
#ifdef HSE_S32K3XX_OTA_ENABLE
    #define OTA_OFFSET  (0x00200000u)
#else
    #define OTA_OFFSET  (0x00000000u)
#endif

#define SBAF_BOOT_MARKER   (0x5AA55AA5)

#define SMR_CONFIGURED                          (7U)

#define CM7_0_ENABLE_SHIFT  (0)
#define CM7_1_ENABLE_SHIFT  (1)

#define RFU_ENABLE_SHIFT    (2)

/*
 * Selects the boot sequence flow:
 * - When 0: releases the host from reset, then runs the HSE firmware
 * - When 1: keeps the host on reset and runs the HSE firmware first; must be set to run a pre-boot verification
 */
#define BOOT_SEQ_SHIFT      (3)

/*
 * PLL enabled by HSE
 * - When 0: PLL not enable by HSE
 * - When 1: PLL enabled by HSE only if BOOT_SEQ is also 1
 */
#define PLL_ENABLE_SHIFT    (4)

/*
 * Boot target watchdog configuration
 * - When 0: disabled
 * - When 1: enabled before releasing the boot target from reset
 */
#define SWT0_ENABLE_SHIFT   (5)



#define CM7_0_ENABLE            (1)
#define CM7_1_ENABLE            (0)
#define CM7_0_VTOR_ADDR         (0x00402000)
#define CM7_1_VTOR_ADDR         (0)
#define CM7_2_VTOR_ADDR         (0)
#define XRDC_CONFIG_ADDR        (0x00000000)
#define LF_CONFIG_ADDR          (0)
#define HSE_FW_ADDR          (0)

#define SECURE_BOOT_APP_ADDR    (0x005040C0)
#define SECURE_BOOT_BACKUP_ADDR (0x00000000)

#define IVT_GMAC                    (0xFFFFFFFF)

#define IVT_RESERVED                (0)




/*=============================================================================
*                                             ENUMS
=============================================================================*/
typedef enum
{
    NO_TEST_EXECUTED = 0x0,
    GET_VERSION_DONE = 0x1,
    KEY_CATALOGS_FORMATTED = 0x2,
    CRYPTOGRAPHIC_SERVICES_SUCCESS = 0x4,
    FW_UPDATE_SUCCESS = 0x8,
    CR_DEBUG_AUTH_MODE = 0x10,
    ADKP_PROGRAMMING_SUCCESS = 0x20,
    LIFECYCLE_ADVANCED_TO_INFIELD = 0x40,
    IVT_AUTHENTICATION_ENABLED = 0x80,
    SECURE_BOOT_CONFIGURATION_DONE = 0x100,
    NVM_DATA_ERASED = 0x200,
    MONOTONIC_CNT_DONE = 0x400,
    HSE_FW_USAGE_ENABLED = 0x800,
    EXTEND_CUST_SECURE_POLICY_ENABLED = 0x1000,
    ACTIVATE_PASSIVE_BLOCK_SUCCESSFUL = 0x2000,
    LIFECYCLE_ADVANCED_TO_OEM_PROD = 0x4000,
    ADKP_KEY_VERIFIED = 0x8000
} testStatus_t;

typedef enum
{
    CRYPTO_SERVICE_SUCCESS_NONE = 0,
    AES_EXAMPLES_SUCCESS = 1,
    HASH_EXAMPLES_SUCCESS = 2,
    ECC_KEYS_EXAMPLES_SUCCESS = 4,
    SESSION_KEY_EXAMPLES_SUCCESS = 8,
    FAST_CMAC_WITH_COUNTER_EXAMPLES_SUCCESS = 16,
    BURMESTER_DESMEDT_EXAMPLES_SUCCESS = 32,
    SYS_AUTHORIZATION_EXAMPLE_SUCCESS = 64,
    UPDATE_NVM_KEY_EXAMPLE_SUCCESS = 128
}CryptoServicesTestStatus_t;


/*=============================================================================
                                 STRUCTURES AND OTHER TYPEDEFS
=============================================================================*/

/*=============================================================================
                                 GLOBAL VARIABLE DECLARATIONS
=============================================================================*/

extern volatile CryptoServicesTestStatus_t gCryptoServicesExecuted;
/**< @brief secure boot type */
extern volatile uint8_t gRunSecureBootType;
/**< @brief current test status */
extern testStatus_t testStatus;
/**< @brief response of key formatting */
extern hseSrvResponse_t formatkey_srvResponse;

/**< @brief IVT */
extern ivt_t IVT;

/**< @brief Global FW version before and after FW update */
extern hseAttrFwVersion_t fwversion[2];


#define CRYPTO_START_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
#include "Crypto_MemMap.h"

/**< @brief Application Debug key password */
extern volatile uint8_t applicationDebugKeyPassword[];

extern int16_t gRunExampleTest;

#define CRYPTO_STOP_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
#include "Crypto_MemMap.h"

/**< @brief Global response */
extern volatile hseSrvResponse_t KeyReadSrvResponse;

/**< @brief test completed */
extern char allTestExecuted;

/**< @brief Global AES 128 Provision Key Info */
extern hseKeyInfo_t gAES128ProvisionKeyInfo;

/**< @brief Global AES 128 Authorization Key */
extern uint8_t gAES128AuthorizationKey[16];

/**< @brief Global AES 128 Authorization Key Info */
extern hseKeyInfo_t gAES128AuthorizationKeyInfo;

/**< @brief Global ECC P256 Boot Key Pair */
extern uint8_t gEccP256BootKeyPub[];
extern uint8_t gEccP256BootKeyPriv[];

/**< @brief Global AES 128 Boot Key */
extern uint8_t gAES128BootKey[16];
/**< @brief Global AES 128 Boot Key Info */
extern hseKeyInfo_t gAES128BootKeyInfo;

extern volatile uint8_t programmed_appdebugkey[16];
extern volatile uint8_t adkp_hash[16];

extern const char hse_status_list[][40];

/*=============================================================================
                                     FUNCTION PROTOTYPES
=============================================================================*/

#ifdef __cplusplus
}
#endif

#endif /* GLOBAL_VARIABLES_H */

/** @} */
