Skip to content

Commit 6afa709

Browse files
authored
Merge pull request #7 from arduino/boot_main
Build MCUboot as library and create custom main function
2 parents 693aaa0 + f92beb3 commit 6afa709

File tree

7 files changed

+81
-71
lines changed

7 files changed

+81
-71
lines changed
File renamed without changes.

default_bd.cpp renamed to app/default_bd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include "ota.h"
2222
#include "rtc.h"
23-
#include "target.h"
23+
#include "board.h"
2424
#include "bootutil/bootutil_log.h"
2525

2626
#include "SlicingBlockDevice.h"

app/dfu/usbd_conf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#if MCUBOOT_APPLICATION_HOOKS && MCUBOOT_APPLICATION_DFU
2121

2222
/* Includes ------------------------------------------------------------------ */
23-
#include "target.h"
23+
#include "board.h"
2424

2525
/* Private typedef ----------------------------------------------------------- */
2626
/* Private define ------------------------------------------------------------ */

app/dfu/usbd_dfu_flash.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "usbd_dfu_flash.h"
2424
//#include "option_bits.h"
2525
#include "mbed.h"
26-
#include "target.h"
26+
#include "board.h"
2727
#include "BlockDevice.h"
2828
#include "FlashSimBlockDevice.h"
2929
#include "flash_map_backend/secondary_bd.h"

app/target.cpp renamed to app/main.cpp

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
#if MCUBOOT_APPLICATION_HOOKS
2020

2121
#include "mbed.h"
22-
#include "target.h"
22+
#include "board.h"
2323
#include "ota.h"
2424
#include "rtc.h"
2525
#include "bootutil/bootutil_log.h"
26+
#include "bootutil/bootutil.h"
27+
#include "bootutil/image.h"
28+
#include "mbedtls/platform.h"
2629

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

