Skip to content

A damaged NSP or XCI is caught at the import, with a message that names it, instead of a crash at "Finding the shaders" - #764

Draft
davdxpx wants to merge 2 commits into
igawa6:mainfrom
davdxpx:damaged-game-files
Draft

davdxpx wants to merge 2 commits into
igawa6:mainfrom
davdxpx:damaged-game-files

Conversation

@davdxpx

@davdxpx davdxpx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Fixes the Peanuts side of #561.

What players get

  • A clear message right at the import. When a game, update or DLC file was copied or downloaded only in part, or its bytes changed on the way, the build stops while it imports and the card says which file it is and what to do:

    The update file Pokémon Legends Z-A [v393216].nsp is damaged or incomplete, so Pokémon Legends: Z-A was not imported. Nothing was changed. Copy it again, or dump it again from your console.

  • No Retry that cannot help. The card offers the game's sheet instead of Retry, because the same file stops the same way every time.
  • Games imported before this reach the shader split as before. If a file of such a .nup is damaged, the split now says so ("Finding the shaders stopped. A game file is damaged: arc › data.trpfs. Copy or dump the game again, then import it again.") with no Retry, instead of struct.error: unpack_from requires a buffer of at least 8725344222 bytes.

Until now a damaged update became a .nup with a garbled file, and the build stopped half an hour later with a raw Python error and a Retry that failed again and again (#561: Z-A 2.0.2's data.trpfs had its right size and a garbled index).

How

  • Importer (dm/peanuts/import/nx_verify.c, new): every plain NCA is checked against its own hash layers while the .nup is written. The fs header against its SHA-256 in the NCA header, the RomFS's IVFC levels and the ExeFS's hash table from the master hash down when the NCA is opened, then each data block as the writer reads it (below the decompression, every block once, on the writer's threads). An update's RomFS is checked through the patched image, and the base bytes it reads against the base's own levels, so the message names the file that is really damaged. The NCA's size is compared with its CNMT record and its header, and a file that ends early is refused before anything else. NCZs keep their whole-file check.
  • Why not hash every NCA whole against its CNMT SHA-256, as NSZ files already are: that reads every byte a second time (minutes on a microSD card for a 15 GB game). The hash layers only hash bytes the import reads anyway.
  • Shader split (shader_split.py): the FlatBuffers readers of Trinity's data.trpfd / data.trpfs check every offset against the file; an index that points outside it ends the split with [RED] /<file>: the game file is damaged: …. PrepareText gives that no Retry (FIX_NONE) and the cause game_file_damaged.
  • App: dev.peanuts.build.DamagedFile turns the importer's message into the card's words; both cases end as FILE_DAMAGED (the game's sheet, no Retry).
  • nximport_cli check now also reads plain NSP/XCI files whole against their hash layers, so a dump can be checked without importing it.

Tests

  • verify_test (new, 18 checks): plain and LZ4 dumps with and without the base's own package import; reading every file checks every block of the patched RomFS and the ExeFS; a file cut short, a container entry shorter than its NCA, and a byte flipped in the base, the update's RomFS header, a file the update carries, its main, a hash level, an fs header, an LZ4 base and a DLC are each refused with the file and its role, and no .nup is written. A damaged byte in a base file the update replaced is never read, so that import goes through.
  • The test fixture's NCAs carry real IVFC / PFS0 hash layers and fs header hashes now. nsp_test (129), ncz_test (58), xci_test (9), nup_meta_test (28), font_test, crypto_test, lz4_test pass as before, also under ASan/UBSan and TSan (x86-64 Linux).
  • peanuts/tests/test_damaged_game_files.py (14): the Trinity readers on a synthetic data.trpfd / data.trpfs (good files read as before; a root, a pack offset or a descriptor past the end, a garbled root as in Pokémon Legends: Z-A — Shader splitting fails in Peanuts Native 0.1.7 #561, a cut descriptor, a pack table outside the file), the card's words, and the wiring. PrepareTextJvmTest: the split's line gets no Retry and its cause.

On an AYN Thor

2026-11-25, the test app's identity, nximport_cli built for arm64 from this branch and from main (4ae5c26).

Pokémon Legends: Z-A, base (4.3 GB), update 2.0.2 (2.1 GB) and the Mega Dimension DLC, from internal storage into a 6.03 GB .nup, each run after sync and a pause:

Run 1 Run 2 Peak memory
main 10.9 s 11.7 s 261 MiB
this branch (with the check) 11.6 s 12.7 s 281 MiB
  • About a second more for a 6 GB game. Both .nups are the same byte for byte (SHA-256 90c6679c…), so a good dump imports as before. The 20 MiB is the last hash level kept in memory (32 bytes per 16 KiB block). Without the sync, the write-back of the previous run's .nup made both swing between 13 and 24 s.

  • nximport_cli check read the three files whole in 15 s: 3 plain NCAs, all good.

  • A copy of 2.0.2 with one byte changed in its middle: main wrote a .nup from it without a word (15.3 s), the damage as in Pokémon Legends: Z-A — Shader splitting fails in Peanuts Native 0.1.7 #561. This branch stopped with:

    Pokemon Legends Z-A v2.0.2 (damaged copy).nsp: the update v393216 is damaged or incomplete: the bytes at 0x1627d8000 of its RomFS do not match their checksum

    and wrote no .nup.

  • The importer's tests on the Thor: verify_test 18, nsp_test 129, ncz_test 54, xci_test 9, nup_meta_test 28, crypto_test, lz4_test, font_test pass, the same counts as main on the Thor (ncz_test counts 58 on x86-64 Linux, on main too).

Not run: the card in the app with a damaged file (its words are covered by the JVM tests), a game on a microSD card, and a desktop build of nximport_cli on Windows or macOS.

…ding the shaders (igawa6#561)

Pokémon Legends: Z-A 2.0.2 stopped at Finding the shaders on a phone with
"struct.error: unpack_from requires a buffer of at least 8725344222 bytes", and
the card offered Retry, which failed the same way every time. The update's
data.trpfs had its right size but a garbled index: the update dump was damaged.
The importer compared an NCZ's bytes with its CNMT record, but never a plain
NCA's, so the damage went into the .nup unnoticed.

The importer now checks every plain NCA it writes from against the NCA's own
hash layers (dm/peanuts/import/nx_verify.c): the fs header against its SHA-256
in the NCA header, the RomFS's IVFC levels and the ExeFS's hash table from the
master hash down when the NCA is opened, and each data block as the writer
reads it. A patch's RomFS is checked through the patched image, and the base
bytes it reads against the base's own levels, so the file named is the one
that is damaged. The NCA's size is compared with its CNMT record and its
header, and a container that ends early is refused before its titles are read.
A mismatch stops the import with "<file>: the update v393216 is damaged or
incomplete: ...", and the card says which file it is (the game, its update or a
DLC), that nothing was changed and what to do, with the game's sheet and no
Retry (dev.peanuts.build.DamagedFile, FILE_DAMAGED).

Hashing each NCA whole against its CNMT SHA-256, as an NCZ is, would read every
byte a second time. The hash layers cost only the hashing of bytes the writer
reads anyway, spread over its worker threads.

A .nup imported before this still reaches the shader split. Its FlatBuffers
readers (trpfs_jobs, trinity_shaders) check every offset against the file now,
and an index that points outside its own file ends the split with
"[RED] /<file>: the game file is damaged: ...". PrepareText gives that line no
Retry and the cause game_file_damaged, which the build shows as a damaged file.

nximport_cli check reads plain NCAs whole against their hash layers too, so a
dump can be checked without importing it.

Tests: verify_test (new, 18 checks): plain and LZ4 dumps with and without the
base's own package import, reading every file checks every block of the
patched RomFS and the ExeFS, and a file cut short, a container entry shorter
than its NCA, and bytes flipped in the base, in the update's RomFS header and
in a file it carries, in its main, in a hash level, in an fs header, in an LZ4
base and in an add-on are each refused with the file and its role. The
fixture's NCAs carry real hash layers and fs header hashes now. nsp_test (129),
ncz_test (58), xci_test (9) and nup_meta_test (28) pass as before, also under
ASan/UBSan and TSan on x86-64 Linux. test_damaged_game_files.py (14) and
PrepareTextJvmTest cover the split and the card's words.
…igawa6#561)

Measured on an AYN Thor with nximport_cli under the test app: Pokémon
Legends: Z-A, its base, update 2.0.2 and a DLC into a 6.03 GB .nup took
11.6 and 12.7 s with the check and 10.9 and 11.7 s without it (main
4ae5c26), each run after sync and a pause. Both .nups were the same byte
for byte. Peak memory went from 261 to 281 MiB, for the last hash level
kept in memory. nximport_cli check read the three files whole in 15 s,
all good.

A copy of the 2.0.2 update with one byte changed in its middle: main wrote
a .nup from it in 15.3 s; this branch stopped with "the update v393216 is
damaged or incomplete: the bytes at 0x1627d8000 of its RomFS do not match
their checksum" and wrote no .nup. The importer's tests (verify_test,
nsp_test, ncz_test, xci_test, nup_meta_test, crypto, lz4, font) pass on the
Thor with the same counts as main.

HOOKS.md has the numbers, the FAQ the second, PEANUTS.md the status.
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