Skip to content

U-Boot

UBoot wraps any tube connected to a U-Boot console: it stops autoboot, learns the prompt, runs commands, reads and audits the environment and dumps memory with md.b.

from hwntools import *

ub = UBoot(serial("/dev/ttyUSB0", 115200))
ub.interrupt()                       # power-cycle the target right after this
print(ub.version())

You can try every snippet on this page against UBoot(SimTube(FakeUBoot(...))) from the simulator.

Stop autoboot

interrupt() sends a key every interval seconds (default 0.05) until a U-Boot prompt appears, then confirms the prompt and returns it. Call it, then power-cycle or reset the target.

ub.interrupt()                               # Ctrl-C, for up to 30 s
ub.interrupt(key=" ", timeout=60)            # space, Enter, ... whatever the board wants
ub.interrupt(key="tpl")                      # vendor stop strings work too
ub.interrupt(wait_banner=True)               # only start sending after "Hit any key ..."

It raises HwnTimeoutError (with the last output) if no prompt shows up in time.

Prompts

When you do not pass prompt=, the prompt is detected from the end of the console output with the regexes in DEFAULT_PROMPTS: =>, U-Boot>, Marvell>>, ath>, ar7240>, vendor prompts starting with IPQ, MT, RT or QCA and ending in # or >, uboot# / u-boot> / boot# style prompts, hisilicon # and a generic word> fallback. If your board uses something else, pass it explicitly or add a regex:

ub = UBoot(io, prompt=b"MyBoard# ")
ub = UBoot(io, prompts=[rb"MyBoard# $", *uboot.DEFAULT_PROMPTS])

sync() resynchronises with the console at any time: it sends Ctrl-C (which U-Boot answers with a fresh prompt and, unlike Enter, never repeats the previous command) and re-learns the prompt.

Run commands

out = ub.run("sf probe 0")                                # output as text, no echo or prompt
ub.run("sf read 0x82000000 0x0 0x1000000", timeout=120)   # slow commands need a longer timeout
raw = ub.run_raw("md.b 0x82000000 0x40")                  # same, as bytes

Commands must be a single line. The default timeout is the timeout= you gave UBoot, else context.timeout.

The environment

env = ub.printenv()                  # dict of every variable
bootcmd = ub.getenv("bootcmd")       # one variable, or None
ub.setenv("bootdelay", "5")          # in RAM only
ub.setenv("bootdelay", None)         # delete it

setenv quotes the value, so a ; inside a bootcmd-like value is stored rather than executed. Values must be single-line, and names may only use letters, digits, _, . and -.

Nothing is written to flash until you call saveenv(), which is destructive and therefore needs an explicit opt-in:

ub.saveenv()              # raises SafetyError
ub.saveenv(force=True)    # writes the environment to persistent storage

A root shell on the next boot

inject_bootargs() appends kernel arguments to bootargs in RAM (nothing is saved) and, with boot=True, boots right away. An existing init= is replaced when you pass a new one.

ub.inject_bootargs("init=/bin/sh", boot=True)

Then continue on the same tube with LinuxShell.

Audit the environment

audit_env() runs the external bootlint tool on the environment (read with printenv() unless you pass a dict) and returns a report with normalised findings, for example an autoboot that can be stopped without a stop key, a kernel booted without signature verification, or init replaced with a shell:

report = ub.audit_env()
for f in report.findings:
    print(f.severity, f.rule_id, f.message)

bootlint must be installed; see Installation.

Dump memory and flash

U-Boot cannot send binary data over the console, but md.b prints memory as hex. md() reads one region with a single md.b; dump() reads a large region in chunks, with progress, retries and resume:

ub.run("sf probe 0")
ub.run("sf read 0x82000000 0x0 0x1000000", timeout=120)      # SPI flash -> RAM
data = ub.dump(0x82000000, 0x1000000, out="spi.bin", chunk=0x1000)
  • Streaming to disk. With out=, each chunk is appended to the file as soon as it arrives.
  • Resume. If the file already holds part of the dump, dump() continues where it stopped (resume=True, the default). Stop the script with Ctrl-C and run it again; it refuses to resume into a file larger than the requested size. resume=False starts over.
  • Retries. A chunk that fails to parse or times out (noise on the line, a dropped character) is retried up to retries times (default 3) after resynchronising the prompt.
  • Speed. Each 16 bytes of memory become a line of about 80 characters of md.b output, so at 115200 baud a dump moves a little over 2 KiB/s and 16 MiB takes about two hours. Copy only the partitions you need when you can (cat /proc/mtd in Linux or the bootcmd offsets tell you where they are).

How to get flash into RAM depends on the board: sf probe; sf read for SPI NOR, nand read for raw NAND, mmc read for eMMC. Make sure the RAM address you pick is free (the load address in bootcmd is usually a safe choice).

From the command line

$ hwn uboot env  --port /dev/ttyUSB0 --interrupt --json
$ hwn uboot dump --port /dev/ttyUSB0 --addr 0x82000000 --size 0x1000000 -o spi.bin
$ hwn uboot dump --port tcp:10.0.0.5:3001 --addr 0x82000000 --size 0x1000000 -o spi.bin

hwn uboot dump resumes like dump() does. It only reads memory: it does not copy flash to RAM for you, so stop autoboot and run sf read (or similar) first, from Python with ub.run() or from a terminal program, then run the dump against the waiting prompt. See the CLI reference for every option.

A complete script is in examples/uboot_dump_flash.py.