From 748b771618b0bf3ce017a705f6e9f2b3c1875b51 Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Thu, 16 Jul 2020 17:55:31 +0100 Subject: [PATCH] rosalina: ndm + shutdown issue workaround --- sysmodules/rosalina/include/minisoc.h | 4 ++ sysmodules/rosalina/source/main.c | 2 + sysmodules/rosalina/source/minisoc.c | 56 ++++++++++++++++++--------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/sysmodules/rosalina/include/minisoc.h b/sysmodules/rosalina/include/minisoc.h index 6040bc8..c735171 100644 --- a/sysmodules/rosalina/include/minisoc.h +++ b/sysmodules/rosalina/include/minisoc.h @@ -21,6 +21,10 @@ extern bool miniSocEnabled; Result miniSocInit(void); + +void miniSocLockState(void); +void miniSocUnlockState(bool force); + Result miniSocExitDirect(void); Result miniSocExit(void); diff --git a/sysmodules/rosalina/source/main.c b/sysmodules/rosalina/source/main.c index 390f09d..4874161 100644 --- a/sysmodules/rosalina/source/main.c +++ b/sysmodules/rosalina/source/main.c @@ -180,6 +180,8 @@ static void handlePreTermNotification(u32 notificationId) (void)notificationId; // Might be subject to a race condition, but heh. + miniSocUnlockState(true); + // Disable input redirection InputRedirection_Disable(100 * 1000 * 1000LL); diff --git a/sysmodules/rosalina/source/minisoc.c b/sysmodules/rosalina/source/minisoc.c index 624db35..252c81d 100644 --- a/sysmodules/rosalina/source/minisoc.c +++ b/sysmodules/rosalina/source/minisoc.c @@ -1,3 +1,4 @@ + /* * This file is part of Luma3DS. * Copyright (C) 2016-2020 Aurora Wright, TuxSH @@ -54,6 +55,41 @@ static Result SOCU_Shutdown(void) return cmdbuf[1]; } +// unsafe but what can I do? +void miniSocLockState(void) +{ + Result res = 0; + __dmb(); + if (!exclusiveStateEntered && isServiceUsable("ndm:u")) + { + ndmuInit(); + res = NDMU_EnterExclusiveState(NDM_EXCLUSIVE_STATE_INFRASTRUCTURE); + if (R_SUCCEEDED(res)) + res = NDMU_LockState(); // prevents ndm from switching to StreetPass when the lid is closed + exclusiveStateEntered = R_SUCCEEDED(res); + __dmb(); + } +} + +void miniSocUnlockState(bool force) +{ + Result res = 0; + + __dmb(); + if (exclusiveStateEntered) + { + if (!force) + { + res = NDMU_UnlockState(); + if (R_SUCCEEDED(res)) + res = NDMU_LeaveExclusiveState(); + } + ndmuExit(); + exclusiveStateEntered = R_FAILED(res); + __dmb(); + } +} + Result miniSocInit(void) { if(AtomicPostIncrement(&miniSocRefCount)) @@ -87,15 +123,7 @@ Result miniSocInit(void) ret = SOCU_Initialize(miniSocMemHandle, socContextSize); if(ret != 0) goto cleanup; - if (!exclusiveStateEntered && isServiceUsable("ndm:u")) - { - ndmuInit(); - ret = NDMU_EnterExclusiveState(NDM_EXCLUSIVE_STATE_INFRASTRUCTURE); - if (R_SUCCEEDED(ret)) - ret = NDMU_LockState(); // prevents ndm from switching to StreetPass when the lid is closed - //ndmuExit(); - exclusiveStateEntered = R_SUCCEEDED(ret); - } + miniSocLockState(); svcKernelSetState(0x10000, 0x10); miniSocEnabled = true; @@ -140,15 +168,7 @@ Result miniSocExitDirect(void) svcControlMemory(&tmp, socContextAddr, socContextAddr, socContextSize, MEMOP_FREE, MEMPERM_DONTCARE); if(ret == 0) { - if (exclusiveStateEntered && isServiceUsable("ndm:u")) - { - //ndmuInit(); - ret = NDMU_UnlockState(); - if (R_SUCCEEDED(ret)) - ret = NDMU_LeaveExclusiveState(); - ndmuExit(); - exclusiveStateEntered = R_FAILED(ret); - } + miniSocUnlockState(false); miniSocEnabled = false; svcKernelSetState(0x10000, 0x10);