From e75f4e9f5ad4f8985d64cc1cc3cb5346c4577343 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 24 Jan 2019 09:05:06 -0800 Subject: [PATCH] troposphere: add reboot_to_payload example homebrew --- Makefile | 7 +- troposphere/Makefile | 12 ++ troposphere/reboot_to_payload/Makefile | 192 ++++++++++++++++++++ troposphere/reboot_to_payload/source/main.c | 103 +++++++++++ 4 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 troposphere/Makefile create mode 100644 troposphere/reboot_to_payload/Makefile create mode 100644 troposphere/reboot_to_payload/source/main.c diff --git a/Makefile b/Makefile index c9ac55b28..8b85264f5 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ ifneq (, $(strip $(shell git status --porcelain 2>/dev/null))) AMSREV := $(AMSREV)-dirty endif -all: fusee stratosphere exosphere thermosphere +all: fusee stratosphere exosphere thermosphere troposphere thermosphere: $(MAKE) -C thermosphere all @@ -17,6 +17,9 @@ exosphere: thermosphere stratosphere: exosphere $(MAKE) -C stratosphere all +troposphere: stratosphere + $(MAKE) -C troposphere all + fusee: exosphere stratosphere $(MAKE) -C $@ all @@ -39,6 +42,7 @@ dist: all rm -rf out mkdir atmosphere-$(AMSVER) mkdir atmosphere-$(AMSVER)/atmosphere + mkdir atmosphere-$(AMSVER)/switch mkdir -p atmosphere-$(AMSVER)/atmosphere/titles/0100000000000036 mkdir -p atmosphere-$(AMSVER)/atmosphere/titles/0100000000000034 mkdir -p atmosphere-$(AMSVER)/atmosphere/titles/0100000000000032 @@ -51,6 +55,7 @@ dist: all cp stratosphere/creport/creport.nsp atmosphere-$(AMSVER)/atmosphere/titles/0100000000000036/exefs.nsp cp stratosphere/fatal/fatal.nsp atmosphere-$(AMSVER)/atmosphere/titles/0100000000000034/exefs.nsp cp stratosphere/set_mitm/set_mitm.nsp atmosphere-$(AMSVER)/atmosphere/titles/0100000000000032/exefs.nsp + cp troposphere/reboot_to_payload/reboot_to_payload.nro atmosphere-$(AMSVER)/switch/reboot_to_payload.nro mkdir -p atmosphere-$(AMSVER)/atmosphere/titles/0100000000000032/flags touch atmosphere-$(AMSVER)/atmosphere/titles/0100000000000032/flags/boot2.flag cd atmosphere-$(AMSVER); zip -r ../atmosphere-$(AMSVER).zip ./*; cd ../; diff --git a/troposphere/Makefile b/troposphere/Makefile new file mode 100644 index 000000000..2be61ea0a --- /dev/null +++ b/troposphere/Makefile @@ -0,0 +1,12 @@ +APPLICATIONS := reboot_to_payload + +SUBFOLDERS := $(APPLICATIONS) + +TOPTARGETS := all clean + +$(TOPTARGETS): $(SUBFOLDERS) + +$(SUBFOLDERS): + $(MAKE) -C $@ $(MAKECMDGOALS) + +.PHONY: $(TOPTARGETS) $(SUBFOLDERS) diff --git a/troposphere/reboot_to_payload/Makefile b/troposphere/reboot_to_payload/Makefile new file mode 100644 index 000000000..be4c825e2 --- /dev/null +++ b/troposphere/reboot_to_payload/Makefile @@ -0,0 +1,192 @@ +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITPRO)),) +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") +endif + +TOPDIR ?= $(CURDIR) +include $(DEVKITPRO)/libnx/switch_rules + +#--------------------------------------------------------------------------------- +# TARGET is the name of the output +# BUILD is the directory where object files & intermediate files will be placed +# SOURCES is a list of directories containing source code +# DATA is a list of directories containing data files +# INCLUDES is a list of directories containing header files +# EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm". +# ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional) +# +# NO_ICON: if set to anything, do not use icon. +# NO_NACP: if set to anything, no .nacp file is generated. +# APP_TITLE is the name of the app stored in the .nacp file (Optional) +# APP_AUTHOR is the author of the app stored in the .nacp file (Optional) +# APP_VERSION is the version of the app stored in the .nacp file (Optional) +# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional) +# ICON is the filename of the icon (.jpg), relative to the project folder. +# If not set, it attempts to use one of the following (in this order): +# - .jpg +# - icon.jpg +# - /default_icon.jpg +#--------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)) +BUILD := build +SOURCES := source +DATA := data +INCLUDES := include +EXEFS_SRC := exefs_src +APP_TITLE := Reboot to Payload +APP_AUTHOR := Atmosphère-NX +APP_VERSION := 1.0.0 +#ROMFS := romfs + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- +ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE + +CFLAGS := -g -Wall -O2 -ffunction-sections \ + $(ARCH) $(DEFINES) + +CFLAGS += $(INCLUDE) -D__SWITCH__ + +CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions + +ASFLAGS := -g $(ARCH) +LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) + +LIBS := -lnx + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := $(PORTLIBS) $(LIBNX) + + +#--------------------------------------------------------------------------------- +# no real need to edit anything past this point unless you need to add additional +# rules for different file extensions +#--------------------------------------------------------------------------------- +ifneq ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- + +export OUTPUT := $(CURDIR)/$(TARGET) +export TOPDIR := $(CURDIR) + +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + +export DEPSDIR := $(CURDIR)/$(BUILD) + +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) + +#--------------------------------------------------------------------------------- +# use CXX for linking C++ projects, CC for standard C +#--------------------------------------------------------------------------------- +ifeq ($(strip $(CPPFILES)),) +#--------------------------------------------------------------------------------- + export LD := $(CC) +#--------------------------------------------------------------------------------- +else +#--------------------------------------------------------------------------------- + export LD := $(CXX) +#--------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------- + +export OFILES_BIN := $(addsuffix .o,$(BINFILES)) +export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) +export OFILES := $(OFILES_BIN) $(OFILES_SRC) +export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) + +export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + -I$(CURDIR)/$(BUILD) + +export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) + +export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC) + +ifeq ($(strip $(ICON)),) + icons := $(wildcard *.jpg) + ifneq (,$(findstring $(TARGET).jpg,$(icons))) + export APP_ICON := $(TOPDIR)/$(TARGET).jpg + else + ifneq (,$(findstring icon.jpg,$(icons))) + export APP_ICON := $(TOPDIR)/icon.jpg + endif + endif +else + export APP_ICON := $(TOPDIR)/$(ICON) +endif + +ifeq ($(strip $(NO_ICON)),) + export NROFLAGS += --icon=$(APP_ICON) +endif + +ifeq ($(strip $(NO_NACP)),) + export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp +endif + +ifneq ($(APP_TITLEID),) + export NACPFLAGS += --titleid=$(APP_TITLEID) +endif + +ifneq ($(ROMFS),) + export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS) +endif + +.PHONY: $(BUILD) clean all + +#--------------------------------------------------------------------------------- +all: $(BUILD) + +$(BUILD): + @[ -d $@ ] || mkdir -p $@ + @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... + @rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf + + +#--------------------------------------------------------------------------------- +else +.PHONY: all + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +all : $(OUTPUT).nro + +ifeq ($(strip $(NO_NACP)),) +$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp +else +$(OUTPUT).nro : $(OUTPUT).elf +endif + +$(OUTPUT).elf : $(OFILES) + +$(OFILES_SRC) : $(HFILES_BIN) + +#--------------------------------------------------------------------------------- +# you need a rule like this for each extension you use as binary data +#--------------------------------------------------------------------------------- +%.bin.o %_bin.h : %.bin +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- diff --git a/troposphere/reboot_to_payload/source/main.c b/troposphere/reboot_to_payload/source/main.c new file mode 100644 index 000000000..297f5fa86 --- /dev/null +++ b/troposphere/reboot_to_payload/source/main.c @@ -0,0 +1,103 @@ +#include +#include +#include + +#include + +#define IRAM_PAYLOAD_MAX_SIZE 0x2F000 +#define IRAM_PAYLOAD_BASE 0x40010000 + +static u8 g_reboot_payload[IRAM_PAYLOAD_MAX_SIZE] __attribute__ ((aligned (0x1000))); +static u8 g_ff_page[0x1000] __attribute__ ((aligned (0x1000))); +static u8 g_work_page[0x1000] __attribute__ ((aligned (0x1000))); + +void do_iram_dram_copy(void *buf, uintptr_t iram_addr, size_t size, int option) { + memcpy(g_work_page, buf, size); + + SecmonArgs args = {0}; + args.X[0] = 0xF0000201; /* smcAmsIramCopy */ + args.X[1] = (uintptr_t)g_work_page; /* DRAM Address */ + args.X[2] = iram_addr; /* IRAM Address */ + args.X[3] = size; /* Copy size */ + args.X[4] = option; /* 0 = Read, 1 = Write */ + svcCallSecureMonitor(&args); + + memcpy(buf, g_work_page, size); +} + +void copy_to_iram(uintptr_t iram_addr, void *buf, size_t size) { + do_iram_dram_copy(buf, iram_addr, size, 1); +} + +void copy_from_iram(void *buf, uintptr_t iram_addr, size_t size) { + do_iram_dram_copy(buf, iram_addr, size, 0); +} + +static void clear_iram(void) { + memset(g_ff_page, 0xFF, sizeof(g_ff_page)); + for (size_t i = 0; i < IRAM_PAYLOAD_MAX_SIZE; i += sizeof(g_ff_page)) { + copy_to_iram(IRAM_PAYLOAD_BASE + i, g_ff_page, sizeof(g_ff_page)); + } +} + +static void reboot_to_payload(void) { + clear_iram(); + + for (size_t i = 0; i < IRAM_PAYLOAD_MAX_SIZE; i += 0x1000) { + copy_to_iram(IRAM_PAYLOAD_BASE + i, &g_reboot_payload[i], 0x1000); + } + + splSetConfig((SplConfigItem)65001, 2); +} + +int main(int argc, char **argv) +{ + gfxInitDefault(); + fsdevMountSdmc(); + consoleInit(NULL); + + bool can_reboot = true; + Result rc = splInitialize(); + if (R_FAILED(rc)) { + printf("Failed to initialize spl: 0x%x\n", rc); + can_reboot = false; + } else { + FILE *f = fopen("sdmc:/atmosphere/reboot_payload.bin", "rb"); + if (f == NULL) { + printf("Failed to open atmosphere/reboot_payload.bin!\n"); + can_reboot = false; + } else { + fread(g_reboot_payload, 1, sizeof(g_reboot_payload), f); + fclose(f); + printf("Press [-] to reboot to payload\n"); + } + } + + printf("Press [L] to exit\n"); + + // Main loop + while(appletMainLoop()) + { + //Scan all the inputs. This should be done once for each frame + hidScanInput(); + + //hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame) + u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO); + + if (can_reboot && kDown & KEY_MINUS) { + reboot_to_payload(); + } + if (kDown & KEY_L) { break; } // break in order to return to hbmenu + + gfxFlushBuffers(); + gfxSwapBuffers(); + } + + if (can_reboot) { + splExit(); + } + fsdevUnmountAll(); + gfxExit(); + return 0; +} +