#!/usr/bin/env python3 import argparse import collections import ctypes import ctypes.util import os import stat import struct import sys import zlib MAGIC = 0x1985 NODE_DIRENT = 0xE001 NODE_INODE = 0xE002 _lzo = None def jffs_crc(data): # Linux crc32(0, ...) is the raw CRC-32 state, without zlib's # initial/final complement convention. return (zlib.crc32(data, 0xffffffff) ^ 0xffffffff) & 0xffffffff def rtime_decompress(src, destlen): positions = [0] * 256 out = bytearray() pos = 0 while len(out) < destlen: value = src[pos] repeat = src[pos + 1] pos += 2 out.append(value) back = positions[value] positions[value] = len(out) for _ in range(repeat): out.append(out[back]) back += 1 return bytes(out[:destlen]) def decompress(kind, src, destlen): global _lzo if kind == 0: return src if kind == 1: return b"\0" * destlen if kind == 2: return rtime_decompress(src, destlen) if kind == 6: return zlib.decompress(src) if kind == 7: if _lzo is None: library = ctypes.util.find_library("lzo2") if not library: for candidate in ( "/opt/homebrew/lib/liblzo2.dylib", "/usr/local/lib/liblzo2.dylib", "liblzo2.so.2", ): if os.path.exists(candidate): library = candidate break if not library: raise RuntimeError("LZO2 library not found") _lzo = ctypes.CDLL(library) _lzo.lzo1x_decompress_safe.argtypes = [ ctypes.c_void_p, ctypes.c_size_t, ctypes.c_void_p, ctypes.POINTER(ctypes.c_size_t), ] _lzo.lzo1x_decompress_safe.restype = ctypes.c_int source = ctypes.create_string_buffer(src) target = ctypes.create_string_buffer(destlen) target_len = ctypes.c_size_t(destlen) rc = _lzo.lzo1x_decompress_safe( source, len(src), target, ctypes.byref(target_len)) if rc != 0 or target_len.value != destlen: raise ValueError(f"LZO rc={rc} size={target_len.value}, expected={destlen}") return target.raw[:destlen] raise ValueError(f"unsupported compression {kind}") def strip_oob(source, dest, page=2048, oob=64): with open(source, "rb") as src, open(dest, "wb") as dst: while True: data = src.read(page) if not data: break if len(data) != page: raise ValueError("short NAND data page") dst.write(data) if len(src.read(oob)) != oob: raise ValueError("short NAND OOB record") def scan(image): data = open(image, "rb").read() dirents = {} inodes = collections.defaultdict(list) counts = collections.Counter() bad_crc = collections.Counter() unsupported = collections.Counter() off = 0 limit = len(data) - 12 while off <= limit: magic, nodetype, totlen, hdr_crc = struct.unpack_from(" len(data): off += 4 continue if jffs_crc(data[off:off + 8]) != hdr_crc: bad_crc["header"] += 1 off += 4 continue counts[f"0x{nodetype:04x}"] += 1 if nodetype == NODE_DIRENT and totlen >= 40: fields = struct.unpack_from(" old[0]: dirents[key] = (version, ino, dtype, off) elif nodetype == NODE_INODE and totlen >= 68: vals = struct.unpack_from("= end or all(covered[start:end]): continue chunk = decompress(node["compr"], node["payload"], node["dsize"]) if len(chunk) != node["dsize"]: raise ValueError(f"decompressed {len(chunk)}, expected {node['dsize']}") for pos in range(start, end): if not covered[pos]: out[pos] = chunk[pos - node["offset"]] covered[pos] = 1 return bytes(out) def main(): ap = argparse.ArgumentParser() ap.add_argument("--source") ap.add_argument("--strip-to") ap.add_argument("--image", required=True) ap.add_argument("--list", action="store_true") ap.add_argument("--match", action="append", default=[]) ap.add_argument("--extract-dir") args = ap.parse_args() if args.source: strip_oob(args.source, args.strip_to) dirents, inodes, counts, bad_crc, unsupported = scan(args.image) entries = live_paths(dirents) print(f"image_bytes={os.path.getsize(args.image)}") print(f"node_counts={dict(counts)}") print(f"valid_dirents={len(dirents)} inode_numbers={len(inodes)} live_paths={len(entries)}") print(f"bad_crc={dict(bad_crc)} unsupported_compression={dict(unsupported)}") matches = [] needles = [x.lower().encode() for x in args.match] for path, ino, dtype, physical in sorted(entries): if args.list or any(n in path.lower() for n in needles): print(f"{ino:8d} type={dtype:2d} dirent@0x{physical:08x} {os.fsdecode(path)}") if needles and any(n in path.lower() for n in needles): matches.append((path, ino, dtype)) if args.extract_dir: os.makedirs(args.extract_dir, exist_ok=True) for path, ino, dtype in matches: if dtype not in (8, 10): continue try: content = reconstruct(inodes[ino]) except Exception as exc: print(f"ERROR {os.fsdecode(path)}: {exc}", file=sys.stderr) continue safe = path.lstrip(b"/").replace(b"/", b"__") target = os.path.join(args.extract_dir, os.fsdecode(safe)) with open(target, "wb") as f: f.write(content) latest = max(inodes[ino], key=lambda n: (n["version"], n["physical"])) print(f"EXTRACT {target} size={len(content)} mode={latest['mode']:o} latest_version={latest['version']}") if __name__ == "__main__": main()