1 /*
2  * Copyright 2021 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _FSL_SBLOADER_V3_H_
9 #define _FSL_SBLOADER_V3_H_
10 
11 #include <stdint.h>
12 
13 #include "fsl_nboot_hal.h"
14 
15 /*! @addtogroup sbloader */
16 /*! @{ */
17 
18 /*******************************************************************************
19  * Definitions
20  *****************************************************************************/
21 
22 /*!
23  * @brief Defines the number of bytes in a cipher block (chunk). This is dictated by
24  * the encryption algorithm.
25  */
26 #define SB3_BYTES_PER_CHUNK 16
27 
28 typedef uint8_t chunk_v3_t[SB3_BYTES_PER_CHUNK];
29 
30 typedef struct _ldr_buf ldr_buf_t;
31 
32 struct _ldr_buf
33 {
34     chunk_v3_t data;
35     uint32_t fillPosition;
36 };
37 
38 /*! @brief Provides forward reference to the loader context definition. */
39 typedef struct _ldr_Context_v3 ldr_Context_v3_t;
40 
41 /*! @brief Function pointer definition for all loader action functions. */
42 typedef status_t (*pLdrFnc_v3_t)(ldr_Context_v3_t *);
43 
44 /*! @brief sb3 section definitions */
45 /*! @brief section type */
46 typedef enum _sectionType
47 {
48     kSectionNone       = 0, /*!< end or invalid */
49     kSectionDataRange  = 1,
50     kSectionDiffUpdate = 2,
51     kSectionDDRConfig  = 3,
52     kSectionRegister   = 4,
53 } section_type_t;
54 
55 #define SB3_DATA_RANGE_HEADER_FLAGS_ERASE_MASK (0x1u) /*!< bit 0 */
56 #define SB3_DATA_RANGE_HEADER_FLAGS_LOAD_MASK  (0x2u) /*!< bit 1 */
57 
58 /*! @brief section data range structure */
59 typedef struct range_header
60 {
61     uint32_t tag;
62     uint32_t startAddress;
63     uint32_t length;
64     uint32_t cmd;
65 } sb3_data_range_header_t;
66 
67 typedef struct range_header_expansion
68 {
69     uint32_t memoryId;
70     uint32_t pad0;
71     uint32_t pad1;
72     uint32_t pad2;
73 } sb3_data_range_expansion_t;
74 
75 typedef struct copy_memory_expansion
76 {
77     uint32_t destAddr;
78     uint32_t memoryIdFrom;
79     uint32_t memoryIdTo;
80     uint32_t pad;
81 } sb3_copy_memory_expansion_t;
82 
83 typedef struct copy
84 {
85     sb3_data_range_header_t header;
86     sb3_copy_memory_expansion_t expansion;
87 } sb3_copy_memory_t;
88 
89 typedef struct load_keyblob
90 {
91     uint32_t tag;
92     uint16_t offset;
93     uint16_t keyWrapId;
94     uint32_t length;
95     uint32_t cmd;
96 } sb3_load_keyblob_t;
97 
98 typedef struct fill_memory_expansion
99 {
100     uint32_t pattern; /*!< word to be used as pattern */
101     uint32_t pad0;
102     uint32_t pad1;
103     uint32_t pad2;
104 } sb3_fill_memory_expansion_t;
105 
106 typedef struct fill_memory
107 {
108     sb3_data_range_header_t header;
109     sb3_fill_memory_expansion_t arg;
110 } sb3_fill_memory_t;
111 
112 typedef struct config_memory
113 {
114     uint32_t tag;
115     uint32_t memoryId;
116     uint32_t address; /*!< address of config blob */
117     uint32_t cmd;
118 } sb3_config_memory_t;
119 
120 enum
121 {
122     kFwVerChk_Id_none      = 0,
123     kFwVerChk_Id_nonsecure = 1,
124     kFwVerChk_Id_secure    = 2,
125 };
126 
127 typedef struct fw_ver_check
128 {
129     uint32_t tag;
130     uint32_t version;
131     uint32_t id;
132     uint32_t cmd;
133 } sb3_fw_ver_check_t;
134 
135 /*! @brief sb3 DATA section header format */
136 typedef struct section_header
137 {
138     uint32_t sectionUid;
139     uint32_t sectionType;
140     uint32_t length;
141     uint32_t _pad;
142 } sb3_section_header_t;
143 
144 /*! @brief loader command enum */
145 typedef enum _loader_command_sb3
146 {
147     kSB3_CmdInvalid         = 0,
148     kSB3_CmdErase           = 1,
149     kSB3_CmdLoad            = 2,
150     kSB3_CmdExecute         = 3,
151     kSB3_CmdCall            = 4,
152     kSB3_CmdProgramFuse     = 5,
153     kSB3_CmdProgramIFR      = 6,
154     kSB3_CmdLoadCmac        = 7,
155     kSB3_CmdCopy            = 8,
156     kSB3_CmdLoadHashLocking = 9,
157     kSB3_CmdLoadKeyBlob     = 10,
158     kSB3_CmdConfigMem       = 11,
159     kSB3_CmdFillMem         = 12,
160     kSB3_CmdFwVerCheck      = 13,
161 } sb3_cmd_t;
162 
163 /*! @brief The all of the allowed command */
164 #define SBLOADER_V3_CMD_SET_ALL                                                                                      \
165     ((1u << kSB3_CmdErase) | (1u << kSB3_CmdLoad) | (1u << kSB3_CmdExecute) | (1u << kSB3_CmdCall) |                 \
166      (1u << kSB3_CmdProgramFuse) | (1u << kSB3_CmdProgramIFR) | (1u << kSB3_CmdCopy) | (1u << kSB3_CmdLoadKeyBlob) | \
167      (1u << kSB3_CmdConfigMem) | (1u << kSB3_CmdFillMem) | (1u << kSB3_CmdFwVerCheck))
168 /*! @brief The allowed command set in ISP mode */
169 #define SBLOADER_V3_CMD_SET_IN_ISP_MODE                                                                            \
170     ((1u << kSB3_CmdErase) | (1u << kSB3_CmdLoad) | (1u << kSB3_CmdExecute) | (1u << kSB3_CmdProgramFuse) |        \
171      (1u << kSB3_CmdProgramIFR) | (1u << kSB3_CmdCopy) | (1u << kSB3_CmdLoadKeyBlob) | (1u << kSB3_CmdConfigMem) | \
172      (1u << kSB3_CmdFillMem) | (1u << kSB3_CmdFwVerCheck))
173 /*! @brief The allowed command set in recovery mode */
174 #define SBLOADER_V3_CMD_SET_IN_REC_MODE                                                                            \
175     ((1u << kSB3_CmdErase) | (1u << kSB3_CmdLoad) | (1u << kSB3_CmdExecute) | (1u << kSB3_CmdProgramFuse) |        \
176      (1u << kSB3_CmdProgramIFR) | (1u << kSB3_CmdCopy) | (1u << kSB3_CmdLoadKeyBlob) | (1u << kSB3_CmdConfigMem) | \
177      (1u << kSB3_CmdFillMem) | (1u << kSB3_CmdFwVerCheck))
178 
179 #define SB3_DATA_BUFFER_SIZE_IN_BYTE (MAX(128, NBOOT_KEY_BLOB_SIZE_IN_BYTE_MAX))
180 
181 /*! @brief Memory region definition. */
182 typedef struct
183 {
184     uint32_t address;
185     uint32_t length;
186 } kb_region_t;
187 
188 /*!
189  * @brief Details of the operation to be performed by the ROM.
190  *
191  * The #kRomAuthenticateImage operation requires the entire signed image to be
192  * available to the application.
193  */
194 typedef enum
195 {
196     kRomAuthenticateImage = 1, /*!< Authenticate a signed image. */
197     kRomLoadImage         = 2, /*!< Load SB file. */
198     kRomOperationCount    = 3,
199 } kb_operation_t;
200 
201 typedef struct
202 {
203     uint32_t profile;
204     uint32_t minBuildNumber;
205     uint32_t overrideSBBootSectionID;
206     uint32_t *userSBKEK;
207     uint32_t regionCount;
208     const kb_region_t *regions;
209 } kb_load_sb_t;
210 
211 typedef struct
212 {
213     uint32_t profile;
214     uint32_t minBuildNumber;
215     uint32_t maxImageLength;
216     uint32_t *userRHK;
217 } kb_authenticate_t;
218 
219 typedef struct
220 {
221     uint32_t version; /*!< Should be set to #kKbootApiVersion. */
222     uint8_t *buffer;  /*!< Caller-provided buffer used by Kboot. */
223     uint32_t bufferLength;
224     kb_operation_t op;
225     union
226     {
227         kb_authenticate_t authenticate; /*!< Settings for #kKbootAuthenticate operation.*/
228         kb_load_sb_t loadSB;            /*!< Settings for #kKbootLoadSB operation.*/
229     };
230 } kb_options_t;
231 
232 /*! @brief Loader context definition. */
233 struct _ldr_Context_v3
234 {
235     pLdrFnc_v3_t Action;        /*!< pointer to loader action function */
236     uint32_t block_size;        /*!< size of each block in bytes */
237     uint32_t block_data_size;   /*!< data size in bytes (NBOOT_SB3_CHUNK_SIZE_IN_BYTES) */
238     uint32_t block_data_total;  /*!< data max size in bytes (block_size * data_size */
239     uint32_t block_buffer_size; /*!< block0 and block size */
240     uint32_t block_buffer_position;
241     uint8_t block_buffer[MAX(NBOOT_SB3_MANIFEST_MAX_SIZE_IN_BYTES,
242                              NBOOT_SB3_BLOCK_MAX_SIZE_IN_BYTES)]; /*! will be used for both block0 and blockx */
243     uint32_t processedBlocks;
244 
245     uint8_t data_block_offset; /*! data block offset in a block. */
246     bool in_data_block;        /*!< in progress of handling a data block within a block */
247     uint8_t *data_block;
248     uint32_t data_block_position;
249 
250     bool in_data_section; /*!< in progress of handling a data section within a data block */
251     uint32_t data_section_handled;
252     sb3_section_header_t data_section_header;
253 
254     bool in_data_range; /*!< in progress of handling a data range within a data section */
255     uint32_t data_range_handled;
256     uint32_t data_range_gap;
257     sb3_data_range_header_t data_range_header;
258     bool has_data_range_expansion;
259     sb3_data_range_expansion_t data_range_expansion;
260 
261     uint32_t commandSet; /*!< support command set during sb file handling */
262 
263     uint32_t data_position;
264     uint8_t data_buffer[SB3_DATA_BUFFER_SIZE_IN_BYTE]; /*!< temporary data buffer */
265 
266     kb_options_t fromAPI; /*!< options from ROM API */
267 };
268 
269 /*! @} */
270 
271 #endif /* _FSL_SBLOADER_V3_H_ */
272