@@ -26,22 +26,53 @@ int main()
26
26
mbed_trace_init ();
27
27
mbed_trace_include_filters_set (" main,MCUb,BL" );
28
28
29
- /* *
30
- * Do whatever is needed to verify the firmware is okay
31
- * (eg: self test, connect to server, etc)
32
- *
33
- * And then mark that the update succeeded
34
- */
35
- // run_self_test();
36
- int ret = boot_set_confirmed ();
37
- if (ret == 0 ) {
38
- tr_info (" Boot confirmed" );
39
- } else {
40
- tr_error (" Failed to confirm boot: %d" , ret);
41
- }
42
-
43
29
InterruptIn btn (DEMO_BUTTON);
44
30
31
+ // Check if an update has been performed
32
+ int swap_type = boot_swap_type ();
33
+ int ret;
34
+ switch (swap_type) {
35
+ case BOOT_SWAP_TYPE_NONE:
36
+ tr_info (" Regular boot" );
37
+ break ;
38
+
39
+ case BOOT_SWAP_TYPE_REVERT:
40
+ // After MCUboot has swapped a (non-permanent) update image
41
+ // into the primary slot, it defaults to reverting the image on the NEXT boot.
42
+ // This is why we see "[INFO][MCUb]: Swap type: revert" which can be misleading.
43
+ // Confirming the CURRENT boot dismisses the reverting.
44
+ tr_info (" Firmware update applied successfully" );
45
+
46
+ // Do whatever is needed to verify the firmware is okay (eg: self test)
47
+ // then mark the update as successful. Here we let the user press a button.
48
+ tr_info (" Press the button to confirm, or reboot to revert the update" );
49
+ #if DEMO_BUTTON_ACTIVE_LOW
50
+ while (btn) {
51
+ #else
52
+ while (!btn) {
53
+ #endif
54
+ sleep ();
55
+ }
56
+
57
+ ret = boot_set_confirmed ();
58
+ if (ret == 0 ) {
59
+ tr_info (" Current firmware set as confirmed" );
60
+ return 0 ;
61
+ } else {
62
+ tr_error (" Failed to confirm the firmware: %d" , ret);
63
+ }
64
+ break ;
65
+
66
+ // Note: Below are intermediate states of MCUboot and
67
+ // should never reach the application...
68
+ case BOOT_SWAP_TYPE_FAIL: // Unable to boot due to invalid image
69
+ case BOOT_SWAP_TYPE_PERM: // Permanent update requested (when signing the image) and to be performed
70
+ case BOOT_SWAP_TYPE_TEST: // Revertable update requested and to be performed
71
+ case BOOT_SWAP_TYPE_PANIC: // Unrecoverable error
72
+ default :
73
+ tr_error (" Unexpected swap type: %d" , swap_type);
74
+ }
75
+
45
76
// Erase secondary slot
46
77
// On the first boot, the secondary BlockDevice needs to be clean
47
78
// If the first boot is not normal, please run the erase step, then reboot
0 commit comments