Import the base tree (MIT, see LICENSE)

This commit is contained in:
Valiantsin Dzerakh
2026-11-04 22:16:41 +03:00
committed by chrg6
commit 0478c13e45
734 changed files with 233953 additions and 0 deletions

21
tools/vendor/macnativegames/LICENSE vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 MacNativeGames contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

14
tools/vendor/macnativegames/README.md vendored Normal file
View File

@@ -0,0 +1,14 @@
# MacNativeGames extraction scripts
Unmodified copies from https://github.com/ly05010419/MacNativeGames at `b75be8cfa704df3701e86130c8ced268e79d6b11`, MIT (see `LICENSE`).
| File | Upstream path |
|---|---|
| `extract_exefs.py` | `games/<game>/scripts/extract_exefs.py` |
| `extract_romfs.py` | `games/<game>/scripts/extract_romfs.py` |
| `extract_update_romfs.py` | `games/<game>/scripts/extract_update_romfs.py` |
| `extract_shared_fonts.py` | `games/<game>/scripts/extract_shared_fonts.py` |
`tools/data/expected-v*.json` come from `games/<game>/assets/` at the same commit.
Keep these files byte-identical to upstream so they can be updated by copying. Windows-specific behaviour lives in `tools/extract.py` and `tools/extract_fonts.py`.

View File