139-
int target_init(void) {
142+
int start_secure_application(void) {
143+
144+
int rc;
145+
146+
BOOT_LOG_INF("Starting MCUboot");
147+
148+
// Initialize mbedtls crypto for use by MCUboot
149+
mbedtls_platform_context unused_ctx;
150+
rc = mbedtls_platform_setup(&unused_ctx);
151+
if(rc != 0) {
152+
BOOT_LOG_ERR("Failed to setup Mbed TLS, error: %d", rc);
153+
return -1;
154+
}
155+
156+
struct boot_rsp rsp;
157+
rc = boot_go(&rsp);
158+
if(rc != 0) {
159+
BOOT_LOG_ERR("Failed to locate firmware image, error: %d\n", rc);
160+
return -1;
161+
}
162+
163+
target_led_off();
164+
165+
// Run the application in the primary slot
166+
// Add header size offset to calculate the actual start address of application
167+
uint32_t address = rsp.br_image_off + rsp.br_hdr->ih_hdr_size;
168+
BOOT_LOG_INF("Booting firmware image at 0x%x\n", address);
169+
mbed_start_application(address);
170+
}
171+
172+
int main(void) {
173+
174+
target_debug_init();
175+
176+
BOOT_LOG_INF("Starting Arduino bootloader");
177+
140178
int magic = RTCGetBKPRegister(RTC_BKP_DR0);
141179

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

237275
HAL_Delay(10);
238276

239-
if (magic == 0xDF59) {
240-
/* Boot stopped by double reset */
241-
return 1;
242-
}
277+
if (magic != 0xDF59) {
278+
if (target_empty_keys()) {
279+
BOOT_LOG_INF("Secure keys not configured");
280+
if ( magic == 0x07AA ) {
281+
/* Try unsecure OTA */
282+
// DR1 contains the backing storage type, DR2 the offset in case of raw device / MBR
283+
storageType storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1);
284+
uint32_t offset = RTCGetBKPRegister(RTC_BKP_DR2);
285+
uint32_t update_size = RTCGetBKPRegister(RTC_BKP_DR3);
286+
BOOT_LOG_INF("Start OTA 0x%X 0x%X 0x%X", storage_type, offset, update_size);
287+
int ota_result = tryOTA(storage_type, offset, update_size);
288+
if (ota_result == 0) {
289+
// clean reboot with success flag
290+
BOOT_LOG_INF("Sketch updated");
291+
RTCSetBKPRegister(RTC_BKP_DR0, 0);
292+
HAL_FLASH_Lock();
293+
// wait for external reboot (watchdog)
294+
while (1) {}
295+
} else {
296+
RTCSetBKPRegister(RTC_BKP_DR0, ota_result);
297+
}
298+
}
243299

244-
if (target_empty_keys()) {
245-
BOOT_LOG_INF("Secure keys not configured");
246-
if ( magic == 0x07AA ) {
247-
/* Try unsecure OTA */
248-
// DR1 contains the backing storage type, DR2 the offset in case of raw device / MBR
249-
storageType storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1);
250-
uint32_t offset = RTCGetBKPRegister(RTC_BKP_DR2);
251-
uint32_t update_size = RTCGetBKPRegister(RTC_BKP_DR3);
252-
BOOT_LOG_INF("Start OTA 0x%X 0x%X 0x%X", storage_type, offset, update_size);
253-
int ota_result = tryOTA(storage_type, offset, update_size);
254-
if (ota_result == 0) {
255-
// clean reboot with success flag
256-
BOOT_LOG_INF("Sketch updated");
300+
if (valid_application()) {
301+
/* Boot Sketch */
302+
BOOT_LOG_INF("Booting sketch at 0x%x\n", APP_DEFAULT_ADD);
257303
RTCSetBKPRegister(RTC_BKP_DR0, 0);
258-
HAL_FLASH_Lock();
259-
// wait for external reboot (watchdog)
260-
while (1) {}
304+
mbed_start_application(APP_DEFAULT_ADD);
261305
} else {
262-
RTCSetBKPRegister(RTC_BKP_DR0, ota_result);
306+
BOOT_LOG_INF("No sketch found");
263307
}
264-
}
265308

266-
if (valid_application()) {
267-
/* Boot Sketch */
268-
BOOT_LOG_INF("Booting sketch at 0x%x\n", APP_DEFAULT_ADD);
269-
mbed_start_application(APP_DEFAULT_ADD);
270309
} else {
271-
BOOT_LOG_INF("No sketch found");
272-
return 1;
310+
/* MCUboot secure boot */
311+
swap_ticker.attach(&swap_feedback, 250ms);
312+
RTCSetBKPRegister(RTC_BKP_DR0, 0);
313+
start_secure_application();
273314
}
274-
275-
} else {
276-
/* MCUboot secure boot */
277-
swap_ticker.attach(&swap_feedback, 250ms);
278-
return 0;
279315
}
316+
target_loop();
317+
318+
return 0;
280319
}
281320

282321
#if MCUBOOT_APPLICATION_DFU

mbed_app.json

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,6 @@
1515
"mbed-trace.enable": false,
1616
"mbed-trace.fea-ipv6": false
1717
},
18-
"NRF52840_DK": {
19-
"target.features_remove": ["CRYPTOCELL310"],
20-
"target.macros_remove": ["MBEDTLS_CONFIG_HW_SUPPORT"],
21-
"mcuboot.primary-slot-address": "0x20000",
22-
"mcuboot.slot-size": "0xC0000",
23-
"mcuboot.scratch-address": "0xE0000",
24-
"mcuboot.scratch-size": "0x20000",
25-
"mcuboot.max-img-sectors": "0x180",
26-
"mcuboot.read-granularity": 4,
27-
"qspif.QSPI_MIN_PROG_SIZE": 4
28-
},
29-
"EP_AGORA": {
30-
"target.features_remove": ["CRYPTOCELL310"],
31-
"target.macros_remove": ["MBEDTLS_CONFIG_HW_SUPPORT"],
32-
"mcuboot.primary-slot-address": "0x20000",
33-
"mcuboot.slot-size": "0xC0000",
34-
"mcuboot.scratch-address": "0xE0000",
35-
"mcuboot.scratch-size": "0x20000",
36-
"mcuboot.max-img-sectors": "0x180",
37-
"mcuboot.read-granularity": 4,
38-
"qspif.QSPI_MIN_PROG_SIZE": 4
39-
},
40-
"DISCO_L475VG_IOT01A": {
41-
"mcuboot.primary-slot-address": "0x8020000",
42-
"mcuboot.slot-size": "0xC0000",
43-
"mcuboot.scratch-address": "0x80E0000",
44-
"mcuboot.scratch-size": "0x20000",
45-
"mcuboot.max-img-sectors": "0x180",
46-
"mcuboot.read-granularity": 1,
47-
"qspif.QSPI_MIN_PROG_SIZE": 1
48-
},
4918
"PORTENTA_H7_M7": {
5019
"target.clock_source": "USE_PLL_HSE_EXTC",
5120
"target.use-mpu": false,
@@ -75,7 +44,8 @@
7544
"mcuboot.application-dfu": true,
7645
"mcuboot.signature-algorithm": "SIGNATURE_TYPE_EC256",
7746
"mcuboot.encrypt-ec256": true,
78-
"mcuboot.include-keys": null
47+
"mcuboot.include-keys": null,
48+
"mcuboot.bootloader-build": false
7949
}
8050
}
8151
}

mbed_app_bootutil.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
"mcuboot.application-dfu": null,
7676
"mcuboot.signature-algorithm": "SIGNATURE_TYPE_EC256",
7777
"mcuboot.encrypt-ec256": true,
78-
"mcuboot.include-keys": null
78+
"mcuboot.include-keys": null,
79+
"mcuboot.bootloader-build": false
7980
}
8081
}
8182
}

0 commit comments

Comments
 (0)