You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

999 lines
30 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_otfdec.c
  4. * @author MCD Application Team
  5. * @brief OTFDEC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the On-The-Fly Decryption (OTFDEC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Region setting/enable functions
  10. * + Peripheral State functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. The OTFDEC HAL driver can be used as follows:
  18. (#) Declare an OTFDEC_HandleTypeDef handle structure (eg. OTFDEC_HandleTypeDef hotfdec).
  19. (#) Initialize the OTFDEC low level resources by implementing the HAL_OTFDEC_MspInit() API:
  20. (++) Enable the OTFDEC interface clock.
  21. (++) NVIC configuration if interrupts are used
  22. (+++) Configure the OTFDEC interrupt priority.
  23. (+++) Enable the NVIC OTFDEC IRQ handle.
  24. (#) Initialize the OTFDEC peripheral by calling the HAL_OTFDEC_Init() API.
  25. (#) For each region,
  26. (++) Configure the region deciphering mode by calling the HAL_OTFDEC_RegionSetMode() API.
  27. (++) Write the region Key by calling the HAL_OTFDEC_RegionSetKey() API. If desired,
  28. read the key CRC by calling HAL_OTFDEC_RegionGetKeyCRC() API and compare the
  29. result with the theoretically expected CRC.
  30. (++) Initialize the OTFDEC region config structure with the Nonce, protected
  31. region start and end addresses and firmware version, and wrap-up the region
  32. configuration by calling HAL_OTFDEC_RegionConfig() API.
  33. (#) At this point, the OTFDEC region configuration is done and the deciphering
  34. is enabled. The region can be deciphered on the fly after having made sure
  35. the OctoSPI is configured in memory-mapped mode.
  36. [..]
  37. (@) Warning: the OTFDEC deciphering is based on a different endianness compared
  38. to the AES-CTR as implemented in the AES peripheral. E.g., if the OTFEC
  39. resorts to the Key (B0, B1, B2, B3) where Bi are 32-bit longwords and B0
  40. is the Least Significant Word, the AES has to be configured with the Key
  41. (B3, B2, B1, B0) to report the same result (with the same swapping applied
  42. to the Initialization Vector).
  43. [..]
  44. *** Callback registration ***
  45. =============================================
  46. [..]
  47. The compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS, when set to 1,
  48. allows the user to configure dynamically the driver callbacks.
  49. Use Functions @ref HAL_OTFDEC_RegisterCallback()
  50. to register an interrupt callback.
  51. [..]
  52. Function @ref HAL_OTFDEC_RegisterCallback() allows to register following callbacks:
  53. (+) ErrorCallback : OTFDEC error callback
  54. (+) MspInitCallback : OTFDEC Msp Init callback
  55. (+) MspDeInitCallback : OTFDEC Msp DeInit callback
  56. This function takes as parameters the HAL peripheral handle, the Callback ID
  57. and a pointer to the user callback function.
  58. [..]
  59. Use function @ref HAL_OTFDEC_UnRegisterCallback to reset a callback to the default
  60. weak function.
  61. [..]
  62. @ref HAL_OTFDEC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  63. and the Callback ID.
  64. This function allows to reset following callbacks:
  65. (+) ErrorCallback : OTFDEC error callback
  66. (+) MspInitCallback : OTFDEC Msp Init callback
  67. (+) MspDeInitCallback : OTFDEC Msp DeInit callback
  68. [..]
  69. By default, after the @ref HAL_OTFDEC_Init() and when the state is @ref HAL_OTFDEC_STATE_RESET
  70. all callbacks are set to the corresponding weak functions:
  71. example @ref HAL_OTFDEC_ErrorCallback().
  72. Exception done for MspInit and MspDeInit functions that are
  73. reset to the legacy weak functions in the @ref HAL_OTFDEC_Init()/ @ref HAL_OTFDEC_DeInit() only when
  74. these callbacks are null (not registered beforehand).
  75. [..]
  76. If MspInit or MspDeInit are not null, the @ref HAL_OTFDEC_Init()/ @ref HAL_OTFDEC_DeInit()
  77. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  78. [..]
  79. Callbacks can be registered/unregistered in @ref HAL_OTFDEC_STATE_READY state only.
  80. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  81. in @ref HAL_OTFDEC_STATE_READY or @ref HAL_OTFDEC_STATE_RESET state,
  82. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  83. [..]
  84. Then, the user first registers the MspInit/MspDeInit user callbacks
  85. using @ref HAL_OTFDEC_RegisterCallback() before calling @ref HAL_OTFDEC_DeInit()
  86. or @ref HAL_OTFDEC_Init() function.
  87. [..]
  88. When the compilation flag USE_HAL_OTFDEC_REGISTER_CALLBACKS is set to 0 or
  89. not defined, the callback registration feature is not available and all callbacks
  90. are set to the corresponding weak functions.
  91. @endverbatim
  92. ******************************************************************************
  93. * @attention
  94. *
  95. * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
  96. * All rights reserved.</center></h2>
  97. *
  98. * This software component is licensed by ST under BSD 3-Clause license,
  99. * the "License"; You may not use this file except in compliance with the
  100. * License. You may obtain a copy of the License at:
  101. * opensource.org/licenses/BSD-3-Clause
  102. *
  103. ******************************************************************************
  104. */
  105. /* Includes ------------------------------------------------------------------*/
  106. #include "stm32h7xx_hal.h"
  107. /** @addtogroup STM32H7xx_HAL_Driver
  108. * @{
  109. */
  110. /** @defgroup OTFDEC OTFDEC
  111. * @brief OTFDEC HAL module driver.
  112. * @{
  113. */
  114. #ifdef HAL_OTFDEC_MODULE_ENABLED
  115. #if defined(OTFDEC1)
  116. /* Private typedef -----------------------------------------------------------*/
  117. /* Private define ------------------------------------------------------------*/
  118. /* Private macro -------------------------------------------------------------*/
  119. /* Private variables ---------------------------------------------------------*/
  120. /* Private function prototypes -----------------------------------------------*/
  121. /* Private functions ---------------------------------------------------------*/
  122. /* Exported functions --------------------------------------------------------*/
  123. /** @addtogroup OTFDEC_Exported_Functions
  124. * @{
  125. */
  126. /** @defgroup OTFDEC_Exported_Functions_Group1 Initialization and de-initialization functions
  127. * @brief Initialization and Configuration functions.
  128. *
  129. @verbatim
  130. ==============================================================================
  131. ##### Initialization and de-initialization functions #####
  132. ==============================================================================
  133. @endverbatim
  134. * @{
  135. */
  136. /**
  137. * @brief Initialize the OTFDEC peripheral and create the associated handle.
  138. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  139. * the configuration information for OTFDEC module
  140. * @retval HAL status
  141. */
  142. HAL_StatusTypeDef HAL_OTFDEC_Init(OTFDEC_HandleTypeDef *hotfdec)
  143. {
  144. /* Check the OTFDEC handle allocation */
  145. if(hotfdec == NULL)
  146. {
  147. return HAL_ERROR;
  148. }
  149. /* Check the parameters */
  150. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  151. if(hotfdec->State == HAL_OTFDEC_STATE_RESET)
  152. {
  153. /* Allocate lock resource and initialize it */
  154. __HAL_UNLOCK(hotfdec);
  155. #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
  156. /* Init the OTFDEC Callback settings */
  157. hotfdec->ErrorCallback = HAL_OTFDEC_ErrorCallback; /* Legacy weak callback */
  158. if (hotfdec->MspInitCallback == NULL)
  159. {
  160. hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
  161. }
  162. /* Init the low level hardware */
  163. hotfdec->MspInitCallback(hotfdec);
  164. #else
  165. /* Init the low level hardware */
  166. HAL_OTFDEC_MspInit(hotfdec);
  167. #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
  168. }
  169. /* Change the OTFDEC state */
  170. hotfdec->State = HAL_OTFDEC_STATE_READY;
  171. /* Return function status */
  172. return HAL_OK;
  173. }
  174. /**
  175. * @brief DeInitialize the OTFDEC peripheral.
  176. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  177. * the configuration information for OTFDEC module
  178. * @retval HAL status
  179. */
  180. HAL_StatusTypeDef HAL_OTFDEC_DeInit(OTFDEC_HandleTypeDef *hotfdec)
  181. {
  182. /* Check the OTFDEC handle allocation */
  183. if(hotfdec == NULL)
  184. {
  185. return HAL_ERROR;
  186. }
  187. /* Check the parameters */
  188. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  189. /* Change the OTFDEC state */
  190. hotfdec->State = HAL_OTFDEC_STATE_BUSY;
  191. #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
  192. if (hotfdec->MspDeInitCallback == NULL)
  193. {
  194. hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
  195. }
  196. /* DeInit the low level hardware: CLOCK, NVIC */
  197. hotfdec->MspDeInitCallback(hotfdec);
  198. #else
  199. /* DeInit the low level hardware: CLOCK, NVIC */
  200. HAL_OTFDEC_MspDeInit(hotfdec);
  201. #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
  202. /* Change the OTFDEC state */
  203. hotfdec->State = HAL_OTFDEC_STATE_RESET;
  204. /* Reset OTFDEC error status */
  205. hotfdec->ErrorCode = HAL_OTFDEC_ERROR_NONE;
  206. /* Release Lock */
  207. __HAL_UNLOCK(hotfdec);
  208. /* Return function status */
  209. return HAL_OK;
  210. }
  211. /**
  212. * @brief Initialize the OTFDEC MSP.
  213. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  214. * the configuration information for OTFDEC module
  215. * @retval None
  216. */
  217. __weak void HAL_OTFDEC_MspInit(OTFDEC_HandleTypeDef *hotfdec)
  218. {
  219. /* Prevent unused argument(s) compilation warning */
  220. UNUSED(hotfdec);
  221. /* NOTE : This function should not be modified; when the callback is needed,
  222. the HAL_OTFDEC_MspInit can be implemented in the user file.
  223. */
  224. }
  225. /**
  226. * @brief DeInitialize OTFDEC MSP.
  227. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  228. * the configuration information for OTFDEC module
  229. * @retval None
  230. */
  231. __weak void HAL_OTFDEC_MspDeInit(OTFDEC_HandleTypeDef *hotfdec)
  232. {
  233. /* Prevent unused argument(s) compilation warning */
  234. UNUSED(hotfdec);
  235. /* NOTE : This function should not be modified; when the callback is needed,
  236. the HAL_OTFDEC_MspDeInit can be implemented in the user file.
  237. */
  238. }
  239. #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
  240. /**
  241. * @brief Register a User OTFDEC Callback
  242. * To be used instead of the weak predefined callback
  243. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  244. * the configuration information for OTFDEC module
  245. * @param CallbackID ID of the callback to be registered
  246. * This parameter can be one of the following values:
  247. * @arg @ref HAL_OTFDEC_ERROR_CB_ID OTFDEC error callback ID
  248. * @arg @ref HAL_OTFDEC_MSPINIT_CB_ID MspInit callback ID
  249. * @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID MspDeInit callback ID
  250. * @param pCallback pointer to the Callback function
  251. * @retval HAL status
  252. */
  253. HAL_StatusTypeDef HAL_OTFDEC_RegisterCallback(OTFDEC_HandleTypeDef *hotfdec, HAL_OTFDEC_CallbackIDTypeDef CallbackID, pOTFDEC_CallbackTypeDef pCallback)
  254. {
  255. HAL_StatusTypeDef status = HAL_OK;
  256. if (pCallback == NULL)
  257. {
  258. /* Update the error code */
  259. hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
  260. return HAL_ERROR;
  261. }
  262. if (hotfdec->State == HAL_OTFDEC_STATE_READY)
  263. {
  264. switch (CallbackID)
  265. {
  266. case HAL_OTFDEC_ERROR_CB_ID :
  267. hotfdec->ErrorCallback = pCallback;
  268. break;
  269. case HAL_OTFDEC_MSPINIT_CB_ID :
  270. hotfdec->MspInitCallback = pCallback;
  271. break;
  272. case HAL_OTFDEC_MSPDEINIT_CB_ID :
  273. hotfdec->MspDeInitCallback = pCallback;
  274. break;
  275. default :
  276. /* Update the error code */
  277. hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
  278. /* Return error status */
  279. status = HAL_ERROR;
  280. break;
  281. }
  282. }
  283. else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
  284. {
  285. switch (CallbackID)
  286. {
  287. case HAL_OTFDEC_MSPINIT_CB_ID :
  288. hotfdec->MspInitCallback = pCallback;
  289. break;
  290. case HAL_OTFDEC_MSPDEINIT_CB_ID :
  291. hotfdec->MspDeInitCallback = pCallback;
  292. break;
  293. default :
  294. /* Update the error code */
  295. hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
  296. /* Return error status */
  297. status = HAL_ERROR;
  298. break;
  299. }
  300. }
  301. else
  302. {
  303. /* Update the error code */
  304. hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
  305. /* Return error status */
  306. status = HAL_ERROR;
  307. }
  308. return status;
  309. }
  310. /**
  311. * @brief Unregister a OTFDEC Callback
  312. * OTFDEC callback is redirected to the weak predefined callback
  313. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  314. * the configuration information for OTFDEC module
  315. * @param CallbackID ID of the callback to be registered
  316. * This parameter can be one of the following values:
  317. * @arg @ref HAL_OTFDEC_ERROR_CB_ID OTFDEC error callback ID
  318. * @arg @ref HAL_OTFDEC_MSPINIT_CB_ID MspInit callback ID
  319. * @arg @ref HAL_OTFDEC_MSPDEINIT_CB_ID MspDeInit callback ID
  320. * @retval HAL status
  321. */
  322. HAL_StatusTypeDef HAL_OTFDEC_UnRegisterCallback(OTFDEC_HandleTypeDef *hotfdec, HAL_OTFDEC_CallbackIDTypeDef CallbackID)
  323. {
  324. HAL_StatusTypeDef status = HAL_OK;
  325. if (hotfdec->State == HAL_OTFDEC_STATE_READY)
  326. {
  327. switch (CallbackID)
  328. {
  329. case HAL_OTFDEC_ERROR_CB_ID :
  330. hotfdec->ErrorCallback = HAL_OTFDEC_ErrorCallback;
  331. break;
  332. case HAL_OTFDEC_MSPINIT_CB_ID :
  333. hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
  334. break;
  335. case HAL_OTFDEC_MSPDEINIT_CB_ID :
  336. hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
  337. break;
  338. default :
  339. /* Update the error code */
  340. hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
  341. /* Return error status */
  342. status = HAL_ERROR;
  343. break;
  344. }
  345. }
  346. else if (HAL_OTFDEC_STATE_RESET == hotfdec->State)
  347. {
  348. switch (CallbackID)
  349. {
  350. case HAL_OTFDEC_MSPINIT_CB_ID :
  351. hotfdec->MspInitCallback = HAL_OTFDEC_MspInit; /* Legacy weak MspInit */
  352. break;
  353. case HAL_OTFDEC_MSPDEINIT_CB_ID :
  354. hotfdec->MspDeInitCallback = HAL_OTFDEC_MspDeInit; /* Legacy weak MspDeInit */
  355. break;
  356. default :
  357. /* Update the error code */
  358. hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
  359. /* Return error status */
  360. status = HAL_ERROR;
  361. break;
  362. }
  363. }
  364. else
  365. {
  366. /* Update the error code */
  367. hotfdec->ErrorCode |= HAL_OTFDEC_ERROR_INVALID_CALLBACK;
  368. /* Return error status */
  369. status = HAL_ERROR;
  370. }
  371. return status;
  372. }
  373. #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
  374. /**
  375. * @}
  376. */
  377. /** @defgroup OTFDEC_Exported_Functions_Group2 OTFDEC IRQ handler management
  378. * @brief OTFDEC IRQ handler.
  379. *
  380. @verbatim
  381. ==============================================================================
  382. ##### OTFDEC IRQ handler management #####
  383. ==============================================================================
  384. [..] This section provides OTFDEC IRQ handler function.
  385. @endverbatim
  386. * @{
  387. */
  388. /**
  389. * @brief Handle OTFDEC interrupt request.
  390. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  391. * the configuration information for OTFDEC module
  392. * @retval None
  393. */
  394. void HAL_OTFDEC_IRQHandler(OTFDEC_HandleTypeDef *hotfdec)
  395. {
  396. uint32_t isr_reg;
  397. isr_reg = READ_REG(hotfdec->Instance->ISR);
  398. if ((isr_reg & OTFDEC_ISR_SEIF) == OTFDEC_ISR_SEIF)
  399. {
  400. SET_BIT( hotfdec->Instance->ICR, OTFDEC_ICR_SEIF );
  401. hotfdec->ErrorCode |= HAL_OTFDEC_SECURITY_ERROR;
  402. }
  403. if ((isr_reg & OTFDEC_ISR_XONEIF) == OTFDEC_ISR_XONEIF)
  404. {
  405. SET_BIT( hotfdec->Instance->ICR, OTFDEC_ICR_XONEIF );
  406. hotfdec->ErrorCode |= HAL_OTFDEC_EXECUTE_ERROR;
  407. }
  408. if ((isr_reg & OTFDEC_ISR_KEIF) == OTFDEC_ISR_KEIF)
  409. {
  410. SET_BIT( hotfdec->Instance->ICR, OTFDEC_ICR_KEIF );
  411. hotfdec->ErrorCode |= HAL_OTFDEC_KEY_ERROR;
  412. }
  413. #if (USE_HAL_OTFDEC_REGISTER_CALLBACKS == 1)
  414. hotfdec->ErrorCallback(hotfdec);
  415. #else
  416. HAL_OTFDEC_ErrorCallback(hotfdec);
  417. #endif /* USE_HAL_OTFDEC_REGISTER_CALLBACKS */
  418. }
  419. /**
  420. * @brief OTFDEC error callback.
  421. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  422. * the configuration information for OTFDEC module
  423. * @retval None
  424. */
  425. __weak void HAL_OTFDEC_ErrorCallback(OTFDEC_HandleTypeDef *hotfdec)
  426. {
  427. /* Prevent unused argument(s) compilation warning */
  428. UNUSED(hotfdec);
  429. /* NOTE : This function should not be modified; when the callback is needed,
  430. the HAL_OTFDEC_ErrorCallback can be implemented in the user file.
  431. */
  432. }
  433. /**
  434. * @}
  435. */
  436. /** @defgroup OTFDEC_Exported_Functions_Group3 Peripheral Control functions
  437. * @brief Peripheral control functions.
  438. *
  439. @verbatim
  440. ==============================================================================
  441. ##### Peripheral Control functions #####
  442. ==============================================================================
  443. [..]
  444. This subsection permits to configure the OTFDEC peripheral
  445. @endverbatim
  446. * @{
  447. */
  448. /**
  449. * @brief Lock region keys.
  450. * @note Writes to this region KEYRx registers are ignored until next OTFDEC reset.
  451. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  452. * the configuration information for OTFDEC module
  453. * @param RegionIndex index of region the keys of which are locked
  454. * @retval HAL state
  455. */
  456. HAL_StatusTypeDef HAL_OTFDEC_RegionKeyLock(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
  457. {
  458. OTFDEC_Region_TypeDef * region;
  459. uint32_t address;
  460. /* Check the parameters */
  461. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  462. assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
  463. /* Take Lock */
  464. __HAL_LOCK(hotfdec);
  465. address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
  466. region = (OTFDEC_Region_TypeDef *)address;
  467. SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_KEYLOCK );
  468. /* Release Lock */
  469. __HAL_UNLOCK(hotfdec);
  470. /* Status is okay */
  471. return HAL_OK;
  472. }
  473. /**
  474. * @brief Set region keys.
  475. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  476. * the configuration information for OTFDEC module
  477. * @param RegionIndex index of region the keys of which are set
  478. * @param pKey pointer at set of keys
  479. * @note The API reads the key CRC computed by the peripheral and compares it with thzt
  480. * theoretically expected. An error is reported if they are different.
  481. * @retval HAL state
  482. */
  483. HAL_StatusTypeDef HAL_OTFDEC_RegionSetKey(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t *pKey)
  484. {
  485. OTFDEC_Region_TypeDef * region;
  486. uint32_t address;
  487. /* Check the parameters */
  488. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  489. assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
  490. if (pKey == NULL)
  491. {
  492. return HAL_ERROR;
  493. }
  494. else
  495. {
  496. /* Take Lock */
  497. __HAL_LOCK(hotfdec);
  498. address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
  499. region = (OTFDEC_Region_TypeDef *)address;
  500. /* Set Key */
  501. WRITE_REG( region->REG_KEYR0, pKey[0]);
  502. __DSB();
  503. __ISB();
  504. WRITE_REG( region->REG_KEYR1, pKey[1]);
  505. __DSB();
  506. __ISB();
  507. WRITE_REG( region->REG_KEYR2, pKey[2]);
  508. __DSB();
  509. __ISB();
  510. WRITE_REG( region->REG_KEYR3, pKey[3]);
  511. /* Compute theoretically expected CRC and compare it with that reported by the peripheral */
  512. if (HAL_OTFDEC_KeyCRCComputation(pKey) != HAL_OTFDEC_RegionGetKeyCRC(hotfdec, RegionIndex))
  513. {
  514. /* Release Lock */
  515. __HAL_UNLOCK(hotfdec);
  516. /* Status is okay */
  517. return HAL_ERROR;
  518. }
  519. /* Release Lock */
  520. __HAL_UNLOCK(hotfdec);
  521. /* Status is okay */
  522. return HAL_OK;
  523. }
  524. }
  525. /**
  526. * @brief Set region mode.
  527. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  528. * the configuration information for OTFDEC module
  529. * @param RegionIndex index of region the mode of which is set
  530. * @param mode This parameter can be only:
  531. * @arg @ref OTFDEC_REG_MODE_INSTRUCTION_ACCESSES_ONLY Only instruction accesses are decrypted
  532. * @arg @ref OTFDEC_REG_MODE_DATA_ACCESSES_ONLY Only data accesses are decrypted
  533. * @arg @ref OTFDEC_REG_MODE_INSTRUCTION_OR_DATA_ACCESSES All read accesses are decrypted (instruction or data)
  534. * @arg @ref OTFDEC_REG_MODE_INSTRUCTION_ACCESSES_ONLY_WITH_CIPHER Only instruction accesses are decrypted with proprietary cipher activated
  535. * @retval HAL state
  536. */
  537. HAL_StatusTypeDef HAL_OTFDEC_RegionSetMode(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, uint32_t mode)
  538. {
  539. OTFDEC_Region_TypeDef * region;
  540. uint32_t address;
  541. /* Check the parameters */
  542. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  543. assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
  544. assert_param(IS_OTFDEC_REGION_OPERATING_MODE(mode));
  545. /* Take Lock */
  546. __HAL_LOCK(hotfdec);
  547. address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
  548. region = (OTFDEC_Region_TypeDef *)address;
  549. /* Set mode */
  550. MODIFY_REG(region->REG_CONFIGR, OTFDEC_REG_CONFIGR_MODE, mode);
  551. /* Release Lock */
  552. __HAL_UNLOCK(hotfdec);
  553. /* Status is okay */
  554. return HAL_OK;
  555. }
  556. /**
  557. * @brief Set region configuration.
  558. * @note Region deciphering is enabled at the end of this function
  559. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  560. * the configuration information for OTFDEC module
  561. * @param RegionIndex index of region that is configured
  562. * @param Config pointer on structure containing the region configuration parameters
  563. * @param lock configuration lock enable or disable parameter
  564. * This parameter can be one of the following values:
  565. * @arg @ref OTFDEC_REG_CONFIGR_LOCK_DISABLE OTFDEC region configuration is not locked
  566. * @arg @ref OTFDEC_REG_CONFIGR_LOCK_ENABLE OTFDEC region configuration is locked
  567. * @retval HAL state
  568. */
  569. HAL_StatusTypeDef HAL_OTFDEC_RegionConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, OTFDEC_RegionConfigTypeDef *Config, uint32_t lock)
  570. {
  571. OTFDEC_Region_TypeDef * region;
  572. uint32_t address;
  573. /* Check the parameters */
  574. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  575. assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
  576. assert_param(IS_OTFDEC_REGION_CONFIG_LOCK(lock));
  577. if (Config == NULL)
  578. {
  579. return HAL_ERROR;
  580. }
  581. else
  582. {
  583. /* Take Lock */
  584. __HAL_LOCK(hotfdec);
  585. address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
  586. region = (OTFDEC_Region_TypeDef *)address;
  587. /* Set Nonce */
  588. WRITE_REG( region->REG_NONCER0, Config->Nonce[0]);
  589. WRITE_REG( region->REG_NONCER1, Config->Nonce[1]);
  590. /* Write region protected area start and end addresses */
  591. WRITE_REG( region->REG_START_ADDR, Config->StartAddress);
  592. WRITE_REG( region->REG_END_ADDR, Config->EndAddress);
  593. /* Write Version */
  594. MODIFY_REG( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_VERSION, (uint32_t)(Config->Version) << OTFDEC_REG_CONFIGR_VERSION_Pos );
  595. /* Enable region deciphering or enciphering (depending of OTFDEC_CR ENC bit setting) */
  596. SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
  597. /* Lock the region configuration according to lock parameter value */
  598. if (lock == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
  599. {
  600. SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE);
  601. }
  602. /* Release Lock */
  603. __HAL_UNLOCK(hotfdec);
  604. /* Status is okay */
  605. return HAL_OK;
  606. }
  607. }
  608. /**
  609. * @brief Compute Key CRC
  610. * @param pKey pointer at set of keys
  611. * @retval CRC value
  612. */
  613. uint32_t HAL_OTFDEC_KeyCRCComputation(uint32_t *pKey)
  614. {
  615. uint8_t crc7_poly = 0x7;
  616. uint32_t key_strobe[4] = {0xAA55AA55U, 0x3U, 0x18U, 0xC0U};
  617. uint8_t i;
  618. uint8_t crc = 0;
  619. uint32_t j, keyval, k;
  620. uint32_t * temp = pKey;
  621. for (j = 0U; j < 4U; j++)
  622. {
  623. keyval = *temp;
  624. temp++;
  625. if (j == 0U)
  626. {
  627. keyval ^= key_strobe[0];
  628. }
  629. else
  630. {
  631. keyval ^= (key_strobe[j] << 24) | ((uint32_t)crc << 16) | (key_strobe[j] << 8) | crc;
  632. }
  633. crc = 0;
  634. for (i = 0; i < (uint8_t)32; i++)
  635. {
  636. k = ((((uint32_t)crc >> 7) ^ ((keyval >> ((uint8_t)31-i))&((uint8_t)0xF)))) & 1U;
  637. crc <<= 1;
  638. if (k != 0U)
  639. {
  640. crc ^= crc7_poly;
  641. }
  642. }
  643. crc^=(uint8_t)0x55;
  644. }
  645. return (uint32_t) crc;
  646. }
  647. /**
  648. * @brief Enable region deciphering.
  649. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  650. * the configuration information for OTFDEC module
  651. * @param RegionIndex index of region the deciphering is enabled
  652. * @note An error is reported when the configuration is locked.
  653. * @retval HAL state
  654. */
  655. HAL_StatusTypeDef HAL_OTFDEC_RegionEnable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
  656. {
  657. OTFDEC_Region_TypeDef * region;
  658. uint32_t address;
  659. /* Check the parameters */
  660. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  661. assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
  662. /* Take Lock */
  663. __HAL_LOCK(hotfdec);
  664. address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
  665. region = (OTFDEC_Region_TypeDef *)address;
  666. if (READ_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE) == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
  667. {
  668. /* Configuration is locked, REG_EN bit can't be modified */
  669. __HAL_UNLOCK(hotfdec);
  670. return HAL_ERROR;
  671. }
  672. /* Enable region processing */
  673. SET_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
  674. /* Release Lock */
  675. __HAL_UNLOCK(hotfdec);
  676. /* Status is okay */
  677. return HAL_OK;
  678. }
  679. /**
  680. * @brief Disable region deciphering.
  681. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  682. * the configuration information for OTFDEC module
  683. * @param RegionIndex index of region the deciphering is disabled
  684. * @note An error is reported when the configuration is locked.
  685. * @retval HAL state
  686. */
  687. HAL_StatusTypeDef HAL_OTFDEC_RegionDisable(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
  688. {
  689. OTFDEC_Region_TypeDef * region;
  690. uint32_t address;
  691. /* Check the parameters */
  692. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  693. assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
  694. /* Take Lock */
  695. __HAL_LOCK(hotfdec);
  696. address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
  697. region = (OTFDEC_Region_TypeDef *)address;
  698. if (READ_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_LOCK_ENABLE) == OTFDEC_REG_CONFIGR_LOCK_ENABLE)
  699. {
  700. /* Configuration is locked, REG_EN bit can't be modified */
  701. __HAL_UNLOCK(hotfdec);
  702. return HAL_ERROR;
  703. }
  704. /* Disable region processing */
  705. CLEAR_BIT( region->REG_CONFIGR, OTFDEC_REG_CONFIGR_REG_ENABLE);
  706. /* Release Lock */
  707. __HAL_UNLOCK(hotfdec);
  708. /* Status is okay */
  709. return HAL_OK;
  710. }
  711. /**
  712. * @}
  713. */
  714. /** @defgroup OTFDEC_Exported_Functions_Group4 Peripheral State and Status functions
  715. * @brief Peripheral State functions.
  716. *
  717. @verbatim
  718. ==============================================================================
  719. ##### Peripheral State functions #####
  720. ==============================================================================
  721. [..]
  722. This subsection permits to get in run-time the status of the peripheral.
  723. @endverbatim
  724. * @{
  725. */
  726. /**
  727. * @brief Return the OTFDEC state.
  728. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  729. * the configuration information for OTFDEC module
  730. * @retval HAL state
  731. */
  732. HAL_OTFDEC_StateTypeDef HAL_OTFDEC_GetState(OTFDEC_HandleTypeDef *hotfdec)
  733. {
  734. return hotfdec->State;
  735. }
  736. /**
  737. * @brief Return region keys CRC.
  738. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  739. * the configuration information for OTFDEC module
  740. * @param RegionIndex index of region the keys CRC of which is read
  741. * @retval Key CRC
  742. */
  743. uint32_t HAL_OTFDEC_RegionGetKeyCRC(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex)
  744. {
  745. OTFDEC_Region_TypeDef * region;
  746. uint32_t address;
  747. uint32_t keycrc;
  748. /* Check the parameters */
  749. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  750. assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
  751. address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
  752. region = (OTFDEC_Region_TypeDef *)address;
  753. keycrc = (READ_REG( region->REG_CONFIGR )) & OTFDEC_REG_CONFIGR_KEYCRC;
  754. keycrc >>= OTFDEC_REG_CONFIGR_KEYCRC_Pos;
  755. return keycrc;
  756. }
  757. /**
  758. * @brief Return region configuration parameters.
  759. * @param hotfdec pointer to an OTFDEC_HandleTypeDef structure that contains
  760. * the configuration information for OTFDEC module
  761. * @param RegionIndex index of region the configuration of which is read
  762. * @param Config pointer on structure that will be filled up with the region configuration parameters
  763. * @retval HAL state
  764. */
  765. HAL_StatusTypeDef HAL_OTFDEC_RegionGetConfig(OTFDEC_HandleTypeDef *hotfdec, uint32_t RegionIndex, OTFDEC_RegionConfigTypeDef *Config)
  766. {
  767. OTFDEC_Region_TypeDef * region;
  768. uint32_t address;
  769. /* Check the parameters */
  770. assert_param(IS_OTFDEC_ALL_INSTANCE(hotfdec->Instance));
  771. assert_param(IS_OTFDEC_REGIONINDEX(RegionIndex));
  772. if (Config == NULL)
  773. {
  774. return HAL_ERROR;
  775. }
  776. else
  777. {
  778. /* Take Lock */
  779. __HAL_LOCK(hotfdec);
  780. address = (uint32_t)(hotfdec->Instance) + 0x20U + (0x30U * RegionIndex);
  781. region = (OTFDEC_Region_TypeDef *)address;
  782. /* Read Nonce */
  783. Config->Nonce[0] = READ_REG(region->REG_NONCER0);
  784. Config->Nonce[1] = READ_REG(region->REG_NONCER1);
  785. /* Read Addresses */
  786. Config->StartAddress = READ_REG(region->REG_START_ADDR);
  787. Config->EndAddress = READ_REG(region->REG_END_ADDR);
  788. /* Read Version */
  789. Config->Version = (uint16_t)(READ_REG(region->REG_CONFIGR) & OTFDEC_REG_CONFIGR_VERSION) >> OTFDEC_REG_CONFIGR_VERSION_Pos;
  790. /* Release Lock */
  791. __HAL_UNLOCK(hotfdec);
  792. /* Status is okay */
  793. return HAL_OK;
  794. }
  795. }
  796. /**
  797. * @}
  798. */
  799. /**
  800. * @}
  801. */
  802. #endif /* OTFDEC1 */
  803. #endif /* HAL_OTFDEC_MODULE_ENABLED */
  804. /**
  805. * @}
  806. */
  807. /**
  808. * @}
  809. */