@@ -0,0 +1,250 @@
#!/usr/bin/env python3
"""Read an XCI or NSP in place; extract and verify its base or explicitly selected update Program ExeFS.
Supports NCA3, key-area or common-ticket AES-CTR, and hierarchical SHA-256 PFS0.
Keys stay in memory. No RomFS extraction, executable launch, or network calls.
"""
import argparse
import hashlib
import json
import struct
import io
from pathlib import Path
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
def require(condition, message):
if not condition:
raise ValueError(message)
def decrypt(key, mode, data):
ctx = Cipher(algorithms.AES(key), mode).decryptor()
return ctx.update(data) + ctx.finalize()
def read_at(f, offset, length):
require(0 <= offset and 0 <= length <= 512 * 1024 * 1024, 'Invalid/big read')
f.seek(offset)
data = f.read(length)
require(len(data) == length, 'Truncated input')
return data
def hfs(f, offset, bound):
return partition_entries(f, offset, bound, b'HFS0', 64)
def partition_entries(f, offset, bound, magic=b'PFS0', stride=24):
"""Read a bounded PFS0/HFS0 table without loading its file payloads."""
require(bound >= 16, 'Truncated partition header')
hdr = read_at(f, offset, 16)
require(hdr[:4] == magic, f'Expected {magic.decode()}')
count, strings_size = struct.unpack_from('<II', hdr, 4)
require(count <= 4096 and strings_size <= 1024 * 1024, 'Invalid partition table')
size = 16 + count * stride + strings_size
require(size <= bound, 'Partition table out of bounds')
raw = read_at(f, offset, size)
strings = raw[16 + count * stride:]
entries = []
names = set()
for i in range(count):
off, length, nameoff = struct.unpack_from('<QQI', raw, 16 + i * stride)
require(nameoff < len(strings), 'Invalid partition filename offset')
end = strings.find(b'\0', nameoff)
require(end >= 0, 'Unterminated partition filename')
name = strings[nameoff:end].decode('utf-8')
require(name not in ('', '.', '..') and '/' not in name and '\\' not in name, 'Unsafe partition filename')
require(name not in names, 'Duplicate partition filename')
names.add(name)
require(size + off + length <= bound, 'Partition entry out of bounds')
entries.append((name, offset + size + off, length))
end = offset + size
for _, start, length in sorted(entries, key=lambda e: e[1]):
require(start >= end, 'Overlapping partition entries')
end = start + length
return entries
def container_entries(f, size):
if read_at(f, 0, 4) == b'PFS0':
return 'nsp', partition_entries(f, 0, size)
require(size >= 0x138 and read_at(f, 0x100, 4) == b'HEAD', 'Expected an XCI or NSP (PFS0) game file')
root = struct.unpack('<Q', read_at(f, 0x130, 8))[0]
partitions = hfs(f, root, size - root)
secure = [e for e in partitions if e[0] == 'secure']
require(len(secure) == 1, 'Expected one XCI secure partition')
return 'xci', hfs(f, secure[0][1], secure[0][2])
def load_keys(path):
keys = {}
for number, line in enumerate(path.read_text().splitlines(), 1):
line = line.split('#', 1)[0].strip()
if '=' in line:
name, value = line.split('=', 1)
try:
keys[name.strip().lower()] = bytes.fromhex(value.strip())
except ValueError:
raise ValueError(f'Invalid key encoding at line {number}') from None
return keys
def common_tickets(f, entries):
"""Keep encrypted title keys in memory only, as nxnative's NSP importer does.
Ticket offsets below are relative to the signed body; accept the standard RSA/ECDSA layouts.
A personalized ticket cannot provide a common title key and is never treated as one.
"""
tickets = {}
bodies = {0x10000: 0x240, 0x10001: 0x140, 0x10002: 0x80,
0x10003: 0x240, 0x10004: 0x140, 0x10005: 0x80}
for name, offset, size in entries:
if not name.lower().endswith('.tik'):
continue
require(size >= 4, 'Truncated NSP ticket')
# Switch ticket signature type is little-endian: RSA2048/SHA256 = 04 00 01 00.
sig = struct.unpack('<I', read_at(f, offset, 4))[0]
require(sig in bodies, 'Unsupported NSP ticket signature type')
body = bodies[sig]
require(size >= body + 0x180, 'Truncated NSP ticket body')
data = read_at(f, offset + body, 0x180)
require(data[0x141] == 0, 'Personalized NSP tickets are unsupported; a common ticket is required')
rights, key = data[0x160:0x170], data[0x40:0x50]
require(rights not in tickets or tickets[rights] == key, 'Conflicting NSP tickets')
tickets[rights] = key
return tickets
def read_nca_header(f, offset, size, keys):
require(size >= 0xc00, 'Truncated NCA header')
require('header_key' in keys, 'Missing header_key in prod.keys')
raw = read_at(f, offset, 0xc00)
hdr = b''.join(decrypt(keys['header_key'], modes.XTS(i.to_bytes(16, 'big')),
raw[i * 512:(i + 1) * 512]) for i in range(6))
require(hdr[0x200:0x204] == b'NCA3', 'Expected NCA3 (check prod.keys and the game file)')
require(struct.unpack_from('<Q', hdr, 0x208)[0] == size, 'NCA size does not match container entry')
for i in range(4):
start, end = struct.unpack_from('<II', hdr, 0x240 + i * 16)
require((start == end == 0) or 0xc00 <= start * 512 < end * 512 <= size, 'NCA section out of bounds')
return hdr
def nca_ctr_key(hdr, keys, tickets):
generation = max(max(hdr[0x206], hdr[0x220]) - 1, 0)
rights = hdr[0x230:0x240]
if rights != bytes(16):
require(rights in tickets, 'Missing common ticket for rights-managed NCA in the NSP')
keyname = f'titlekek_{generation:02x}'
require(keyname in keys, f'Missing {keyname} in prod.keys')
return decrypt(keys[keyname], modes.ECB(), tickets[rights])
require(hdr[0x207] < 3, 'Unsupported NCA key area')
area = ('application', 'ocean', 'system')[hdr[0x207]]
keyname = f'key_area_key_{area}_{generation:02x}'
require(keyname in keys, f'Missing {keyname} in prod.keys')
return decrypt(keys[keyname], modes.ECB(), hdr[0x300:0x340])[32:48]
def nca_inventory(f, entries, keys):
records = []
for name, offset, length in entries:
if not name.lower().endswith('.nca'):
continue
hdr = read_nca_header(f, offset, length, keys)
record = dict(name=name, nca_offset=offset, size=length, content_type=hdr[0x205],
title_id=f'{struct.unpack_from("<Q", hdr, 0x210)[0]:016X}')
records.append((record, hdr))
return records
def program_nca(records, update=False):
if update:
programs = [(r, h) for r, h in records if r["content_type"] == 0]
require(len(programs) == 1, "Expected one update Program NCA; combined packages are unsupported")
record, hdr = programs[0]
require(record["title_id"] == "0100F2C0115B6000", "Update belongs to another game")
require(any(hdr[0x400 + i*512 + 4] == 4 for i in range(4)), "Expected a BKTR update, not a base game")
require(all(r["title_id"] in ("0100F2C0115B6000", "0100F2C0115B6800") for r, _ in records), "Mixed-title update package")
return record, hdr
# Update Program NCAs can retain the base title ID; their Meta NCA carries the update ID.
metadata = [r for r, _ in records if r['content_type'] == 1]
require(len(metadata) <= 1 and all(r['title_id'] == '0100F2C0115B6000' for r in metadata),
'Expected base game metadata; updates, DLC and combined packages are unsupported')
programs = [(r, h) for r, h in records if r['content_type'] == 0]
require(len(programs) == 1, 'Expected one base Program NCA; updates, DLC and combined packages are unsupported')
record, hdr = programs[0]
require(record['title_id'] == '0100F2C0115B6000',
'Expected Ocarina of Time base game 1.0.0; updates, DLC and other games are unsupported')
return record, hdr
def record_offset(record):
# Read manifests produced before NSP support too.
return record['nca_offset'] if 'nca_offset' in record else record['xci_offset']
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('game', type=Path, help='base game 1.0.0 (.xci or .nsp)')
parser.add_argument('--keys', required=True, type=Path)
parser.add_argument('--update', action='store_true', help='extract update code; caller must verify supported version hashes')
parser.add_argument('--output', required=True, type=Path)
args = parser.parse_args()
require(not args.output.exists(), 'Output already exists; choose a new directory')
keys = load_keys(args.keys)
with args.game.open('rb') as f:
kind, entries = container_entries(f, args.game.stat().st_size)
tickets = common_tickets(f, entries)
records = nca_inventory(f, entries, keys)
record, hdr = program_nca(records, update=args.update)
ctr_key = nca_ctr_key(hdr, keys, tickets)
for i in range(4):
start, end = struct.unpack_from('<II', hdr, 0x240 + 16 * i)
if not start:
continue
fs = hdr[0x400 + 512 * i:0x600 + 512 * i]
require(hashlib.sha256(fs).digest() == hdr[0x280 + 32 * i:0x2A0 + 32 * i], 'FS header hash mismatch')
if fs[2:4] != b'\x01\x02':
continue
require(fs[4] == 3, 'Only AES-CTR ExeFS is supported')
blocksize, levels = struct.unpack_from('<II', fs, 0x28)
hashoff, hashsize, pfsoff, pfssize = struct.unpack_from('<QQQQ', fs, 0x30)
secstart, secsize = start * 512, (end - start) * 512
require(levels == 2 and blocksize > 0 and pfssize <= 512 * 1024 * 1024, 'Unsupported ExeFS hash layout')
def section_read(relative, size):
require(relative >= 0 and relative + size <= secsize, 'Section read out of bounds')
absolute = secstart + relative
aligned = absolute & ~15
iv = fs[0x140:0x148][::-1] + (aligned // 16).to_bytes(8, 'big')
data = read_at(f, record_offset(record) + aligned, size + absolute - aligned)
return decrypt(ctr_key, modes.CTR(iv), data)[absolute - aligned:]
hashes = section_read(hashoff, hashsize)
require(hashlib.sha256(hashes).digest() == fs[8:40], 'ExeFS hash-table mismatch')
pfs = section_read(pfsoff, pfssize)
for index, pos in enumerate(range(0, pfssize, blocksize)):
expected = hashes[index * 32:(index + 1) * 32]
require(hashlib.sha256(pfs[pos:pos + blocksize]).digest() == expected,
f'ExeFS block hash mismatch: {index}')
files = [(name, pfs[off:off + size]) for name, off, size in
partition_entries(io.BytesIO(pfs), 0, len(pfs))]
require(any(name == 'main.npdm' for name, _ in files), 'PFS0 is not ExeFS')
args.output.mkdir(parents=True)
extracted = []
for name, data in files:
(args.output / name).write_bytes(data)
extracted.append({'name': name, 'size': len(data), 'sha256': hashlib.sha256(data).hexdigest()})
manifest = {'source': str(args.game.resolve()), 'source_size': args.game.stat().st_size,
'source_mtime_ns': args.game.stat().st_mtime_ns, 'container': kind,
'nca_inventory': [r for r, _ in records], 'program': record, 'section_index': i,
'integrity': 'FS header, ExeFS hash table, and every ExeFS data block verified; RSA signatures not checked',
'files': extracted}
(args.output.parent / 'extraction-manifest.json').write_text(json.dumps(manifest, indent=2) + '\n')
print(json.dumps({'integrity': manifest['integrity'], 'files': extracted}, indent=2))
return
raise ValueError('No ExeFS section found')
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,83 @@
#!/usr/bin/env python3
"""Verify all six IVFC levels, stream a local RomFS image, and index its files."""
import argparse
import hashlib
import json
import struct
from pathlib import Path
from cryptography.hazmat.primitives.ciphers import modes
from extract_exefs import (decrypt, read_at, require, load_keys, container_entries, common_tickets,
nca_inventory, program_nca, nca_ctr_key, record_offset)
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--keys',type=Path,required=True)
parser.add_argument('--manifest',type=Path,default=Path('extracted/v0/extraction-manifest.json'))
parser.add_argument('--output',type=Path,default=Path('extracted/v0/romfs'))
args=parser.parse_args(); require(not args.output.exists(),'Output already exists')
manifest=json.loads(args.manifest.read_text());record=manifest['program'];keys=load_keys(args.keys)
source=Path(manifest['source']);require(source.stat().st_size==manifest['source_size'],'Source size changed')
with source.open('rb') as f:
_,entries=container_entries(f,source.stat().st_size)
actual,hdr=program_nca(nca_inventory(f,entries,keys))
require(actual['name']==record['name'] and actual['size']==record['size'] and
record_offset(actual)==record_offset(record),'Source Program NCA changed; extract ExeFS again')
ctr_key=nca_ctr_key(hdr,keys,common_tickets(f,entries))
for i in range(4):
start,end=struct.unpack_from('<II',hdr,0x240+i*16)
if not start:continue
fs=hdr[0x400+512*i:0x600+512*i]
require(hashlib.sha256(fs).digest()==hdr[0x280+32*i:0x2a0+32*i],'FS header hash')
if fs[2:5]!=bytes([0,3,3]):continue
require(fs[8:12]==b'IVFC' and struct.unpack_from('<III',fs,12)==(0x20000,32,7),'Unsupported IVFC layout')
levels=[struct.unpack_from('<QQII',fs,0x18+j*24) for j in range(6)]
def read(relative,size):
require(0<=relative<=relative+size<=(end-start)*512,'Section bounds')
absolute=start*512+relative;aligned=absolute&~15;skip=absolute-aligned
iv=fs[0x140:0x148][::-1]+(aligned//16).to_bytes(8,'big')
return decrypt(ctr_key,modes.CTR(iv),read_at(f,record_offset(record)+aligned,size+skip))[skip:]
args.output.mkdir(parents=True);image_path=args.output/'romfs.bin';digest=hashlib.sha256();counts=[]
parent=fs[0xc8:0xe8]
with image_path.open('wb') as image:
for level,(offset,size,power,reserved) in enumerate(levels):
require(12<=power<=24 and reserved==0,'IVFC block size');block=1<<power
require(len(parent)>=(size+block-1)//block*32,'IVFC parent size')
captured=bytearray() if level<5 else None
index=0
for pos in range(0,size,8*1024*1024):
chunk=read(offset+pos,min(8*1024*1024,size-pos))
for j in range(0,len(chunk),block):
payload=chunk[j:j+block]
require(hashlib.sha256(payload+bytes(block-len(payload))).digest()==parent[index*32:index*32+32],f'IVFC level {level} block {index} failed')
index+=1
if captured is not None:captured.extend(chunk)
else:image.write(chunk);digest.update(chunk)
counts.append(index)
if captured is not None:parent=bytes(captured)
print(f'IVFC level {level}: {index} blocks verified',flush=True)
files=index_romfs(image_path)
report=dict(status='pass',source_size=source.stat().st_size,program=record['name'],section=i,ivfc_blocks=counts,romfs_size=levels[-1][1],romfs_sha256=digest.hexdigest(),files=files,integrity='FS header and all six IVFC levels verified; RSA signature not checked')
(args.output/'index.json').write_text(json.dumps(report,indent=2)+'\n')
print(json.dumps(dict(status='pass',file_count=len(files),size=levels[-1][1],sha256=digest.hexdigest())),flush=True);return
raise ValueError('No supported RomFS found')
def index_romfs(path):
with path.open('rb') as f:
h=struct.unpack('<10Q',read_at(f,0,80));require(h[0]==80,'RomFS header');length=path.stat().st_size
for off,size in [(h[1],h[2]),(h[3],h[4]),(h[5],h[6]),(h[7],h[8])]:require(off+size<=length,'RomFS table bounds')
dirs=read_at(f,h[3],h[4]);entries=read_at(f,h[7],h[8]);files=[];seen_dirs=set();seen_files=set();paths=set()
def name(table,off,n):
require(off+n<=len(table),'Name bounds');s=table[off:off+n].decode('utf-8');require(s not in ['.','..'] and '/' not in s and '\\' not in s and '\0' not in s,'Unsafe path');return s
todo=[(0,'')]
while todo:
off,parent=todo.pop();require(off not in seen_dirs and off+24<=len(dirs),'Invalid/cyclic directory');seen_dirs.add(off)
p,sibling,child,file,hash_next,n=struct.unpack_from('<6I',dirs,off);part=name(dirs,off+24,n);prefix=parent+part+'/' if part else parent
if sibling!=0xffffffff:todo.append((sibling,parent))
if child!=0xffffffff:todo.append((child,prefix))
while file!=0xffffffff:
require(file not in seen_files and file+32<=len(entries),'Invalid/cyclic file');seen_files.add(file)
p,next_file,data,size,hash_next,n=struct.unpack_from('<IIQQII',entries,file);part=name(entries,file+32,n);require(part!='','Empty file name');filename=prefix+part
require(filename not in paths and h[9]+data+size<=length,'Duplicate/out-of-bounds file');paths.add(filename)
files.append(dict(path=filename,offset=h[9]+data,size=size));file=next_file
return sorted(files,key=lambda item:item['path'])
if __name__=='__main__':main()

View File

@@ -0,0 +1,202 @@
#!/usr/bin/env python3
"""Extract the system shared fonts (BFTTF) from firmware data NCAs, verifying the NCA FS header,
all six IVFC levels and each BFTTF header. Fonts stay in the BFTTF form that hardware places in
shared memory; only local copies are written, never printed.
Sources: --xci <game card image> reads the data NCAs from the card's system update partition (a game card carries the
system update it needs, shared fonts included); --firmware reads an unpacked NCA directory. The older --inventory
mode lists the NCAs of an installed firmware."""
import argparse
import hashlib
import json
import struct
from pathlib import Path
from cryptography.hazmat.primitives.ciphers import modes
from extract_exefs import decrypt, read_at, require, load_keys, hfs, nca_ctr_key
from extract_romfs import index_romfs
FIRMWARE = Path.home() / 'Library/Application Support/Ryujinx/bis/system/Contents/registered'
# nn::pl::SharedFontType -> (system data title, BFTTF file name)
FONTS = [
(0, 'standard', '0100000000000811', 'nintendo_udsg-r_std_003.bfttf'),
(1, 'chinese-simplified', '0100000000000814', 'nintendo_udsg-r_org_zh-cn_003.bfttf'),
(2, 'chinese-simplified-extension', '0100000000000814', 'nintendo_udsg-r_ext_zh-cn_003.bfttf'),
(3, 'chinese-traditional', '0100000000000813', 'nintendo_udjxh-db_zh-tw_003.bfttf'),
(4, 'korean', '0100000000000812', 'nintendo_udsg-r_ko_003.bfttf'),
(5, 'nintendo-extension', '0100000000000810', 'nintendo_ext_003.bfttf'),
]
BFTTF_DECODED_MAGIC = 0x7f9a0218
class Slice:
"""A file seen from a base offset: an NCA inside a game card image reads like a standalone NCA file."""
def __init__(self, path, base=0):
self.f, self.base = path.open('rb'), base
def seek(self, offset, whence=0):
return self.f.seek(self.base + offset if whence == 0 else offset, whence) - self.base
def read(self, size=-1):
return self.f.read(size)
def __enter__(self):
return self
def __exit__(self, *exc):
self.f.close()
def hfs0_entries(f, offset):
"""(name, absolute offset, size) of each file in the HFS0 partition at offset."""
length = f.seek(0, 2)
return hfs(f, offset, length - offset)
def nca_header(f, keys):
raw = read_at(f, 0, 0xc00)
return b''.join(decrypt(keys['header_key'], modes.XTS(i.to_bytes(16, 'big')), raw[i * 512:(i + 1) * 512]) for i in range(6))
def xci_data_ncas(xci, keys):
"""title ID -> offset of each data NCA in the card's system update partition."""
found = {}
with xci.open('rb') as f:
require(read_at(f, 0x100, 4) == b'HEAD', 'Not a game card image (XCI)')
root = struct.unpack('<Q', read_at(f, 0x130, 8))[0]
update = [e for e in hfs0_entries(f, root) if e[0] == 'update']
require(update, 'The game card image has no system update partition')
for name, offset, size in hfs0_entries(f, update[0][1]):
if not name.endswith('.nca') or name.endswith('.cnmt.nca'):
continue
with Slice(xci, offset) as nca:
hdr = nca_header(nca, keys)
if hdr[0x200:0x204] == b'NCA3' and hdr[0x205] == 4: # content type 4: data
found[f'{struct.unpack_from("<Q", hdr, 0x210)[0]:016x}'] = offset
return found
def nca_romfs(source, keys, image_path, tickets=None):
"""Write the verified RomFS image of a data NCA (a Path, or a Slice of a game card image) to image_path."""
with (source if isinstance(source, Slice) else Slice(source)) as f:
raw = read_at(f, 0, 0xc00)
hdr = b''.join(decrypt(keys['header_key'], modes.XTS(i.to_bytes(16, 'big')), raw[i * 512:(i + 1) * 512]) for i in range(6))
require(hdr[0x200:0x204] == b'NCA3', f'Unsupported NCA at {f.base:#x}')
ctr_key = nca_ctr_key(hdr, keys, tickets or {})
for i in range(4):
start, end = struct.unpack_from('<II', hdr, 0x240 + i * 16)
if not start:
continue
fs = hdr[0x400 + 512 * i:0x600 + 512 * i]
require(hashlib.sha256(fs).digest() == hdr[0x280 + 32 * i:0x2a0 + 32 * i], 'FS header hash')
if fs[2:5] != bytes([0, 3, 3]):
continue
require(fs[8:12] == b'IVFC' and struct.unpack_from('<III', fs, 12) == (0x20000, 32, 7), 'Unsupported IVFC layout')
levels = [struct.unpack_from('<QQII', fs, 0x18 + j * 24) for j in range(6)]
def read(relative, size):
require(0 <= relative <= relative + size <= (end - start) * 512, 'Section bounds')
absolute = start * 512 + relative
aligned = absolute & ~15
skip = absolute - aligned
iv = fs[0x140:0x148][::-1] + (aligned // 16).to_bytes(8, 'big')
return decrypt(ctr_key, modes.CTR(iv), read_at(f, aligned, size + skip))[skip:]
parent = fs[0xc8:0xe8]
image = bytearray()
for level, (offset, size, power, reserved) in enumerate(levels):
require(12 <= power <= 24 and reserved == 0, 'IVFC block size')
block = 1 << power
data = read(offset, size)
for index, j in enumerate(range(0, len(data), block)):
payload = data[j:j + block]
require(hashlib.sha256(payload + bytes(block - len(payload))).digest() == parent[index * 32:index * 32 + 32], f'IVFC level {level} block {index}')
parent = data
if level == 5:
image = data
image_path.write_bytes(image)
return
raise ValueError(f'No RomFS in the NCA at {f.base:#x}')
def bfttf_size(data):
"""BFTTF: big-endian words XORed with key = word0 ^ 0x7f9a0218; word1 = font size; payload follows."""
magic, size = struct.unpack_from('>II', data, 0)
key = magic ^ BFTTF_DECODED_MAGIC
decoded = size ^ key
require(decoded + 8 == len(data), 'BFTTF size does not match file')
first = struct.unpack_from('>I', data, 8)[0] ^ key
require(first in (0x00010000, 0x4f54544f), 'BFTTF payload is not a TrueType/OpenType font')
return decoded
def firmware_data_ncas(folder, keys):
"""Accept an unpacked firmware directory or an installed registered/ directory."""
require(folder.is_dir(), 'Firmware must be a directory containing NCA files')
wanted = {title for _, _, title, _ in FONTS}
found = {}
for path in sorted(folder.iterdir()):
if path.is_dir():
path = path / '00'
if not path.is_file() or (path.name != '00' and path.suffix.lower() != '.nca'):
continue
with path.open('rb') as f:
hdr = nca_header(f, keys)
require(hdr[0x200:0x204] == b'NCA3', 'Invalid firmware NCA header (check prod.keys)')
title = f'{struct.unpack_from("<Q", hdr, 0x210)[0]:016x}'
if hdr[0x205] == 4 and title in wanted:
require(title not in found, f'Duplicate shared-font title {title} in firmware')
found[title] = path
require(wanted <= found.keys(), 'Firmware is missing shared-font data NCAs: ' + ', '.join(sorted(wanted - found.keys())))
return found
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--keys', type=Path, required=True)
sources = parser.add_mutually_exclusive_group()
sources.add_argument('--xci', type=Path, help='game card image: take the fonts from its system update partition')
sources.add_argument('--firmware', type=Path, help='unpacked firmware NCA directory (for NSP builds)')
parser.add_argument('--inventory', type=Path, default=Path('analysis/firmware-inventory.json'))
parser.add_argument('--output', type=Path, default=Path('extracted/firmware/shared-fonts'))
args = parser.parse_args()
require(not args.output.exists(), 'Output already exists')
keys = load_keys(args.keys)
if args.xci:
card = xci_data_ncas(args.xci, keys)
missing = sorted({t for _, _, t, _ in FONTS} - card.keys())
require(not missing, f'Shared font data missing from the card update partition: {", ".join(missing)}')
source = lambda title: Slice(args.xci, card[title])
elif args.firmware:
firmware = firmware_data_ncas(args.firmware, keys)
source = lambda title: firmware[title]
else:
rows = json.loads(args.inventory.read_text())['rows']
data_nca = {r['title_id']: r['file'] for r in rows if r['content_type'] == 4}
source = lambda title: (FIRMWARE / data_nca[title] / '00') if (FIRMWARE / data_nca[title]).is_dir() else FIRMWARE / data_nca[title]
args.output.mkdir(parents=True)
images = {}
report = []
for kind, name, title, filename in FONTS:
if title not in images:
image_path = args.output / f'{title}.romfs'
nca_romfs(source(title), keys, image_path)
images[title] = (image_path, {e['path']: e for e in index_romfs(image_path)})
image_path, entries = images[title]
require(filename in entries, f'{filename} missing in {title}')
e = entries[filename]
with image_path.open('rb') as f:
data = read_at(f, e['offset'], e['size'])
size = bfttf_size(data)
out = args.output / f'{kind}-{name}.bfttf'
out.write_bytes(data)
report.append(dict(type=kind, name=name, title_id=title, file=filename, bfttf_bytes=len(data), font_bytes=size,
sha256=hashlib.sha256(data).hexdigest()))
for image_path, _ in images.values():
image_path.unlink()
manifest = dict(status='pass', integrity='NCA FS header and six IVFC levels verified per NCA; BFTTF header decodes to an sfnt font; RSA not checked', fonts=report)
(args.output / 'manifest.json').write_text(json.dumps(manifest, indent=2) + '\n')
print(json.dumps(dict(status='pass', fonts=[(r['type'], r['font_bytes']) for r in report])))
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,141 @@
#!/usr/bin/env python3
"""Merge a base XCI/NSP and an update NSP with the pinned hactool BKTR reader.
Temporary common-ticket keys are private files, never arguments or log output.
The caller additionally verifies the resulting image against its known version hash.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
from extract_exefs import (load_keys, container_entries, common_tickets, nca_inventory,
program_nca, record_offset, require)
from extract_romfs import index_romfs
# Lockpick_RCM writes these as 17 bytes: the 16-byte source plus a trailing NUL.
# hactool exits unless the value is exactly 16 bytes, before any BKTR check.
_LOCKPICK_KEK_SOURCE = re.compile(r'(?:mariko_)?master_kek_source_[0-9a-f]{2}\Z')
def copy_range(source, offset, size, target):
with source.open('rb') as src, target.open('wb') as dst:
src.seek(offset)
while size:
data = src.read(min(size, 8 << 20))
require(bool(data), 'Truncated NCA')
dst.write(data)
size -= len(data)
def hactool_prod_keys(source, target):
"""Copy prod.keys for hactool. The caller's file is left unchanged.
hactool knows no comments: it reads the rest of a line as the value, so a trailing `# note` makes it exit. The copy
keeps only the `name = value` lines, read the way load_keys reads them (comments dropped, spaces trimmed).
A Lockpick trailing NUL on master_kek_source / mariko_master_kek_source is dropped. Any other length is rejected
without recording the key bytes.
"""
lines = []
for number, line in enumerate(Path(source).read_text().splitlines(), 1):
body = line.split('#', 1)[0]
if '=' not in body:
continue
name, value = (part.strip() for part in body.split('=', 1))
if _LOCKPICK_KEK_SOURCE.fullmatch(name.lower()) is not None:
require(re.fullmatch(r'[0-9a-fA-F]+', value) is not None, f'Invalid key encoding at line {number}')
if len(value) == 34 and value[-2:].lower() == '00':
value = value[:32]
require(len(value) == 32, f'{name.lower()} must be 16 bytes')
lines.append(f'{name} = {value}')
fd = os.open(target, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(fd, 'w') as f:
f.write('\n'.join(lines) + ('\n' if lines else ''))
def merge(base, update, keys_path, hactool, output, *, base_romfs=None):
require(not output.exists(), 'Output exists')
keys = load_keys(keys_path)
inputs = []
tickets = {}
sources = ((update, True),) if base_romfs else ((base, False), (update, True))
for source, is_update in sources:
with source.open('rb') as f:
kind, entries = container_entries(f, source.stat().st_size)
require(not is_update or kind == 'nsp', 'Update must be an NSP')
found = common_tickets(f, entries)
for rights, key in found.items():
require(rights not in tickets or tickets[rights] == key, 'Conflicting common tickets')
tickets[rights] = key
record, header = program_nca(nca_inventory(f, entries, keys), update=is_update)
rights = header[0x230:0x240]
require(rights == bytes(16) or rights in tickets, 'Missing common ticket')
inputs.append((source, record, header))
sections = [i for i in range(4) if inputs[-1][2][0x400+i*512+4] == 4]
require(len(sections) == 1, 'Expected one BKTR section')
output.parent.mkdir(parents=True, exist_ok=True)
require(shutil.disk_usage(output.parent).free > inputs[-1][1]['size'] + 18*(1 << 30),
'Need about 19 GiB free for the temporary update NCA and merged image')
if base_romfs:
require(Path(base_romfs).is_file(), 'Previous-version RomFS is missing')
with tempfile.TemporaryDirectory(prefix='update-merge-', dir=output.parent) as tmp:
tmp = Path(tmp)
keyfile = tmp/'title.keys'
fd = os.open(keyfile, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(fd, 'w') as f:
for rights, key in tickets.items():
f.write(f'{rights.hex()} = {key.hex()}\n')
source, record, _ = inputs[-1]
copy_range(source, record_offset(record), record['size'], tmp/'update.nca')
result = tmp/'result'
result.mkdir()
base_arg = '--baseromfs='+str(Path(base_romfs).resolve()) if base_romfs else '--basenca='+str(base.resolve())
environment = dict(os.environ, MNG_TITLE_KEYS=str(keyfile))
environment.pop('MNG_BASE_RANGE_OFFSET', None); environment.pop('MNG_BASE_RANGE_SIZE', None)
if not base_romfs:
environment.update(MNG_BASE_RANGE_OFFSET=str(record_offset(inputs[0][1])),
MNG_BASE_RANGE_SIZE=str(inputs[0][1]['size']))
hactool_keys = tmp/'hactool.keys'
hactool_prod_keys(keys_path, hactool_keys)
command = [str(hactool.resolve()), '-x', '-y', '--disablekeywarns', '--suppresskeys',
'-k', str(hactool_keys.resolve()), base_arg,
f'--section{sections[0]}='+str(result/'romfs.bin'), str(tmp/'update.nca')]
with (result/'merge.log').open('w') as log:
code = subprocess.run(command, env=environment,
stdout=log, stderr=subprocess.STDOUT).returncode
log = (result/'merge.log').read_text()
# hactool also verifies the NCA's fixed-key and NPDM signatures, which need Nintendo's public keys:
# without them it always prints those two lines as (FAIL). They are not data failures; the six IVFC
# levels and the image SHA-256 recorded below decide whether the merge worked.
unsigned = re.compile(r'\s*(?:Fixed-Key Signature|NPDM Signature) \(FAIL\)')
checked = '\n'.join(line for line in log.splitlines() if not unsigned.match(line))
if code or re.search(r'\(FAIL\)|\(INVALID\)|Failed to|Error:', checked):
# hactool repeats a key it cannot parse ("Key (<value>) must be 32 hex digits!"): keep no long hex in the log
(output.parent/(output.name+'-failure.log')).write_text(re.sub(r'[0-9A-Fa-f]{32,}', '<hex removed>', log))
require(code == 0 and not re.search(r'\(FAIL\)|\(INVALID\)|Failed to|Error:', checked), 'BKTR verification failed')
require(all(re.search(rf'Level {i} \(GOOD\)', log) for i in range(6)), 'Missing verified IVFC levels')
image = result/'romfs.bin'
with image.open('rb') as f:
digest = hashlib.file_digest(f, 'sha256').hexdigest()
files = index_romfs(image)
report = dict(status='pass', romfs_size=image.stat().st_size, romfs_sha256=digest,
files=files, integrity='BKTR merge; all six IVFC levels verified')
(result/'index.json').write_text(json.dumps(report, indent=2)+'\n')
result.rename(output)
print(json.dumps(dict(status='pass', files=len(files), sha256=digest)))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('base', type=Path)
parser.add_argument('update', type=Path)
parser.add_argument('--keys', required=True, type=Path)
parser.add_argument('--hactool', required=True, type=Path)
parser.add_argument('--base-romfs', type=Path, help='full decrypted base IVFC section, including hash levels (not a raw RomFS image)')
parser.add_argument('--output', required=True, type=Path)
args = parser.parse_args()
merge(args.base, args.update, args.keys, args.hactool, args.output, base_romfs=args.base_romfs)