stm32l4xx_hal_firewall.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_firewall.h
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  7. * @brief Header file of FIREWALL HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32L4xx_HAL_FIREWALL_H
  39. #define __STM32L4xx_HAL_FIREWALL_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32l4xx_hal_def.h"
  45. /** @addtogroup STM32L4xx_HAL_Driver
  46. * @{
  47. */
  48. /** @addtogroup FIREWALL FIREWALL
  49. * @{
  50. */
  51. /* Exported types ------------------------------------------------------------*/
  52. /** @defgroup FIREWALL_Exported_Types FIREWALL Exported Types
  53. * @{
  54. */
  55. /**
  56. * @brief FIREWALL Initialization Structure definition
  57. */
  58. typedef struct
  59. {
  60. uint32_t CodeSegmentStartAddress; /*!< Protected code segment start address. This value is 24-bit long, the 8 LSB bits are
  61. reserved and forced to 0 in order to allow a 256-byte granularity. */
  62. uint32_t CodeSegmentLength; /*!< Protected code segment length in bytes. This value is 22-bit long, the 8 LSB bits are
  63. reserved and forced to 0 for the length to be a multiple of 256 bytes. */
  64. uint32_t NonVDataSegmentStartAddress; /*!< Protected non-volatile data segment start address. This value is 24-bit long, the 8 LSB
  65. bits are reserved and forced to 0 in order to allow a 256-byte granularity. */
  66. uint32_t NonVDataSegmentLength; /*!< Protected non-volatile data segment length in bytes. This value is 22-bit long, the 8 LSB
  67. bits are reserved and forced to 0 for the length to be a multiple of 256 bytes. */
  68. uint32_t VDataSegmentStartAddress; /*!< Protected volatile data segment start address. This value is 17-bit long, the 6 LSB bits
  69. are reserved and forced to 0 in order to allow a 64-byte granularity. */
  70. uint32_t VDataSegmentLength; /*!< Protected volatile data segment length in bytes. This value is 17-bit long, the 6 LSB
  71. bits are reserved and forced to 0 for the length to be a multiple of 64 bytes. */
  72. uint32_t VolatileDataExecution; /*!< Set VDE bit specifying whether or not the volatile data segment can be executed.
  73. When VDS = 1 (set by parameter VolatileDataShared), VDE bit has no meaning.
  74. This parameter can be a value of @ref FIREWALL_VolatileData_Executable */
  75. uint32_t VolatileDataShared; /*!< Set VDS bit in specifying whether or not the volatile data segment can be shared with a
  76. non-protected application code.
  77. This parameter can be a value of @ref FIREWALL_VolatileData_Shared */
  78. }FIREWALL_InitTypeDef;
  79. /**
  80. * @}
  81. */
  82. /* Exported constants --------------------------------------------------------*/
  83. /** @defgroup FIREWALL_Exported_Constants FIREWALL Exported Constants
  84. * @{
  85. */
  86. /** @defgroup FIREWALL_VolatileData_Executable FIREWALL volatile data segment execution status
  87. * @{
  88. */
  89. #define FIREWALL_VOLATILEDATA_NOT_EXECUTABLE ((uint32_t)0x0000)
  90. #define FIREWALL_VOLATILEDATA_EXECUTABLE ((uint32_t)FW_CR_VDE)
  91. /**
  92. * @}
  93. */
  94. /** @defgroup FIREWALL_VolatileData_Shared FIREWALL volatile data segment share status
  95. * @{
  96. */
  97. #define FIREWALL_VOLATILEDATA_NOT_SHARED ((uint32_t)0x0000)
  98. #define FIREWALL_VOLATILEDATA_SHARED ((uint32_t)FW_CR_VDS)
  99. /**
  100. * @}
  101. */
  102. /** @defgroup FIREWALL_Pre_Arm FIREWALL pre arm status
  103. * @{
  104. */
  105. #define FIREWALL_PRE_ARM_RESET ((uint32_t)0x0000)
  106. #define FIREWALL_PRE_ARM_SET ((uint32_t)FW_CR_FPA)
  107. /**
  108. * @}
  109. */
  110. /**
  111. * @}
  112. */
  113. /* Private macros --------------------------------------------------------*/
  114. /** @defgroup FIREWALL_Private_Macros FIREWALL Private Macros
  115. * @{
  116. */
  117. #define IS_FIREWALL_CODE_SEGMENT_ADDRESS(ADDRESS) (((ADDRESS) >= FLASH_BASE) && ((ADDRESS) < (FLASH_BASE + FLASH_SIZE)))
  118. #define IS_FIREWALL_CODE_SEGMENT_LENGTH(ADDRESS, LENGTH) (((ADDRESS) + (LENGTH)) <= (FLASH_BASE + FLASH_SIZE))
  119. #define IS_FIREWALL_NONVOLATILEDATA_SEGMENT_ADDRESS(ADDRESS) (((ADDRESS) >= FLASH_BASE) && ((ADDRESS) < (FLASH_BASE + FLASH_SIZE)))
  120. #define IS_FIREWALL_NONVOLATILEDATA_SEGMENT_LENGTH(ADDRESS, LENGTH) (((ADDRESS) + (LENGTH)) <= (FLASH_BASE + FLASH_SIZE))
  121. #define IS_FIREWALL_VOLATILEDATA_SEGMENT_ADDRESS(ADDRESS) (((ADDRESS) >= SRAM1_BASE) && ((ADDRESS) < (SRAM1_BASE + SRAM1_SIZE_MAX)))
  122. #define IS_FIREWALL_VOLATILEDATA_SEGMENT_LENGTH(ADDRESS, LENGTH) (((ADDRESS) + (LENGTH)) <= (SRAM1_BASE + SRAM1_SIZE_MAX))
  123. #define IS_FIREWALL_VOLATILEDATA_SHARE(SHARE) (((SHARE) == FIREWALL_VOLATILEDATA_NOT_SHARED) || \
  124. ((SHARE) == FIREWALL_VOLATILEDATA_SHARED))
  125. #define IS_FIREWALL_VOLATILEDATA_EXECUTE(EXECUTE) (((EXECUTE) == FIREWALL_VOLATILEDATA_NOT_EXECUTABLE) || \
  126. ((EXECUTE) == FIREWALL_VOLATILEDATA_EXECUTABLE))
  127. /**
  128. * @}
  129. */
  130. /* Exported macros -----------------------------------------------------------*/
  131. /** @defgroup FIREWALL_Exported_Macros FIREWALL Exported Macros
  132. * @{
  133. */
  134. /** @brief Check whether the FIREWALL is enabled or not.
  135. * @retval FIREWALL enabling status (TRUE or FALSE).
  136. */
  137. #define __HAL_FIREWALL_IS_ENABLED() HAL_IS_BIT_CLR(SYSCFG->CFGR1, SYSCFG_CFGR1_FWDIS)
  138. /** @brief Enable FIREWALL pre arm.
  139. * @note When FPA bit is set, any code executed outside the protected segment
  140. * closes the Firewall, otherwise it generates a system reset.
  141. * @note This macro provides the same service as HAL_FIREWALL_EnablePreArmFlag() API
  142. * but can be executed inside a code area protected by the Firewall.
  143. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  144. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  145. * 0, that is, when the non volatile data segment is defined), the macro can be
  146. * executed only when the Firewall is opened.
  147. */
  148. #define __HAL_FIREWALL_PREARM_ENABLE() \
  149. do { \
  150. __IO uint32_t tmpreg; \
  151. SET_BIT(FIREWALL->CR, FW_CR_FPA) ; \
  152. /* Read bit back to ensure it is taken into account by IP */ \
  153. /* (introduce proper delay inside macro execution) */ \
  154. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_FPA) ; \
  155. UNUSED(tmpreg); \
  156. } while(0)
  157. /** @brief Disable FIREWALL pre arm.
  158. * @note When FPA bit is set, any code executed outside the protected segment
  159. * closes the Firewall, otherwise, it generates a system reset.
  160. * @note This macro provides the same service as HAL_FIREWALL_DisablePreArmFlag() API
  161. * but can be executed inside a code area protected by the Firewall.
  162. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  163. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  164. * 0, that is, when the non volatile data segment is defined), the macro can be
  165. * executed only when the Firewall is opened.
  166. */
  167. #define __HAL_FIREWALL_PREARM_DISABLE() \
  168. do { \
  169. __IO uint32_t tmpreg; \
  170. CLEAR_BIT(FIREWALL->CR, FW_CR_FPA) ; \
  171. /* Read bit back to ensure it is taken into account by IP */ \
  172. /* (introduce proper delay inside macro execution) */ \
  173. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_FPA) ; \
  174. UNUSED(tmpreg); \
  175. } while(0)
  176. /** @brief Enable volatile data sharing in setting VDS bit.
  177. * @note When VDS bit is set, the volatile data segment is shared with non-protected
  178. * application code. It can be accessed whatever the Firewall state (opened or closed).
  179. * @note This macro can be executed inside a code area protected by the Firewall.
  180. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  181. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  182. * 0, that is, when the non volatile data segment is defined), the macro can be
  183. * executed only when the Firewall is opened.
  184. */
  185. #define __HAL_FIREWALL_VOLATILEDATA_SHARED_ENABLE() \
  186. do { \
  187. __IO uint32_t tmpreg; \
  188. SET_BIT(FIREWALL->CR, FW_CR_VDS) ; \
  189. /* Read bit back to ensure it is taken into account by IP */ \
  190. /* (introduce proper delay inside macro execution) */ \
  191. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_VDS) ; \
  192. UNUSED(tmpreg); \
  193. } while(0)
  194. /** @brief Disable volatile data sharing in resetting VDS bit.
  195. * @note When VDS bit is reset, the volatile data segment is not shared and cannot be
  196. * hit by a non protected executable code when the Firewall is closed. If it is
  197. * accessed in such a condition, a system reset is generated by the Firewall.
  198. * @note This macro can be executed inside a code area protected by the Firewall.
  199. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  200. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  201. * 0, that is, when the non volatile data segment is defined), the macro can be
  202. * executed only when the Firewall is opened.
  203. */
  204. #define __HAL_FIREWALL_VOLATILEDATA_SHARED_DISABLE() \
  205. do { \
  206. __IO uint32_t tmpreg; \
  207. CLEAR_BIT(FIREWALL->CR, FW_CR_VDS) ; \
  208. /* Read bit back to ensure it is taken into account by IP */ \
  209. /* (introduce proper delay inside macro execution) */ \
  210. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_VDS) ; \
  211. UNUSED(tmpreg); \
  212. } while(0)
  213. /** @brief Enable volatile data execution in setting VDE bit.
  214. * @note VDE bit is ignored when VDS is set. IF VDS = 1, the Volatile data segment can be
  215. * executed whatever the VDE bit value.
  216. * @note When VDE bit is set (with VDS = 0), the volatile data segment is executable. When
  217. * the Firewall call is closed, a "call gate" entry procedure is required to open
  218. * first the Firewall.
  219. * @note This macro can be executed inside a code area protected by the Firewall.
  220. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  221. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  222. * 0, that is, when the non volatile data segment is defined), the macro can be
  223. * executed only when the Firewall is opened.
  224. */
  225. #define __HAL_FIREWALL_VOLATILEDATA_EXECUTION_ENABLE() \
  226. do { \
  227. __IO uint32_t tmpreg; \
  228. SET_BIT(FIREWALL->CR, FW_CR_VDE) ; \
  229. /* Read bit back to ensure it is taken into account by IP */ \
  230. /* (introduce proper delay inside macro execution) */ \
  231. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_VDE) ; \
  232. UNUSED(tmpreg); \
  233. } while(0)
  234. /** @brief Disable volatile data execution in resetting VDE bit.
  235. * @note VDE bit is ignored when VDS is set. IF VDS = 1, the Volatile data segment can be
  236. * executed whatever the VDE bit value.
  237. * @note When VDE bit is reset (with VDS = 0), the volatile data segment cannot be executed.
  238. * @note This macro can be executed inside a code area protected by the Firewall.
  239. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  240. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  241. * 0, that is, when the non volatile data segment is defined), the macro can be
  242. * executed only when the Firewall is opened.
  243. */
  244. #define __HAL_FIREWALL_VOLATILEDATA_EXECUTION_DISABLE() \
  245. do { \
  246. __IO uint32_t tmpreg; \
  247. CLEAR_BIT(FIREWALL->CR, FW_CR_VDE) ; \
  248. /* Read bit back to ensure it is taken into account by IP */ \
  249. /* (introduce proper delay inside macro execution) */ \
  250. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_VDE) ; \
  251. UNUSED(tmpreg); \
  252. } while(0)
  253. /** @brief Check whether or not the volatile data segment is shared.
  254. * @note This macro can be executed inside a code area protected by the Firewall.
  255. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  256. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  257. * 0, that is, when the non volatile data segment is defined), the macro can be
  258. * executed only when the Firewall is opened.
  259. * @retval VDS bit setting status (TRUE or FALSE).
  260. */
  261. #define __HAL_FIREWALL_GET_VOLATILEDATA_SHARED() ((FIREWALL->CR & FW_CR_VDS) == FW_CR_VDS)
  262. /** @brief Check whether or not the volatile data segment is declared executable.
  263. * @note This macro can be executed inside a code area protected by the Firewall.
  264. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  265. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  266. * 0, that is, when the non volatile data segment is defined), the macro can be
  267. * executed only when the Firewall is opened.
  268. * @retval VDE bit setting status (TRUE or FALSE).
  269. */
  270. #define __HAL_FIREWALL_GET_VOLATILEDATA_EXECUTION() ((FIREWALL->CR & FW_CR_VDE) == FW_CR_VDE)
  271. /** @brief Check whether or not the Firewall pre arm bit is set.
  272. * @note This macro can be executed inside a code area protected by the Firewall.
  273. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  274. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  275. * 0, that is, when the non volatile data segment is defined), the macro can be
  276. * executed only when the Firewall is opened.
  277. * @retval FPA bit setting status (TRUE or FALSE).
  278. */
  279. #define __HAL_FIREWALL_GET_PREARM() ((FIREWALL->CR & FW_CR_FPA) == FW_CR_FPA)
  280. /**
  281. * @}
  282. */
  283. /* Exported functions --------------------------------------------------------*/
  284. /** @addtogroup FIREWALL_Exported_Functions FIREWALL Exported Functions
  285. * @{
  286. */
  287. /** @addtogroup FIREWALL_Exported_Functions_Group1 Initialization Functions
  288. * @brief Initialization and Configuration Functions
  289. * @{
  290. */
  291. /* Initialization functions ********************************/
  292. HAL_StatusTypeDef HAL_FIREWALL_Config(FIREWALL_InitTypeDef * fw_init);
  293. void HAL_FIREWALL_GetConfig(FIREWALL_InitTypeDef * fw_config);
  294. void HAL_FIREWALL_EnableFirewall(void);
  295. void HAL_FIREWALL_EnablePreArmFlag(void);
  296. void HAL_FIREWALL_DisablePreArmFlag(void);
  297. /**
  298. * @}
  299. */
  300. /**
  301. * @}
  302. */
  303. /**
  304. * @}
  305. */
  306. /**
  307. * @}
  308. */
  309. #ifdef __cplusplus
  310. }
  311. #endif
  312. #endif /* __STM32L4xx_HAL_FIREWALL_H */
  313. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/