Skip to content

Learn in your terminal

Prefer the terminal to a browser? The same nine lessons ship inside hwntools as an interactive tutorial, in the spirit of rustlings, ziglings and vimtutor:

$ pip install hwntools
$ hwn learn

hwn learn: the lesson list, lesson 1, and a passing check

Each lesson prints a short explanation, creates an exercise file with TODOs, and hwn learn check runs your file against the simulated router and tells you whether it got the right answer. Everything runs on your own computer, offline: no board, no adapter, no network.

How it works

$ hwn learn                  # the lessons and your progress (✓ done, ▶ next)
$ hwn learn next             # start the next lesson: explanation + hwn-learn/01_uart.py
$ $EDITOR hwn-learn/01_uart.py
$ hwn learn check            # run it against the simulator
✓ Lesson 1 passed: kernel command line: console=ttyS0,115200 root=/dev/ram0 rw
Next: hwn learn next (lesson 2)

An exercise is a Python file with a solve() function. The checker calls it with a freshly powered-on simulated device and compares what you return with what the device really holds: the kernel command line it printed, the baud rate it runs at, the SHA-256 of its flash, the backup U-Boot environment in its firmware, the serial number behind its login prompt, the reset vector of a chip behind JTAG, and finally a hidden flag. Where a fixed answer could be copied, the checker changes it every run (a different baud rate, serial number or chip).

This is the exercise lesson 1 creates:

hwn-learn/01_uart.py
"""Lesson 1: talk to a UART.

`io` is the console of a simulated router that was just powered on. It prints its boot
log by itself. Return the Linux kernel command line it prints, as a str.
"""


def solve(io):
    # The board prints, among other things:
    #     Hit any key to stop autoboot:  3
    #     ...
    #     [    0.000000] Kernel command line: console=ttyS0,115200 ...
    #
    # TODO: read everything up to b"Kernel command line: " with io.recvuntil(...),
    #       then read the rest of that line with io.recvline().
    #       Return it as str without the line ending (.decode().strip()).
    return None

Commands

Command What it does
hwn learn List the lessons with your progress.
hwn learn next Start the next unfinished lesson.
hwn learn N Start lesson N (0 to 8). An exercise file you already have is kept.
hwn learn check [N] Check your exercise (default: the current lesson). Exit status 0 when it passes.
hwn learn check --watch Re-check every time you save the file, and move on to the next lesson when it passes.
hwn learn verify N Same as hwn learn check N.
hwn learn hint [N] One more hint (three per lesson).
hwn learn solution N Show the reference solution, after asking. --yes skips the question.
hwn learn reset [N] Restore the exercise file(s) and forget progress. Your old file is kept as *.py.bak.
hwn learn console Lesson 3 hands-on: stop the lab router's U-Boot by hand in hwn console.

The workspace

Exercise files and a small progress file (.hwn-learn.json) live in a workspace folder: --dir DIR if you pass it, else $HWN_LEARN_DIR, else the current folder if it already is a workspace, else ./hwn-learn. Delete the folder to start over.

Checks run your file in a separate Python process inside a temporary folder, with a time limit, so a recvuntil() that waits forever or an exception cannot hang hwn learn; your own print() output is shown as usual. Colors are used only on a terminal (set NO_COLOR or pass --no-color to turn them off).

The lessons

# Lesson You return Docs
0 Before you start the lab image size, after agreeing to the rules 0
1 Talk to a UART the kernel command line from the boot log 1
2 Find the baud rate the baud rate of a random board, with autobaud() 2
3 Stop U-Boot the kernel command line after a one-boot init=/bin/sh 3
4 Dump the flash the 32 KiB flash, over md.b 4
5 Analyze the firmware the bootargs of the backup environment that boots a shell 5
6 Get a shell a random serial number, read after logging in 6
7 SPI flash and JTAG a random chip's reset handler, over OpenOCD 7
8 Capture the flag the flag 8

The web lessons go deeper and show more of each API; the terminal lessons are short and graded. Both use the same practice device, hwntools.learn.lab, so they always agree.

Platforms

hwn learn is pure Python and runs on macOS, Linux and Windows. It does not need a pseudo-terminal: the simulated devices run in-process. One exception: lesson 6's simulated Linux console runs commands with /bin/sh, so on Windows that lesson is marked as skipped (use WSL to do it). hwn learn console works in any terminal, including Windows Terminal.

Authorized use only

Lesson 0 starts with the rules. Use what you learn here only on devices you own or are allowed to test; see Safety and legal.