diff --git a/Makefile b/Makefile index 086818f..42811f9 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,19 @@ ifneq ($(strip $(shell firmtool -v 2>&1 | grep usage)),) $(error "Please install firmtool v1.1 or greater") endif +# Disable kext and firmlaunch patches, all custom sysmodules except Loader, enable PASLR. +# Dangerous. Don't enable this unless you know what you're doing! +export BUILD_FOR_EXPLOIT_DEV ?= 0 + +# Build with O0 & frame pointer information for use with GDB +export BUILD_FOR_GDB ?= 0 + +# Default 3DSX TitleID for hb:ldr +export HBLDR_DEFAULT_3DSX_TID ?= 000400000D921E00 + +# What to call the title corresponding to HBLDR_DEFAULT_3DSX_TID +export HBLDR_DEFAULT_3DSX_TITLE_NAME ?= "hblauncher_loader" + NAME := $(notdir $(CURDIR)) REVISION := $(shell git describe --tags --match v[0-9]* --abbrev=8 | sed 's/-[0-9]*-g/-/') diff --git a/arm11/Makefile b/arm11/Makefile index 61b8e4a..1940b76 100644 --- a/arm11/Makefile +++ b/arm11/Makefile @@ -26,7 +26,7 @@ INCLUDES := include include/svc # options for code generation #--------------------------------------------------------------------------------- ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft -DEFINES := -DARM11 -D_3DS +DEFINES := -DARM11 -D__3DS__ CFLAGS := -g -std=gnu11 -Wall -Wextra -Werror -O2 -mword-relocations \ -fomit-frame-pointer -ffunction-sections -fdata-sections \ diff --git a/arm9/Makefile b/arm9/Makefile index 843f8ad..04a15c3 100644 --- a/arm9/Makefile +++ b/arm9/Makefile @@ -35,6 +35,9 @@ ifeq ($(strip $(shell git describe --tags --match v[0-9]* | grep -)),) export IS_RELEASE := 1 endif +# Default 3DSX TitleID for hb:ldr (note: also defined in top-level Makefile) +export HBLDR_DEFAULT_3DSX_TID ?= 000400000D921E00 + #--------------------------------------------------------------------------------- # TARGET is the name of the output # BUILD is the directory where object files & intermediate files will be placed @@ -53,7 +56,12 @@ INCLUDES := include # options for code generation #--------------------------------------------------------------------------------- ARCH := -marm -march=armv5te -mtune=arm946e-s -DEFINES := -DARM9 -D_3DS + +ifeq ($(BUILD_FOR_EXPLOIT_DEV),1) + DEFINES := -DARM9 -D__3DS__ -DHBLDR_DEFAULT_3DSX_TID="0x$(HBLDR_DEFAULT_3DSX_TID)ULL" -DBUILD_FOR_EXPLOIT_DEV=1 +else + DEFINES := -DARM9 -D__3DS__ -DHBLDR_DEFAULT_3DSX_TID="0x$(HBLDR_DEFAULT_3DSX_TID)ULL" +endif FALSEPOSITIVES := -Wno-array-bounds -Wno-stringop-overflow -Wno-stringop-overread CFLAGS := -g -std=gnu11 -Wall -Wextra -Werror -O2 -mword-relocations \ diff --git a/arm9/source/config.h b/arm9/source/config.h index cf64164..23b4a04 100644 --- a/arm9/source/config.h +++ b/arm9/source/config.h @@ -28,7 +28,6 @@ #include "types.h" -#define HBLDR_DEFAULT_3DSX_TID 0x000400000D921E00ull #define AUTOBOOT_DEFAULT_TWL_TID 0x0003000448424C41ull #define CONFIG(a) (((configData.config >> (a)) & 1) != 0) diff --git a/arm9/source/firm.c b/arm9/source/firm.c index 6088bbe..c0b4f33 100755 --- a/arm9/source/firm.c +++ b/arm9/source/firm.c @@ -352,8 +352,7 @@ static inline void mergeSection0(FirmwareType firmType, u32 firmVersion, bool lo 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; + u8 *arm9Section = (u8 *)firm + firm->section[2].offset; if(ISN3DS) { @@ -367,22 +366,27 @@ u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStora process9MemAddr; u8 *process9Offset = getProcess9Info(arm9Section, firm->section[2].size, &process9Size, &process9MemAddr); - //Find the Kernel11 SVC table and handler, exceptions page and free space locations - u32 baseK11VA; - u8 *freeK11Space; - u32 *arm11SvcHandler, - *arm11ExceptionsPage, - *arm11SvcTable = getKernel11Info(arm11Section1, firm->section[1].size, &baseK11VA, &freeK11Space, &arm11SvcHandler, &arm11ExceptionsPage); - u32 kernel9Size = (u32)(process9Offset - arm9Section) - sizeof(Cxi) - 0x200, ret = 0; +#ifndef BUILD_FOR_EXPLOIT_DEV //Skip on FIRMs < 4.0 if(ISN3DS || firmVersion >= 0x1D) { + //Find the Kernel11 SVC table and handler, exceptions page and free space locations + u8 *arm11Section1 = (u8 *)firm + firm->section[1].offset; + u32 baseK11VA; + u8 *freeK11Space; + u32 *arm11SvcHandler, + *arm11ExceptionsPage, + *arm11SvcTable = getKernel11Info(arm11Section1, firm->section[1].size, &baseK11VA, &freeK11Space, &arm11SvcHandler, &arm11ExceptionsPage); + ret += installK11Extension(arm11Section1, firm->section[1].size, needToInitSd, baseK11VA, arm11ExceptionsPage, &freeK11Space); ret += patchKernel11(arm11Section1, firm->section[1].size, baseK11VA, arm11SvcTable, arm11ExceptionsPage); } +#else + (void)needToInitSd; +#endif //Apply signature patches ret += patchSignatureChecks(process9Offset, process9Size); @@ -393,8 +397,10 @@ u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStora //Apply FIRM0/1 writes patches on SysNAND to protect A9LH else if(isFirmProtEnabled) ret += patchFirmWrites(process9Offset, process9Size); +#ifndef BUILD_FOR_EXPLOIT_DEV //Apply firmlaunch patches ret += patchFirmlaunches(process9Offset, process9Size, process9MemAddr); +#endif //Apply dev unit check patches related to NCCH encryption if(!ISDEVUNIT) diff --git a/k11_extension/Makefile b/k11_extension/Makefile index a4c0b3d..3a7f833 100644 --- a/k11_extension/Makefile +++ b/k11_extension/Makefile @@ -27,7 +27,7 @@ INCLUDES := include include/svc #--------------------------------------------------------------------------------- # -mgeneral-regs-only so that the C code is guaranteed not to use FPU regs ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft -mgeneral-regs-only -DEFINES := -DARM11 -D_3DS +DEFINES := -DARM11 -D__3DS__ FALSEPOSITIVES := -Wno-array-bounds -Wno-stringop-overflow -Wno-stringop-overread CFLAGS := -g -std=gnu11 -Wall -Wextra -Werror -O2 -mword-relocations \ diff --git a/sysmodules/Makefile b/sysmodules/Makefile index bdd18a5..2477587 100644 --- a/sysmodules/Makefile +++ b/sysmodules/Makefile @@ -1,9 +1,15 @@ -SUBFOLDERS := loader sm pm pxi rosalina -CXIS := $(foreach dir, $(SUBFOLDERS), $(dir)/$(dir).cxi) +ifeq ($(BUILD_FOR_EXPLOIT_DEV),1) + # Keep Loader so that we can execute 3DSX + SUBFOLDERS := loader +else + SUBFOLDERS := loader sm pm pxi rosalina +endif -.PHONY: all clean $(SUBFOLDERS) +CXIS := $(foreach dir, $(SUBFOLDERS), $(dir)/$(dir).cxi) -all: sysmodules.bin +.PHONY: all clean $(SUBFOLDERS) + +all: sysmodules.bin clean: @$(foreach dir, $(SUBFOLDERS), $(MAKE) -C $(dir) clean &&) true diff --git a/sysmodules/loader/Makefile b/sysmodules/loader/Makefile index c8eb15d..82ac5cf 100755 --- a/sysmodules/loader/Makefile +++ b/sysmodules/loader/Makefile @@ -9,6 +9,9 @@ endif TOPDIR ?= $(CURDIR) include $(DEVKITARM)/3ds_rules +# Default 3DSX TitleID for hb:ldr (note: also defined in top-level Makefile) +export HBLDR_DEFAULT_3DSX_TID ?= 000400000D921E00 + #--------------------------------------------------------------------------------- # TARGET is the name of the output # BUILD is the directory where object files & intermediate files will be placed @@ -26,13 +29,35 @@ INCLUDES := include # options for code generation #--------------------------------------------------------------------------------- ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft -DEFINES := -D__3DS__ -COMMON_FLAGS = -g -Wall -Wextra -Werror -O2 -mword-relocations \ - -fomit-frame-pointer -ffunction-sections -fdata-sections \ - $(ARCH) $(DEFINES) $(INCLUDE) +ifeq ($(BUILD_FOR_EXPLOIT_DEV),1) + DEFINES := -D__3DS__ -DHBLDR_DEFAULT_3DSX_TID="0x$(HBLDR_DEFAULT_3DSX_TID)ULL" -DBUILD_FOR_EXPLOIT_DEV=1 +else + DEFINES := -D__3DS__ -DHBLDR_DEFAULT_3DSX_TID="0x$(HBLDR_DEFAULT_3DSX_TID)ULL" +endif -CFLAGS := -std=gnu11 $(COMMON_FLAGS) +ifeq ($(BUILD_FOR_GDB),1) + OPTFLAGS := -O0 + LIBS := -lctrud +else + OPTFLAGS := -O2 -fomit-frame-pointer + LIBS := -lctru +endif + +COMMON_FLAGS =\ + -g \ + -Wall \ + -Wextra \ + -Werror \ + -mword-relocations \ + -ffunction-sections \ + -fdata-sections \ + $(OPTFLAGS) \ + $(ARCH) \ + $(DEFINES) \ + $(INCLUDE) + +CFLAGS := -std=gnu11 $(COMMON_FLAGS) CXXFLAGS := -fno-rtti -fno-exceptions -std=gnu++17 $(COMMON_FLAGS) diff --git a/sysmodules/loader/source/loader.c b/sysmodules/loader/source/loader.c index 1c0795a..9884f2d 100644 --- a/sysmodules/loader/source/loader.c +++ b/sysmodules/loader/source/loader.c @@ -5,7 +5,6 @@ #include "ifile.h" #include "util.h" #include "hbldr.h" -#include "luma_shared_config.h" extern u32 config, multiConfig, bootConfig; extern bool isN3DS, isSdMode; diff --git a/sysmodules/loader/source/luma_shared_config.h b/sysmodules/loader/source/luma_shared_config.h index 31cb490..ac7eab2 100644 --- a/sysmodules/loader/source/luma_shared_config.h +++ b/sysmodules/loader/source/luma_shared_config.h @@ -18,9 +18,6 @@ #include <3ds/types.h> -/// Default TitleID for 3DSX loading -#define HBLDR_DEFAULT_3DSX_TID 0x000400000D921E00ULL - /// Luma shared config type. typedef struct LumaSharedConfig { u64 hbldr_3dsx_tid; ///< Title ID to use for 3DSX loading. diff --git a/sysmodules/loader/source/paslr.c b/sysmodules/loader/source/paslr.c index 43de7b5..3e1f67f 100644 --- a/sysmodules/loader/source/paslr.c +++ b/sysmodules/loader/source/paslr.c @@ -47,7 +47,7 @@ static bool needsPaslr(u32 *outRegion, const ExHeader_Info *exhi) *outRegion = region; -#ifdef LOADER_ENABLE_PASLR +#if defined(LOADER_ENABLE_PASLR) || defined(BUILD_FOR_EXPLOIT_DEV) // Only applications and system applets (HM, Internet Browser...) are eligible for PASLR if (region != MEMOP_REGION_APP && region != MEMOP_REGION_SYSTEM) return false; diff --git a/sysmodules/pm/Makefile b/sysmodules/pm/Makefile index a750339..79493fd 100644 --- a/sysmodules/pm/Makefile +++ b/sysmodules/pm/Makefile @@ -28,19 +28,33 @@ INCLUDES := include ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft DEFINES := -D__3DS__ -CFLAGS := -g -std=gnu11 -Wall -Wextra -Werror -Os -mword-relocations \ - -fomit-frame-pointer -ffunction-sections -fdata-sections \ - $(ARCH) $(DEFINES) +ifeq ($(BUILD_FOR_GDB),1) + OPTFLAGS := -O0 + LIBS := -lctrud +else + OPTFLAGS := -O2 -fomit-frame-pointer + LIBS := -lctru +endif -CFLAGS += $(INCLUDE) +CFLAGS :=\ + -g \ + -std=gnu11 \ + -Wall \ + -Wextra \ + -Werror \ + -mword-relocations \ + -ffunction-sections \ + -fdata-sections \ + $(OPTFLAGS) \ + $(ARCH) \ + $(DEFINES) \ + $(INCLUDE) CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 ASFLAGS := -g $(ARCH) LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map),-wrap,exit -LIBS := -lctru - #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing # include and lib diff --git a/sysmodules/pxi/Makefile b/sysmodules/pxi/Makefile index 1cc0b23..58b3251 100644 --- a/sysmodules/pxi/Makefile +++ b/sysmodules/pxi/Makefile @@ -28,9 +28,27 @@ INCLUDES := include ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft DEFINES := -D__3DS__ -CFLAGS := -g -std=gnu11 -Wall -Wextra -Werror -O2 -mword-relocations \ - -fomit-frame-pointer -ffunction-sections -fdata-sections \ - $(ARCH) $(DEFINES) +ifeq ($(BUILD_FOR_GDB),1) + OPTFLAGS := -O0 + LIBS := -lctrud +else + OPTFLAGS := -O2 -fomit-frame-pointer + LIBS := -lctru +endif + +CFLAGS :=\ + -g \ + -std=gnu11 \ + -Wall \ + -Wextra \ + -Werror \ + -mword-relocations \ + -ffunction-sections \ + -fdata-sections \ + $(OPTFLAGS) \ + $(ARCH) \ + $(DEFINES) \ + $(INCLUDE) CFLAGS += $(INCLUDE) @@ -39,8 +57,6 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 ASFLAGS := -g $(ARCH) LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map),-wrap,exit -LIBS := -lctru - #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing # include and lib diff --git a/sysmodules/rosalina/Makefile b/sysmodules/rosalina/Makefile index 016b11a..99f666b 100644 --- a/sysmodules/rosalina/Makefile +++ b/sysmodules/rosalina/Makefile @@ -9,6 +9,12 @@ endif TOPDIR ?= $(CURDIR) include $(DEVKITARM)/3ds_rules +# Default 3DSX TitleID for hb:ldr (note: also defined in top-level Makefile) +export HBLDR_DEFAULT_3DSX_TID ?= 000400000D921E00 + +# What to call the title corresponding to HBLDR_DEFAULT_3DSX_TID (note: also defined in top-level Makefile) +export HBLDR_DEFAULT_3DSX_TITLE_NAME ?= "hblauncher_loader" + #--------------------------------------------------------------------------------- # TARGET is the name of the output # BUILD is the directory where object files & intermediate files will be placed @@ -26,7 +32,7 @@ INCLUDES := include include/gdb include/menus include/redshift # options for code generation #--------------------------------------------------------------------------------- ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft -DEFINES := -D__3DS__ +DEFINES := -D__3DS__ -DHBLDR_DEFAULT_3DSX_TID="0x$(HBLDR_DEFAULT_3DSX_TID)ULL" -DHBLDR_DEFAULT_3DSX_TITLE_NAME="\"$(HBLDR_DEFAULT_3DSX_TITLE_NAME)\"" FALSEPOSITIVES := -Wno-array-bounds -Wno-stringop-overflow -Wno-stringop-overread CFLAGS := -g -std=gnu11 -Wall -Wextra -Werror -Wno-unused-value -Os -mword-relocations \ diff --git a/sysmodules/rosalina/include/luma_shared_config.h b/sysmodules/rosalina/include/luma_shared_config.h index 222af23..ac7eab2 100644 --- a/sysmodules/rosalina/include/luma_shared_config.h +++ b/sysmodules/rosalina/include/luma_shared_config.h @@ -18,9 +18,6 @@ #include <3ds/types.h> -/// Default TitleID for 3DSX loading -#define HBLDR_DEFAULT_3DSX_TID 0x000400000D921E00ULL - /// Luma shared config type. typedef struct LumaSharedConfig { u64 hbldr_3dsx_tid; ///< Title ID to use for 3DSX loading. diff --git a/sysmodules/rosalina/source/menus/miscellaneous.c b/sysmodules/rosalina/source/menus/miscellaneous.c index bf76139..2c12907 100644 --- a/sysmodules/rosalina/source/menus/miscellaneous.c +++ b/sysmodules/rosalina/source/menus/miscellaneous.c @@ -120,12 +120,20 @@ Menu miscellaneousMenu = { }; int lastNtpTzOffset = 0; +static inline bool compareTids(u64 tidA, u64 tidB) +{ + // Just like p9 clears them, ignore platform/N3DS bits + return ((tidA ^ tidB) & ~0xF0000000ull) == 0; +} + void MiscellaneousMenu_SwitchBoot3dsxTargetTitle(void) { Result res; char failureReason[64]; + u64 currentTid = Luma_SharedConfig->hbldr_3dsx_tid; + u64 newTid = currentTid; - if(Luma_SharedConfig->hbldr_3dsx_tid == HBLDR_DEFAULT_3DSX_TID) + if(compareTids(currentTid, HBLDR_DEFAULT_3DSX_TID)) { FS_ProgramInfo progInfo; u32 pid; @@ -133,8 +141,8 @@ void MiscellaneousMenu_SwitchBoot3dsxTargetTitle(void) res = PMDBG_GetCurrentAppInfo(&progInfo, &pid, &launchFlags); if(R_SUCCEEDED(res)) { + newTid = progInfo.programId; Luma_SharedConfig->hbldr_3dsx_tid = progInfo.programId; - miscellaneousMenu.items[0].title = "Switch the hb. title to hblauncher_loader"; } else { @@ -145,10 +153,15 @@ void MiscellaneousMenu_SwitchBoot3dsxTargetTitle(void) else { res = 0; - Luma_SharedConfig->hbldr_3dsx_tid = HBLDR_DEFAULT_3DSX_TID; - miscellaneousMenu.items[0].title = "Switch the hb. title to the current app."; + newTid = HBLDR_DEFAULT_3DSX_TID; } + Luma_SharedConfig->hbldr_3dsx_tid = newTid; + if (compareTids(newTid, HBLDR_DEFAULT_3DSX_TID)) + miscellaneousMenu.items[0].title = "Switch the hb. title to the current app."; + else + miscellaneousMenu.items[0].title = "Switch the hb. title to " HBLDR_DEFAULT_3DSX_TITLE_NAME; + Draw_Lock(); Draw_ClearFramebuffer(); Draw_FlushFramebuffer(); diff --git a/sysmodules/sm/Makefile b/sysmodules/sm/Makefile index a750339..58b3251 100644 --- a/sysmodules/sm/Makefile +++ b/sysmodules/sm/Makefile @@ -28,9 +28,27 @@ INCLUDES := include ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft DEFINES := -D__3DS__ -CFLAGS := -g -std=gnu11 -Wall -Wextra -Werror -Os -mword-relocations \ - -fomit-frame-pointer -ffunction-sections -fdata-sections \ - $(ARCH) $(DEFINES) +ifeq ($(BUILD_FOR_GDB),1) + OPTFLAGS := -O0 + LIBS := -lctrud +else + OPTFLAGS := -O2 -fomit-frame-pointer + LIBS := -lctru +endif + +CFLAGS :=\ + -g \ + -std=gnu11 \ + -Wall \ + -Wextra \ + -Werror \ + -mword-relocations \ + -ffunction-sections \ + -fdata-sections \ + $(OPTFLAGS) \ + $(ARCH) \ + $(DEFINES) \ + $(INCLUDE) CFLAGS += $(INCLUDE) @@ -39,8 +57,6 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 ASFLAGS := -g $(ARCH) LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map),-wrap,exit -LIBS := -lctru - #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing # include and lib