@@ -37,10 +37,8 @@ int main()
37
37
38
38
DigitalIn btn (DEMO_BUTTON);
39
39
40
- // Use a buffered block device to allow arbitrary length writes to the underlying BD
41
40
BlockDevice *secondary_bd = get_secondary_bd ();
42
- BufferedBlockDevice bufferedSecBD (secondary_bd);
43
- int ret = bufferedSecBD.init ();
41
+ int ret = secondary_bd->init ();
44
42
if (ret == 0 ) {
45
43
tr_info (" Secondary BlockDevice inited" );
46
44
} else {
@@ -58,21 +56,38 @@ int main()
58
56
}
59
57
60
58
tr_info (" Erasing secondary BlockDevice..." );
61
- ret = bufferedSecBD. erase (0 , bufferedSecBD. size ());
59
+ ret = secondary_bd-> erase (0 , secondary_bd-> size ());
62
60
if (ret == 0 ) {
63
61
tr_info (" Secondary BlockDevice erased" );
64
62
} else {
65
63
tr_error (" Cannot erase secondary BlockDevice: %d" , ret);
66
64
}
67
65
66
+ // Workaround: for block devices such as MicroSD cards where there is no fixed erase value,
67
+ // mcuboot will think that the "magic" region on the secondary BD, which it uses to store
68
+ // state info, is corrupt instead of simply not written yet.
69
+ // To fix this, we need to actually fill the last 40 bytes with 0xFFs.
70
+ if (secondary_bd->get_erase_value () == -1 )
71
+ {
72
+ const std::vector<uint8_t > writeBuffer (40 , 0xFF );
73
+ secondary_bd->program (writeBuffer.data (), secondary_bd->size () - 40 , 40 );
74
+ }
75
+
68
76
tr_info (" > Press button to copy update image to secondary BlockDevice" );
69
77
70
78
while (!DEMO_BUTTON_IS_PRESSED) {
71
79
ThisThread::sleep_for (10ms);
72
80
}
73
81
74
82
// Copy the update image from internal flash to secondary BlockDevice
75
- bufferedSecBD.program (&_binary_SimpleApp_update_image_bin_start, 0 , SimpleApp_update_image_bin_length);
83
+ secondary_bd->program (&_binary_SimpleApp_update_image_bin_start, 0 , SimpleApp_update_image_bin_length);
84
+
85
+ ret = secondary_bd->deinit ();
86
+ if (ret == 0 ) {
87
+ tr_info (" Secondary BlockDevice deinited" );
88
+ } else {
89
+ tr_error (" Cannot deinit secondary BlockDevice: %d" , ret);
90
+ }
76
91
77
92
// Activate the image in the secondary BlockDevice
78
93
tr_info (" > Image copied to secondary BlockDevice, press button to activate" );
0 commit comments