tft: all four checksums solved (three header CRCs, trailer for any length)

0x44 = CRC of the resource area, 0xC4 = CRC of header [0,0xC4), 0x18C = CRC of
[0xC8,0x18C); trailer = CRC of the whole file ^ (len & 0xFF) ^ 0x56. tft.seal()
recomputes all of them. Verified against 24 Editor-made files of 8 lengths.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Fable 5.2 committed 2026-11-16 16:54:59 +01:00
1 parent 6b2835334a
commit 7337e693a5
6 files changed
+124 -22

No files matched your search

+62 -6
View File
@@ -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("<I", tft, OFF_RES_SIZE)[0]
res = word_per_byte_crc(tft[RES_START:RES_START + res_size])
head1 = bytearray(tft[:OFF_HEAD1_CRC])
struct.pack_into("<I", head1, OFF_RES_CRC, res)
return {
OFF_RES_CRC: res,
OFF_HEAD1_CRC: word_per_byte_crc(bytes(head1)),
OFF_HEAD2_CRC: word_per_byte_crc(tft[LAYOUT_START:OFF_HEAD2_CRC]),
}
def verify_header(tft: bytes) -> bool:
return all(struct.unpack_from("<I", tft, o)[0] == v for o, v in header_checksums(tft).items())
def fix_header(tft: bytes) -> 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("<I", out, o, v)
return bytes(out)
TRAILER_XOR = 0x56
def trailer(tft: bytes) -> 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("<I", tft, len(tft) - 4)[0] == trailer(tft)
def seal(tft: bytes) -> bytes:
"""Recompute every checksum of a .tft image: the three header checksums, then the trailer."""
out = fix_header(tft)
return out[:-4] + struct.pack("<I", trailer(out))
def _zero_words(n: int, c: int) -> int:
for _ in range(n):
c = _feed_word(c, 0)