#!/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 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('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("