commit e3f450ba7b22db550d9a2f2d1906e7bc78e68932 Author: MCMrARM Date: Tue May 15 14:57:40 2018 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbb6e74 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +build +/*.elf +/*.nacp +/*.nro +/*.nso +/*.map +/*.pfs0 +/*.lst + +# Clion files +.idea +cmake-build-debug +CMakeLists.txt diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d90a244 --- /dev/null +++ b/Makefile @@ -0,0 +1,172 @@ +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITPRO)),) +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") +endif + +TOPDIR ?= $(CURDIR) +include $(DEVKITPRO)/libnx/switch_rules + +TARGET := swupdatedumper +BUILD := build +SOURCES := source +DATA := data +INCLUDES := include +EXEFS_SRC := exefs_src +#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 -std=gnu++11 + +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).pfs0 $(OUTPUT).nro + +$(OUTPUT).pfs0 : $(OUTPUT).nso + +$(OUTPUT).nso : $(OUTPUT).elf + +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 +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- diff --git a/source/fsext.c b/source/fsext.c new file mode 100644 index 0000000..3ea3a8e --- /dev/null +++ b/source/fsext.c @@ -0,0 +1,156 @@ +#include "fsext.h" + +#include + + +// IFileSystemProxy +Result fsOpenGameCard(FsStorage* out, u32 handle, u32 partition) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + u32 handle; + u32 partition; + } PACKED *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 30; + raw->handle = handle; + raw->partition = partition; + + Result rc = serviceIpcDispatch(fsGetServiceSession()); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + } *resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc)) { + serviceCreate(&out->s, r.Handles[0]); + } + } + + return rc; +} + + +// IDeviceOperator +Result fsDeviceOperatorIsGameCardInserted(FsDeviceOperator* d, bool* out) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 200; + + Result rc = serviceIpcDispatch(&d->s); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + u8 is_inserted; + } *resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc)) { + *out = resp->is_inserted != 0; + } + } + + return rc; +} + +Result fsDeviceOperatorGetGameCardHandle(FsDeviceOperator* d, u32* out) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 202; + + Result rc = serviceIpcDispatch(&d->s); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + u32 handle; + } *resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc)) { + *out = resp->handle; + } + } + + return rc; +} + + +// FsStorage +Result fsStorageGetSize(FsStorage* s, u64* out) { + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 4; + + Result rc = serviceIpcDispatch(&s->s); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + ipcParse(&r); + + struct { + u64 magic; + u64 result; + u64 size; + } *resp = r.Raw; + + rc = resp->result; + + if (R_SUCCEEDED(rc)) { + *out = resp->size; + } + } + + return rc; +} \ No newline at end of file diff --git a/source/fsext.h b/source/fsext.h new file mode 100644 index 0000000..e709f9f --- /dev/null +++ b/source/fsext.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +// IFileSystemProxy +Result fsOpenGameCard(FsStorage* out, u32 handle, u32 partition); + + +// IDeviceOperator +Result fsDeviceOperatorIsGameCardInserted(FsDeviceOperator* d, bool* out); +Result fsDeviceOperatorGetGameCardHandle(FsDeviceOperator* d, u32* out); + + +// FsStorage +Result fsStorageGetSize(FsStorage* s, u64* out); \ No newline at end of file diff --git a/source/main.c b/source/main.c new file mode 100644 index 0000000..2b48b11 --- /dev/null +++ b/source/main.c @@ -0,0 +1,103 @@ +#include +#include +#include + +#include +#include "fsext.h" + +bool isGameCardInserted(FsDeviceOperator* o) { + bool inserted; + if (R_FAILED(fsDeviceOperatorIsGameCardInserted(o, &inserted))) + return false; + return inserted; +} + +bool doLogic(FsDeviceOperator* fsOperator) { + bool cardInserted = isGameCardInserted(fsOperator); + printf("IsGameCardInserted = %i\n", cardInserted); + if (!cardInserted) + return false; + + u32 handle; + if (R_FAILED(fsDeviceOperatorGetGameCardHandle(fsOperator, &handle))) { + printf("GetGameCardHandle failed\n"); + return false; + } + FsStorage gameCardStorage; + Result result; + if (R_FAILED(result = fsOpenGameCard(&gameCardStorage, handle, 0))) { + printf("MountGameCard failed %i\n", result); + return false; + } + printf("Opened card\n"); + + u64 size; + fsStorageGetSize(&gameCardStorage, &size); + printf("Size = %li\n", size); + FILE* outFile = fopen("out.bin", "wb"); + printf("Opened output file\n"); + + const size_t bufs = 1024 * 1024; + char* buf = (char*) malloc(bufs); + for (u64 off = 0; off < size; off += bufs) { + u64 n = bufs; + if (size - off < n) + n = size - off; + if (R_FAILED(result = fsStorageRead(&gameCardStorage, off, buf, n))) { + printf("fsStorageRead error\n"); + fsStorageClose(&gameCardStorage); + return false; + } + if (fwrite(buf, 1, n, outFile) != n) { + printf("fwrite error\n"); + fsStorageClose(&gameCardStorage); + return false; + } + printf("Dumping %i%% [%li / %li]\n", (int) (off * 100 / size), off, size); + } + free(buf); + + fclose(outFile); + + fsStorageClose(&gameCardStorage); + + printf("Done!\n"); + + return true; +} + +int main(int argc, char **argv) +{ + gfxInitDefault(); + consoleInit(NULL); + + FsDeviceOperator fsOperator; + if (R_FAILED(fsOpenDeviceOperator(&fsOperator))) { + printf("Failed to open device operator\n"); + return -1; + } + + doLogic(&fsOperator); + + fsDeviceOperatorClose(&fsOperator); + + + while(appletMainLoop()) + { + hidScanInput(); + + // + + u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO); + + if (kDown & KEY_PLUS) break; + + gfxFlushBuffers(); + gfxSwapBuffers(); + gfxWaitForVsync(); + } + + gfxExit(); + return 0; +} +