1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _SECURE_BOOT_STAGE_DEFS_H
8 #define _SECURE_BOOT_STAGE_DEFS_H
9 
10 #ifdef __cplusplus
11 extern "C"
12 {
13 #endif
14 
15 /*! @file
16 @brief This file contains all of the definitions and structures used for the run-time Secure Boot.
17 */
18 
19 #include "cc_pal_mem.h"
20 #include "cc_crypto_boot_defs.h"
21 #include "cc_sec_defs.h"
22 #include "secureboot_parser_gen_defs.h"
23 #include "secureboot_general_hwdefs.h"
24 #include "bsv_crypto_driver.h"
25 #include "bsv_crypto_api.h"
26 #include "crypto_driver.h"
27 #include "bsv_hw_defs.h"
28 #include "sbrt_int_func.h"
29 #include "mbedtls_cc_mng_int.h"
30 #include "pka_hw_defs.h"
31 
32 extern unsigned long gCcRegBase;
33 
34 /* ROM ==> RT */
35 #define CC_BSV_CHIP_MANUFACTURE_LCS CC_MNG_LCS_CM
36 #define CC_BSV_DEVICE_MANUFACTURE_LCS   CC_MNG_LCS_DM
37 #define CC_BSV_SECURE_LCS       CC_MNG_LCS_SEC_ENABLED
38 #define CC_BSV_RMA_LCS          CC_MNG_LCS_RMA
39 
40 #define UTIL_MemCopy(pDst, pSrc ,size)          \
41     CC_PalMemCopy(pDst, pSrc, size)
42 #define UTIL_MemSet(pBuff, val, size )          \
43     CC_PalMemSet(pBuff, val, size)
44 #define UTIL_MemCmp(pBuff1, pBuff2, size)       \
45     SBRT_MemCmp(pBuff1, pBuff2, size)
46 #define UTIL_ReverseMemCopy(pDst, pSrc, size)       \
47     SBRT_ReverseMemCopy(pDst, pSrc, size)
48 #define UTIL_ReverseBuff(pBuff, size)           \
49     SBRT_ReverseMemCopy(pBuff, pBuff, size)
50 
51 #define _CCSbImageLoadAndVerify(preHashflashRead_func, preHashUserContext, hwBaseAddress, isLoadFromFlash, isVerifyImage, cryptoMode, keyType, AESIv, pSwRecSignedData, pSwRecNoneSignedData, workspace_ptr, workspaceSize) \
52     SBRT_ImageLoadAndVerify(preHashflashRead_func, preHashUserContext, hwBaseAddress, isLoadFromFlash, isVerifyImage, cryptoMode, keyType, AESIv, pSwRecSignedData, pSwRecNoneSignedData, workspace_ptr, workspaceSize)
53 
54 #define _RSA_PSS_Verify(hwBaseAddress, mHash, pN, pNp, pSign)   \
55     SBRT_RSA_PSS_Verify(hwBaseAddress, mHash, pN, pNp, pSign)
56 
57 #define CC_BsvLcsGet(hwBaseAddress, pLcs) \
58     SBRT_LcsGet(hwBaseAddress, pLcs)
59 
60 #define SB_HAL_WRITE_REGISTER(addr,val) \
61         ((*((volatile uint32_t*)(gCcRegBase + addr))) = (unsigned long)(val))
62 #define SB_HAL_READ_REGISTER(addr,val)  \
63         ((val) = (*((volatile uint32_t*)(gCcRegBase + addr))))
64 
65 #define SB_HalClearInterruptBit(hwBaseAddress, data) \
66     SBRT_HalClearInterruptBit(hwBaseAddress, data)
67 
68 #define SB_HalMaskInterrupt(hwBaseAddress, data) \
69     SBRT_HalMaskInterrupt(hwBaseAddress, data)
70 
71 #define SB_HalWaitInterrupt(hwBaseAddress, data) \
72     SBRT_HalWaitInterrupt(hwBaseAddress, data)
73 
74 #define CC_BsvOTPWordRead(hwBaseAddress, otpAddress, pOtpWord) \
75     SBRT_OTPWordRead(hwBaseAddress, otpAddress, pOtpWord)
76 
77 #define CC_BsvSwVersionGet(hwBaseAddress, keyIndex, swVersion)  \
78     SBRT_SwVersionGet(hwBaseAddress, keyIndex, swVersion)
79 
80 #define CC_BsvPubKeyHashGet(hwBaseAddress, keyIndex, hashedPubKey, hashResultSizeWords) \
81     SBRT_PubKeyHashGet(hwBaseAddress, keyIndex, hashedPubKey, hashResultSizeWords)
82 
83 #define CC_BsvSHA256(hwBaseAddress, pDataIn, dataSize, hashBuff) \
84     SBRT_SHA256(hwBaseAddress, pDataIn, dataSize, hashBuff)
85 
86 #define BsvCryptoImageInit(hwBaseAddress, mode, keyType) \
87     SBRT_CryptoImageInit(hwBaseAddress, mode, keyType)
88 
89 #define BsvCryptoImageUpdate(hwBaseAddress, mode, keyType, pCtrStateBuf, pDataIn, pDataOut, dataSize, hashBuff, isLoadIV) \
90     SBRT_CryptoImageUpdate(hwBaseAddress, mode, keyType, pCtrStateBuf, pDataIn, pDataOut, dataSize, hashBuff, isLoadIV)
91 
92 #define BsvCryptoImageFinish(hwBaseAddress, mode, hashBuff) \
93     SBRT_CryptoImageFinish(hwBaseAddress, mode, hashBuff)
94 
95 #undef min
96 #define min(a, b) \
97     CC_MIN(a, b)
98 
99 #ifdef BIG__ENDIAN
100 #define UTIL_REVERT_UINT32_BYTES( val ) \
101    ( ((val) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24) )
102 #else
103 #define UTIL_REVERT_UINT32_BYTES( val ) (val)
104 #endif
105 
106 #ifdef BIG__ENDIAN
107 #define UTIL_INVERSE_UINT32_BYTES( val )    (val)
108 #else
109 #define UTIL_INVERSE_UINT32_BYTES( val ) \
110    ( ((val) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24) )
111 #endif
112 
113 /* rsa_hwdefs => pka_hw_defs */
114 #define RSA_PKA_MAX_COUNT_OF_REGS_SIZES         PKA_NUM_OF_PKA_LEN_IDS_REGS
115 #define RSA_PKA_BIG_WORD_SIZE_IN_BITS           CC_PKA_WORD_SIZE_IN_BITS
116 #define RSA_PKA_BIG_WORD_SIZE_IN_32_BIT_WORDS       PKA_WORD_SIZE_IN_32BIT_WORDS
117 #define RSA_PKA_EXTRA_BITS                  PKA_EXTRA_BITS
118 #define   PkaModExp                 PKA_OPCODE_ID_MODEXP
119 #define RSA_PKA_SRAM_REGS_MEM_OFFSET_WORDS      CC_SRAM_PKA_BASE_ADDRESS
120 #define RSA_HW_PKI_PKA_N_NP_T0_T1_REG_DEFAULT_VAL   PKA_N_NP_T0_T1_REG_DEFAULT_VAL
121 
122 #define RSA_PKA_FullOpCode( Opcode,LenID,IsAImmed,OpA,IsBImmed,OpB,ResDiscard,Res,Tag ) \
123     PKA_SET_FULL_OPCODE(Opcode,LenID,IsAImmed,OpA,IsBImmed,OpB,ResDiscard,Res,Tag )
124 #define RSA_PKA_WAIT_ON_PKA_PIPE_READY(VirtualHwBaseAddr) \
125     CC_UNUSED_PARAM(VirtualHwBaseAddr); \
126     PKA_WAIT_ON_PKA_PIPE_READY()
127 #define RSA_PKA_WAIT_ON_PKA_DONE(VirtualHwBaseAddr) \
128     CC_UNUSED_PARAM(VirtualHwBaseAddr); \
129     PKA_WAIT_ON_PKA_DONE()
130 #define RSA_PKA_ReadRegSize(SizeBits, EntryNum, VirtualHwBaseAddr) \
131     CC_UNUSED_PARAM(VirtualHwBaseAddr); \
132     PKA_GET_REG_SIZE(SizeBits, EntryNum)
133 #define RSA_HW_PKI_HW_LOAD_BLOCK_TO_PKA_MEM( VirtualHwBaseAddr , Addr , ptr , SizeWords ) \
134     CC_UNUSED_PARAM(VirtualHwBaseAddr); \
135     PKA_HW_LOAD_BLOCK_TO_PKA_MEM(Addr, ptr, SizeWords)
136 #define RSA_HW_PKI_HW_CLEAR_PKA_MEM( VirtualHwBaseAddr , Addr , SizeWords ) \
137     CC_UNUSED_PARAM(VirtualHwBaseAddr); \
138     PKA_HW_CLEAR_PKA_MEM(Addr, SizeWords)
139 #define RSA_HW_PKI_HW_READ_BLOCK_FROM_PKA_MEM( VirtualHwBaseAddr , Addr , ptr , SizeWords ) \
140     CC_UNUSED_PARAM(VirtualHwBaseAddr); \
141     PKA_HW_READ_BLOCK_FROM_PKA_MEM(Addr, ptr, SizeWords)
142 #define RSA_PKA_GetRegAddress(VirtReg, VirtualHwBaseAddr) \
143     (*((volatile uint32_t*)(gCcRegBase + CC_REG_OFFSET(CRY_KERNEL, MEMORY_MAP0) + 4*(VirtReg))))
144 
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif
151 
152 
153