Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions docs/snagrecover.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,24 +248,36 @@ 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.

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.

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.
Expand Down
4 changes: 2 additions & 2 deletions src/snagflash/dfu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand Down
11 changes: 7 additions & 4 deletions src/snagrecover/firmware/firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ 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":

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}")

Expand Down Expand Up @@ -83,7 +86,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")
Expand All @@ -106,7 +109,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")
Expand Down Expand Up @@ -136,7 +139,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")
Expand Down
2 changes: 1 addition & 1 deletion src/snagrecover/firmware/zynqmp_fw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions src/snagrecover/protocols/dfu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
45 changes: 40 additions & 5 deletions src/snagrecover/recoveries/stm32mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Loading