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
+15
-1
@@ -11,6 +11,8 @@
|
||||
// <profile>/save-backup/<time>/ the three committed saves before the current one
|
||||
// <profile>/temp/ emptied at start
|
||||
// <profile>/cache/<index>/ cache storage
|
||||
// <profile>/sdcard/ the SD card a mod plugin mounts (MountSdCard, under any mount name: Zonai
|
||||
// Fieldworks keeps its settings and logs there); the game never mounts it
|
||||
//
|
||||
// The save mount is journaled as on Horizon: changes go to a working copy and become the committed save atomically at
|
||||
// Commit; a crash or an Unmount before Commit leaves the previous committed save. Commit returns at once and the copy
|
||||
@@ -33,7 +35,8 @@
|
||||
#define NMOUNTS 16
|
||||
#define PATH_CAP 1100
|
||||
|
||||
enum { M_FREE, M_SAVE, M_TEMP, M_CACHE, M_SAVE_RO }; // M_SAVE: the working copy; M_SAVE_RO: the committed save
|
||||
// M_SAVE: the working copy; M_SAVE_RO: the committed save; M_SDCARD: <profile>/sdcard
|
||||
enum { M_FREE, M_SAVE, M_TEMP, M_CACHE, M_SAVE_RO, M_SDCARD };
|
||||
|
||||
typedef struct { int kind; char name[32]; char root[1024]; } Mount;
|
||||
typedef struct { PlatFile *h; int mode, save; } File; // h NULL = free; save: on a save-data mount
|
||||
@@ -800,6 +803,14 @@ static uint32_t mount_temp(const char *name) {
|
||||
return mount_add(name, M_TEMP, p);
|
||||
}
|
||||
|
||||
static uint32_t mount_sdcard(const char *name) {
|
||||
ready();
|
||||
char p[PATH_CAP];
|
||||
snprintf(p, sizeof p, "%s/sdcard", profile);
|
||||
make_dirs(p);
|
||||
return mount_add(name, M_SDCARD, p);
|
||||
}
|
||||
|
||||
static void cache_dir(int index, char *out, size_t cap) { snprintf(out, cap, "%s/cache/%d", profile, index); }
|
||||
|
||||
static uint32_t create_cache(int index, int64_t size, int64_t journal) {
|
||||
@@ -1210,6 +1221,7 @@ static void ov_mount_save(Cpu *c) { ret_x(c, mount_save(arg_p(c, 0), arg_p(c, 1)
|
||||
static void ov_mount_save_ro(Cpu *c) { ret_x(c, mount_save_ro(arg_p(c, 0), arg_x(c, 1), arg_p(c, 2))); }
|
||||
static void ov_save_exists(Cpu *c) { ret_x(c, save_exists(arg_x(c, 0), arg_p(c, 1))); }
|
||||
static void ov_mount_temp(Cpu *c) { ret_x(c, mount_temp(arg_p(c, 0))); }
|
||||
static void ov_mount_sdcard(Cpu *c) { ret_x(c, mount_sdcard(arg_p(c, 0))); }
|
||||
static void ov_unmount(Cpu *c) { unmount(arg_p(c, 0)); }
|
||||
static void ov_commit(Cpu *c) { ret_x(c, commit(arg_p(c, 0))); }
|
||||
static void ov_create_cache(Cpu *c) { ret_x(c, create_cache(I32(0), I64(1), I64(2))); }
|
||||
@@ -1245,6 +1257,7 @@ static void ov_set_priority(Cpu *c) { (void)c; }
|
||||
#define S_MOUNT_SAVE_RO "_ZN2nn2fs21MountSaveDataReadOnlyEPKcNS_13ApplicationIdERKNS_7account3UidE"
|
||||
#define S_SAVE_EXISTS "_ZN2nn2fs18IsSaveDataExistingENS_13ApplicationIdERKNS_7account3UidE"
|
||||
#define S_MOUNT_TEMP "_ZN2nn2fs21MountTemporaryStorageEPKc"
|
||||
#define S_MOUNT_SDCARD "_ZN2nn2fs11MountSdCardEPKc"
|
||||
#define S_UNMOUNT "_ZN2nn2fs7UnmountEPKc"
|
||||
#define S_COMMIT "_ZN2nn2fs6CommitEPKc"
|
||||
#define S_CREATE_CACHE "_ZN2nn2fs18CreateCacheStorageEill"
|
||||
@@ -1281,6 +1294,7 @@ HOST_OVERRIDE(S_MOUNT_SAVE, ov_mount_save);
|
||||
HOST_OVERRIDE(S_MOUNT_SAVE_RO, ov_mount_save_ro);
|
||||
HOST_OVERRIDE(S_SAVE_EXISTS, ov_save_exists);
|
||||
HOST_OVERRIDE(S_MOUNT_TEMP, ov_mount_temp);
|
||||
HOST_OVERRIDE(S_MOUNT_SDCARD, ov_mount_sdcard);
|
||||
HOST_OVERRIDE(S_UNMOUNT, ov_unmount);
|
||||
HOST_OVERRIDE(S_COMMIT, ov_commit);
|
||||
HOST_OVERRIDE(S_CREATE_CACHE, ov_create_cache);
|
||||
|
||||
@@ -119,6 +119,18 @@ static void ov_desired_language(Cpu *c) {
|
||||
ret_x(c, v);
|
||||
}
|
||||
|
||||
// nn::settings::detail::GetLanguageCode(LanguageCode *out): the system language, the one GetDesiredLanguage gives.
|
||||
// The game never calls it; Material Columns picks its text by it. x0 = 0 for the callers that read a Result.
|
||||
static void ov_settings_language(Cpu *c) {
|
||||
const char *code = ov_language();
|
||||
uint8_t *out = arg_p(c, 0);
|
||||
if (out) {
|
||||
memset(out, 0, 8);
|
||||
memcpy(out, code, strlen(code));
|
||||
}
|
||||
ret_x(c, 0);
|
||||
}
|
||||
|
||||
static void ov_oe_initialize(Cpu *c) { (void)c; rt_log("oe: application environment: a local host process"); }
|
||||
|
||||
// void SetPerformanceConfiguration(PerformanceMode mode, int config)
|
||||
@@ -872,6 +884,7 @@ static void ov_font_size(Cpu *c) {
|
||||
// ==== registration
|
||||
HOST_OVERRIDE("_ZN2nn2oe10InitializeEv", ov_oe_initialize);
|
||||
HOST_OVERRIDE("_ZN2nn2oe18GetDesiredLanguageEv", ov_desired_language);
|
||||
HOST_OVERRIDE("_ZN2nn8settings6detail15GetLanguageCodeEPNS0_12LanguageCodeE", ov_settings_language);
|
||||
HOST_OVERRIDE("_ZN2nn2oe27SetPerformanceConfigurationENS0_15PerformanceModeEi", ov_performance_config);
|
||||
HOST_OVERRIDE("_ZN2nn2oe15SetCpuBoostModeENS0_12CpuBoostModeE", ov_cpu_boost);
|
||||
HOST_OVERRIDE("_ZN2nn2oe16GetOperationModeEv", ov_operation_mode);
|
||||
|
||||
+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