Secure Boot on PIC32CM LS60 Curiosity Pro Evaluation Kit Using MPLAB® Harmony v3 Software Framework: Step 3
Now, both the Secure Bootloader and test application projects are created and the code is added. Let us test on target to see the results.
Generate the PIC32CM LS60 User Row Configurations for the Test Application Project
Generate the test application's Unified Binary. A Unified Binary is a single executable file generated by MPLAB® X Integrated Development Environment (IDE) using the Secure and Non-Secure HEX files. This can be used to program both Secure and Non-Secure regions together.
The Secure Bootloader project consists of the following scripts folder which has the following files. These files will be used to run the test application.
scripts folder path:
<Your_Secure_Bootloader_Downloaded_Path>/pic32cm_ls60_cpro_secure_boot/scripts
scripts folder files:
- application_user_configurations_in.txt: This is the input file consisting of PIC32CM LS60 User Row Bitfield configurations for the test application.
- application_user_configurations_out.txt: This is the output file that has the CRC information calculated using the Python™ script for the given input file application_user_configurations_in.txt.
- btl_dev_cfg_gen.py: Used to generate the device configuration
- btl_host.py: Used to load the test application binary from Host-PC to the target device over UART
- secure_bootloader_with_tpds.c: This is the device configuration file for the Secure Bootloader project.
- secure_bootloader_with_tpds_and_application.c: This is the device configuration file for the Secure Bootloader and test application project.
Steps to Generate the PIC32CM LS60 User Row Configurations for the Test Application Project
The pre-generated application_user_configurations_out.txt configuration file will be used by the btl_host.py script to update the fuse configurations while programming the test application.
To know how this file is generated, open the above app_pic32cm_ls60_cpro_NonSecure project in MPLAB Code Configurator (MCC) and check the Device & Project Configuration under the System module. For example, the Memory Configurations of the test application can be seen in the accompanying image.
Go to the
<Your_Secure_Bootloader_Downloaded_Path>/pic32cm_ls60_cpro_secure_boot/scripts folder and execute the following command to show the Help menu for the script.
The syntax for generating the device configuration output file is:
- To know more about btl_dev_cfg_gen.py - refer to the "Device Configuration Generation Script Help" page.
Now, to generate the CRC for the fuse settings, execute the following command:
Secure Boot Configuration | Provisioning | Secure Booting
Secure Boot Provisioning
Steps to program and provision the Secure bootloader code using Mirochip's Trust Platform Design Suite (TPDS).
- Perform the hardware setup steps mentioned in the lab objective, if not done already.
- Open the TPDS application and select the TrustFlex use case, i.e., Secure Boot-PIC32CMLS60.
- This use case will sign an application HEX file to support Secure Boot in combination with the ATECC608 embedded into the PIC32CM LS60.
- The application that is to be signed must be generated as per the current datasheet for Secure Boot.
Generate a Private/Public key pair: Click on Option 1 to Generate a Private/Public key pair to perform the factory programming of the default binary and wait for the response OK.
Again press Option 1 - Generate Private/Public key. In this step, the public key required to verify the SecureBoot is stored in Slot 15. This time a pop-up appears to Generate a Private/Public key, as shown in the accompanying image.
After successful execution of Option 1, a green tick mark appears in Option 1 as shown in the accompanying image, and check the log window for the message "Executed Step-1 Successfully".
After the successful execution of Option 2, a green tick mark appears in Option 2 as shown in the accompanying image, and check the log window for the message "Executed Step-2 Successfully".
In this step, the digest and signature will be generated for the Secure Bootloader HEX file.
- Copy the Secure Bootloader pic32cm_ls60_cpro_Secure.X.production.hex file from:
- <Your_Secure_Bootloader_Project_Path>/secure_boot/hex/secure_bootloader to
- <user_path>/.trustplatform/pic32cmls60_secure_boot
- Load HEX file, calculate digest and signature: Select Option 3 to load the Secure Bootloader HEX file from the TPDS Trust platform folder i.e., <user_path>/.trustplatform/pic32cmls60_secure_boot to calculate the digest and signature.
After the successful execution of Option 3, a green tick mark appears in Option 3 as shown in the accompanying image, and check the log window for the message "Executed Step-3 Successfully".
After the successful execution of Option 4, a green tick mark appears in Option 4 as shown in the accompanying image, and check the log window for the message "Executed Step-4 Successfully".
After the successful execution of Option 5, a green tick mark appears in Option 5 as shown in the accompanying image, and check the log window for the message "Executed Step-5 Successfully".
This completes the use case execution. i.e., the provisioning flow of PIC32CM LS60 MCU.
- Reset or power cycle the device.
Running the Secure Bootloader Application
The successful running of the Secure Bootloader is indicated by turning the green LED0 ON.
- A green LED (LED0) on the PIC32CM LS60 Curiosity Pro Evaluation Kit turns ON when the Secure Bootloader is running.
- Open MPLAB X IDE and you can check the Configuration Bits by going to Window > Target Memory Views > Configuration Bits in the MPLAB X IDE
- The Secure Bootloader fuse configuration bits are stored in the secure_bootloader_with_tpds.c file.
- Here, you can see that the BOOTROM_BOOTOPT value is set to 0x5, which enables the Secure Boot.
- Also, check the BOOTKEY which is set by the user, and IOPROTKEY which is stored by the TPDS.
Receive the test application image over UART from a PC by running a Python script. The successful receiving of the test application is indicated by toggling the red LED.
- Open the Virtual Debug COM port and set the baud rate to 115200.
- Connect the Type-A male to Mini-B USB cable to Mini-B USB-UART Click on board to receive the test application firmware from the Host-PC.
- Note: Install USB-UART serial driver if not installed on your PC.
- Go to the following Secure Bootloader scripts folder path, and open the command prompt to run the bootloader host script btl_host.py.
scripts folder path:
<Your_Secure_Bootloader_Downloaded_Path>/pic32cm_ls60_cpro_secure_boot/scripts
- Execute the following command to show the help menu for the btl_host.py script.
- Copy pic32cm_ls60_cpro_NonSecure.X.production.unified.bin from C:/secure_boot/hex/test_app to <Your_Secure_Bootloader_Downloaded_Path>/pic32cm_ls60_cpro_secure_boot/scripts
- The following command sends the test application's Unified binary from Host-PC to the PIC32CM LS60 Curiosity Pro Evaluation Kit and updates the User Row fuse configurations bits.
- Note: Replace the COM port number with the USB-UART Serial COM port which is enumerated when you connect the Type-A male to Mini-B USB cable to Mini-B USB-UART Click on board.
- See the following output on the command prompt.
- Observe the LED1 (Red-LED) blinking while receiving the test application.
- After receiving the test application's unified binary and update of device configuration bits, remove the Mini-USB from the USB-UART Click board and reset the device.
Red LED1 starts toggling indicating Secure and successful test application booting.
- Now the LED1 (Red-LED) toggles and prints a message in the Virtual COM as shown in the accompanying image, indicating the test application is loaded from the Secure Bootloader.
- Press and hold the Switch SW0 key to trigger the Secure Bootloader to program the test application once again or with the new application.
- Notice that the green LED0 turns ON when the Secure Bootloader is triggered.
- Now open MPLAB X IDE and check the Device Fuse Configuration Bits after programming the test application. Go to Window > Target Memory Views > Configuration Bits in the MPLAB X IDE. The configuration bits are saved in the secure_bootloader_with_tpds.c file.
- In this file, notice that only User Row configurations (i.e., USER WORD) are updated based on the Test Application's configuration, whereas the Boot Configuration Row (i.e., BOCOR WORD) is not modified.
- You can check the diff of the secure_bootloader_with_tpds.c and secure_bootloader_with_tpds_and_application.c present in the scripts folder to see which user configuration bitfields are updated by the Bootloader script based on the application's project configuration.
Impersonating Bootloader
Restoring the Secure Bootloader and Validation
Since the Secure Bootloader is restored, the Secure Bootloader and test application work as expected. Red LED1 starts toggling indicating Secure and successful test application booting.
Results
You successfully created a Secure Bootloader application using the PIC32CM LS60 Curiosity Pro Evaluation and experienced how to develop Secure Bootloader, provisioning steps using the TPDS, and receiving the test application to demonstrate the Secure Booting feature.
Analysis
In this lab, you have successfully created a project from scratch, added Peripheral Libraries (PLIBs), and Bootloader, and learned how to use PIC32CM LS60 Secure Boot features, TrustZone® Configurations, and Bootloader configurations. You also learned how to do the provisioning using the TPDS software.
Conclusions
In this tutorial, you discovered how to configure the device to configure the Secure Boot features, Arm® TrustZone configurations, and Bootloader configurations. This tutorial can be used as a reference when you develop a real-time application where the Secure Booting feature is required.