Skip to content

Commit

Permalink
Merge branch 'LumaTeam:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Popax21 authored Feb 28, 2023
2 parents 324eef8 + ef1072f commit b852138
Show file tree
Hide file tree
Showing 61 changed files with 2,113 additions and 848 deletions.
Binary file modified arm9/data/config_template.ini
Binary file not shown.
255 changes: 237 additions & 18 deletions arm9/source/config.c

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions arm9/source/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
#define MULTICONFIG(a) ((configData.multiConfig >> (2 * (a))) & 3)
#define BOOTCONFIG(a, b) ((configData.bootConfig >> (a)) & (b))

#define CONFIG_FILE "config.bin"
#define CONFIG_FILE "config.ini"
#define CONFIG_VERSIONMAJOR 3
#define CONFIG_VERSIONMINOR 1
#define CONFIG_VERSIONMINOR 6

#define BOOTCFG_NAND BOOTCONFIG(0, 7)
#define BOOTCFG_FIRM BOOTCONFIG(3, 7)
Expand All @@ -51,6 +51,7 @@ enum multiOptions
PIN,
NEWCPU,
AUTOBOOTMODE,
FORCEAUDIOOUTPUT,
};

enum singleOptions
Expand All @@ -59,6 +60,7 @@ enum singleOptions
USEEMUFIRM,
LOADEXTFIRMSANDMODULES,
PATCHGAMES,
REDIRECTAPPTHREADS,
PATCHVERSTRING,
SHOWGBABOOT,
PATCHUNITINFO,
Expand Down
33 changes: 28 additions & 5 deletions arm9/source/deliver_arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
#include "utils.h"
#include "memory.h"
#include "config.h"
#include "fs.h"

u8 *loadDeliverArg(void)
{
static u8 deliverArg[0x1000] = {0};
static __attribute__((aligned(8))) u8 deliverArg[0x1000] = {0};
static bool deliverArgLoaded = false;

if (!deliverArgLoaded)
Expand All @@ -50,8 +51,11 @@ u8 *loadDeliverArg(void)

// Validate deliver arg
u32 testPattern = *(u32 *)(deliverArg + 0x438);
u32 crc = *(u32 *)(deliverArg + 0x43C);
u32 *crcPtr = (u32 *)(deliverArg + 0x43C);
u32 crc = *crcPtr;
*crcPtr = 0; // clear crc field before calculation
u32 expectedCrc = crc32(deliverArg + 0x400, 0x140, 0xFFFFFFFF);
*crcPtr = crc;
if (testPattern != 0xFFFF || crc != expectedCrc)
memset(deliverArg, 0, 0x1000);
}
Expand Down Expand Up @@ -93,6 +97,7 @@ void commitDeliverArg(void)
if (mode == 0) // CTR mode
{
*(u32 *)(deliverArg + 0x438) = 0xFFFF;
*(u32 *)(deliverArg + 0x43C) = 0; // clear CRC field before calculating it
*(u32 *)(deliverArg + 0x43C) = crc32(deliverArg + 0x400, 0x140, 0xFFFFFFFF);
memcpy((void *)0x20000000, deliverArg, 0x1000);
}
Expand Down Expand Up @@ -185,9 +190,27 @@ bool configureHomebrewAutoboot(void)
u32 bootenv = CFG_BOOTENV;
u32 mode = bootenv >> 1;

u32 testPattern = *(u32 *)(deliverArg + 0x438);
if (mode != 0 || testPattern == 0xFFFF)
return false; // bail out if this isn't a coldboot/plain reboot
// NS always writes a valid deliver arg on reboot, no matter what.
// Check if it is empty, and, of course, bail out if we aren't rebooting from
// NATIVE_FIRM.
// Checking if it is empty is necessary to let us reboot from autobooted hbmenu
// to hbmenu.

if (mode != 0)
return false;
else if (bootenv == 1)
{
for (u32 i = 0; i < 0x410; i++)
{
if (deliverArg[i] != 0)
return false;
}
for (u32 i = 0x440; i < 0x1000; i++)
{
if (deliverArg[i] != 0)
return false;
}
}

