stm32l4xx_hal_firewall.c 12 KB

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