Skip to content

boot: imgtool: Extend align to 128B - #2797

Open
URNHere wants to merge 3 commits into
mcu-tools:mainfrom
URNHere:feat/128BAlign
Open

boot: imgtool: Extend align to 128B#2797
URNHere wants to merge 3 commits into
mcu-tools:mainfrom
URNHere:feat/128BAlign

Conversation

@URNHere

@URNHere URNHere commented Jul 21, 2026

Copy link
Copy Markdown

I wanted to use the latest mcuboot upstream version together building it with Zephyr.
We are using a Renesas RA6M3 MCU, which has a controller which can write the flash only 128Bytes at a time.
I extended the aligment and max-alignment in the imgtool signer to allow the app to be signed by west, and the image gets correctly signed.

For what concerns bootloader support, I managed to make the boot work on a custom board we have by setting CONFIG_BOOT_SERIAL_UNALIGNED_BUFFER_SIZE=128.
CONFIG_BOOT_MAX_IMG_SECTORS_AUTO=n
CONFIG_BOOT_MAX_IMG_SECTORS=14
As the renesas drivers do not support the soc-nv-flash compatible yet and the mapped-partitions but AFAIK there is some work in progress here:
zephyrproject-rtos/zephyr#110365
zephyrproject-rtos/zephyr#109125

@d3zd3z d3zd3z left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to test the larger alignment in the simulator.

Also, the docs/design.md needs to be updated to include the new alignments possible.

Comment thread scripts/imgtool/main.py Outdated
@URNHere

URNHere commented Jul 23, 2026

Copy link
Copy Markdown
Author

Thanks for the review.
I followed what has been done in #2564.
Didn't edit the sim.yaml yet (maybe you have some advice before I do).
When I run cargo test --features "max-align-128" or --features "max-align-64", the only test that fails is oversized_secondary_slot and only for Stm32f4SpiFlash (if I comment it out from ALL_DEVICES all tests pass).
Do you have any hints?
First time contributing, and not so proficient with rust, let me know if you have some advice.

@d3zd3z d3zd3z left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure the simulator support is also then tested. Also, the docs still need to be updated to include the new alignments.

Comment thread sim/Cargo.toml
@jeremydick

Copy link
Copy Markdown

#2801 will handle setting CONFIG_BOOT_SERIAL_UNALIGNED_BUFFER_SIZE to 128 for all renesas RA family of MCUs

@nordicjm

Copy link
Copy Markdown
Collaborator

Would suggest reading comments on #2440 if your write size is 128 bytes then really it doesn't make much sense to use a swap mode since your overhead is so large it destroys any real swap use case

@URNHere

URNHere commented Jul 27, 2026

Copy link
Copy Markdown
Author

Thanks @nordicjm for the review. Still new to the project, and I missed that closed PR.
So, no swap modes due to the overhead it brings regarding flash size (I'll remove that assert line change).
However, I am missing one point to fully understand: #2440 was closed, but if I want to sign an image with Imgtool using --overwrite-only for the Renesas RA family I still need the modifications to allow 128B as max-align (since it is used by Imgtool even in that case), am I correct or am I missing something?

@jeremydick

jeremydick commented Jul 27, 2026

Copy link
Copy Markdown

I'm not understanding the reason for the closure of #2440 either. I believe swap is still usable. The write block size is 128 bytes, but the erase block or sector size is larger (a combination of 8-8KiB and 30-32KiB blocks on the RA6M3, other RA6 family have similar). If you treat it all as 32KiB sectors, only 6KiB is required for the trailer, rather than 1.5MiB. Treating it all as 8KiB sectors requires a 24KiB trailer. Not great, but not unusable.

@URNHere

URNHere commented Jul 27, 2026

Copy link
Copy Markdown
Author

Yes, also looking back at the CMakeLists.txt we have:

 if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO)
    if(DEFINED slot0_size AND DEFINED erase_size_slot0)
      math(EXPR slot_min_sectors "${slot0_size} / ${erase_size_slot0}")

      if(${slot_min_sectors} GREATER ${auto_min_sectors})
        set(auto_min_sectors ${slot_min_sectors})

        if(${image} EQUAL 0)
          set(image_0_min_sectors ${slot_min_sectors})
        endif()
      endif()

In my case we have:
slot0_size = 448KB, erase_size_slot0 = 32KB (.dts of the RA6M3 here ), as I reserved the small 8K sectors to MCUBoot, and all the big 32K to the app) , which yields to slot_min_sectors= 14.

Then we have:
 if(CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE)
      if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO AND DEFINED image_0_min_sectors AND "${image_0_min_sectors}" GREATER "0")
        math(EXPR boot_status_data_size "${image_0_min_sectors} * (3 * ${write_size})")

