Running it in the emulator¶
Chikuma needs no iPod to work on. The machine in qemu-ipod/ is a QEMU model of the S5L8702 that
boots both Apple's RetailOS and Chikuma against the same disk image, and that is what makes the
project's claims checkable.
The two commands¶
cd qemu-ipod
tools/retailos.py # Apple's firmware, in a window
tools/run.py --payload ../chikuma/payload.elf # Chikuma instead
retailos.py is run.py with the device's identity and the savestate conveniences added. A payload
or an unusual configuration goes to run.py directly, and --help lists everything. The older
./retailos.sh and ./run.sh still work, as shims that print a note and run these.
Headless, for a single frame:
The machine reaches the Language screen in about 5 seconds.
Booting through the real chain¶
run.py --payload loads the ELF straight in, from a fresh reset, and skips everything before the
OS. ./run_secure_boot.sh boots the way the device does, through the reproduced boot ROM, the C ONB,
the signature check and the hand-off. The whole chain, and Tomodachi covers it, along
with the macOS app that drives the machine as an iPod.
Two ONB gates run the same models without a window:
| Gate | What it checks |
|---|---|
gates/onb_integ_gate.py |
boots a bare-metal payload that drives one ported module, and takes its verdict from the exit code |
gates/onb_display_gate.py |
the panel's pixels, for the display trio |
Driving it by hand¶
In the window, the keys map to the iPod's controls. The pointer is absolute, so the window never grabs the mouse.
| Input | iPod control |
|---|---|
| two-finger scroll, mouse wheel | click wheel |
| Up / Down | one wheel step |
| Enter, Space, left click | SELECT |
| Left / Right | PREVIOUS / NEXT |
| M, Esc, right click | MENU |
| P | PLAY/PAUSE |
Driving it from a script¶
tools/ipodmachine.py is the Python interface, and tools/drive.py is a thin CLI on top of it.
Import it rather than re-implementing the QMP handshake.
A script steers with the dock's Simple Remote, the way an accessory does over iAP. One press is exactly one row, which the wheel never promises. The stock firmware obeys it too, because an unauthenticated accessory may send it.
Where you are is then read out of the running interface: the focused list's class, id, row count and selection. That works on either firmware, and nothing is guessed from a screenshot.
from ipodmachine import Machine
with Machine(audio="coreaudio", wav="/tmp/out.wav", debug="sm1") as m:
m.wait_for_screen() # boot, then the first frame with real content
m.remote("down") # one row, exactly
m.remote("select")
print(m.ui_tree()) # Chikuma's live view tree; m.retail_ui() on RetailOS
m.shot("/tmp/menu.png") # for the human, not for steering
print(m.uart()) # what the guest printed
The wheel calls (press, scroll, go_to_row) are still there, for testing the wheel itself.
Sockets, logs and the gdb port are per process. A probe never disturbs an emulator already running, and two probes can run at once.
Input and timing pitfalls¶
A press must have a duration
Down and up sent in one batch make a zero-length press, and the firmware's debounce never sees
it. About 120 ms of guest time is a tap, and a second is a press-and-hold. The monitor's
sendkey gets this right by itself.
The wheel is coarse, and its gain is not fixed
A menu row costs between six and a dozen notches depending on the list, and the wheel
accelerates. An open-loop scroll(N) that works on one screen overshoots on the next. Simple
Remote avoids the problem. When the wheel itself is under test, go_to_row closes the loop on
the selection the interface reports.
--warp breaks interaction
icount keeps the instruction count exact, and by default virtual time follows real time. With
--warp (sleep=off) QEMU skips the idle, and the guest clock runs about 35× fast. A
100 ms press arrives as a three-second hold, and the firmware's 150-second idle sleep lands
about four seconds after boot. --warp is for batch boots that take no input.
The disk decides the library¶
The library lives on the disk image. The default, chikuma/build/ipod_apm.img, is a small test
volume. A larger one with movies and podcasts serves the media work and is passed explicitly.
When the menus show up but the songs do not, the FireWire GUID is wrong. The retail database is bound to the GUID it was synced with, and the machine has to present the same one.
Debugging¶
tools/hook.sh attaches gdb hooks by the Ghidra project's own function names:
Counting instead of breaking¶
When cadence is what's under test, a gdb breakpoint is the wrong instrument. Its RSP round trip on every hit changes what it measures.
tools/pccount_plugin.c is a TCG plugin that counts executions of given runtime addresses and dumps
them to a file. In Cover Flow it ties the hitch at each cover change to 100 texture uploads per
flick.
Frames as a measurement¶
The pipe model writes one file per published frame, so the timestamps give the real update cadence. A screenshot loop samples at its own rate and misses the hitches.
As a scripted action, frames <dir> starts and stops the dump mid-run, so the boot's frames need
not be kept.
Traffic logs and savestates¶
--debug <subsystem> (sm1, i2c, pmu, …) turns on the machine's own traffic logs, and often
that is the whole experiment. With --debug i2c, for example, the volume curve is checked by
comparing the codec writes of Chikuma's payload with the original's.
A savestate frozen just before the interesting moment saves the guest time to reach it, which can be an hour or more. A 90-minute end-of-track run becomes a three-minute one.
Why QEMU¶
QEMU won over a Python and Unicorn emulator on interrupt timing. Unicorn delivers interrupts only at slice boundaries. QEMU raises the line the instant a device asserts it, as the hardware does. An interrupt landing in the middle of a kernel service faults under QEMU and never shows up under Unicorn.
The measurements behind each device model sit in qemu-ipod/devices/, as comments in the QEMU
models.