diff --git a/injector/source/patcher.c b/injector/source/patcher.c index c5c2627..362230d 100644 --- a/injector/source/patcher.c +++ b/injector/source/patcher.c @@ -3,7 +3,7 @@ #include "patcher.h" #include "ifile.h" -static CFWInfo info = {0}; +static CFWInfo info; static int memcmp(const void *buf1, const void *buf2, u32 size) { @@ -97,11 +97,10 @@ static void loadCFWInfo(void) if(!infoLoaded) { svcGetCFWInfo(&info); + IFile file; if(BOOTCONFIG(5, 1) && R_SUCCEEDED(fileOpen(&file, ARCHIVE_SDMC, "/", FS_OPEN_READ))) //Init SD card if SAFE_MODE is being booted - { IFile_Close(&file); - } infoLoaded = true; } diff --git a/loader/source/main.c b/loader/source/main.c index 4605d25..f6d9cae 100644 --- a/loader/source/main.c +++ b/loader/source/main.c @@ -23,7 +23,7 @@ #include "memory.h" #include "cache.h" -extern u32 payloadSize; //defined in start.s +extern u32 payloadSize; //Defined in start.s void main(void) { diff --git a/patches/emunand.s b/patches/emunand.s index a7e6cfa..0836c1f 100644 --- a/patches/emunand.s +++ b/patches/emunand.s @@ -43,4 +43,4 @@ nand_sd: sdmmc: .ascii "SDMC" nand_offset: .ascii "NAND" ; for rednand this should be 1 ncsd_header_offset: .ascii "NCSD" ; depends on nand manufacturer + emunand type (GW/RED) -.close +.close \ No newline at end of file diff --git a/patches/reboot.s b/patches/reboot.s index 9cb779f..9174a23 100644 --- a/patches/reboot.s +++ b/patches/reboot.s @@ -125,4 +125,4 @@ dat_fname: .dcw "sdmc:/Luma3DS.dat" bx r0 .pool -.close +.close \ No newline at end of file diff --git a/patches/twl_k11modules.s b/patches/twl_k11modules.s index 88fcc30..d808b27 100644 --- a/patches/twl_k11modules.s +++ b/patches/twl_k11modules.s @@ -143,4 +143,4 @@ patchesEnd: .pool -.close +.close \ No newline at end of file diff --git a/source/config.c b/source/config.c index 6b684fa..a5f7c5c 100644 --- a/source/config.c +++ b/source/config.c @@ -64,7 +64,7 @@ void writeConfig(const char *configPath, u32 configTemp) } } -void configure(void) +void configMenu(void) { initScreens(); diff --git a/source/config.h b/source/config.h index ecdf4b4..899317a 100644 --- a/source/config.h +++ b/source/config.h @@ -43,4 +43,4 @@ extern cfgData configData; bool readConfig(const char *configPath); void writeConfig(const char *configPath, u32 configTemp); -void configure(void); \ No newline at end of file +void configMenu(void); \ No newline at end of file diff --git a/source/firm.c b/source/firm.c index cbf6d17..c6a120d 100755 --- a/source/firm.c +++ b/source/firm.c @@ -137,11 +137,11 @@ void main(void) if(pinExists) verifyPin(&pin); //If no configuration file exists or SELECT is held, load configuration menu - bool shouldLoadConfigurationMenu = needConfig == CREATE_CONFIGURATION || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1)); + bool shouldLoadConfigMenu = needConfig == CREATE_CONFIGURATION || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1)); - if(shouldLoadConfigurationMenu) + if(shouldLoadConfigMenu) { - configure(); + configMenu(); if(!pinExists && CONFIG(8)) newPin(); @@ -164,7 +164,7 @@ void main(void) if(CONFIG(7) && loadSplash()) pressed = HID_PAD; /* If L and R/A/Select or one of the single payload buttons are pressed, - chainload an external payload (the PIN, if any, has been verified)*/ + chainload an external payload */ bool shouldLoadPayload = (pressed & SINGLE_PAYLOAD_BUTTONS) || ((pressed & BUTTON_L1) && (pressed & L_PAYLOAD_BUTTONS)); if(shouldLoadPayload) loadPayload(pressed); @@ -177,8 +177,8 @@ void main(void) //If R is pressed, boot the non-updated NAND with the FIRM of the opposite one if(pressed & BUTTON_R1) { - nandType = (useSysAsDefault) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND; - firmSource = (useSysAsDefault) ? FIRMWARE_SYSNAND : FIRMWARE_EMUNAND; + nandType = useSysAsDefault ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND; + firmSource = useSysAsDefault ? FIRMWARE_SYSNAND : FIRMWARE_EMUNAND; } /* Else, boot the NAND the user set to autoboot or the opposite one, depending on L, @@ -221,7 +221,8 @@ void main(void) patchNativeFirm(firmVersion, nandType, emuHeader, isA9lh); break; case SAFE_FIRM: - patchSafeFirm(); + case NATIVE_FIRM2X: + patch2xNativeAndSafeFirm(); break; default: //Skip patching on unsupported O3DS AGB/TWL FIRMs @@ -247,9 +248,9 @@ static inline u32 loadFirm(FirmwareType *firmType, FirmwareSource firmSource) if(firmSource != FIRMWARE_SYSNAND || firmVersion < 9) error("An old unsupported NAND has been detected.\nLuma3DS is unable to boot it"); - if(BOOTCONFIG(5, 1)) error("SAFE_MODE is not supported on 2.x FIRM!"); + if(BOOTCONFIG(5, 1)) error("SAFE_MODE is not supported on 2.x FIRM"); - *firmType = SAFE_FIRM; + *firmType = NATIVE_FIRM2X; } //We can't boot a 3.x/4.x NATIVE_FIRM, load one from SD @@ -328,11 +329,10 @@ static inline void patchLegacyFirm(FirmwareType firmType) applyLegacyFirmPatches((u8 *)firm, firmType); - if(firmType == TWL_FIRM && CONFIG(5)) - patchTwlBg((u8 *)firm + section[1].offset); + if(firmType == TWL_FIRM && CONFIG(5)) patchTwlBg((u8 *)firm + section[1].offset); } -static inline void patchSafeFirm(void) +static inline void patch2xNativeAndSafeFirm(void) { u8 *arm9Section = (u8 *)firm + section[2].offset; @@ -344,7 +344,7 @@ static inline void patchSafeFirm(void) patchFirmWrites(arm9Section, section[2].size); } - else patchFirmWriteSafe(arm9Section, section[2].size); + else patchOldFirmWrites(arm9Section, section[2].size); } static inline void copySection0AndInjectSystemModules(void) diff --git a/source/firm.h b/source/firm.h index 12ae222..5053910 100644 --- a/source/firm.h +++ b/source/firm.h @@ -57,6 +57,6 @@ typedef enum ConfigurationStatus static inline u32 loadFirm(FirmwareType *firmType, FirmwareSource firmSource); static inline void patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 emuHeader, bool isA9lh); static inline void patchLegacyFirm(FirmwareType firmType); -static inline void patchSafeFirm(void); +static inline void patch2xNativeAndSafeFirm(void); static inline void copySection0AndInjectSystemModules(void); static inline void launchFirm(FirmwareType firmType); \ No newline at end of file diff --git a/source/patches.c b/source/patches.c index 957a9b0..6bfc7b8 100644 --- a/source/patches.c +++ b/source/patches.c @@ -48,7 +48,7 @@ static void findFreeK11Space(u8 *pos, u32 size) { if(freeK11Space == NULL) { - const u8 pattern[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + const u8 pattern[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; freeK11Space = memsearch(pos, pattern, size, 5) + 1; } @@ -113,23 +113,23 @@ void patchFirmWrites(u8 *pos, u32 size) off2[1] = writeBlock[1]; } -void patchFirmWriteSafe(u8 *pos, u32 size) +void patchOldFirmWrites(u8 *pos, u32 size) { - const u16 writeBlockSafe[2] = {0x2400, 0xE01D}; + const u16 writeBlockOld[2] = {0x2400, 0xE01D}; //Look for FIRM writing code const u8 pattern[] = {0x04, 0x1E, 0x1D, 0xDB}; u16 *off = (u16 *)memsearch(pos, pattern, size, 4); - off[0] = writeBlockSafe[0]; - off[1] = writeBlockSafe[1]; + off[0] = writeBlockOld[0]; + off[1] = writeBlockOld[1]; } void reimplementSvcBackdoor(u8 *pos, u32 size) { //Official implementation of svcBackdoor - const u8 svcBackdoor[40] = {0xFF, 0x10, 0xCD, 0xE3, //bic r1, sp, #0xff + const u8 svcBackdoor[40] = {0xFF, 0x10, 0xCD, 0xE3, //bic r1, sp, #0xff 0x0F, 0x1C, 0x81, 0xE3, //orr r1, r1, #0xf00 0x28, 0x10, 0x81, 0xE2, //add r1, r1, #0x28 0x00, 0x20, 0x91, 0xE5, //ldr r2, [r1] @@ -175,7 +175,7 @@ void implementSvcGetCFWInfo(u8 *pos, u32 size) } else isRelease = rev[4] == 0; - info->flags = 0 /* master branch */ | (((isRelease) ? 1 : 0) << 1) /* is release */; + info->flags = 0 /* master branch */ | ((isRelease ? 1 : 0) << 1) /* is release */; findArm11SvcTable(pos, size); @@ -235,15 +235,15 @@ void applyLegacyFirmPatches(u8 *pos, FirmwareType firmType) void patchTwlBg(u8 *pos) { - u8 *dst = pos + ((isN3DS) ? 0xFEA4 : 0xFCA0); + u8 *dst = pos + (isN3DS ? 0xFEA4 : 0xFCA0); memcpy(dst, twl_k11modules, twl_k11modules_size); //Install K11 hook u32 *off = (u32 *)memsearch(dst, "LAUN", twl_k11modules_size, 4); - *off = (isN3DS) ? 0xCDE88 : 0xCD5F8; //Dev SRL launcher offset + *off = isN3DS ? 0xCDE88 : 0xCD5F8; //Dev SRL launcher offset - u16 *src1 = (u16 *)(pos + ((isN3DS) ? 0xE38 : 0xE3C)), - *src2 = (u16 *)(pos + ((isN3DS) ? 0xE54 : 0xE58)); + u16 *src1 = (u16 *)(pos + (isN3DS ? 0xE38 : 0xE3C)), + *src2 = (u16 *)(pos + (isN3DS ? 0xE54 : 0xE58)); //Construct BLX instructions: src1[0] = 0xF000 | ((((u32)dst - (u32)src1 - 4) & (0xFFF << 11)) >> 12); diff --git a/source/patches.h b/source/patches.h index 24e82bb..8b6ccae 100644 --- a/source/patches.h +++ b/source/patches.h @@ -54,7 +54,7 @@ void patchSignatureChecks(u8 *pos, u32 size); void patchTitleInstallMinVersionCheck(u8 *pos, u32 size); void patchFirmlaunches(u8 *pos, u32 size, u32 process9MemAddr); void patchFirmWrites(u8 *pos, u32 size); -void patchFirmWriteSafe(u8 *pos, u32 size); +void patchOldFirmWrites(u8 *pos, u32 size); void reimplementSvcBackdoor(u8 *pos, u32 size); void implementSvcGetCFWInfo(u8 *pos, u32 size); void applyLegacyFirmPatches(u8 *pos, FirmwareType firmType); diff --git a/source/pin.c b/source/pin.c index aac0d41..10209fd 100644 --- a/source/pin.c +++ b/source/pin.c @@ -47,7 +47,7 @@ bool readPin(PINData *out) computePINHash(tmp, zeroes, 1); - return memcmp(out->testHash, tmp, 32) == 0; //test vector verification (SD card has (or hasn't) been used on another console) + return memcmp(out->testHash, tmp, 32) == 0; //Test vector verification (SD card has, or hasn't been used on another console) } static inline char PINKeyToLetter(u32 pressed) @@ -66,8 +66,8 @@ void newPin(void) drawString("Enter your NEW PIN: ", 10, 10, COLOR_WHITE); - // Set the default value as 0x00 so we can check if there are any unentered characters. - u8 __attribute__((aligned(4))) enteredPassword[16 * ((PIN_LENGTH + 15) / 16)] = {0}; // pad to AES block length + //Pad to AES block length with zeroes + u8 __attribute__((aligned(4))) enteredPassword[16 * ((PIN_LENGTH + 15) / 16)] = {0}; u32 cnt = 0; int charDrawPos = 20 * SPACING_X; @@ -84,10 +84,11 @@ void newPin(void) pressed &= PIN_BUTTONS & ~BUTTON_START; if(!pressed) continue; - char key = PINKeyToLetter(pressed); - enteredPassword[cnt++] = (u8)key; // add character to password. - // visualize character on screen. + char key = PINKeyToLetter(pressed); + enteredPassword[cnt++] = (u8)key; //Add character to password + + //Visualize character on screen drawCharacter(key, 10 + charDrawPos, 10, COLOR_WHITE); charDrawPos += 2 * SPACING_X; } @@ -123,7 +124,7 @@ void verifyPin(PINData *in) drawString("Press START to shutdown or enter pin to proceed.", 10, 10, COLOR_WHITE); drawString("Pin: ", 10, 10 + 2 * SPACING_Y, COLOR_WHITE); - // Set the default characters as 0x00 so we can check if there are any unentered characters. + //Pad to AES block length with zeroes u8 __attribute__((aligned(4))) enteredPassword[16 * ((PIN_LENGTH + 15) / 16)] = {0}; u32 cnt = 0; @@ -145,9 +146,9 @@ void verifyPin(PINData *in) if(!pressed) continue; char key = PINKeyToLetter(pressed); - enteredPassword[cnt++] = (u8)key; // add character to password. + enteredPassword[cnt++] = (u8)key; //Add character to password - // visualize character on screen. + //Visualize character on screen drawCharacter(key, 10 + charDrawPos, 10 + 2 * SPACING_Y, COLOR_WHITE); charDrawPos += 2 * SPACING_X; diff --git a/source/types.h b/source/types.h index a6946d5..223d38c 100644 --- a/source/types.h +++ b/source/types.h @@ -49,5 +49,6 @@ typedef enum FirmwareType NATIVE_FIRM = 0, TWL_FIRM = 1, AGB_FIRM = 2, - SAFE_FIRM = 3 + SAFE_FIRM = 3, + NATIVE_FIRM2X = 4 } FirmwareType; \ No newline at end of file