mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-24 13:45:57 +00:00
Mod plugins that hook the middle of game functions, swap pointers in the game's data, use the SD card or switch their hooks during play build in, and may call what the game itself calls; Zonai Fieldworks starts (#1388)
Zonai Fieldworks was refused at several steps; each is now handled: 1. The start-up asked GetInfo 6, 7, 16 and 17 and SetHeapSize; runtime/plugin_init.c serves them. 2. Its start-up calls main where the lift has no function start. The tool names those addresses (--entries-out) and the installer lifts again with them as `entry` lines, up to four rounds (installer/steps.py PLUGIN_ENTRY_ROUNDS). 3. Branch hooks into the middle of game functions (far-jump veneers) and pointer swaps in main's data are accepted hook kinds; the veneer targets seed the plugin's lift, and an Orig trampoline counts as a seed. 4. Code the start-up writes into the plugin's own .text (hook pads it switches to during play) is found by comparing the image before and after the init, and seeds the lift (init_code). Without it the game stopped with "no function at guest pc" when a feature switched on. 5. recomp/gen.py: a branch-out alternative on a flag-writing site leaves with the flags from before the site; an inline hook may sit on a blr. 6. nn::fs::MountSdCard mounts <profile>/sdcard (runtime/fs_host.c), and nn::settings language codes are answered (runtime/ov_system.c). The import check now also allows what the game's main imports (--exefs, game_imports): the game runs on those paths, so the runtime serves them. Left out: nn::ro, the C runtime start, the thread-local allocator hook and the shader compiler. Break in code the hooks reach is allowed (an abort ends the game with the reason, as on the Switch), and RenameFile, DeleteDirectoryRecursively, Commit and Unmount join the SD card calls. Checked: Zonai Fieldworks, The Second Quest, Challenge Mode and Material Columns on 1.4.3, Full Inventory Expansion, Quick Weapon Durability and Magic of the Kingdom on 1.2.1 accepted; UltraCam's init still crashes its service run. Plain lift unchanged; probes 4/4 on both versions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
942b6b7b2b
commit
3fe8d7a1d9
9 files changed
+552
-136
No files matched your search
+32
-13
@@ -38,9 +38,15 @@
|
||||
// hook branches reach main's .text.
|
||||
// - nn::oe::GetDisplayVersion is answered with this build's game version, so the plugin picks its table for the
|
||||
// installed game without the applet services.
|
||||
// Only svcs the init needs are allowed (QueryMemory, GetSystemTick, OutputDebugString, GetInfo 2-5, 12-15 and 65001,
|
||||
// Map/UnmapProcessMemory); any other one, such as CreateSession and ReplyAndReceive of exlaunch's process-handle
|
||||
// fallback, ends the run with the reason in the record.
|
||||
// - GetInfo 6, 16 and 17, the total memory and the system resource size and use, are constants (memory.c
|
||||
// mem_info); 7, the memory in use, is the same at the same point of the same init. Zonai Fieldworks asks them
|
||||
// after its hooks are in.
|
||||
// - SetHeapSize grows the process heap past the size the game's nninitStartup set before the init runs (Zonai
|
||||
// Fieldworks keeps a pool in the tail). The heap base is fixed and the game asked its size the same way at every
|
||||
// start, so the init gets the same answer each time.
|
||||
// Only svcs the init needs are allowed (SetHeapSize, QueryMemory, GetSystemTick, OutputDebugString, GetInfo 2-7,
|
||||
// 12-17 and 65001, Map/UnmapProcessMemory); any other one, such as CreateSession and ReplyAndReceive of exlaunch's
|
||||
// process-handle fallback, ends the run with the reason in the record.
|
||||
//
|
||||
// The record, in DIR: record.json (modules, which plugins' inits ran, the writes into every game module as words, the
|
||||
// alias calls, svc and import counts, the tick offset, a refusal), plugin<N>.img (the plugin's image after its init,
|
||||
@@ -69,7 +75,9 @@
|
||||
#define POOL_SIZE (64 * GiB)
|
||||
#define POOL_ALIGN (4 * GiB)
|
||||
#define RTLD_ALIGN 0x200000ull
|
||||
#define MAX_ALIASES 256
|
||||
// exlaunch maps a page alias for every patch it writes and unmaps it after; Zonai Fieldworks makes over 211 in its
|
||||
// init. Each slot is 48 bytes, and only the live aliases hold a copy of their pages.
|
||||
#define MAX_ALIASES 2048
|
||||
#define MAX_IMPORTS 64
|
||||
#define CUR_PROCESS 0xFFFF8001u
|
||||
// Horizon memory states and permissions of QueryMemory, and results.
|
||||
@@ -94,7 +102,7 @@ static uint64_t pool_base;
|
||||
static int running; // a plugin's init runs: svcs and imports are counted
|
||||
static int play; // plugins_start: a refused SVC fails the plugin instead of ending the process
|
||||
static char failed[256]; // play: why the running init was refused
|
||||
static uint64_t svc_count[0x80], info_count[16], info_65001;
|
||||
static uint64_t svc_count[0x80], info_count[18], info_65001;
|
||||
static struct { const char *name; uint64_t calls; } imports[MAX_IMPORTS];
|
||||
static int nimports;
|
||||
static uint8_t **pre; // per loaded module: its image before the plugins' inits
|
||||
@@ -198,11 +206,22 @@ static uint32_t process_memory(int unmap, uint64_t dst, uint32_t process, uint64
|
||||
}
|
||||
return R_INVALID_STATE;
|
||||
}
|
||||
if (!module_holding(src, size)) return R_INVALID_STATE; // only the modules' own memory
|
||||
if (!module_holding(src, size)) { // only the modules' own memory
|
||||
rt_log("plugin-init: MapProcessMemory refused: the source %#llx+%#llx is no module's memory",
|
||||
(unsigned long long)src, (unsigned long long)size);
|
||||
return R_INVALID_STATE;
|
||||
}
|
||||
RtMemoryInfo q;
|
||||
mem_query(dst, &q);
|
||||
if (q.state != ST_FREE || dst + size > q.base + q.size) return R_INVALID_STATE;
|
||||
if (naliases == MAX_ALIASES) return R_LIMIT_REACHED;
|
||||
if (q.state != ST_FREE || dst + size > q.base + q.size) {
|
||||
rt_log("plugin-init: MapProcessMemory refused: the destination %#llx+%#llx is not free (state %#x)",
|
||||
(unsigned long long)dst, (unsigned long long)size, q.state);
|
||||
return R_INVALID_STATE;
|
||||
}
|
||||
if (naliases == MAX_ALIASES) {
|
||||
rt_log("plugin-init: MapProcessMemory refused: all %d alias slots used", MAX_ALIASES);
|
||||
return R_LIMIT_REACHED;
|
||||
}
|
||||
if (!plat_mem_commit(dst, size)) return R_INVALID_STATE; // not inside one of our reservations
|
||||
Alias *al = &aliases[naliases];
|
||||
al->snap = malloc(size);
|
||||
@@ -235,10 +254,10 @@ static int plugin_svc(Cpu *c, uint32_t imm, uint64_t pc) {
|
||||
svc_count[imm & 0x7F]++;
|
||||
char why[256], at[128];
|
||||
switch (imm) {
|
||||
case 0x06: case 0x1E: case 0x27: case 0x74: case 0x75: return 0;
|
||||
case 0x01: case 0x06: case 0x1E: case 0x27: case 0x74: case 0x75: return 0;
|
||||
case 0x29: {
|
||||
uint32_t id = (uint32_t)c->x[1];
|
||||
if ((id >= 2 && id <= 5) || (id >= 12 && id <= 15)) {
|
||||
if ((id >= 2 && id <= 7) || (id >= 12 && id <= 17)) {
|
||||
info_count[id]++;
|
||||
return 0;
|
||||
}
|
||||
@@ -252,8 +271,8 @@ static int plugin_svc(Cpu *c, uint32_t imm, uint64_t pc) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
snprintf(why, sizeof why, "svc %#x at %s (only QueryMemory, GetSystemTick, OutputDebugString, GetInfo 2-5, 12-15 "
|
||||
"and 65001, Map/UnmapProcessMemory are served)", imm, ldr_describe(pc, at, sizeof at));
|
||||
snprintf(why, sizeof why, "svc %#x at %s (only SetHeapSize, QueryMemory, GetSystemTick, OutputDebugString, "
|
||||
"GetInfo 2-7, 12-17 and 65001, Map/UnmapProcessMemory are served)", imm, ldr_describe(pc, at, sizeof at));
|
||||
refuse(why);
|
||||
c->x[0] = R_NOT_IMPLEMENTED;
|
||||
return 1;
|
||||
@@ -392,7 +411,7 @@ static void write_record(const char *refused) {
|
||||
if (svc_count[i]) fprintf(f, "%s\"%#x\": %llu", first ? "" : ", ", i, (unsigned long long)svc_count[i]), first = 0;
|
||||
fputs("},\n\"get_info\": {", f);
|
||||
first = 1;
|
||||
for (int i = 0; i < 16; i++)
|
||||
for (int i = 0; i < 18; i++)
|
||||
if (info_count[i]) fprintf(f, "%s\"%d\": %llu", first ? "" : ", ", i, (unsigned long long)info_count[i]), first = 0;
|
||||
if (info_65001) fprintf(f, "%s\"65001\": %llu", first ? "" : ", ", (unsigned long long)info_65001);
|
||||
fputs("},\n\"imports\": {", f);
|
||||
|
||||
Reference in new issue
Block a user