diff --git a/docs/building.md b/docs/building.md index 8eb2a540..f68d8392 100644 --- a/docs/building.md +++ b/docs/building.md @@ -78,6 +78,9 @@ of them. .venv-py314\Scripts\python tools\extract.py ``` + `--skip-base-romfs` leaves out the base game's 17 GB RomFS image (`extracted 0 omfs`): no build step reads it, and + the update merge verifies what it takes from the base game file. Only a build of 1.0.0 itself needs it. + 4. Shader translator (no game files; again after a change to its sources, and `tools\make_release.py` stops on an older one). This publishes `build\xlate\totk_xlate.exe`: diff --git a/tests/extract_skip_base_romfs_test.py b/tests/extract_skip_base_romfs_test.py new file mode 100644 index 00000000..a839989c --- /dev/null +++ b/tests/extract_skip_base_romfs_test.py @@ -0,0 +1,86 @@ +"""tools/extract.py --skip-base-romfs: the 17 GB base RomFS is not extracted, and nothing else stops being checked. + + python tests/extract_skip_base_romfs_test.py + +The base game's ExeFS still decides that the file is TotK 1.0.0, and the update merge still verifies all six IVFC +levels of what it takes from that file, so the flag trades a check that runs before the merge for one that runs during +it, and saves the 17 GB image no tool in the repository reads. +""" +import json +import sys +import tempfile +import unittest +from pathlib import Path +from unittest import mock + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tools')) +import extract # noqa: E402 + +EXPECTED = {'version': 0, 'display_version': '1.0.0', + 'exefs_sha256': {**{name: '0' * 64 for name in extract.EXEFS_CODE}, 'main.npdm': '0' * 64}, + 'romfs': {'sha256': '1' * 64, 'size': 17_069_139_640}} + + +class SkipBaseRomfsTest(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.out = Path(self.tmp.name) / 'v0' + (self.out / 'exefs').mkdir(parents=True) + for name in extract.EXEFS_CODE: # an ExeFS that matches EXPECTED, so no extraction is needed + (self.out / 'exefs' / name).write_bytes(b'') + self.patch = mock.patch.object(extract, 'sha256', lambda path: '0' * 64) + self.patch.start() + self.addCleanup(self.patch.stop) + self.addCleanup(self.tmp.cleanup) + + def run_base(self, *, skip_romfs, romfs_present=False): + """extract_base with the two RomFS steps recorded, and the log it printed.""" + if romfs_present: + (self.out / 'romfs').mkdir() + (self.out / 'romfs' / 'index.json').write_text(json.dumps( + {'romfs_sha256': '1' * 64, 'romfs_size': 17_069_139_640, 'files': []})) + (self.out / 'romfs' / 'romfs.bin').write_bytes(b'') + calls = [] + with mock.patch.object(extract, 'extract_base_romfs', lambda *a, **k: calls.append('extract')), \ + mock.patch.object(extract, 'check_romfs', lambda *a, **k: calls.append('check')), \ + mock.patch.object(extract, 'say', lambda message: calls.append(message)): + extract.extract_base(Path('base.xci'), Path('prod.keys'), self.out, Path('logs'), EXPECTED, + skip_romfs=skip_romfs) + return calls + + def test_skipped_romfs_is_neither_extracted_nor_checked(self): + calls = self.run_base(skip_romfs=True) + self.assertNotIn('extract', calls) + self.assertNotIn('check', calls) + self.assertIn(' RomFS skipped (--skip-base-romfs)', calls) + + def test_the_exefs_check_still_runs_when_the_romfs_is_skipped(self): + """The flag drops the RomFS only: the ExeFS is what identifies the base game.""" + calls = self.run_base(skip_romfs=True) + self.assertIn(' ExeFS 1.0.0: main, sdk, subsdk0, rtld match', calls) + + def test_without_the_flag_the_romfs_is_extracted_and_checked(self): + calls = self.run_base(skip_romfs=False) + self.assertIn('extract', calls) + self.assertIn('check', calls) + self.assertNotIn(' RomFS skipped (--skip-base-romfs)', calls) + + def test_without_the_flag_a_verified_romfs_is_reused(self): + calls = self.run_base(skip_romfs=False, romfs_present=True) + self.assertNotIn('extract', calls) # already there and verified: nothing to extract + self.assertIn('check', calls) # and still checked + + def test_a_base_game_that_is_not_totk_1_0_0_is_refused_even_when_skipping(self): + """The negative control: skipping the RomFS must not turn the base game check into a no-op.""" + (self.out / 'exefs' / 'main').write_bytes(b'') + with mock.patch.object(extract, 'sha256', lambda path: 'f' * 64), \ + mock.patch.object(extract, 'extract_exefs', lambda *a, **k: None), \ + mock.patch.object(extract, 'say', lambda message: None): + with self.assertRaises(ValueError) as raised: + extract.extract_base(Path('base.xci'), Path('prod.keys'), self.out, Path('logs'), EXPECTED, + skip_romfs=True) + self.assertIn('Base game is not TotK 1.0.0', str(raised.exception)) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/run_all.py b/tests/run_all.py index fc74a277..541430ca 100644 --- a/tests/run_all.py +++ b/tests/run_all.py @@ -53,6 +53,7 @@ TESTS = { 'excl_mt': ('.venv', 'cc', [], 'compiles the threaded exclusive-monitor harness with clang'), 'check_isa_test': ('.venv', None, [], ''), 'compile_budget_test': ('.venv', None, [], ''), + 'extract_skip_base_romfs_test': ('.venv-py314', None, [], ''), 'pack_shader_cache_test': ('.venv', None, [], ''), 'spin_waits_test': ('.venv', None, [], ''), 'worktree_test': ('.venv', None, [], ''), diff --git a/tools/extract.py b/tools/extract.py index cb48eada..dda5dc6b 100644 --- a/tools/extract.py +++ b/tools/extract.py @@ -3,7 +3,9 @@ Steps, each skipped when its verified output already exists: 1. ExeFS (code modules) of the base game and of the update. -2. Base RomFS, verified through all six IVFC levels. +2. Base RomFS, verified through all six IVFC levels. --skip-base-romfs leaves this out: no other tool in the + repository reads it, and the merge of step 3 takes the base Program NCA from the base game file and verifies all + six IVFC levels of what it merges, so damage in the base game is still found, only later. 3. Update RomFS: hactool merges the base Program NCA with the update's BKTR section. Steps 1 and 3 run for every file in `input.updates`. Each update's version is identified by the SHA-256 of its `main`, @@ -274,6 +276,28 @@ def merge_update_romfs(base, update, keys_path, hactool, output, logs, phase=lam result.rename(output) +def extract_base(base, keys, out, logs, expected, *, skip_romfs=False): + """The base game's ExeFS, and its RomFS unless skip_romfs. + + The ExeFS identifies the base game as TotK 1.0.0: extract_update matches every update against the base game file + and reports a damaged one itself. skip_romfs leaves out the 17 GB image nothing reads, see the module docstring. + """ + if exefs_problems(out / 'exefs', expected): + say(' extracting ExeFS') + extract_exefs(base, keys, out, logs, update=False) + problems = exefs_problems(out / 'exefs', expected) + require(not problems, f'Base game is not TotK {expected["display_version"]}: {", ".join(problems)} differ') + say(f' ExeFS {expected["display_version"]}: main, sdk, subsdk0, rtld match') + report_npdm(out / 'exefs', expected) + if skip_romfs: + say(' RomFS skipped (--skip-base-romfs)') + return + if not romfs_verified(out / 'romfs', expected): + say(' extracting RomFS (17 GB)') + extract_base_romfs(out, keys, logs, base) + check_romfs(out / 'romfs', expected) + + def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('--config', type=Path, default=ROOT / 'config.local.toml') @@ -285,6 +309,9 @@ def main(): parser.add_argument('--keys', type=Path, help='prod.keys (default: input.keys of the config)') parser.add_argument('--work', type=Path, help='output directory with extracted/ and logs/ (default: work.dir of ' 'the config, else build)') + parser.add_argument('--skip-base-romfs', action='store_true', + help="do not extract the base game's 17 GB RomFS: no other tool reads it, and the update merge " + 'verifies all six IVFC levels of what it takes from the base game file') args = parser.parse_args() config = {} @@ -300,18 +327,7 @@ def main(): v0 = versions['1.0.0'] say('Base game') - base_out = extracted / 'v0' - if exefs_problems(base_out / 'exefs', v0): - say(' extracting ExeFS') - extract_exefs(base, keys, base_out, logs, update=False) - problems = exefs_problems(base_out / 'exefs', v0) - require(not problems, f'Base game is not TotK 1.0.0: {", ".join(problems)} differ') - say(' ExeFS 1.0.0: main, sdk, subsdk0, rtld match') - report_npdm(base_out / 'exefs', v0) - if not romfs_verified(base_out / 'romfs', v0): - say(' extracting RomFS (17 GB)') - extract_base_romfs(base_out, keys, logs, base) - check_romfs(base_out / 'romfs', v0) + extract_base(base, keys, extracted / 'v0', logs, v0, skip_romfs=args.skip_base_romfs) for update in updates: extract_update(base, Path(update), keys, args.hactool, extracted, logs, versions)