|
19 | 19 | #if MCUBOOT_APPLICATION_HOOKS
|
20 | 20 |
|
21 | 21 | #include "mbed.h"
|
22 |
| -#include "target.h" |
| 22 | +#include "board.h" |
23 | 23 | #include "ota.h"
|
24 | 24 | #include "rtc.h"
|
25 | 25 | #include "bootutil/bootutil_log.h"
|
| 26 | +#include "bootutil/bootutil.h" |
| 27 | +#include "bootutil/image.h" |
| 28 | +#include "mbedtls/platform.h" |
26 | 29 |
|
27 | 30 | // clock source is selected with CLOCK_SOURCE in json config
|
28 | 31 | #define USE_PLL_HSE_EXTC 0x8 // Use external clock (ST Link MCO)
|
@@ -136,7 +139,42 @@ int target_led_off(void) {
|
136 | 139 | return 0;
|
137 | 140 | }
|
138 | 141 |
|
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 | + |
140 | 178 | int magic = RTCGetBKPRegister(RTC_BKP_DR0);
|
141 | 179 |
|
142 | 180 | // 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) {
|
236 | 274 |
|
237 | 275 | HAL_Delay(10);
|
238 | 276 |
|
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 | + } |
243 | 299 |
|
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); |
257 | 303 | 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); |
261 | 305 | } else {
|
262 |
| - RTCSetBKPRegister(RTC_BKP_DR0, ota_result); |
| 306 | + BOOT_LOG_INF("No sketch found"); |
263 | 307 | }
|
264 |
| - } |
265 | 308 |
|
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); |
270 | 309 | } 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(); |
273 | 314 | }
|
274 |
| - |
275 |
| - } else { |
276 |
| - /* MCUboot secure boot */ |
277 |
| - swap_ticker.attach(&swap_feedback, 250ms); |
278 |
| - return 0; |
279 | 315 | }
|
| 316 | + target_loop(); |
| 317 | + |
| 318 | + return 0; |
280 | 319 | }
|
281 | 320 |
|
282 | 321 | #if MCUBOOT_APPLICATION_DFU
|
|
0 commit comments