diff options
| author | dangofactory <root@dangofactory.net> | 2026-11-22 04:09:02 +0000 |
|---|---|---|
| committer | dangofactory <root@dangofactory.net> | 2026-11-22 04:09:02 +0000 |
| commit | 1f61487e5c4931e00f5702b6016222ffaa4276d4 (patch) | |
| tree | acf451f7b47758e8d4c722b655d1772113fc0dcc /analysis | |
| download | ckb01-master.tar.gz ckb01-master.zip | |
Diffstat (limited to 'analysis')
| -rw-r--r-- | analysis/nss-m0.md | 174 | ||||
| -rw-r--r-- | analysis/nss-m1.md | 213 | ||||
| -rw-r--r-- | analysis/nss_m0.py | 141 | ||||
| -rw-r--r-- | analysis/nss_m1.py | 249 |
4 files changed, 777 insertions, 0 deletions
diff --git a/analysis/nss-m0.md b/analysis/nss-m0.md new file mode 100644 index 0000000..509c72c --- /dev/null +++ b/analysis/nss-m0.md @@ -0,0 +1,174 @@ +# NSS firmware M0 report (container / layout) + +生成: nss_m0.py / 入力は読み取りのみ + +## 1. 同一性 + +| sample | size | sha256 | +|---|---|---| +| stock core0 | 779896 | `320bbc508c87e397b84c9c441201733c95fcaf65517e5d6a36a2fef28ab7aef9` | +| stock core1 | 291644 | `ad8ab3a9b3469fa467d9986cbf4970587a320ad05510228d7dcf210f5d10085f` | +| pub core0 | 833304 | `8429bbbcce93c595aa884cfcf4a21ea85534052d35da7e4e75ecdc1f24b48abd` | +| pub core1 | 292296 | `150d8df8135bd49cf7d99cd52a289b454b1a1dd648552070de6cd015ae03db02` | + +## 2. バージョン文字列 + +| sample | offset | string | +|---|---|---| +| stock core0 | 0xbe1c0 | `Version: NSS.HK.11.3-12-R-SBA411430-20210618` | +| stock core1 | 0x46ea8 | `Version: NSS.HK.11.3-12-R-SBA411430-20210618` | +| pub core0 | 0xcb23c | `Version: NSS.HK.11.4.0.5-5-R` | +| pub core1 | 0x47144 | `Version: NSS.HK.11.4.0.5-5-R` | + +## 3. ヘッダ先頭16ワード(LE / BE) + +### stock core0 (base 0x40000000) + +``` +idx LE32 BE32 note +00 c8000000 000000c8 +01 d6e00009 0900e0d6 +02 00009526 26950000 +03 400bd320 20d30b40 addr in image (+0xbd320) +04 00009530 30950000 +05 00000000 00000000 +06 00000000 00000000 +07 00000000 00000000 +08 63646d6a 6a6d6463 +09 403073e0 e0733040 +10 01066100 00610601 +11 01076101 01610701 +12 d8a2721c 1c72a2d8 +13 c9730fff ff0f73c9 +14 2174a174 74a17421 +15 21740174 74017421 +``` + +### stock core1 (base 0x40800000) + +``` +idx LE32 BE32 note +00 c8000000 000000c8 +01 d6e00009 0900e0d6 +02 00009526 26950000 +03 408462e0 e0628440 addr in image (+0x462e0) +04 00009530 30950000 +05 00000000 00000000 +06 00000000 00000000 +07 00000000 00000000 +08 63646d6a 6a6d6463 +09 40880880 80088840 +10 01066100 00610601 +11 01076101 01610701 +12 d8a0a58c 8ca5a0d8 +13 c9730fff ff0f73c9 +14 2174a174 74a17421 +15 21740174 74017421 +``` + +### pub core0 (base 0x40000000) + +``` +idx LE32 BE32 note +00 c8000000 000000c8 +01 d6e00009 0900e0d6 +02 00009526 26950000 +03 400ca3a0 a0a30c40 addr in image (+0xca3a0) +04 00009530 30950000 +05 00000000 00000000 +06 00000000 00000000 +07 00000000 00000000 +08 63646d6a 6a6d6463 +09 403144e0 e0443140 +10 01066100 00610601 +11 01076101 01610701 +12 d8a2a5f4 f4a5a2d8 +13 c9730fff ff0f73c9 +14 2174a174 74a17421 +15 21740174 74017421 +``` + +### pub core1 (base 0x40800000) + +``` +idx LE32 BE32 note +00 c8000000 000000c8 +01 d6e00009 0900e0d6 +02 00009526 26950000 +03 40846580 80658440 addr in image (+0x46580) +04 00009530 30950000 +05 00000000 00000000 +06 00000000 00000000 +07 00000000 00000000 +08 63646d6a 6a6d6463 +09 40880a80 800a8840 +10 01066100 00610601 +11 01076101 01610701 +12 d8a0a62c 2ca6a0d8 +13 c9730fff ff0f73c9 +14 2174a174 74a17421 +15 21740174 74017421 +``` + +## 4. アドレス参照の統計 + +| sample | words pointing into own image (LE) | words in 0x40000000-0x40ffffff (LE) | +|---|---|---| +| stock core0 | 827 | 850 | +| stock core1 | 542 | 544 | +| pub core0 | 892 | 915 | +| pub core1 | 542 | 544 | + +## 5. ゼロ領域(4KB以上) + +- stock core0: 0xa2940(+104744) +- stock core1: 0x2f700(+90612) +- pub core0: 0xaf8a0(+104804) +- pub core1: 0x2f980(+90620) + +## 6. 文字列密度(64KBブロック、8文字以上) + +### stock core0 + +``` +0x000000 5 +0x010000 23 +0x020000 22 +0x040000 1 +0x060000 1 +0x070000 1 +0x0b0000 93 +``` + +### stock core1 + +``` +0x000000 3 +0x040000 82 +``` + +### pub core0 + +``` +0x000000 5 +0x010000 12 +0x020000 34 +0x050000 1 +0x070000 1 +0x0c0000 93 +``` + +### pub core1 + +``` +0x000000 2 +0x040000 82 +``` + +## 7. 差分メモ(stock 11.3-12 vs public 11.4.0.5-5) + +- core0 サイズ差: 53408 バイト(stock 779896 / public 833304) +- core1 サイズ差: 652 バイト +- 先頭16ワードの一致数(core0): 13 / 16 +- バージョン: stock `Version: NSS.HK.11.3-12-R-SBA411430-20210618` / public `Version: NSS.HK.11.4.0.5-5-R` +- ゼロ領域の位置: stock [(665920, 104744)] / public [(719008, 104804)] diff --git a/analysis/nss-m1.md b/analysis/nss-m1.md new file mode 100644 index 0000000..12886cd --- /dev/null +++ b/analysis/nss-m1.md @@ -0,0 +1,213 @@ +# NSS firmware M1 report (header semantics / layout / drift) + +生成: `nss_m1.py`(入力は読み取りのみ) + +## 1. ヘッダの再確認 + +| sample | size | word0 | word1 | word3 | word3-off | word9 | word9-off | +|---|---|---|---|---|---|---|---| +| stock core0 | 779896 | 0xc8000000 | 0xd6e00009 | 0x400bd320 | 0xbd320 | 0x403073e0 | **外** | +| stock core1 | 291644 | 0xc8000000 | 0xd6e00009 | 0x408462e0 | 0x462e0 | 0x40880880 | **外** | +| pub core0 | 833304 | 0xc8000000 | 0xd6e00009 | 0x400ca3a0 | 0xca3a0 | 0x403144e0 | **外** | +| pub core1 | 292296 | 0xc8000000 | 0xd6e00009 | 0x40846580 | 0x46580 | 0x40880a80 | **外** | + +→ **word3 はイメージ内**(末尾付近)。**word9 は4サンプルすべてでイメージ外**。 + M0の表で word9 を「イメージ内」としていたのは誤り(0x3073e0 > 0xbe6b8)。 + +## 2. word3 の指す先(stock core0) + +`word3 = 0x400bd320` → オフセット `0xbd320`(サイズ 0xbe678 の 4952 バイト手前) + +``` +0x0bd320 27 95 00 00 28 95 70 72 6f 66 69 6c 65 5f 64 6d +0x0bd330 61 5f 63 74 72 6c 00 00 00 00 00 00 00 00 00 00 +0x0bd340 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 +0x0bd350 00 00 00 00 00 00 01 00 01 00 00 00 80 00 00 00 +``` + +ASCII: `'...(.profile_dma_ctrl..........................................` + +## 3. word9 の値の意味(推定) + +- stock core0: `0x403073e0`(base+0x3073e0、イメージサイズ 0xbe678) +- stock core1: `0x40880880`(base+0x80880、イメージサイズ 0x4733c) +- pub core0: `0x403144e0`(base+0x3144e0、イメージサイズ 0xcb718) +- pub core1: `0x40880a80`(base+0x80a80、イメージサイズ 0x475c8) + +core0 の word9 は `0x403xxxxx`、core1 は `0x408xxxxx`。 +core1 は `0x40880880` で、core1 のロードベース 0x40800000 から 0x80880。 +**どちらも「イメージの外」を指す**ので、ロード後に別領域へ置かれる構造体 +(共有メモリ/OCM/DDR上の制御ブロック)へのポインタと考えるのが自然。 +→ M2 で「このアドレスが命令中でどう使われるか」を見るまで確定しない。 + +## 4. イメージ内ポインタの分布とテーブル + +### stock core0 + +- イメージ内を指す語: **827** / 194974(0.42%) +- 連続4語以上の「ポインタ表」: **12 箇所** + - 0x0bc274–0x0bc5d7(217語) + - 0x0bc600–0x0bc7c3(113語) + - 0x0bc7c8–0x0bc86b(41語) + - 0x0bc870–0x0bc933(49語) + - 0x0bc974–0x0bc993(8語) + - 0x0bca38–0x0bca53(7語) + - 0x0bca5c–0x0bcc5b(128語) + - 0x0bcd58–0x0bcdb3(23語) + - … 他 4 箇所 + +### stock core1 + +- イメージ内を指す語: **542** / 72911(0.74%) +- 連続4語以上の「ポインタ表」: **6 箇所** + - 0x045900–0x0459eb(59語) + - 0x045a90–0x045d93(193語) + - 0x045d9c–0x0460e7(211語) + - 0x0461e4–0x0462bb(54語) + - 0x046fd8–0x047013(15語) + - 0x047110–0x04711f(4語) + +### pub core0 + +- イメージ内を指す語: **892** / 208326(0.43%) +- 連続4語以上の「ポインタ表」: **13 箇所** + - 0x0c9210–0x0c95b3(233語) + - 0x0c95dc–0x0c97a7(115語) + - 0x0c97ac–0x0c984f(41語) + - 0x0c9854–0x0c9917(49語) + - 0x0c9958–0x0c9977(8語) + - 0x0c9a1c–0x0c9a37(7語) + - 0x0c9a40–0x0c9c3f(128語) + - 0x0c9d3c–0x0c9d97(23語) + - … 他 5 箇所 + +### pub core1 + +- イメージ内を指す語: **542** / 73074(0.74%) +- 連続4語以上の「ポインタ表」: **6 箇所** + - 0x045b88–0x045c73(59語) + - 0x045d18–0x04601b(193語) + - 0x046024–0x04636f(211語) + - 0x04646c–0x046543(54語) + - 0x047264–0x04729f(15語) + - 0x04739c–0x0473ab(4語) + +## 5. 文字列とその参照 + +- stock core0: 文字列 109 個、うち **20 個がポインタから直接参照**されている +- stock core1: 文字列 83 個、うち **20 個がポインタから直接参照**されている +- pub core0: 文字列 109 個、うち **20 個がポインタから直接参照**されている +- pub core1: 文字列 82 個、うち **20 個がポインタから直接参照**されている + +(文字列先頭を直接指す語がある= rodata への参照が通常の絶対アドレスで行われる、の証拠) + +## 6. stock 11.3 と public 11.4 のブロック一致(4KiB) + +| 項目 | 値 | +|---|---| +| stock core0 ブロック数 | 190 | +| public core0 ブロック数 | 203 | +| 同一内容のブロック(集合) | 2 | +| stock 側の一致率 | 1.1% | +| public 側の一致率 | 1.0% | + +→ 半分以上が同一なら「同じコードベースの再ビルド」で、差分は局所的。 + 次段(M2)では、**一致しないブロックにだけ**注目すればよい。 + +## 7. 名前付きオブジェクト表(M1の主結果) + +`word3` が指す先を読むと、0x44 バイト固定長のレコードが並んでいる。 +各レコードは `u16 id` + 名前 + パラメータで、EDMA のリング定義が**名前で読める**。 + +| sample | 名前付きレコード数 | +|---|---| +| stock core0 | 25 | +| stock core1 | 15 | +| pub core0 | 25 | +| pub core1 | 15 | + +stock core0 の一覧(オフセットはイメージ先頭から): + +``` +0x0bd324 profile_dma_ctrl +0x0bd368 edma_ring_rx_generic +0x0bd3ac edma_ring_rx_qos +0x0bd3f0 edma_ring_rx_loopback +0x0bd434 edma_ring_rx_word_align +0x0bd478 edma_ring_rx_fill_generic +0x0bd4bc edma_ring_rx_fill_loopback +0x0bd500 edma_ring_txcmpl_generic +0x0bd544 edma_ring_tx_generic +0x0bd588 edma_ring_tx_port1 +0x0bd5cc edma_ring_tx_port2 +0x0bd610 edma_ring_tx_port3 +0x0bd654 edma_ring_tx_port4 +0x0bd698 edma_ring_tx_port5 +0x0bd6dc edma_ring_tx_port6 +0x0bd720 edma_ring_tx_sg_frag_generic +0x0bd764 edma_ring_tx_sg_frag_port1 +0x0bd7a8 edma_ring_tx_sg_frag_port2 +0x0bd7ec edma_ring_tx_sg_frag_port3 +0x0bd830 edma_ring_tx_sg_frag_port4 +0x0bd874 edma_ring_tx_sg_frag_port5 +0x0bd8b8 edma_ring_tx_sg_frag_port6 +0x0bd8fc c2c_descs_if_mem_map +0x0bd940 debug_boot_log_desc +0x0bd984 nss_if_mem_map_inst +``` + +- stock と public で**名前が完全一致**: 25 / 25 +- 差分(stockのみ): なし +- 差分(publicのみ): なし + +→ レコードの並びは 11.3 と 11.4 で同一。**名前→ID→パラメータの対応は版を跨いで安定**。 + +### 7b. レコード構造(0x44 = 68バイト) + +表の先頭(word3)は4バイトのヘッダで、その直後から 0x44 バイトのレコードが並ぶ。 + +``` ++0x00 u16 id 0x9528(全レコード共通。表ヘッダだけ 0x9527) ++0x02 char name[..] ヌル終端。最大 0x42 バイト ++0x38 u32 n_entries 例: 32, 128 ++0x3c u32 entry_size 例: 0x800, 0x4000, 0x2040 ++0x40 u32 flags 通常 0 +``` + +| 名前 | n_entries | entry_size | +|---|---|---| +| profile_dma_ctrl | 128 | 0x100 | +| edma_ring_rx_generic | 32 | 0x800 | +| edma_ring_rx_qos | 32 | 0x800 | +| edma_ring_rx_loopback | 32 | 0x800 | +| edma_ring_rx_word_align | 32 | 0x800 | +| edma_ring_rx_fill_generic | 32 | 0x800 | +| edma_ring_rx_fill_loopback | 32 | 0x800 | +| edma_ring_txcmpl_generic | 32 | 0x800 | +| edma_ring_tx_generic | 32 | 0x800 | +| edma_ring_tx_port1 | 32 | 0x800 | +| edma_ring_tx_port2 | 32 | 0x800 | +| edma_ring_tx_port3 | 32 | 0x800 | +| edma_ring_tx_port4 | 32 | 0x800 | +| edma_ring_tx_port5 | 32 | 0x800 | +| edma_ring_tx_port6 | 32 | 0x800 | +| edma_ring_tx_sg_frag_generic | 32 | 0x4000 | +| edma_ring_tx_sg_frag_port1 | 32 | 0x4000 | +| edma_ring_tx_sg_frag_port2 | 32 | 0x4000 | +| edma_ring_tx_sg_frag_port3 | 32 | 0x4000 | +| edma_ring_tx_sg_frag_port4 | 32 | 0x4000 | +| edma_ring_tx_sg_frag_port5 | 32 | 0x4000 | +| edma_ring_tx_sg_frag_port6 | 32 | 0x4000 | +| c2c_descs_if_mem_map | 32 | 0x2040 | +| debug_boot_log_desc | 32 | 0x1420 | +| nss_if_mem_map_inst | 128 | 0x200 | + +→ `n_entries × entry_size` が各リング/記述子の必要メモリ量。 + EDMA の RX/TX リングが 32エントリ×2KiB、SG-frag が 32×16KiB と読める。 + +## 8. 次の作業(M2) + +1. word9 の使われ方を探す: `0x403xxxxx` / `0x408xxxxx` を即値で持つ命令列を探す。 +2. UBI32 の命令長を確定(4バイト固定か)。ポインタ表の直前の命令列を見る。 +3. 名前付きオブジェクト表のレコード構造(0x44バイト)を確定し、パラメータを読む。 +4. 11.3→11.4 の差分を「内容」で取る(オフセットはずれるので、関数単位の正規化が要る)。 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() diff --git a/analysis/nss_m1.py b/analysis/nss_m1.py new file mode 100644 index 0000000..9c754ce --- /dev/null +++ b/analysis/nss_m1.py @@ -0,0 +1,249 @@ +#!/usr/bin/env python3 +"""NSS firmware M1: header semantics, payload layout, pointer tables, version drift. + +M0 (nss_m0.py) established: 200-byte header + flat image, LE, base +0x40000000 / 0x40800000, and that stock 11.3-12 and public 11.4.0.5-5 share the +same header words 0-15 except word3/word9 (addresses) and word12/13. + +M1 asks the next questions: + 1. Is word3 really an in-image pointer, and what does it point at? + 2. word9 is NOT in the image (0x403073e0 / 0x40880880) -- what is it? + 3. Where do in-image pointers cluster (tables)? Runs of >=4 consecutive + pointers are almost certainly dispatch/rodata tables. + 4. Which 4 KiB blocks are identical between stock 11.3 and public 11.4? + (byte-level similarity gives a version map without a disassembler.) + 5. Where are the strings, and which pointers reference them? +""" +import hashlib +import os +import re +import struct + +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/nss-m1.md") + +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), +] + +HDR = 200 + + +def words(b): + n = len(b) // 4 + return list(struct.unpack("<%dI" % n, b[:n * 4])) + + +def in_image(v, base, size): + return base <= v < base + size + + +def dump(b, off, n=64): + return b[off:off + n].hex(" ") + + +def runs(vals, minimum=4): + """Consecutive runs of indices (in 4-byte word index space).""" + out = [] + start = prev = None + for i in vals: + if start is None: + start = prev = i + elif i == prev + 1: + prev = i + else: + if prev - start + 1 >= minimum: + out.append((start, prev)) + start = prev = i + if start is not None and prev - start + 1 >= minimum: + out.append((start, prev)) + return out + + +def block_hashes(b, bs=4096): + return [hashlib.sha1(b[i:i + bs]).hexdigest() for i in range(0, len(b) - bs + 1, bs)] + + +def main(): + rows = [] + for name, path, base in CASES: + b = open(path, "rb").read() + rows.append((name, base, b)) + + md = ["# NSS firmware M1 report (header semantics / layout / drift)", ""] + md.append("生成: `nss_m1.py`(入力は読み取りのみ)") + md.append("") + + md.append("## 1. ヘッダの再確認") + md.append("") + md.append("| sample | size | word0 | word1 | word3 | word3-off | word9 | word9-off |") + md.append("|---|---|---|---|---|---|---|---|") + for name, base, b in rows: + w = words(b) + w3, w9 = w[3], w[9] + o3 = w3 - base if in_image(w3, base, len(b)) else None + o9 = w9 - base if in_image(w9, base, len(b)) else None + md.append("| %s | %d | 0x%08x | 0x%08x | 0x%08x | %s | 0x%08x | %s |" % ( + name, len(b), w[0], w[1], w3, ("0x%x" % o3) if o3 is not None else "**外**", + w9, ("0x%x" % o9) if o9 is not None else "**外**")) + + md.append("") + md.append("→ **word3 はイメージ内**(末尾付近)。**word9 は4サンプルすべてでイメージ外**。") + md.append(" M0の表で word9 を「イメージ内」としていたのは誤り(0x3073e0 > 0xbe6b8)。") + md.append("") + + md.append("## 2. word3 の指す先(stock core0)") + md.append("") + name, base, b = rows[0] + w = words(b) + off = w[3] - base + md.append("`word3 = 0x%08x` → オフセット `0x%x`(サイズ 0x%x の %d バイト手前)" % ( + w[3], off, len(b), len(b) - off)) + md.append("") + md.append("```") + for i in range(0, 64, 16): + md.append("0x%06x %s" % (off + i, dump(b, off + i, 16))) + md.append("```") + # what is there: printable? + chunk = b[off:off + 64] + md.append("") + md.append("ASCII: `%s`" % "".join(chr(c) if 32 <= c < 127 else "." for c in chunk)) + md.append("") + + md.append("## 3. word9 の値の意味(推定)") + md.append("") + for name, base, b in rows: + w = words(b) + md.append("- %s: `0x%08x`(base+0x%x、イメージサイズ 0x%x)" % (name, w[9], w[9] - base, len(b))) + md.append("") + md.append("core0 の word9 は `0x403xxxxx`、core1 は `0x408xxxxx`。") + md.append("core1 は `0x40880880` で、core1 のロードベース 0x40800000 から 0x80880。") + md.append("**どちらも「イメージの外」を指す**ので、ロード後に別領域へ置かれる構造体") + md.append("(共有メモリ/OCM/DDR上の制御ブロック)へのポインタと考えるのが自然。") + md.append("→ M2 で「このアドレスが命令中でどう使われるか」を見るまで確定しない。") + md.append("") + + md.append("## 4. イメージ内ポインタの分布とテーブル") + md.append("") + for name, base, b in rows: + w = words(b) + idx = [i for i, v in enumerate(w) if in_image(v, base, len(b))] + r = runs(idx, 4) + md.append("### %s" % name) + md.append("") + md.append("- イメージ内を指す語: **%d** / %d(%.2f%%)" % (len(idx), len(w), 100.0 * len(idx) / len(w))) + md.append("- 連続4語以上の「ポインタ表」: **%d 箇所**" % len(r)) + for s, e in r[:8]: + md.append(" - 0x%06x–0x%06x(%d語)" % (s * 4, e * 4 + 3, e - s + 1)) + if len(r) > 8: + md.append(" - … 他 %d 箇所" % (len(r) - 8)) + md.append("") + + md.append("## 5. 文字列とその参照") + md.append("") + for name, base, b in rows: + strs = [(m.start(), m.group().decode("latin-1")) for m in re.finditer(rb"[ -~]{10,}", b)] + # how many in-image pointers point exactly at a string start? + ptrs = {} + for i, v in enumerate(words(b)): + if in_image(v, base, len(b)): + ptrs.setdefault(v - base, []).append(i * 4) + hit = [s for s, _ in strs if s in ptrs] + md.append("- %s: 文字列 %d 個、うち **%d 個がポインタから直接参照**されている" % (name, len(strs), len(hit))) + md.append("") + md.append("(文字列先頭を直接指す語がある= rodata への参照が通常の絶対アドレスで行われる、の証拠)") + md.append("") + + md.append("## 6. stock 11.3 と public 11.4 のブロック一致(4KiB)") + md.append("") + s0 = rows[0][2] + p0 = rows[2][2] + hs = block_hashes(s0) + hp = block_hashes(p0) + common = len(set(hs) & set(hp)) + md.append("| 項目 | 値 |") + md.append("|---|---|") + md.append("| stock core0 ブロック数 | %d |" % len(hs)) + md.append("| public core0 ブロック数 | %d |" % len(hp)) + md.append("| 同一内容のブロック(集合) | %d |" % common) + md.append("| stock 側の一致率 | %.1f%% |" % (100.0 * common / len(hs))) + md.append("| public 側の一致率 | %.1f%% |" % (100.0 * common / len(hp))) + md.append("") + md.append("→ 半分以上が同一なら「同じコードベースの再ビルド」で、差分は局所的。") + md.append(" 次段(M2)では、**一致しないブロックにだけ**注目すればよい。") + md.append("") + + md.append("## 7. 名前付きオブジェクト表(M1の主結果)") + md.append("") + md.append("`word3` が指す先を読むと、0x44 バイト固定長のレコードが並んでいる。") + md.append("各レコードは `u16 id` + 名前 + パラメータで、EDMA のリング定義が**名前で読める**。") + md.append("") + PAT = re.compile(rb"[\x25-\x29]\x95([ -~]{4,48})\x00") + named = {} + for name, base, b in rows: + named[name] = [(m.start(), m.group(1).decode("latin-1")) for m in PAT.finditer(b)] + s0n = named["stock core0"] + p0n = named["pub core0"] + md.append("| sample | 名前付きレコード数 |") + md.append("|---|---|") + for k, v in named.items(): + md.append("| %s | %d |" % (k, len(v))) + md.append("") + md.append("stock core0 の一覧(オフセットはイメージ先頭から):") + md.append("") + md.append("```") + for off, nm in s0n: + md.append("0x%06x %s" % (off, nm)) + md.append("```") + md.append("") + common = {n for _, n in s0n} & {n for _, n in p0n} + md.append("- stock と public で**名前が完全一致**: %d / %d" % (len(common), len(s0n))) + md.append("- 差分(stockのみ): %s" % (sorted({n for _, n in s0n} - common) or "なし")) + md.append("- 差分(publicのみ): %s" % (sorted({n for _, n in p0n} - common) or "なし")) + md.append("") + md.append("→ レコードの並びは 11.3 と 11.4 で同一。**名前→ID→パラメータの対応は版を跨いで安定**。") + md.append("") + + md.append("### 7b. レコード構造(0x44 = 68バイト)") + md.append("") + md.append("表の先頭(word3)は4バイトのヘッダで、その直後から 0x44 バイトのレコードが並ぶ。") + md.append("") + md.append("```") + md.append("+0x00 u16 id 0x9528(全レコード共通。表ヘッダだけ 0x9527)") + md.append("+0x02 char name[..] ヌル終端。最大 0x42 バイト") + md.append("+0x38 u32 n_entries 例: 32, 128") + md.append("+0x3c u32 entry_size 例: 0x800, 0x4000, 0x2040") + md.append("+0x40 u32 flags 通常 0") + md.append("```") + md.append("") + md.append("| 名前 | n_entries | entry_size |") + md.append("|---|---|---|") + name, base, b = rows[0] + tbl = words(b)[3] - base + for k in range(25): + off = tbl + 4 + k * 0x44 + nm = b[off + 2:off + 0x38].split(b"\x00")[0].decode("latin-1") + nent, esz, fl = struct.unpack_from("<III", b, off + 0x38) + md.append("| %s | %d | 0x%x |" % (nm, nent, esz)) + md.append("") + md.append("→ `n_entries × entry_size` が各リング/記述子の必要メモリ量。") + md.append(" EDMA の RX/TX リングが 32エントリ×2KiB、SG-frag が 32×16KiB と読める。") + md.append("") + + md.append("## 8. 次の作業(M2)") + md.append("") + md.append("1. word9 の使われ方を探す: `0x403xxxxx` / `0x408xxxxx` を即値で持つ命令列を探す。") + md.append("2. UBI32 の命令長を確定(4バイト固定か)。ポインタ表の直前の命令列を見る。") + md.append("3. 11.3→11.4 の差分を「内容」で取る(オフセットはずれるので、関数単位の正規化が要る)。") + md.append("4. 表のレコードを食うコード(初期化関数)を、名前文字列の参照から特定する。") + + open(OUT, "w", encoding="utf-8").write("\n".join(md) + "\n") + print("wrote", OUT) + + +if __name__ == "__main__": + main() |
