Skip to content

SPI flash

Experimental

Tested against a fake flashrom only so far; the API may change in minor releases.

See the SPI flash guide for an introduction.

spiflash

Wrappers around the flashrom CLI for reading and (carefully) writing SPI flash.

Example

from hwntools import Flashrom

fr = Flashrom("ch341a_spi")
print(fr.detect())                  # ['W25Q128.V']
image = fr.read("dump.bin")         # reads twice and compares

FlashromError

Bases: HwnError

flashrom exited with an error.

FlashChip dataclass

FlashChip(vendor: str, name: str, size: int, bus: str)

A chip reported by flashrom. size is in bytes.

Flashrom

Flashrom(programmer: str = 'ch341a_spi', chip: str | None = None, flashrom: str = 'flashrom', extra_args: Sequence[str] = (), timeout: float = 1800.0)

Run flashrom with a fixed programmer (ch341a_spi, linux_spi:dev=..., ...).

Writing and erasing require force=True; reads are verified by reading twice.

detect

detect() -> list[FlashChip]

Probe the programmer and return the detected chip(s).

When several chip definitions match (common for clones), set chip= to one of the returned names.

read

read(out: str | PathLike[str] | None = None, verify: bool = True, tries: int = 3) -> bytes

Read the whole chip. With verify it is read twice and both reads must match.

On a mismatch (loose clip, marginal wiring) up to tries pairs are attempted.

write

write(image: str | PathLike[str], force: bool = False, verify: bool = True) -> None

Program image to the chip. Destructive: requires force=True.

Make (and verify) a backup with read first.

erase

erase(force: bool = False) -> None

Erase the whole chip. Destructive: requires force=True.

verify

verify(image: str | PathLike[str]) -> bool

Compare chip contents with image (flashrom -v).

parse_flashrom_chips

parse_flashrom_chips(output: str) -> list[FlashChip]

Parse Found <vendor> flash chip "<name>" (<n> kB, SPI) lines.