From c0d61e4c13338c0d0fb98da4d0887ff6576475dd Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 6 Oct 2026 18:31:34 +0000 Subject: [PATCH] refactor(os): write OSNet's weak NWC24 shutdown helpers instead of forcing their strings (grok-osnet) (worker 32669a13) --- libs/RVL_SDK/src/os/OSNet.c | 176 ++++++++++++++++++++- tools/decomp-assist/grok-osnet.attempts.md | 8 + 2 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 tools/decomp-assist/grok-osnet.attempts.md diff --git a/libs/RVL_SDK/src/os/OSNet.c b/libs/RVL_SDK/src/os/OSNet.c index 9c9a946d..d908bc0f 100644 --- a/libs/RVL_SDK/src/os/OSNet.c +++ b/libs/RVL_SDK/src/os/OSNet.c @@ -1,9 +1,23 @@ #include #include +#include + +NWC24Err NWC24iPrepareShutdown(void) NO_INLINE; +s32 NWC24SuspendScheduler(void) NO_INLINE; +s32 NWC24ResumeScheduler(void) NO_INLINE; #include #include +static s32 nwc24ShtFd = -1; +static s32 nwc24ShtRetryRest; +static OSShutdownFunctionInfo ShutdownFuncInfo; + +static NWC24Err CheckCallingStatus(const char* callerName) NO_INLINE; +BOOL NWC24Shutdown_(BOOL final, u32 event) NO_INLINE; +NWC24Err NWC24iRequestShutdown(u32 event, s32* result) NO_INLINE; +NWC24Err NWC24iSetRtcCounter_(s32 rtc, BOOL save) NO_INLINE; + void __OSInitNet() { NWC24Err ret; OSIOSRev iosRev; @@ -38,11 +52,157 @@ s32 __OSSyncTimeWithNetRM() { return NWC24iSynchronizeRtcCounter(FALSE); } -DECOMP_FORCE_ACTIVE(OSNet_c, "NWC24iPrepareShutdown"); -DECOMP_FORCE_ACTIVE(OSNet_c, "/dev/net/kd/request"); -DECOMP_FORCE_ACTIVE(OSNet_c, "NWC24SuspendScheduler"); -DECOMP_FORCE_ACTIVE(OSNet_c, "NWC24ResumeScheduler"); -DECOMP_FORCE_ACTIVE(OSNet_c, "NWC24iRequestShutdown"); -DECOMP_FORCE_ACTIVE(OSNet_c, "NWC24Shutdown_: Give up!\n"); // Give up! -DECOMP_FORCE_ACTIVE(OSNet_c, "NWC24iSetRtcCounter_"); -DECOMP_FORCE_ACTIVE(OSNet_c, "/dev/net/kd/time"); +static NWC24Err CheckCallingStatus(const char* callerName) NO_INLINE { + if (OSGetCurrentThread() == 0) { + return NWC24_ERR_FATAL; + } + + return NWC24_OK; +} + +DECL_WEAK NWC24Err NWC24iPrepareShutdown(void) NO_INLINE { + NWC24Err result = NWC24_OK; + + ShutdownFuncInfo.func = NWC24Shutdown_; + ShutdownFuncInfo.priority = 110; + OSRegisterShutdownFunction(&ShutdownFuncInfo); + + if (nwc24ShtFd < 0) { + result = NWC24iOpenResourceManager("NWC24iPrepareShutdown", "/dev/net/kd/request", &nwc24ShtFd, 1); + } + + nwc24ShtRetryRest = 5; + + if (result == NWC24_OK) { + result = 1; + } + + return result; +} + +DECL_WEAK s32 NWC24SuspendScheduler(void) NO_INLINE { + static u8 susResult[0x20] ALIGN32; + NWC24Err result; + NWC24Err closeResult; + s32 fd; + + result = CheckCallingStatus("NWC24SuspendScheduler"); + if (result < NWC24_OK) { + return result; + } + + result = NWC24iOpenResourceManager("NWC24SuspendScheduler", "/dev/net/kd/request", &fd, 0); + if (result >= NWC24_OK) { + result = NWC24iIoctlResourceManager("NWC24SuspendScheduler", fd, NWC24_IOCTL_SUSPEND_SCHEDULER, NULL, 0, susResult, sizeof(susResult)); + if (result >= NWC24_OK) { + result = *(NWC24Err*)susResult; + } + + closeResult = NWC24iCloseResourceManager("NWC24SuspendScheduler", fd); + if (closeResult < NWC24_OK) { + result = closeResult; + } + } + + return result; +} + +DECL_WEAK s32 NWC24ResumeScheduler(void) NO_INLINE { + static u8 resResult[0x20] ALIGN32; + NWC24Err result; + NWC24Err closeResult; + s32 fd; + + result = CheckCallingStatus("NWC24ResumeScheduler"); + if (result < NWC24_OK) { + return result; + } + + result = NWC24iOpenResourceManager("NWC24ResumeScheduler", "/dev/net/kd/request", &fd, 0); + if (result >= NWC24_OK) { + result = NWC24iIoctlResourceManager("NWC24ResumeScheduler", fd, NWC24_IOCTL_RESUME_SCHEDULER, NULL, 0, resResult, sizeof(resResult)); + if (result >= NWC24_OK) { + result = *(NWC24Err*)resResult; + } + + closeResult = NWC24iCloseResourceManager("NWC24ResumeScheduler", fd); + if (closeResult < NWC24_OK) { + result = closeResult; + } + } + + return result; +} + +NWC24Err NWC24iRequestShutdown(u32 event, s32* result) NO_INLINE { + static u32 shtBuffer[8] ALIGN32; + static u32 shtResult[8] ALIGN32; + + shtBuffer[0] = event; + return NWC24iIoctlResourceManagerAsync("NWC24iRequestShutdown", nwc24ShtFd, NWC24_IOCTL_SHUTDOWN, shtBuffer, sizeof(shtBuffer), shtResult, + sizeof(shtResult), result); +} + +BOOL NWC24Shutdown_(BOOL final, u32 event) NO_INLINE { + static BOOL shuttingdown; + static s32 result; + + if (final) { + return TRUE; + } + + if (shuttingdown) { + if (NWC24iIsAsyncRequestPending()) { + return FALSE; + } + + if (result >= 0) { + return TRUE; + } + + if (nwc24ShtRetryRest > 0) { + shuttingdown = FALSE; + nwc24ShtRetryRest--; + } else { + OSReport("NWC24Shutdown_: Give up!\n"); + return TRUE; + } + } else if (NWC24iRequestShutdown(event, &result) >= NWC24_OK) { + shuttingdown = TRUE; + } + + return FALSE; +} + +static u32 nwc24TimeCommonBuffer[8] ALIGN32; +static u32 nwc24TimeCommonResult[8] ALIGN32; + +NWC24Err NWC24iSetRtcCounter_(s32 rtc, BOOL save) NO_INLINE { + NWC24Err result; + NWC24Err closeResult; + s32 fd; + + result = CheckCallingStatus("NWC24iSetRtcCounter_"); + if (result < NWC24_OK) { + return result; + } + + result = NWC24iOpenResourceManager("NWC24iSetRtcCounter_", "/dev/net/kd/time", &fd, 0); + if (result >= NWC24_OK) { + nwc24TimeCommonBuffer[0] = rtc; + nwc24TimeCommonBuffer[1] = save; + + result = NWC24iIoctlResourceManager("NWC24iSetRtcCounter_", fd, NWC24_IOCTL_SET_RTC_COUNTER, nwc24TimeCommonBuffer, + sizeof(nwc24TimeCommonBuffer), nwc24TimeCommonResult, sizeof(nwc24TimeCommonResult)); + if (result >= NWC24_OK) { + result = *(s32*)nwc24TimeCommonResult; + } + + closeResult = NWC24iCloseResourceManager("NWC24iSetRtcCounter_", fd); + if (result >= NWC24_OK) { + result = closeResult; + } + } + + return result; +} diff --git a/tools/decomp-assist/grok-osnet.attempts.md b/tools/decomp-assist/grok-osnet.attempts.md new file mode 100644 index 00000000..a4de7bb9 --- /dev/null +++ b/tools/decomp-assist/grok-osnet.attempts.md @@ -0,0 +1,8 @@ +# OSNet dead NWC24 functions (grok-osnet) + +Target object keeps only __OSInitNet and __OSSyncTimeWithNetRM. The eight fossil strings are the caller names and paths from the SDK shutdown/scheduler/rtc helpers. RevoEX already defines NWC24iPrepareShutdown, NWC24SuspendScheduler, and NWC24ResumeScheduler, so those three are weak here and the linker keeps the RevoEX copies. + +- Full zeldaret bodies, including CheckCallingStatus's "Illegal status..." report: pool diverged (extra string, and IPA inlined the helpers into __OSInitNet). +- never_inline on the definition only: still inlined. MWCC uses the first declaration. Prototypes with NO_INLINE before private/nwc24.h stop the inline. __OSInitNet 0xb4/0xb4 differing 0, __OSSyncTimeWithNetRM 0x8/0x8 differing 0. Pool identical, 11 strings. +- Strong definitions: link failed, multiply-defined against NWC24System.o and NWC24Schedule.o. +- DECL_WEAK on those three: link succeeded. main.dol SHA1 26116613f624061ba99c8d1a299aaa6efa85670d. -- 2.49.1