4 minute read

Serial Firmware updates with MCUboot & Zephyr

Walkthrough video

Result: v1 → v2 over UART

Recently at the Zephyr Robotics Workgroup I committed to doing an investigation on DFU methods. I decided to start with the simplest and most straight forward one. DFU over Serial using MCUboot.

This would be an ideal case if you have an application where there is a connected gateway upstream connected by serial or some serial emulator such as USB, think about gaming keyboards, test and instrumentation applications such as Logic analyzers and such.

The goal today would be to achieve a Firmware update over serial, the tools we will use are

  • FRDM_MCXN236 Board
  • MCUboot
  • MCUmgr Library
  • A SMP Client

Layout: bootloader, slots, signing

Starting from a base application such as blinky we can take the following path

  • Add sysbuild.conf Enable MCUboot on single app mode
    SB_CONFIG_BOOTLOADER_MCUBOOT=y
    SB_CONFIG_MCUBOOT_MODE_SINGLE_APP=y
    
  • Add logging for mcuboot in sysbuild/mcuboot.conf
    CONFIG_LOG=y
    CONFIG_MCUBOOT_LOG_LEVEL_INF=y
    

Enable mcuboot partition ins sysbuild/mcuboot.overlay

/ {
        chosen {
                zephyr,code-partition = &boot_partition;
        };
};

This is the first stage, this enabled MCUboot as our bootloader of choice. However note that just haveing MCUboot does not enable DFU, we would need to leverage at bit more, as we will see later.

This would be a good point to build and test

west build -p -b frdm_mcxn236 --sysbuild applications/dfu_sample/

west flash 

You should see something like

*** Using Zephyr OS build v4.5.0-5481-gcc8bef3d4b6c ***
*** Booting MCUboot v2.5.0 ***
I: Starting bootloader
I: Bootloader chainload address offset: 0x14000
I: Image version: v0.0.5
I: Jumping to the first image slot

Build with sysbuild

Now that we have MCUboot as the bootloader I need to do some general housekeeping. One of the factors we have to be careful with DFU in the wild is that we want to ensure only legitimate firmware is accepted by the device so for this we need to sign our releases prior to sending them to the device

Generate Serial Key

python bootloader/mcuboot/scripts/imgtool.py keygen -t ecdsa-p256 -k applications/dfu_sample/ my_key.pem

Add key to mcuboot in sysbuild.conf

SB_CONFIG_BOOT_SIGNATURE_KEY_FILE="\${APP_DIR}/my_key.pem"

SB_CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256=y

Now updates built without this signature will fail, Ideally you would have this build step happening in a CI server that automates releases with builds so its not hanging around in repositories or developer machines. Ensure to back up this key as without it we will be unable to effect further firmware updates.

A particular feature of SMP I like is being able to do DFU over UART to the application this means we don’t need be in a reset to do an update and this lends itself to having this automated and repeatable.

We need to chane MCUboot to from single app mode and add an SMP Server capability to our application, with Zephyr this is rather straight forward and here how we do it.

Enable Serial Recovery in mcuboot.conf

CONFIG_MCUBOOT_SERIAL=y
CONFIG_BOOT_SERIAL_UART=y
CONFIG_UART_CONSOLE=n
CONFIG_MCUBOOT_INDICATION_LED=y

Add button and led for recovery in mcuboot.overlay

/ {
        aliases {
                mcuboot-button0 = &user_button_2;
                mcuboot-led0 = &green_led;
        };
};

Add a VERSION file to the application is nice so we can tell what the edge is running when debugging and supporting customers

VERSION_MAJOR = 0
VERSION_MINOR = 0
PATCHLEVEL = 0
VERSION_TWEAK = 0
EXTRAVERSION = 

Now enable DFU to the application via UART we can use the zephyr SMP Server sample as a solid starting base.

# Enable MCUMGR 
CONFIG_MCUMGR=y
CONFIG_UART_MCUMGR=y

# Enable MCUMGR management for both OS and Images
CONFIG_MCUMGR_GRP_OS=y
CONFIG_MCUMGR_GRP_IMG=y

# Configure MCUMGR transport to UART
CONFIG_MCUMGR_TRANSPORT_UART=y

# Dependencies
# Configure dependencies for CONFIG_MCUMGR  
CONFIG_NET_BUF=y
CONFIG_ZCBOR=y
CONFIG_CRC=y

# Configure dependencies for CONFIG_MCUMGR_GRP_IMG  
CONFIG_FLASH=y
CONFIG_IMG_MANAGER=y

# Configure dependencies for CONFIG_IMG_MANAGER  
CONFIG_STREAM_FLASH=y
CONFIG_FLASH_MAP=y

# Configure dependencies for CONFIG_MCUMGR_TRANSPORT_UART 
CONFIG_BASE64=y 

Disable single app mode in sysbuild.conf SB_CONFIG_MCUBOOT_MODE_SINGLE_APP=n

west build -p -b frdm_mcxn236 --sysbuild applications/dfu_sample/

west flash 

After this we can now start using a SMP client such as AUterm to upload firmare.

Upload the signed image

Now that we have MCUboot and DFU over application working on the device we no longer need to go into recovery mode to do updates we can build and upload updates without the flash step Change something in the blinky application such as the blink speed or the led. Update a version number and build with sysbuild again, however this time don’t flash the application

  1. Connect Auterm to the device over application serial
  2. List the images you should see the current one with the current version number
  3. Use the MCUmgr upload to upload an image
  4. Since we are using the FRDM MCXn236 we can’t do swaps only firmware upgrades, so when we reset we should see our new version listed with a different LED blinking

Segue console and mcumgr on one UART

If you do this update a few times, one notices that sometimee the device ends up in a bad state and updates will fail. I have found that separating the application console and MCUmgr console remedies this so there is no contention to the UART during the DFU process.

Move Application uart to flexcom0

/ {
	chosen {
		zephyr,console =  &flexcomm0_lpuart0;
		zephyr,shell-uart = &flexcomm0_lpuart0;
	};
};


&flexcomm0 {
	status = "okay"; 
};

&flexcomm0_lpuart0 {
	status = "okay";
};

With this we should have many many stable firmware updates, well that is until the lifetime of our Flash read and write is done!

Recap and Next time

Today we enabled DFU over application serial and thus we can manage to send updates to devices in the field, one caveat is that we need a gateway device. However this should be fairly common in nowadays systems, next time we can explore longer update paths such as CAN and ethernet for when serial is unviable.

Happy Debugging