/***********************************************************************************************************************
 *  FILE DESCRIPTION
 *  ------------------------------------------------------------------------------------------------------------------*/
/** \file
 *  \brief         Implementation of a download check by using an embedded validation structure
 *                 Declaration of functions, variables and constants
 *
 *  --------------------------------------------------------------------------------------------------------------------
 *  COPYRIGHT
 *  --------------------------------------------------------------------------------------------------------------------
 *  \par Copyright
 *  \verbatim
 *  Copyright (c) 2026 by Vector Informatik GmbH.                                                  All rights reserved.
 *
 *                This software is copyright protected and proprietary to Vector Informatik GmbH.
 *                Vector Informatik GmbH grants to you only those rights as set out in the license conditions.
 *                All other rights remain with Vector Informatik GmbH.
 *  \endverbatim
 */
/**********************************************************************************************************************/

/***********************************************************************************************************************
 *  TEMPLATE REVISION HISTORY
 *  --------------------------------------------------------------------------------------------------------------------
 *  Version    Date        Author       Change Id        Description
 *  --------------------------------------------------------------------------------------------------------------------
 *  01.00.00   2016-07-01  visci        -                Initial version
 *  01.01.00   2017-02-03  vadcel       ESCAN00093858    Added support for compatibility check
 *                                                        Added blank check functionality
 *                                                        Added API for external public key
 *                                                        Reworked return value handling
 *  01.01.01   2017-11-09  visci        ESCAN00096836    No changes
 *  01.01.02   2018-03-23  visdkl       ESCAN00097287    Bootloader does not properly use configuration of CLASS_DDD
 *  01.02.00   2018-06-15  visci        ESCAN00098963    Corrected return value for public key read failure
 *  01.03.00   2019-01-25  visci        ESCAN00098904    Improved verification sequence
 *                                      ESCAN00101065    Alignment for Blank Check is not applied correctly
 *                                      ESCAN00101321    Added option to skip download segment check
 *  01.03.01   2019-02-06  visci        ESCAN00102005    No changes
 *  01.04.00   2019-06-14  visci        ESCAN00102956    No changes
 *                                      ESCAN00102957    No changes
 *  01.05.00   2019-06-25  visdlm       ESCAN00103492    Added gap fill mechanism
 *  --------------------------------------------------------------------------------------------------------------------
 *  REVISION HISTORY
 *  --------------------------------------------------------------------------------------------------------------------
 *  Version    Date        Author       Change Id        Description
 *  --------------------------------------------------------------------------------------------------------------------
 *  05.00.00   2022-08-15  vistmo       FBL-5514         Support Additional OTA functionality
 *                                      ESCAN00112435    No changes
 *                                      ESCAN00112575    No changes
 *  05.01.00   2022-10-04  jschmitding  FBL-5917         No changes
 *                                      ESCAN00113919    No changes
 *  05.01.01   2022-10-17  fmenke       ESCAN00112879    No changes
 *  05.01.02   2023-01-13  fmenke       ESCAN00112890    No changes
 *                                      ESCAN00113612    No changes
 *  05.02.00   2023-02-09  vistmo       ESCAN00112972    No changes
 *                                      ESCAN00113292    No changes
 *                                      ESCAN00113835    No changes
 *                                      ESCAN00113843    No changes
 *                                      ESCAN00113764    No changes
 *                                      FBL-6438         No changes
 *  05.02.01   2023-05-11  fmenke       ESCAN00113831    No changes
 *  05.03.00   2023-07-24  fmenke       FBL-7335         No changes
 *  05.03.01   2023-08-31  lhopfhauer   ESCAN00115494    No changes
 *  05.03.02   2024-07-16  erizk        ESCAN00117344    No changes
 *  05.03.03   2025-02-06  virchl       ESCAN00118587    No changes
 *  05.04.00   2025-02-28  jjohnson     FBL-10065        No changes
 *  05.04.01   2025-06-10  jostravsky   ESCAN00120315    No changes
 *  05.04.02   2025-07-08  visarcmijo   ESCAN00119833    No changes
 *  05.05.00   2025-09-01  fmenke       FBL-11218        No changes
 *                                      ESCAN00120917    No changes
 *  05.06.00   2025-10-28  mpatil       FBL-11690        No changes
 *  05.07.00   2026-01-13  dganesh      FBL-12162        No changes
 **********************************************************************************************************************/

#ifndef FBL_VALSTRUCT_OEM_H
#define FBL_VALSTRUCT_OEM_H

/***********************************************************************************************************************
 *  DEFINES
 **********************************************************************************************************************/

#if defined( FBL_ENABLE_VALSTRUCT )

/** Specify default options for validation structure processing */
# define FBL_VALSTRUCT_OPTION_DEFAULT                 (FBL_VALSTRUCT_OPTION_NONE)

/** Maximum number of blocks in validation struct */
# if !defined( FBL_VALSTRUCT_BLOCKCOUNT_MAX )
/* At least two segments required for SBL */
#  define FBL_VALSTRUCT_BLOCKCOUNT_MAX                (SWM_DATA_MAX_NOAR - 2u)
# endif

