stm32l4xx_hal_firewall.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_firewall.c
  4. * @author MCD Application Team
  5. * @brief FIREWALL HAL module driver.
  6. * This file provides firmware functions to manage the Firewall
  7. * Peripheral initialization and enabling.
  8. *
  9. *
  10. @verbatim
  11. ===============================================================================
  12. ##### How to use this driver #####
  13. ===============================================================================
  14. [..]
  15. The FIREWALL HAL driver can be used as follows:
  16. (#) Declare a FIREWALL_InitTypeDef initialization structure.
  17. (#) Resort to HAL_FIREWALL_Config() API to initialize the Firewall
  18. (#) Enable the FIREWALL in calling HAL_FIREWALL_EnableFirewall() API
  19. (#) To ensure that any code executed outside the protected segment closes the
  20. FIREWALL, the user must set the flag FIREWALL_PRE_ARM_SET in calling
  21. __HAL_FIREWALL_PREARM_ENABLE() macro if called within a protected code segment
  22. or
  23. HAL_FIREWALL_EnablePreArmFlag() API if called outside of protected code segment
  24. after HAL_FIREWALL_Config() call.
  25. @endverbatim
  26. ******************************************************************************
  27. * @attention
  28. *
  29. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  30. *
  31. * Redistribution and use in source and binary forms, with or without modification,
  32. * are permitted provided that the following conditions are met:
  33. * 1. Redistributions of source code must retain the above copyright notice,
  34. * this list of conditions and the following disclaimer.
  35. * 2. Redistributions in binary form must reproduce the above copyright notice,
  36. * this list of conditions and the following disclaimer in the documentation
  37. * and/or other materials provided with the distribution.
  38. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  39. * may be used to endorse or promote products derived from this software
  40. * without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  43. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  45. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  46. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  47. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  48. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  49. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  51. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52. *
  53. ******************************************************************************
  54. */
  55. /* Includes ------------------------------------------------------------------*/
  56. #include "stm32l4xx_hal.h"
  57. /** @addtogroup STM32L4xx_HAL_Driver
  58. * @{
  59. */
  60. /** @defgroup FIREWALL FIREWALL
  61. * @brief HAL FIREWALL module driver
  62. * @{
  63. */
  64. #ifdef HAL_FIREWALL_MODULE_ENABLED
  65. /* Private typedef -----------------------------------------------------------*/
  66. /* Private define ------------------------------------------------------------*/
  67. /* Private macro -------------------------------------------------------------*/
  68. /* Private variables ---------------------------------------------------------*/
  69. /* Private function prototypes -----------------------------------------------*/
  70. /* Private functions ---------------------------------------------------------*/
  71. /** @defgroup FIREWALL_Exported_Functions FIREWALL Exported Functions
  72. * @{
  73. */
  74. /** @defgroup FIREWALL_Exported_Functions_Group1 Initialization Functions
  75. * @brief Initialization and Configuration Functions
  76. *
  77. @verbatim
  78. ===============================================================================
  79. ##### Initialization and Configuration functions #####
  80. ===============================================================================
  81. [..]
  82. This subsection provides the functions allowing to initialize the Firewall.
  83. Initialization is done by HAL_FIREWALL_Config():
  84. (+) Enable the Firewall clock thru __HAL_RCC_FIREWALL_CLK_ENABLE() macro.
  85. (+) Set the protected code segment address start and length.
  86. (+) Set the protected non-volatile and/or volatile data segments
  87. address starts and lengths if applicable.
  88. (+) Set the volatile data segment execution and sharing status.
  89. (+) Length must be set to 0 for an unprotected segment.
  90. @endverbatim
  91. * @{
  92. */
  93. /**
  94. * @brief Initialize the Firewall according to the FIREWALL_InitTypeDef structure parameters.
  95. * @param fw_init: Firewall initialization structure
  96. * @note The API returns HAL_ERROR if the Firewall is already enabled.
  97. * @retval HAL status
  98. */
  99. HAL_StatusTypeDef HAL_FIREWALL_Config(FIREWALL_InitTypeDef * fw_init)
  100. {
  101. /* Check the Firewall initialization structure allocation */
  102. if(fw_init == NULL)
  103. {
  104. return HAL_ERROR;
  105. }
  106. /* Enable Firewall clock */
  107. __HAL_RCC_FIREWALL_CLK_ENABLE();
  108. /* Make sure that Firewall is not enabled already */
  109. if (__HAL_FIREWALL_IS_ENABLED() != RESET)
  110. {
  111. return HAL_ERROR;
  112. }
  113. /* Check Firewall configuration addresses and lengths when segment is protected */
  114. /* Code segment */
  115. if (fw_init->CodeSegmentLength != 0)
  116. {
  117. assert_param(IS_FIREWALL_CODE_SEGMENT_ADDRESS(fw_init->CodeSegmentStartAddress));
  118. assert_param(IS_FIREWALL_CODE_SEGMENT_LENGTH(fw_init->CodeSegmentStartAddress, fw_init->CodeSegmentLength));
  119. /* Make sure that NonVDataSegmentLength is properly set to prevent code segment access */
  120. if (fw_init->NonVDataSegmentLength < 0x100)
  121. {
  122. return HAL_ERROR;
  123. }
  124. }
  125. /* Non volatile data segment */
  126. if (fw_init->NonVDataSegmentLength != 0)
  127. {
  128. assert_param(IS_FIREWALL_NONVOLATILEDATA_SEGMENT_ADDRESS(fw_init->NonVDataSegmentStartAddress));
  129. assert_param(IS_FIREWALL_NONVOLATILEDATA_SEGMENT_LENGTH(fw_init->NonVDataSegmentStartAddress, fw_init->NonVDataSegmentLength));
  130. }
  131. /* Volatile data segment */
  132. if (fw_init->VDataSegmentLength != 0)
  133. {
  134. assert_param(IS_FIREWALL_VOLATILEDATA_SEGMENT_ADDRESS(fw_init->VDataSegmentStartAddress));
  135. assert_param(IS_FIREWALL_VOLATILEDATA_SEGMENT_LENGTH(fw_init->VDataSegmentStartAddress, fw_init->VDataSegmentLength));
  136. }
  137. /* Check Firewall Configuration Register parameters */
  138. assert_param(IS_FIREWALL_VOLATILEDATA_EXECUTE(fw_init->VolatileDataExecution));
  139. assert_param(IS_FIREWALL_VOLATILEDATA_SHARE(fw_init->VolatileDataShared));
  140. /* Configuration */
  141. /* Protected code segment start address configuration */
  142. WRITE_REG(FIREWALL->CSSA, (FW_CSSA_ADD & fw_init->CodeSegmentStartAddress));
  143. /* Protected code segment length configuration */
  144. WRITE_REG(FIREWALL->CSL, (FW_CSL_LENG & fw_init->CodeSegmentLength));
  145. /* Protected non volatile data segment start address configuration */
  146. WRITE_REG(FIREWALL->NVDSSA, (FW_NVDSSA_ADD & fw_init->NonVDataSegmentStartAddress));
  147. /* Protected non volatile data segment length configuration */
  148. WRITE_REG(FIREWALL->NVDSL, (FW_NVDSL_LENG & fw_init->NonVDataSegmentLength));
  149. /* Protected volatile data segment start address configuration */
  150. WRITE_REG(FIREWALL->VDSSA, (FW_VDSSA_ADD & fw_init->VDataSegmentStartAddress));
  151. /* Protected volatile data segment length configuration */
  152. WRITE_REG(FIREWALL->VDSL, (FW_VDSL_LENG & fw_init->VDataSegmentLength));
  153. /* Set Firewall Configuration Register VDE and VDS bits
  154. (volatile data execution and shared configuration) */
  155. MODIFY_REG(FIREWALL->CR, FW_CR_VDS|FW_CR_VDE, fw_init->VolatileDataExecution|fw_init->VolatileDataShared);
  156. return HAL_OK;
  157. }
  158. /**
  159. * @brief Retrieve the Firewall configuration.
  160. * @param fw_config: Firewall configuration, type is same as initialization structure
  161. * @note This API can't be executed inside a code area protected by the Firewall
  162. * when the Firewall is enabled
  163. * @note If NVDSL register is different from 0, that is, if the non volatile data segment
  164. * is defined, this API can't be executed when the Firewall is enabled.
  165. * @note User should resort to __HAL_FIREWALL_GET_PREARM() macro to retrieve FPA bit status
  166. * @retval None
  167. */
  168. void HAL_FIREWALL_GetConfig(FIREWALL_InitTypeDef * fw_config)
  169. {
  170. /* Enable Firewall clock, in case no Firewall configuration has been carried
  171. out up to this point */
  172. __HAL_RCC_FIREWALL_CLK_ENABLE();
  173. /* Retrieve code segment protection setting */
  174. fw_config->CodeSegmentStartAddress = (READ_REG(FIREWALL->CSSA) & FW_CSSA_ADD);
  175. fw_config->CodeSegmentLength = (READ_REG(FIREWALL->CSL) & FW_CSL_LENG);
  176. /* Retrieve non volatile data segment protection setting */
  177. fw_config->NonVDataSegmentStartAddress = (READ_REG(FIREWALL->NVDSSA) & FW_NVDSSA_ADD);
  178. fw_config->NonVDataSegmentLength = (READ_REG(FIREWALL->NVDSL) & FW_NVDSL_LENG);
  179. /* Retrieve volatile data segment protection setting */
  180. fw_config->VDataSegmentStartAddress = (READ_REG(FIREWALL->VDSSA) & FW_VDSSA_ADD);
  181. fw_config->VDataSegmentLength = (READ_REG(FIREWALL->VDSL) & FW_VDSL_LENG);
  182. /* Retrieve volatile data execution setting */
  183. fw_config->VolatileDataExecution = (READ_REG(FIREWALL->CR) & FW_CR_VDE);
  184. /* Retrieve volatile data shared setting */
  185. fw_config->VolatileDataShared = (READ_REG(FIREWALL->CR) & FW_CR_VDS);
  186. return;
  187. }
  188. /**
  189. * @brief Enable FIREWALL.
  190. * @note Firewall is enabled in clearing FWDIS bit of SYSCFG CFGR1 register.
  191. * Once enabled, the Firewall cannot be disabled by software. Only a
  192. * system reset can set again FWDIS bit.
  193. * @retval None
  194. */
  195. void HAL_FIREWALL_EnableFirewall(void)
  196. {
  197. /* Clears FWDIS bit of SYSCFG CFGR1 register */
  198. CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_FWDIS);
  199. }
  200. /**
  201. * @brief Enable FIREWALL pre arm.
  202. * @note When FPA bit is set, any code executed outside the protected segment
  203. * will close the Firewall.
  204. * @note This API provides the same service as __HAL_FIREWALL_PREARM_ENABLE() macro
  205. * but can't be executed inside a code area protected by the Firewall.
  206. * @note When the Firewall is disabled, user can resort to HAL_FIREWALL_EnablePreArmFlag() API any time.
  207. * @note When the Firewall is enabled and NVDSL register is equal to 0 (that is,
  208. * when the non volatile data segment is not defined),
  209. * ** this API can be executed when the Firewall is closed
  210. * ** when the Firewall is opened, user should resort to
  211. * __HAL_FIREWALL_PREARM_ENABLE() macro instead
  212. * @note When the Firewall is enabled and NVDSL register is different from 0
  213. * (that is, when the non volatile data segment is defined)
  214. * ** FW_CR register can be accessed only when the Firewall is opened:
  215. * user should resort to __HAL_FIREWALL_PREARM_ENABLE() macro instead.
  216. * @retval None
  217. */
  218. void HAL_FIREWALL_EnablePreArmFlag(void)
  219. {
  220. /* Set FPA bit */
  221. SET_BIT(FIREWALL->CR, FW_CR_FPA);
  222. }
  223. /**
  224. * @brief Disable FIREWALL pre arm.
  225. * @note When FPA bit is reset, any code executed outside the protected segment
  226. * when the Firewall is opened will generate a system reset.
  227. * @note This API provides the same service as __HAL_FIREWALL_PREARM_DISABLE() macro
  228. * but can't be executed inside a code area protected by the Firewall.
  229. * @note When the Firewall is disabled, user can resort to HAL_FIREWALL_EnablePreArmFlag() API any time.
  230. * @note When the Firewall is enabled and NVDSL register is equal to 0 (that is,
  231. * when the non volatile data segment is not defined),
  232. * ** this API can be executed when the Firewall is closed
  233. * ** when the Firewall is opened, user should resort to
  234. * __HAL_FIREWALL_PREARM_DISABLE() macro instead
  235. * @note When the Firewall is enabled and NVDSL register is different from 0
  236. * (that is, when the non volatile data segment is defined)
  237. * ** FW_CR register can be accessed only when the Firewall is opened:
  238. * user should resort to __HAL_FIREWALL_PREARM_DISABLE() macro instead.
  239. * @retval None
  240. */
  241. void HAL_FIREWALL_DisablePreArmFlag(void)
  242. {
  243. /* Clear FPA bit */
  244. CLEAR_BIT(FIREWALL->CR, FW_CR_FPA);
  245. }
  246. /**
  247. * @}
  248. */
  249. /**
  250. * @}
  251. */
  252. #endif /* HAL_FIREWALL_MODULE_ENABLED */
  253. /**
  254. * @}
  255. */
  256. /**
  257. * @}
  258. */
  259. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/