Skip to content

3. Stop U-Boot

Goal: get a U-Boot prompt, read how the board boots, and change it for one boot.

You will learn

  • UBoot.interrupt(), version(), printenv(), getenv(), setenv()
  • what bootcmd, bootargs and bootdelay mean
  • the init=/bin/sh trick, and why hwntools never saves it for you

In your terminal: hwn learn 3 (how)

Interrupt the countdown

U-Boot is the bootloader: it sets up RAM, loads Linux from flash and starts it. During Hit any key to stop autoboot you can stop it and get its own command prompt, from which you can read and change almost anything. UBoot.interrupt() sends Ctrl-C every 50 ms until a prompt appears. On a real board you call it and then power-cycle the target.

03_uboot_env.py
from hwntools import UBoot
from hwntools.learn.lab import device

ub = UBoot(device())
ub.interrupt()  # spam Ctrl-C until a U-Boot prompt appears

print(ub.version())
env = ub.printenv()
for key in ("bootcmd", "bootargs", "bootdelay"):
    print(f"{key:9} = {env[key]}")
Output
[x] Interrupting U-Boot autoboot
[+] Interrupting U-Boot autoboot: got prompt b'=> '
U-Boot 2020.04 (Mar 12 2024 - 10:21:07 +0000)
bootcmd   = sf probe; sf read 0x82000000 0x0 0x8000; bootm 0x82003000
bootargs  = console=ttyS0,115200 root=/dev/ram0 rw
bootdelay = 3

What just happened

  • interrupt() detected the prompt (=>) by itself. hwntools knows common vendor prompts such as ath> or IPQ#; pass prompt=b"..." to UBoot() for unusual ones.
  • printenv() ran U-Boot's printenv and parsed the key=value lines into a dict.
  • The three variables you printed are the ones that matter most:
    • bootcmd is what U-Boot runs when the countdown ends. Here: probe the SPI flash, copy 0x8000 bytes from flash offset 0 to RAM at 0x82000000, and boot the image at 0x82003000. That tells you where the firmware is, which you need in lesson 4.
    • bootargs is the Linux kernel command line.
    • bootdelay is the countdown in seconds. 0 leaves almost no time to interrupt, and -2 usually disables interrupting altogether.

One boot with init=/bin/sh

init= tells Linux which program to start first. If you replace it with a shell, the kernel hands you a root shell before any login or password check runs. inject_bootargs() adds it to bootargs in RAM only; nothing is written to flash, so the next power cycle boots normally.

03_inject.py
from hwntools import UBoot
from hwntools.learn.lab import device

io = device()
ub = UBoot(io)
ub.interrupt()

ub.inject_bootargs("init=/bin/sh")  # RAM only, nothing is saved
ub.boot()
io.recvuntil(b"Kernel command line: ")
print("kernel sees:", io.recvline().decode().strip())
Output
[x] Interrupting U-Boot autoboot
[+] Interrupting U-Boot autoboot: got prompt b'=> '
[*] bootargs = console=ttyS0,115200 root=/dev/ram0 rw init=/bin/sh
[*] Booting with 'boot'
kernel sees: console=ttyS0,115200 root=/dev/ram0 rw init=/bin/sh

Simulator limit

The simulated board stops after printing the kernel command line, so you do not get the shell here. On a real device you would now see a # prompt, usually with / mounted read-only and no services running. Lesson 6 practises the shell part on a simulated Linux console.

Try it

Try it

Set bootdelay to 0 with setenv(), read it back with getenv(), then try to make the change permanent with saveenv(). What happens, and why?

Solution
03_try.py
from hwntools import SafetyError, UBoot
from hwntools.learn.lab import device

ub = UBoot(device())
ub.interrupt()
ub.setenv("bootdelay", "0")
print(ub.getenv("bootdelay"))
try:
    ub.saveenv()
except SafetyError as exc:
    print("refused:", exc)
Output
[x] Interrupting U-Boot autoboot
[+] Interrupting U-Boot autoboot: got prompt b'=> '
0
refused: saveenv writes to flash; pass force=True if this is intended

setenv() only changes RAM. saveenv() writes the environment to flash, which can brick a board if it goes wrong, so hwntools raises SafetyError unless you pass force=True. Every destructive call works this way (see Safety and legal).

Next: Dump the flash