STM32L486xx HAL User Manual
stm32l4xx_hal_firewall.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_firewall.c
00004   * @author  MCD Application Team
00005   * @brief   FIREWALL HAL module driver.
00006   *          This file provides firmware functions to manage the Firewall
00007   *          Peripheral initialization and enabling.
00008   *
00009   *
00010   @verbatim
00011  ===============================================================================
00012                         ##### How to use this driver #####
00013  ===============================================================================
00014   [..]
00015     The FIREWALL HAL driver can be used as follows:
00016 
00017     (#) Declare a FIREWALL_InitTypeDef initialization structure.
00018 
00019     (#) Resort to HAL_FIREWALL_Config() API to initialize the Firewall
00020 
00021     (#) Enable the FIREWALL in calling HAL_FIREWALL_EnableFirewall() API
00022 
00023     (#) To ensure that any code executed outside the protected segment closes the
00024         FIREWALL, the user must set the flag FIREWALL_PRE_ARM_SET in calling
00025         __HAL_FIREWALL_PREARM_ENABLE() macro if called within a protected code segment
00026         or
00027         HAL_FIREWALL_EnablePreArmFlag() API if called outside of protected code segment
00028         after HAL_FIREWALL_Config() call.
00029 
00030 
00031   @endverbatim
00032   ******************************************************************************
00033   * @attention
00034   *
00035   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00036   *
00037   * Redistribution and use in source and binary forms, with or without modification,
00038   * are permitted provided that the following conditions are met:
00039   *   1. Redistributions of source code must retain the above copyright notice,
00040   *      this list of conditions and the following disclaimer.
00041   *   2. Redistributions in binary form must reproduce the above copyright notice,
00042   *      this list of conditions and the following disclaimer in the documentation
00043   *      and/or other materials provided with the distribution.
00044   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00045   *      may be used to endorse or promote products derived from this software
00046   *      without specific prior written permission.
00047   *
00048   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00049   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00050   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00051   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00052   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00053   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00054   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00055   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00056   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00057   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00058   *
00059   ******************************************************************************
00060   */
00061 
00062 /* Includes ------------------------------------------------------------------*/
00063 #include "stm32l4xx_hal.h"
00064 
00065 /** @addtogroup STM32L4xx_HAL_Driver
00066   * @{
00067   */
00068 
00069 /** @defgroup FIREWALL FIREWALL
00070   * @brief HAL FIREWALL module driver
00071   * @{
00072   */
00073 #ifdef HAL_FIREWALL_MODULE_ENABLED
00074 
00075 /* Private typedef -----------------------------------------------------------*/
00076 /* Private define ------------------------------------------------------------*/
00077 /* Private macro -------------------------------------------------------------*/
00078 /* Private variables ---------------------------------------------------------*/
00079 /* Private function prototypes -----------------------------------------------*/
00080 /* Private functions ---------------------------------------------------------*/
00081 
00082 
00083 /** @defgroup FIREWALL_Exported_Functions FIREWALL Exported Functions
00084   * @{
00085   */
00086 
00087 /** @defgroup FIREWALL_Exported_Functions_Group1 Initialization Functions
00088   * @brief    Initialization and Configuration Functions
00089   *
00090 @verbatim
00091 ===============================================================================
00092             ##### Initialization and Configuration functions #####
00093  ===============================================================================
00094     [..]
00095     This subsection provides the functions allowing to initialize the Firewall.
00096     Initialization is done by HAL_FIREWALL_Config():
00097 
00098       (+) Enable the Firewall clock thru __HAL_RCC_FIREWALL_CLK_ENABLE() macro.
00099 
00100       (+) Set the protected code segment address start and length.
00101 
00102       (+) Set the protected non-volatile and/or volatile data segments
00103           address starts and lengths if applicable.
00104 
00105       (+) Set the volatile data segment execution and sharing status.
00106 
00107       (+) Length must be set to 0 for an unprotected segment.
00108 
00109 @endverbatim
00110   * @{
00111   */
00112 
00113 /**
00114   * @brief Initialize the Firewall according to the FIREWALL_InitTypeDef structure parameters.
00115   * @param fw_init: Firewall initialization structure
00116   * @note  The API returns HAL_ERROR if the Firewall is already enabled.
00117   * @retval HAL status
00118   */
00119 HAL_StatusTypeDef HAL_FIREWALL_Config(FIREWALL_InitTypeDef * fw_init)
00120 {
00121   /* Check the Firewall initialization structure allocation */
00122   if(fw_init == NULL)
00123   {
00124     return HAL_ERROR;
00125   }
00126 
00127   /* Enable Firewall clock */
00128   __HAL_RCC_FIREWALL_CLK_ENABLE();
00129 
00130   /* Make sure that Firewall is not enabled already */
00131   if (__HAL_FIREWALL_IS_ENABLED() != RESET)
00132   {
00133     return HAL_ERROR;
00134   }
00135 
00136   /* Check Firewall configuration addresses and lengths when segment is protected */
00137   /* Code segment */
00138   if (fw_init->CodeSegmentLength != 0)
00139   {
00140     assert_param(IS_FIREWALL_CODE_SEGMENT_ADDRESS(fw_init->CodeSegmentStartAddress));
00141     assert_param(IS_FIREWALL_CODE_SEGMENT_LENGTH(fw_init->CodeSegmentStartAddress, fw_init->CodeSegmentLength));
00142     /* Make sure that NonVDataSegmentLength is properly set to prevent code segment access */
00143     if (fw_init->NonVDataSegmentLength < 0x100)
00144     {
00145       return HAL_ERROR;
00146     }
00147   }
00148   /* Non volatile data segment */
00149   if (fw_init->NonVDataSegmentLength != 0)
00150   {
00151     assert_param(IS_FIREWALL_NONVOLATILEDATA_SEGMENT_ADDRESS(fw_init->NonVDataSegmentStartAddress));
00152     assert_param(IS_FIREWALL_NONVOLATILEDATA_SEGMENT_LENGTH(fw_init->NonVDataSegmentStartAddress, fw_init->NonVDataSegmentLength));
00153   }
00154   /* Volatile data segment */
00155   if (fw_init->VDataSegmentLength != 0)
00156   {
00157     assert_param(IS_FIREWALL_VOLATILEDATA_SEGMENT_ADDRESS(fw_init->VDataSegmentStartAddress));
00158     assert_param(IS_FIREWALL_VOLATILEDATA_SEGMENT_LENGTH(fw_init->VDataSegmentStartAddress, fw_init->VDataSegmentLength));
00159   }
00160 
00161   /* Check Firewall Configuration Register parameters */
00162   assert_param(IS_FIREWALL_VOLATILEDATA_EXECUTE(fw_init->VolatileDataExecution));
00163   assert_param(IS_FIREWALL_VOLATILEDATA_SHARE(fw_init->VolatileDataShared));
00164 
00165 
00166    /* Configuration */
00167 
00168   /* Protected code segment start address configuration */
00169   WRITE_REG(FIREWALL->CSSA, (FW_CSSA_ADD & fw_init->CodeSegmentStartAddress));
00170         /* Protected code segment length configuration */
00171   WRITE_REG(FIREWALL->CSL, (FW_CSL_LENG & fw_init->CodeSegmentLength));
00172 
00173   /* Protected non volatile data segment start address configuration */
00174   WRITE_REG(FIREWALL->NVDSSA, (FW_NVDSSA_ADD & fw_init->NonVDataSegmentStartAddress));
00175         /* Protected non volatile data segment length configuration */
00176   WRITE_REG(FIREWALL->NVDSL, (FW_NVDSL_LENG & fw_init->NonVDataSegmentLength));
00177 
00178   /* Protected volatile data segment start address configuration */
00179   WRITE_REG(FIREWALL->VDSSA, (FW_VDSSA_ADD & fw_init->VDataSegmentStartAddress));
00180         /* Protected volatile data segment length configuration */
00181   WRITE_REG(FIREWALL->VDSL, (FW_VDSL_LENG & fw_init->VDataSegmentLength));
00182 
00183   /* Set Firewall Configuration Register VDE and VDS bits
00184      (volatile data execution and shared configuration) */
00185   MODIFY_REG(FIREWALL->CR, FW_CR_VDS|FW_CR_VDE, fw_init->VolatileDataExecution|fw_init->VolatileDataShared);
00186 
00187   return HAL_OK;
00188 }
00189 
00190 /**
00191   * @brief Retrieve the Firewall configuration.
00192   * @param fw_config: Firewall configuration, type is same as initialization structure
00193   * @note This API can't be executed inside a code area protected by the Firewall
00194   *       when the Firewall is enabled
00195   * @note If NVDSL register is different from 0, that is, if the non volatile data segment
00196   *       is defined, this API can't be executed when the Firewall is enabled.
00197   * @note User should resort to __HAL_FIREWALL_GET_PREARM() macro to retrieve FPA bit status
00198   * @retval None
00199   */
00200 void HAL_FIREWALL_GetConfig(FIREWALL_InitTypeDef * fw_config)
00201 {
00202 
00203   /* Enable Firewall clock, in case no Firewall configuration has been carried
00204      out up to this point */
00205   __HAL_RCC_FIREWALL_CLK_ENABLE();
00206 
00207   /* Retrieve code segment protection setting */
00208   fw_config->CodeSegmentStartAddress = (READ_REG(FIREWALL->CSSA) & FW_CSSA_ADD);
00209   fw_config->CodeSegmentLength = (READ_REG(FIREWALL->CSL) & FW_CSL_LENG);
00210 
00211   /* Retrieve non volatile data segment protection setting */
00212   fw_config->NonVDataSegmentStartAddress = (READ_REG(FIREWALL->NVDSSA) & FW_NVDSSA_ADD);
00213   fw_config->NonVDataSegmentLength = (READ_REG(FIREWALL->NVDSL) & FW_NVDSL_LENG);
00214 
00215   /* Retrieve volatile data segment protection setting */
00216   fw_config->VDataSegmentStartAddress = (READ_REG(FIREWALL->VDSSA) & FW_VDSSA_ADD);
00217   fw_config->VDataSegmentLength = (READ_REG(FIREWALL->VDSL) & FW_VDSL_LENG);
00218 
00219   /* Retrieve volatile data execution setting */
00220   fw_config->VolatileDataExecution = (READ_REG(FIREWALL->CR) & FW_CR_VDE);
00221 
00222   /* Retrieve volatile data shared setting */
00223   fw_config->VolatileDataShared = (READ_REG(FIREWALL->CR) & FW_CR_VDS);
00224 
00225   return;
00226 }
00227 
00228 
00229 
00230 /**
00231   * @brief Enable FIREWALL.
00232   * @note Firewall is enabled in clearing FWDIS bit of SYSCFG CFGR1 register.
00233   *       Once enabled, the Firewall cannot be disabled by software. Only a
00234   *       system reset can set again FWDIS bit.
00235   * @retval None
00236   */
00237 void HAL_FIREWALL_EnableFirewall(void)
00238 {
00239   /* Clears FWDIS bit of SYSCFG CFGR1 register */
00240   CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_FWDIS);
00241 
00242 }
00243 
00244 /**
00245   * @brief Enable FIREWALL pre arm.
00246   * @note When FPA bit is set, any code executed outside the protected segment
00247   *       will close the Firewall.
00248   * @note This API provides the same service as __HAL_FIREWALL_PREARM_ENABLE() macro
00249   *       but can't be executed inside a code area protected by the Firewall.
00250   * @note When the Firewall is disabled, user can resort to HAL_FIREWALL_EnablePreArmFlag() API any time.
00251   * @note When the Firewall is enabled and NVDSL register is equal to 0 (that is,
00252   *       when the non volatile data segment is not defined),
00253   *        **  this API can be executed when the Firewall is closed
00254   *        **  when the Firewall is opened, user should resort to
00255   *            __HAL_FIREWALL_PREARM_ENABLE() macro instead
00256   * @note When the Firewall is enabled and  NVDSL register is different from 0
00257   *       (that is, when the non volatile data segment is defined)
00258   *       **  FW_CR register can be accessed only when the Firewall is opened:
00259   *           user should resort to  __HAL_FIREWALL_PREARM_ENABLE() macro instead.
00260   * @retval None
00261   */
00262 void HAL_FIREWALL_EnablePreArmFlag(void)
00263 {
00264   /* Set FPA bit */
00265   SET_BIT(FIREWALL->CR, FW_CR_FPA);
00266 }
00267 
00268 
00269 /**
00270   * @brief Disable FIREWALL pre arm.
00271   * @note When FPA bit is reset, any code executed outside the protected segment
00272   *       when the Firewall is opened will generate a system reset.
00273   * @note This API provides the same service as __HAL_FIREWALL_PREARM_DISABLE() macro
00274   *       but can't be executed inside a code area protected by the Firewall.
00275   * @note When the Firewall is disabled, user can resort to HAL_FIREWALL_EnablePreArmFlag() API any time.
00276   * @note When the Firewall is enabled and NVDSL register is equal to 0 (that is,
00277   *       when the non volatile data segment is not defined),
00278   *        **  this API can be executed when the Firewall is closed
00279   *        **  when the Firewall is opened, user should resort to
00280   *            __HAL_FIREWALL_PREARM_DISABLE() macro instead
00281   * @note When the Firewall is enabled and  NVDSL register is different from 0
00282   *       (that is, when the non volatile data segment is defined)
00283   *       **  FW_CR register can be accessed only when the Firewall is opened:
00284   *           user should resort to  __HAL_FIREWALL_PREARM_DISABLE() macro instead.
00285 
00286   * @retval None
00287   */
00288 void HAL_FIREWALL_DisablePreArmFlag(void)
00289 {
00290   /* Clear FPA bit */
00291   CLEAR_BIT(FIREWALL->CR, FW_CR_FPA);
00292 }
00293 
00294 /**
00295   * @}
00296   */
00297 
00298 /**
00299   * @}
00300   */
00301 
00302 #endif /* HAL_FIREWALL_MODULE_ENABLED */
00303 /**
00304   * @}
00305   */
00306 
00307 /**
00308   * @}
00309   */
00310 
00311 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/