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 |
required |
prompt
|
bytes | str | None
|
exact prompt bytes (e.g. |
None
|
prompts
|
Sequence[bytes]
|
regexes used for detection, see |
DEFAULT_PROMPTS
|
timeout
|
float | None
|
default command timeout (falls back to |
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
¶
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 and return its output (without echo and prompt) as text.
printenv
¶
Return the environment (or a single variable) as a dict.
getenv
¶
Return the value of one environment variable, or None if it is not set.
setenv
¶
Set (or with None delete) a variable in RAM. Use saveenv to persist.
saveenv
¶
Persist the environment to flash. Destructive: requires force=True.
inject_bootargs
¶
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.
md
¶
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 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 printenv output into a dict (ignores the trailing size line and errors).