Skip to content

Commit

Permalink
Use CMake to generate the images
Browse files Browse the repository at this point in the history
  • Loading branch information
multiplemonomials committed Dec 14, 2024
1 parent 57b5dc6 commit 00859f2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ jobs:
run: |
apt-get update
apt-get install -y python3-venv
- name: Generate transient key pair
run: |
python3 -m pip install --user --upgrade pyopenssl # Work around runtime error
python3 -m pip install --user -r mcuboot/scripts/requirements.txt
python3 -m pip install --user mcuboot/scripts
$HOME/.local/bin/imgtool keygen -k signing-keys.pem -t rsa-2048
- name: Build project for ${{ matrix.mbed_target }}
run: |
mkdir build && cd build
cmake .. -GNinja -DMBED_TARGET=${{ matrix.mbed_target }}
cmake .. -GNinja -DMBED_TARGET=${{ matrix.mbed_target }} -DMCUBOOT_SIGNING_KEY=signing-keys.pem
ninja
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set(MBED_OUTPUT_EXT "" CACHE STRING "" FORCE)

add_subdirectory(mbed-os)

project(mbed-mcuboot-demo-app)
project(mbed-mcuboot-demo-app VERSION 1.0.0)

# Compile mcuboot sources
add_subdirectory(mcuboot/boot/bootutil)
Expand All @@ -23,19 +23,22 @@ target_link_libraries(SimpleApp
mbed-storage
mbed-mcuboot)
mbed_set_post_build(SimpleApp)
mcuboot_generate_update_image(SimpleApp)

add_executable(UpdaterApp UpdaterApp.cpp secondary_bd.cpp)
target_link_libraries(UpdaterApp
mbed-os
mbed-storage
mbed-mcuboot)
mbed_set_post_build(UpdaterApp)
mcuboot_generate_initial_image(UpdaterApp)
mcuboot_generate_update_image(UpdaterApp)

# Time for a bit of CMake magic: we take the bin file for SimpleApp, then link it into
# UpdaterApp as block of constant data. The name in code is based on the filename passed
# here to the linker.
# See here for more details on this: https://gareus.org/wiki/embedding_resources_in_executables
target_link_options(UpdaterApp PRIVATE -Wl,-b,binary,$<TARGET_FILE_BASE_NAME:SimpleApp>.bin -Wl,-b,elf32-littlearm)
target_link_options(UpdaterApp PRIVATE -Wl,-b,binary,SimpleApp-update-image.bin -Wl,-b,elf32-littlearm)
add_dependencies(UpdaterApp SimpleApp) # Ensure SimpleApp gets built before UpdaterApp

mbed_finalize_build()
29 changes: 14 additions & 15 deletions UpdaterApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

// SimpleApp.bin gets loaded into ram between these addresses.
// See CMakeLists.txt for details on how this is done.
extern "C" uint8_t _binary_SimpleApp_bin_start;
extern "C" uint8_t _binary_SimpleApp_bin_end;
const size_t SimpleApp_bin_length = &_binary_SimpleApp_bin_end - &_binary_SimpleApp_bin_start;
extern "C" uint8_t _binary_SimpleApp_update_image_bin_start;
extern "C" uint8_t _binary_SimpleApp_update_image_bin_end;
const size_t SimpleApp_update_image_bin_length = &_binary_SimpleApp_update_image_bin_end - &_binary_SimpleApp_update_image_bin_start;

int main()
{
Expand All @@ -37,6 +37,16 @@ int main()

DigitalIn btn(DEMO_BUTTON);

// Use a buffered block device to allow arbitrary length writes to the underlying BD
BlockDevice *secondary_bd = get_secondary_bd();
BufferedBlockDevice bufferedSecBD(secondary_bd);
int ret = bufferedSecBD.init();
if (ret == 0) {
tr_info("Secondary BlockDevice inited");
} else {
tr_error("Cannot init secondary BlockDevice: %d", ret);
}

// Erase secondary slot
// On the first boot, the secondary BlockDevice needs to be clean
// If the first boot is not normal, please run the erase step, then reboot
Expand All @@ -47,17 +57,6 @@ int main()
ThisThread::sleep_for(10ms);
}

BlockDevice *secondary_bd = get_secondary_bd();
int ret = secondary_bd->init();
if (ret == 0) {
tr_info("Secondary BlockDevice inited");
} else {
tr_error("Cannot init secondary BlockDevice: %d", ret);
}

// Use a buffered block device to allow arbitrary length writes to the underlying BD
BufferedBlockDevice bufferedSecBD(secondary_bd);

tr_info("Erasing secondary BlockDevice...");
ret = bufferedSecBD.erase(0, bufferedSecBD.size());
if (ret == 0) {
Expand All @@ -73,7 +72,7 @@ int main()
}

// Copy the update image from internal flash to secondary BlockDevice
bufferedSecBD.program(&_binary_SimpleApp_bin_start, 0, SimpleApp_bin_length);
bufferedSecBD.program(&_binary_SimpleApp_update_image_bin_start, 0, SimpleApp_update_image_bin_length);

// Activate the image in the secondary BlockDevice
tr_info("> Image copied to secondary BlockDevice, press button to activate");
Expand Down
2 changes: 2 additions & 0 deletions mbed_app.json5
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
}
},

"demo-button-active-low": true,

// On K64F we store the secondary slot in external memory, not internal.
// So, the primary slot can take up most of flash.
"mcuboot.primary-slot-address": "0x20000",
Expand Down
2 changes: 1 addition & 1 deletion mcuboot
4 changes: 2 additions & 2 deletions secondary_bd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mbed::BlockDevice* get_secondary_bd(void) {

// In this case, our flash is much larger than a single image so
// slice it into the size of an image slot
static mbed::SlicingBlockDevice sliced_bd(default_bd, 0x0, MCUBOOT_SLOT_SIZE);
return &sliced_bd;
//static mbed::SlicingBlockDevice sliced_bd(default_bd, 0x0, MCUBOOT_SLOT_SIZE);
return default_bd;
}
#endif

0 comments on commit 00859f2

Please sign in to comment.