67 lines
2.8 KiB
Python
67 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Move every Rust pin and doc digest of a re-sealed PTX from its old body to its new one.
|
|
|
|
repin.py OLD_DIR NEW_DIR NAME [NAME ...] OLD_DIR/NAME.ptx = the sealed file before, NEW_DIR/NAME.ptx = after
|
|
|
|
The pin of a sealed file is the SHA-256 of its body (the file without its terminal NUL, or the whole file when it has none), written as
|
|
a decimal byte array in Rust (`sha: [..]`, `Some((len, [..]))` with len = body + 1 for the NUL the loader appends) and as lowercase hex in
|
|
the seal-plan JSON. Run from the repository root; prints what it changed and fails if a pin of the old body is not found anywhere
|
|
(the assets without a Rust pin say so explicitly: the MTP tail/checks files are guarded by the UNSEALED marker only).
|
|
"""
|
|
import glob
|
|
import hashlib
|
|
import re
|
|
import sys
|
|
|
|
NO_RUST_PIN = {'mtp_original_tail', 'mtp_original_full_checks'}
|
|
|
|
|
|
def body(path):
|
|
b = open(path, 'rb').read()
|
|
return b.rstrip(b'\0') if b.endswith(b'\0') else b
|
|
|
|
|
|
def arr(digest, sep=', '):
|
|
return sep.join(str(x) for x in digest)
|
|
|
|
|
|
def main(old_dir, new_dir, names):
|
|
rs = {f: open(f, encoding='utf-8', newline='').read() for f in glob.glob('crates/**/*.rs', recursive=True)}
|
|
docs = {f: open(f, encoding='utf-8', newline='').read() for f in ['crates/cuda-driver/seal-plans/cuda-seal-plan-additions-window.json']}
|
|
changed = set()
|
|
for name in names:
|
|
ob, nb = body(f'{old_dir}/{name}.ptx'), body(f'{new_dir}/{name}.ptx')
|
|
od, nd = hashlib.sha256(ob).digest(), hashlib.sha256(nb).digest()
|
|
found = 0
|
|
for f, s in rs.items():
|
|
if f.endswith('sm86_table.rs'):
|
|
continue # regenerated by sm86_assets.py build
|
|
for sep in (', ', ','):
|
|
if arr(od, sep) in s:
|
|
# `(len, [..])` pins carry body + 1
|
|
pat = re.compile(r'\((\d+), \[' + re.escape(arr(od, sep)) + r'\]\)')
|
|
s = pat.sub(lambda m: f'({len(nb) + 1}, [{arr(nd, sep)}])', s)
|
|
s = s.replace(arr(od, sep), arr(nd, sep))
|
|
rs[f] = s
|
|
changed.add(f)
|
|
found += 1
|
|
for f, s in docs.items():
|
|
if od.hex() in s:
|
|
docs[f] = s.replace(od.hex(), nd.hex())
|
|
changed.add(f)
|
|
found += 1
|
|
print(f'{name}: {od.hex()[:12]} -> {nd.hex()[:12]}, {found} place(s)')
|
|
if found == 0 and name not in NO_RUST_PIN:
|
|
raise SystemExit(f'{name}: no pin of the old body found')
|
|
for f in changed:
|
|
text = rs.get(f, docs.get(f))
|
|
open(f, 'w', encoding='utf-8', newline='').write(text)
|
|
print('rewrote', ', '.join(sorted(changed)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) < 4:
|
|
print(__doc__)
|
|
sys.exit(2)
|
|
main(sys.argv[1], sys.argv[2], sys.argv[3:])
|