RA6 Mcuboot Internal Code Flash Linear Mode
RA6 Mcuboot Internal Code Flash Linear Mode
Renesas RA Family
RA6 Secure Bootloader Using MCUboot and
Internal Code Flash
Introduction
MCUboot is a secure bootloader for 32-bit MCUs. It defines a common infrastructure for the bootloader,
defines system flash layout on microcontroller systems, and provides a secure bootloader that enables easy
software update. MCUboot is independent of operating system and hardware, and relies on hardware porting
layers from the operating system it works with. The Renesas Flexible Software Package (FSP) integrates an
MCUboot port starting from FSP v3.0.0. Users can benefit from using the FSP MCUboot Module to create a
Root of Trust (RoT) for the system and perform secure booting and fail-safe application updates.
MCUboot is maintained by Linaro in the GitHub mcu-tools page https://github.com/mcu-tools/mcuboot. There
is a \docs folder that holds the documentation for MCUboot in .md file format. This application note refers to
the above-mentioned documents wherever possible and is intended to provide additional information that is
related to using the MCUboot Module with Renesas RA FSP v3.0.0 or later.
This application note guides you through application project creation using the MCUboot Module on Renesas
EK-RA6M4 and EK-RA6M3 kits for the internal flash usage using FSP v4.2.0. Example projects for the use
case of designing with TrustZone® for multi-image support are provided for EK-RA6M4 internal flash.
Example projects for the use case of designing with single-image support are provided for EK-RA6M3
internal flash. The MCUboot Module is supported across the entire RA MCU Family. Guidelines of how to
adapt the example project configurations for other RA Family MCUs are provided.
Required Resources
Development tools and software
• The e2 studio ISDE v2021-10 or greater
• Renesas Flexible Software Package (FSP) v4.2.0
• SEGGER J-link® USB driver
Note: The above three software components are bundled in a downloadable platform installer available on
the FSP webpage at renesas.com/ra/fsp.
• Python v3.9 or later (https://www.python.org/downloads/)
Hardware
• EK-RA6M4 Evaluation Kit for RA6M4 MCU Group (http://www.renesas.com/ra/ek-ra6m4)
• EK-RA6M3 Evaluation Kit for RA6M3 MCU Group (http://www.renesas.com/ra/ek-ra6m3)
• Workstation running Windows® 10 and Tera Term0 console or similar application
• One USB device cable (type-A male to micro-B male)
6. Appendix.................................................................................................................................. 45
6.1 Making the Bootloader for Cortex-M33 Immutable ............................................................................... 45
6.2 Making the Bootloader for Cortex-M4 Immutable ................................................................................. 45
6.3 Device Lifecycle Management for Renesas RA Cortex-M33 MCUs ..................................................... 45
6.4 Device Lifecycle Management for Renesas RA Cortex-M4 MCUs ....................................................... 46
7. References .............................................................................................................................. 46
• Swap
In the Swap image upgrade mode, the active image is also stored in the Primary slot and is always
started by the bootloader. If the bootloader finds a valid image in the Secondary slot that is marked for
upgrade, then contents of the Primary slot and the Secondary slot are swapped. The new image then
starts from the Primary slot. Upgrading an old image with a new one by swapping can be a two-step
process. In this process, MCUboot performs a “test” swap of image data in flash and boots the new
image. The new image can then update the contents of flash at runtime to mark itself “OK”, and
MCUboot will then still choose to run it during the next boot.
• Pros
• The bootloader can revert the swapping as a fallback mechanism to recover the previous
working firmware version after a faulty update.
• The application can perform a self-test to mark itself permanent.
• This image upgrade mode is fail-safe and resistant to power-cut failures.
• Encrypted image support is available when using external flash.
• Cons
• Need to allocate a Scratch Area.
• Larger memory overhead, due to a larger image trailer and additional Scratch Area.
• Larger number of write cycles in the Scratch Area, faster wearing out of Scratch sectors.
Swap upgrade mode is supported by Renesas RA FSP v3.0.0 or later. Runtime image testing is
supported by FSP v3.4.0 or later, excluding v3.5.0. External flash memory support is supported by FSP
v3.5.0 or later. The swap update mode without test mode is demonstrated in section 3.4 and the swap
update mode with test mode is demonstrated in section 3.8.
We recommended reviewing MCUboot Port section of the FSP User’s Manual to understand the Build Time
Configurations for MCUboot. This section is not meant to cover all the configurable properties. Only some of
the most frequently used configuration options are introduced.
When Overwrite mode is selected, the --pad or --confirm option generates signing commands such that the
overwrite will occur and the Secondary application will overwrite the Primary application.
Once the EK-RA6M4 is powered up, initialize the MCU prior to exercising the bootloader project.
Erase the entire MCU flash and ensure the MCU is in Secure Software Development Device Lifecycle State.
This can be achieved using the Renesas Device Partition Manager.
1. Power cycle the board, launch e2 studio, and open the Renesas Device Partition Manager.
Click Resume twice and boot the Primary image. All three LEDs should be blinking. Pause the
execution and confirm the execution is in the Non-secure Primary slot.
2. On the top of the e2 studio toolbar, click the Load Ancillary File button to load the new application
images to the Secondary slot region. Refer to section 3.9 for troubleshooting when using the Load
Ancillary File function.
Figure 19. Load the Secondary Secure Application Image for Overwrite Update Mode
Figure 20. Load the Secondary Non-Secure Application Image for Overwrite Update Mode
3. Click Resume . The overwrite occurs and the new image is executed. The blue and green led will be
blinking instead of all three LEDs.
4. On the RTT Viewer output, confirm that the following messages are printed and only the blue and green
LEDs are blinking.
Figure 21. Executing the Secondary Non-Secure image for Overwrite Update Mode
3.3.7 Update the Non-Secure Secondary Image
This step is provided as a reference for implementation of individual image update when designing in a
TrustZone environment.
Click Pause again and download the Primary Non-Secure application to the Secondary Non-Secure slot
using the Load Ancillary File tool. Click OK. Click Resume again. The three LEDs start to blink
again and the RTT Viewer shows the same message as Figure 38.
• For Overwrite update mode, if the Secondary image is marked for update, overwrite always occurs.
• It is possible to update the Secure and Non-Secure applications individually with proper application
design.
Figure 22. Load the Primary Non-Secure Image to the Second Slot
Figure 25. Execution of Primary Non-Secure Application for Swap Update Mode
3.4.1 Downloading and Running the Secondary Applications
During development, you can use the Ancillary loading capability to load the new Secure image to the
intended location. You can use the example new Secure application provided in this application and follow
the steps below to perform an application upgrade. Refer to section 3.9 for troubleshooting when using the
Load Ancillary File function.
4. Click Resume . The swap occurs, and the new image is executed. Only the blue and green LEDs
should be blinking.
5. Confirm the execution result.
Figure 26. Executing the Secondary Non-Secure Image for Swap Update Mode
3.5 Running the EK-RA6M4 DXIP Update Mode Example
The process of running the EK-RA6M4 DXIP Update Modes is similar to the Overwrite Update mode. This
section will focus on the difference in the operation:
1. Follow section 3.3 to initialize the RA6M4 MCU.
2. Import the project under folder \ra6m4_dxip_with_bootloader_flat to a workspace and see the
following set of example projects.
Figure 27. Example Projects for RA6M4 Direct XIP Update Mode
The functionalities of the application projects are blinking the LEDs and providing RTT viewer outputs.
3. Configure the Python signing environment by following section 3.2 if this is the first time you are signing
the application image.
4. The bootloader needs to be compiled first. For each project, open the configuration.xlm file, click
Generate Project Contents and then click to build the project. Compile the example projects
following below orders. Ensure the signed image for the application project is located under the /Debug
folder and is named <application_project_name>_bin.signed
1. ra_mcuboot_ra6m4_dxip
2. app_ra6m4_primary
3. app_ra6m4_secondary
5. Verify the debug configuration and follow section 3.3 step 3.3.4 to start debugging the application.
During development, you can use the Ancillary loading capability from the top of the e2 studio toolbar
to load the new image to the intended location. You can use the example new application provided in this
application and follow the steps below to perform an application upgrade:
2. Load the new application images to the Secondary slot region using the Ancillary loading capability
from the top of the e2 studio toolbar.
Figure 30. Load the Secondary Secure Application Image for DXIP Update Mode
3. Click Resume . The swap occurs, and the new image is executed. Only the blue and green LEDs
should be blinking.
4. Confirm the same configuration as shown in Figure 17, then click OK.
Figure 31. Executing the Secondary Image for DXIP Update Mode
3.6 Set up EK-RA6M3
Erase the entire MCU flash prior to proceeding to the following steps. This can be done using J-Link Flash
Lite. Launch J-Flash Lite and select the RA6M3, as shown in Figure 39.
Follow section 3.2 to set up the Python signing environment if this is the first time you are signing the
application image.
3.7.2 Compile the Projects
The bootloader needs to be compiled first. For each project, open the configuration.xml file, click
Generate Project Contents, and then click to build the project. For the application projects, the post-
build command will also sign the corresponding images. The signed image is located under the \Debug
folder and is named <project_name>.bin.signed (for example,
/app_ra6m3_primary/Debug/app_ra6m3_primary.bin.signed)
1. ra_mcuboot_ra6m3
2. app_ra6m3_primary
3. app_ra6m3_secondary
Click Resume twice and boot the Primary image. All three LEDs should be blinking.
3.7.4 Open the J-Link RTT Viewer
Configure the RTT Viewer as shown below. Configure the address search range as 0x1ffe0000 0x8000.
2. Load the new application images to the Secondary slot region using the Ancillary loading capability
from the top of the e2 studio toolbar. Select Load as raw binary image and configure the Address to
0x30000.
Figure 39. Load the Secondary Application Image for Overwrite Mode
3. Click Resume . The overwrite occurs and the new image is executed. Now only the Blue LED should
be blinking.
4. Confirm the same configuration as shown in Figure 37, then click OK. The following output is printed and
only the blue LED blinks.
Figure 40. Executing the Secondary Application Image for Overwrite Update Mode
3.8 Running the EK-RA6M3 Swap Test Update Mode Example
Follow the steps below to run the example projects for EK-RA6M3 using the MCUboot Swap Test Update
mode.
3.8.1 Import the Projects
Import the projects under Folder \ra6m3_swap_test_with_bootloader to a Workspace.
The following example projects are included in this folder:
Figure 41. Example Projects for RA6M3 Swap Test Update Mode
• Project ra_mcuboot_ra6m3 is the bootloader project.
• Project app_ra6m3_primary is the initial Primary application project. This project blinks the three
LEDs on the EK-RA6M3 kit.
• Project app_ra6m3_secondary is the Secondary application project. This project blinks the blue LED
on the EK-RA6M3 kit.
Click Resume twice and boot the Primary image. All three LEDs should be blinking.
3.8.4 Open the J-Link RTT Viewer
Configure the RTT Viewer as shown in Figure 37. Observe the following output on the RTT Viewer. This
output shows the Primary application is being executed and all three LEDs are blinking.
Figure 43. Execution of Primary Application for Swap Test Mode Mode
2. Load the new application images to the Secondary slot region using the Ancillary loading capability
from the top of the e2 studio toolbar in a similar way as Figure 39. Select Load as raw binary image and
configure the Address to 0x38000.
3. Click Resume . The swap occurs and the new image is executed. Now only the blue LED should be
blinking.
4. Confirm the same configuration as shown in Figure 37, then click OK. The following output is printed and
only the blue LED should blink.
Figure 44. Executing the Secondary Application Image for Swap Test Update Mode
5. Pause and reset the application from the debugger.
3.9 Troubleshooting
When running the example projects, you may experience USB Debug connection or the RTT Viewer
connection issue when using the “Load Ancillary File” button to download the Secondary image. To
recover from these failures:
• If the USB Debug connection disconnects, the recommendation is to try out another available USB port
for the USB Debug connection. If failure persists, contact Renesas support.
• If the RTT Viewer disconnects, the recommendation is to power cycle the board and restart the debug
session.
4. Creating the Bootloader
This section provides a walk-through of the bootloader creation of the example projects as well as how to link
the standalone application with the bootloader. For most of the steps, the considerations and configurations
in creating bootloader with the different upgrade mode are common. Whenever there is a difference in the
implementation of the different update mode, the difference will be addressed.
The walk-through of the bootloader creation in this section targets the bootloader used in section 3.3 for the
TrustZone enabled system. Wherever there is a need to address the Non-TrustZone enabled
implementation, it will be addressed.
4.1 Creating a Bootloader Project for RA Family
The screen captures used in these sections are based on the RA6M4 based bootloader projects used in
section 3.3, 3.4, and 3.5. Follow this section to establish the bootloader projects used in section 3.3, which
uses Overwrite Only as the application update mode. Updates needed for the bootloader projects used in the
section 3.4 and 3.5.are addressed.
The creation of the RA6M3 based bootloaders used in section 3.7 and 3.8 are very similar. Wherever there is
a difference in the operation, it will be addressed inline.
Figure 53. Configure Highlighted Properties for the MbedTLS (Crypto Only) Stack
Figure 64. Example Public Keys and Private Keys Included in MCUboot Port Stack
To use the example keys, select Add Example Keys > New > MCUboot Example Keys (NOT FOR
PRODUCTION).
Figure 66. Standalone Example Projects for RA6M4 with No Bootloader support
5.1.2 Configure the Application Projects to Use the Bootloader
We will now alter the project Properties configuration to have it use the bootloader. Right-click on the
app_ra6m4_s_primary folder in the Project Explorer and select Properties. Select C/C++ Build>Build
Variables, click Add and set the Variable name to BootloaderDataFile and check the Apply to all
configurations box. Change the Type to File and enter
${workspace_loc:ra_mcuboot_ra6m4}/Debug/ra_mcuboot_ra6m4.bld for the value. Click OK to save the
changes.
5.2.1 Click Generate Project Content and Compile All Four Application Projects
For both Primary and Secondary applications, compile the Secure application first and then the Non-Secure
application.
5.2.2 Configure the debug configuration
1. Open the Debug Configurations: app_ra6m4_s_primary > Debug As > Debug Configurations
Make sure that app_ra6m4_s_primary Debug is selected and select the Startup tab.
Figure 76. Select to load Symbols only for the Secure and Non-Secure Project
3. Add the signed binary image to the download options using Raw Binary Load type.
6. Appendix
6.1 Making the Bootloader for Cortex-M33 Immutable
To make the bootloader immutable, you must lock the flash blocks containing the bootloader from being
programmed and erased.
The RA6M4 features two sets of registers which facilitate flash block locking. Block Protect Setting (BPS)
Registers feature bits that map to individual flash blocks. When a bit is set to zero, the corresponding flash
block cannot be erased or programmed. The Permanent Block Protect Setting (PBPS) Registers have a
similar bit mapping to flash blocks. When a bit is set in one of these registers, the corresponding flash block
is permanently locked from being erased and programmed if the same bit in the Block Protect Setting
Register is also cleared to zero. This process is irreversible. Once a flash block is permanently locked, it
cannot be unlocked again.
Based on the example bootloaders provided in this application project, the flash blocks used by the
bootloader are:
• RA6M4 Overwrite Mode: block 0-7
• RA6M4 Swap Mode: block 0-8
• RA6M3 Overwrite Mode: block 0-7
Refer to the RA Family MCU Securing Data at Rest using TrustZone Application Project to understand the
operational flow of setting up the Flash Block Protection.
Note that ticking the BSP0 and PBPS0 Flash Block settings will permanently lock the flash blocks. This
CANNOT be reversed. Further details can be found in sections 6.2.6 and 6.2.7 of the RA6M4 Hardware
User’s Manual.
6.2 Making the Bootloader for Cortex-M4 Immutable
Refer to the Renesas RA MCU Family Securing Data at Rest Utilizing the Renesas Security MPU application
project section Permanent Locking of the FAW Region to understand how to make the bootloader for Cortex-
M4 Immutable. Section PC Application to Permanently Lock the FAW in the same application note describes
how to handle Flash locking in production mode.
6.3 Device Lifecycle Management for Renesas RA Cortex-M33 MCUs
Once the bootloader development is finished, you may want to transition the Device Lifecycle State of the RA
Cortex-M33 MCU to lock down the debugger and the serial programming interface.
We recommend referring to the Device Lifecycle State Transitions in the Production Flow section in the
Renesas RA Family MCU Device Lifecycle Management Key Installation Application Note to understand the
device lifecycle management options during production.
The operational overview of how to use Renesas Flash Programmer to perform these transitions are
explained in the Overview of Device Lifecycle State Transitions using Renesas Flash Programmer section.
Trademarks
Renesas and the Renesas logo are trademarks of Renesas Electronics
Corporation. All trademarks and registered trademarks are the property
of their respective owners.