Advertisement
Guest User

Secure main.c

a guest
Nov 23rd, 2023
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.89 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2023 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. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21.  
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24.  
  25. /* USER CODE END Includes */
  26.  
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29.  
  30. /* USER CODE END PTD */
  31.  
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34.  
  35. /* USER CODE END PD */
  36.  
  37. /* USER CODE BEGIN VTOR_TABLE */
  38.  
  39. /* Non-secure Vector table to jump to (internal Flash Bank2 here)             */
  40. /* Caution: address must correspond to non-secure internal Flash where is     */
  41. /*          mapped in the non-secure vector table                             */
  42.  
  43. #define VTOR_TABLE_NS_START_ADDR  0x08100000UL
  44.  
  45. /* USER CODE END VTOR_TABLE*/
  46.  
  47. /* Private macro -------------------------------------------------------------*/
  48. /* USER CODE BEGIN PM */
  49.  
  50. /* USER CODE END PM */
  51.  
  52. /* Private variables ---------------------------------------------------------*/
  53.  
  54. /* USER CODE BEGIN PV */
  55.  
  56. /* USER CODE END PV */
  57.  
  58. /* Private function prototypes -----------------------------------------------*/
  59. static void NonSecure_Init(void);
  60. static void MX_GPIO_Init(void);
  61. static void MX_GTZC_S_Init(void);
  62. static void MX_ICACHE_Init(void);
  63. /* USER CODE BEGIN PFP */
  64.  
  65. /* USER CODE END PFP */
  66.  
  67. /* Private user code ---------------------------------------------------------*/
  68. /* USER CODE BEGIN 0 */
  69.  
  70. /* USER CODE END 0 */
  71.  
  72. /**
  73.   * @brief  The application entry point.
  74.   * @retval int
  75.   */
  76. int main(void)
  77. {
  78.   /* USER CODE BEGIN 1 */
  79.  
  80.   /* USER CODE END 1 */
  81.  
  82.   /* MCU Configuration--------------------------------------------------------*/
  83.  
  84.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  85.   HAL_Init();
  86.  
  87.   /* USER CODE BEGIN Init */
  88.  
  89.   /* USER CODE END Init */
  90.  
  91.   /* GTZC initialisation */
  92.   MX_GTZC_S_Init();
  93.  
  94.   /* USER CODE BEGIN SysInit */
  95.  
  96.   /* USER CODE END SysInit */
  97.  
  98.   /* Initialize all configured peripherals */
  99.   MX_GPIO_Init();
  100.   MX_ICACHE_Init();
  101.   /* USER CODE BEGIN 2 */
  102.  
  103.   /* USER CODE END 2 */
  104.  
  105.   /*************** Setup and jump to non-secure *******************************/
  106.  
  107.   NonSecure_Init();
  108.  
  109.   /* Non-secure software does not return, this code is not executed */
  110.   /* Infinite loop */
  111.   /* USER CODE BEGIN WHILE */
  112.   while (1)
  113.   {
  114.     /* USER CODE END WHILE */
  115.  
  116.     /* USER CODE BEGIN 3 */
  117.   }
  118.   /* USER CODE END 3 */
  119. }
  120.  
  121. /**
  122.   * @brief  Non-secure call function
  123.   *         This function is responsible for Non-secure initialization and switch
  124.   *         to non-secure state
  125.   * @retval None
  126.   */
  127. static void NonSecure_Init(void)
  128. {
  129.   funcptr_NS NonSecure_ResetHandler;
  130.  
  131.   SCB_NS->VTOR = VTOR_TABLE_NS_START_ADDR;
  132.  
  133.   /* Set non-secure main stack (MSP_NS) */
  134.   __TZ_set_MSP_NS((*(uint32_t *)VTOR_TABLE_NS_START_ADDR));
  135.  
  136.   /* Get non-secure reset handler */
  137.   NonSecure_ResetHandler = (funcptr_NS)(*((uint32_t *)((VTOR_TABLE_NS_START_ADDR) + 4U)));
  138.  
  139.   /* Start non-secure state software application */
  140.   NonSecure_ResetHandler();
  141. }
  142.  
  143. /**
  144.   * @brief GTZC_S Initialization Function
  145.   * @param None
  146.   * @retval None
  147.   */
  148. static void MX_GTZC_S_Init(void)
  149. {
  150.  
  151.   /* USER CODE BEGIN GTZC_S_Init 0 */
  152.  
  153.   /* USER CODE END GTZC_S_Init 0 */
  154.  
  155.   MPCBB_ConfigTypeDef MPCBB_Area_Desc = {0};
  156.  
  157.   /* USER CODE BEGIN GTZC_S_Init 1 */
  158.  
  159.   /* USER CODE END GTZC_S_Init 1 */
  160.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_USART3, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  161.   {
  162.     Error_Handler();
  163.   }
  164.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_I2C1, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  165.   {
  166.     Error_Handler();
  167.   }
  168.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_UCPD1, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  169.   {
  170.     Error_Handler();
  171.   }
  172.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_TIM1, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  173.   {
  174.     Error_Handler();
  175.   }
  176.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_USART1, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  177.   {
  178.     Error_Handler();
  179.   }
  180.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_SAI1, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  181.   {
  182.     Error_Handler();
  183.   }
  184.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_SAI2, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  185.   {
  186.     Error_Handler();
  187.   }
  188.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_USB, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  189.   {
  190.     Error_Handler();
  191.   }
  192.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_SPI5, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  193.   {
  194.     Error_Handler();
  195.   }
  196.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_I2C4, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  197.   {
  198.     Error_Handler();
  199.   }
  200.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_ADC12, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  201.   {
  202.     Error_Handler();
  203.   }
  204.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_SDMMC1, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  205.   {
  206.     Error_Handler();
  207.   }
  208.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_FMC_REG, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  209.   {
  210.     Error_Handler();
  211.   }
  212.   if (HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_OCTOSPI1, GTZC_TZSC_PERIPH_SEC|GTZC_TZSC_PERIPH_NPRIV) != HAL_OK)
  213.   {
  214.     Error_Handler();
  215.   }
  216.   MPCBB_Area_Desc.SecureRWIllegalMode = GTZC_MPCBB_SRWILADIS_ENABLE;
  217.   MPCBB_Area_Desc.InvertSecureState = GTZC_MPCBB_INVSECSTATE_NOT_INVERTED;
  218.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[0] =   0x00000000;
  219.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[1] =   0x00000000;
  220.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[2] =   0x00000000;
  221.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[3] =   0x00000000;
  222.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[4] =   0x00000000;
  223.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[5] =   0x00000000;
  224.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[6] =   0x00000000;
  225.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[7] =   0x00000000;
  226.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[8] =   0x00000000;
  227.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[9] =   0x00000000;
  228.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[10] =   0x00000000;
  229.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[11] =   0x00000000;
  230.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[12] =   0x00000000;
  231.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[13] =   0x00000000;
  232.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[14] =   0x00000000;
  233.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[15] =   0x00000000;
  234.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[16] =   0x00000000;
  235.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[17] =   0x00000000;
  236.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[18] =   0x00000000;
  237.   MPCBB_Area_Desc.AttributeConfig.MPCBB_SecConfig_array[19] =   0x00000000;
  238.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[0] =   0xFFFFFFFF;
  239.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[1] =   0xFFFFFFFF;
  240.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[2] =   0xFFFFFFFF;
  241.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[3] =   0xFFFFFFFF;
  242.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[4] =   0xFFFFFFFF;
  243.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[5] =   0xFFFFFFFF;
  244.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[6] =   0xFFFFFFFF;
  245.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[7] =   0xFFFFFFFF;
  246.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[8] =   0xFFFFFFFF;
  247.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[9] =   0xFFFFFFFF;
  248.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[10] =   0xFFFFFFFF;
  249.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[11] =   0xFFFFFFFF;
  250.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[12] =   0xFFFFFFFF;
  251.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[13] =   0xFFFFFFFF;
  252.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[14] =   0xFFFFFFFF;
  253.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[15] =   0xFFFFFFFF;
  254.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[16] =   0xFFFFFFFF;
  255.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[17] =   0xFFFFFFFF;
  256.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[18] =   0xFFFFFFFF;
  257.   MPCBB_Area_Desc.AttributeConfig.MPCBB_PrivConfig_array[19] =   0xFFFFFFFF;
  258.   MPCBB_Area_Desc.AttributeConfig.MPCBB_LockConfig_array[0] =   0x00000000;
  259.   if (HAL_GTZC_MPCBB_ConfigMem(SRAM3_BASE, &MPCBB_Area_Desc) != HAL_OK)
  260.   {
  261.     Error_Handler();
  262.   }
  263.   /* USER CODE BEGIN GTZC_S_Init 2 */
  264.  
  265.   /* USER CODE END GTZC_S_Init 2 */
  266.  
  267. }
  268.  
  269. /**
  270.   * @brief ICACHE Initialization Function
  271.   * @param None
  272.   * @retval None
  273.   */
  274. static void MX_ICACHE_Init(void)
  275. {
  276.  
  277.   /* USER CODE BEGIN ICACHE_Init 0 */
  278.  
  279.   /* USER CODE END ICACHE_Init 0 */
  280.  
  281.   /* USER CODE BEGIN ICACHE_Init 1 */
  282.  
  283.   /* USER CODE END ICACHE_Init 1 */
  284.   /* USER CODE BEGIN ICACHE_Init 2 */
  285.  
  286.   /* USER CODE END ICACHE_Init 2 */
  287.  
  288. }
  289.  
  290. /**
  291.   * @brief GPIO Initialization Function
  292.   * @param None
  293.   * @retval None
  294.   */
  295. static void MX_GPIO_Init(void)
  296. {
  297. /* USER CODE BEGIN MX_GPIO_Init_1 */
  298. /* USER CODE END MX_GPIO_Init_1 */
  299.  
  300.   /* GPIO Ports Clock Enable */
  301.   __HAL_RCC_GPIOG_CLK_ENABLE();
  302.   __HAL_RCC_GPIOH_CLK_ENABLE();
  303.   __HAL_RCC_GPIOC_CLK_ENABLE();
  304.   __HAL_RCC_GPIOA_CLK_ENABLE();
  305.  
  306.   /*IO attributes management functions */
  307.   HAL_GPIO_ConfigPinAttributes(GPIOG, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_11, GPIO_PIN_NSEC);
  308.  
  309.   /*IO attributes management functions */
  310.   HAL_GPIO_ConfigPinAttributes(GPIOH, GPIO_PIN_0, GPIO_PIN_NSEC);
  311.  
  312.   /*IO attributes management functions */
  313.   HAL_GPIO_ConfigPinAttributes(GPIOC, GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_NSEC);
  314.  
  315.   /*IO attributes management functions */
  316.   HAL_GPIO_ConfigPinAttributes(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7, GPIO_PIN_NSEC);
  317.  
  318. /* USER CODE BEGIN MX_GPIO_Init_2 */
  319. /* USER CODE END MX_GPIO_Init_2 */
  320. }
  321.  
  322. /* USER CODE BEGIN 4 */
  323.  
  324. /* USER CODE END 4 */
  325.  
  326. /**
  327.   * @brief  This function is executed in case of error occurrence.
  328.   * @retval None
  329.   */
  330. void Error_Handler(void)
  331. {
  332.   /* USER CODE BEGIN Error_Handler_Debug */
  333.   /* User can add his own implementation to report the HAL error return state */
  334.   __disable_irq();
  335.   while (1)
  336.   {
  337.   }
  338.   /* USER CODE END Error_Handler_Debug */
  339. }
  340.  
  341. #ifdef  USE_FULL_ASSERT
  342. /**
  343.   * @brief  Reports the name of the source file and the source line number
  344.   *         where the assert_param error has occurred.
  345.   * @param  file: pointer to the source file name
  346.   * @param  line: assert_param error line source number
  347.   * @retval None
  348.   */
  349. void assert_failed(uint8_t *file, uint32_t line)
  350. {
  351.   /* USER CODE BEGIN 6 */
  352.   /* User can add his own implementation to report the file name and line number,
  353.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  354.   /* USER CODE END 6 */
  355. }
  356. #endif /* USE_FULL_ASSERT */
  357.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement