stm32l4xx_hal_firewall.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. * Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* Define to prevent recursive inclusion -------------------------------------*/
  19. #ifndef STM32L4xx_HAL_FIREWALL_H
  20. #define STM32L4xx_HAL_FIREWALL_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "stm32l4xx_hal_def.h"
  26. /** @addtogroup STM32L4xx_HAL_Driver
  27. * @{
  28. */
  29. /** @addtogroup FIREWALL FIREWALL
  30. * @{
  31. */
  32. /* Exported types ------------------------------------------------------------*/
  33. /** @defgroup FIREWALL_Exported_Types FIREWALL Exported Types
  34. * @{
  35. */
  36. /**
  37. * @brief FIREWALL Initialization Structure definition
  38. */
  39. typedef struct
  40. {
  41. uint32_t CodeSegmentStartAddress; /*!< Protected code segment start address. This value is 24-bit long, the 8 LSB bits are
  42. reserved and forced to 0 in order to allow a 256-byte granularity. */
  43. uint32_t CodeSegmentLength; /*!< Protected code segment length in bytes. This value is 22-bit long, the 8 LSB bits are
  44. reserved and forced to 0 for the length to be a multiple of 256 bytes. */
  45. uint32_t NonVDataSegmentStartAddress; /*!< Protected non-volatile data segment start address. This value is 24-bit long, the 8 LSB
  46. bits are reserved and forced to 0 in order to allow a 256-byte granularity. */
  47. uint32_t NonVDataSegmentLength; /*!< Protected non-volatile data segment length in bytes. This value is 22-bit long, the 8 LSB
  48. bits are reserved and forced to 0 for the length to be a multiple of 256 bytes. */
  49. uint32_t VDataSegmentStartAddress; /*!< Protected volatile data segment start address. This value is 17-bit long, the 6 LSB bits
  50. are reserved and forced to 0 in order to allow a 64-byte granularity. */
  51. uint32_t VDataSegmentLength; /*!< Protected volatile data segment length in bytes. This value is 17-bit long, the 6 LSB
  52. bits are reserved and forced to 0 for the length to be a multiple of 64 bytes. */
  53. uint32_t VolatileDataExecution; /*!< Set VDE bit specifying whether or not the volatile data segment can be executed.
  54. When VDS = 1 (set by parameter VolatileDataShared), VDE bit has no meaning.
  55. This parameter can be a value of @ref FIREWALL_VolatileData_Executable */
  56. uint32_t VolatileDataShared; /*!< Set VDS bit in specifying whether or not the volatile data segment can be shared with a
  57. non-protected application code.
  58. This parameter can be a value of @ref FIREWALL_VolatileData_Shared */
  59. }FIREWALL_InitTypeDef;
  60. /**
  61. * @}
  62. */
  63. /* Exported constants --------------------------------------------------------*/
  64. /** @defgroup FIREWALL_Exported_Constants FIREWALL Exported Constants
  65. * @{
  66. */
  67. /** @defgroup FIREWALL_VolatileData_Executable FIREWALL volatile data segment execution status
  68. * @{
  69. */
  70. #define FIREWALL_VOLATILEDATA_NOT_EXECUTABLE ((uint32_t)0x0000)
  71. #define FIREWALL_VOLATILEDATA_EXECUTABLE ((uint32_t)FW_CR_VDE)
  72. /**
  73. * @}
  74. */
  75. /** @defgroup FIREWALL_VolatileData_Shared FIREWALL volatile data segment share status
  76. * @{
  77. */
  78. #define FIREWALL_VOLATILEDATA_NOT_SHARED ((uint32_t)0x0000)
  79. #define FIREWALL_VOLATILEDATA_SHARED ((uint32_t)FW_CR_VDS)
  80. /**
  81. * @}
  82. */
  83. /** @defgroup FIREWALL_Pre_Arm FIREWALL pre arm status
  84. * @{
  85. */
  86. #define FIREWALL_PRE_ARM_RESET ((uint32_t)0x0000)
  87. #define FIREWALL_PRE_ARM_SET ((uint32_t)FW_CR_FPA)
  88. /**
  89. * @}
  90. */
  91. /**
  92. * @}
  93. */
  94. /* Private macros --------------------------------------------------------*/
  95. /** @defgroup FIREWALL_Private_Macros FIREWALL Private Macros
  96. * @{
  97. */
  98. #define IS_FIREWALL_CODE_SEGMENT_ADDRESS(ADDRESS) (((ADDRESS) >= FLASH_BASE) && ((ADDRESS) < (FLASH_BASE + FLASH_SIZE)))
  99. #define IS_FIREWALL_CODE_SEGMENT_LENGTH(ADDRESS, LENGTH) (((ADDRESS) + (LENGTH)) <= (FLASH_BASE + FLASH_SIZE))
  100. #define IS_FIREWALL_NONVOLATILEDATA_SEGMENT_ADDRESS(ADDRESS) (((ADDRESS) >= FLASH_BASE) && ((ADDRESS) < (FLASH_BASE + FLASH_SIZE)))
  101. #define IS_FIREWALL_NONVOLATILEDATA_SEGMENT_LENGTH(ADDRESS, LENGTH) (((ADDRESS) + (LENGTH)) <= (FLASH_BASE + FLASH_SIZE))
  102. #define IS_FIREWALL_VOLATILEDATA_SEGMENT_ADDRESS(ADDRESS) (((ADDRESS) >= SRAM1_BASE) && ((ADDRESS) < (SRAM1_BASE + SRAM1_SIZE_MAX)))
  103. #define IS_FIREWALL_VOLATILEDATA_SEGMENT_LENGTH(ADDRESS, LENGTH) (((ADDRESS) + (LENGTH)) <= (SRAM1_BASE + SRAM1_SIZE_MAX))
  104. #define IS_FIREWALL_VOLATILEDATA_SHARE(SHARE) (((SHARE) == FIREWALL_VOLATILEDATA_NOT_SHARED) || \
  105. ((SHARE) == FIREWALL_VOLATILEDATA_SHARED))
  106. #define IS_FIREWALL_VOLATILEDATA_EXECUTE(EXECUTE) (((EXECUTE) == FIREWALL_VOLATILEDATA_NOT_EXECUTABLE) || \
  107. ((EXECUTE) == FIREWALL_VOLATILEDATA_EXECUTABLE))
  108. /**
  109. * @}
  110. */
  111. /* Exported macros -----------------------------------------------------------*/
  112. /** @defgroup FIREWALL_Exported_Macros FIREWALL Exported Macros
  113. * @{
  114. */
  115. /** @brief Check whether the FIREWALL is enabled or not.
  116. * @retval FIREWALL enabling status (TRUE or FALSE).
  117. */
  118. #define __HAL_FIREWALL_IS_ENABLED() HAL_IS_BIT_CLR(SYSCFG->CFGR1, SYSCFG_CFGR1_FWDIS)
  119. /** @brief Enable FIREWALL pre arm.
  120. * @note When FPA bit is set, any code executed outside the protected segment
  121. * closes the Firewall, otherwise it generates a system reset.
  122. * @note This macro provides the same service as HAL_FIREWALL_EnablePreArmFlag() API
  123. * but can be executed inside a code area protected by the Firewall.
  124. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  125. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  126. * 0, that is, when the non volatile data segment is defined), the macro can be
  127. * executed only when the Firewall is opened.
  128. */
  129. #define __HAL_FIREWALL_PREARM_ENABLE() \
  130. do { \
  131. __IO uint32_t tmpreg; \
  132. SET_BIT(FIREWALL->CR, FW_CR_FPA) ; \
  133. /* Read bit back to ensure it is taken into account by Peripheral */ \
  134. /* (introduce proper delay inside macro execution) */ \
  135. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_FPA) ; \
  136. UNUSED(tmpreg); \
  137. } while(0)
  138. /** @brief Disable 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_DisablePreArmFlag() 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_DISABLE() \
  149. do { \
  150. __IO uint32_t tmpreg; \
  151. CLEAR_BIT(FIREWALL->CR, FW_CR_FPA) ; \
  152. /* Read bit back to ensure it is taken into account by Peripheral */ \
  153. /* (introduce proper delay inside macro execution) */ \
  154. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_FPA) ; \
  155. UNUSED(tmpreg); \
  156. } while(0)
  157. /** @brief Enable volatile data sharing in setting VDS bit.
  158. * @note When VDS bit is set, the volatile data segment is shared with non-protected
  159. * application code. It can be accessed whatever the Firewall state (opened or closed).
  160. * @note This macro can be executed inside a code area protected by the Firewall.
  161. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  162. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  163. * 0, that is, when the non volatile data segment is defined), the macro can be
  164. * executed only when the Firewall is opened.
  165. */
  166. #define __HAL_FIREWALL_VOLATILEDATA_SHARED_ENABLE() \
  167. do { \
  168. __IO uint32_t tmpreg; \
  169. SET_BIT(FIREWALL->CR, FW_CR_VDS) ; \
  170. /* Read bit back to ensure it is taken into account by Peripheral */ \
  171. /* (introduce proper delay inside macro execution) */ \
  172. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_VDS) ; \
  173. UNUSED(tmpreg); \
  174. } while(0)
  175. /** @brief Disable volatile data sharing in resetting VDS bit.
  176. * @note When VDS bit is reset, the volatile data segment is not shared and cannot be
  177. * hit by a non protected executable code when the Firewall is closed. If it is
  178. * accessed in such a condition, a system reset is generated by the Firewall.
  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_DISABLE() \
  186. do { \
  187. __IO uint32_t tmpreg; \
  188. CLEAR_BIT(FIREWALL->CR, FW_CR_VDS) ; \
  189. /* Read bit back to ensure it is taken into account by Peripheral */ \
  190. /* (introduce proper delay inside macro execution) */ \
  191. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_VDS) ; \
  192. UNUSED(tmpreg); \
  193. } while(0)
  194. /** @brief Enable volatile data execution in setting VDE bit.
  195. * @note VDE bit is ignored when VDS is set. IF VDS = 1, the Volatile data segment can be
  196. * executed whatever the VDE bit value.
  197. * @note When VDE bit is set (with VDS = 0), the volatile data segment is executable. When
  198. * the Firewall call is closed, a "call gate" entry procedure is required to open
  199. * first the Firewall.
  200. * @note This macro can be executed inside a code area protected by the Firewall.
  201. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  202. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  203. * 0, that is, when the non volatile data segment is defined), the macro can be
  204. * executed only when the Firewall is opened.
  205. */
  206. #define __HAL_FIREWALL_VOLATILEDATA_EXECUTION_ENABLE() \
  207. do { \
  208. __IO uint32_t tmpreg; \
  209. SET_BIT(FIREWALL->CR, FW_CR_VDE) ; \
  210. /* Read bit back to ensure it is taken into account by Peripheral */ \
  211. /* (introduce proper delay inside macro execution) */ \
  212. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_VDE) ; \
  213. UNUSED(tmpreg); \
  214. } while(0)
  215. /** @brief Disable volatile data execution in resetting VDE bit.
  216. * @note VDE bit is ignored when VDS is set. IF VDS = 1, the Volatile data segment can be
  217. * executed whatever the VDE bit value.
  218. * @note When VDE bit is reset (with VDS = 0), the volatile data segment cannot be executed.
  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_DISABLE() \
  226. do { \
  227. __IO uint32_t tmpreg; \
  228. CLEAR_BIT(FIREWALL->CR, FW_CR_VDE) ; \
  229. /* Read bit back to ensure it is taken into account by Peripheral */ \
  230. /* (introduce proper delay inside macro execution) */ \
  231. tmpreg = READ_BIT(FIREWALL->CR, FW_CR_VDE) ; \
  232. UNUSED(tmpreg); \
  233. } while(0)
  234. /** @brief Check whether or not the volatile data segment is shared.
  235. * @note This macro can be executed inside a code area protected by the Firewall.
  236. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  237. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  238. * 0, that is, when the non volatile data segment is defined), the macro can be
  239. * executed only when the Firewall is opened.
  240. * @retval VDS bit setting status (TRUE or FALSE).
  241. */
  242. #define __HAL_FIREWALL_GET_VOLATILEDATA_SHARED() ((FIREWALL->CR & FW_CR_VDS) == FW_CR_VDS)
  243. /** @brief Check whether or not the volatile data segment is declared executable.
  244. * @note This macro can be executed inside a code area protected by the Firewall.
  245. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  246. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  247. * 0, that is, when the non volatile data segment is defined), the macro can be
  248. * executed only when the Firewall is opened.
  249. * @retval VDE bit setting status (TRUE or FALSE).
  250. */
  251. #define __HAL_FIREWALL_GET_VOLATILEDATA_EXECUTION() ((FIREWALL->CR & FW_CR_VDE) == FW_CR_VDE)
  252. /** @brief Check whether or not the Firewall pre arm bit is set.
  253. * @note This macro can be executed inside a code area protected by the Firewall.
  254. * @note This macro can be executed whatever the Firewall state (opened or closed) when
  255. * NVDSL register is equal to 0. Otherwise (when NVDSL register is different from
  256. * 0, that is, when the non volatile data segment is defined), the macro can be
  257. * executed only when the Firewall is opened.
  258. * @retval FPA bit setting status (TRUE or FALSE).
  259. */
  260. #define __HAL_FIREWALL_GET_PREARM() ((FIREWALL->CR & FW_CR_FPA) == FW_CR_FPA)
  261. /**
  262. * @}
  263. */
  264. /* Exported functions --------------------------------------------------------*/
  265. /** @addtogroup FIREWALL_Exported_Functions FIREWALL Exported Functions
  266. * @{
  267. */
  268. /** @addtogroup FIREWALL_Exported_Functions_Group1 Initialization Functions
  269. * @brief Initialization and Configuration Functions
  270. * @{
  271. */
  272. /* Initialization functions ********************************/
  273. HAL_StatusTypeDef HAL_FIREWALL_Config(FIREWALL_InitTypeDef * fw_init);
  274. void HAL_FIREWALL_GetConfig(FIREWALL_InitTypeDef * fw_config);
  275. void HAL_FIREWALL_EnableFirewall(void);
  276. void HAL_FIREWALL_EnablePreArmFlag(void);
  277. void HAL_FIREWALL_DisablePreArmFlag(void);
  278. /**
  279. * @}
  280. */
  281. /**
  282. * @}
  283. */
  284. /**
  285. * @}
  286. */
  287. /**
  288. * @}
  289. */
  290. #ifdef __cplusplus
  291. }
  292. #endif
  293. #endif /* STM32L4xx_HAL_FIREWALL_H */