switch (MULTICONFIG(AUTOBOOTMODE))
{
Expand Down
9 changes: 8 additions & 1 deletion arm9/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,19 @@ void main(int argc, char **argv, u32 magicWord)
nandType = FIRMWARE_SYSNAND;
firmSource = (BOOTCFG_NAND != 0) == (BOOTCFG_FIRM != 0) ? FIRMWARE_SYSNAND : (FirmwareSource)BOOTCFG_FIRM;

//Prevent multiple boot options-forcing
// Prevent multiple boot options-forcing
// This bit is a bit weird. Basically, as you return to Home Menu by pressing either
// the HOME or POWER button, nandType will be overridden to "SysNAND" (needed). But,
// if you reboot again (e.g. via Rosalina menu), it'll use your default settings.
if(nandType != BOOTCFG_NAND || firmSource != BOOTCFG_FIRM) isNoForceFlagSet = true;

goto boot;
}

// Configure homebrew autoboot (if deliver arg ends up not containing anything)
if (bootenv == 1 && MULTICONFIG(AUTOBOOTMODE) != 0)
configureHomebrewAutoboot();

/* Force the last used boot options if doing autolaunch from TWL, or unless a button is pressed
or the no-forcing flag is set */
if(validTlnc || !(pressed || BOOTCFG_NOFORCEFLAG))
Expand Down
8 changes: 6 additions & 2 deletions arm9/source/patches.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ u32 installK11Extension(u8 *pos, u32 size, bool needToInitSd, u32 baseK11VA, u32
u32 splashDurationMsec;
u64 hbldr3dsxTitleId;
u32 rosalinaMenuCombo;
u16 screenFiltersCct;
s16 ntpTzOffetMinutes;

ScreenFiltersCfgData topScreenFilter;
ScreenFiltersCfgData bottomScreenFilter;

u64 autobootTwlTitleId;
u8 autobootCtrAppmemtype;
} info;
Expand Down Expand Up @@ -209,8 +212,9 @@ u32 installK11Extension(u8 *pos, u32 size, bool needToInitSd, u32 baseK11VA, u32
info->splashDurationMsec = configData.splashDurationMsec;
info->hbldr3dsxTitleId = configData.hbldr3dsxTitleId;
info->rosalinaMenuCombo = configData.rosalinaMenuCombo;
info->screenFiltersCct = configData.screenFiltersCct;
info->ntpTzOffetMinutes = configData.ntpTzOffetMinutes;
info->topScreenFilter = configData.topScreenFilter;
info->bottomScreenFilter = configData.bottomScreenFilter;
info->autobootTwlTitleId = configData.autobootTwlTitleId;
info->autobootCtrAppmemtype = configData.autobootCtrAppmemtype;
info->versionMajor = VERSION_MAJOR;
Expand Down
14 changes: 12 additions & 2 deletions arm9/source/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,27 @@ typedef volatile s64 vs64;
#define ISN3DS (CFG11_SOCINFO & 2)
#define ISDEVUNIT (CFG_UNITINFO != 0)

typedef struct {
typedef struct ScreenFiltersCfgData {
u16 cct;
bool invert;
s64 gammaEnc;
s64 contrastEnc;
s64 brightnessEnc;
} ScreenFiltersCfgData;

typedef struct CfgData {
u16 formatVersionMajor, formatVersionMinor;

u32 config, multiConfig, bootConfig;
u32 splashDurationMsec;

u64 hbldr3dsxTitleId;
u32 rosalinaMenuCombo;
u16 screenFiltersCct;
s16 ntpTzOffetMinutes;

ScreenFiltersCfgData topScreenFilter;
ScreenFiltersCfgData bottomScreenFilter;

u64 autobootTwlTitleId;
u8 autobootCtrAppmemtype;
} CfgData;
Expand Down
2 changes: 2 additions & 0 deletions k11_extension/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum multiOptions
PIN,
NEWCPU,
AUTOBOOTMODE,
FORCEAUDIOOUTPUT,
};

enum singleOptions
Expand All @@ -30,6 +31,7 @@ enum singleOptions
USEEMUFIRM,
LOADEXTFIRMSANDMODULES,
PATCHGAMES,
REDIRECTAPPTHREADS,
PATCHVERSTRING,
SHOWGBABOOT,
PATCHUNITINFO,
Expand Down
14 changes: 13 additions & 1 deletion k11_extension/include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern void (*KScheduler__AdjustThread)(KScheduler *this, KThread *thread, u32 o
extern void (*KScheduler__AttemptSwitchingThreadContext)(KScheduler *this);

extern Result (*ControlMemory)(u32 *addrOut, u32 addr0, u32 addr1, u32 size, MemOp op, MemPerm perm, bool isLoader);
extern Result (*CreateThread)(Handle *outThreadHandle, u32 ep, u32 arg, u32 stackTop, s32 priority, s32 processorId);
extern void (*SleepThread)(s64 ns);
extern Result (*CloseHandle)(Handle handle);
extern Result (*GetHandleInfo)(s64 *out, Handle handle, u32 type);
Expand Down Expand Up @@ -114,6 +115,14 @@ extern void (*initFPU)(void);
extern void (*mcuReboot)(void);
extern void (*coreBarrier)(void);

typedef struct ScreenFiltersCfgData {
u16 cct;
bool invert;
s64 gammaEnc;
s64 contrastEnc;
s64 brightnessEnc;
} ScreenFiltersCfgData;

typedef struct CfwInfo
{
char magic[4];
Expand All @@ -130,8 +139,11 @@ typedef struct CfwInfo
u32 splashDurationMsec;
u64 hbldr3dsxTitleId;
u32 rosalinaMenuCombo;
u16 screenFiltersCct;
s16 ntpTzOffetMinutes;

ScreenFiltersCfgData topScreenFilter;
ScreenFiltersCfgData bottomScreenFilter;

u64 autobootTwlTitleId;
u8 autobootCtrAppmemtype;
} CfwInfo;
Expand Down
9 changes: 8 additions & 1 deletion k11_extension/include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ offsetof(classname##O3DSPre8x, field)))
#define KPROCESSHWINFO_GET_RVALUE(obj, field) *(KPROCESSHWINFO_GET_PTR(obj, field))
#define KPROCESSHWINFO_GET_RVALUE_TYPE(type, obj, field) *(KPROCESSHWINFO_GET_PTR_TYPE(type, obj, field))

extern u32 pidOffsetKProcess, hwInfoOffsetKProcess, codeSetOffsetKProcess, handleTableOffsetKProcess, debugOffsetKProcess;
extern u32 pidOffsetKProcess, hwInfoOffsetKProcess, codeSetOffsetKProcess, handleTableOffsetKProcess, debugOffsetKProcess, flagsKProcess;

static inline u32 idOfProcess(KProcess *process)
{
Expand Down Expand Up @@ -1195,6 +1195,13 @@ static inline KDebug *debugOfProcess(KProcess *process)
return debug;
}

static inline u32 flagsOfProcess(KProcess *process)
{
u32 flags;
memcpy(&flags, (const u8 *)process + flagsKProcess, 4);
return flags;
}

static inline const char *classNameOfAutoObject(KAutoObject *object)
{
const char *name;
Expand Down
32 changes: 32 additions & 0 deletions k11_extension/include/svc/CreateThread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016-2023 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/

#include "utils.h"
#include "kernel.h"
#include "svc.h"

Result CreateThreadHookWrapper(Handle *outThreadHandle, u32 ep, u32 arg, u32 stackTop, s32 priority, s32 processorId);
Result CreateThreadHook(Handle *outThreadHandle, u32 ep, u32 arg, u32 stackTop, s32 priority, s32 processorId);
3 changes: 2 additions & 1 deletion k11_extension/source/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void (*KScheduler__AdjustThread)(KScheduler *this, KThread *thread, u32 oldSched
void (*KScheduler__AttemptSwitchingThreadContext)(KScheduler *this);

Result (*ControlMemory)(u32 *addrOut, u32 addr0, u32 addr1, u32 size, MemOp op, MemPerm perm, bool isLoader);
Result (*CreateThread)(Handle *outThreadHandle, u32 ep, u32 arg, u32 stackTop, s32 priority, s32 processorId);
void (*SleepThread)(s64 ns);
Result (*CloseHandle)(Handle handle);
Result (*GetHandleInfo)(s64 *out, Handle handle, u32 type);
Expand Down Expand Up @@ -114,4 +115,4 @@ u32 stolenSystemMemRegionSize;
vu32 rosalinaState;
bool hasStartedRosalinaNetworkFuncsOnce;

u32 pidOffsetKProcess, hwInfoOffsetKProcess, codeSetOffsetKProcess, handleTableOffsetKProcess, debugOffsetKProcess;
u32 pidOffsetKProcess, hwInfoOffsetKProcess, codeSetOffsetKProcess, handleTableOffsetKProcess, debugOffsetKProcess, flagsKProcess;
2 changes: 2 additions & 0 deletions k11_extension/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ void configHook(vu8 *cfgPage)
codeSetOffsetKProcess = KPROCESS_OFFSETOF(codeSet);
handleTableOffsetKProcess = KPROCESS_OFFSETOF(handleTable);
debugOffsetKProcess = KPROCESS_OFFSETOF(debug);
flagsKProcess = KPROCESS_OFFSETOF(kernelFlags);
}

static void findUsefulSymbols(void)
Expand Down Expand Up @@ -240,6 +241,7 @@ static void findUsefulSymbols(void)
// The official prototype of ControlMemory doesn't have that extra param'
ControlMemory = (Result (*)(u32 *, u32, u32, u32, MemOp, MemPerm, bool))
decodeArmBranch((u32 *)officialSVCs[0x01] + 5);
CreateThread = (Result (*)(Handle *, u32, u32, u32, s32, s32))decodeArmBranch((u32 *)officialSVCs[0x08] + 5);
SleepThread = (void (*)(s64))officialSVCs[0x0A];
CloseHandle = (Result (*)(Handle))officialSVCs[0x23];
GetHandleInfo = (Result (*)(s64 *, Handle, u32))decodeArmBranch((u32 *)officialSVCs[0x29] + 3);
Expand Down
3 changes: 3 additions & 0 deletions k11_extension/source/svc.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "synchronization.h"
#include "svc.h"
#include "svc/ControlMemory.h"
#include "svc/CreateThread.h"
#include "svc/GetHandleInfo.h"
#include "svc/GetSystemInfo.h"
#include "svc/GetProcessInfo.h"
Expand Down Expand Up @@ -63,6 +64,8 @@ void buildAlteredSvcTable(void)

alteredSvcTable[0x01] = ControlMemoryHookWrapper;

if (isN3DS)
alteredSvcTable[0x08] = CreateThreadHookWrapper;
alteredSvcTable[0x29] = GetHandleInfoHookWrapper;
alteredSvcTable[0x2A] = GetSystemInfoHookWrapper;
alteredSvcTable[0x2B] = GetProcessInfoHookWrapper;
Expand Down
37 changes: 37 additions & 0 deletions k11_extension/source/svc/CreateThread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016-2023 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/

#include "svc/CreateThread.h"

Result CreateThreadHook(Handle *outThreadHandle, u32 ep, u32 arg, u32 stackTop, s32 priority, s32 processorId)
{
u32 flags = flagsOfProcess(currentCoreContext->objectContext.currentProcess);
if (isN3DS && CONFIG(REDIRECTAPPTHREADS) && processorId == 1 && (flags & 0xF00) == 0x100)
processorId = 2;

return CreateThread(outThreadHandle, ep, arg, stackTop, priority, processorId);
}

Loading

0 comments on commit b852138

Please sign in to comment.