"""Rewrite an exefs's ZBIC-compressed NSOs (flags bit 7, Switch firmware 22.0.0+) as plain uncompressed NSOs, in place, so a64-kit's nso.py (LZ4 or none) and everything after it reads them unchanged. python -I nx2/unpack.py The segment hashes at 0xa0 are of the decompressed data, so they stay valid; they are checked here. NSOs with LZ4 or no compression are left as they are. """ import hashlib import os import struct import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # (python -I leaves the script folder off) import zbic # noqa: E402 ZBIC_FLAG = 1 << 7 NAMES = ["rtld", "main"] + ["subsdk%d" % i for i in range(10)] + ["sdk"] def unpack_nso(path): raw = open(path, "rb").read() if raw[:4] != b"NSO0": return None flags = struct.unpack_from(" 1 else None if not exefs: raise SystemExit(__doc__) for n in NAMES: p = os.path.join(exefs, n) if os.path.exists(p): r = unpack_nso(p) print("%-8s %s" % (n, {None: "not an NSO", False: "not ZBIC, kept", True: "ZBIC -> plain"}[r])) if __name__ == "__main__": main()