aboutsummaryrefslogtreecommitdiff
path: root/analysis/nss_m0.py
diff options
context:
space:
mode:
Diffstat (limited to 'analysis/nss_m0.py')
-rw-r--r--analysis/nss_m0.py141
1 files changed, 141 insertions, 0 deletions
diff --git a/analysis/nss_m0.py b/analysis/nss_m0.py
new file mode 100644
index 0000000..2ce6006
--- /dev/null
+++ b/analysis/nss_m0.py
@@ -0,0 +1,141 @@
+#!/usr/bin/env python3
+"""NSS firmware M0: container / layout analysis.
+
+Compares the stock CKB01 blobs (11.3-12-R) with the public 11.4.0.5-5-R blobs.
+Read-only w.r.t. the vendor rootfs.
+"""
+import hashlib
+import os
+import re
+import struct
+import sys
+
+STOCK = "/ROM/ckb01/work/vroot/lib/firmware"
+PUB = os.path.expanduser("~/ckb01/firmware/nss-11.4.0.5-5")
+OUT = os.path.expanduser("~/ckb01/analysis")
+
+CASES = [
+ ("stock core0", os.path.join(STOCK, "qca-nss0-retail.bin"), 0x40000000),
+ ("stock core1", os.path.join(STOCK, "qca-nss1-retail.bin"), 0x40800000),
+ ("pub core0", os.path.join(PUB, "retail_router0.bin"), 0x40000000),
+ ("pub core1", os.path.join(PUB, "retail_router1.bin"), 0x40800000),
+]
+
+
+def sha(b):
+ return hashlib.sha256(b).hexdigest()
+
+
+def zero_runs(b, minimum=4096):
+ return [(m.start(), m.end() - m.start()) for m in re.finditer(b"\x00{%d,}" % minimum, b)]
+
+
+def strings_by_block(b, block=65536):
+ out = []
+ for off in range(0, len(b), block):
+ chunk = b[off:off + block]
+ n = len(re.findall(rb"[ -~]{8,}", chunk))
+ out.append((off, n))
+ return out
+
+
+def analyse(path, base):
+ b = open(path, "rb").read()
+ le = struct.unpack("<16I", b[:64])
+ be = struct.unpack(">16I", b[:64])
+ n = len(b) // 4
+ words = struct.unpack("<%dI" % n, b[:n * 4])
+ ptr = [w for w in words if base <= w < base + len(b)]
+ ptr_high = [w for w in words if 0x40000000 <= w < 0x41000000]
+ ver = re.search(rb"Version: [^\x00]*", b)
+ return {
+ "path": path,
+ "size": len(b),
+ "sha": sha(b),
+ "le": le,
+ "be": be,
+ "ptr_in_image": len(ptr),
+ "ptr_40": len(ptr_high),
+ "ver": ver.group().decode("latin-1") if ver else None,
+ "ver_off": ver.start() if ver else None,
+ "zeros": zero_runs(b),
+ "blocks": strings_by_block(b),
+ "b": b,
+ }
+
+
+def main():
+ os.makedirs(OUT, exist_ok=True)
+ rows = []
+ for name, path, base in CASES:
+ if not os.path.exists(path):
+ print("MISSING", path, file=sys.stderr)
+ continue
+ rows.append((name, base, analyse(path, base)))
+
+ md = []
+ md.append("# NSS firmware M0 report (container / layout)\n")
+ md.append("生成: nss_m0.py / 入力は読み取りのみ\n")
+ md.append("## 1. 同一性\n")
+ md.append("| sample | size | sha256 |")
+ md.append("|---|---|---|")
+ for name, base, r in rows:
+ md.append("| %s | %d | `%s` |" % (name, r["size"], r["sha"]))
+
+ md.append("\n## 2. バージョン文字列\n")
+ md.append("| sample | offset | string |")
+ md.append("|---|---|---|")
+ for name, base, r in rows:
+ md.append("| %s | %s | `%s` |" % (name, hex(r["ver_off"]) if r["ver_off"] is not None else "-", r["ver"] or "-"))
+
+ md.append("\n## 3. ヘッダ先頭16ワード(LE / BE)\n")
+ for name, base, r in rows:
+ md.append("### %s (base 0x%08x)\n" % (name, base))
+ md.append("```")
+ md.append("idx LE32 BE32 note")
+ for i in range(16):
+ note = ""
+ if base <= r["le"][i] < base + r["size"]:
+ note = "addr in image (+0x%x)" % (r["le"][i] - base)
+ md.append("%02d %08x %08x %s" % (i, r["le"][i], r["be"][i], note))
+ md.append("```\n")
+
+ md.append("## 4. アドレス参照の統計\n")
+ md.append("| sample | words pointing into own image (LE) | words in 0x40000000-0x40ffffff (LE) |")
+ md.append("|---|---|---|")
+ for name, base, r in rows:
+ md.append("| %s | %d | %d |" % (name, r["ptr_in_image"], r["ptr_40"]))
+
+ md.append("\n## 5. ゼロ領域(4KB以上)\n")
+ for name, base, r in rows:
+ z = ", ".join("0x%x(+%d)" % (o, n) for o, n in r["zeros"][:8])
+ md.append("- %s: %s" % (name, z or "なし"))
+
+ md.append("\n## 6. 文字列密度(64KBブロック、8文字以上)\n")
+ for name, base, r in rows:
+ md.append("### %s\n" % name)
+ md.append("```")
+ for off, n in r["blocks"]:
+ if n:
+ md.append("0x%06x %3d" % (off, n))
+ md.append("```\n")
+
+ md.append("## 7. 差分メモ(stock 11.3-12 vs public 11.4.0.5-5)\n")
+ if len(rows) >= 4:
+ s0 = rows[0][2]
+ p0 = rows[2][2]
+ md.append("- core0 サイズ差: %d バイト(stock %d / public %d)" % (p0["size"] - s0["size"], s0["size"], p0["size"]))
+ md.append("- core1 サイズ差: %d バイト" % (rows[3][2]["size"] - rows[1][2]["size"]))
+ md.append("- 先頭16ワードの一致数(core0): %d / 16" %
+ sum(1 for i in range(16) if s0["le"][i] == p0["le"][i]))
+ md.append("- バージョン: stock `%s` / public `%s`" % (s0["ver"], p0["ver"]))
+ md.append("- ゼロ領域の位置: stock %s / public %s" % (s0["zeros"][:2], p0["zeros"][:2]))
+
+ path = os.path.join(OUT, "nss-m0.md")
+ open(path, "w", encoding="utf-8").write("\n".join(md) + "\n")
+ print("wrote", path)
+ print("\n".join(md[:40]))
+
+
+if __name__ == "__main__":
+ main()