stm32l4xx_hal_firewall.h 19 KB

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