3. Stop U-Boot¶
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.
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]}")
[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 asath>orIPQ#; passprompt=b"..."toUBoot()for unusual ones.printenv()ran U-Boot'sprintenvand parsed thekey=valuelines into adict.- The three variables you printed are the ones that matter most:
bootcmdis what U-Boot runs when the countdown ends. Here: probe the SPI flash, copy 0x8000 bytes from flash offset 0 to RAM at0x82000000, and boot the image at0x82003000. That tells you where the firmware is, which you need in lesson 4.bootargsis the Linux kernel command line.bootdelayis the countdown in seconds.0leaves almost no time to interrupt, and-2usually 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.
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())
[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
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)
[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).