/** Size of validation struct members */
# if ( SEC_SECURITY_CLASS_VERIFY == SEC_CLASS_CCC )
#  define FBL_VALSTRUCT_TAG_SIZE                      0x00u
#  define FBL_VALSTRUCT_VERSION_SIZE                  0x02u
#  define FBL_VALSTRUCT_BLOCKCOUNT_SIZE               0x02u
#  define FBL_VALSTRUCT_BLOCKADDRESS_SIZE             0x04u
#  define FBL_VALSTRUCT_BLOCKLENGTH_SIZE              0x04u
#  define FBL_VALSTRUCT_BLOCKCHECKSUM_SIZE            0x20u
#  define FBL_VALSTRUCT_VALIDATESUM_SIZE              0x00u
# else
#  define FBL_VALSTRUCT_TAG_SIZE                      0x04u
#  define FBL_VALSTRUCT_VERSION_SIZE                  0x00u
#  define FBL_VALSTRUCT_BLOCKCOUNT_SIZE               0x02u
#  define FBL_VALSTRUCT_BLOCKADDRESS_SIZE             0x04u
#  define FBL_VALSTRUCT_BLOCKLENGTH_SIZE              0x04u
#  define FBL_VALSTRUCT_BLOCKCHECKSUM_SIZE            0x02u
#  define FBL_VALSTRUCT_VALIDATESUM_SIZE              0x02u
# endif /* SEC_SECURITY_CLASS_VERIFY == SEC_CLASS_CCC */

/** Supported Valstruct version can be overwritten */
# ifndef FBL_VALSTRUCT_VERSION
#  define FBL_VALSTRUCT_VERSION                       0x0000u
# endif /* FBL_VALSTRUCT_VERSION */

/** Define supported structure versions */
# define FblValStructIsVersionSupported(ver)          ((FBL_VALSTRUCT_VERSION == (ver)) ? kFblValStructOk : kFblValStructNotAuthentic)

/** Provide base address of validation structure and signature as well as the respective download segment dimension */

/****** Ford (CCC + DDD use case, signature embedded before validation structure ******/
# if ( SEC_SECURITY_CLASS_VERIFY == SEC_CLASS_CCC )
/* Block header points to the verification structure, the signature is located below */
#  define FblValStructGetSigAddress(block)            ((block)->headerAddress - SEC_VERIFY_CLASS_CCC_VERIFY_SIZE)
#  define FblValStructGetVsAddress(block)             ((block)->headerAddress)
#  define FblValStructGetVsSegAddress(block)          FblValStructGetSigAddress(block)
#  define FblValStructGetVsSegLength(vsLength)        (SEC_VERIFY_CLASS_CCC_VERIFY_SIZE + (vsLength))

/* Provide signature data */
#  define FblValStructGetSignature(block, buffer)     (tFblValStructResult)((SEC_VERIFY_CLASS_CCC_VERIFY_SIZE == \
                                                       FblReadProm(FblValStructGetSigAddress(block), (buffer), SEC_VERIFY_CLASS_CCC_VERIFY_SIZE)) ? \
                                                       kFblValStructOk : kFblValStructMemoryReadError)

#  if defined( FBL_VALSTRUCT_ENABLE_EXTERNAL_KEY )
/** Provide public key */
#   define FblValStructGetPublicKey(key)               ApplFblReadVerificationKey(key)
#  endif
# else /* SEC_ENABLE_SECURITY_CLASS_DDD */
/* Block header points to the verification structure */
#  define FblValStructGetVsAddress(block)             ((block)->headerAddress)
#  define FblValStructGetVsSegAddress(block)          FblValStructGetVsAddress(block)
#  define FblValStructGetVsSegLength(vsLength)        (vsLength)
# endif /* SEC_SECURITY_CLASS_VERIFY == SEC_CLASS_CCC */

/** Provide segment list with address, length and checksum for each segment */
# define FblValStructGetSegmentList(block, segList)   FblDiagGetSegmentList(block, segList)

/** Options only relevant for OTA */

# if defined( FBL_ENABLE_GAP_FILL )
#  if defined( FBL_VALSTRUCT_ENABLE_GAP_FILL) || \
      defined( FBL_VALSTRUCT_DISABLE_GAP_FILL )
/* Valstruct gap fill handling explicitly defined outside */
#  else
#   define FBL_VALSTRUCT_ENABLE_GAP_FILL
#  endif /* FBL_VALSTRUCT_ENABLE_GAP_FILL */
# endif /* FBL_ENABLE_GAP_FILL */

#endif /* FBL_ENABLE_VALSTRUCT */

/** OEM specific return code mapping (tFblValStructResult) */
#define kFblValStructOk                               0u
#define kFblValStructNotAuthentic                     1u
#define kFblValStructInvalidFormat                    2u
#define kFblValStructBlockNumberMismatch              3u
#define kFblValStructChecksumValueMismatch            4u
#define kFblValStructMemoryReadError                  5u
#define kFblValStructBlockMismatch                    6u
#define kFblValStructKeyError                         7u
#define kFblValStructBlankCheckFailed                 8u
#define kFblValStructGapFillFailed                    9u

#endif /* FBL_VALSTRUCT_OEM_H */

/***********************************************************************************************************************
 *  END OF FILE: FBL_VALSTRUCT_OEM.H
 **********************************************************************************************************************/