And:
math(EXPR boot_swap_data_size "${max_align_size} * 4")

boot_status_data_size = 14 * 3 * 128 = 5376 B
boot_swap_data_size = 128*4 = 512 B

In the end:
math(EXPR trailer_size "${key_size} + ${boot_magic_size} + ${boot_swap_data_size} + ${boot_status_data_size}")
trailer_size = 256 (considering RSA) + 128 (magic is aligned to 128, which is the write size) + 5376 + 512 = 6272 B, which needs a whole 32KB sector, because it has to be aligned to the erase size. Not so great, but definitely far from the 1.5MB depicted in #2440.

@nordicjm You're far more expert than me, can you tell me please if we miss something?

P.S. I found out how it manages to sign in overwrite only mode, it sets align to 1 here

@nordicjm

Copy link
Copy Markdown
Collaborator

Thanks @nordicjm for the review. Still new to the project, and I missed that closed PR. So, no swap modes due to the overhead it brings regarding flash size (I'll remove that assert line change). However, I am missing one point to fully understand: #2440 was closed, but if I want to sign an image with Imgtool using --overwrite-only for the Renesas RA family I still need the modifications to allow 128B as max-align (since it is used by Imgtool even in that case), am I correct or am I missing something?

With overwrite only, the max align is ignored since you have nothing extra to write, so you don't need any changes

@nordicjm

Copy link
Copy Markdown
Collaborator

I'm not understanding the reason for the closure of #2440 either. I believe swap is still usable. The write block size is 128 bytes, but the erase block or sector size is larger (a combination of 8-8KiB and 30-32KiB blocks on the RA6M3, other RA6 family have similar). If you treat it all as 32KiB sectors, only 6KiB is required for the trailer, rather than 1.5MiB. Treating it all as 8KiB sectors requires a 24KiB trailer. Not great, but not unusable.

The comment gives the figures it uses which was based on a random image size of 512KiB and assuming that the erase and write block sizes were the same (the user did not specify an erase size)

@URNHere

URNHere commented Jul 28, 2026

Copy link
Copy Markdown
Author

Ok thanks for all the clarifications.
Updated the sim.yaml to include some testing for the new aligments and the docs, in case you want to go ahead and give it a test.
I had to disable the oversized_secondary_slot because it was failing only on one device (stm32f4ExternalSpi).
@nordicjm and @d3zd3z if you consider it something useful to add we can go ahead, otherwise we can close the PR, up to you.

@URNHere

URNHere commented Jul 29, 2026

Copy link
Copy Markdown
Author

Should be signed-off now, sorry about that

@d3zd3z d3zd3z left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is in reasonably good shape. I think, aside from this, we probably have an issue with the CI test for each PR getting too long, but that is something to address separately. There is a formatting issue in the docs, which should be fixable by pasting in the actual output of --help instead of editing that output.

Comment thread docs/imgtool.md Outdated
@d3zd3z

d3zd3z commented Jul 29, 2026

Copy link
Copy Markdown
Member

I have confirmed that #2808 fixes the issue with the Stm32f4SpiFlash, and the combined result passes CI without needing to disable at specific part.

@URNHere

URNHere commented Jul 29, 2026

Copy link
Copy Markdown
Author

Great, I'll remove the suppression, thanks for your help!
I'll let the CI finish a full round of tests to assess if there is anything else to do before pushing another time.

@d3zd3z

d3zd3z commented Jul 29, 2026

Copy link
Copy Markdown
Member

We'll need to get 2808 merged before I'd expect CI to pass. You can also pull 2808 into your tree, rebase on top of that, and set the merge base of this PR to my branch, but it might be easier to just get the 2 reviews on 2808 and get it merged, and you can then just rebase on main.

@URNHere

URNHere commented Jul 29, 2026

Copy link
Copy Markdown
Author

Ok, I'll keep an eye out for #2808 being merged, I'll rebase and remove that suppression before pushing again.
Thanks for your help! Highly appreciated!

URNHere added 3 commits July 30, 2026 09:39
Incresaed align to 128B to support Renesas RA family MCUs,
which have a flash controller that expects 128B writes.

Signed-off-by: Paolo Bazzanella <pbazzanella@qubicaamf.com>
Added simulator support for the flash used in the RA6M3.
Added two new features: max-align-64 and max-align-128.

Signed-off-by: Paolo Bazzanella <pbazzanella@qubicaamf.com>
Signed-off-by: Paolo Bazzanella <pbazzanella@qubicaamf.com>
@URNHere

URNHere commented Jul 30, 2026

Copy link
Copy Markdown
Author

Rebased on top of main which now features the commits contained in #2808 and edited the second commit to remove the suppression tests/core.rs not in the modified files list anymore.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants