1. Talk to a UART¶
A console is just a stream of bytes¶
On a real board you would solder or clip a USB-UART adapter to the TX, RX and GND pins
and open the port with serial("/dev/ttyUSB0", 115200). hwntools hands you back a tube:
an object you can read bytes from and send bytes to. TCP (remote()), local programs
(process()) and the simulator (SimTube) give you the same kind of object, so the code
after the first line is the same everywhere.
lab.device() "powers on" the simulated router and returns a SimTube connected to its
console. The board starts printing straight away, like a real one.
from hwntools.learn.lab import device
io = device() # "power on" the lab board
banner = io.recvuntil(b"Hit any key to stop autoboot:")
print(banner.decode().strip())
# We press nothing, so the countdown runs out and the board boots Linux.
io.recvuntil(b"Kernel command line: ")
print("kernel cmdline:", io.recvline().decode().strip())
U-Boot 2020.04 (Mar 12 2024 - 10:21:07 +0000)
CPU: Simulated ARMv7 @ 800 MHz
DRAM: 128 MiB
SF: Detected w25q128 with page size 256 Bytes, erase size 4 KiB, total 16 MiB
In: serial
Out: serial
Err: serial
Net: eth0
Hit any key to stop autoboot:
kernel cmdline: console=ttyS0,115200 root=/dev/ram0 rw
It takes about three seconds: that is the autoboot countdown running out.
What just happened¶
io.recvuntil(b"Hit any key to stop autoboot:")read everything up to and including that text. Reads wait for data; if the text never comes, they give up aftercontext.timeout(10 seconds by default) and returnb"", leaving what arrived in the buffer, just like pwntools. Passstrict=Trueto get anHwnTimeoutErrorinstead.io.recvline()read up to the next newline.- Everything is
bytes. Use.decode()to print it.
Reading the boot log¶
Even before you type anything, this log is useful:
U-Boot 2020.04: the bootloader and its version. Old versions have known bugs.SF: Detected w25q128 ... total 16 MiB: there is a 16 MiB SPI flash chip. That is where the firmware lives, and its size is how much you would dump.Hit any key to stop autoboot: the boot can be interrupted. You will do that in lesson 3.Kernel command line: console=ttyS0,115200 ...: Linux uses this UART as its console too, so whatever Linux prints later (and maybe a login prompt) comes out of the same pins.
On real hardware
To just watch a console and type into it, you do not need a script:
hwn console /dev/ttyUSB0 -b 115200 --log session.log opens an interactive terminal
and keeps a log of everything the board printed (Ctrl-] quits). Try it now with
hwn console --sim, and press a key within five seconds to stop the simulated autoboot.
Simulator limit
The simulated board stops after printing the kernel command line. A real one would keep going with hundreds of kernel messages and end at a login prompt or a shell.
Try it¶
Try it
Power on a fresh device and print only the line that names the network interface
(the one starting with Net:), using a single call. Hint: look at the tube methods
whose names start with recvline.