Skip to content

feat: keep nano 6G/7G play data across writes and enable sync for the iPod nano 7G - #79

Open
Pred8or wants to merge 10 commits into
jvgomg:mainfrom
Pred8or:pr/4-nano7-preserve
Open

Pred8or wants to merge 10 commits into
jvgomg:mainfrom
Pred8or:pr/4-nano7-preserve

Conversation

@Pred8or

@Pred8or Pred8or commented Oct 28, 2026 •

Copy link
Copy Markdown

Part 4 of 8 for iPod nano 7th generation support (overview: #75). Stacked on #78: until that merges, this PR also shows its commits; only the commits named below are new here.

Branch pr/4-nano7-preserve (2 commits: 27f33d0, 9c7d699). Review after PR 3.

  • feat(libgpod): the nano records plays, skips, ratings, bookmarks and whether a track was started in Dynamic.itdb. libgpod rebuilt that file from the iTunesDB and Play Counts only, so every write threw that away. Now itdb_parse() reads Dynamic.itdb by track id (plays counted once, later dates win), takes exact UTC dates from Library.itdb/Locations.itdb, keeps the device's audio/subtitle track choices and its "played" mark, and writes SQLite dates as UTC with 0 for unset. A write fails rather than replace Dynamic.itdb when it could not be read.
  • feat(devices): nano_7g becomes syncable, with verified: 'hardware'. The nano 6G stays read-only (synthetic tests only). Nano 7G videos use the ipod-classic profile until PR 5.

Hardware test on my iPod nano 7G (firmware 1.0.4), starting from the library macOS had put on it, with macOS's device agents paused for the whole session so nothing else touched the iPod:

  • a rewrite without changes, an added track, a playlist, a rating set and cleared from the computer, and a removed track all showed and played on the iPod, also after a reboot;
  • plays and skips recorded on the iPod were counted once over repeated syncs, and a rating set on the iPod was kept; iTunesCDB and Dynamic.itdb agreed after every write;
  • a write in another time zone (America/Los_Angeles) changed no date;
  • signatures, schema level, the six categories and SQLite integrity were checked on a copy after every write.

That run used an earlier build of this branch. It showed that a write reset the "played" mark, which the iPod sets as soon as a track is started or skipped. I fixed that and repeated the check with the final code: the marks of four tracks the iPod had just started or skipped stayed set over two writes, nothing else changed, and the iPod played everything after a reboot.

Not covered by hardware: adding missing categories to an empty library, and bookmarks (synthetic tests only).

Notes, all unchanged libgpod behaviour: normal smart playlists lose their rules in Library.itdb (the category playlists keep theirs); libgpod numbers the location kinds its own way (playback is not affected); when the iPod changes a rating, libgpod keeps the earlier computer rating in user_rating_common (the iPod doesn't show it).

Testing: round trip: plays counted once over repeated writes, Play Counts dates with and without a Dynamic.itdb row, activity recorded only in Dynamic.itdb (track choices and the played mark), edits and clears from the computer, kept/removed/new tracks, four host time zones, an unreadable Dynamic.itdb. Integration tests of libgpod-node and core against the patched libgpod.

…an invalid signature

On macOS 27 (arm64, Bun 1.3.13) the compiled binary keeps the linker's
ad-hoc signature after Bun appends the payload. codesign --verify reports
"code or signature have been modified" and the kernel kills the binary on
launch with status 137. compile.sh now verifies the signature on darwin and
re-signs ad hoc only when verification fails.
A Nano 7 synced by macOS carries iTunes Library.itlp and iTunesCDB but no
iTunesDB; libgpod parses the CDB, yet IpodDatabase.hasDatabase only probed
iTunesDB. device scan reported "iTunesDB not found" and sync refused with
IPOD_NEEDS_INIT, pointing at device init for a device with 21 tracks.
hasDatabase now accepts either file.
…line

runRepair ran its own refusal check with the injected assessIpodIdentity
but called runRepairPipeline without deps, so the pipeline's second
check assessed the real device path. The supported-device test therefore
failed on any host without an iPod at /Volumes/iPod. The assessor is
now forwarded through the pipeline's existing deps; both checks stay,
and the test counts that both used the injected assessor.
…abases

The nano 6th and 7th generation sign iTunesCDB and Locations.itdb.cbk
with HashAB. libgpod 0.8.3 only computes it through an external blob
loaded from LIBGPOD_BLOB_DIR, which podkit does not ship. dstaley/hashab
is an Unlicense C implementation of the same calcHashAB function.

The C sources, headers, licence and the 100 upstream reference vectors
are copied byte for byte from the pinned commit; UPSTREAM.md records the
source. Nothing builds them yet.
…B signer

libgpod 0.8.3 loads calcHashAB from an external hashab module in
LIBGPOD_BLOB_DIR and fails nano 6G/7G writes without it. A small patch
replaces that loader with a direct call into the vendored sources, which
prepare-libgpod-nano.sh copies into the libgpod tree before autoreconf.
They are built as an internal convenience library with -fwrapv, and
their global symbols are renamed and hidden, so that they neither clash
with other code linked with libgpod nor become part of its interface.
The macOS developer build and the static prebuild dependencies both
apply the patch. The signer only accepts an eight-byte FireWire GUID, so
the patch checks for 16 hex digits first and fails the write otherwise.

The static dependency cache now records a fingerprint of the patch and
the vendored sources, and the CI cache keys hash all of tools/prebuild,
so either change rebuilds libgpod. macOS static links name the system
sqlite3 and libxml2 that libgpod already uses, and Linux static links
group the archives, so standalone executables link too.

tools/nano7 holds the checks: a read-only probe that verifies the
signatures of a copied Locations.itdb/.cbk pair and iTunesCDB, including
the hashing scheme libgpod writes, tests against the upstream vectors
and a reference checksum book, and a libgpod roundtrip for nano 6G/7G
that the prebuild workflow runs. ADR-030 records the decision. Both
generations stay read-only.
The support notes, unsupported-device reasons and docs still said
libgpod cannot produce the nano 6G database format or needs an external
hashab blob. With the signer linked in, the remaining blocker is
different: on a nano 7G the firmware shows no music for a library libgpod
writes, even though its signatures verify. The nano 6G write path is
covered only by synthetic tests.

Both generations stay read-only. The notes and reasons now say that
syncing is not supported yet and, for the nano 7G, what the firmware
shows; the generated support matrix, the supported-devices page and the
device notes follow, the generation notes in docs/formats/generations.md
and docs/test-devices.md point to ADR-030, and the classify/identify
tests track the new reason. The generation comments drop the test
devices' serial numbers.
…evel

A nano 7G (firmware 1.0.4) shows no music for a library libgpod writes,
although its signatures verify. Replacing the databases on the device
showed that the firmware needs the hidden category containers together
with iTunes' schema level; the containers alone are not enough.

For devices that sign with HashAB and are not iOS devices, the patch now
- adds the columns iTunes' Library.itdb has beyond libgpod's schema,
  appended after the tables are created; the inserts that relied on
  column positions name their columns,
- writes version_info 1.117 as iTunes does and sets the Genius.itdb
  user_version to 2,
- runs the device's post-process command set 25, which fits that schema,
  adds is_home_video and sets user_version 26, instead of the newest set.
  Two commands of the set add a column that is already there:
  item_stats.rental_expired, which libgpod's schema has, is skipped once
  the column is confirmed, and the repeated AddGenreMapCompilationCount
  only after its first run succeeded.

On these devices a missing command set, a missing, malformed or failing
command, an insert that fails while the databases are generated or a
failed commit now fails itdb_write() before any library file is
replaced; failed post-process commands are rolled back. Other devices
keep logging such failures. Attached database paths are quoted.

The tools/nano7 roundtrip checks the schema level for nano 6G/7G, that
each kind of command failure, a rejected insert and a commit that fails
as on a full disk leave the library unchanged, that the first of the
repeated genre commands runs, and that a nano 5G keeps libgpod's layout.
Both generations stay read-only.
The nano 6G/7G firmware browses six hidden smart containers: Music,
Videos, Movies, TV Shows, Audiobooks and Rentals. iTunes keeps them as
dataset 5 playlists in the iTunesCDB and as container rows in
Library.itdb. libgpod parsed and rewrote the dataset 5 playlists but
wrote no SQLite rows for them.

On devices that sign with HashAB and are not iOS devices:
- Dataset 5 headers keep +0x52, the category kind that SQLite stores as
  distinguished_kind, and +0x54 as read. libgpod copied +0x50 into
  +0x52, which turns the Videos category (+0x50 0, +0x52 1) into an
  ordinary playlist for iTunes.
- Library.itdb gets one hidden, live smart container per category, with
  iTunes' smart settings and the rules as smart_criteria, serialised by
  the existing mhod 51 writer. The firmware evaluates the rules itself,
  so no members are written; Music gets the master playlist's media
  kinds, as in iTunes. Dynamic.itdb gets their container_ui rows.
- A library that lacks a category, such as a new one, gets it with
  iTunes' rules.
- The Rentals rule has an action libgpod cannot classify; it is written
  as read instead of passing through itdb_splr_validate(), which only
  warned about it.

The roundtrip checks the rows and rules in both files, that they stay
the same over resaves, that missing categories are added while the
others keep their ids, Music's media kinds with audio and a movie, that
the three header fields round-trip independently, and that a nano 5G
still writes dataset 5 as before.
…writes

The nano 6G/7G firmware records plays, skips, ratings, bookmarks and
whether a track was started in Dynamic.itdb, and reports plays and skips
in Play Counts as well (the count minus play_count_user_original).
libgpod rebuilt Dynamic.itdb from the iTunesDB and Play Counts only, so
a write dropped what the device had recorded there. SQLite dates were
also off by the host's offset, and an unset one was written as
-978307200 minus the offset.

On devices that sign with HashAB and are not iOS devices:
- itdb_parse() reads the device's Dynamic.itdb by item pid after the
  Play Counts merge. Counts take the larger of the two totals, as the
  parser has added the Play Counts delta already; ratings, bookmarks and
  recent counts take the device's value, and play and skip dates the
  later of the two reports. A track the device marked as played, which
  it does when a track is started or skipped, stays marked. The exact
  UTC dates of items and playlists come from Library.itdb and
  Locations.itdb when they can be read. Edits made after parsing are
  written as usual, and the post-process commands reset the *_original
  counts to the totals.
- A write keeps the audio and subtitle track choices the device recorded
  for items still in the library; the post-process commands would reset
  them. It fails, rather than replace the file, when Dynamic.itdb could
  not be read at parse time.
- SQLite dates are UTC seconds since 2001 and an unset one is 0; the
  value libgpod wrote for an unset date before reads as unset.
  Locations.itdb records the date added, as iTunes does, and an unset
  one stays 0.
- Play Counts dates use the database's fixed offset and the file's "no
  date" value sets none; a reported skip date is taken over like the
  play date.

The roundtrip covers plays counted once over repeated writes, Play
Counts dates with and without a Dynamic.itdb row and when the two
reports differ, activity recorded only in Dynamic.itdb including the
four track choice columns and the played mark, host edits that change or
clear it, kept, removed and new items, a Dynamic.itdb without track
choice columns, dates in four host time zones, and an unreadable
Dynamic.itdb. Both generations stay read-only.
With the firmware categories, iTunes' schema level and the device's play
data kept, podkit's libgpod writes the iPod nano 7G library in the form
its firmware needs: on firmware 1.0.4, the categories together with that
schema level made a library show and play, also after a reboot. A nano
7G on that firmware showed and played libraries written by this code,
kept the plays, skips and ratings it had recorded, and took added and
removed tracks and playlists, so nano_7g becomes syncable with hardware
provenance. Its USB product IDs leave the unsupported table, so
detection, readiness and sync treat it like the other syncable nanos.
Its videos use the ipod-classic profile, as for other generations
without a profile of their own.

The nano 6G stays read-only: podkit writes its library the same way, but
only synthetic tests cover that. Its note and refusal say so now instead
of claiming that writes are unsupported.

Tests that used the nano 7G as their example of a read-only or refused
device use the nano 6G. The generated support matrix, the
supported-devices page, the device notes and tools/nano7 follow, and the
changeset covers the libgpod-node changes of the previous commits.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant