1 /*
2  * Copyright 2024 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __FSL_LOADER_UTILS_H__
9 #define __FSL_LOADER_UTILS_H__
10 
11 #include <stdint.h>
12 #include "fsl_common.h"
13 #include "fsl_loader.h"
14 #include "fusemap.h"
15 #include "fsl_ocotp.h"
16 #include "fsl_os_abstraction.h"
17 
18 //! @addtogroup sbloader
19 //! @{
20 /*! @brief Status group numbers. */
21 #define kStatusGroup_SBLoader                  (101U)
22 #define NBOOT_SB3_BLOCK_HASH384_SIZE_IN_BYTES  (48u)
23 #define NBOOT_ROOT_OF_TRUST_HASH_SIZE_IN_BYTES (48u)
24 #define NBOOT_EC_COORDINATE_384_SIZE_IN_BYTES  (48u)
25 #define NBOOT_EC_COORDINATE_MAX_SIZE           NBOOT_EC_COORDINATE_384_SIZE_IN_BYTES
26 #define NBOOT_ROOT_CERT_COUNT                  (4u)
27 #define NBOOT_SB3_CHUNK_SIZE_IN_BYTES          (256u)
28 #define NBOOT_KEYINFO_WORDLEN                  (23u)
29 #define NXPCLHASH_WA_SIZE_MAX                  (128u + 64u)
30 #define NBOOT_CONTEXT_BYTELEN                  (192u + NXPCLHASH_WA_SIZE_MAX)
31 #define NBOOT_CONTEXT_WORDLEN                  (NBOOT_CONTEXT_BYTELEN / sizeof(uint32_t))
32 #define NXPCLCSS_HASH_RTF_OUTPUT_SIZE          ((size_t)32U)
33 
34 #define SECURE_TERM_PART_LOCK        (0x6ac3c36au)
35 #define SECURE_TERM_PART_OPEN        (0xc36ac36au)
36 #define SECURE_TERM_SECURE_PART_OPEN (0xc36a6ac3u)
37 #define SECURE_TERM_FA_PART_OPEN     (0xc3c36a6au)
38 #define SECURE_OEM_FA_PART_OPEN      (0xc3c3a6a6u)
39 
40 #define LOADER_RAW_BINARY_FORMAT      (0x72617762U)
41 #ifdef CONFIG_FW_VDLLV2
42 #define LOADER_VDLL_RAW_BINARY_FORMAT (0x76646c6cU)
43 #endif
44 
45 /*! @brief partition table constants. */
46 #define WIFI_IMAGE_SIZE_MAX (0xa0000U)
47 #define BLE_IMAGE_SIZE_MAX  (0x50000U)
48 #define Z154_IMAGE_SIZE_MAX (0x50000U)
49 #ifndef WIFI_IMAGE_A_OFFSET
50 #define WIFI_IMAGE_A_OFFSET (0x08400000U)
51 #endif
52 #define WIFI_IMAGE_B_OFFSET (WIFI_IMAGE_A_OFFSET + WIFI_IMAGE_SIZE_MAX) // 0x4a0000
53 #define BLE_IMAGE_A_OFFSET  (WIFI_IMAGE_B_OFFSET + WIFI_IMAGE_SIZE_MAX) // 0x540000
54 #define BLE_IMAGE_B_OFFSET  (BLE_IMAGE_A_OFFSET + BLE_IMAGE_SIZE_MAX)   // 0x590000
55 #define Z154_IMAGE_A_OFFSET (BLE_IMAGE_B_OFFSET + BLE_IMAGE_SIZE_MAX)   // 0x5e0000
56 #define Z154_IMAGE_B_OFFSET (Z154_IMAGE_A_OFFSET + Z154_IMAGE_SIZE_MAX) // 0x630000
57 
58 /** Type for nboot status codes */
59 typedef uint32_t fsl_nboot_status_t;
60 
61 /**
62  * \defgroup nbootStatusValues  This type defines status return values used by NBOOT functions that are not easily
63  * disturbed by Fault Attacks
64  * @{
65  */
66 #define kStatus_NBOOT_Success                ((fsl_nboot_status_t)0x5A5A5A5Au) /*!< Operation completed successfully. */
67 #define kStatus_NBOOT_Fail                   ((fsl_nboot_status_t)0x5A5AA5A5u) /*!< Operation failed. */
68 #define kStatus_NBOOT_InvalidArgument        ((fsl_nboot_status_t)0x5A5AA5F0u) /*!< Invalid argument passed to the function. */
69 #define kStatus_NBOOT_RequestTimeout         ((fsl_nboot_status_t)0x5A5AA5E1u) /*!< Operation timed out. */
70 #define kStatus_NBOOT_KeyNotLoaded           ((fsl_nboot_status_t)0x5A5AA5E2u) /*!< The requested key is not loaded. */
71 #define kStatus_NBOOT_AuthFail               ((fsl_nboot_status_t)0x5A5AA5E4u) /*!< Authentication failed. */
72 #define kStatus_NBOOT_OperationNotAvaialable ((fsl_nboot_status_t)0x5A5AA5E5u) /*!< Operation not available on this HW. */
73 #define kStatus_NBOOT_KeyNotAvailable        ((fsl_nboot_status_t)0x5A5AA5E6u) /*!< Key is not avaialble. */
74 #define kStatus_NBOOT_IvCounterOverflow      ((fsl_nboot_status_t)0x5A5AA5E7u) /*!< Overflow of IV counter (PRINCE/IPED). */
75 #define kStatus_NBOOT_SelftestFail           ((fsl_nboot_status_t)0x5A5AA5E8u) /*!< FIPS self-test failure. */
76 #define kStatus_NBOOT_InvalidDataFormat      ((fsl_nboot_status_t)0x5A5AA5E9u) /*!< Invalid data format for example antipole */
77 #define kStatus_NBOOT_IskCertUserDataTooBig \
78     ((fsl_nboot_status_t)0x5A5AA5EAu) /*!< Size of User data in ISK certificate is greater than 96 bytes */
79 #define kStatus_NBOOT_IskCertSignatureOffsetTooSmall \
80     ((fsl_nboot_status_t)0x5A5AA5EBu) /*!< Signature offset in ISK certificate is smaller than expected */
81 #define kStatus_NBOOT_MemcpyFail ((fsl_nboot_status_t)0x5A5A845A) /*!< Unexpected error detected during nboot_memcpy() */
82 
83 /*! @brief sb3.1 maigc number. */
84 #define TAG_SB_V3 (0x33766273U) // "sbv3"
85 
86 /*!
87  * @brief NBOOT type for the root key revocation
88  *
89  * This type defines the NBOOT root key revocation
90  *
91  */
92 #define kNBOOT_RootKey_Enabled (0xAAu)
93 #define kNBOOT_RootKey_Revoked (0xBBu)
94 
95 /*! @brief The size of the root of trust key table hash. */
96 #define NBOOT_ROOT_ROTKH_SIZE_IN_WORD (12)
97 #define NBOOT_ROOT_ROTKH_SIZE_IN_BYTE (NBOOT_ROOT_ROTKH_SIZE_IN_WORD * 4)
98 
99 /*! @brief The size of PKC Blob. */
100 #define NBOOT_PCK_BLOB_SIZE_IN_WORD (12)
101 #define NBOOT_PCK_BLOB_SIZE_IN_BYTE (NBOOT_PCK_BLOB_SIZE_IN_WORD * 4)
102 
103 /*!
104  * @brief NBOOT type specifying the elliptic curve to be used
105  *
106  * This type defines the elliptic curve type and length
107  *
108  */
109 #define kNBOOT_RootKey_Ecdsa_P256 (0x0000FE01u)
110 #define kNBOOT_RootKey_Ecdsa_P384 (0x0000FD02u)
111 
112 /*!
113  * @brief NBOOT type for the root key usage
114  *
115  * This type defines the NBOOT root key usage
116  *
117  */
118 #define kNBOOT_RootKeyUsage_DebugCA_ImageCA_FwCA_ImageKey_FwKey (0x0u)
119 #define kNBOOT_RootKeyUsage_Unused                              (0x7u)
120 
121 //! @brief SB loader status codes.
122 enum _sbloader_status
123 {
124     kStatusRomLdrSectionOverrun  = MAKE_STATUS(kStatusGroup_SBLoader, 0),
125     kStatusRomLdrSignature       = MAKE_STATUS(kStatusGroup_SBLoader, 1),
126     kStatusRomLdrSectionLength   = MAKE_STATUS(kStatusGroup_SBLoader, 2),
127     kStatusRomLdrUnencryptedOnly = MAKE_STATUS(kStatusGroup_SBLoader, 3),
128     kStatusRomLdrEOFReached      = MAKE_STATUS(kStatusGroup_SBLoader, 4),
129     kStatusRomLdrChecksum        = MAKE_STATUS(kStatusGroup_SBLoader, 5),
130     kStatusRomLdrCrc32Error      = MAKE_STATUS(kStatusGroup_SBLoader, 6),
131     kStatusRomLdrUnknownCommand  = MAKE_STATUS(kStatusGroup_SBLoader, 7),
132     kStatusRomLdrIdNotFound      = MAKE_STATUS(kStatusGroup_SBLoader, 8),
133     kStatusRomLdrDataUnderrun    = MAKE_STATUS(kStatusGroup_SBLoader, 9),
134     kStatusRomLdrJumpReturned    = MAKE_STATUS(kStatusGroup_SBLoader, 10),
135     kStatusRomLdrCallFailed      = MAKE_STATUS(kStatusGroup_SBLoader, 11),
136     kStatusRomLdrKeyNotFound     = MAKE_STATUS(kStatusGroup_SBLoader, 12),
137     kStatusRomLdrSecureOnly      = MAKE_STATUS(kStatusGroup_SBLoader, 13),
138     kStatusRomLdrResetReturned   = MAKE_STATUS(kStatusGroup_SBLoader, 14),
139 
140     kStatusRomLdrRollbackBlocked        = MAKE_STATUS(kStatusGroup_SBLoader, 15),
141     kStatusRomLdrInvalidSectionMacCount = MAKE_STATUS(kStatusGroup_SBLoader, 16),
142     kStatusRomLdrUnexpectedCommand      = MAKE_STATUS(kStatusGroup_SBLoader, 17),
143     kStatusRomLdrBadSBKEK               = MAKE_STATUS(kStatusGroup_SBLoader, 18),
144     kStatusRomLdrPendingJumpCommand     = MAKE_STATUS(kStatusGroup_SBLoader, 19),
145 };
146 
147 /*!
148  * @brief Boolean type for the NBOOT functions
149  *
150  * This type defines boolean values used by NBOOT functions that are not easily disturbed by Fault Attacks
151  *
152  */
153 typedef enum _fsl_nboot_bool
154 {
155     kNBOOT_TRUE                = 0x3C5AC33Cu, /*!< Value for TRUE.  */
156     kNBOOT_TRUE256             = 0x3C5AC35Au, /*!< Value for TRUE when P256 was used to sign the image.  */
157     kNBOOT_TRUE384             = 0x3C5AC3A5u, /*!< Value for TRUE when P384 was used to sign the image.  */
158     kNBOOT_FALSE               = 0x5AA55AA5u, /*!< Value for FALSE. */
159     kNBOOT_OperationAllowed    = 0x3c5a33ccU,
160     kNBOOT_OperationDisallowed = 0x5aa5cc33U,
161 } fsl_nboot_bool_t;
162 
163 /** Type for nboot protected status codes */
164 typedef uint64_t fsl_nboot_status_protected_t;
165 
166 /*!
167  * @brief NBOOT type for a timestamp
168  *
169  * This type defines the NBOOT timestamp
170  *
171  */
172 typedef uint32_t fsl_nboot_timestamp_t[2];
173 typedef uint32_t fsl_nboot_root_key_revocation_t;
174 typedef uint32_t fsl_nboot_root_key_usage_t;
175 typedef uint32_t fsl_nboot_root_key_type_and_length_t;
176 typedef uint32_t fsl_nboot_soc_lifecycle_t;
177 
178 /*!
179  * @brief NBOOT type for the root of trust parameters
180  *
181  * This type defines the NBOOT root of trust parameters
182  *
183  */
184 #define kNBOOT_SocRkh_Size_Words_P384 (12u)
185 #define kNBOOT_SocRkh_Size_Words_P256 (8u)
186 typedef struct _fsl_nboot_rot_auth_parms
187 {
188     /* trusted information originated from CFPA */
189     fsl_nboot_root_key_revocation_t soc_rootKeyRevocation[NBOOT_ROOT_CERT_COUNT]; /*!< Provided by caller based on NVM
190                                                                                  information in CFPA: ROTKH_REVOKE */
191     uint32_t soc_imageKeyRevocation; /*!< Provided by caller based on NVM information in CFPA: IMAGE_KEY_REVOKE */
192 
193     /* trusted information originated from CMPA */
194     uint32_t soc_rkh[kNBOOT_SocRkh_Size_Words_P384]; /*!< Provided by caller based on NVM information in CMPA: ROTKH
195                                                         (hash of hashes) */
196     /*!< In case of kNBOOT_RootKey_Ecdsa_P384, sock_rkh[0..11] are used */
197     /*!< In case of kNBOOT_RootKey_Ecdsa_P256, sock_rkh[0..7] are used */
198 
199     uint32_t soc_numberOfRootKeys; /* unsigned int, between minimum = 1 and maximum = 4; */
200     fsl_nboot_root_key_usage_t soc_rootKeyUsage[NBOOT_ROOT_CERT_COUNT]; /* CMPA */
201     fsl_nboot_root_key_type_and_length_t
202         soc_rootKeyTypeAndLength; /* static selection between ECDSA P-256 or ECDSA P-384 based root keys */
203 
204     /* trusted information originated from OTP fuses */
205     fsl_nboot_soc_lifecycle_t soc_lifecycle;
206 } fsl_nboot_rot_auth_parms_t;
207 
208 /*!
209  * @brief NBOOT SB3.1 header type
210  *
211  * This type defines the header used in the SB3.1 manifest
212  *
213  */
214 typedef struct _fsl_nboot_sb3_header
215 {
216     uint32_t magic;         /*! offset 0x00: Fixed 4-byte string of 'sbv3' without the trailing NULL */
217     uint32_t formatVersion; /*! offset 0x04: (major = 3, minor = 1); The format version determines the manifest (block0)
218                                size. */
219     uint32_t flags;         /*! offset 0x08: not defined yet, keep zero for future compatibility */
220     uint32_t blockCount;    /*! offset 0x0C: Number of blocks not including the manifest (block0). */
221     uint32_t blockSize; /*! offset 0x10: Size in bytes of data block (repeated blockCount times for SB3 data stream). */
222     fsl_nboot_timestamp_t timeStamp;     /*! offset 0x14: 64-bit value used as key derivation data. */
223     uint32_t firmwareVersion;        /*! offset 0x1c: Version number of the included firmware */
224     uint32_t imageTotalLength;       /*! offset 0x20: Total manifest length in bytes, including signatures etc. */
225     uint32_t imageType;              /*! offset 0x24: image type and flags */
226     uint32_t certificateBlockOffset; /*! offset 0x28: Offset from start of header block to the certificate block. */
227     uint8_t description[16];         /*! offset 0x32: This field provides description of the file. It is an arbitrary
228                                                       string injected by the signing tool, which helps to identify the file. */
229 } fsl_nboot_sb3_header_t;
230 
231 /*!
232  * @brief  manifest loading parameters
233  *
234  * This type defines the NBOOT SB3.1 manifest loading parameters
235  *
236  */
237 typedef struct _fsl_nboot_sb3_load_manifest_parms
238 {
239     fsl_nboot_rot_auth_parms_t soc_RoTNVM;   /*! trusted information originated from CFPA and NMPA */
240     uint32_t soc_trustedFirmwareVersion; /*!< Provided by caller based on NVM information in CFPA: Secure_FW_Version */
241     uint8_t pckBlob[48];                 /*! CSSv2 protected blob with Part Common Key (PCK) */
242 } fsl_nboot_sb3_load_manifest_parms_t;
243 
244 /*! @brief Data structure holding input arguments to POR secure boot (authentication) algorithm. Shall be read from SoC
245  * trusted NVM or SoC fuses. */
246 typedef struct _fsl_nboot_img_auth_ecdsa_parms
247 {
248     /* trusted information originated from CFPA and NMPA */
249     fsl_nboot_rot_auth_parms_t soc_RoTNVM;
250 
251     uint32_t soc_trustedFirmwareVersion; /*!< Provided by caller based on NVM information in CFPA: Secure_FW_Version */
252 } fsl_nboot_img_auth_ecdsa_parms_t;
253 
254 /*! @brief The size of the blob with Key Blob. */
255 #define NBOOT_KEY_BLOB_SIZE_IN_BYTE_256 (32)
256 #define NBOOT_KEY_BLOB_SIZE_IN_BYTE_384 (48)
257 #define NBOOT_KEY_BLOB_SIZE_IN_BYTE_MAX (NBOOT_KEY_BLOB_SIZE_IN_BYTE_384)
258 
259 #define NBOOT_SB3_MANIFEST_MAX_SIZE_IN_BYTES (808)
260 #define NBOOT_SB3_BLOCK_MAX_SIZE_IN_BYTES    (308)
261 
262 // Provides forward reference to the loader context definition.
263 typedef struct _fsl_ldr_Context_v3 fsl_ldr_Context_v3_t;
264 
265 //! sb3 section definitions
266 
267 //! section type
268 typedef enum _fsl_sectionType
269 {
270     kSectionNone       = 0, // end or invalid
271     kSectionDataRange  = 1,
272     kSectionDiffUpdate = 2,
273     kSectionDDRConfig  = 3,
274     kSectionRegister   = 4,
275 } fsl_section_type_t;
276 
277 #define SB3_DATA_RANGE_HEADER_FLAGS_ERASE_MASK (0x1u) // bit 0
278 #define SB3_DATA_RANGE_HEADER_FLAGS_LOAD_MASK  (0x2u) // bit 1
279 
280 #define SB3_DATA_RANGE_HEADER_TAG       (0x55aaaa55U)
281 #define SB3_DATA_ALIGNMENT_SIZE_IN_BYTE (16u)
282 
283 //! section data range structure
284 typedef struct fsl_range_header
285 {
286     uint32_t tag;
287     uint32_t startAddress;
288     uint32_t length;
289     uint32_t cmd;
290 } fsl_sb3_data_range_header_t;
291 
292 typedef struct fsl_range_header_expansion
293 {
294     uint32_t memoryId;
295     uint32_t pad0;
296     uint32_t pad1;
297     uint32_t pad2;
298 } fsl_sb3_data_range_expansion_t;
299 
300 //! sb3 DATA section header format
301 typedef struct fsl_section_header
302 {
303     uint32_t sectionUid;
304     uint32_t sectionType;
305     uint32_t length;
306     uint32_t _pad;
307 } fsl_sb3_section_header_t;
308 
309 // loader command enum
310 
311 typedef enum _fsl_loader_command_sb3
312 {
313     kSB3_CmdInvalid = 0,
314     kSB3_CmdLoad    = 2,
315     kSB3_CmdExecute = 3,
316 } fsl_sb3_cmd_t;
317 
318 //! The all of the allowed command
319 #define SBLOADER_V3_CMD_SET_ALL ((1u << kSB3_CmdLoad) | (1u << kSB3_CmdExecute))
320 
321 #define SB3_DATA_BUFFER_SIZE_IN_BYTE (MAX(128, NBOOT_KEY_BLOB_SIZE_IN_BYTE_MAX))
322 
323 /*! @brief Data structure holding secure counter value used by nboot library */
324 typedef struct _fsl_nboot_secure_counter
325 {
326     uint32_t sc;
327     uint32_t scAp;
328 } fsl_nboot_secure_counter_t;
329 
330 /*!
331  * @brief NBOOT context type
332  *
333  * This type defines the NBOOT context
334  *
335  */
336 typedef struct _fsl_nboot_context
337 {
338     uint32_t totalBlocks; /*!< holds number of SB3 blocks. Initialized by nboot_sb3_load_header(). */
339     uint32_t processData; /*!< flag, initialized by nboot_sb3_load_header().
340                              SB3 related flag set by NBOOT in case the nboot_sb3_load_block()
341                              provides plain data to output buffer (for processing by ROM SB3 loader */
342     uint32_t timeout;     /*!< timeout value for css operation. In case it is 0, infinite wait is performed */
343     uint32_t keyinfo[NBOOT_KEYINFO_WORDLEN]; /*!< data for NBOOT key management. */
344     uint32_t context[NBOOT_CONTEXT_WORDLEN]; /*!< work area for NBOOT lib. */
345     uint32_t uuid[4];                        /*!< holds UUID value from NMPA */
346     uint32_t prngReadyFlag;     /*!< flag, used by nboot_rng_generate_lq_random() to determine whether CSS is ready to
347                                    generate rnd number */
348     uint32_t multipartMacBuffer[1024 / sizeof(uint32_t)];
349     uint32_t oemShareValidFlag; /*!< flag, used during TP to determine whether valid oemShare was set by
350                                    nboot_tp_isp_gen_oem_master_share() */
351     uint32_t oemShare[4]; /*!< buffer to store OEM_SHARE computed by nxpCLTrustProv_nboot_isp_gen_oem_master_share() */
352     fsl_nboot_secure_counter_t secureCounter; /*!< Secure counter used by nboot */
353     uint32_t rtf[NXPCLCSS_HASH_RTF_OUTPUT_SIZE / sizeof(uint32_t)];
354     uint32_t imageHash[48 / sizeof(uint32_t)];
355     uint32_t authStatus;
356     fsl_nboot_bool_t disableProvisioningFirmwareNXP; /*!< Flag to disable execution of NXP signed provisioning Firmwares */
357 } fsl_nboot_context_t;
358 
359 //! @brief Structure of version property.
360 //!
361 //! @ingroup bl_core
362 typedef union fsl_StandardVersion
363 {
364     struct
365     {
366         uint8_t bugfix; //!< bugfix version [7:0]
367         uint8_t minor;  //!< minor version [15:8]
368         uint8_t major;  //!< major version [23:16]
369         char name;      //!< name [31:24]
370     };
371     uint32_t version;   //!< combined version numbers
372 
373 #if defined(__cplusplus)
StandardVersion()374     StandardVersion() : version(0)
375     {
376     }
StandardVersion(uint32_t version)377     StandardVersion(uint32_t version) : version(version)
378     {
379     }
380 #endif
381 } fsl_standard_version_t;
382 
383 //!@brief Memory region information table
384 typedef struct fsl_mem_region
385 {
386     uint32_t start;
387     uint32_t end;
388 } fsl_mem_region_t;
389 
390 //! @brief Memory Attribute Structure
391 typedef struct _fsl_mem_attribute
392 {
393     uint32_t memId;
394     uint32_t regionCount;
395     fsl_mem_region_t *memRegions;
396     void *context;
397 } fsl_mem_attribute_t;
398 
399 typedef struct _fsl_arena_context
400 {
401     uint32_t start;
402     uint32_t end;
403     uint32_t nextAddr;
404 } fsl_arena_context_t;
405 
406 //!@brief Memory region interface structure
407 typedef struct fsl_api_memory_region_interface
408 {
409     status_t (*init)(fsl_mem_attribute_t *attr);
410 #if defined(ROM_API_HAS_FEATURE_MEM_READ)
411 #if ROM_API_HAS_FEATURE_MEM_READ
412     status_t (*read)(fsl_mem_attribute_t *attr, uint32_t addr, uint32_t leth, uint8_t *buf);
413 #endif
414 #endif
415 
416     status_t (*write)(fsl_mem_attribute_t *attr, uint32_t addr, uint32_t len, const uint8_t *buf);
417     status_t (*fill)(fsl_mem_attribute_t *attr, uint32_t addr, uint32_t len, uint32_t pattern);
418     status_t (*flush)(fsl_mem_attribute_t *attr);
419     status_t (*erase)(fsl_mem_attribute_t *attr, uint32_t addr, uint32_t len);
420     status_t (*config)(fsl_mem_attribute_t *attr, uint32_t *buf);
421     status_t (*erase_all)(fsl_mem_attribute_t *attr);
422     status_t (*alloc_ctx)(fsl_arena_context_t *ctx, fsl_mem_attribute_t *attr, void *miscParams);
423 } fsl_api_memory_region_interface_t;
424 
425 /*******************************************************************************
426  * Definitions
427  ******************************************************************************/
428 //! @brief Contiguous RAM region count
429 #define RAM_REGION_COUNT (2U)
430 
431 //! @brief Contiguous FLEXSPINOR meomry count
432 #define FLEXSPINOR_REGION_COUNT (1U)
433 
434 //! @brief Memory Interface count
435 #define MEM_INTERFACE_COUNT (2U)
436 
437 //!@brief FlexSPI LUT Sequence structure
438 typedef struct _fsl_lut_sequence
439 {
440     uint8_t seqNum; //!< Sequence Number, valid number: 1-16
441     uint8_t seqId;  //!< Sequence Index, valid number: 0-15
442     uint16_t reserved;
443 } fsl_flexspi_lut_seq_t;
444 
445 //!@brief FlexSPI Pad Configuration Override
446 typedef struct
447 {
448     uint8_t pu_pd_override_en;
449     uint8_t pu_pd_value;
450     uint8_t sr_config_override_en;
451     uint8_t sr_config_value;
452 } fsl_flexspi_pad_config_override_t;
453 
454 typedef struct
455 {
456     uint8_t time_100ps;  // Data valid time, in terms of 100ps
457     uint8_t delay_cells; // Data valid time, in terms of delay cells
458 } fsl_flexspi_dll_time_t;
459 
460 //!@brief FlexSPI Memory Configuration Block
461 typedef struct _fsl_FlexSPIConfig
462 {
463     uint32_t tag;               //!< [0x000-0x003] Tag, fixed value 0x42464346UL
464     uint32_t version;           //!< [0x004-0x007] Version,[31:24] -'V', [23:16] - Major, [15:8] - Minor, [7:0] - bugfix
465     uint32_t reserved0;         //!< [0x008-0x00b] Reserved for future use
466     uint8_t readSampleClkSrc;   //!< [0x00c-0x00c] Read Sample Clock Source, valid value: 0/1/3
467     uint8_t csHoldTime;         //!< [0x00d-0x00d] CS hold time, default value: 3
468     uint8_t csSetupTime;        //!< [0x00e-0x00e] CS setup time, default value: 3
469     uint8_t columnAddressWidth; //!< [0x00f-0x00f] Column Address with, for HyperBus protocol, it is fixed to 3, For
470     //! Serial NAND, need to refer to datasheet
471     uint8_t deviceModeCfgEnable; //!< [0x010-0x010] Device Mode Configure enable flag, 1 - Enable, 0 - Disable
472     uint8_t deviceModeType; //!< [0x011-0x011] Specify the configuration command type:Quad Enable, DPI/QPI/OPI switch,
473     //! Generic configuration, etc.
474     uint16_t waitTimeCfgCommands; //!< [0x012-0x013] Wait time for all configuration commands, unit: 100us, Used for
475     //! DPI/QPI/OPI switch or reset command
476     fsl_flexspi_lut_seq_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt
477     //! sequence number, [31:16] Reserved
478     uint32_t deviceModeArg;    //!< [0x018-0x01b] Argument/Parameter for device configuration
479     uint8_t configCmdEnable;   //!< [0x01c-0x01c] Configure command Enable Flag, 1 - Enable, 0 - Disable
480     uint8_t configModeType[3]; //!< [0x01d-0x01f] Configure Mode Type, similar as deviceModeTpe
481     fsl_flexspi_lut_seq_t
482         configCmdSeqs[3]; //!< [0x020-0x02b] Sequence info for Device Configuration command, similar as deviceModeSeq
483     uint32_t reserved1;   //!< [0x02c-0x02f] Reserved for future use
484     uint32_t configCmdArgs[3];     //!< [0x030-0x03b] Arguments/Parameters for device Configuration commands
485     uint32_t reserved2;            //!< [0x03c-0x03f] Reserved for future use
486     uint32_t controllerMiscOption; //!< [0x040-0x043] Controller Misc Options, see Misc feature bit definitions for more
487     //! details
488     uint8_t deviceType;    //!< [0x044-0x044] Device Type:  See Flash Type Definition for more details
489     uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
490     uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot
491     //! Chapter for more details
492     uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
493     //! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
494     uint32_t reserved3[2]; //!< [0x048-0x04f] Reserved for future use
495     uint32_t sflashA1Size; //!< [0x050-0x053] Size of Flash connected to A1
496     uint32_t sflashA2Size; //!< [0x054-0x057] Size of Flash connected to A2 (unused/not applicable on RW610)
497     uint32_t sflashB1Size; //!< [0x058-0x05b] Size of Flash connected to B1
498     uint32_t sflashB2Size; //!< [0x05c-0x05f] Size of Flash connected to B2 (unused/not applicable on RW610)
499     fsl_flexspi_pad_config_override_t csPadSettingOverride;   //!< [0x060-0x063] CS pad setting override value
500     fsl_flexspi_pad_config_override_t sclkPadSettingOverride; //!< [0x064-0x067] SCK pad setting override value
501     fsl_flexspi_pad_config_override_t dataPadSettingOverride; //!< [0x068-0x06b] data pad setting override value
502     fsl_flexspi_pad_config_override_t dqsPadSettingOverride;  //!< [0x06c-0x06f] DQS pad setting override value
503     uint32_t timeoutInMs;                                 //!< [0x070-0x073] Timeout threshold for read status command
504     uint32_t commandInterval;                             //!< [0x074-0x077] CS deselect interval between two commands
505     fsl_flexspi_dll_time_t dataValidTime[2]; //!< [0x078-0x07b] CLK edge to data valid time for PORT A and PORT B
506     uint16_t busyOffset;                 //!< [0x07c-0x07d] Busy offset, valid value: 0-31
507     uint16_t busyBitPolarity; //!< [0x07e-0x07f] Busy flag polarity, 0 - busy flag is 1 when flash device is busy, 1 -
508     //! busy flag is 0 when flash device is busy
509     uint32_t lookupTable[64];           //!< [0x080-0x17f] Lookup table holds Flash command sequences
510     fsl_flexspi_lut_seq_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences
511     uint32_t dll0CrVal;                 //!> [0x1b0-0x1b3] Customizable DLL0CR setting
512     uint32_t dll1CrVal;                 //!> [0x1b4-0x1b7] Customizable DLL1CR setting
513     uint32_t reserved4[2];              //!< [0x1b8-0x1bf] Reserved for future use
514 } fsl_flexspi_mem_config_t;
515 
516 typedef struct fsl_soc_memory_map_struct
517 {
518     struct
519     {
520         uint32_t start;
521         uint32_t end;
522     } ramRegions[RAM_REGION_COUNT];
523     struct
524     {
525         uint32_t start;
526         uint32_t end;
527     } flexspiNorRegions[FLEXSPINOR_REGION_COUNT];
528 } fsl_soc_mem_regions_t;
529 
530 //!@brief Memory entry data structure
531 typedef struct fsl_memory_map_entry
532 {
533     fsl_mem_attribute_t *memoryAttribute;
534     const fsl_api_memory_region_interface_t *memoryInterface;
535 } fsl_api_memory_map_entry_t;
536 
537 //!@brief API initialization data structure
538 typedef struct fsl_kb_api_parameter_struct
539 {
540     uint32_t allocStart;
541     uint32_t allocSize;
542 } fsl_kp_api_init_param_t;
543 
544 //!@brief Memory context structure
545 typedef struct fsl_memory_context_struct
546 {
547     status_t (*flush)(fsl_mem_attribute_t *attr);
548     fsl_mem_attribute_t *attr;
549 } fsl_mem_context_t;
550 
551 /*
552  *  Serial NOR configuration block
553  */
554 typedef struct _fsl_flexspi_nor_config
555 {
556     fsl_flexspi_mem_config_t memConfig; //!< Common memory configuration info via FlexSPI
557     uint32_t pageSize;              //!< Page size of Serial NOR
558     uint32_t sectorSize;            //!< Sector size of Serial NOR
559     uint8_t ipcmdSerialClkFreq;     //!< Clock frequency for IP command
560     uint8_t isUniformBlockSize;     //!< Sector/Block size is the same
561     uint8_t isDataOrderSwapped;     //!< Data order (D0, D1, D2, D3) is swapped (D1,D0, D3, D2)
562     uint8_t reserved0[1];           //!< Reserved for future use
563     uint8_t serialNorType;          //!< Serial NOR Flash type: 0/1/2/3
564     uint8_t needExitNoCmdMode;      //!< Need to exit NoCmd mode before other IP command
565     uint8_t halfClkForNonReadCmd;   //!< Half the Serial Clock for non-read command: true/false
566     uint8_t needRestoreNoCmdMode;   //!< Need to Restore NoCmd mode after IP commmand execution
567     uint32_t blockSize;             //!< Block size
568     uint32_t flashStateCtx;         //!< Flash State Context
569     uint32_t reserve2[10];          //!< Reserved for future use
570 } fsl_flexspi_nor_config_t;
571 
572 //!@brief The API context structure
573 typedef struct fsl_api_core_context
574 {
575     fsl_soc_mem_regions_t memRegions;
576     fsl_arena_context_t arenaCtx;
577     fsl_flexspi_nor_config_t flexspinorCfg;
578     fsl_mem_context_t memCtx;
579     fsl_ldr_Context_v3_t *sbloaderCtx;
580     fsl_nboot_context_t *nbootCtx;
581     uint8_t *sharedBuf;
582     fsl_api_memory_map_entry_t memEntries[MEM_INTERFACE_COUNT];
583 } fsl_api_core_context_t;
584 
585 //!@brief IAP API Interface structure
586 typedef struct fsl_iap_api_interface_struct
587 {
588     fsl_standard_version_t version; //!< IAP API version number.
589     status_t (*api_init)(fsl_api_core_context_t *coreCtx, const fsl_kp_api_init_param_t *param);
590     status_t (*api_deinit)(fsl_api_core_context_t *coreCtx);
591     status_t (*mem_init)(fsl_api_core_context_t *ctx);
592     status_t (*mem_read)(fsl_api_core_context_t *ctx, uint32_t addr, uint32_t len, uint8_t *buf, uint32_t memoryId);
593     status_t (*mem_write)(fsl_api_core_context_t *ctx, uint32_t addr, uint32_t len, const uint8_t *buf, uint32_t memoryId);
594     status_t (*mem_fill)(fsl_api_core_context_t *ctx, uint32_t addr, uint32_t len, uint32_t pattern, uint32_t memoryId);
595     status_t (*mem_flush)(fsl_api_core_context_t *ctx);
596     status_t (*mem_erase)(fsl_api_core_context_t *ctx, uint32_t addr, uint32_t len, uint32_t memoryId);
597     status_t (*mem_config)(fsl_api_core_context_t *ctx, uint32_t *buf, uint32_t memoryId);
598     status_t (*mem_erase_all)(fsl_api_core_context_t *ctx, uint32_t memoryId);
599     status_t (*fsl_sbloader_init)(fsl_api_core_context_t *ctx);
600     status_t (*fsl_sbloader_pump)(fsl_api_core_context_t *ctx, uint8_t *data, uint32_t length);
601     status_t (*fsl_sbloader_finalize)(fsl_api_core_context_t *ctx);
602 } fsl_iap_api_interface_t;
603 
604 //! Function pointer definition for all loader action functions.
605 // typedef status_t (*fsl_pLdrFnc_v3_t)(ldr_Context_v3_t *);
606 typedef status_t (*fsl_pLdrFnc_v3_t)(fsl_api_core_context_t *n);
607 
608 //! Loader context definition.
609 struct _fsl_ldr_Context_v3
610 {
611     fsl_pLdrFnc_v3_t Action;        //!< pointer to loader action function
612     uint32_t block_size;        //!< size of each block in bytes
613     uint32_t block_data_size;   //!< data size in bytes (NBOOT_SB3_CHUNK_SIZE_IN_BYTES)
614     uint32_t block_data_total;  //!< data max size in bytes (block_size * data_size
615     uint32_t block_buffer_size; //!< block0 and block size
616     uint32_t block_buffer_position;
617     uint8_t block_buffer[MAX(NBOOT_SB3_MANIFEST_MAX_SIZE_IN_BYTES,
618                              NBOOT_SB3_BLOCK_MAX_SIZE_IN_BYTES)]; //! will be used for both block0 and blockx
619     uint32_t processedBlocks;
620 
621     uint8_t data_block_offset; //! data block offset in a block.
622     bool in_data_block;        //!< in progress of handling a data block within a block
623     uint8_t *data_block;
624     uint32_t data_block_position;
625 
626     bool in_data_section; //!< in progress of handling a data section within a data block
627     uint32_t data_section_handled;
628     fsl_sb3_section_header_t data_section_header;
629 
630     bool in_data_range; //!< in progress of handling a data range within a data section
631     uint32_t data_range_handled;
632     uint32_t data_range_gap;
633     fsl_sb3_data_range_header_t data_range_header;
634     bool has_data_range_expansion;
635     fsl_sb3_data_range_expansion_t data_range_expansion;
636 
637     uint32_t commandSet; //!< support command set during sb file handling
638 
639     uint32_t data_position;
640     uint8_t data_buffer[SB3_DATA_BUFFER_SIZE_IN_BYTE]; //!< temporary data buffer
641 
642     uint32_t fuse_cmd_position;
643     uint8_t fuse_cmd_buffer[32 * 4]; //!< used for fuse command
644 };
645 
646 #if defined(__cplusplus)
647 extern "C" {
648 #endif // __cplusplus
649 
650 #if defined(__cplusplus)
651 }
652 #endif // __cplusplus
653 
654 #define BIT0 (1UL << 0U)
655 
656 void power_on_device_impl(LOAD_Target_Type loadTarget);
657 void power_off_device_impl(LOAD_Target_Type loadTarget);
658 void reset_device(LOAD_Target_Type loadTarget);
659 status_t load_service(LOAD_Target_Type loadTarget, uint32_t sourceAddr);
660 status_t sb3_fw_download_impl(LOAD_Target_Type loadTarget, uint32_t flag, uint32_t sourceAddr);
661 status_t sb3_fw_reset_impl(LOAD_Target_Type loadTarget, uint32_t flag, uint32_t sourceAddr);
662 fsl_nboot_status_t nboot_hal_get_sb3_manifest_params(fsl_nboot_context_t *context, fsl_nboot_sb3_load_manifest_parms_t *parms);
663 status_t loader_process_sb_file(uint32_t readOffset);
664 status_t fsl_sbloader_init(fsl_api_core_context_t *ctx);
665 status_t fsl_sbloader_finalize(fsl_api_core_context_t *ctx);
666 uint8_t get_chip_revision(void);
667 
668 //! @}
669 
670 #endif /* __FSL_LOADER_UTILS_H__ */
671