From 53622d777bd22e5384c3779d66369b476991a83a Mon Sep 17 00:00:00 2001 From: Aurora Wright Date: Tue, 21 Aug 2018 18:57:45 +0200 Subject: [PATCH] Fix https://github.com/AuroraWright/Luma3DS/issues/1095 and https://github.com/AuroraWright/Luma3DS/issues/1118 --- k11_extension/source/svc/GetSystemInfo.c | 2 +- source/firm.c | 4 ++-- source/firm.h | 2 +- source/main.c | 15 ++++++++++++--- source/patches.c | 4 ++-- source/patches.h | 2 +- sysmodules/loader/source/loader.c | 6 +++--- sysmodules/loader/source/patcher.h | 2 +- 8 files changed, 23 insertions(+), 14 deletions(-) diff --git a/k11_extension/source/svc/GetSystemInfo.c b/k11_extension/source/svc/GetSystemInfo.c index 9cb8b51..52c5c32 100644 --- a/k11_extension/source/svc/GetSystemInfo.c +++ b/k11_extension/source/svc/GetSystemInfo.c @@ -71,7 +71,7 @@ Result GetSystemInfoHook(s64 *out, s32 type, s32 param) case 0x201: // isN3DS *out = (cfwInfo.flags >> 4) & 1; break; - case 0x202: // isSafeMode + case 0x202: // needToInitSd *out = (cfwInfo.flags >> 5) & 1; break; case 0x203: // isSdMode diff --git a/source/firm.c b/source/firm.c index c6d73dd..430d61c 100755 --- a/source/firm.c +++ b/source/firm.c @@ -344,7 +344,7 @@ static inline void mergeSection0(FirmwareType firmType, u32 firmVersion, bool lo } } -u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStorage, bool isFirmProtEnabled, bool isSafeMode, bool doUnitinfoPatch) +u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStorage, bool isFirmProtEnabled, bool needToInitSd, bool doUnitinfoPatch) { u8 *arm9Section = (u8 *)firm + firm->section[2].offset, *arm11Section1 = (u8 *)firm + firm->section[1].offset; @@ -374,7 +374,7 @@ u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStora //Skip on FIRMs < 4.0 if(ISN3DS || firmVersion >= 0x1D) { - ret += installK11Extension(arm11Section1, firm->section[1].size, isSafeMode, baseK11VA, arm11ExceptionsPage, &freeK11Space); + ret += installK11Extension(arm11Section1, firm->section[1].size, needToInitSd, baseK11VA, arm11ExceptionsPage, &freeK11Space); ret += patchKernel11(arm11Section1, firm->section[1].size, baseK11VA, arm11SvcTable, arm11ExceptionsPage); } diff --git a/source/firm.h b/source/firm.h index f1a6e98..5299a42 100644 --- a/source/firm.h +++ b/source/firm.h @@ -31,7 +31,7 @@ u32 loadNintendoFirm(FirmwareType *firmType, FirmwareSource nandType, bool loadFromStorage, bool isSafeMode); void loadHomebrewFirm(u32 pressed); -u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStorage, bool isFirmProtEnabled, bool isSafeMode, bool doUnitinfoPatch); +u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStorage, bool isFirmProtEnabled, bool needToInitSd, bool doUnitinfoPatch); u32 patchTwlFirm(u32 firmVersion, bool loadFromStorage, bool doUnitinfoPatch); u32 patchAgbFirm(bool loadFromStorage, bool doUnitinfoPatch); u32 patch1x2xNativeAndSafeFirm(void); diff --git a/source/main.c b/source/main.c index 120b8ae..15c0cc3 100644 --- a/source/main.c +++ b/source/main.c @@ -51,6 +51,7 @@ void main(int argc, char **argv, u32 magicWord) { bool isFirmProtEnabled, isSafeMode = false, + needToInitSd = false, isNoForceFlagSet = false, isNtrBoot; FirmwareType firmType; @@ -192,9 +193,16 @@ void main(int argc, char **argv, u32 magicWord) goto boot; } - /* Else, force the last used boot options unless a button is pressed + //Account for DSiWare soft resets if exiting TWL_FIRM + if(CFG_BOOTENV == 3) + { + static const u8 TLNC[] = {0x54, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x43}; + if(memcmp((void *)0x20000C00, TLNC, 10) == 0) needToInitSd = true; + } + + /* Force the last used boot options if autobooting a TWL title, or unless a button is pressed or the no-forcing flag is set */ - if(!pressed && !BOOTCFG_NOFORCEFLAG) + if(needToInitSd || memcmp((void *)0x20000300, "TLNC", 4) == 0 || (!pressed && !BOOTCFG_NOFORCEFLAG)) { nandType = (FirmwareSource)BOOTCFG_NAND; firmSource = (FirmwareSource)BOOTCFG_FIRM; @@ -223,6 +231,7 @@ void main(int argc, char **argv, u32 magicWord) firmSource = FIRMWARE_SYSNAND; isSafeMode = true; + needToInitSd = true; //If the PIN has been verified, wait to make it easier to press the SAFE_MODE combo if(pinExists && !shouldLoadConfigMenu) @@ -328,7 +337,7 @@ boot: switch(firmType) { case NATIVE_FIRM: - res = patchNativeFirm(firmVersion, nandType, loadFromStorage, isFirmProtEnabled, isSafeMode, doUnitinfoPatch); + res = patchNativeFirm(firmVersion, nandType, loadFromStorage, isFirmProtEnabled, needToInitSd, doUnitinfoPatch); break; case TWL_FIRM: res = patchTwlFirm(firmVersion, loadFromStorage, doUnitinfoPatch); diff --git a/source/patches.c b/source/patches.c index 38966b0..af6fcf9 100644 --- a/source/patches.c +++ b/source/patches.c @@ -100,7 +100,7 @@ static inline u32 *getKernel11HandlerVAPos(u8 *pos, u32 *arm11ExceptionsPage, u3 return (u32 *)(pos + pointedInstructionVA - baseK11VA + 8); } -u32 installK11Extension(u8 *pos, u32 size, bool isSafeMode, u32 baseK11VA, u32 *arm11ExceptionsPage, u8 **freeK11Space) +u32 installK11Extension(u8 *pos, u32 size, bool needToInitSd, u32 baseK11VA, u32 *arm11ExceptionsPage, u8 **freeK11Space) { //The parameters to be passed on to the kernel ext //Please keep that in sync with the definition in k11_extension/source/main.c @@ -201,7 +201,7 @@ u32 installK11Extension(u8 *pos, u32 size, bool isSafeMode, u32 baseK11VA, u32 * if(ISRELEASE) info->flags = 1; if(ISN3DS) info->flags |= 1 << 4; - if(isSafeMode) info->flags |= 1 << 5; + if(needToInitSd) info->flags |= 1 << 5; if(isSdMode) info->flags |= 1 << 6; return 0; diff --git a/source/patches.h b/source/patches.h index 1382125..c7ae877 100644 --- a/source/patches.h +++ b/source/patches.h @@ -39,7 +39,7 @@ u8 *getProcess9Info(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr); u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space, u32 **arm11SvcHandler, u32 **arm11ExceptionsPage); -u32 installK11Extension(u8 *pos, u32 size, bool isSafeMode, u32 baseK11VA, u32 *arm11ExceptionsPage, u8 **freeK11Space); +u32 installK11Extension(u8 *pos, u32 size, bool needToInitSd, u32 baseK11VA, u32 *arm11ExceptionsPage, u8 **freeK11Space); u32 patchKernel11(u8 *pos, u32 size, u32 baseK11VA, u32 *arm11SvcTable, u32 *arm11ExceptionsPage); u32 patchSignatureChecks(u8 *pos, u32 size); u32 patchOldSignatureChecks(u8 *pos, u32 size); diff --git a/sysmodules/loader/source/loader.c b/sysmodules/loader/source/loader.c index ec0f0c2..7370c7c 100644 --- a/sysmodules/loader/source/loader.c +++ b/sysmodules/loader/source/loader.c @@ -12,7 +12,7 @@ #define HBLDR_3DSX_TID (*(vu64 *)0x1FF81100) u32 config, multiConfig, bootConfig; -bool isN3DS, isSafeMode, isSdMode; +bool isN3DS, needToInitSd, isSdMode; const char CODE_PATH[] = {0x01, 0x00, 0x00, 0x00, 0x2E, 0x63, 0x6F, 0x64, 0x65, 0x00, 0x00, 0x00}; @@ -47,12 +47,12 @@ static inline void loadCFWInfo(void) if(R_FAILED(svcGetSystemInfo(&out, 0x10000, 0x201))) svcBreak(USERBREAK_ASSERT); isN3DS = (bool)out; if(R_FAILED(svcGetSystemInfo(&out, 0x10000, 0x202))) svcBreak(USERBREAK_ASSERT); - isSafeMode = (bool)out; + needToInitSd = (bool)out; if(R_FAILED(svcGetSystemInfo(&out, 0x10000, 0x203))) svcBreak(USERBREAK_ASSERT); isSdMode = (bool)out; IFile file; - if(isSafeMode) fileOpen(&file, ARCHIVE_SDMC, "/", FS_OPEN_READ); //Init SD card if SAFE_MODE is being booted + if(needToInitSd) fileOpen(&file, ARCHIVE_SDMC, "/", FS_OPEN_READ); //Init SD card if SAFE_MODE is being booted } static int lzss_decompress(u8 *end) diff --git a/sysmodules/loader/source/patcher.h b/sysmodules/loader/source/patcher.h index e2eebd7..3fceb0d 100644 --- a/sysmodules/loader/source/patcher.h +++ b/sysmodules/loader/source/patcher.h @@ -39,7 +39,7 @@ enum singleOptions }; extern u32 config, multiConfig, bootConfig; -extern bool isN3DS, isSafeMode, isSdMode; +extern bool isN3DS, needToInitSd, isSdMode; void patchCode(u64 progId, u16 progVer, u8 *code, u32 size, u32 textSize, u32 roSize, u32 dataSize, u32 roAddress, u32 dataAddress); Result fileOpen(IFile *file, FS_ArchiveID archiveId, const char *path, int flags);