From 00859f2f4e46ae5e2c17d024b2090868426a7396 Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Sat, 14 Dec 2024 11:34:45 -0800 Subject: [PATCH] Use CMake to generate the images --- .github/workflows/compile.yml | 11 +++++++++-- CMakeLists.txt | 7 +++++-- UpdaterApp.cpp | 29 ++++++++++++++--------------- mbed_app.json5 | 2 ++ mcuboot | 2 +- secondary_bd.cpp | 4 ++-- 6 files changed, 33 insertions(+), 22 deletions(-) diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml index 5187ef1..2d8d87e 100644 --- a/.github/workflows/compile.yml +++ b/.github/workflows/compile.yml @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index ff2c181..bcfade7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -23,6 +23,7 @@ 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 @@ -30,12 +31,14 @@ target_link_libraries(UpdaterApp 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,$.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() \ No newline at end of file diff --git a/UpdaterApp.cpp b/UpdaterApp.cpp index 968c52d..a6c21ce 100644 --- a/UpdaterApp.cpp +++ b/UpdaterApp.cpp @@ -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() { @@ -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 @@ -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) { @@ -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"); diff --git a/mbed_app.json5 b/mbed_app.json5 index a65ab69..2eb9ef4 100644 --- a/mbed_app.json5 +++ b/mbed_app.json5 @@ -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", diff --git a/mcuboot b/mcuboot index 464c796..b35a239 160000 --- a/mcuboot +++ b/mcuboot @@ -1 +1 @@ -Subproject commit 464c79661da3409ebe4abb8c4bb09f28d5ec5907 +Subproject commit b35a239456f5740df5da66920f4cb53cdc429576 diff --git a/secondary_bd.cpp b/secondary_bd.cpp index 52c1ba6..ddb8ad7 100644 --- a/secondary_bd.cpp +++ b/secondary_bd.cpp @@ -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