1 /**************************************************************************//**
2  * @file     MKROMLib.h
3  * @version  V2.00
4  * @brief    MaskROM library header file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2018-2020 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 #ifndef __MKROM_LIB_H__
10 #define __MKROM_LIB_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 
18 /** @addtogroup Standard_Driver Standard Driver
19   @{
20 */
21 
22 /** @addtogroup MKROM_Driver MKROM Driver
23   @{
24 */
25 
26 /** @addtogroup MKROM_EXPORTED_CONSTANTS MKROM Exported Constants
27   @{
28 */
29 /*--------------------------------------------------------------------------------------------------*/
30 /*  Status and Error Code Constant Definitions                                                      */
31 /*--------------------------------------------------------------------------------------------------*/
32 #define BL_ERR_TT_CHECK         0xF0F00000UL    /*!< Not a Non-secure parameter         */
33 #define BL_ERR_PARAMETER        0xF0F00001UL    /*!< Invalid parameter                  */
34 #define BL_PARAM_ALIGN          0xF0F00002UL    /*!< Parameter alignment error          */
35 #define BL_NOT_FLASH_ADDR       0xF0F00003UL    /*!< Invalid flash address              */
36 #define BL_NOT_SRAM_ADDR        0xF0F00004UL    /*!< Invalid sram address               */
37 #define BL_XOM_NOT_CONFIG       0xF0F00005UL    /*!< XOM is not configure yet           */
38 #define BL_XOM_HAS_CONFIG       0xF0F00006UL    /*!< XOM has beeen configured           */
39 #define BL_XOM_HAS_ACTIVE       0xF0F00007UL    /*!< XOM is actived                     */
40 #define BL_XOM_BASE_ERROR       0xF0F00008UL    /*!< Invalid XOM base address           */
41 #define BL_KPROM_NOT_ENABLE     0xF0F00009UL    /*!< KPROM is not enabled yet           */
42 #define BL_KPROM_KEY_FORBID     0xF0F0000AUL    /*!< KPROM comparison is forbidden      */
43 #define BL_KPROM_KEY_UNMATCH    0xF0F0000BUL    /*!< KPROM comparison is unmatched      */
44 #define BL_KPROM_KEY_LOCKED     0xF0F0000CUL    /*!< KPROM write-protect is enabled     */
45 #define BL_KPROM_SET_FAIL       0xF0F0000EUL    /*!< Set KPROM key fail                 */
46 #define BL_ISP_CMD_FAIL         (-1)            /*!< FMC command fail                   */
47 #define BL_FLASH_ALLONE         0xA11FFFFFUL    /*!< Check-all-one result is all one    */
48 #define BL_FLASH_NOT_ALLONE     0xA1100000UL    /*!< Check-all-one result is not all one*/
49 
50 /*--------------------------------------------------------------------------------------------------*/
51 /*  Random Number Generator Constant Definitions                                                    */
52 /*--------------------------------------------------------------------------------------------------*/
53 #define BL_RNG_PRNG             (0UL)   /*!<Use H/W random number generator */
54 #define BL_RNG_SWRNG            (1UL)   /*!<Use S/W random number generator */
55 #define BL_RNG_LIRC32K          (0UL)   /*!<Use LIRC32 for random number generator */
56 #define BL_RNG_LXT              (2UL)   /*!<Use LXT for random number generator */
57 #define XTRNG_PRNG              (0UL)   /*!<Use H/W random number generator */
58 #define XTRNG_SWRNG             (1UL)   /*!<Use S/W random number generator */
59 #define XTRNG_LIRC32K           (0UL)   /*!<Use LIRC32 for random number generator */
60 #define XTRNG_LXT               (2UL)   /*!<Use LXT for random number generator */
61 
62 /*--------------------------------------------------------------------------------------------------*/
63 /*  Maximum SecureISP Mode Transmit/Receive Packet Size Constant Definitions                        */
64 /*--------------------------------------------------------------------------------------------------*/
65 #define MAX_PKT_SIZE            64
66 
67 /*@}*/ /* end of group MKROM_EXPORTED_CONSTANTS */
68 
69 
70 /** @addtogroup MKROM_EXPORTED_STRUCTS MKROM Exported Structs
71   @{
72 */
73 /**
74   * @details    Random number generator structure
75   */
76 typedef struct
77 {
78     uint32_t opt;       /*!< Operation mode */
79     int32_t data_len;   /*!< Internal use for random number generator */
80     uint8_t buf[32];    /*!< Internal use for random number generator */
81     uint8_t buf2[20];   /*!< Internal use for random number generator */
82 } BL_RNG_T;
83 
84 typedef struct
85 {
86     uint32_t opt;       /*!< Operation mode */
87     int32_t data_len;   /*!< Internal use for random number generator */
88     uint8_t buf[32];    /*!< Internal use for random number generator */
89     uint8_t buf2[20];   /*!< Internal use for random number generator */
90 } XTRNG_T;
91 
92 
93 /**
94   * @details    XCRPT_T is structure for access MKROM Crypto library
95   */
96 typedef struct
97 {
98     CRPT_T      *crpt;       /*!< The pointer of the CRYPTO module */
99     ECC_CURVE   *pCurve;     /*!< Internal use for ECC */
100     ECC_CURVE   Curve_Copy;  /*!< Internal use for ECC */
101     uint32_t    AES_CTL[4];  /*!< AES channel selection */
102     uint32_t    TDES_CTL[4]; /*!< TDES channel selection */
103 } XCRPT_T;
104 
105 /*---------------------------------------------------------------------------------------------------*/
106 /*  Define a global constant XCRPT as the access address of g_xcrpta for using MKROM Crypto library. */
107 /*---------------------------------------------------------------------------------------------------*/
108 extern XCRPT_T g_xcrpt;
109 #define XCRPT (&g_xcrpt)
110 
111 
112 /**
113   * @details    Command packet structure for transmit/receive data in SecureISP function
114   */
115 typedef struct
116 {
117     /* Word-0 */
118     uint16_t        u16CRC16;       /* CRC16 checksum of from u8CmdID to Word-13 */
119     uint16_t        u16CmdID;       /* Command ID */
120 
121     /* Word-1 */
122     uint16_t        u16PacketID;    /* Packet ID */
123     uint16_t        u16Len;         /* Valid data length in command data field */
124 
125     /* Word-2 ~ 13 */
126     uint32_t        au32Data[12];   /* Data fields */
127 
128     /* Word-14 */
129     uint32_t        u32CRC32;       /* CRC32 from Word-0 to Word-13 for check cmd integrity */
130 
131     /* Word-15 */
132     uint32_t        RSVD;           /* Reserved */
133 
134 } CMD_PACKET_T;
135 
136 
137 /**
138   * @details    ECC public key structure
139   */
140 typedef struct
141 {
142     uint32_t        au32Key0[8];    /* 256-bits */
143     uint32_t        au32Key1[8];    /* 256-bits */
144 } __attribute__((packed)) ECC_PUBKEY_T;
145 
146 
147 /**
148   * @details    ECC ECDSA signature structure
149   */
150 typedef struct
151 {
152     uint32_t        au32R[8];   /* 256-bits */
153     uint32_t        au32S[8];   /* 256-bits */
154 } __attribute__((packed)) ECDSA_SIGN_T;
155 
156 
157 /**
158   * @details    SecureISP operation mode enumerate
159   */
160 typedef enum
161 {
162     USB_MODE        = 0x1,
163     UART_MODE       = 0x2,
164     USB_UART_MODE   = 0x3,
165     RESYNC_ISP      = 0x80, /* To set SecureISP in waiting connection state */
166 } E_ISP_MODE;
167 
168 
169 /**
170   * @details    Global ISP information data for perform SecureISP function
171   */
172 typedef void (*ISPCallback)(uint32_t *pu32Buf, uint32_t u32Data);
173 typedef void (*USBDEPFunc)(void);
174 typedef struct
175 {
176     uint32_t        u32CmdMask;         /* Disable the specify command in SecureISP */
177 
178     uint32_t        au32AESKey[8];      /* AES-256 keys */
179     uint32_t        au32AESIV[4];       /* AES IV, 128-bits */
180 
181     ECC_PUBKEY_T    ClientPubKey;       /* Client's ECC public key, 64-bytes (256-bits + 256-bits) */
182     ECC_PUBKEY_T    ServerPubKey;       /* Server's ECC public key, 64-bytes (256-bits + 256-bits) */
183 
184     ECDSA_SIGN_T    sign;               /* 64-bytes (256-bits R + 256-bits S) */
185 
186     uint32_t        IsConnectOK;        /* Internal use in SecureISP */
187     uint32_t        timeout;            /* Timeout period for connecting to SecureISP Tool */
188 
189     __attribute__((aligned(4))) uint8_t rcvbuf[MAX_PKT_SIZE]; /* Internal use in SecureISP */
190     __attribute__((aligned(4))) uint8_t rspbuf[MAX_PKT_SIZE]; /* Internal use in SecureISP */
191 
192     USBDEPFunc      pfnUSBDEP[USBD_MAX_EP]; /* Internal use in SecureISP */
193     uint32_t        IsUSBDataReady;     /* Internal use in SecureISP */
194 
195     uint32_t        UARTClockFreq;      /* UART clock frequency */
196     uint32_t        UARTDataIdx;        /* Internal use in SecureISP */
197     uint32_t        IsUARTDataReady;    /* Internal use in SecureISP */
198 
199     ISPCallback     pfnVendorFunc;      /* Vendor function address */
200 
201     uint32_t        tmp0[8];            /* Internal use in SecureISP */
202     uint32_t        tmp1[8];            /* Internal use in SecureISP */
203 
204 } ISP_INFO_T;
205 
206 
207 /**
208   * @details    Global USBD data for SecureISP USB
209         This data is internal use in SecureISP operation
210   */
211 #pragma pack(push)
212 #pragma pack(1)
213 typedef struct
214 {
215     uint8_t             g_usbd_SetupPacket[8];
216     volatile uint8_t    g_usbd_RemoteWakeupEn;
217     volatile uint8_t    g_usbd_u8ZeroFlag;
218 
219     volatile uint8_t    *g_usbd_CtrlInPointer;
220     volatile uint8_t    *g_usbd_CtrlOutPointer;
221     volatile uint32_t   g_usbd_CtrlInSize;
222     volatile uint32_t   g_usbd_CtrlOutSize;
223     volatile uint32_t   g_usbd_CtrlOutSizeLimit;
224     volatile uint32_t   g_usbd_UsbAddr;
225     volatile uint32_t   g_usbd_UsbConfig;
226     volatile uint32_t   g_usbd_CtrlMaxPktSize;
227     volatile uint32_t   g_usbd_UsbAltInterface;
228 
229     S_USBD_INFO_T       *g_usbd_sInfo;
230 
231     VENDOR_REQ          g_usbd_pfnVendorRequest;
232     CLASS_REQ           g_usbd_pfnClassRequest;
233     SET_INTERFACE_REQ   g_usbd_pfnSetInterface;
234     SET_CONFIG_CB       g_usbd_pfnSetConfigCallback;
235     uint32_t            g_u32EpStallLock;
236 
237 } BL_USBD_INFO_T;
238 #pragma pack(pop)
239 
240 /*@}*/ /* end of group MKROM_EXPORTED_STRUCTS */
241 
242 
243 /** @addtogroup MKROM_EXPORTED_FUNCTIONS Bootloader Exported Functions
244   @{
245 */
246 /**
247   * @brief      Get MKROM Version Number
248   * @param      None
249   * @return     Version number of MKROM
250   * @details    This API will return the MKROM version number.
251   */
252 uint32_t BL_GetVersion(void);
253 
254 
255 /**
256   * @brief      Enable FMC ISP Function and return maximum APROM size
257   * @param      None
258   * @return     Maximum APROM size
259   * @details    This API will unlock register write-protect, enable relative settings for access FMC ISP commands
260   *             and return maximum APROM by chip package.
261   */
262 uint32_t BL_EnableFMC(void);
263 
264 
265 /**
266   * @brief      Disable FMC ISP Function
267   * @param      None
268   * @return     None
269   * @details    This API will disable relative settings for disable FMC ISP function and lock register write-protect
270   *             until last ISP operation is finished.
271   */
272 void BL_DisableFMC(void);
273 
274 
275 /**
276   * @brief      Get FMC ISP Busy Status
277   * @param      None
278   * @retval     0   ISP operation is finished
279   * @retval     1   ISP operation is in processing
280   * @details    This API indicates ISP operation in in processing or finished.
281   */
282 uint32_t BL_GetISPStatus(void);
283 
284 
285 /**
286   * @brief      Get Non-secure Boundary
287   * @param      None
288   * @return     Current Non-secure boundary
289   * @details    This API can get current Non-secure boundary address.
290   */
291 uint32_t BL_GetNSBoundary(void);
292 
293 
294 /**
295   * @brief      Set All Flash Region Lock
296   * @param      None
297   * @retval     -1      Set flash all lock failed
298   * @retval     0       Set flash all lock operation is success
299   * @details    This API will protect all flash region read/write operate by ICE interface.
300   */
301 int32_t BL_SetFlashAllLock(void);
302 
303 
304 /**
305   * @brief      Read Non-secure Flash Address Data (for Non-secure region)
306   * @param[in]  u32NSAddr   Non-secure flash address
307   * @retval     0xF0F00000  u32NSAddr isn't in Non-secure area
308   * @retval     0xF0F00001  u32NSAddr isn't word aligned
309   * @retval     0xF0F00003  u32NSAddr isn't valid flash address
310   * @retval     -1          Flash read failed
311   * @retval     The data of specified Non-secure address
312   * @details    To read word data from specified Non-secure flash address.
313   */
314 uint32_t BL_FlashRead(uint32_t u32NSAddr);
315 
316 
317 /**
318   * @brief      Read Multi-Word Data from Non-secure Flash Address (for Non-secure region)
319   * @param[in]  u32NSAddr       Starting Non-secure flash address
320   * @param[out] pu32NSRamBuf    Non-secure sram address to store reading data
321   * @param[in]  u32Size         Total read byte counts, it should be word aligned and maximum size is one page size.
322   * @retval     0xF0F00000      u32NSAddr or pu32NSRamBuf region isn't in Non-secure area
323   * @retval     0xF0F00001      Wrong u32NSAddr, pu32NSRamBuf or u32Size parameter
324   * @retval     0xF0F00003      u32NSAddr isn't valid flash address
325   * @retval     0xF0F00004      pu32NSRamBuf isn't valid sram address
326   * @retval     -1              Multi-words read failed
327   * @retval     0               Read operation is success
328   * @details    To read multi-words data start from specified Non-secure flash address.
329   *             And maximum read size is one page size, 2048 bytes.
330   */
331 int32_t BL_FlashMultiRead(uint32_t u32NSAddr, uint32_t *pu32NSRamBuf, uint32_t u32Size);
332 
333 
334 /**
335   * @brief      Program Data into Non-secure Flash Address (for Non-secure region)
336   * @param[in]  u32NSAddr   Non-secure flash address
337   * @param[in]  u32Data     32-bit Data to program
338   * @retval     0xF0F00000  u32NSAddr isn't in Non-secure area
339   * @retval     0xF0F00001  u32NSAddr isn't word aligned
340   * @retval     0xF0F00003  u32NSAddr isn't valid flash address
341   * @retval     -1          Flash write failed
342   * @retval     0           Program command is finished
343   * @details    To program word data into specified Non-secure flash address.
344   */
345 int32_t BL_FlashWrite(uint32_t u32NSAddr, uint32_t u32Data);
346 
347 
348 /**
349   * @brief      Program Multi-Word Data into Non-secure Flash Address (for Non-secure region)
350   * @param[in]  u32NSAddr       Starting Non-secure flash address
351   * @param[in]  pu32NSRamBuf    Non-secure sram buffer address to store program data
352   * @param[in]  u32Size         Total program byte counts, it should be word aligned and maximum size is one page size.
353   * @retval     0xF0F00000      u32NSAddr or pu32NSRamBuf region isn't in Non-secure area
354   * @retval     0xF0F00001      Wrong u32NSAddr, pu32NSRamBuf or u32Size parameter
355   * @retval     0xF0F00003      u32NSAddr isn't valid flash address
356   * @retval     0xF0F00004      pu32NSRamBuf isn't valid sram address
357   * @retval     -1              Multi-words write failed
358   * @retval     0               Program operation is finished
359   * @details    To program multi-words data start from specified Non-secure flash address.
360   *             And maximum program size is one page size, 2048 bytes.
361   */
362 int32_t BL_FlashMultiWrite(uint32_t u32NSAddr, uint32_t *pu32NSRamBuf, uint32_t u32Size);
363 
364 
365 /**
366   * @brief      Non-secure Flash Page Erase (for Non-secure region)
367   * @param[in]  u32NSAddr   Non-secure flash region to be erased and must be a page size aligned address.
368   * @retval     0xF0F00000  u32NSAddr region isn't in Non-secure area
369   * @retval     0xF0F00001  u32NSAddr isn't page size aligned
370   * @retval     0xF0F00003  u32NSAddr isn't valid flash address
371   * @retval     -1          Page erase failed
372   * @retval     0           Page erase success
373   * @details    This API is used to perform page erase command on specified Non-secure flash address.
374   *             And this address must be a page size aligned address.
375   */
376 int32_t BL_FlashPageErase(uint32_t u32NSAddr);
377 
378 
379 /**
380   * @brief      Get Non-secure Flash Area CRC32 Checksum (for Non-secure region)
381   * @param[in]  u32NSAddr       Non-secure flash region to be calculated. u32NSAddr must be a page size aligned address.
382   * @param[in]  u32ByteCount    Byte counts of Non-secure flash area to be calculated. It must be multiple of 2048 bytes.
383   * @retval     0xF0F00000      u32NSAddr region isn't in Non-secure area
384   * @retval     0xF0F00001      Wrong u32NSAddr or u32ByteCount parameter
385   * @retval     0xF0F00003      u32NSAddr isn't valid flash address
386   * @retval     -1              Execute CRC32 operation failed
387   * @retval     Result of CRC32 checksum
388   * @details    This API will calculate the CRC32 checksum result of specified non-secure flash area.
389   *             The starting address and calculated size must be all 2048 bytes page size aligned.
390   */
391 uint32_t BL_FlashChecksum(uint32_t u32NSAddr, uint32_t u32ByteCount);
392 
393 
394 /**
395   * @brief      Check Non-secure Flash Area Data are all ONE or not (for Non-secure region)
396   * @param[in]  u32NSAddr       Non-secure flash region to be calculated. u32NSAddr must be a page size aligned address.
397   * @param[in]  u32ByteCount    Byte counts of Non-secure flash area to be calculated. It must be multiple of 2048 bytes.
398   * @retval     0xF0F00000      u32NSAddr region isn't in Non-secure area
399   * @retval     0xF0F00001      Wrong u32NSAddr or u32ByteCount parameter
400   * @retval     0xF0F00003      u32NSAddr isn't valid flash address
401   * @retval     -1              Execute Check Flash All One operation failed
402   * @retval     0xA11FFFFF      The contents of verified Non-secure flash area are 0xFFFFFFFF
403   * @retval     0xA1100000      Some contents of verified Non-secure flash area are not 0xFFFFFFFF
404   * @details    This API is used to check specified Non-secure flash area are all 0xFFFFFFFF or not.
405   */
406 uint32_t BL_CheckFlashAllOne(uint32_t u32NSAddr, uint32_t u32ByteCount);
407 
408 
409 /**
410   * @brief      Read Company ID
411   * @param      None
412   * @return     The company ID (32-bit)
413   * @details    The company ID of Nuvoton is fixed to be 0xDA.
414   */
415 uint32_t BL_ReadCID(void);
416 
417 
418 /**
419   * @brief      Read Device ID
420   * @param      None
421   * @return     The device ID (32-bit)
422   * @details    This function is used to read device ID.
423   */
424 uint32_t BL_ReadDID(void);
425 
426 
427 /**
428   * @brief      Read Product ID
429   * @param      None
430   * @return     The product ID (32-bit)
431   * @details    This function is used to read product ID.
432   */
433 uint32_t BL_ReadPID(void);
434 
435 
436 /**
437   * @brief      Read UCID
438   * @param[in]  u32Index    Index of the UCID to read and u32Index must be 0, 1, 2, or 3.
439   * @return     The UCID of specified index
440   * @details    This function is used to read unique chip ID (UCID).
441   */
442 uint32_t BL_ReadUCID(uint32_t u32Index);
443 
444 
445 /**
446   * @brief      Read UID
447   * @param[in]  u32Index    UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
448   * @return     The 32-bit unique ID data of specified UID index
449   * @details    To read out specified 32-bit unique ID.
450   */
451 uint32_t BL_ReadUID(uint32_t u32Index);
452 
453 
454 /**
455   * @brief      Get XOM Active Status
456   * @param[in]  u32XOM      Specified XOM region, it must be between 0~3.
457   * @retval     0xF0F00001  Invalid u32XOM number
458   * @retval     0           Current XOM region isn't active yet
459   * @retval     1           Current XOM region is active
460   * @details    This API will return specified XOM region active status.
461   */
462 uint32_t BL_GetXOMActiveStatus(uint32_t u32XOM);
463 
464 
465 /**
466   * @brief      Read XOM Setting (for Non-secure region)
467   * @param[in]  u32XOM          Specified XOM region, it must be between 0~3
468   * @param[out] pu32Base        Return specified XOM base address
469   * @param[out] pu32PageCnt     Return specified XOM page count
470   * @retval     0xF0F00000      pu32Base, pu32PageCnt or XOM base address isn't in Non-secure area
471   * @retval     0xF0F00001      Wrong u32XOM, pu32Base or pu32PageCnt parameter
472   * @retval     0xF0F00003      XOM base address isn't valid flash address
473   * @retval     0xF0F00004      pu32Base or pu32PageCnt isn't valid sram address
474   * @retval     0xF0F00005      XOM region isn't configured
475   * @retval     0               Read specified XOM setting success
476   * @details    This API will read specified XOM relative settings.
477   */
478 int32_t BL_ReadXOMRegion(uint32_t u32XOM, uint32_t *pu32Base, uint32_t *pu32PageCnt);
479 
480 
481 /**
482   * @brief      Set XOM Region and Active (for Non-secure region)
483   * @param[in]  u32XOM          Specified XOM region, it must be between 0~3
484   * @param[in]  u32Base         Base address of XOM region
485   * @param[in]  u32PageCnt      Page count of XOM region
486   * @param[in]  u32IsDebugMode  1: Enable XOM debug mode; others will disable XOM debug mode.
487   * @retval     0xF0F00000      XOM region isn't in Non-secure area
488   * @retval     0xF0F00001      Wrong u32XOM, u32Base or u32PageCnt parameter
489   * @retval     0xF0F00003      u32Base isn't valid flash address
490   * @retval     0xF0F00006      XOM region has configured
491   * @retval     0xF0F00007      XOM region has active
492   * @retval     -1              Set XOM failed
493   * @retval     0               Set specified XOM success
494   * @details    This API will set specified XOM active.
495   */
496 int32_t BL_SetXOMRegion(uint32_t u32XOM, uint32_t u32Base, uint32_t u32PageCnt, uint32_t u32IsDebugMode);
497 
498 
499 /**
500   * @brief      Erase XOM Setting (for Non-secure region)
501   * @param[in]  u32XOMBase  Specified XOM region to be erase
502   * @retval     0xF0F00000  u32XOMBase or erase XOM region isn't in Non-secure area
503   * @retval     0xF0F00001  u32XOMBase isn't page size aligned
504   * @retval     0xF0F00003  u32XOMBase isn't valid flash address
505   * @retval     0xF0F00008  Invalid u32XOMBase address
506   * @retval     -1          Erase XOM region failed
507   * @retval     0           Erase XOM region success
508   * @details    This API will erase specified XOM region data and relative XOM setting.
509   */
510 int32_t BL_EraseXOMRegion(uint32_t u32XOMBase);
511 
512 
513 /**
514   * @brief      Get XOM Erased Status
515   * @param      None
516   * @retval     -1      Erase XOM operation failed
517   * @retval     0       Erase XOM operation success
518   * @details    This API will return the XOM erase operation is success or not.
519   */
520 int32_t BL_GetXOMEraseStatus(void);
521 
522 
523 /**
524   * @brief      Read KPKEYSTS Status
525   * @param      None
526   * @return     KPKEYSTS register status
527   * @details    This API can read KPROM KPKEYSTS register status.
528   */
529 uint32_t BL_GetKPROMStatus(void);
530 
531 
532 /**
533   * @brief      Read KPKEYCNT Status
534   * @param      None
535   * @return     KPKEYCNT register status
536   * @details    This API can read KPROM KPKEYCNT register status.
537   */
538 uint32_t BL_GetKPROMCounter(void);
539 
540 
541 /**
542   * @brief      Read KPCNT Status
543   * @param      None
544   * @return     KPCNT register status
545   * @details    This API can read KPROM KPCNT register status.
546   */
547 uint32_t BL_GetKPROMPowerOnCounter(void);
548 
549 
550 /**
551   * @brief      Execute KPROM Key Comparison
552   * @param[in]  key0        KPROM key0
553   * @param[in]  key1        KPROM key1
554   * @param[in]  key2        KPROM key2
555   * @retval     0xF0F00009  KPROM function isn't enabled
556   * @retval     0xF0F0000A  Trigger KPROM key comparison is FORBID
557   * @retval     0xF0F0000B  KPROM Key is mismatch
558   * @retval     0xF0F0000C  KPROM key still locked
559   * @retval     0           KPROM Key are matched
560   * @details    With this API, user can unlock KPROM write-protection and then execute FMC program command well.
561   */
562 int32_t BL_TrgKPROMCompare(uint32_t key0, uint32_t key1, uint32_t key2);
563 
564 
565 /**
566   * @brief      Execute CHIP Reset
567   * @param      None
568   * @return     None
569   * @details    This API will perform reset CHIP command to reset chip.
570   */
571 void BL_ResetChip(void);
572 
573 
574 /*--------------------------------------------------------------------------------------------------*/
575 /*  The following functions are for Secure code only                                                */
576 /*--------------------------------------------------------------------------------------------------*/
577 /**
578   * @brief      Check if ECC Private Key Valid
579   * @param[in]  xcrpt       The pointer of the global XCRPT data
580   * @param[in]  ecc_curve   The pre-defined ECC curve.
581   * @param[in]  private_k   The input private key.
582   * @return     1   Is valid.
583   * @return     0   Is not valid.
584   * @return     -1  Invalid curve.
585   * @details    This API is used to check if the private key is placed in valid range of curve.
586   */
587 int32_t XECC_IsPrivateKeyValid(XCRPT_T *xcrpt, E_ECC_CURVE ecc_curve, char private_k[]);
588 
589 
590 /**
591   * @brief      Generate ECC Public Key
592   * @param[in]  xcrpt       The pointer of the global XCRPT data
593   * @param[in]  ecc_curve   The pre-defined ECC curve.
594   * @param[in]  private_k   The input private key.
595   * @param[out] public_k1   The output public key 1.
596   * @param[out] public_k2   The output public key 2.
597   * @return     0   Success.
598   * @return     -1  "ecc_curve" value is invalid.
599   * @details    This API is used to generate a public key pair by a specified ECC private key and ECC curve.
600   */
601 int32_t XECC_GeneratePublicKey(XCRPT_T *xcrpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[]);
602 
603 
604 /**
605   * @brief      Generate ECDSA Signature
606   * @param[in]  xcrpt       The pointer of the global XCRPT data
607   * @param[in]  ecc_curve   The pre-defined ECC curve.
608   * @param[in]  message     The hash value of source context.
609   * @param[in]  d           The private key.
610   * @param[in]  k           The selected random integer.
611   * @param[out] R           R of the (R,S) pair digital signature
612   * @param[out] S           S of the (R,S) pair digital signature
613   * @return     0   Success.
614   * @return     -1  "ecc_curve" value is invalid.
615   * @details    This API is used to generate an ECDSA digital signature.
616   */
617 int32_t XECC_GenerateSignature(XCRPT_T *xcrpt, E_ECC_CURVE ecc_curve, char *message, char *d, char *k, char *R, char *S);
618 
619 
620 /**
621   * @brief      Verify ECDSA Signature
622   * @param[in]  xcrpt       The pointer of the global XCRPT data
623   * @param[in]  ecc_curve   The pre-defined ECC curve.
624   * @param[in]  message     The hash value of source context.
625   * @param[in]  public_k1   The public key 1.
626   * @param[in]  public_k2   The public key 2.
627   * @param[in]  R           R of the (R,S) pair digital signature
628   * @param[in]  S           S of the (R,S) pair digital signature
629   * @return     0   Success.
630   * @return     -1  "ecc_curve" value is invalid.
631   * @return     -2  Verification failed.
632   * @details    This API is used to perform the ECDSA digital signature verification.
633   */
634 int32_t XECC_VerifySignature(XCRPT_T *xcrpt, E_ECC_CURVE ecc_curve, char *message, char *public_k1, char *public_k2, char *R, char *S);
635 
636 
637 /**
638   * @brief      Generate ECDH Secret Shared Key
639   * @param[in]  xcrpt       The pointer of the global XCRPT data
640   * @param[in]  ecc_curve   The pre-defined ECC curve.
641   * @param[in]  private_k   One's own private key.
642   * @param[in]  public_k1   The other party's public key 1.
643   * @param[in]  public_k2   The other party's public key 2.
644   * @param[out] secret_z    The ECC CDH secret Z.
645   * @return     0   Success.
646   * @return     -1  "ecc_curve" value is invalid.
647   * @details    This API is used to generate an ECDH shared key.
648   */
649 int32_t XECC_GenerateSecretZ(XCRPT_T *xcrpt, E_ECC_CURVE ecc_curve, char *private_k, char public_k1[], char public_k2[], char secret_z[]);
650 
651 
652 /**
653   * @brief      Convert Data to Hex Format
654   * @param[in]  count   Byte counts for convert.
655   * @param[in]  reg     The input data buffer.
656   * @param[out] output  The output data buffer.
657   * @return     None
658   * @details    This API is used to convert the data to hex format.
659   */
660 void XECC_Reg2Hex(int32_t count, uint32_t volatile reg[], char output[]);
661 
662 
663 /**
664   * @brief      Convert Data to Register Format
665   * @param[in]  input   The input data buffer.
666   * @param[out] reg     The output data buffer.
667   * @return     None
668   * @details    This API is used to convert the data in a register data format.
669   */
670 void XECC_Hex2Reg(char input[], uint32_t volatile reg[]);
671 
672 
673 /**
674   * @brief      Get ID ECC R, S digital signature (for Secure code)
675   * @param[out] R           R of the (R,S) pair digital signature
676   * @param[out] S           S of the (R,S) pair digital signature
677   * @retval     -1          Get R, S digital signature fail
678   * @retval     0           Success
679   * @details    This API will return ECC R, S digital signature of chip ID, include PDID, UID0~2 and UCID0~3.
680   */
681 int32_t XECC_GetIDECCSignature(uint32_t *R, uint32_t *S);
682 
683 
684 /**
685   * @brief      Open TDES Encrypt/Decrypt
686   * @param[in]  xcrpt       The pointer of the global XCRPT data
687   * @param[in]  u32Channel  TDES channel. Must be 0~3.
688   * @param[in]  u32EncDec   1: TDES encode; 0: TDES decode
689   * @param[in]  Is3DES      1: TDES; 0: DES
690   * @param[in]  Is3Key      1: TDES 3 key mode; 0: TDES 2 key mode
691   * @param[in]  u32OpMode   TDES operation mode, including:
692   *                 - \ref TDES_MODE_ECB
693   *                 - \ref TDES_MODE_CBC
694   *                 - \ref TDES_MODE_CFB
695   *                 - \ref TDES_MODE_OFB
696   *                 - \ref TDES_MODE_CTR
697   * @param[in]  u32SwapType is TDES input/output data swap control and word swap control, including:
698   *                 - \ref TDES_NO_SWAP
699   *                 - \ref TDES_WHL_SWAP
700   *                 - \ref TDES_OUT_SWAP
701   *                 - \ref TDES_OUT_WHL_SWAP
702   *                 - \ref TDES_IN_SWAP
703   *                 - \ref TDES_IN_WHL_SWAP
704   *                 - \ref TDES_IN_OUT_SWAP
705   *                 - \ref TDES_IN_OUT_WHL_SWAP
706   * @return     None
707   * @details    This API is used to enable TDES encrypt/decrypt function.
708   */
709 void XTDES_Open(XCRPT_T *xcrpt, uint32_t u32Channel, uint32_t u32EncDec, int32_t Is3DES, int32_t Is3Key, uint32_t u32OpMode, uint32_t u32SwapType);
710 
711 
712 /**
713   * @brief      Start TDES Encrypt/Decrypt
714   * @param[in]  xcrpt       The pointer of the global XCRPT data
715   * @param[in]  u32Channel  TDES channel. Must be 0~3.
716   * @param[in]  u32DMAMode  TDES DMA control, including:
717   *                 - \ref CRYPTO_DMA_ONE_SHOT  One shot TDES encrypt/decrypt.
718   *                 - \ref CRYPTO_DMA_CONTINUE  Continuous TDES encrypt/decrypt.
719   *                 - \ref CRYPTO_DMA_LAST      Last TDES encrypt/decrypt of a series of XTDES_Start.
720   * @return     None
721   * @details    This API is used to start TDES encrypt/decrypt.
722   */
723 void XTDES_Start(XCRPT_T *xcrpt, int32_t u32Channel, uint32_t u32DMAMode);
724 
725 
726 /**
727   * @brief      Set TDES Keys
728   * @param[in]  xcrpt           The pointer of the global XCRPT data
729   * @param[in]  u32Channel      TDES channel. Must be 0~3.
730   * @param[in]  au32Keys        The TDES keys. au32Keys[0][0] is Key0 high word and au32Keys[0][1] is key0 low word.
731   * @return     None
732   * @details    This API is used to set TDES keys.
733   */
734 void XTDES_SetKey(XCRPT_T *xcrpt, uint32_t u32Channel, uint32_t au32Keys[3][2]);
735 
736 
737 /**
738   * @brief      Set TDES Initial Vectors
739   * @param[in]  xcrpt       The pointer of the global XCRPT data
740   * @param[in]  u32Channel  TDES channel. Must be 0~3.
741   * @param[in]  u32IVH      TDES initial vector high word.
742   * @param[in]  u32IVL      TDES initial vector low word.
743   * @return     None
744   * @details    This API is used to set TDES initial vectors.
745   */
746 void XTDES_SetInitVect(XCRPT_T *xcrpt, uint32_t u32Channel, uint32_t u32IVH, uint32_t u32IVL);
747 
748 
749 /**
750   * @brief      Set TDES DMA Transfer Configuration
751   * @param[in]  xcrpt       The pointer of the global XCRPT data
752   * @param[in]  u32Channel  TDES channel. Must be 0~3.
753   * @param[in]  u32SrcAddr  TDES DMA source address
754   * @param[in]  u32DstAddr  TDES DMA destination address
755   * @param[in]  u32TransCnt TDES DMA transfer byte count
756   * @return     None
757   * @details    This API is used to configure the TDES DMA transfer.
758   */
759 void XTDES_SetDMATransfer(XCRPT_T *xcrpt, uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt);
760 
761 
762 /**
763   * @brief      Open SHA Encrypt
764   * @param[in]  xcrpt       The pointer of the global XCRPT data
765   * @param[in]  u32OpMode   SHA operation mode, including:
766   *                 - \ref SHA_MODE_SHA1
767   *                 - \ref SHA_MODE_SHA224
768   *                 - \ref SHA_MODE_SHA256
769   *                 - \ref SHA_MODE_SHA384
770   *                 - \ref SHA_MODE_SHA512
771   * @param[in]  u32SwapType is the SHA input/output data swap control, including:
772   *                 - \ref SHA_NO_SWAP
773   *                 - \ref SHA_OUT_SWAP
774   *                 - \ref SHA_IN_SWAP
775   *                 - \ref SHA_IN_OUT_SWAP
776   * @param[in]  hmac_key_len    HMAC key byte count
777   * @return     None
778   * @details    This API is used to enable SHA encrypt function.
779   */
780 void XSHA_Open(XCRPT_T *xcrpt, uint32_t u32OpMode, uint32_t u32SwapType, uint32_t hmac_key_len);
781 
782 
783 /**
784   * @brief      Start SHA Encrypt
785   * @param[in]  xcrpt       The pointer of the global XCRPT data
786   * @param[in]  u32DMAMode  SHA DMA control, including:
787   *                 - \ref CRYPTO_DMA_ONE_SHOT  One shot SHA encrypt.
788   *                 - \ref CRYPTO_DMA_CONTINUE  Continuous SHA encrypt.
789   *                 - \ref CRYPTO_DMA_LAST      Last SHA encrypt of a series of XSHA_Start.
790   * @return     None
791   * @details    This API is used to start SHA encrypt.
792   */
793 void XSHA_Start(XCRPT_T *xcrpt, uint32_t u32DMAMode);
794 
795 
796 /**
797   * @brief      Set SHA DMA Transfer Configuration
798   * @param[in]  xcrpt       The pointer of the global XCRPT data
799   * @param[in]  u32SrcAddr  SHA DMA source address
800   * @param[in]  u32TransCnt SHA DMA transfer byte count
801   * @return     None
802   * @details    This API is used to configure the SHA DMA transfer.
803   */
804 void XSHA_SetDMATransfer(XCRPT_T *xcrpt, uint32_t u32SrcAddr, uint32_t u32TransCnt);
805 
806 
807 /**
808   * @brief      Read SHA Digest
809   * @param[in]  xcrpt       The pointer of the global XCRPT data
810   * @param[out] u32Digest   The SHA encrypt output digest.
811   * @return     None
812   * @details    This API is used to read the SHA digest.
813   */
814 void XSHA_Read(XCRPT_T *xcrpt, uint32_t u32Digest[]);
815 
816 
817 /**
818   * @brief      Open AES Encrypt/Decrypt
819   * @param[in]  xcrpt       The pointer of the global XCRPT data
820   * @param[in]  u32Channel  AES channel. Must be 0~3.
821   * @param[in]  u32EncDec   1: AES encode;  0: AES decode
822   * @param[in]  u32OpMode   AES operation mode, including:
823   *                 - \ref AES_MODE_ECB
824   *                 - \ref AES_MODE_CBC
825   *                 - \ref AES_MODE_CFB
826   *                 - \ref AES_MODE_OFB
827   *                 - \ref AES_MODE_CTR
828   *                 - \ref AES_MODE_CBC_CS1
829   *                 - \ref AES_MODE_CBC_CS2
830   *                 - \ref AES_MODE_CBC_CS3
831   * @param[in]  u32KeySize is AES key size, including:
832   *                 - \ref AES_KEY_SIZE_128
833   *                 - \ref AES_KEY_SIZE_192
834   *                 - \ref AES_KEY_SIZE_256
835   * @param[in]  u32SwapType is AES input/output data swap control, including:
836   *                 - \ref AES_NO_SWAP
837   *                 - \ref AES_OUT_SWAP
838   *                 - \ref AES_IN_SWAP
839   *                 - \ref AES_IN_OUT_SWAP
840   * @return     None
841   * @details    This API is used to enable AES encrypt/decrypt function.
842   */
843 void XAES_Open(XCRPT_T *xcrpt, uint32_t u32Channel, uint32_t u32EncDec, uint32_t u32OpMode, uint32_t u32KeySize, uint32_t u32SwapType);
844 
845 
846 /**
847   * @brief      Start AES Encrypt/Decrypt
848   * @param[in]  xcrpt       The pointer of the global XCRPT data
849   * @param[in]  u32Channel  AES channel. Must be 0~3.
850   * @param[in]  u32DMAMode  AES DMA control, including:
851   *                     - \ref CRYPTO_DMA_ONE_SHOT  One shot AES encrypt/decrypt.
852   *                     - \ref CRYPTO_DMA_CONTINUE  Continuous AES encrypt/decrypt.
853   *                     - \ref CRYPTO_DMA_LAST      Last AES encrypt/decrypt of a series of XAES_Start.
854   * @return     None
855   * @details    This API is used to start AES encrypt/decrypt.
856   */
857 void XAES_Start(XCRPT_T *xcrpt, int32_t u32Channel, uint32_t u32DMAMode);
858 
859 
860 /**
861   * @brief      Set AES Keys
862   * @param[in]  xcrpt       The pointer of the global XCRPT data
863   * @param[in]  u32Channel  AES channel. Must be 0~3.
864   * @param[in]  au32Keys    An word array contains AES keys.
865   * @param[in]  u32KeySize is AES key size, including:
866   *                 - \ref AES_KEY_SIZE_128
867   *                 - \ref AES_KEY_SIZE_192
868   *                 - \ref AES_KEY_SIZE_256
869   * @return     None
870   * @details    This API is used to set AES keys.
871   */
872 void XAES_SetKey(XCRPT_T *xcrpt, uint32_t u32Channel, uint32_t au32Keys[], uint32_t u32KeySize);
873 
874 
875 /**
876   * @brief      Set AES Initial Vectors
877   * @param[in]  xcrpt       The pointer of the global XCRPT data
878   * @param[in]  u32Channel  AES channel. Must be 0~3.
879   * @param[in]  au32IV      A four entry word array contains AES initial vectors.
880   * @return     None
881   * @details    This API is used to set AES initial vectors.
882   */
883 void XAES_SetInitVect(XCRPT_T *xcrpt, uint32_t u32Channel, uint32_t au32IV[]);
884 
885 
886 /**
887   * @brief      Set AES DMA Transfer Configuration
888   * @param[in]  xcrpt       The pointer of the global XCRPT data
889   * @param[in]  u32Channel   AES channel. Must be 0~3.
890   * @param[in]  u32SrcAddr   AES DMA source address
891   * @param[in]  u32DstAddr   AES DMA destination address
892   * @param[in]  u32TransCnt  AES DMA transfer byte count
893   * @return     None
894   * @details    This API is used to configure the AES DMA transfer.
895   */
896 void XAES_SetDMATransfer(XCRPT_T *xcrpt, uint32_t u32Channel, uint32_t u32SrcAddr, uint32_t u32DstAddr, uint32_t u32TransCnt);
897 
898 
899 /**
900   * @brief      Initial Random Number Generator (for Secure code)
901   *
902   * @param[in]  rng     The structure of random number generator
903   * @param[in]  opt     Operation modes. Possible options are,
904   *                         (XTRNG_PRNG | XTRNG_LIRC32K),
905   *                         (XTRNG_PRNG | XTRNG_LXT),
906   *                         (XTRNG_SWRNG | XTRNG_LIRC32K),
907   *                         (XTRNG_SWRNG | XTRNG_LXT)
908   * @retval     -1      Fail
909   * @retval     0       Success
910   *
911   * @details    This API is used to initial random number generator.
912   *             After initial this API success, user can call XTRNG_Random API to generate the random number.
913   */
914 int32_t XTRNG_RandomInit(XTRNG_T *rng, uint32_t opt);
915 
916 
917 /**
918   * @brief      Generate Random Number (for Secure code)
919   *
920   * @param[in]  rng     The structure of random number generator
921   * @param[out] p       Starting buffer address to store random number
922   * @param[in]  size    Total byte counts of random number
923   * @retval     -1      Fail
924   * @retval     0       Success
925   * @details    This API is used to generate random number.
926   */
927 int32_t XTRNG_Random(XTRNG_T *rng, uint8_t *p, uint32_t size);
928 
929 
930 /**
931   * @brief      Get ID ECC R, S Digital Signature (for Secure code)
932   * @param[out] R           R of the (R,S) pair digital signature
933   * @param[out] S           S of the (R,S) pair digital signature
934   * @retval     -1          Get R, S digital signature fail
935   * @retval     0           Success
936   * @details    This API will return ECC R, S digital signature of chip ID, include PDID, UID0~2 and UCID0~3.
937   */
938 int32_t BL_GetIDECCSignature(uint32_t *R, uint32_t *S);
939 
940 
941 /**
942   * @brief      Initial Random Number Generator (for Secure code)
943   *
944   * @param[in]  rng     The structure of random number generator
945   * @param[in]  opt     Operation modes. Possible options are,
946   *                         (BL_RNG_PRNG | BL_RNG_LIRC32K),
947   *                         (BL_RNG_PRNG | BL_RNG_LXT),
948   *                         (BL_RNG_SWRNG | BL_RNG_LIRC32K),
949   *                         (BL_RNG_SWRNG | BL_RNG_LXT)
950   * @retval     -1      Fail
951   * @retval     0       Success
952   *
953   * @details    This API is used to initial random number generator.
954   *             After initial this API success, user can perform BL_Random API to generate the random number.
955   */
956 int32_t BL_RandomInit(BL_RNG_T *rng, uint32_t opt);
957 
958 
959 /**
960   * @brief      Generate Random Number (for Secure code)
961   *
962   * @param[in]  rng     The structure of random number generator
963   * @param[out] p       Starting buffer address to store random number
964   * @param[in]  size    Total byte counts of random number
965   * @retval     -1      Fail
966   * @retval     0       Success
967   * @details    This API is used to generate random number.
968   */
969 int32_t BL_Random(BL_RNG_T *rng, uint8_t *p, uint32_t size);
970 
971 
972 /**
973   * @brief      Initialize SecureISP Function (for Secure code)
974   * @param[in]  pISPInfo    The ISP information data buffer address
975   * @param[in]  pUSBDInfo   USB data buffer for SecureISP USB
976   * @param[in]  mode        Operation mode. Possible options are
977   *                             - \ref USB_MODE
978   *                             - \ref UART_MODE
979   *                             - \ref USB_UART_MODE
980   *                             - \ref RESYNC_ISP
981   * @return     Return process status and exit SecureISP mode.
982   * @details    Executing this API will initialize USB or UART1 SecureISP function.
983   *             User can use SecureISP Tool to communicate with target chip.
984         Configure relate USB and UART1 settings are necessary before executing this API.
985   */
986 int32_t BL_SecureISPInit(ISP_INFO_T *pISPInfo, BL_USBD_INFO_T *pUSBDInfo, E_ISP_MODE mode);
987 
988 
989 /**
990   * @brief      Process USBD Interrupt (for Secure code)
991   * @param[in]  pfnEPTable      Starting address to store EP callback function
992   * @param[in]  pInfo           The ISP information data buffer address
993   * @param[in]  pUSBDInfo       USB data buffer for SecureISP USB
994   * @retval     -1      Execute API in Non-secure code
995   * @retval     0       Process USBD interrupt event success
996   * @details    This API is used to process USBD command and should be called in USBD_IRQHandler().
997   */
998 int32_t BL_ProcessUSBDInterrupt(uint32_t *pfnEPTable, uint32_t *pInfo, uint32_t *pUSBDInfo);
999 
1000 
1001 /**
1002   * @brief      Process UART1 Interrupt (for Secure code)
1003   * @param[in]  pInfo   The ISP information data buffer address
1004   * @retval     -1      Execute API in Non-secure code
1005   * @retval     0       Process UART1 interrupt event success
1006   * @details    This API is used to process UART1 command and should be called in UART1_IRQHandler().
1007   */
1008 int32_t BL_ProcessUART1Interrupt(uint32_t *pInfo);
1009 
1010 
1011 /**
1012   * @brief      Get Vendor Data (for Secure code)
1013   * @param[in]  pInfo       The ISP information data buffer address
1014   * @param[out] pu32Data    Data buffer to store vendor data. Maximum buffer size is 44 bytes.
1015   * @param[in]  pu32Buf     Internal used data buffer address
1016   * @retval     0           Success
1017   * @retval     -1          Invalid command packet
1018   * @retval     -2          Not in vendor function
1019   * @details    This API is used to get the vendor data and should be called in the vendor function.
1020   */
1021 int32_t BL_GetVendorData(uint32_t *pInfo, uint32_t *pu32Data, uint32_t *pu32Buf);
1022 
1023 
1024 /**
1025   * @brief      Return Vendor Data (for Secure code)
1026   * @param[in]  pu32Data    Data buffer to store response data
1027   * @param[in]  u32Len      Data buffer length, maximum size is 40 bytes.
1028   * @param[in]  pu32Buf     Internal used data buffer address
1029   * @retval     0           Success
1030   * @retval     -1          Invalid buffer length
1031   * @retval     -2          Not in vendor function
1032   * @details    This API is used to return vendor data to server and should be called in the vendor function.
1033   */
1034 int32_t BL_ReturnVendorData(uint32_t *pu32Data, uint32_t u32Len, uint32_t *pu32Buf);
1035 
1036 
1037 /**
1038   * @brief      Generate Command Response Data (for Secure code)
1039   * @param[in]  pCMD        Buffer address to store command data
1040   * @param[in]  pISPInfo    The ISP information data buffer address
1041   * @retval     0           Command success
1042   * @retval     Other       Command fail
1043   * @details    This API is used to generate response data to server.
1044   */
1045 int32_t Cmd_GenRspPacket(CMD_PACKET_T *pCMD, ISP_INFO_T *pISPInfo);
1046 
1047 
1048 /**
1049   * @brief      Parse Command (for Secure code)
1050   * @param[in]  pCMD        Buffer address to store command
1051   * @param[in]  pISPInfo    The ISP information data buffer address
1052   * @retval     0           Command success
1053   * @retval     Other       Command fail
1054   * @details    This API is used to parse data from server.
1055   */
1056 int32_t Cmd_ParseReqPacket(CMD_PACKET_T *pCMD, ISP_INFO_T *pISPInfo);
1057 
1058 
1059 /**
1060   * @brief      Parse CONNECT Command (for Secure code)
1061   * @param[in]  pISPInfo    The ISP information data buffer address
1062   * @retval     0           Success
1063   * @retval     Other       Command fail
1064   * @details    This API is used for parse CONNECT command only.
1065   */
1066 int32_t ParseCONNECT(ISP_INFO_T *pISPInfo);
1067 
1068 
1069 /**
1070   * @brief      Parse related ECDH Commands (for Secure code)
1071   * @param[in]  pISPInfo    The ISP information data buffer address
1072   * @retval     0           Success
1073   * @retval     Other       Command fail
1074   * @details    This API is used for parse related ECDH commands.
1075   */
1076 int32_t ParseECDH(ISP_INFO_T *pISPInfo);
1077 
1078 
1079 /**
1080   * @brief      Parse Commands (for Secure code)
1081   * @param[in]  pISPInfo    The ISP information data buffer address
1082   * @retval     0           Success
1083   * @retval     Other       Command fail
1084   * @details    This API is used for parse all commands except CONNECT and related ECDH commands.
1085   */
1086 int32_t ParseCommands(ISP_INFO_T *pISPInfo);
1087 
1088 
1089 /**
1090   * @brief      Initialize USBD Module (for Secure code)
1091   * @param[in]  param           The structure of USBD information
1092   * @param[in]  pfnClassReq     USB Class request callback function
1093   * @param[in]  pfnSetInterface USB Set Interface request callback function
1094   * @param[in]  pUSBDInfo       USB data buffer for SecureISP USB mode
1095   * @retval     -1              Execute API in Non-secure code
1096   * @retval     0               Success
1097   * @details    This function will enable USB controller, USB PHY transceiver and pull-up resistor of USB_D+ pin. USB PHY will drive SE0 to bus.
1098   */
1099 int32_t BL_USBDOpen(const S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface, uint32_t *pUSBDInfo);
1100 
1101 
1102 /**
1103   * @brief      Install EP Callback Function (for Secure code)
1104   * @param[in]  ep              EP number
1105   * @param[in]  pfnEPHandler    EP callback function
1106   * @param[in]  pfnEPTable      Starting address to store EP callback function
1107   * @retval     -1      Fail
1108   * @retval     0       Success
1109   * @details    This function is used to set specific EP callback function
1110   */
1111 int32_t BL_USBDInstallEPHandler(uint32_t ep, void *pfnEPHandler, uint32_t *pfnEPTable);
1112 
1113 
1114 /**
1115   * @brief      Start USBD Function (for Secure code)
1116   * @param      None
1117   * @retval     -1      Execute API in Non-secure code
1118   * @retval     0       Success
1119   * @details    Enable WAKEUP, FLDET, USB and BUS interrupts. Disable software-disconnect function after 100ms delay with SysTick timer.
1120   */
1121 int32_t BL_USBDStart(void);
1122 
1123 
1124 /*@}*/ /* end of group MKROM_EXPORTED_FUNCTIONS */
1125 
1126 /*@}*/ /* end of group MKROM_Driver */
1127 
1128 /*@}*/ /* end of group Standard_Driver */
1129 
1130 
1131 #ifdef __cplusplus
1132 }
1133 #endif
1134 
1135 #endif  /* __MKROM_LIB_H__ */
1136 
1137