How to start with STM32CubeMX OEMiRoT Boot path on STM32H573

Revision as of 14:35, 8 August 2023 by Registered User (→‎Initial Code Generation and modication)


Target description

The purpose of this article is to explain how to proceed step by step to generate a boot path using STM32CubeMx.
The example below will show how to configure and provision a boot path for an OEMiROT with a secure and non-secure user application initial code generation.
Read the Secure Boot STM32H5 How to Introduction article before starting the practical example described below.

More technical details you may need to understand this getting started are available in the following articles:

The How to start described in this article is using the Boot path number 1 of the figure below.

Figure 1 STM32H5 Bootpath CubeMx examples

Prerequisites

To execute the example described below, you need:

  • Discovery board: STM32H573I-DK|
Figure2 STM32H573 DK MB1677.png
  • The following tools:
    • STM32CubeMX_6.9.0 or later (see STM32CubeMX ST web page[1])
    • IAR Embedded Workbench rev 9.20.1 or later
    • STM32CubeProgrammer rev 2.13.0

Note:

  • STM32 Trusted Package Creator (TPC) is automatically installed during the STM32CubeMX installation. There is a TPC version dedicated to STM32CubeMX and installed in the STM32CubeMX/utilities folder.
  • The latest STM32Cube_FW revision is installed through STM32CubeMX (see appendix)
  • If needed set the windows environment variable (see appendix). It is required in case the H5 doesn’t appear in the “Access to MCU Selector” of STM32CubeMx

1. OEMiROT_Boot firmware compilation

Before starting the STM32CubeMX project configuration it is advised to compile the project OEMiROT_Boot. OEMiROT_Boot is executed in HDPL1 in User Flash and perform authenticity and integrity checks of the User application.
It also configures the number of User application and images. This project is located in STM32CubeH5 firmware : C:\STM32Cube_FW_H5_V1.1.0\Projects\STM32H573I-DK\Applications\ROT\OEMiROT_Boot

SECURITY OEMiROT Boot folder 2.png



Open the OEMiROT_Boot project with the IDE you prefer (EWARM/MDK-ARM or STM32CubeIDE) and rebuild all files.


Please refer to OEMiROT STM32H5 How to Introduction for more informations.

2. Setting the STM32CubeMX project

Launch STM32CubeMX

  • 1) Click on Access to MCU selector (easier for this example to enable only the needed GPIOs, so it's advised to use the MCU selector instead of the board selector).
  • 2) Select STM32H5 serie and select the device used in STM32H5-DK
  • 3) Click start project
  • 4) Enable the TrustZone, as shown in the figure 1, for the OEMiROT bootpath the TZ needs to be enabled.


Figure 3 STM32CubeMX project start


Set the STM32CubeMX project:

  • 1) Type the name of the project (the related folder will be created)
  • 2) Chose the folder for this project (avoid long path)
  • 3) Check that Secure Project and Non Secure Project are ticked (should be the setting by default)
  • 4) Select the Toolchain, for this example EWARN is used
SECURITY STM32CubeMX project setting for OEMiROT on STM32H573 2.png
  • 5) File -> Save Project -> the project folder (if not already existing) and the STM32H5_OEMiROT_SLed_NSLed.ioc file is created

3. GPIO configuration

For this proposed example three GPIO need to be configured for the discovery board:

  • PF4 for the blue LED (LD4 on the discovery board) used in the secure user application code
  • PC13 for the blue user button used in the secure user application code
  • PI9 for the green LED (LD1 on the discovery board) used in the non-secure user application code


In the STM32CubeMX select the Pinout & Configuration window

Figure 11 GPIO configuration window

To configure the GPIO for the blue LED proceed as follow:

  • Click left on PF4: select GPIO_Output -> a push pull output will be configured need to drive an LED
  • Click right on PF4: and select Cortex-M33 secure. This GPIO is then assigned to the secure user application code
Figure 12 GPIO setting for discovery board blue LED

To configure the user bue button proceed as follow:

  • Click left on PC13: select GPIO_Input
  • Click right on PC13: and select Cortex-M33 secure. This GPIO is then assigned to the secure user application code
Figure 13 GPIO setting for discovery board blue user button

To configure the GPIO for the green LED proceed as follow:

  • Click left on PI9: select GPIO_Output -> a push pull output will be configured need to drive an LED
  • Click right on PI9: and select Cortex-M33 non-secure. This GPIO is then assigned to the non-secure user application code
Figure 14 GPIO setting for discovery board green LED
  • Select GPIO: the summary of the settings are displayed as shown in the figure below
Figure 15 GPIO setting summary

4. Configure the OEMiROT bootpath

To configure the bootpath number 1 of the figure 1 proceed as follow:

  • 1) Click on "Boot Path and Debug Authentication"
  • 2) Click on "Select"
  • 3) Select the OEM-iROT. The TZ activation has already been chosen, this selection is defining the UBE option byte (but you don't need to take care about the setting of this option byte).
  • 4) Click on "Next"
  • 5) Select Secure Application (since for this example there is no uROT, so no second boot stage)
  • 6) Click Finish

