interplane.Get Omega
Read in your language

omega-gpu-driver

aien-dev/omega-gpu-driver

Brought by drakestapleton

branch main Branches Tags History

omega-gpu-driver / tests / arena_large.py

93 lines · 4280 bytes Raw Blame History
1"""Check offline GB20B composition against the pinned vendor bytes and radix layout."""
2import argparse
3import hashlib
4import struct
5import subprocess
6from pathlib import Path
7
8PAGE = 4096
9BASE = 1048576
10FW = "32cb7cf3e77f97e92f6e92135939ccb3ba382748144569c0dfccf3338d43d963"
11PARTS = {
12 "bl.img": "6da8510c912ccd5a8b73f9a5cce9e0f4ea657aa430431d2f8e4874834aeadb94",
13 "fmc.img": "b92f83c1250019af11162d9ee471b8fb74081ea598cf6added0bee7a3663e2be",
14 "fmc.hash": "4a5854c785fa7a9ea4aaabb52f6c9838242f81506659ff413da0f4b8ac1f40bf",
15 "fmc.pubkey": "54076b108ad3da65e09f675d88efd553340a0c0653cf13372f4e30dd6ab17e7c",
16 "fmc.sig": "6c5fbbf096efff9d0f47d4cc70096898f059579154464e6c3b972d2e606f1334",
17}
18
19
20def sha(data):
21 return hashlib.sha256(data).hexdigest()
22
23
24def span(path, digest):
25 body = path.read_bytes()
26 if sha(body) != digest:
27 raise ValueError(f"wrong firmware input {path.name}")
28 return body
29
30
31def check(exe, fw, inputs):
32 firmware = span(fw, FW)
33 image = firmware[64:64 + 74969088]
34 signature = firmware[74969199:74969199 + 4096]
35 boot, fmc, fmc_hash, pubkey, signed = [span(inputs / k, v) for k, v in PARTS.items()]
36 source = b"".join([image, signature, boot, fmc, fmc_hash, pubkey, signed])
37 def run(mode, data):
38 return subprocess.run([exe], input=bytes([mode]) + data, capture_output=True, timeout=120)
39 small = run(1, source)
40 assert (small.returncode, small.stdout) == (3, b""), "old capacity must refuse before output"
41 for bad in (source[:-1], source + b"x"):
42 refused = run(0, bad)
43 assert (refused.returncode, refused.stdout) == (2, b""), "incomplete or surplus input admitted"
44 regular = run(0, source)
45 assert regular.returncode == 0, regular.returncode
46 dirty = run(2, source)
47 assert dirty.returncode == 0 and regular.stdout == dirty.stdout, "uncleared reused arena"
48 aligned = run(3, source)
49 assert aligned.returncode == 0 and regular.stdout == aligned.stdout, "aligned and unaligned inputs differ"
50 out = memoryview(regular.stdout)
51 arena, cot = out[:-868], out[-868:]
52 def word(data, offset):
53 return struct.unpack_from("<Q", data, offset)[0]
54 def at(data, offset):
55 address = word(data, offset)
56 assert BASE <= address < BASE + len(arena)
57 return address - BASE
58 radix = at(arena, 16)
59 assert word(arena, 24) == len(image)
60 # Three levels; check every intermediate and leaf pointer, including the final partial page.
61 pages = (len(image) + PAGE - 1) // PAGE
62 levels = [(pages + 512**i - 1) // 512**i for i in (3, 2, 1, 0)]
63 current = radix
64 for count, children in zip(levels, levels[1:]):
65 child = current + PAGE * count
66 for i in range(children):
67 assert at(arena, current + i * 8) == child + i * PAGE, (current, i)
68 assert not any(arena[current + children * 8:child]), "nonzero radix padding"
69 current = child
70 assert arena[current:current + len(image)] == image, "firmware bytes changed"
71 assert not any(arena[current + len(image):current + pages * PAGE])
72 for data, ref, offset in [(arena, boot, 32), (arena, signature, 72), (cot, fmc, 12)]:
73 start = at(data, offset)
74 assert arena[start:start + len(ref)] == ref
75 assert word(arena, 40) == len(boot) and word(arena, 80) == len(signature)
76 assert (word(arena, 48), word(arena, 56), word(arena, 64)) == (61440, 2560, 0)
77 assert struct.unpack_from("<I", cot)[0] == (3 << 30)
78 assert struct.unpack_from("<HH", cot, 8) == (2, 860)
79 assert cot[44:92] == fmc_hash and cot[92:476] == pubkey and cot[476:860] == signed
80 assert at(arena, at(cot, 860) + 16) == 0
81 print(f"PASS {len(arena)} staged bytes, {len(image)} firmware bytes, {pages} data pages, {len(cot)} COT bytes")
82 print(f"arena_sha256 {sha(arena)}\ncot_sha256 {sha(cot)}")
83 print("PASS undersized arena, short/trailing input, dirty high pages and aligned/unaligned copies")
84
85
86if __name__ == "__main__":
87 if not __debug__:
88 raise SystemExit("assertions required")
89 ap = argparse.ArgumentParser(description=__doc__)
90 for name in ("exe", "firmware", "package"):
91 ap.add_argument(name, type=Path)
92 args = ap.parse_args()
93 check(args.exe.resolve(), args.firmware, args.package)