fsl_iap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2018 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. *
  7. */
  8. #include "fsl_iap.h"
  9. #include "fsl_iap_ffr.h"
  10. #include "fsl_device_registers.h"
  11. /*!
  12. * @addtogroup flash_driver_api
  13. * @{
  14. */
  15. #define ROM_API_TREE ((uint32_t *)0x130010f0)
  16. #define BOOTLOADER_API_TREE_POINTER ((bootloader_tree_t *)ROM_API_TREE)
  17. static uint32_t S_Version_minor = 0;
  18. typedef status_t (*EraseCommend_t)(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key);
  19. typedef status_t (*ProgramCommend_t)(flash_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes);
  20. typedef status_t (*VerifyProgramCommend_t)(flash_config_t *config,
  21. uint32_t start,
  22. uint32_t lengthInBytes,
  23. const uint8_t *expectedData,
  24. uint32_t *failedAddress,
  25. uint32_t *failedData);
  26. typedef status_t (*FFR_CustomerPagesInit_t)(flash_config_t *config);
  27. typedef status_t (*FFR_InfieldPageWrite_t)(flash_config_t *config, uint8_t *page_data, uint32_t valid_len);
  28. typedef status_t (*FFR_GetManufactureData_t)(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len);
  29. typedef status_t (*FFR_GetRompatchData_t)(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len);
  30. /*
  31. *!@brief Structure of version property.
  32. *
  33. *!@ingroup bl_core
  34. */
  35. typedef union BootloaderVersion
  36. {
  37. struct
  38. {
  39. uint32_t bugfix : 8; /*!< bugfix version [7:0] */
  40. uint32_t minor : 8; /*!< minor version [15:8] */
  41. uint32_t major : 8; /*!< major version [23:16] */
  42. uint32_t name : 8; /*!< name [31:24] */
  43. } B;
  44. uint32_t version; /*!< combined version numbers. */
  45. } standard_version_t;
  46. /*! @brief Interface for the flash driver.*/
  47. typedef struct FlashDriverInterface
  48. {
  49. standard_version_t version; /*!< flash driver API version number.*/
  50. /*!< Flash driver.*/
  51. status_t (*flash_init)(flash_config_t *config);
  52. status_t (*flash_erase)(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key);
  53. status_t (*flash_program)(flash_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes);
  54. status_t (*flash_verify_erase)(flash_config_t *config, uint32_t start, uint32_t lengthInBytes);
  55. status_t (*flash_verify_program)(flash_config_t *config,
  56. uint32_t start,
  57. uint32_t lengthInBytes,
  58. const uint8_t *expectedData,
  59. uint32_t *failedAddress,
  60. uint32_t *failedData);
  61. status_t (*flash_get_property)(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value);
  62. /*!< Flash FFR driver*/
  63. status_t (*ffr_init)(flash_config_t *config);
  64. status_t (*ffr_deinit)(flash_config_t *config);
  65. status_t (*ffr_cust_factory_page_write)(flash_config_t *config, uint8_t *page_data, bool seal_part);
  66. status_t (*ffr_get_uuid)(flash_config_t *config, uint8_t *uuid);
  67. status_t (*ffr_get_customer_data)(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len);
  68. status_t (*ffr_keystore_write)(flash_config_t *config, ffr_key_store_t *pKeyStore);
  69. status_t (*ffr_keystore_get_ac)(flash_config_t *config, uint8_t *pActivationCode);
  70. status_t (*ffr_keystore_get_kc)(flash_config_t *config, uint8_t *pKeyCode, ffr_key_type_t keyIndex);
  71. status_t (*ffr_infield_page_write)(flash_config_t *config, uint8_t *page_data, uint32_t valid_len);
  72. status_t (*ffr_get_customer_infield_data)(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len);
  73. } flash_driver_interface_t;
  74. /*!
  75. * @brief Root of the bootloader API tree.
  76. *
  77. * An instance of this struct resides in read-only memory in the bootloader. It
  78. * provides a user application access to APIs exported by the bootloader.
  79. *
  80. * @note The order of existing fields must not be changed.
  81. */
  82. typedef struct BootloaderTree
  83. {
  84. void (*runBootloader)(void *arg); /*!< Function to start the bootloader executing. */
  85. standard_version_t bootloader_version; /*!< Bootloader version number. */
  86. const char *copyright; /*!< Copyright string. */
  87. const uint32_t *reserved; /*!< Do NOT use. */
  88. const flash_driver_interface_t *flashDriver; /*!< Flash driver API. */
  89. } bootloader_tree_t;
  90. /*******************************************************************************
  91. * Variables
  92. ******************************************************************************/
  93. /*! @brief Global pointer to the flash driver API table in ROM. */
  94. flash_driver_interface_t *FLASH_API_TREE;
  95. /*! Get pointer to flash driver API table in ROM. */
  96. #define FLASH_API_TREE BOOTLOADER_API_TREE_POINTER->flashDriver
  97. /*******************************************************************************
  98. * Code
  99. ******************************************************************************/
  100. /*! See fsl_flash.h for documentation of this function. */
  101. status_t FLASH_Init(flash_config_t *config)
  102. {
  103. assert(FLASH_API_TREE);
  104. config->modeConfig.sysFreqInMHz = kSysToFlashFreq_defaultInMHz;
  105. S_Version_minor = FLASH_API_TREE->version.B.minor;
  106. return FLASH_API_TREE->flash_init(config);
  107. }
  108. /*! See fsl_flash.h for documentation of this function. */
  109. status_t FLASH_Erase(flash_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
  110. {
  111. if (S_Version_minor == 0)
  112. {
  113. EraseCommend_t EraseCommand =
  114. (EraseCommend_t)(0x1300413b); /*!< get the flash erase api location adress int rom */
  115. return EraseCommand(config, start, lengthInBytes, key);
  116. }
  117. else
  118. {
  119. assert(FLASH_API_TREE);
  120. return FLASH_API_TREE->flash_erase(config, start, lengthInBytes, key);
  121. }
  122. }
  123. /*! See fsl_flash.h for documentation of this function. */
  124. status_t FLASH_Program(flash_config_t *config, uint32_t start, uint8_t *src, uint32_t lengthInBytes)
  125. {
  126. if (S_Version_minor == 0)
  127. {
  128. ProgramCommend_t ProgramCommend =
  129. (ProgramCommend_t)(0x1300419d); /*!< get the flash program api location adress in rom*/
  130. return ProgramCommend(config, start, src, lengthInBytes);
  131. }
  132. else
  133. {
  134. assert(FLASH_API_TREE);
  135. return FLASH_API_TREE->flash_program(config, start, src, lengthInBytes);
  136. }
  137. }
  138. /*! See fsl_flash.h for documentation of this function. */
  139. status_t FLASH_VerifyErase(flash_config_t *config, uint32_t start, uint32_t lengthInBytes)
  140. {
  141. assert(FLASH_API_TREE);
  142. return FLASH_API_TREE->flash_verify_erase(config, start, lengthInBytes);
  143. }
  144. /*! See fsl_flash.h for documentation of this function. */
  145. status_t FLASH_VerifyProgram(flash_config_t *config,
  146. uint32_t start,
  147. uint32_t lengthInBytes,
  148. const uint8_t *expectedData,
  149. uint32_t *failedAddress,
  150. uint32_t *failedData)
  151. {
  152. if (S_Version_minor == 0)
  153. {
  154. VerifyProgramCommend_t VerifyProgramCommend =
  155. (VerifyProgramCommend_t)(0x1300427d); /*!< get the flash verify program api location adress in rom*/
  156. return VerifyProgramCommend(config, start, lengthInBytes, expectedData, failedAddress, failedData);
  157. }
  158. else
  159. {
  160. assert(FLASH_API_TREE);
  161. return FLASH_API_TREE->flash_verify_program(config, start, lengthInBytes, expectedData, failedAddress,
  162. failedData);
  163. }
  164. }
  165. /*! See fsl_flash.h for documentation of this function.*/
  166. status_t FLASH_GetProperty(flash_config_t *config, flash_property_tag_t whichProperty, uint32_t *value)
  167. {
  168. assert(FLASH_API_TREE);
  169. return FLASH_API_TREE->flash_get_property(config, whichProperty, value);
  170. }
  171. /********************************************************************************
  172. * fsl_flash_ffr CODE
  173. *******************************************************************************/
  174. /*! See fsl_flash_ffr.h for documentation of this function. */
  175. status_t FFR_Init(flash_config_t *config)
  176. {
  177. assert(FLASH_API_TREE);
  178. return FLASH_API_TREE->ffr_init(config);
  179. }
  180. /*! See fsl_flash_ffr.h for documentation of this function. */
  181. status_t FFR_Deinit(flash_config_t *config)
  182. {
  183. assert(FLASH_API_TREE);
  184. return FLASH_API_TREE->ffr_deinit(config);
  185. }
  186. status_t FFR_CustomerPagesInit(flash_config_t *config)
  187. {
  188. assert(FLASH_API_TREE);
  189. FFR_CustomerPagesInit_t FFR_CustomerPagesInit_cmd = (FFR_CustomerPagesInit_t)(0x13004951);
  190. return FFR_CustomerPagesInit_cmd(config);
  191. }
  192. status_t FFR_InfieldPageWrite(flash_config_t *config, uint8_t *page_data, uint32_t valid_len)
  193. {
  194. FFR_InfieldPageWrite_t FFR_InfieldPageWrite_cmd = (FFR_InfieldPageWrite_t)(0x13004a0b);
  195. return FFR_InfieldPageWrite_cmd(config, page_data, valid_len);
  196. }
  197. /*! See fsl_flash_ffr.h for documentation of this function. */
  198. status_t FFR_CustFactoryPageWrite(flash_config_t *config, uint8_t *page_data, bool seal_part)
  199. {
  200. assert(FLASH_API_TREE);
  201. return FLASH_API_TREE->ffr_cust_factory_page_write(config, page_data, seal_part);
  202. }
  203. /*! See fsl_flash_ffr.h for documentation of this function. */
  204. status_t FFR_GetCustomerData(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len)
  205. {
  206. assert(FLASH_API_TREE);
  207. return FLASH_API_TREE->ffr_get_customer_data(config, pData, offset, len);
  208. }
  209. /*! See fsl_flash_ffr.h for documentation of this function. */
  210. status_t FFR_KeystoreWrite(flash_config_t *config, ffr_key_store_t *pKeyStore)
  211. {
  212. assert(FLASH_API_TREE);
  213. return FLASH_API_TREE->ffr_keystore_write(config, pKeyStore);
  214. }
  215. /*! See fsl_flash_ffr.h for documentation of this function. */
  216. status_t FFR_KeystoreGetAC(flash_config_t *config, uint8_t *pActivationCode)
  217. {
  218. assert(FLASH_API_TREE);
  219. return FLASH_API_TREE->ffr_keystore_get_ac(config, pActivationCode);
  220. }
  221. /*! See fsl_flash_ffr.h for documentation of this function. */
  222. status_t FFR_KeystoreGetKC(flash_config_t *config, uint8_t *pKeyCode, ffr_key_type_t keyIndex)
  223. {
  224. assert(FLASH_API_TREE);
  225. return FLASH_API_TREE->ffr_keystore_get_kc(config, pKeyCode, keyIndex);
  226. }
  227. status_t FFR_GetRompatchData(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len)
  228. {
  229. FFR_GetRompatchData_t FFR_GetRompatchData_cmd = (FFR_GetRompatchData_t)(0x13004db3);
  230. return FFR_GetRompatchData_cmd(config, pData, offset, len);
  231. }
  232. /* APIs to access NMPA pages */
  233. status_t FFR_GetManufactureData(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len)
  234. {
  235. FFR_GetManufactureData_t FFR_GetManufactureData_cmd = (FFR_GetManufactureData_t)(0x13004e15);
  236. return FFR_GetManufactureData_cmd(config, pData, offset, len);
  237. }
  238. /*! See fsl_flash_ffr.h for documentation of this function. */
  239. status_t FFR_GetUUID(flash_config_t *config, uint8_t *uuid)
  240. {
  241. assert(FLASH_API_TREE);
  242. return FLASH_API_TREE->ffr_get_uuid(config, uuid);
  243. }
  244. /*! See fsl_flash_ffr.h for documentation of this function. */
  245. status_t FFR_GetCustomerInfieldData(flash_config_t *config, uint8_t *pData, uint32_t offset, uint32_t len)
  246. {
  247. assert(FLASH_API_TREE);
  248. return FLASH_API_TREE->ffr_get_customer_infield_data(config, pData, offset, len);
  249. }
  250. /*! @}*/
  251. /********************************************************************************
  252. * EOF
  253. *******************************************************************************/