Note: depending on the STM32CubeMx version, the Debug Authentication Configure button can't be selected before completing the OEMiROT boot path configuration.

SECURITY STM32H573 Configure Boot Path 2.png


OEMiROT_Boot firmware

You can see the OEMiROT_Boot firmware path in Boot path and Linker tabs.

SECURITY OEMIROT Boot default repository 2.png

This firmware is located by default in STM32CubeH5 package installed through STM32CubeMX. If necessary, the OEMiRoT_Boot may be compiled by the user during the provisioning.
If you want to use another repository for STM32CubeH5 package than the default CubeMX folder you can define it in
Project Manager--> Project Tab-->MCu and Firmware Package:

  • Uncheck Use Default Firmware Location
  • Define the path you want to use in Firmware Relative Path
SECURITY OEMIROT Boot repository.png



OEMiROT configuration

In the Project Manager window, select Edit Config, as shown in the figure below.

SECURITY Edit config file OEMiROT bootPath STM32H573 2.png

It will automatically open STM32Trusted Package Creator (TPC).
And the following window is displayed.

  • Generate the OBKey file :
SECURITY TPC edit config STM32H573 bootpath.png

Notes:

  • The figure above, shows the path where the OEMiROT_Config.xml file is located. This file contains all the configuration parameters for the OEMiROT.
  • The configuration of the OEM-iROT is made in the OEMiROT_Boot firmware. During the provisioning the download area is used to store the encrypted user application, that is decrypted by the OEMiROT and installed in the execution area (refer to the OEMiROT STM32H5 How to Introduction article).

As mentioned above, for this example you don't need to make any other updates of the default configuration.


Select H5-Image Gen1 as shown in figure below No modification to be done in this window, just some explanations:

  • The OEMiROT_S_Code_Image.xml file contains the settings needed to generate the images.
  • Two paths are indicated for binary files:
    • During the code compilation using the IDE, a binary file containing the compiled secure application is created.
    • The IDE will also perform a postbuild command to generate from this binary input files a single encrypted and signed binary image.
Note: The above mentioned paths are updated by STM32CubeMx and must not be modified.

Select H5-Image Gen2 and you have the same informations for the non secure images generation with the file OEMiROT_NS_Code_Image.xml.

SECURITY Image Gen H573.png
  • Close STM32 Trusted Package Creator

5. Debug Authentication configuration

Refer to the Debug Authentication for STM32H5 article for more details.

A default configuration file (fully functional) and related OBKey file are provided through the STM32CubeFW.
To customize the Debug Authentication proceed as following:

  • In STM32CubeMx -> Project Manager -> Select: Debug Authentication Configure.
SECURITY STM32CubeMX DA select.png
  • The STM32Trusted Package Creator is launched (TPC installed together with STM32CubeProgrammer) and the DA_Config.xml file is automatically open.
  • The "key_1_root" key is needed to reopen the device or to perform a regression. To protect you own developed application, this key needs to be regenerated. If regenerated, it's important to not lose this new key.
  • Don't regenerate the key for this example
  • The permission mask is set to allow all possible regressions and debug openings in the secure and non-secure user application.

See Debug Authentication STM32H5 How to Introduction article for more details.
This permission mask, called the SOC mask is stored in the device during the provisioning process.
The owner of the root key has then the defined privileges to perform a regression or open the debugger according set permissions.

SECURITY DA config for cubeMX.png
  • Generate the OBKey file that will be used during the provisioning, click:Security GenerateOBKeyIcon.png
  • Close STM32Trusted Package Creator

6. Initial Code Generation and modication

Select Project Manager in STM32CubeMX

  • Ensure Sign Binaries is selected -> at each user application code compilation using the IDE an encrypted and signed binary is created containing both secure and non secure user application (compilation -> postbuild command)
  • Note that start and end address are indicated for the secure and non-secure according to the OEMiROT configuration


SECURITY CubeMX SignBinary and mapping 3.png
  • Click Generate Code.
    • You can have the following warning message. Click yes to generate the code.
    • Or if you want to enable the Instruction Cache, through the Pinout & Configuration Tab select ICache located just below the GPIO configuration.
warning.png
  • Open generated user application.
SECURITY Open generated user app CubeMX 2.png

Open the secure user application code

  • In main.c file, comment out NonSecure_Init() for the jump into the non secure user application code (replaced with the code below).
  • Insert the following code as shown in the figure below

                 HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_4);
                 HAL_Delay(500);
                 if (HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13) == 0x1)
                  {
                   NonSecure_Init(); 
                  }

This code will make the blue LED blinking with the chosen Delay, showing that the secure user application is executed.
If the user blue button is pushed the jump in the non secure code is done through the Non-Secure_Init .

SECURITY Secure user application code modification.png
  • Compile the code using the IDE (Project -> Rebuild all).
  • Note: the secure code need to be compiled first (before the non-secure code)

