1 /*
2 * Copyright 2018-2021 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 */
8
9 #include "fsl_iap.h"
10 #include "fsl_iap_ffr.h"
11 #include "fsl_iap_kbp.h"
12 #include "fsl_iap_skboot_authenticate.h"
13 #include "fsl_device_registers.h"
14 /*******************************************************************************
15 * Definitions
16 ******************************************************************************/
17 /* Component ID definition, used by tools. */
18 #ifndef FSL_COMPONENT_ID
19 #define FSL_COMPONENT_ID "platform.drivers.iap1"
20 #endif
21
22 #if (defined(LPC5512_SERIES) || defined(LPC5514_SERIES) || defined(LPC55S14_SERIES) || defined(LPC5516_SERIES) || \
23 defined(LPC55S16_SERIES) || defined(LPC5524_SERIES) || defined(LPC5502_SERIES) || defined(LPC5504_SERIES) || \
24 defined(LPC5506_SERIES) || defined(LPC55S04_SERIES) || defined(LPC55S06_SERIES))
25
26 #define BOOTLOADER_API_TREE_POINTER ((bootloader_tree_t *)0x1301fe00U)
27
28 #elif (defined(LPC55S69_cm33_core0_SERIES) || defined(LPC55S69_cm33_core1_SERIES) || defined(LPC5526_SERIES) || \
29 defined(LPC55S26_SERIES) || defined(LPC5528_SERIES) || defined(LPC55S28_SERIES) || \
30 defined(LPC55S66_cm33_core0_SERIES) || defined(LPC55S66_cm33_core1_SERIES))
31
32 #define BOOTLOADER_API_TREE_POINTER ((bootloader_tree_t *)0x130010f0U)
33
34 #else
35 #error "No valid CPU defined!"
36
37 #endif
38
39 /*******************************************************************************
40 * Prototypes
41 ******************************************************************************/
42 static status_t get_cfpa_higher_version(flash_config_t *config);
43
44 /*!
45 * @name flash and ffr Structure
46 * @{
47 */
48
49 typedef union functionCommandOption
50 {
51 uint32_t commandAddr;
52 status_t (*eraseCommand)(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key);
53 status_t (*programCommand)(flash_config_t *config, uint32_t start, const uint8_t *src, uint32_t lengthInBytes);
54 status_t (*verifyProgramCommand)(flash_config_t *config,
55 uint32_t start,
56 uint32_t lengthInBytes,
57 const uint8_t *expectedData,
58 uint32_t *failedAddress,
59 uint32_t *failedData);
60 status_t (*flashReadCommand)(flash_config_t *config, uint32_t start, uint8_t *dest, uint32_t lengthInBytes);
61 } function_command_option_t;
62
63 /*
64 *!@brief Structure of version property.
65 *
66 *!@ingroup bl_core
67 */
68 typedef union StandardVersion
69 {
70 struct
71 {
72 uint32_t bugfix : 8; /*!< bugfix version [7:0] */
73 uint32_t minor : 8; /*!< minor version [15:8] */
74 uint32_t major : 8; /*!< major version [23:16] */
75 uint32_t name : 8; /*!< name [31:24] */
76 };
77 uint32_t version; /*!< combined version numbers. */
78 #if defined(__cplusplus)
StandardVersion()79 StandardVersion() : version(0)
80 {
81 }
StandardVersion(uint32_t version)82 StandardVersion(uint32_t version) : version(version)
83 {
84 }
85 #endif
86 } standard_version_t;
87
88 /*! @brief Interface for the flash driver.*/
89 typedef struct version1FlashDriverInterface
90 {
91 standard_version_t version; /*!< flash driver API version number.*/
92
93 /*!< Flash driver.*/
94 status_t (*flash_init)(flash_config_t *config);
95 status_t (*flash_erase)(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key);
96 status_t (*flash_program)(flash_config_t *config, uint32_t start, const uint8_t *src, uint32_t lengthInBytes);
97 status_t (*flash_verify_erase)(flash_config_t *config, uint32_t start, uint32_t lengthInBytes);
98 status_t (*flash_verify_program)(flash_config_t *config,
99 uint32_t start,
100 uint32_t lengthInBytes,
101 const uint8_t *expectedData,
102 uint32_t *failedAddress,
103 uint32_t *failedData);
104 status_t (*flash_get_property)(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value);
105 uint32_t reserved[3]; /*! Reserved for future use */
106 /*!< Flash FFR driver*/
107 status_t (*ffr_init)(flash_config_t *config);
108 status_t (*ffr_lock_all)(flash_config_t *config);
109 status_t (*ffr_cust_factory_page_write)(flash_config_t *config, uint8_t *page_data, bool seal_part);
110 status_t (*ffr_get_uuid)(flash_config_t *config, uint8_t *uuid);
111 status_t (*ffr_get_customer_data)(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len);
112 status_t (*ffr_keystore_write)(flash_config_t *config, ffr_key_store_t *pKeyStore);
113 status_t (*ffr_keystore_get_ac)(flash_config_t *config, uint8_t *pActivationCode);
114 status_t (*ffr_keystore_get_kc)(flash_config_t *config, uint8_t *pKeyCode, ffr_key_type_t keyIndex);
115 status_t (*ffr_infield_page_write)(flash_config_t *config, uint8_t *page_data, uint32_t valid_len);
116 status_t (*ffr_get_customer_infield_data)(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len);
117 } version1_flash_driver_interface_t;
118
119 /*! @brief Interface for the flash driver.*/
120 typedef struct version0FlashDriverInterface
121 {
122 standard_version_t version; /*!< flash driver API version number.*/
123
124 /*!< Flash driver.*/
125 status_t (*flash_init)(flash_config_t *config);
126 status_t (*flash_erase)(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key);
127 status_t (*flash_program)(flash_config_t *config, uint32_t start, const uint8_t *src, uint32_t lengthInBytes);
128 status_t (*flash_verify_erase)(flash_config_t *config, uint32_t start, uint32_t lengthInBytes);
129 status_t (*flash_verify_program)(flash_config_t *config,
130 uint32_t start,
131 uint32_t lengthInBytes,
132 const uint8_t *expectedData,
133 uint32_t *failedAddress,
134 uint32_t *failedData);
135 status_t (*flash_get_property)(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value);
136
137 /*!< Flash FFR driver*/
138 status_t (*ffr_init)(flash_config_t *config);
139 status_t (*ffr_lock_all)(flash_config_t *config);
140 status_t (*ffr_cust_factory_page_write)(flash_config_t *config, uint8_t *page_data, bool seal_part);
141 status_t (*ffr_get_uuid)(flash_config_t *config, uint8_t *uuid);
142 status_t (*ffr_get_customer_data)(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len);
143 status_t (*ffr_keystore_write)(flash_config_t *config, ffr_key_store_t *pKeyStore);
144 status_t (*ffr_keystore_get_ac)(flash_config_t *config, uint8_t *pActivationCode);
145 status_t (*ffr_keystore_get_kc)(flash_config_t *config, uint8_t *pKeyCode, ffr_key_type_t keyIndex);
146 status_t (*ffr_infield_page_write)(flash_config_t *config, uint8_t *page_data, uint32_t valid_len);
147 status_t (*ffr_get_customer_infield_data)(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len);
148 } version0_flash_driver_interface_t;
149
150 typedef union flashDriverInterface
151 {
152 const version1_flash_driver_interface_t *version1FlashDriver;
153 const version0_flash_driver_interface_t *version0FlashDriver;
154 } flash_driver_interface_t;
155
156 /*! @}*/
157
158 /*!
159 * @name Bootloader API and image authentication Structure
160 * @{
161 */
162
163 /*! @brief Interface for Bootloader API functions. */
164 typedef struct _kb_interface
165 {
166 /*!< Initialize the API. */
167 status_t (*kb_init_function)(kb_session_ref_t **session, const kb_options_t *options);
168 status_t (*kb_deinit_function)(kb_session_ref_t *session);
169 status_t (*kb_execute_function)(kb_session_ref_t *session, const uint8_t *data, uint32_t dataLength);
170 } kb_interface_t;
171
172 //! @brief Interface for image authentication API
173 typedef struct _skboot_authenticate_interface
174 {
175 skboot_status_t (*skboot_authenticate_function)(const uint8_t *imageStartAddr, secure_bool_t *isSignVerified);
176 void (*skboot_hashcrypt_irq_handler)(void);
177 } skboot_authenticate_interface_t;
178 /*! @}*/
179
180 /*!
181 * @brief Root of the bootloader API tree.
182 *
183 * An instance of this struct resides in read-only memory in the bootloader. It
184 * provides a user application access to APIs exported by the bootloader.
185 *
186 * @note The order of existing fields must not be changed.
187 */
188 typedef struct BootloaderTree
189 {
190 void (*runBootloader)(void *arg); /*!< Function to start the bootloader executing. */
191 standard_version_t bootloader_version; /*!< Bootloader version number. */
192 const char *copyright; /*!< Copyright string. */
193 const uint32_t reserved0; /*!< Do NOT use. */
194 flash_driver_interface_t flashDriver;
195 const kb_interface_t *kbApi; /*!< Bootloader API. */
196 const uint32_t reserved1[4]; /*!< Do NOT use. */
197 const skboot_authenticate_interface_t *skbootAuthenticate; /*!< Image authentication API. */
198 } bootloader_tree_t;
199
200 /*******************************************************************************
201 * Prototype
202 ******************************************************************************/
203 static uint32_t get_rom_api_version(void);
204
205 /*******************************************************************************
206 * Variables
207 ******************************************************************************/
208
209 /*! Get pointer to flash driver API table in ROM. */
210 #define VERSION1_FLASH_API_TREE BOOTLOADER_API_TREE_POINTER->flashDriver.version1FlashDriver
211 #define VERSION0_FLASH_API_TREE BOOTLOADER_API_TREE_POINTER->flashDriver.version0FlashDriver
212 #define LPC55S69_REV0_FLASH_READ_ADDR (0x130043a3U)
213 #define LPC55S69_REV1_FLASH_READ_ADDR (0x13007539U)
214 #define LPC55S16_REV0_FLASH_READ_ADDR (0x1300ade5U)
215
216 /*******************************************************************************
217 * Code
218 ******************************************************************************/
219
get_rom_api_version(void)220 static uint32_t get_rom_api_version(void)
221 {
222 if (BOOTLOADER_API_TREE_POINTER->bootloader_version.major == 3u)
223 {
224 return 1u;
225 }
226 else
227 {
228 return 0u;
229 }
230 }
231
232 /*!
233 * @brief Initializes the global flash properties structure members.
234 *
235 * This function checks and initializes the Flash module for the other Flash APIs.
236 */
FLASH_Init(flash_config_t * config)237 status_t FLASH_Init(flash_config_t *config)
238 {
239 status_t status;
240 /* Initialize the clock to 96MHz */
241 config->modeConfig.sysFreqInMHz = (uint32_t)kSysToFlashFreq_defaultInMHz;
242 if (get_rom_api_version() == 1u)
243 {
244 status = VERSION1_FLASH_API_TREE->flash_init(config);
245 }
246 else
247 {
248 status = VERSION0_FLASH_API_TREE->flash_init(config);
249 }
250
251 if (config->PFlashTotalSize == 0xA0000U)
252 {
253 config->PFlashTotalSize -= 17U * config->PFlashPageSize;
254 }
255
256 return status;
257 }
258
259 /*!
260 * @brief Erases the flash sectors encompassed by parameters passed into function.
261 *
262 * This function erases the appropriate number of flash sectors based on the
263 * desired start address and length.
264 */
FLASH_Erase(flash_config_t * config,uint32_t start,uint32_t lengthInBytes,uint32_t key)265 status_t FLASH_Erase(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
266 {
267 uint32_t core_frequency = CLOCK_GetFreq(kCLOCK_CoreSysClk);
268
269 /* Flash ERASE operations must be performed with a system clock below or equal to 100 MHz.*/
270 if (core_frequency > 100000000U)
271 {
272 return (status_t)kStatus_FLASH_EraseFrequencyError;
273 }
274
275 if (get_rom_api_version() == 0u)
276 {
277 function_command_option_t runCmdFuncOption;
278 runCmdFuncOption.commandAddr = 0x1300413bU; /*!< get the flash erase api location adress in rom */
279 return runCmdFuncOption.eraseCommand(config, start, lengthInBytes, key);
280 }
281 else
282 {
283 return VERSION1_FLASH_API_TREE->flash_erase(config, start, lengthInBytes, key);
284 }
285 }
286
287 /*! See fsl_iap.h for documentation of this function. */
FLASH_Program(flash_config_t * config,uint32_t start,const uint8_t * src,uint32_t lengthInBytes)288 status_t FLASH_Program(flash_config_t *config, uint32_t start, const uint8_t *src, uint32_t lengthInBytes)
289 {
290 uint32_t core_frequency = CLOCK_GetFreq(kCLOCK_CoreSysClk);
291
292 /* Flash PROGRAM operations must be performed with a system clock below or equal to 100 MHz.*/
293 if (core_frequency > 100000000U)
294 {
295 return (status_t)kStatus_FLASH_ProgramFrequencyError;
296 }
297
298 if (get_rom_api_version() == 0u)
299 {
300 function_command_option_t runCmdFuncOption;
301 runCmdFuncOption.commandAddr = 0x1300419dU; /*!< get the flash program api location adress in rom*/
302 return runCmdFuncOption.programCommand(config, start, src, lengthInBytes);
303 }
304 else
305 {
306 assert(VERSION1_FLASH_API_TREE);
307 return VERSION1_FLASH_API_TREE->flash_program(config, start, src, lengthInBytes);
308 }
309 }
310
311 /*! See fsl_iap.h for documentation of this function. */
FLASH_Read(flash_config_t * config,uint32_t start,uint8_t * dest,uint32_t lengthInBytes)312 status_t FLASH_Read(flash_config_t *config, uint32_t start, uint8_t *dest, uint32_t lengthInBytes)
313 {
314 if (get_rom_api_version() == 0u)
315 {
316 /*!< get the flash read api location adress in rom*/
317 function_command_option_t runCmdFuncOption;
318 runCmdFuncOption.commandAddr = LPC55S69_REV0_FLASH_READ_ADDR;
319 return runCmdFuncOption.flashReadCommand(config, start, dest, lengthInBytes);
320 }
321 else
322 {
323 /*!< get the flash read api location adress in rom*/
324 function_command_option_t runCmdFuncOption;
325 if ((SYSCON->DIEID & SYSCON_DIEID_REV_ID_MASK) != 0u)
326 {
327 runCmdFuncOption.commandAddr = LPC55S69_REV1_FLASH_READ_ADDR;
328 }
329 else
330 {
331 runCmdFuncOption.commandAddr = LPC55S16_REV0_FLASH_READ_ADDR;
332 }
333 return runCmdFuncOption.flashReadCommand(config, start, dest, lengthInBytes);
334 }
335 }
336
337 /*! See fsl_iap.h for documentation of this function. */
FLASH_VerifyErase(flash_config_t * config,uint32_t start,uint32_t lengthInBytes)338 status_t FLASH_VerifyErase(flash_config_t *config, uint32_t start, uint32_t lengthInBytes)
339 {
340 assert(VERSION1_FLASH_API_TREE);
341 return VERSION1_FLASH_API_TREE->flash_verify_erase(config, start, lengthInBytes);
342 }
343
344 /*!
345 * @brief Verifies programming of the desired flash area at a specified margin level.
346 *
347 * This function verifies the data programed in the flash memory using the
348 * Flash Program Check Command and compares it to the expected data for a given
349 * flash area as determined by the start address and length.
350 */
FLASH_VerifyProgram(flash_config_t * config,uint32_t start,uint32_t lengthInBytes,const uint8_t * expectedData,uint32_t * failedAddress,uint32_t * failedData)351 status_t FLASH_VerifyProgram(flash_config_t *config,
352 uint32_t start,
353 uint32_t lengthInBytes,
354 const uint8_t *expectedData,
355 uint32_t *failedAddress,
356 uint32_t *failedData)
357 {
358 if (get_rom_api_version() == 0u)
359 {
360 function_command_option_t runCmdFuncOption;
361 runCmdFuncOption.commandAddr = 0x1300427dU; /*!< get the flash verify program api location adress in rom*/
362 return runCmdFuncOption.verifyProgramCommand(config, start, lengthInBytes, expectedData, failedAddress,
363 failedData);
364 }
365 else
366 {
367 assert(VERSION1_FLASH_API_TREE);
368 return VERSION1_FLASH_API_TREE->flash_verify_program(config, start, lengthInBytes, expectedData, failedAddress,
369 failedData);
370 }
371 }
372
373 /*!
374 * @brief Returns the desired flash property.
375 */
FLASH_GetProperty(flash_config_t * config,flash_property_tag_t whichProperty,uint32_t * value)376 status_t FLASH_GetProperty(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value)
377 {
378 assert(VERSION1_FLASH_API_TREE);
379 return VERSION1_FLASH_API_TREE->flash_get_property(config, whichProperty, value);
380 }
381 /********************************************************************************
382 * fsl iap ffr CODE
383 *******************************************************************************/
384
get_cfpa_higher_version(flash_config_t * config)385 static status_t get_cfpa_higher_version(flash_config_t *config)
386 {
387 uint32_t pageData[FLASH_FFR_MAX_PAGE_SIZE / sizeof(uint32_t)];
388 uint32_t versionPing = 0U;
389 uint32_t versionPong = 0U;
390
391 /* Get the CFPA ping page data and the corresponding version */
392 config->ffrConfig.cfpaPageOffset = 1U;
393 status_t status = FFR_GetCustomerInfieldData(config, (uint8_t *)pageData, 0U, FLASH_FFR_MAX_PAGE_SIZE);
394 if (status != (int32_t)kStatus_FLASH_Success)
395 {
396 return status;
397 }
398 versionPing = pageData[1];
399
400 /* Get the CFPA pong page data and the corresponding version */
401 config->ffrConfig.cfpaPageOffset = 2U;
402 status = FFR_GetCustomerInfieldData(config, (uint8_t *)pageData, 0U, FLASH_FFR_MAX_PAGE_SIZE);
403 if (status != (int32_t)kStatus_FLASH_Success)
404 {
405 return status;
406 }
407 versionPong = pageData[1];
408
409 /* Compare the CFPA ping version and pong version and set it correctly in flash_config structure */
410 if (versionPing > versionPong)
411 {
412 config->ffrConfig.cfpaPageVersion = versionPing;
413 config->ffrConfig.cfpaPageOffset = 1U;
414 }
415 else
416 {
417 config->ffrConfig.cfpaPageVersion = versionPong;
418 config->ffrConfig.cfpaPageOffset = 2U;
419 }
420 return (int32_t)kStatus_FLASH_Success;
421 }
422
423 /*!
424 * Initializes the global FFR properties structure members.
425 */
FFR_Init(flash_config_t * config)426 status_t FFR_Init(flash_config_t *config)
427 {
428 status_t status;
429 if (get_rom_api_version() == 0u)
430 {
431 assert(VERSION0_FLASH_API_TREE);
432 status = VERSION0_FLASH_API_TREE->ffr_init(config);
433 if (status != (status_t)kStatus_FLASH_Success)
434 {
435 return status;
436 }
437 return get_cfpa_higher_version(config);
438 }
439 else
440 {
441 assert(VERSION1_FLASH_API_TREE);
442 status = VERSION1_FLASH_API_TREE->ffr_init(config);
443 if (status != (status_t)kStatus_FLASH_Success)
444 {
445 return status;
446 }
447 return get_cfpa_higher_version(config);
448 }
449 }
450
451 /*!
452 * Enable firewall for all flash banks.
453 */
FFR_Lock_All(flash_config_t * config)454 status_t FFR_Lock_All(flash_config_t *config)
455 {
456 if (get_rom_api_version() == 0u)
457 {
458 assert(VERSION0_FLASH_API_TREE);
459 return VERSION0_FLASH_API_TREE->ffr_lock_all(config);
460 }
461 else
462 {
463 assert(VERSION1_FLASH_API_TREE);
464 return VERSION1_FLASH_API_TREE->ffr_lock_all(config);
465 }
466 }
467
468 /*!
469 * APIs to access CMPA pages;
470 * This routine will erase "customer factory page" and program the page with passed data.
471 */
FFR_CustFactoryPageWrite(flash_config_t * config,uint8_t * page_data,bool seal_part)472 status_t FFR_CustFactoryPageWrite(flash_config_t *config, uint8_t *page_data, bool seal_part)
473 {
474 if (get_rom_api_version() == 0u)
475 {
476 assert(VERSION0_FLASH_API_TREE);
477 return VERSION0_FLASH_API_TREE->ffr_cust_factory_page_write(config, page_data, seal_part);
478 }
479 else
480 {
481 assert(VERSION1_FLASH_API_TREE);
482 return VERSION1_FLASH_API_TREE->ffr_cust_factory_page_write(config, page_data, seal_part);
483 }
484 }
485
486 /*!
487 * See fsl_iap_ffr.h for documentation of this function.
488 */
FFR_GetUUID(flash_config_t * config,uint8_t * uuid)489 status_t FFR_GetUUID(flash_config_t *config, uint8_t *uuid)
490 {
491 if (get_rom_api_version() == 0u)
492 {
493 assert(VERSION0_FLASH_API_TREE);
494 return VERSION0_FLASH_API_TREE->ffr_get_uuid(config, uuid);
495 }
496 else
497 {
498 assert(VERSION1_FLASH_API_TREE);
499 return VERSION1_FLASH_API_TREE->ffr_get_uuid(config, uuid);
500 }
501 }
502
503 /*!
504 * APIs to access CMPA pages
505 * Read data stored in 'Customer Factory CFG Page'.
506 */
FFR_GetCustomerData(flash_config_t * config,uint8_t * pData,uint32_t offset,uint32_t len)507 status_t FFR_GetCustomerData(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len)
508 {
509 if (get_rom_api_version() == 0u)
510 {
511 assert(VERSION0_FLASH_API_TREE);
512 return VERSION0_FLASH_API_TREE->ffr_get_customer_data(config, pData, offset, len);
513 }
514 else
515 {
516 assert(VERSION1_FLASH_API_TREE);
517 return VERSION1_FLASH_API_TREE->ffr_get_customer_data(config, pData, offset, len);
518 }
519 }
520
521 /*!
522 * This routine writes the 3 pages allocated for Key store data,
523 * Used during manufacturing. Should write pages when 'customer factory page' is not in sealed state.
524 */
FFR_KeystoreWrite(flash_config_t * config,ffr_key_store_t * pKeyStore)525 status_t FFR_KeystoreWrite(flash_config_t *config, ffr_key_store_t *pKeyStore)
526 {
527 if (get_rom_api_version() == 0u)
528 {
529 assert(VERSION0_FLASH_API_TREE);
530 return VERSION0_FLASH_API_TREE->ffr_keystore_write(config, pKeyStore);
531 }
532 else
533 {
534 assert(VERSION1_FLASH_API_TREE);
535 return VERSION1_FLASH_API_TREE->ffr_keystore_write(config, pKeyStore);
536 }
537 }
538
539 /*! See fsl_iap_ffr.h for documentation of this function. */
FFR_KeystoreGetAC(flash_config_t * config,uint8_t * pActivationCode)540 status_t FFR_KeystoreGetAC(flash_config_t *config, uint8_t *pActivationCode)
541 {
542 if (get_rom_api_version() == 0u)
543 {
544 assert(VERSION0_FLASH_API_TREE);
545 return VERSION0_FLASH_API_TREE->ffr_keystore_get_ac(config, pActivationCode);
546 }
547 else
548 {
549 assert(VERSION1_FLASH_API_TREE);
550 return VERSION1_FLASH_API_TREE->ffr_keystore_get_ac(config, pActivationCode);
551 }
552 }
553
554 /*! See fsl_iap_ffr.h for documentation of this function. */
FFR_KeystoreGetKC(flash_config_t * config,uint8_t * pKeyCode,ffr_key_type_t keyIndex)555 status_t FFR_KeystoreGetKC(flash_config_t *config, uint8_t *pKeyCode, ffr_key_type_t keyIndex)
556 {
557 if (get_rom_api_version() == 0u)
558 {
559 assert(VERSION0_FLASH_API_TREE);
560 return VERSION0_FLASH_API_TREE->ffr_keystore_get_kc(config, pKeyCode, keyIndex);
561 }
562 else
563 {
564 assert(VERSION1_FLASH_API_TREE);
565 return VERSION1_FLASH_API_TREE->ffr_keystore_get_kc(config, pKeyCode, keyIndex);
566 }
567 }
568
569 /*!
570 * APIs to access CFPA pages
571 * This routine will erase CFPA and program the CFPA page with passed data.
572 */
FFR_InfieldPageWrite(flash_config_t * config,uint8_t * page_data,uint32_t valid_len)573 status_t FFR_InfieldPageWrite(flash_config_t *config, uint8_t *page_data, uint32_t valid_len)
574 {
575 if (get_rom_api_version() == 0u)
576 {
577 assert(VERSION0_FLASH_API_TREE);
578 return VERSION0_FLASH_API_TREE->ffr_infield_page_write(config, page_data, valid_len);
579 }
580 else
581 {
582 assert(VERSION1_FLASH_API_TREE);
583 return VERSION1_FLASH_API_TREE->ffr_infield_page_write(config, page_data, valid_len);
584 }
585 }
586
587 /*!
588 * APIs to access CFPA pages
589 * Generic read function, used by customer to read data stored in 'Customer In-field Page'.
590 */
FFR_GetCustomerInfieldData(flash_config_t * config,uint8_t * pData,uint32_t offset,uint32_t len)591 status_t FFR_GetCustomerInfieldData(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len)
592 {
593 if (get_rom_api_version() == 0u)
594 {
595 assert(VERSION0_FLASH_API_TREE);
596 return VERSION0_FLASH_API_TREE->ffr_get_customer_infield_data(config, pData, offset, len);
597 }
598 else
599 {
600 assert(VERSION1_FLASH_API_TREE);
601 return VERSION1_FLASH_API_TREE->ffr_get_customer_infield_data(config, pData, offset, len);
602 }
603 }
604
605 /********************************************************************************
606 * Bootloader API
607 *******************************************************************************/
608 /*!
609 * @brief Initialize ROM API for a given operation.
610 *
611 * Inits the ROM API based on the options provided by the application in the second
612 * argument. Every call to rom_init() should be paired with a call to rom_deinit().
613 */
kb_init(kb_session_ref_t ** session,const kb_options_t * options)614 status_t kb_init(kb_session_ref_t **session, const kb_options_t *options)
615 {
616 assert(BOOTLOADER_API_TREE_POINTER);
617 return BOOTLOADER_API_TREE_POINTER->kbApi->kb_init_function(session, options);
618 }
619
620 /*!
621 * @brief Cleans up the ROM API context.
622 *
623 * After this call, the @a context parameter can be reused for another operation
624 * by calling rom_init() again.
625 */
kb_deinit(kb_session_ref_t * session)626 status_t kb_deinit(kb_session_ref_t *session)
627 {
628 assert(BOOTLOADER_API_TREE_POINTER);
629 return BOOTLOADER_API_TREE_POINTER->kbApi->kb_deinit_function(session);
630 }
631
632 /*!
633 * Perform the operation configured during init.
634 *
635 * This application must call this API repeatedly, passing in sequential chunks of
636 * data from the boot image (SB file) that is to be processed. The ROM will perform
637 * the selected operation on this data and return. The application may call this
638 * function with as much or as little data as it wishes, which can be used to select
639 * the granularity of time given to the application in between executing the operation.
640 *
641 * @param context Current ROM context pointer.
642 * @param data Buffer of boot image data provided to the ROM by the application.
643 * @param dataLength Length in bytes of the data in the buffer provided to the ROM.
644 *
645 * @retval #kStatus_Success The operation has completed successfully.
646 * @retval #kStatus_Fail An error occurred while executing the operation.
647 * @retval #kStatus_RomApiNeedMoreData No error occurred, but the ROM needs more data to
648 * continue processing the boot image.
649 */
kb_execute(kb_session_ref_t * session,const uint8_t * data,uint32_t dataLength)650 status_t kb_execute(kb_session_ref_t *session, const uint8_t *data, uint32_t dataLength)
651 {
652 assert(BOOTLOADER_API_TREE_POINTER);
653 return BOOTLOADER_API_TREE_POINTER->kbApi->kb_execute_function(session, data, dataLength);
654 }
655
656 /********************************************************************************
657 * Image authentication API
658 *******************************************************************************/
659
660 /*!
661 * @brief Authenticate entry function with ARENA allocator init
662 *
663 * This is called by ROM boot or by ROM API g_skbootAuthenticateInterface
664 */
skboot_authenticate(const uint8_t * imageStartAddr,secure_bool_t * isSignVerified)665 skboot_status_t skboot_authenticate(const uint8_t *imageStartAddr, secure_bool_t *isSignVerified)
666 {
667 assert(BOOTLOADER_API_TREE_POINTER);
668 return BOOTLOADER_API_TREE_POINTER->skbootAuthenticate->skboot_authenticate_function(imageStartAddr,
669 isSignVerified);
670 }
671
672 /*!
673 * @brief Interface for image authentication API
674 */
HASH_IRQHandler(void)675 void HASH_IRQHandler(void)
676 {
677 assert(BOOTLOADER_API_TREE_POINTER);
678 BOOTLOADER_API_TREE_POINTER->skbootAuthenticate->skboot_hashcrypt_irq_handler();
679 }
680
681 /********************************************************************************
682 * runBootloader API
683 *******************************************************************************/
BOOTLOADER_UserEntry(void * arg)684 void BOOTLOADER_UserEntry(void *arg)
685 {
686 assert(BOOTLOADER_API_TREE_POINTER);
687 BOOTLOADER_API_TREE_POINTER->runBootloader(arg);
688 }
689 /********************************************************************************
690 * EOF
691 *******************************************************************************/
692