diff --git a/CLAUDE.md b/CLAUDE.md index 5447ed5..2107fc8 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -18,7 +18,7 @@ src/nxhmi/ pictures.py N.i / N.is fonts.py .zi v6 fonts, generation from TrueType render.py page renderer matching the Editor canvas - tft.py .tft research helpers (layout table, same-length trailer patch) + tft.py .tft research helpers (layout table, header checksums, trailer, seal) cli.py python -m nxhmi ... mcp_server.py MCP server (stdio JSON-RPC, no deps) -- tools for Claude incl. render_page images examples/make_demo.py builds demo/ (demo.HMI, decoded/, render/) diff --git a/README.md b/README.md index 49c6af4..9a311b1 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Every percentage is a count with a stated basis, so it can be checked (details i | Readable attribute names in JSON | **100 %** (31/31 types) | every value list the Editor documents, incl. the Intelligent-only types and load effects | | Rendering, Basic components | **100 %** (20/20 pixel-exact) | incl. gauges and QR codes (own encoder, no dependencies) | | Rendering, all components | **94 %** (29/31 pixel-exact) | Intelligent ComboBox and Switch are drawn, but their anti-aliased edges are approximate (~99 % / ~92 % of pixels exact) | -| `.tft` compilation | **31 %** (2.5/8 milestones) | layout mapped, layout table decoded, trailer checksum (same-length edits only); not usable yet | +| `.tft` compilation | **50 %** (4/8 milestones) | layout mapped, layout table decoded, all four checksums solved (any length); page format not yet, so not usable | ## Compatibility diff --git a/docs/COVERAGE.md b/docs/COVERAGE.md index bcc8eba..0628de0 100644 --- a/docs/COVERAGE.md +++ b/docs/COVERAGE.md @@ -29,7 +29,7 @@ byte for byte, or a file written by nxhmi was opened in the Editor and showed th - **Rendering**: a component type counts as covered when every test case of it is pixel-identical to the Editor canvas (name tags and selection outline excluded). Invisible types (timer, variable, hotspot, touch cap) count when they draw nothing, like the Editor. -- **`.tft` milestones** (8): layout map; layout table; trailer checksum (half: same-length only); +- **`.tft` milestones** (8): layout map; layout table; trailer checksum; the three header checksums; compiled page/component format; event-code tokens; pictures and fonts in the TFT; a builder that produces a file the display accepts. @@ -110,13 +110,13 @@ byte for byte, or a file written by nxhmi was opened in the Editor and showed th |---|---| | File layout | mapped (header, embedded Nextion files, project section, trailer) | | Layout table | decoded (fixed XOR key) | -| Trailer checksum | works for same-length edits; **unknown for different lengths** (stuck) | -| 3 header checksums (0x44, 0xC4, 0x18C) | not analysed | +| Trailer checksum | solved for any length (`tft.trailer`); one model measured | +| 3 header checksums (0x44, 0xC4, 0x18C) | solved (`tft.header_checksums`, `tft.seal`) | | Compiled page format, event-code tokens | first look only | | Pictures and fonts inside the TFT | not analysed | | Licensing | every `.tft` embeds Nextion's firmware files; the repo must never ship them -- a builder would read them from the user's own Editor installation | ## Suggested next steps -1. `.tft`: try the remaining trailer-checksum ideas listed in NOTES.md; then the compiled page format. +1. `.tft`: the compiled page/component format and the event-code token table (see NOTES.md). 2. The anti-aliasing routine of Intelligent rounded shapes (switch knob/track, ComboBox). 3. The premultiplied-alpha colour step for transparent JPEG pictures (a few levels off). diff --git a/docs/NOTES.md b/docs/NOTES.md index 89a398a..09d6b6b 100644 --- a/docs/NOTES.md +++ b/docs/NOTES.md @@ -104,14 +104,29 @@ ending right before the trailer -- proved on two independent one-change pairs. C **same-length edits can be re-checksummed** (`nxhmi.tft.patch_trailer`, reproduces the Editor's output byte for byte for a colour change and for `dim=50` -> `dim=51`). -**STUCK (2026-11-15):** start offset / seed of the trailer CRC across files of different lengths. -Tried, all without a fit (three minimal files of different lengths + the 6-page project): -every start offset 0..end with a constant seed (solved exactly by GF(2) linear algebra); seeds -taken from header fields 0x44/0xC4/0x18C (raw, inverted, LE/BE); the header checksum fields -zeroed or 0xFF in every combination; the layout table de-obfuscated before checksumming. -Ideas not yet tried: the stream skipping a region whose size varies (e.g. FF padding after the -page data); a seed derived from file size or layout words; the trailer covering only the -project section in a different order (e.g. pages then the zero area). +### All four checksums solved (2026-11-16) -Not started: the 3 header checksums, the compiled page format (component records, the token -table for event code), pictures/fonts inside the TFT. +All are the word-per-byte CRC with seed 0xFFFFFFFF (`nxhmi.tft.header_checksums`, `trailer`, +`seal`); verified on every sample (two layouts, 0x40000 and 0x90000 resource areas): + +| Where | Covers | +|---|---| +| 0x44 | the resource area `[0x10000, 0x10000 + u32@0x40)` -- firmware files, pictures, fonts. Constant for all picture-less projects of one model, which is why it looked "layout only" | +| 0xC4 | header `[0x00, 0xC4)` (includes 0x44) | +| 0x18C | `[0xC8, 0x18C)`: the layout table as stored (XORed) and the FF padding after it | +| last 4 bytes | the whole file before the trailer, then XOR `(file length & 0xFF) ^ 0x56` | + +Why the trailer resisted: the stream and seed were the obvious ones all along, but the result +is XORed with a byte that depends on the file length, so every "constant seed" search failed. +What cracked it: compute, per file, the seed that a from-offset-0 CRC would need (run the CRC +backwards from the trailer). Files of equal length needed the same seed even with different +headers -> the header is in the stream as-is and only the length matters; then +`trailer ^ CRC(file)` turned out to be a single byte, `len & 0xFF ^ 0x56`, on all eight lengths +seen (low bytes D8 E0 E4 E8 EC F0 00 C0). **OPEN:** whether 0x56 is model-specific (only +NX4832T035 measured), and whether more than the low length byte takes part. + +The file length is always a multiple of 4 (the page section is padded with zeros). + +Other dead ends, for the record: seeds from header fields; header checksum fields zeroed/0xFF in +every combination; the layout table de-obfuscated before checksumming; prefixes built from +length/layout words; runs of 00/FF bytes before the file. diff --git a/src/nxhmi/tft.py b/src/nxhmi/tft.py index c8c24e6..1ada377 100644 --- a/src/nxhmi/tft.py +++ b/src/nxhmi/tft.py @@ -3,17 +3,17 @@ Only what is verified is implemented here: * the obfuscated layout table at 0xC8..0x113 is XOR 0xEBAB2932 per 32-bit word; -* the trailer (last 4 bytes, LE) is a word-per-byte CRC whose stream ends at the trailer -- so - for edits that keep the file length, the trailer can be updated by linearity - (:func:`patch_trailer`). Building a .tft from scratch is not possible yet: the trailer's start - offset / seed are unknown, and every .tft embeds Nextion's own firmware files, which this - project does not and must not distribute. +* the three header checksums (:func:`header_checksums`) and the trailer (:func:`trailer`) are + word-per-byte CRCs; :func:`seal` recomputes all four. + +Every .tft embeds Nextion's own firmware files, which this project does not and must not +distribute. """ from __future__ import annotations import struct -from .checksum import _feed_byte_as_word, _feed_word +from .checksum import _feed_byte_as_word, _feed_word, word_per_byte_crc LAYOUT_KEY = 0xEBAB2932 LAYOUT_START, LAYOUT_END = 0xC8, 0x114 @@ -34,6 +34,62 @@ def header_info(tft: bytes) -> dict: } +RES_START = 0x10000 # resource area: firmware files, pictures, fonts +OFF_RES_SIZE, OFF_RES_CRC, OFF_HEAD1_CRC, OFF_HEAD2_CRC = 0x40, 0x44, 0xC4, 0x18C + + +def header_checksums(tft: bytes) -> dict: + """The three header checksums as they should be (all word-per-byte CRCs, seed 0xFFFFFFFF): + + * 0x44 -- over the resource area ``[0x10000, 0x10000 + u32@0x40)``; + * 0xC4 -- over header bytes ``[0x00, 0xC4)`` (so it includes the value at 0x44); + * 0x18C -- over ``[0xC8, 0x18C)``, the layout table as stored (still XORed) plus its padding. + """ + res_size = struct.unpack_from(" bool: + return all(struct.unpack_from(" bytes: + """Return ``tft`` with the three header checksums recomputed (the trailer is left alone).""" + out = bytearray(tft) + for o, v in header_checksums(tft).items(): + struct.pack_into(" int: + """The file checksum that belongs in the last 4 bytes (LE) of ``tft`` (whose last 4 bytes are + ignored): the word-per-byte CRC (seed 0xFFFFFFFF) of everything before the trailer, XORed with + ``(file length & 0xFF) ^ 0x56``. Measured on files of eight different lengths, one display model + (NX4832T035); whether 0x56 depends on the model is not known.""" + n = len(tft) + return word_per_byte_crc(tft[:n - 4]) ^ (n & 0xFF) ^ TRAILER_XOR + + +def verify_trailer(tft: bytes) -> bool: + return struct.unpack_from(" bytes: + """Recompute every checksum of a .tft image: the three header checksums, then the trailer.""" + out = fix_header(tft) + return out[:-4] + struct.pack(" int: for _ in range(n): c = _feed_word(c, 0) diff --git a/tests/test_tft_cli.py b/tests/test_tft_cli.py index 5f431e0..fe0d966 100644 --- a/tests/test_tft_cli.py +++ b/tests/test_tft_cli.py @@ -23,6 +23,21 @@ def fake_tft(payload: bytes) -> bytes: return body + struct.pack(" bytes: + """A synthetic image with all four checksums computed by the independent bitwise CRC.""" + head = bytearray(b"\xff" * tft.RES_START) + head[:0x200] = bytes(0x200) + struct.pack_into("