Open the non-secure user application

  • In main.c file, insert the following code as shown in the figure below

    HAL_GPIO_TogglePin(GPIOI,GPIO_PIN_9);
    HAL_Delay(800);

This code will make the geen LED blinking with the chosen Delay, showing that the non-secure user application is executed.

SECURITY NON Secure user application code modification.png
  • Compile the code using the IDE (Project -> Rebuild all).

Two binaries are created as shown in the figure below.

  • The binary obtained after signature and encryption of the binary of Secure application.
  • The binary obtained after signature and encryption of the binary of Non Secure application.

Both binaries have been generated through the automatically called postbuild command

SECURITY User App generated binaries.png

7. Device Provisioning

For the device provisioning the OBKey files generated previously are used to configure the device OEMiROT accordingly and to configure the Debug Authentication according to the permission mask.
The encrypted and signed binary is uploaded in the download area defined during the OEMiROT_Boot configuration.
The OEMiROT_Boot firmware verifies the authenticity, decrypt and install the secure and non-secure user applications in the execution area defined during the OEMiROT_Boot configuration.

For the provisioning STM32CubeProgrammer Command Line (CLI) are called automatically by the provisioning script.
It's important that the path to the STM32CubeProgrammer is correct in the env.bat file. Check and update as shown in the figure below:
! Don't modify the path to the STM32TrustedPackageCreator included in the STM32CubeMX.

SECURITY env.bat repository.png
SECURITY env.bat command.png

The provisioning of the device is done using the provided provisioning script.

  • Connect your board with the USB cable (USB STLink connector)
  • Double click on the provisioning.bat shown in the figure below
  • The provisioning Step 1 is the configuration
    • The OEMiROT configuration setting done previously (generation of the related OEMiRoT_Config.obk file)
    • The DA configuration setting done previously (generation of the related DA_Config.obk file)
  • The provisioning Step 2 is the image generation
    • If it has not been done at the beginning : OEMiROT_Boot firmware compilation, you have to open the OEMiROT_Boot project with the IDE you prefer (EWARM/MDK-ARM or STM32CubeIDE) and rebuild all files.
    • The user application code image generation has been done previously during the compilation and the postbuild.
    • The image generation is not applicable for this example -> type any key
  • The provisioning Step 3 is the board provisioning
    • Before typing any key, check the indicated boot switch position
    • The STM32H5 device provisioning is executed, the configuration is stored, and the user application codes are installed


SECURITY provisioning.bat repository.png
SECURITY provisioning script window 2.png
  • The last Step is the product state setting, for this example the device is set in CLOSED
  • After the message that the board is correctly configured, the script can be closed.

See Security:Introduction_to_Silicon_device_life_cycle and Security:Debug_Authentication articles for detail explanations.

8. Code execution

  • Press the black reset button, the blue LED is blinking showing that the secure user application is executed
  • Press the blue button, the green LED is blinking showing that the jump to the non-secure application has been successful and that the non-secure code is executed.

9. STM32CubeProgrammer, discovery command

  • Start STM32CubeProgrammer and try to connect.
  • The error message below is displayed because the device has been set in CLOSED state and the debugger can't be connected even to see the non-secure user application code.
  • To reopen the debugger a Debug Authentication is needed and the opening can only be done by the owner of the root key.

The product state can be seen using the Discovery command (see figure below):

  • 1) In STM32CubeProgrammer click on the shield icon
  • 2) Select Debug Authentication
  • 3) Click on Discover, the lifecycle CLOSED is displayed.
Figure 24 Discovery command

An example on how to reopen the debugger, view the code and attach an IDE to perform a step by step application execution is shown in the chapter 3 of the article How to start with OEM-iROT on STM32H573 and 563–TrustZone enabled

10. Regression

The regression will remove the user applications and protections.
After completing a hands-on, if your board is not in OPEN state, it's advised to perform a regression mainly if you have regenerated the root key.

  • Double click to start the regression script shown in the figure below
  • The regression is performed automatically. The Debug Authentication is done using the root and using the permission defined during the OEMiROT configuration
SECURITY Regression script Repository and Execution.png
  • You can connect STM32CubeProgrammer and see that the debugger is again open and that the flash is fully erased.

11. Appendix

11.1. STM32CubeFW installation

The STM32CubeFW needs to be installed through STM32CubeMX.

  • Step 1: the repository folder has to be defined:
    • In CubeMx: Help menu -> Updater Settings
    • Browse the repository you have chosen for the STM32CubeFW


Figure 26 STM32CubeFW repository setting
  • Step 2: STM32CubeFW installation
    • In STM32CubeMX: Select Install/Remove
    • In the description frame: select STM32H5
    • Select the CubeFW package to install
    • In case you have locally the zip file of the STM32CubeFWH5: it can be installed by drag and drop this file in the description window

Note: only official STM32CubeFW release can be installed by STM32CubeMX.

Figurex STM32CubeFW installation