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, 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, 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, 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     if (get_rom_api_version() == 0u)
268     {
269         function_command_option_t runCmdFuncOption;
270         runCmdFuncOption.commandAddr = 0x1300413bU; /*!< get the flash erase api location adress in rom */
271         return runCmdFuncOption.eraseCommand(config, start, lengthInBytes, key);
272     }
273     else
274     {
275         return VERSION1_FLASH_API_TREE->flash_erase(config, start, lengthInBytes, key);
276     }
277 }
278 
279 /*! See fsl_iap.h for documentation of this function. */
FLASH_Program(flash_config_t * config,uint32_t start,uint8_t * src,uint32_t lengthInBytes)280 status_t FLASH_Program(flash_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
281 {
282     if (get_rom_api_version() == 0u)
283     {
284         function_command_option_t runCmdFuncOption;
285         runCmdFuncOption.commandAddr = 0x1300419dU; /*!< get the flash program api location adress in rom*/
286         return runCmdFuncOption.programCommand(config, start, src, lengthInBytes);
287     }
288     else
289     {
290         assert(VERSION1_FLASH_API_TREE);
291         return VERSION1_FLASH_API_TREE->flash_program(config, start, src, lengthInBytes);
292     }
293 }
294 
295 /*! See fsl_iap.h for documentation of this function. */
FLASH_Read(flash_config_t * config,uint32_t start,uint8_t * dest,uint32_t lengthInBytes)296 status_t FLASH_Read(flash_config_t *config, uint32_t start, uint8_t *dest, uint32_t lengthInBytes)
297 {
298     if (get_rom_api_version() == 0u)
299     {
300         /*!< get the flash read api location adress in rom*/
301         function_command_option_t runCmdFuncOption;
302         runCmdFuncOption.commandAddr = LPC55S69_REV0_FLASH_READ_ADDR;
303         return runCmdFuncOption.flashReadCommand(config, start, dest, lengthInBytes);
304     }
305     else
306     {
307         /*!< get the flash read api location adress in rom*/
308         function_command_option_t runCmdFuncOption;
309         if ((SYSCON->DIEID & SYSCON_DIEID_REV_ID_MASK) != 0u)
310         {
311             runCmdFuncOption.commandAddr = LPC55S69_REV1_FLASH_READ_ADDR;
312         }
313         else
314         {
315             runCmdFuncOption.commandAddr = LPC55S16_REV0_FLASH_READ_ADDR;
316         }
317         return runCmdFuncOption.flashReadCommand(config, start, dest, lengthInBytes);
318     }
319 }
320 
321 /*! See fsl_iap.h for documentation of this function. */
FLASH_VerifyErase(flash_config_t * config,uint32_t start,uint32_t lengthInBytes)322 status_t FLASH_VerifyErase(flash_config_t *config, uint32_t start, uint32_t lengthInBytes)
323 {
324     assert(VERSION1_FLASH_API_TREE);
325     return VERSION1_FLASH_API_TREE->flash_verify_erase(config, start, lengthInBytes);
326 }
327 
328 /*!
329  * @brief Verifies programming of the desired flash area at a specified margin level.
330  *
331  * This function verifies the data programed in the flash memory using the
332  * Flash Program Check Command and compares it to the expected data for a given
333  * flash area as determined by the start address and length.
334  */
FLASH_VerifyProgram(flash_config_t * config,uint32_t start,uint32_t lengthInBytes,const uint8_t * expectedData,uint32_t * failedAddress,uint32_t * failedData)335 status_t FLASH_VerifyProgram(flash_config_t *config,
336                              uint32_t start,
337                              uint32_t lengthInBytes,
338                              const uint8_t *expectedData,
339                              uint32_t *failedAddress,
340                              uint32_t *failedData)
341 {
342     if (get_rom_api_version() == 0u)
343     {
344         function_command_option_t runCmdFuncOption;
345         runCmdFuncOption.commandAddr = 0x1300427dU; /*!< get the flash verify program api location adress in rom*/
346         return runCmdFuncOption.verifyProgramCommand(config, start, lengthInBytes, expectedData, failedAddress,
347                                                      failedData);
348     }
349     else
350     {
351         assert(VERSION1_FLASH_API_TREE);
352         return VERSION1_FLASH_API_TREE->flash_verify_program(config, start, lengthInBytes, expectedData, failedAddress,
353                                                              failedData);
354     }
355 }
356 
357 /*!
358  * @brief Returns the desired flash property.
359  */
FLASH_GetProperty(flash_config_t * config,flash_property_tag_t whichProperty,uint32_t * value)360 status_t FLASH_GetProperty(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value)
361 {
362     assert(VERSION1_FLASH_API_TREE);
363     return VERSION1_FLASH_API_TREE->flash_get_property(config, whichProperty, value);
364 }
365 /********************************************************************************
366  * fsl iap ffr CODE
367  *******************************************************************************/
368 
get_cfpa_higher_version(flash_config_t * config)369 static status_t get_cfpa_higher_version(flash_config_t *config)
370 {
371     uint32_t pageData[FLASH_FFR_MAX_PAGE_SIZE / sizeof(uint32_t)];
372     uint32_t versionPing = 0U;
373     uint32_t versionPong = 0U;
374 
375     /* Get the CFPA ping page data and the corresponding version */
376     config->ffrConfig.cfpaPageOffset = 1U;
377     status_t status = FFR_GetCustomerInfieldData(config, (uint8_t *)pageData, 0U, FLASH_FFR_MAX_PAGE_SIZE);
378     if (status != (int32_t)kStatus_FLASH_Success)
379     {
380         return status;
381     }
382     versionPing = pageData[1];
383 
384     /* Get the CFPA pong page data and the corresponding version */
385     config->ffrConfig.cfpaPageOffset = 2U;
386     status = FFR_GetCustomerInfieldData(config, (uint8_t *)pageData, 0U, FLASH_FFR_MAX_PAGE_SIZE);
387     if (status != (int32_t)kStatus_FLASH_Success)
388     {
389         return status;
390     }
391     versionPong = pageData[1];
392 
393     /* Compare the CFPA ping version and pong version and set it correctly in flash_config structure */
394     if (versionPing > versionPong)
395     {
396         config->ffrConfig.cfpaPageVersion = versionPing;
397         config->ffrConfig.cfpaPageOffset  = 1U;
398     }
399     else
400     {
401         config->ffrConfig.cfpaPageVersion = versionPong;
402         config->ffrConfig.cfpaPageOffset  = 2U;
403     }
404     return (int32_t)kStatus_FLASH_Success;
405 }
406 
407 /*!
408  * Initializes the global FFR properties structure members.
409  */
FFR_Init(flash_config_t * config)410 status_t FFR_Init(flash_config_t *config)
411 {
412     status_t status;
413     if (get_rom_api_version() == 0u)
414     {
415         assert(VERSION0_FLASH_API_TREE);
416         status = VERSION0_FLASH_API_TREE->ffr_init(config);
417         if (status != (status_t)kStatus_FLASH_Success)
418         {
419             return status;
420         }
421         return get_cfpa_higher_version(config);
422     }
423     else
424     {
425         assert(VERSION1_FLASH_API_TREE);
426         status = VERSION1_FLASH_API_TREE->ffr_init(config);
427         if (status != (status_t)kStatus_FLASH_Success)
428         {
429             return status;
430         }
431         return get_cfpa_higher_version(config);
432     }
433 }
434 
435 /*!
436  * Enable firewall for all flash banks.
437  */
FFR_Lock_All(flash_config_t * config)438 status_t FFR_Lock_All(flash_config_t *config)
439 {
440     if (get_rom_api_version() == 0u)
441     {
442         assert(VERSION0_FLASH_API_TREE);
443         return VERSION0_FLASH_API_TREE->ffr_lock_all(config);
444     }
445     else
446     {
447         assert(VERSION1_FLASH_API_TREE);
448         return VERSION1_FLASH_API_TREE->ffr_lock_all(config);
449     }
450 }
451 
452 /*!
453  * APIs to access CMPA pages;
454  * This routine will erase "customer factory page" and program the page with passed data.
455  */
FFR_CustFactoryPageWrite(flash_config_t * config,uint8_t * page_data,bool seal_part)456 status_t FFR_CustFactoryPageWrite(flash_config_t *config, uint8_t *page_data, bool seal_part)
457 {
458     if (get_rom_api_version() == 0u)
459     {
460         assert(VERSION0_FLASH_API_TREE);
461         return VERSION0_FLASH_API_TREE->ffr_cust_factory_page_write(config, page_data, seal_part);
462     }
463     else
464     {
465         assert(VERSION1_FLASH_API_TREE);
466         return VERSION1_FLASH_API_TREE->ffr_cust_factory_page_write(config, page_data, seal_part);
467     }
468 }
469 
470 /*!
471  * See fsl_iap_ffr.h for documentation of this function.
472  */
FFR_GetUUID(flash_config_t * config,uint8_t * uuid)473 status_t FFR_GetUUID(flash_config_t *config, uint8_t *uuid)
474 {
475     if (get_rom_api_version() == 0u)
476     {
477         assert(VERSION0_FLASH_API_TREE);
478         return VERSION0_FLASH_API_TREE->ffr_get_uuid(config, uuid);
479     }
480     else
481     {
482         assert(VERSION1_FLASH_API_TREE);
483         return VERSION1_FLASH_API_TREE->ffr_get_uuid(config, uuid);
484     }
485 }
486 
487 /*!
488  * APIs to access CMPA pages
489  * Read data stored in 'Customer Factory CFG Page'.
490  */
FFR_GetCustomerData(flash_config_t * config,uint8_t * pData,uint32_t offset,uint32_t len)491 status_t FFR_GetCustomerData(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len)
492 {
493     if (get_rom_api_version() == 0u)
494     {
495         assert(VERSION0_FLASH_API_TREE);
496         return VERSION0_FLASH_API_TREE->ffr_get_customer_data(config, pData, offset, len);
497     }
498     else
499     {
500         assert(VERSION1_FLASH_API_TREE);
501         return VERSION1_FLASH_API_TREE->ffr_get_customer_data(config, pData, offset, len);
502     }
503 }
504 
505 /*!
506  * This routine writes the 3 pages allocated for Key store data,
507  * Used during manufacturing. Should write pages when 'customer factory page' is not in sealed state.
508  */
FFR_KeystoreWrite(flash_config_t * config,ffr_key_store_t * pKeyStore)509 status_t FFR_KeystoreWrite(flash_config_t *config, ffr_key_store_t *pKeyStore)
510 {
511     if (get_rom_api_version() == 0u)
512     {
513         assert(VERSION0_FLASH_API_TREE);
514         return VERSION0_FLASH_API_TREE->ffr_keystore_write(config, pKeyStore);
515     }
516     else
517     {
518         assert(VERSION1_FLASH_API_TREE);
519         return VERSION1_FLASH_API_TREE->ffr_keystore_write(config, pKeyStore);
520     }
521 }
522 
523 /*! See fsl_iap_ffr.h for documentation of this function. */
FFR_KeystoreGetAC(flash_config_t * config,uint8_t * pActivationCode)524 status_t FFR_KeystoreGetAC(flash_config_t *config, uint8_t *pActivationCode)
525 {
526     if (get_rom_api_version() == 0u)
527     {
528         assert(VERSION0_FLASH_API_TREE);
529         return VERSION0_FLASH_API_TREE->ffr_keystore_get_ac(config, pActivationCode);
530     }
531     else
532     {
533         assert(VERSION1_FLASH_API_TREE);
534         return VERSION1_FLASH_API_TREE->ffr_keystore_get_ac(config, pActivationCode);
535     }
536 }
537 
538 /*! See fsl_iap_ffr.h for documentation of this function. */
FFR_KeystoreGetKC(flash_config_t * config,uint8_t * pKeyCode,ffr_key_type_t keyIndex)539 status_t FFR_KeystoreGetKC(flash_config_t *config, uint8_t *pKeyCode, ffr_key_type_t keyIndex)
540 {
541     if (get_rom_api_version() == 0u)
542     {
543         assert(VERSION0_FLASH_API_TREE);
544         return VERSION0_FLASH_API_TREE->ffr_keystore_get_kc(config, pKeyCode, keyIndex);
545     }
546     else
547     {
548         assert(VERSION1_FLASH_API_TREE);
549         return VERSION1_FLASH_API_TREE->ffr_keystore_get_kc(config, pKeyCode, keyIndex);
550     }
551 }
552 
553 /*!
554  * APIs to access CFPA pages
555  * This routine will erase CFPA and program the CFPA page with passed data.
556  */
FFR_InfieldPageWrite(flash_config_t * config,uint8_t * page_data,uint32_t valid_len)557 status_t FFR_InfieldPageWrite(flash_config_t *config, uint8_t *page_data, uint32_t valid_len)
558 {
559     if (get_rom_api_version() == 0u)
560     {
561         assert(VERSION0_FLASH_API_TREE);
562         return VERSION0_FLASH_API_TREE->ffr_infield_page_write(config, page_data, valid_len);
563     }
564     else
565     {
566         assert(VERSION1_FLASH_API_TREE);
567         return VERSION1_FLASH_API_TREE->ffr_infield_page_write(config, page_data, valid_len);
568     }
569 }
570 
571 /*!
572  * APIs to access CFPA pages
573  * Generic read function, used by customer to read data stored in 'Customer In-field Page'.
574  */
FFR_GetCustomerInfieldData(flash_config_t * config,uint8_t * pData,uint32_t offset,uint32_t len)575 status_t FFR_GetCustomerInfieldData(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len)
576 {
577     if (get_rom_api_version() == 0u)
578     {
579         assert(VERSION0_FLASH_API_TREE);
580         return VERSION0_FLASH_API_TREE->ffr_get_customer_infield_data(config, pData, offset, len);
581     }
582     else
583     {
584         assert(VERSION1_FLASH_API_TREE);
585         return VERSION1_FLASH_API_TREE->ffr_get_customer_infield_data(config, pData, offset, len);
586     }
587 }
588 
589 /********************************************************************************
590  * Bootloader API
591  *******************************************************************************/
592 /*!
593  * @brief Initialize ROM API for a given operation.
594  *
595  * Inits the ROM API based on the options provided by the application in the second
596  * argument. Every call to rom_init() should be paired with a call to rom_deinit().
597  */
kb_init(kb_session_ref_t ** session,const kb_options_t * options)598 status_t kb_init(kb_session_ref_t **session, const kb_options_t *options)
599 {
600     assert(BOOTLOADER_API_TREE_POINTER);
601     return BOOTLOADER_API_TREE_POINTER->kbApi->kb_init_function(session, options);
602 }
603 
604 /*!
605  * @brief Cleans up the ROM API context.
606  *
607  * After this call, the @a context parameter can be reused for another operation
608  * by calling rom_init() again.
609  */
kb_deinit(kb_session_ref_t * session)610 status_t kb_deinit(kb_session_ref_t *session)
611 {
612     assert(BOOTLOADER_API_TREE_POINTER);
613     return BOOTLOADER_API_TREE_POINTER->kbApi->kb_deinit_function(session);
614 }
615 
616 /*!
617  * Perform the operation configured during init.
618  *
619  * This application must call this API repeatedly, passing in sequential chunks of
620  * data from the boot image (SB file) that is to be processed. The ROM will perform
621  * the selected operation on this data and return. The application may call this
622  * function with as much or as little data as it wishes, which can be used to select
623  * the granularity of time given to the application in between executing the operation.
624  *
625  * @param context Current ROM context pointer.
626  * @param data Buffer of boot image data provided to the ROM by the application.
627  * @param dataLength Length in bytes of the data in the buffer provided to the ROM.
628  *
629  * @retval #kStatus_Success The operation has completed successfully.
630  * @retval #kStatus_Fail An error occurred while executing the operation.
631  * @retval #kStatus_RomApiNeedMoreData No error occurred, but the ROM needs more data to
632  *     continue processing the boot image.
633  */
kb_execute(kb_session_ref_t * session,const uint8_t * data,uint32_t dataLength)634 status_t kb_execute(kb_session_ref_t *session, const uint8_t *data, uint32_t dataLength)
635 {
636     assert(BOOTLOADER_API_TREE_POINTER);
637     return BOOTLOADER_API_TREE_POINTER->kbApi->kb_execute_function(session, data, dataLength);
638 }
639 
640 /********************************************************************************
641  * Image authentication API
642  *******************************************************************************/
643 
644 /*!
645  * @brief Authenticate entry function with ARENA allocator init
646  *
647  * This is called by ROM boot or by ROM API g_skbootAuthenticateInterface
648  */
skboot_authenticate(const uint8_t * imageStartAddr,secure_bool_t * isSignVerified)649 skboot_status_t skboot_authenticate(const uint8_t *imageStartAddr, secure_bool_t *isSignVerified)
650 {
651     assert(BOOTLOADER_API_TREE_POINTER);
652     return BOOTLOADER_API_TREE_POINTER->skbootAuthenticate->skboot_authenticate_function(imageStartAddr,
653                                                                                          isSignVerified);
654 }
655 
656 /*!
657  * @brief Interface for image authentication API
658  */
HASH_IRQHandler(void)659 void HASH_IRQHandler(void)
660 {
661     assert(BOOTLOADER_API_TREE_POINTER);
662     BOOTLOADER_API_TREE_POINTER->skbootAuthenticate->skboot_hashcrypt_irq_handler();
663 }
664 /********************************************************************************
665  * EOF
666  *******************************************************************************/
667