Skip to content

Storage

The iPod's disk holds a small FAT firmware partition and the HFS+ data partition, where the music, the databases and the artwork live. Chikuma drives both, in the arrangement the device uses, which the kernel page explains. The code is in chikuma/drivers/fs/, with the ATA driver in chikuma/drivers/hw/.

The layers

From the disk up:

Layer What Chikuma reproduces
ATA reads and writes with 28-bit and 48-bit addressing, through DMA, with programmed I/O as the fallback
CE-ATA the firmware's own driver, for boards that carry a CE-ATA disk on the SD controller, chosen by a strap pin
Partitions the MBR at block 0, then the Apple Partition Map where one is found
FAT FAT16 read and write for the firmware resource partition, and the FAT32 self-formatter
HFS+ read, in-place writes, growing a file, create, delete, rename, extended attributes, and a journal of Chikuma's own

ATA

The drive decides from its own identify words whether 48-bit addressing is on, and how big it is. The 28-bit guard refuses an over-range write instead of masking the address, because a masked write would land on the wrong sectors.

The CE-ATA driver was found in the firmware and sits behind the same interface. The device models cover the disk hardware underneath.

Partitions

Chikuma reads the MBR first, as the firmware does. The MBR gives the firmware partition and the window the data region sits behind.

The data partition is nested and carries its own partition map inside. A flat HFS+ volume with no inner map will not register, and that matches how a real synced device is laid out.

FAT

For a blank or foreign disk the firmware creates a FAT32 volume, and Chikuma reproduces that formatter, which never writes HFS.

4K logical sectors

The iPod disk presents 4096-byte logical blocks. The filesystem requests in 4096-byte units, and the block layer scales to the device's own sector size.

The firmware brings up the volume with one 4096-byte read at the start of the data partition. The HFS+ volume header sits 1024 bytes in, so it falls inside that one block.

Images built for 512-byte sectors fail silently

An image built on a 512-byte assumption puts the partition and the header at the wrong byte offsets. The volume then fails to mount with no obvious cause. Chikuma's images use the 4K geometry.

Three sector sizes are in play at once: the ATA transfer unit, the partition map's unit, and the FAT parameter block's own.

Whole physical sectors

The PATA drive in the development unit is stricter still. It answers any read or write that does not cover a whole, aligned 4 KiB physical sector with an error, at once and with no other sign.

So Chikuma reads whole physical sectors and serves smaller requests out of them. A write that covers part of a sector reads the sector, patches it and writes it back whole. The emulator's disk model can refuse the same commands, and that is how the fix is tested.

The filesystem posts to a queue and blocks

The filesystem builds a request, pushes it onto a work list and blocks. A separate disk task issues the command later, on its own stack, and the completion interrupt wakes the caller.

Chikuma reproduces this seam exactly, because the device is arranged this way and structure follows the firmware. A stack trace taken when a disk command goes out can never name the filesystem code that wanted the data. That code is in another task, already blocked.

The full request chain was recovered by running it and watching the queue, since its two ends live in different tasks. The FAT and HFS+ read and write drivers are told apart the same way, by behaviour. The seam between them gets built at mount, with no fixed table in the image.

HFS+

HFS+ is a published on-disk format, in Apple's Technical Note TN1150 and Apple's open-source HFS sources. Chikuma treats the format as a standard. The one thing it recovers from the firmware is which operations the firmware performs, in what order, with what arguments.

The volume header fields, the four special forks, the catalog and extents B-trees, the fork layout and the big-endian encoding all follow the standard. The values recovered from the firmware were checked against Apple's published format headers, and they agreed.

The catalog and the B-trees

The catalog reader walks the catalog B-tree, big-endian throughout:

  • the header node and the node size
  • records stored back to front from each node's end
  • keys made of a parent id plus a name
  • folder and file record types, and a file's data fork

The B-tree code is tree-agnostic, shared by the catalog, extents-overflow and attributes trees. Nodes split by record count, with no borrowing from a neighbour. Apple's own code does not rotate either.

A synthetic split test forces new root nodes and checks the record count on that path, which integration runs of 500 files never reach.

Growing the tree

A create into a full catalog leaf goes through instead of being refused. The leaf splits, the record goes into whichever half holds its slot, and the new key travels up the path just walked, creating a new root when it has to.

That split is Chikuma's own, built on the published format, because the firmware's half of the B-tree write has not been read. A test forces dozens of creates through small nodes and checks that the tree stays sorted and every record stays findable.

Dates

Chikuma writes exactly three catalog date fields: create, content-modified and backup. They are stamped in UTC with the standard 1904-to-1970 epoch offset. The format has two more date fields, but nothing measured fills them, so Chikuma leaves them alone.

The journal is Chikuma's

The firmware writes a journaled HFS+ volume but never maintains the journal. The journal's magic never appears in the image, and first-boot writes never land in the journal region on the emulated machine.

So the journal writer and replayer are declared OURS. Current macOS silently discards a pending journal on a volume with allocation blocks larger than 4K, and the iPod's geometry has them. Chikuma replays its own journal at mount, validating first, all or nothing. With journalling turned off, the write path does exactly what the firmware does.

Row order comes from the database

The order of rows in Songs, Albums, Artists, Genres and Composers is the database's order. iTunes writes index lists into the iTunesDB already sorted, and the device builds its lists from those. When an index is unusable, the device sorts at boot with a Unicode-weight collation, and Chikuma does the same. The music database covers both paths.

Tests

Tests run against the real volume image and real iTunesDB databases. Fixtures written to match the parser are not used.

For writes, the oracle is macOS's own fsck_hfs, run on a temporary copy of the image. Deliberately corrupted volumes serve as negative controls, and show that the check fails when it should. The delete and split paths are covered this way.

Writes on the real iPod

Writes are on by default, and the write path has run on real hardware. On the development unit, with the volume's metadata backed up first, Preferences were rewritten in place. A new file was created in the catalog: journal transaction, catalog insert, parent valence, both volume headers. A forced fsck_hfs passed a copy of the volume rebuilt from the device's own metadata, before and after.

The same unit's 160 GB drive was laid out and formatted by an HFS+ formatter built on Chikuma's own HFS+ code. It is declared OURS, since the firmware itself only ever formats FAT32.

The mount decision

The mount gate is open for the moment. What turns a found partition into a mounted volume is tied to device discovery and is not fully recovered. Status has the live account.