Skip to content

Target mcuboot-image does not upload the signed executable #18

Description

@cfoucher-laas

Describe the bug
On STSTM32 platform, we use the mcuboot-image target to sign the image after building. We had trouble making this target to work flawlessly, as while the firmware is correctly built, it is the pre-mcuboot firmware that gets uploaded to the board. We finally found some workarounds, but they all have drawbacks. Here are the different steps we took:

First try

We started by using the following parameters in platformio.ini:

[env]
board_build.zephyr.bootloader.header_len = 0x200
board_build.zephyr.bootloader.flash_alignment = 8
board_build.zephyr.bootloader.slot_size = 0x37800
board_build.zephyr.bootloader.secondary_slot = 1
board_upload.offset_address = 0x8047800

targets = mcuboot-image

In this case, the firmware is correctly built, but the upload phase will not upload the signed firmware (firmware.mcuboot.bin), only the pre-mcuboot file firmware.bin. At this stage, the workaround was to use an external tool to upload the firmware to the board.

Second try

We identified the following code snippet in STSTM32's main.py:

    if "zephyr" in frameworks and "mcuboot-image" in COMMAND_LINE_TARGETS:
        target_firm = env.MCUbootImage(
            join("$BUILD_DIR", "${PROGNAME}.mcuboot.bin"), target_firm)

[...]

upload_source = target_firm

This lead us to a second try by ensuring the upload phase is done in a single step with the build/sign process. Here, we modified the targets in platformio.ini as follows:

targets = mcuboot-image,upload

There, it works but this is an akward configuration because the "build" button in the bottom bar of VS Code will also upload the file (thus would result in an error when not connected to the board), while the "upload" button still uploads the wrong file.

Third try

Finally, we resorted in a trick consisting in forcing pio scripts use the mcuboot-image even with the single "upload" step, by altering the COMMAND_LINE_TARGETS variable. This uses a "pre" hook as follows:

extra_scripts = pre:upload_signed_firmware.py
targets = mcuboot-image

With the following content in upload_signed_firmware.py:

Import("env")

# Make sure mcuboot-image is in the target list when uploading,
# because pio scripts will behave incorrectly if it isn't
# (wrong file gets uploaded)
if "mcuboot-image" not in COMMAND_LINE_TARGETS:
	COMMAND_LINE_TARGETS.insert(0, "mcuboot-image")

To Reproduce
Steps to reproduce the behavior:

  1. Add targets = mcuboot-image to platformio.ini
  2. Click on the "build" button in VS Code and observe that it produces a file firmware.mcuboot.bin
  3. Click on the "upload" button in VS Code
  4. Watch the uploader upload the wrong file firmware.bin

Expected behavior
When using the mcuboot-image target in platformio.ini, the "upload" phase should upload the final, signed, firmware, not the one produced at an earlier phase of the build process.

Impact
The impact was a time-consuming process of going through the pio scripts and understanding Scons in order to find a workaround. Now that it works, the impact is minimal (only an additional script to ship in our repo).

Environment

  • OS: Linux
  • Toolchain: Platformio toolchain within the STSTM32 platform, using VS Code
  • Commit SHA or Version used: framework-zephyr version 2.30400.230914

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions