Skip to content

Commit dcd2e6d

Browse files
Fix missing slicing BD, add workaround for SD cards
1 parent 4111457 commit dcd2e6d

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

UpdaterApp.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ int main()
3737

3838
DigitalIn btn(DEMO_BUTTON);
3939

40-
// Use a buffered block device to allow arbitrary length writes to the underlying BD
4140
BlockDevice *secondary_bd = get_secondary_bd();
42-
BufferedBlockDevice bufferedSecBD(secondary_bd);
43-
int ret = bufferedSecBD.init();
41+
int ret = secondary_bd->init();
4442
if (ret == 0) {
4543
tr_info("Secondary BlockDevice inited");
4644
} else {
@@ -58,21 +56,38 @@ int main()
5856
}
5957

6058
tr_info("Erasing secondary BlockDevice...");
61-
ret = bufferedSecBD.erase(0, bufferedSecBD.size());
59+
ret = secondary_bd->erase(0, secondary_bd->size());
6260
if (ret == 0) {
6361
tr_info("Secondary BlockDevice erased");
6462
} else {
6563
tr_error("Cannot erase secondary BlockDevice: %d", ret);
6664
}
6765

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+
6876
tr_info("> Press button to copy update image to secondary BlockDevice");
6977

7078
while(!DEMO_BUTTON_IS_PRESSED) {
7179
ThisThread::sleep_for(10ms);
7280
}
7381

7482
// 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+
}
7691

7792
// Activate the image in the secondary BlockDevice
7893
tr_info("> Image copied to secondary BlockDevice, press button to activate");

secondary_bd.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "SlicingBlockDevice.h"
1111
#include "FlashIAPBlockDevice.h"
12+
#include "BufferedBlockDevice.h"
1213

1314
#if MBED_CONF_APP_SECONDARY_SLOT_IN_FLASH
1415

@@ -35,9 +36,14 @@ mbed::BlockDevice* get_secondary_bd(void) {
3536
// If this assert fails, there is no block def
3637
MBED_ASSERT(default_bd != nullptr);
3738

39+
// mcuboot assumes that the read size of the secondary block device is the same as the read size
40+
// of flash, so use a BufferedBlockDevice to wrap the underlying BD and ensure this is the case.
41+
static mbed::BufferedBlockDevice buffered_bd(default_bd);
42+
3843
// In this case, our flash is much larger than a single image so
3944
// slice it into the size of an image slot
40-
//static mbed::SlicingBlockDevice sliced_bd(default_bd, 0x0, MCUBOOT_SLOT_SIZE);
41-
return default_bd;
45+
static mbed::SlicingBlockDevice sliced_bd(&buffered_bd, 0x0, MCUBOOT_SLOT_SIZE);
46+
47+
return &sliced_bd;
4248
}
4349
#endif

0 commit comments

Comments
 (0)