Skip to content

Build MCUboot as library and create custom main function #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
2 changes: 1 addition & 1 deletion default_bd.cpp → app/default_bd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "ota.h"
#include "rtc.h"
#include "target.h"
#include "board.h"
#include "bootutil/bootutil_log.h"

#include "SlicingBlockDevice.h"
Expand Down
2 changes: 1 addition & 1 deletion app/dfu/usbd_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#if MCUBOOT_APPLICATION_HOOKS && MCUBOOT_APPLICATION_DFU

/* Includes ------------------------------------------------------------------ */
#include "target.h"
#include "board.h"

/* Private typedef ----------------------------------------------------------- */
/* Private define ------------------------------------------------------------ */
Expand Down
2 changes: 1 addition & 1 deletion app/dfu/usbd_dfu_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "usbd_dfu_flash.h"
//#include "option_bits.h"
#include "mbed.h"
#include "target.h"
#include "board.h"
#include "BlockDevice.h"
#include "FlashSimBlockDevice.h"
#include "flash_map_backend/secondary_bd.h"
Expand Down
109 changes: 74 additions & 35 deletions app/target.cpp → app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
#if MCUBOOT_APPLICATION_HOOKS

#include "mbed.h"
#include "target.h"
#include "board.h"
#include "ota.h"
#include "rtc.h"
#include "bootutil/bootutil_log.h"
#include "bootutil/bootutil.h"
#include "bootutil/image.h"
#include "mbedtls/platform.h"

// clock source is selected with CLOCK_SOURCE in json config
#define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO)
Expand Down Expand Up @@ -136,7 +139,42 @@ int target_led_off(void) {
return 0;
}

