Skip to content

U-Boot

See the U-Boot guide for an introduction.

uboot

Drive a U-Boot console: stop autoboot, run commands, read env, dump memory with md.b.

Example

from hwntools import *

io = serial("/dev/ttyUSB0", 115200)
ub = UBoot(io)
ub.interrupt()                       # power-cycle the target now
env = ub.printenv()
ub.run("sf probe; sf read 0x82000000 0x0 0x1000000")
data = ub.dump(0x82000000, 0x1000000, out="flash.bin")

DEFAULT_PROMPTS module-attribute

DEFAULT_PROMPTS: tuple[bytes, ...] = (b'=> $', b'U-Boot> $', b'Marvell>> $', b'ath> $', b'ar7240> $', b'(?:IPQ|MT|RT|QCA)[\\w-]*\\s?[#>] $', b'(?:uboot|u-boot|boot)\\s?[#>] $', b'hisilicon\\s?# $', b'\\w+>\\s?$')

UBoot

UBoot(tube: Tube, prompt: bytes | str | None = None, prompts: Sequence[bytes] = DEFAULT_PROMPTS, timeout: float | None = None)

High-level helper around a tube connected to a U-Boot console.

Parameters:

Name Type Description Default
tube Tube

any Tube (serial, remote, process, sim).

required
prompt bytes | str | None

exact prompt bytes (e.g. b"=> "). Detected automatically when omitted.

None
prompts Sequence[bytes]

regexes used for detection, see DEFAULT_PROMPTS.

DEFAULT_PROMPTS
timeout float | None

default command timeout (falls back to context.timeout).

None

interrupt

interrupt(key: bytes | str = b'\x03', timeout: float = 30.0, interval: float = 0.05, wait_banner: bool = False) -> bytes

Spam key until a U-Boot prompt appears, then return the detected prompt.

Power-cycle (or reset) the target right after calling this. Common keys are Ctrl-C (default), space, Enter, or a vendor string such as "tpl". With wait_banner the spam only starts after an autoboot banner (Hit any key) is seen.

sync

sync(hint: bytes | None = None, timeout: float = 3.0) -> bytes

Resynchronise with the console and learn the exact prompt.

Sends Ctrl-C (which U-Boot answers with a fresh prompt and, unlike an empty line, never repeats the previous command) and reads until the output ends in a prompt.

run

run(cmd: str, timeout: float | None = None) -> str

Run cmd and return its output (without echo and prompt) as text.

run_raw

run_raw(cmd: str, timeout: float | None = None) -> bytes

Like run but returns bytes.

version

version() -> str

Return the output of U-Boot's version command.

printenv

printenv(name: str | None = None) -> dict[str, str]

Return the environment (or a single variable) as a dict.

getenv

getenv(name: str) -> str | None

Return the value of one environment variable, or None if it is not set.

setenv

setenv(name: str, value: str | None) -> None

Set (or with None delete) a variable in RAM. Use saveenv to persist.

saveenv

saveenv(force: bool = False) -> str

Persist the environment to flash. Destructive: requires force=True.

inject_bootargs

inject_bootargs(extra: str = 'init=/bin/sh', boot: bool = False, replace_init: bool = True) -> str

Append extra to bootargs in RAM (not saved) and optionally boot.

Existing init= arguments are replaced when extra contains one and replace_init is true. Returns the new bootargs.

audit_env

audit_env(env: dict[str, str] | None = None, min_severity: str | None = None) -> BootlintReport

Audit the environment with bootlint (reads it with printenv if needed).

The env is written to a temporary key=value file and passed as --type env.

boot

boot(cmd: str = 'boot') -> None

Start booting (does not wait for a prompt).

md

md(addr: int, length: int, timeout: float | None = None) -> bytes

Read length bytes at addr with a single md.b command.

dump

dump(addr: int, size: int, out: str | PathLike[str] | None = None, chunk: int = 4096, resume: bool = True, retries: int = 3) -> bytes

Dump size bytes from addr using md.b in chunk-sized requests.

With out, data is appended to the file as it arrives; if the file already holds a partial dump and resume is true, reading continues where it stopped. Each chunk is retried up to retries times on parse errors or timeouts. Returns the full data.

parsing

Pure parsers for device console output.

Everything here takes plain bytes/str and returns plain Python types, with no I/O, so an accelerated implementation can be swapped in later without touching callers.

parse_md_b

parse_md_b(text: bytes | str, start: int, length: int) -> bytes

Parse U-Boot md.b output into bytes.

Validates that line addresses are contiguous from start and that exactly length bytes are present. The ASCII column is ignored even when it contains hex-looking text, because only as many hex tokens as expected for each line are consumed.

Raises ParseError on gaps, garbage or short output.

parse_env

parse_env(text: bytes | str) -> dict[str, str]

Parse printenv output into a dict (ignores the trailing size line and errors).