From 619978d80d0f7231ae5d786819c87b4474755509 Mon Sep 17 00:00:00 2001 From: Martin Fuzzey Date: Thu, 1 Oct 2026 15:36:32 +0200 Subject: [PATCH 1/2] DFU: remove STM32MP first block special case The DFU constuctor has an optional argument "stm32", defaulting to True, which causes the block number in the USB requests to start from 2 instead of 0. This was done because reverse engineering of the STCubeProgrammer USB packet dumps showed it used 2 based indices but this is not actually required (tested), nor is documented anywhere. This is problematic for 2 reasons: * It requires all the non STM32 users of DFU to use stm32=False * For the upcoming support of SPL rather than TF-A on stm32 we must start from block 0 since the u-boot SPL DFU protocol errors out with an unexpected block number if we start with 2. So drop the stm32 constructor argument completely and alwys use zero based packet indices. --- src/snagflash/dfu.py | 4 ++-- src/snagrecover/firmware/firmware.py | 7 ++++--- src/snagrecover/firmware/zynqmp_fw.py | 2 +- src/snagrecover/protocols/dfu.py | 8 ++------ 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/snagflash/dfu.py b/src/snagflash/dfu.py index 5892196..e495cef 100644 --- a/src/snagflash/dfu.py +++ b/src/snagflash/dfu.py @@ -36,7 +36,7 @@ def dfu_detach(dev: Device, altsetting: int = 0): logger.info("Sending DFU detach command...") - dfu_cmd = dfu.DFU(dev, stm32=False) + dfu_cmd = dfu.DFU(dev) dfu_cmd.get_status() dfu_cmd.detach(altsetting) logger.info("Done") @@ -48,7 +48,7 @@ def dfu_download(dev: Device, altsetting: int, path: str): size = len(blob) logger.info(f"Downloading {path} to altsetting {altsetting}...") logger.debug(f"DFU config altsetting:{altsetting} size:0x{size:x} path:{path}") - dfu_cmd = dfu.DFU(dev, stm32=False) + dfu_cmd = dfu.DFU(dev) dfu_cmd.get_status() dfu_cmd.download_and_run(blob, altsetting, 0, size, show_progress=True) dfu_cmd.get_status() diff --git a/src/snagrecover/firmware/firmware.py b/src/snagrecover/firmware/firmware.py index 3dee89f..0928cfd 100644 --- a/src/snagrecover/firmware/firmware.py +++ b/src/snagrecover/firmware/firmware.py @@ -31,6 +31,7 @@ def stm32mp_run(port, fw_name: str, fw_blob: bytes): There isn't a lot of complicated logic to handle stm32mp firmware so we can leave it in the common module for now """ + if fw_name == "tf-a": partprefixes = ["@FSBL"] elif fw_name == "fip": @@ -83,7 +84,7 @@ def am6x_run(dev, fw_name: str, fw_blob: bytes): partid = dfu.search_partid(dev, partname) if partid is None: raise Exception(f"No DFU altsetting found with iInterface='{partname}'") - dfu_cmd = dfu.DFU(dev, stm32=False) + dfu_cmd = dfu.DFU(dev) logger.info("Downloading file...") dfu_cmd.download_and_run(fw_blob, partid, offset=0, size=len(fw_blob)) logger.info("Done") @@ -106,7 +107,7 @@ def am62lx_run(dev, fw_name: str, fw_blob: bytes): partid = dfu.search_partid(dev, partname) if partid is None: raise Exception(f"No DFU altsetting found with iInterface='{partname}'") - dfu_cmd = dfu.DFU(dev, stm32=False) + dfu_cmd = dfu.DFU(dev) logger.info("Downloading file...") dfu_cmd.download_and_run(fw_blob, partid, offset=0, size=len(fw_blob)) logger.info("Done") @@ -136,7 +137,7 @@ def rzn1_run(dev, fw_name: str, fw_blob: bytes): There isn't a lot of complicated logic to handle RZ/N1 firmware so we can leave it in the common module for now """ - dfu_cmd = dfu.DFU(dev, stm32=False) + dfu_cmd = dfu.DFU(dev) logger.info("Downloading file...") dfu_cmd.download_and_run(fw_blob, 0, offset=0, size=len(fw_blob)) logger.info("Done") diff --git a/src/snagrecover/firmware/zynqmp_fw.py b/src/snagrecover/firmware/zynqmp_fw.py index 72b7f60..33441cb 100644 --- a/src/snagrecover/firmware/zynqmp_fw.py +++ b/src/snagrecover/firmware/zynqmp_fw.py @@ -146,7 +146,7 @@ def drop_images(boot_bin: bytearray, keep_images): def zynqmp_run(dev, fw_name, fw_blob, subfw_name): - dfu_cmd = dfu.DFU(dev, stm32=False) + dfu_cmd = dfu.DFU(dev) if fw_name == "fsbl": partid = 0 diff --git a/src/snagrecover/protocols/dfu.py b/src/snagrecover/protocols/dfu.py index ad98120..60a3d40 100644 --- a/src/snagrecover/protocols/dfu.py +++ b/src/snagrecover/protocols/dfu.py @@ -89,9 +89,8 @@ class DFU: 0x0F: "errSTALLEDPKT", } - def __init__(self, dev: usb.core.Device, stm32: bool = True): + def __init__(self, dev: usb.core.Device): self.dev = dev - self.stm32 = stm32 # set when dfu is used to recover stm32mp boards # try to find wTransferSize bMaxPacketSize0 = dev.bMaxPacketSize0 self.transfer_size = bMaxPacketSize0 @@ -138,10 +137,7 @@ def download_and_run( if state != DFU.state_codes["dfuIDLE"]: raise ValueError(f"Incompatible state {state} detected") - if self.stm32: - block_index = 2 # wValue 0 and 1 seem to be reserved - else: - block_index = 0 + block_index = 0 # for other commands (erase, set exec address, etc.) bytes_written = 0 for chunk in utils.dnload_iter( From fb1e9860cc6f812080b857a9ef3d3152a7c832a7 Mon Sep 17 00:00:00 2001 From: Martin Fuzzey Date: Thu, 1 Oct 2026 15:42:07 +0200 Subject: [PATCH 2/2] snagrecover: support SPL mode on STM32MP1 Normally STM32MP1 uses TF-A as the first stage bootloader but it is also possible to use SPL instead. Enable this by using a configuration file like: spl: path: u-boot-2024/out/flb_std_stm32mp15/u-boot-spl.stm32 u-boot: path: u-boot-2024/out/flb_std_stm32mp15/u-boot.img It is also possible to specify both TF-A and SPL in the configuration file, in which case an additional "first_stage" property is required: first_stage: spl tf-a: path: ./tf-a-stm32mp157f-dk2.stm32 spl: path: u-boot-2024/out/flb_std_stm32mp15/u-boot-spl.stm32 fip: path: ./fip.bin u-boot: path: u-boot-2024/out/flb_std_stm32mp15/u-boot.img Alternatively the first_stage property may be omitted from the config file and supplied on the command line by adding -F "{'first_stage': 'spl'}" --- docs/snagrecover.md | 18 +++++++++-- src/snagrecover/firmware/firmware.py | 4 ++- src/snagrecover/recoveries/stm32mp.py | 45 ++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/docs/snagrecover.md b/docs/snagrecover.md index 363cf6a..9f01d5b 100644 --- a/docs/snagrecover.md +++ b/docs/snagrecover.md @@ -248,9 +248,14 @@ when configuring U-Boot. [example](https://github.com/bootlin/snagboot/blob/main/src/snagrecover/templates/stm32mp1-stm32mp157f-dk2.yaml) -TF-A is used as the first stage and U-Boot as the second stage. +Normally TF-A is used as the first stage and U-Boot as the second stage, as an alternative it is possible +to use u-boot SPL, wrapped in a ST header as the first stage. -**fip:** Contains at least U-Boot with an stm32 image header. Usually the +If only one of "tf-a" or "spl" is specified in the config file that is used, if both are present the one +to use must be specified by an additional "first_stage" property in the config file or given as an override +on the command line (eg -F "{'first_stage': 'spl'}") + +**fip:** (TF-A mode only) Contains at least U-Boot with an stm32 image header. Usually the raw U-Boot image needs to be generated first, then packaged by a trusted-arm-firmware build. If the autoboot feature is enabled, then U-Boot will enter DFU mode after recovery. @@ -258,7 +263,7 @@ enter DFU mode after recovery. configuration: * path -**tf-a:** Arm-trusted firmware BL2, with an stm32 image header. In typical +**tf-a:** (TF-A mode only) Arm-trusted firmware BL2, with an stm32 image header. In typical build strategies, you have to pass your U-Boot binary to the tf-a build process. For the secure firmware, use SP_MIN if available. OPTEE can also work. @@ -266,6 +271,13 @@ OPTEE can also work. configuration: * path +**spl:** (SPL mode ony) U-Boot SPL, with an stm32 image header. This is usually "u-boot-spl.stm32" from the u-boot build. + +configuration: + * path + +** u-boot:** (SPL mode only) Main U-Boot image, **without** a stm32 image header. This is usually "u-boot.img" from the u-boot build. + #### For STM32MP2 only: **fip-ddr:** Contains the DDR initialization firmware. diff --git a/src/snagrecover/firmware/firmware.py b/src/snagrecover/firmware/firmware.py index 0928cfd..9ae0efe 100644 --- a/src/snagrecover/firmware/firmware.py +++ b/src/snagrecover/firmware/firmware.py @@ -32,12 +32,14 @@ def stm32mp_run(port, fw_name: str, fw_blob: bytes): so we can leave it in the common module for now """ - if fw_name == "tf-a": + if fw_name in ("tf-a", "spl"): partprefixes = ["@FSBL"] elif fw_name == "fip": partprefixes = ["@Partition3", "@SSBL", "@FIP"] elif fw_name == "fip-ddr": partprefixes = ["@DDR FIP"] + elif fw_name == "u-boot": + partprefixes = ["u-boot"] else: cli_error(f"unsupported firmware {fw_name}") diff --git a/src/snagrecover/recoveries/stm32mp.py b/src/snagrecover/recoveries/stm32mp.py index a5db817..f3587e3 100644 --- a/src/snagrecover/recoveries/stm32mp.py +++ b/src/snagrecover/recoveries/stm32mp.py @@ -34,13 +34,44 @@ def main(): soc_model = recovery_config["soc_model"] + + # Choose first stage, must be TF-A or SPL, implicit if only one present, + # explicit by "first_stage" config property otherwise + fw_config = recovery_config["firmware"] + first_stages = ("tf-a", "spl") + explicit_first_stage = fw_config.get("first_stage") + if explicit_first_stage is None: + first_stage = None + for fs in first_stages: + if fs in fw_config: + if first_stage is None: + first_stage = fs + else: + raise Exception( + "Multiple first stage binaries specified - need explicit first_stage config" + ) + logger.info("Selected first stage '%s' implicitly", first_stage) + else: + if explicit_first_stage not in first_stages: + raise Exception( + f"Explicit first stage '{explicit_first_stage}' is not supported, must be one of '{first_stages}''" + ) + + if explicit_first_stage not in fw_config: + raise Exception( + f"No binary configured for explicit first stage '{explicit_first_stage}'" + ) + + first_stage = explicit_first_stage + logger.info("Selected first stage '%s' explicitly", first_stage) + # USB ENUMERATION usb_addr = recovery_config["usb_path"] dev = get_usb(usb_addr) - # DOWNLOAD TF-A + # DOWNLOAD First stage dfu_cmd = dfu.DFU(dev) - run_firmware(dev, "tf-a") + run_firmware(dev, first_stage) if soc_model in ["stm32mp13", "stm32mp25"]: logger.info("Sending detach command to SPL...") phase_id = dfu_cmd.stm32_get_phase() @@ -49,7 +80,7 @@ def main(): # DOWNLOAD FLASH LAYOUT TO BEGINNING OF RAM # Configuration of the flashing phase is out of scope for # Snagrecover so we only download a dummy flash layout - if soc_model == "stm32mp15": + if soc_model == "stm32mp15" and first_stage == "tf-a": phase_id = dfu_cmd.stm32_get_phase() part0 = dfu.search_partid(dev, "@Partition0", match_prefix=True) if part0 is None: @@ -83,9 +114,13 @@ def main(): dev = get_usb(usb_addr) dfu_cmd = dfu.DFU(dev) - run_firmware(dev, "fip") + second_stage = {"tf-a": "fip", "spl": "u-boot"}[first_stage] + run_firmware(dev, second_stage) # DETACH DFU DEVICE logger.info("Sending detach command to U-Boot...") - phase_id = dfu_cmd.stm32_get_phase() + if first_stage == "tfa-a": + phase_id = dfu_cmd.stm32_get_phase() + else: + phase_id = 0 dfu_cmd.detach(phase_id)