Skip to content

Simulator

See the simulator quickstart for an introduction.

sim

Simulated devices for dry runs and tests: a fake U-Boot console and a fake Linux shell.

A SimDevice can be driven in-process through SimTube, or exposed on a real pseudo-terminal with PtyBridge (POSIX only) so that hwntools.serial talks to it exactly like a USB-UART adapter:

from hwntools import serial
from hwntools.sim import FakeUBoot, PtyBridge

with PtyBridge(FakeUBoot()) as bridge:
    io = serial(bridge.port)
    ...

These fakes model the behaviour that matters for scripting (echo, prompts, autoboot timing, md.b formatting, login prompts) and are not emulators.

SimDevice

SimDevice()

Base class: a byte-oriented device with an output queue and an input handler.

emit

emit(data: bytes | str) -> None

Queue data as device output (str is UTF-8 encoded).

on_input

on_input(data: bytes) -> None

Handle bytes sent by the host. Override in subclasses.

tick

tick() -> None

Called periodically while the host reads; use for time-based behaviour.

write

write(data: bytes) -> None

Host side: deliver data to the device. Raises EOFError once closed.

read

read(numb: int, timeout: float | None) -> bytes

Host side: return up to numb bytes of output, b"" on timeout.

close

close() -> None

Close the device; pending and future reads raise EOFError.

SimTube

SimTube(device: SimDevice, timeout: float | None = None, newline: bytes | None = None)

Bases: Tube

In-process tube connected to a SimDevice.

FakeUBoot

FakeUBoot(memory: bytes = b'', mem_base: int = 2147483648, env: Mapping[str, str] | None = None, prompt: str = '=> ', autoboot_delay: float = 1.0, stop_string: str | None = None, corrupt_md_every: int = 0, max_md_count: int = 65536)

Bases: _LineDevice

A U-Boot console with autoboot countdown, env and a memory region for md.b.

Parameters:

Name Type Description Default
memory bytes

bytes visible at mem_base.

b''
env Mapping[str, str] | None

initial environment.

None
autoboot_delay float

seconds the countdown waits for a key (0 = boot immediately).

1.0
stop_string str | None

if set, only this string (e.g. "tpl") stops autoboot.

None
corrupt_md_every int

corrupt one byte of every N-th md.b output (tests retries).

0

FakeLinuxShell

FakeLinuxShell(credentials: Mapping[str, str] | None = None, root: bool = True, cwd: str | None = None, path: str | None = None, hostname: str = 'device', boot_log: str = '', command_hook: Callable[[str], str | None] | None = None)

Bases: _LineDevice

A serial console with an optional login prompt in front of a real /bin/sh.

Every command line is executed with sh -c (POSIX only) in cwd, so file reads, base64, od and friends behave like on a device. path restricts the tools that are available, which lets tests exercise fallbacks.

prompt property

prompt: str

The shell prompt, root@<hostname>:~# or user@<hostname>:~$.

PtyBridge

PtyBridge(device: SimDevice)

Expose a SimDevice on a pseudo-terminal (POSIX only).

bridge.port is the slave device path to open with hwntools.serial.

close

close() -> None

Stop the bridge thread, close the pseudo-terminal and the device.