From 8636a74ba48fe60747498708bc00b4d48d1efc11 Mon Sep 17 00:00:00 2001
From: TuxSH <tuxsh@sfr.fr>
Date: Thu, 18 May 2017 01:07:29 +0200
Subject: [PATCH] Revert "Minor stuff"

This reverts commit ba88ec25f6780519835e45ee5c3ec272cea6fbcf.

Conflicts:
	source/firm.c
---
 source/crypto.c | 34 +++++++++++++++++++---------------
 source/firm.c   |  9 +++++++++
 source/main.c   |  6 +++++-
 3 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/source/crypto.c b/source/crypto.c
index 537f932..647d6b3 100755
--- a/source/crypto.c
+++ b/source/crypto.c
@@ -464,21 +464,26 @@ void kernel9Loader(Arm9Bin *arm9Section)
 {
     //Determine the kernel9loader version
     u32 k9lVersion;
-    switch(arm9Section->magic[3])
+    if(arm9Section == NULL)
+        k9lVersion = 2;
+    else
     {
-        case 0xFF:
-            k9lVersion = 0;
-            break;
-        case '1':
-            k9lVersion = 1;
-            break;
-        default:
-            k9lVersion = 2;
-            break;
+        switch(arm9Section->magic[3])
+        {
+            case 0xFF:
+                k9lVersion = 0;
+                break;
+            case '1':
+                k9lVersion = 1;
+                break;
+            default:
+                k9lVersion = 2;
+                break;
+        }
     }
 
     u32 *startOfArm9Bin = (u32 *)((u8 *)arm9Section + 0x800);
-    bool needToDecrypt = *startOfArm9Bin != 0x47704770 && *startOfArm9Bin != 0xB0862000;
+    bool needToDecrypt = arm9Section != NULL && *startOfArm9Bin != 0x47704770 && *startOfArm9Bin != 0xB0862000;
 
     //Set 0x11 keyslot
     __attribute__((aligned(4))) const u8 key1s[2][AES_BLOCK_SIZE] = {
@@ -544,10 +549,6 @@ void kernel9Loader(Arm9Bin *arm9Section)
         aes_setkey(slot, decKey, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
     }
 
-    if(!ISSIGHAX) return;
-
-    twlConsoleInfoInit();
-
     if(k9lVersion == 2)
     {
         aes_setkey(0x11, key1s[ISDEVUNIT ? 1 : 0], AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL);
@@ -555,6 +556,9 @@ void kernel9Loader(Arm9Bin *arm9Section)
         aes(decKey, keyBlocks[0], 1, NULL, AES_ECB_DECRYPT_MODE, 0);
         aes_setkey(0x18, decKey, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
     }
+
+    if(ISSIGHAX)
+        twlConsoleInfoInit();
 }
 
 void computePinHash(u8 *outbuf, const u8 *inbuf)
diff --git a/source/firm.c b/source/firm.c
index 59c415d..3ae04b9 100755
--- a/source/firm.c
+++ b/source/firm.c
@@ -124,6 +124,9 @@ u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 emuHeader, boo
         kernel9Loader((Arm9Bin *)arm9Section);
         firm->arm9Entry = (u8 *)0x801B01C;
     }
+
+    if(!ISN3DS)
+        kernel9Loader(NULL); //Just set the N3DS 9.6+ keys even on O3DS
     
     //Find the Process9 .code location, size and memory address
     u32 process9Size,
@@ -224,6 +227,8 @@ u32 patchTwlFirm(u32 firmVersion, bool doUnitinfoPatch)
         kernel9Loader((Arm9Bin *)arm9Section);
         firm->arm9Entry = (u8 *)0x801301C;
     }
+    else
+        kernel9Loader(NULL); //Just set the keys
 
     //Find the Process9 .code location, size and memory address
     u32 process9Size,
@@ -257,6 +262,8 @@ u32 patchAgbFirm(bool doUnitinfoPatch)
         kernel9Loader((Arm9Bin *)arm9Section);
         firm->arm9Entry = (u8 *)0x801301C;
     }
+    else
+        kernel9Loader(NULL); //Just set the keys
 
     //Find the Process9 .code location, size and memory address
     u32 process9Size,
@@ -285,6 +292,8 @@ u32 patch1x2xNativeAndSafeFirm(bool enableExceptionHandlers)
         kernel9Loader((Arm9Bin *)arm9Section);
         firm->arm9Entry = (u8 *)0x801B01C;
     }
+    else
+        kernel9Loader(NULL); //Just set the keys
 
     //Find the Process9 .code location, size and memory address
     u32 process9Size,
diff --git a/source/main.c b/source/main.c
index acb84d8..5e6ab25 100644
--- a/source/main.c
+++ b/source/main.c
@@ -158,7 +158,11 @@ void main(void)
         pressed = HID_PAD;
     }
     else if(((pressed & SINGLE_PAYLOAD_BUTTONS) && !(pressed & (BUTTON_L1 | BUTTON_R1 | BUTTON_A))) ||
-            ((pressed & L_PAYLOAD_BUTTONS) && (pressed & BUTTON_L1))) loadPayload(pressed, NULL);
+            ((pressed & L_PAYLOAD_BUTTONS) && (pressed & BUTTON_L1)))
+    {
+        kernel9Loader(NULL);
+        loadPayload(pressed, NULL);
+    }
 
     if(splashMode == 2) loadSplash();