| This article is not applicable to M33-TD flavor |
1. Article purpose[edit | edit source]
The purpose of this article is to explain how to handle a software update using A/B mechanism with the FWU implementation in OpenSTLinux (Example given with Rauc as "update client"). It is highly recommended to read the page Secure Firmware Update where the FWU implementation in OpenSTLinux is explained.
2. Update the metadata content[edit | edit source]
The mkfwumdata tool (from U-Boot) is used to generate the metadata partition: used on build.
The metadata generation is described in the following article: Metadata generation tool.
The fwumdata tool is used to update the metadata partition: used on the target before and after switching bank.
Without argument, the tool will return the content of the metadata partition:
root@stm32mp2-ota-e3-b8-dc:~# fwumdata
FWU Metadata:
Version: 2
Active Index: 1
Previous Index: 0
CRC32: 0xc7192f2c
Metadata Size: 280 bytes
Descriptor Offset: 32
Bank States:
Bank 0: accepted (0xfc)
Bank 1: accepted (0xfc)
The metadata partition can also be read inside U-Boot with the following command:
STM32MP> fwu_mdata_read
FWU Metadata
crc32: 0xc7192f2c
version: 0x2
size: 0x118
active_index: 0x1
previous_active_index: 0x0
bank_state[0]: 0xfc
bank_state[1]: 0xfc
bank_state[2]: 0xff
bank_state[3]: 0xff
Image Info
Image Type Guid: 19D5DF83-11B0-457B-BE2C-7559C13142A5
Location Guid: 8A7A84A0-8387-40F6-AB41-A8B9A5A60D23
Image Guid: 4FD84C93-54EF-463F-A7EF-AE25FF887087
Image Acceptance: yes
Image Guid: 09C54952-D5BF-45AF-ACEE-335303766FB3
Image Acceptance: yes
Image Type Guid: 8DA7C9D3-D368-487B-B28B-71E1B0C68CF0
Location Guid: 8A7A84A0-8387-40F6-AB41-A8B9A5A60D23
Image Guid: C9B01B31-B604-4C2A-95B4-E23AB45FD0C5
Image Acceptance: yes
Image Guid: 67A73CEA-322C-42C0-B477-6EF0039FEFED
Image Acceptance: yes
Image Type Guid: B28961F8-C72C-4088-BBDB-C785861473FA
Location Guid: 8A7A84A0-8387-40F6-AB41-A8B9A5A60D23
Image Guid: E91C4E10-16E6-4C0E-BD0E-77BECF4A3582
Image Acceptance: yes
Image Guid: 360DB83F-C038-4E10-AE79-1DCE8846D2F0
Image Acceptance: yes
3. mkfwumdata and fwumdata versus FWU states[edit | edit source]
mkfwumdata and fwumdata consider three states where two of them correspond to Firmware update state machine:
- the (mk)fwumdata state Accepted is the "Firmware update" state machine regular state
- the (mk)fwumdata state Valid is the Firmware update state machine trial state
- the (mk)fwumdata state Invalid has no corresponding "Firmware update" state machine state.
- the Firmware update state machine staging state has no corresponding (mk)fwumdata state.
4. Steps to run a secure firmware update[edit | edit source]
- Prerequisite: system is running on bank A (fip-a, bootfs-a, rootfs-a...)
- The "update manager", which is in touch with the software provisioning server, is notified about a new update that is written into bank B (fip-b, bootfs-b, rootfs-b...)
- When the new image is downloaded into the bank B, the "update manager" will updates the metadata partition (through fwumdata linux tool) requesting TF-A to try to boot on the new bank B: so the "update manager" changes the metadata partition by:
- updating active_index to bank B (managed through -a option). Bank A becomes then previous_active_index (managed through -p option).
- switching bank B as Valid (managed through -s option). Bank A is kept as Accepted, to prevent failure of bank B.
fwumdata -a 1 -p 0 -s 1 valid
Note that /dev/disk/by-partlabel/metadata2 is also updated, which is the backup of metadata1, and is used by TF-A in case of CRC error of metadata1 partition.
- On reboot, TF-A boots bank B, and there are now two possibilities:
- bank B crashs: as the watchdog is enabled, the platform reboots on crash. As bank B is in Valid mode, the "bootcount" is decremented on each reboot. After 3 attempts (default value), TF-A considers the bank B is not able to boot, so it switches back to bank A. As "active_index" is bank B, but bank A is active (Bank A is kept in Accepted), the update manager can consider bank B as Invalid, and notify the "software provisioning server" if needed.
- bank B successfully boots: the "update manager" from bank B can update the metadata partition to switch the bank B state to Accepted: on the next reboot, TF-A considers this bank Accepted, and disables the "bootcount":
fwumdata -s 1 accepted
The systemd service /system/multi-user.target.wants/bootcount.service is used to accept the bank and switch it from Valid to Accepted.
It calls the script /usr/sbin/bootcount.sh which can also notify the update manager (rauc as example) like below:
if [ "${bank_state}" = "valid" ]; then
fwumdata -s "${active_index}" accepted
+ /usr/bin/rauc status mark-good
fi
The filesystem is mount through the systemd-mount service (/usr/sbin/mount-partitions.sh) following these rules (for sdcard or eMMC):
- Get the rootfs partuuid from the cmdline and get the bootfs, vendorfs, userfs partuuid
- For each partition to be mounted, use the uuid partition if present, otherwise use the partition name with '-a' or '_a' suffix (must be '-b' or '_b' for b partition).
- The following script example /usr/sbin/mount-partitions-via-metadata.sh gets the uuid from metadata instead of a static list.
- On ST boards, as there are many filesystems that can be used for testing, the file /etc/fwumdata_current.config contains the path of the metadata partition.
fwumdata -c /etc/fwumdata_current.config -l
5. Yocto layer[edit | edit source]
A Yocto layer is available. This layer is not a reference design for a product but it has the unique goal to show how the secure firmware update is working in OpenSTLinux:
- It demonstrates A/B mechanism already integrated by default in OpenSTLinux (in TF-A), using Rauc[1] as an "update manager".
- It includes few scripts that make the "glue" between the "update manager" and STM32MPU ecosystem.
To have deeper information about RAUC or to have support to design a product with, you can contact Pengutronix, member of ST partner Program and maintainer of RAUC.