[libubootenv,RFC,0/4] Add the FWU metadata storage format

Message ID 20261108133615.2483762-1-dario.binacchi@amarulasolutions.com
Headers
Series Add the FWU metadata storage format |

Message

Dario Binacchi Nov. 8, 2026, 1:36 p.m. UTC
This series lets libubootenv access the Arm FWU metadata through a
namespace, as discussed in [1]. An update agent can then switch the boot
bank with the existing API and tools, together with the U-Boot
environment.

- Patch 1 is a fix: filewrite() ignores the result of fsync().
- Patch 2 moves loading and storing behind callbacks selected by the
  format of the namespace. No functional change.
- Patch 3 adds the "fwu-mdata" format, versions 1 and 2. Only
  active_index, previous_active_index and bank<N>_state are exposed.
- Patch 4 documents it.

The SWUpdate side follows in a separate patch.

Tested on the STM32MP257F-EV1 with TF-A and U-Boot, FWU metadata
version 2 with two banks: update of either bank from SWUpdate,
acceptance at boot, rollback by TF-A after failed trial boots. Both
versions, corrupted copies and invalid values were also tested on the
host.

[1] https://groups.google.com/g/swupdate/c/kYr2Hy-f3us

Dario Binacchi (4):
  uboot_env: report fsync errors when writing to a file
  uboot_env: access the storage through callbacks
  add FWU metadata storage format
  docs: fw_env_config.md: document the FWU metadata format

 docs/fw_env_config.md |  61 ++++
 src/CMakeLists.txt    |   1 +
 src/extended_config.c |  59 ++++
 src/fw_printenv.c     |   7 +-
 src/fwu_mdata.c       | 709 ++++++++++++++++++++++++++++++++++++++++++
 src/uboot_env.c       |  60 +++-
 src/uboot_private.h   |  38 ++-
 7 files changed, 923 insertions(+), 12 deletions(-)
 create mode 100644 src/fwu_mdata.c

-- 
2.43.0
  

Comments

Stefano Babic Nov. 9, 2026, 1:56 p.m. UTC | #1
Hi Dario,


On 11/8/26 14:36, Dario Binacchi wrote:
> This series lets libubootenv access the Arm FWU metadata through a
> namespace, as discussed in [1]. An update agent can then switch the boot
> bank with the existing API and tools, together with the U-Boot
> environment.
> 
> - Patch 1 is a fix: filewrite() ignores the result of fsync().
> - Patch 2 moves loading and storing behind callbacks selected by the
>    format of the namespace. No functional change.
> - Patch 3 adds the "fwu-mdata" format, versions 1 and 2. Only
>    active_index, previous_active_index and bank<N>_state are exposed.
> - Patch 4 documents it.
> 
> The SWUpdate side follows in a separate patch.
> 
> Tested on the STM32MP257F-EV1 with TF-A and U-Boot, FWU metadata
> version 2 with two banks: update of either bank from SWUpdate,
> acceptance at boot, rollback by TF-A after failed trial boots. Both
> versions, corrupted copies and invalid values were also tested on the
> host.
> 
> [1] https://groups.google.com/g/swupdate/c/kYr2Hy-f3us
> 
> Dario Binacchi (4):
>    uboot_env: report fsync errors when writing to a file
>    uboot_env: access the storage through callbacks
>    add FWU metadata storage format
>    docs: fw_env_config.md: document the FWU metadata format
> 
>   docs/fw_env_config.md |  61 ++++
>   src/CMakeLists.txt    |   1 +
>   src/extended_config.c |  59 ++++
>   src/fw_printenv.c     |   7 +-
>   src/fwu_mdata.c       | 709 ++++++++++++++++++++++++++++++++++++++++++
>   src/uboot_env.c       |  60 +++-
>   src/uboot_private.h   |  38 ++-
>   7 files changed, 923 insertions(+), 12 deletions(-)
>   create mode 100644 src/fwu_mdata.c
> 

thanks for the series, this is the direction we discussed: the FWU
metadata is a storage format of a namespace, and the update agent and
the tools keep the same interface.

I tested it on the host with version 2 metadata in two files (2 banks, 2 
images) and fw_printenv / fw_setenv -m: the switch of the active bank, a 
corrupted first copy (the second one is used, and both are repaired on 
the next store), an invalid state, an index out of range, adding and 
removing a
variable.

All work as described, and the CRC is correct in both copies.

Patch 1/4 and 2/4 are fine for me.

Some (minor) points for patch 3/4:

1. Accepted flags of the images in version 2

 > +	accept = state == FWU_MDATA_BANK_ACCEPTED;
 > +	/* Version 1 has no bank state and uses the acceptance of the images */
 > +	if (info->version == 1 || accept) {

In version 2, setting a bank to "valid" or "invalid" changes only
bank_state, and the images of the bank keep their accepted flag. After
the usual switch on a device where both banks were accepted:

     fw_setenv -m fwu previous_active_index 0 active_index 1 bank1_state 
valid

bank 1 is "valid", but all its images are still marked as accepted.
U-Boot takes the trial state from bank_state in version 2, so the boot
works, but the metadata is inconsistent: U-Boot clears the flag of an
image when it updates it (fwu_clear_accept_image()), and requires all
flags set before it moves a bank to accepted. Please clear the flags
when a bank leaves the accepted state in version 2 too, as for version
1.

2. Byte order

The fields are read and written in host byte order, while the metadata
is little endian. Please use le32toh() / htole32() (and the 16-bit
variants for the descriptor), so that the code is correct on a
big-endian host as well.

3. Copies on MTD

The store writes copy 0 and then copy 1. On an MTD device, if both
copies are in the same erase block, erasing for the second write also
removes the first one, and a power cut leaves no valid copy. This is 
known, it is the same for environment, but maybe it is better to 
doocument it (I let to you to decide).

4. Validation when the value is set

An invalid state or an index out of range is detected only by store, so
fw_setenv reports "Error storing the env". It is correct, but rejecting
the value in libuboot_set_env() gives a clearer error and lets an
application check a value before the store. Not a blocker.

5. Tests

Please add tests in tests/ for both versions: loading, switching,
corrupted and invalid copies, and the fixed list of variables. They are
easy to do on the host with metadata created by the test itself.

For patch 4/4: please mention that "invalid" is not supported with
version 1 (it returns -ENOTSUP), and the requirement for MTD of point 3.

Best regards,
Stefano Babic



-- 
_______________________________________________________________________
Nabla Software Engineering GmbH
Hirschstr. 111A    | 86156 Augsburg | Tel: +49 821 45592596
Geschäftsführer : Stefano Babic     | HRB 40522 Augsburg
E-Mail: sbabic@nabladev.com