int target_init(void) {
int start_secure_application(void) {

int rc;

BOOT_LOG_INF("Starting MCUboot");

// Initialize mbedtls crypto for use by MCUboot
mbedtls_platform_context unused_ctx;
rc = mbedtls_platform_setup(&unused_ctx);
if(rc != 0) {
BOOT_LOG_ERR("Failed to setup Mbed TLS, error: %d", rc);
return -1;
}

struct boot_rsp rsp;
rc = boot_go(&rsp);
if(rc != 0) {
BOOT_LOG_ERR("Failed to locate firmware image, error: %d\n", rc);
return -1;
}

target_led_off();

// Run the application in the primary slot
// Add header size offset to calculate the actual start address of application
uint32_t address = rsp.br_image_off + rsp.br_hdr->ih_hdr_size;
BOOT_LOG_INF("Booting firmware image at 0x%x\n", address);
mbed_start_application(address);
}

int main(void) {

target_debug_init();

BOOT_LOG_INF("Starting Arduino bootloader");

int magic = RTCGetBKPRegister(RTC_BKP_DR0);

// in case we have been reset let's wait 500 ms to see if user is trying to stay in bootloader
Expand Down Expand Up @@ -236,47 +274,48 @@ int target_init(void) {

HAL_Delay(10);

if (magic == 0xDF59) {
/* Boot stopped by double reset */
return 1;
}
if (magic != 0xDF59) {
if (target_empty_keys()) {
BOOT_LOG_INF("Secure keys not configured");
if ( magic == 0x07AA ) {
/* Try unsecure OTA */
// DR1 contains the backing storage type, DR2 the offset in case of raw device / MBR
storageType storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1);
uint32_t offset = RTCGetBKPRegister(RTC_BKP_DR2);
uint32_t update_size = RTCGetBKPRegister(RTC_BKP_DR3);
BOOT_LOG_INF("Start OTA 0x%X 0x%X 0x%X", storage_type, offset, update_size);
int ota_result = tryOTA(storage_type, offset, update_size);
if (ota_result == 0) {
// clean reboot with success flag
BOOT_LOG_INF("Sketch updated");
RTCSetBKPRegister(RTC_BKP_DR0, 0);
HAL_FLASH_Lock();
// wait for external reboot (watchdog)
while (1) {}
} else {
RTCSetBKPRegister(RTC_BKP_DR0, ota_result);
}
}

if (target_empty_keys()) {
BOOT_LOG_INF("Secure keys not configured");
if ( magic == 0x07AA ) {
/* Try unsecure OTA */
// DR1 contains the backing storage type, DR2 the offset in case of raw device / MBR
storageType storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1);
uint32_t offset = RTCGetBKPRegister(RTC_BKP_DR2);
uint32_t update_size = RTCGetBKPRegister(RTC_BKP_DR3);
BOOT_LOG_INF("Start OTA 0x%X 0x%X 0x%X", storage_type, offset, update_size);
int ota_result = tryOTA(storage_type, offset, update_size);
if (ota_result == 0) {
// clean reboot with success flag
BOOT_LOG_INF("Sketch updated");
if (valid_application()) {
/* Boot Sketch */
BOOT_LOG_INF("Booting sketch at 0x%x\n", APP_DEFAULT_ADD);
RTCSetBKPRegister(RTC_BKP_DR0, 0);
HAL_FLASH_Lock();
// wait for external reboot (watchdog)
while (1) {}
mbed_start_application(APP_DEFAULT_ADD);
} else {
RTCSetBKPRegister(RTC_BKP_DR0, ota_result);
BOOT_LOG_INF("No sketch found");
}
}

if (valid_application()) {
/* Boot Sketch */
BOOT_LOG_INF("Booting sketch at 0x%x\n", APP_DEFAULT_ADD);
mbed_start_application(APP_DEFAULT_ADD);
} else {
BOOT_LOG_INF("No sketch found");
return 1;
/* MCUboot secure boot */
swap_ticker.attach(&swap_feedback, 250ms);
RTCSetBKPRegister(RTC_BKP_DR0, 0);
start_secure_application();
}

} else {
/* MCUboot secure boot */
swap_ticker.attach(&swap_feedback, 250ms);
return 0;
}
target_loop();

return 0;
}

#if MCUBOOT_APPLICATION_DFU
Expand Down
34 changes: 2 additions & 32 deletions mbed_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,6 @@
"mbed-trace.enable": false,
"mbed-trace.fea-ipv6": false
},
"NRF52840_DK": {
"target.features_remove": ["CRYPTOCELL310"],
"target.macros_remove": ["MBEDTLS_CONFIG_HW_SUPPORT"],
"mcuboot.primary-slot-address": "0x20000",
"mcuboot.slot-size": "0xC0000",
"mcuboot.scratch-address": "0xE0000",
"mcuboot.scratch-size": "0x20000",
"mcuboot.max-img-sectors": "0x180",
"mcuboot.read-granularity": 4,
"qspif.QSPI_MIN_PROG_SIZE": 4
},
"EP_AGORA": {
"target.features_remove": ["CRYPTOCELL310"],
"target.macros_remove": ["MBEDTLS_CONFIG_HW_SUPPORT"],
"mcuboot.primary-slot-address": "0x20000",
"mcuboot.slot-size": "0xC0000",
"mcuboot.scratch-address": "0xE0000",
"mcuboot.scratch-size": "0x20000",
"mcuboot.max-img-sectors": "0x180",
"mcuboot.read-granularity": 4,
"qspif.QSPI_MIN_PROG_SIZE": 4
},
"DISCO_L475VG_IOT01A": {
"mcuboot.primary-slot-address": "0x8020000",
"mcuboot.slot-size": "0xC0000",
"mcuboot.scratch-address": "0x80E0000",
"mcuboot.scratch-size": "0x20000",
"mcuboot.max-img-sectors": "0x180",
"mcuboot.read-granularity": 1,
"qspif.QSPI_MIN_PROG_SIZE": 1
},
"PORTENTA_H7_M7": {
"target.clock_source": "USE_PLL_HSE_EXTC",
"target.use-mpu": false,
Expand Down Expand Up @@ -75,7 +44,8 @@
"mcuboot.application-dfu": true,
"mcuboot.signature-algorithm": "SIGNATURE_TYPE_EC256",
"mcuboot.encrypt-ec256": true,
"mcuboot.include-keys": null
"mcuboot.include-keys": null,
"mcuboot.bootloader-build": false
}
}
}
3 changes: 2 additions & 1 deletion mbed_app_bootutil.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"mcuboot.application-dfu": null,
"mcuboot.signature-algorithm": "SIGNATURE_TYPE_EC256",
"mcuboot.encrypt-ec256": true,
"mcuboot.include-keys": null
"mcuboot.include-keys": null,
"mcuboot.bootloader-build": false
}
}
}