From 54f2afcb19d0b09e6048c67105721c959b89b547 Mon Sep 17 00:00:00 2001 From: profi200 Date: Fri, 26 Dec 2014 02:31:59 +0100 Subject: [PATCH 01/15] Added CFGU_GetConfigInfoBlk2() and an example. --- examples/get_system_language/Makefile | 175 +++++++++++++++++++++ examples/get_system_language/README.md | 4 + examples/get_system_language/source/main.c | 46 ++++++ libctru/include/3ds/services/cfgu.h | 1 + libctru/source/services/cfgu.c | 16 ++ 5 files changed, 242 insertions(+) create mode 100644 examples/get_system_language/Makefile create mode 100644 examples/get_system_language/README.md create mode 100644 examples/get_system_language/source/main.c diff --git a/examples/get_system_language/Makefile b/examples/get_system_language/Makefile new file mode 100644 index 0000000..1ed1097 --- /dev/null +++ b/examples/get_system_language/Makefile @@ -0,0 +1,175 @@ +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITARM)),) +$(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") +endif + +TOPDIR ?= $(CURDIR) +include $(DEVKITARM)/3ds_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 +# +# NO_SMDH: if set to anything, no SMDH file is generated. +# APP_TITLE is the name of the app stored in the SMDH file (Optional) +# APP_DESCRIPTION is the description of the app stored in the SMDH file (Optional) +# APP_AUTHOR is the author of the app stored in the SMDH file (Optional) +# ICON is the filename of the icon (.png), relative to the project folder. +# If not set, it attempts to use one of the following (in this order): +# - .png +# - icon.png +# - /default_icon.png +#--------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)) +BUILD := build +SOURCES := source +DATA := data +INCLUDES := include + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- +ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=softfp + +CFLAGS := -g -Wall -O2 -mword-relocations \ + -fomit-frame-pointer -ffast-math \ + $(ARCH) + +CFLAGS += $(INCLUDE) -DARM11 -D_3DS + +CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 + +ASFLAGS := -g $(ARCH) +LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) + +LIBS := -lctru -lm + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := $(CTRULIB) + + +#--------------------------------------------------------------------------------- +# 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 := $(addsuffix .o,$(BINFILES)) \ + $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) + +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) + +ifeq ($(strip $(ICON)),) + icons := $(wildcard *.png) + ifneq (,$(findstring $(TARGET).png,$(icons))) + export APP_ICON := $(TOPDIR)/$(TARGET).png + else + ifneq (,$(findstring icon.png,$(icons))) + export APP_ICON := $(TOPDIR)/icon.png + endif + endif +else + export APP_ICON := $(TOPDIR)/$(ICON) +endif + +ifeq ($(strip $(NO_SMDH)),) + export _3DSXFLAGS += --smdh=$(CURDIR)/$(TARGET).smdh +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).3dsx $(OUTPUT).smdh $(TARGET).elf + + +#--------------------------------------------------------------------------------- +else + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +ifeq ($(strip $(NO_SMDH)),) +$(OUTPUT).3dsx : $(OUTPUT).elf $(OUTPUT).smdh +else +$(OUTPUT).3dsx : $(OUTPUT).elf +endif + +$(OUTPUT).elf : $(OFILES) + +#--------------------------------------------------------------------------------- +# you need a rule like this for each extension you use as binary data +#--------------------------------------------------------------------------------- +%.bin.o : %.bin +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + +# WARNING: This is not the right way to do this! TODO: Do it right! +#--------------------------------------------------------------------------------- +%.vsh.o : %.vsh +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @python $(AEMSTRO)/aemstro_as.py $< ../$(notdir $<).shbin + @bin2s ../$(notdir $<).shbin | $(PREFIX)as -o $@ + @echo "extern const u8" `(echo $(notdir $<).shbin | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" > `(echo $(notdir $<).shbin | tr . _)`.h + @echo "extern const u8" `(echo $(notdir $<).shbin | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> `(echo $(notdir $<).shbin | tr . _)`.h + @echo "extern const u32" `(echo $(notdir $<).shbin | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(notdir $<).shbin | tr . _)`.h + @rm ../$(notdir $<).shbin + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- diff --git a/examples/get_system_language/README.md b/examples/get_system_language/README.md new file mode 100644 index 0000000..a54dbd6 --- /dev/null +++ b/examples/get_system_language/README.md @@ -0,0 +1,4 @@ +get_system_language +======= + +This is an example on how to get the system language on the 3DS. diff --git a/examples/get_system_language/source/main.c b/examples/get_system_language/source/main.c new file mode 100644 index 0000000..20494fa --- /dev/null +++ b/examples/get_system_language/source/main.c @@ -0,0 +1,46 @@ +#include +#include +#include <3ds.h> + + +int main(int argc, char** argv) +{ + // Initialize services + gfxInit(); + initCfgu(); + + + u8 language = 0; + + // Init console for text output + consoleInit(GFX_BOTTOM, NULL); + + // Read the language field from the config savegame. + // See here for more block IDs: + // http://3dbrew.org/wiki/Config_Savegame#Configuration_blocks + CFGU_GetConfigInfoBlk2(1, 0xA0002, &language); + + + // Main loop + while (aptMainLoop()) + { + hidScanInput(); + + // Print language code + printf("\x1b[0;0HLanguage code: %d", (int)language); + + u32 kDown = hidKeysDown(); + if (kDown & KEY_START) + break; // break in order to return to hbmenu + + // Flush and swap framebuffers + gfxFlushBuffers(); + gfxSwapBuffers(); + gspWaitForVBlank(); + } + + // Exit services + exitCfgu(); + gfxExit(); + return 0; +} diff --git a/libctru/include/3ds/services/cfgu.h b/libctru/include/3ds/services/cfgu.h index 036e7d2..8ff7db4 100644 --- a/libctru/include/3ds/services/cfgu.h +++ b/libctru/include/3ds/services/cfgu.h @@ -8,3 +8,4 @@ Result CFGU_GetSystemModel(u8* model); Result CFGU_GetModelNintendo2DS(u8* value); Result CFGU_GetCountryCodeString(u16 code, u16* string); Result CFGU_GetCountryCodeID(u16 string, u16* code); +Result CFGU_GetConfigInfoBlk2(u32 size, u32 blkID, u8* outData); diff --git a/libctru/source/services/cfgu.c b/libctru/source/services/cfgu.c index c28a6cc..6ff7752 100644 --- a/libctru/source/services/cfgu.c +++ b/libctru/source/services/cfgu.c @@ -90,3 +90,19 @@ Result CFGU_GetCountryCodeID(u16 string, u16* code) return (Result)cmdbuf[1]; } + +Result CFGU_GetConfigInfoBlk2(u32 size, u32 blkID, u8* outData) +{ + Result ret = 0; + u32 *cmdbuf = getThreadCommandBuffer(); + + cmdbuf[0] = 0x00010082; + cmdbuf[1] = size; + cmdbuf[2] = blkID; + cmdbuf[3] = (size<<4)|12; + cmdbuf[4] = (u32)outData; + + if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; + + return (Result)cmdbuf[1]; +} From eb0f771409742bf8810fa1279e0f12b0ed2d9cb0 Mon Sep 17 00:00:00 2001 From: profi200 Date: Fri, 26 Dec 2014 02:46:38 +0100 Subject: [PATCH 02/15] Replaced spaces in cfgu.c. --- examples/get_system_language/source/main.c | 5 +- libctru/source/services/cfgu.c | 90 +++++++++++----------- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/examples/get_system_language/source/main.c b/examples/get_system_language/source/main.c index 20494fa..940ef35 100644 --- a/examples/get_system_language/source/main.c +++ b/examples/get_system_language/source/main.c @@ -11,6 +11,7 @@ int main(int argc, char** argv) u8 language = 0; + Result res; // Init console for text output consoleInit(GFX_BOTTOM, NULL); @@ -18,7 +19,7 @@ int main(int argc, char** argv) // Read the language field from the config savegame. // See here for more block IDs: // http://3dbrew.org/wiki/Config_Savegame#Configuration_blocks - CFGU_GetConfigInfoBlk2(1, 0xA0002, &language); + res = CFGU_GetConfigInfoBlk2(1, 0xA0002, &language); // Main loop @@ -26,7 +27,7 @@ int main(int argc, char** argv) { hidScanInput(); - // Print language code + // Print return value and language code printf("\x1b[0;0HLanguage code: %d", (int)language); u32 kDown = hidKeysDown(); diff --git a/libctru/source/services/cfgu.c b/libctru/source/services/cfgu.c index 6ff7752..01e74db 100644 --- a/libctru/source/services/cfgu.c +++ b/libctru/source/services/cfgu.c @@ -8,101 +8,101 @@ static Handle CFGU_handle = 0; Result initCfgu() { - return srvGetServiceHandle(&CFGU_handle, "cfg:u"); + return srvGetServiceHandle(&CFGU_handle, "cfg:u"); } Result exitCfgu() { - Result ret = svcCloseHandle(CFGU_handle); - CFGU_handle = 0; + Result ret = svcCloseHandle(CFGU_handle); + CFGU_handle = 0; - return ret; + return ret; } Result CFGU_GetRegionCanadaUSA(u8* value) { - Result ret = 0; - u32 *cmdbuf = getThreadCommandBuffer(); + Result ret = 0; + u32 *cmdbuf = getThreadCommandBuffer(); - cmdbuf[0] = 0x00040000; + cmdbuf[0] = 0x00040000; - if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; + if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; - *value = (u8)cmdbuf[2]; + *value = (u8)cmdbuf[2]; - return (Result)cmdbuf[1]; + return (Result)cmdbuf[1]; } Result CFGU_GetSystemModel(u8* model) { - Result ret = 0; - u32 *cmdbuf = getThreadCommandBuffer(); + Result ret = 0; + u32 *cmdbuf = getThreadCommandBuffer(); - cmdbuf[0] = 0x00050000; + cmdbuf[0] = 0x00050000; - if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; + if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; - *model = (u8)cmdbuf[2]; + *model = (u8)cmdbuf[2]; - return (Result)cmdbuf[1]; + return (Result)cmdbuf[1]; } Result CFGU_GetModelNintendo2DS(u8* value) { - Result ret = 0; - u32 *cmdbuf = getThreadCommandBuffer(); + Result ret = 0; + u32 *cmdbuf = getThreadCommandBuffer(); - cmdbuf[0] = 0x00060000; + cmdbuf[0] = 0x00060000; - if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; + if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; - *value = (u8)cmdbuf[2]; + *value = (u8)cmdbuf[2]; - return (Result)cmdbuf[1]; + return (Result)cmdbuf[1]; } Result CFGU_GetCountryCodeString(u16 code, u16* string) { - Result ret = 0; - u32 *cmdbuf = getThreadCommandBuffer(); + Result ret = 0; + u32 *cmdbuf = getThreadCommandBuffer(); - cmdbuf[0] = 0x00090040; - cmdbuf[1] = (u32)code; + cmdbuf[0] = 0x00090040; + cmdbuf[1] = (u32)code; - if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; + if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; - *string = (u16)cmdbuf[2]; + *string = (u16)cmdbuf[2]; - return (Result)cmdbuf[1]; + return (Result)cmdbuf[1]; } Result CFGU_GetCountryCodeID(u16 string, u16* code) { - Result ret = 0; - u32 *cmdbuf = getThreadCommandBuffer(); + Result ret = 0; + u32 *cmdbuf = getThreadCommandBuffer(); - cmdbuf[0] = 0x000A0040; - cmdbuf[1] = (u32)string; + cmdbuf[0] = 0x000A0040; + cmdbuf[1] = (u32)string; - if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; + if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; - *code = (u16)cmdbuf[2]; + *code = (u16)cmdbuf[2]; - return (Result)cmdbuf[1]; + return (Result)cmdbuf[1]; } Result CFGU_GetConfigInfoBlk2(u32 size, u32 blkID, u8* outData) { - Result ret = 0; - u32 *cmdbuf = getThreadCommandBuffer(); + Result ret = 0; + u32 *cmdbuf = getThreadCommandBuffer(); - cmdbuf[0] = 0x00010082; - cmdbuf[1] = size; - cmdbuf[2] = blkID; - cmdbuf[3] = (size<<4)|12; - cmdbuf[4] = (u32)outData; + cmdbuf[0] = 0x00010082; + cmdbuf[1] = size; + cmdbuf[2] = blkID; + cmdbuf[3] = (size<<4)|12; + cmdbuf[4] = (u32)outData; - if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; + if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; - return (Result)cmdbuf[1]; + return (Result)cmdbuf[1]; } From 9ab796b058d1518fab8a48aee70f5b57201e8ecb Mon Sep 17 00:00:00 2001 From: profi200 Date: Fri, 26 Dec 2014 02:57:49 +0100 Subject: [PATCH 03/15] Forgot to finish the changes on the get_system_language example. --- examples/get_system_language/source/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/get_system_language/source/main.c b/examples/get_system_language/source/main.c index 940ef35..d254957 100644 --- a/examples/get_system_language/source/main.c +++ b/examples/get_system_language/source/main.c @@ -28,7 +28,8 @@ int main(int argc, char** argv) hidScanInput(); // Print return value and language code - printf("\x1b[0;0HLanguage code: %d", (int)language); + printf("\x1b[0;0Hresult: %d", (int)res); + printf("\x1b[1;0HLanguage code: %d", (int)language); u32 kDown = hidKeysDown(); if (kDown & KEY_START) From 4f08f191ff9834d799a6efad89caac3a958b9b92 Mon Sep 17 00:00:00 2001 From: profi200 Date: Fri, 26 Dec 2014 03:03:40 +0100 Subject: [PATCH 04/15] Better in hex. --- examples/get_system_language/source/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/get_system_language/source/main.c b/examples/get_system_language/source/main.c index d254957..1a5d4c0 100644 --- a/examples/get_system_language/source/main.c +++ b/examples/get_system_language/source/main.c @@ -28,7 +28,7 @@ int main(int argc, char** argv) hidScanInput(); // Print return value and language code - printf("\x1b[0;0Hresult: %d", (int)res); + printf("\x1b[0;0Hresult: 0x%x", (int)res); printf("\x1b[1;0HLanguage code: %d", (int)language); u32 kDown = hidKeysDown(); From bb5c4a6ca860b49ae2e7fd0f453daa3dfdf9cb2c Mon Sep 17 00:00:00 2001 From: profi200 Date: Mon, 29 Dec 2014 22:46:59 +0100 Subject: [PATCH 05/15] Simplified reading the system language by adding a wrapper. Print only once in the get_system_language example. --- examples/get_system_language/source/main.c | 12 +++++------- libctru/include/3ds/services/cfgu.h | 1 + libctru/source/services/cfgu.c | 7 +++++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/examples/get_system_language/source/main.c b/examples/get_system_language/source/main.c index 1a5d4c0..d0fa815 100644 --- a/examples/get_system_language/source/main.c +++ b/examples/get_system_language/source/main.c @@ -17,9 +17,11 @@ int main(int argc, char** argv) consoleInit(GFX_BOTTOM, NULL); // Read the language field from the config savegame. - // See here for more block IDs: - // http://3dbrew.org/wiki/Config_Savegame#Configuration_blocks - res = CFGU_GetConfigInfoBlk2(1, 0xA0002, &language); + res = CFGU_GetSystemLanguage(&language); + + // Print return value and language code + printf(" Result: 0x%x\n", (int)res); + printf("Language code: %d", (int)language); // Main loop @@ -27,10 +29,6 @@ int main(int argc, char** argv) { hidScanInput(); - // Print return value and language code - printf("\x1b[0;0Hresult: 0x%x", (int)res); - printf("\x1b[1;0HLanguage code: %d", (int)language); - u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; // break in order to return to hbmenu diff --git a/libctru/include/3ds/services/cfgu.h b/libctru/include/3ds/services/cfgu.h index 8ff7db4..10c4178 100644 --- a/libctru/include/3ds/services/cfgu.h +++ b/libctru/include/3ds/services/cfgu.h @@ -9,3 +9,4 @@ Result CFGU_GetModelNintendo2DS(u8* value); Result CFGU_GetCountryCodeString(u16 code, u16* string); Result CFGU_GetCountryCodeID(u16 string, u16* code); Result CFGU_GetConfigInfoBlk2(u32 size, u32 blkID, u8* outData); +Result CFGU_GetSystemLanguage(u8* language); diff --git a/libctru/source/services/cfgu.c b/libctru/source/services/cfgu.c index 01e74db..e5e2556 100644 --- a/libctru/source/services/cfgu.c +++ b/libctru/source/services/cfgu.c @@ -91,6 +91,8 @@ Result CFGU_GetCountryCodeID(u16 string, u16* code) return (Result)cmdbuf[1]; } +// See here for block IDs: +// http://3dbrew.org/wiki/Config_Savegame#Configuration_blocks Result CFGU_GetConfigInfoBlk2(u32 size, u32 blkID, u8* outData) { Result ret = 0; @@ -106,3 +108,8 @@ Result CFGU_GetConfigInfoBlk2(u32 size, u32 blkID, u8* outData) return (Result)cmdbuf[1]; } + +Result CFGU_GetSystemLanguage(u8* language) +{ + return CFGU_GetConfigInfoBlk2(1, 0xA0002, language); +} From 18190a023731f96bf8fb1f07ed163d688a6e0512 Mon Sep 17 00:00:00 2001 From: profi200 Date: Mon, 29 Dec 2014 23:31:15 +0100 Subject: [PATCH 06/15] Added more cfg:u cmds. --- libctru/include/3ds/services/cfgu.h | 2 ++ libctru/source/services/cfgu.c | 30 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/libctru/include/3ds/services/cfgu.h b/libctru/include/3ds/services/cfgu.h index 10c4178..aaafd26 100644 --- a/libctru/include/3ds/services/cfgu.h +++ b/libctru/include/3ds/services/cfgu.h @@ -3,6 +3,8 @@ Result initCfgu(void); Result exitCfgu(void); +Result CFGU_SecureInfoGetRegion(u8* region); +Result CFGU_GenHashConsoleUnique(u32 appIDSalt, u64* hash); Result CFGU_GetRegionCanadaUSA(u8* value); Result CFGU_GetSystemModel(u8* model); Result CFGU_GetModelNintendo2DS(u8* value); diff --git a/libctru/source/services/cfgu.c b/libctru/source/services/cfgu.c index e5e2556..a8bbc49 100644 --- a/libctru/source/services/cfgu.c +++ b/libctru/source/services/cfgu.c @@ -19,6 +19,36 @@ Result exitCfgu() return ret; } +Result CFGU_SecureInfoGetRegion(u8* region) +{ + Result ret = 0; + u32 *cmdbuf = getThreadCommandBuffer(); + + cmdbuf[0] = 0x00020000; + + if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; + + *region = (u8)cmdbuf[2]; + + return (Result)cmdbuf[1]; +} + +Result CFGU_GenHashConsoleUnique(u32 appIDSalt, u64* hash) +{ + Result ret = 0; + u32 *cmdbuf = getThreadCommandBuffer(); + + cmdbuf[0] = 0x00030000; + cmdbuf[1] = appIDSalt; + + if((ret = svcSendSyncRequest(CFGU_handle))!=0)return ret; + + *hash = (u64)cmdbuf[2]; + *hash |= ((u64)cmdbuf[3])<<32; + + return (Result)cmdbuf[1]; +} + Result CFGU_GetRegionCanadaUSA(u8* value) { Result ret = 0; From 2ed782f5f3fa0384e92fa01de8386828717980fe Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Mon, 29 Dec 2014 23:52:08 +0000 Subject: [PATCH 07/15] Set default stack to 32K, allow user adjustment --- libctru/source/system/allocateHeaps.c | 7 ++--- libctru/source/system/ctru_exit.c | 2 +- libctru/source/system/initSystem.c | 2 +- libctru/source/system/init_exit_Wrappers.s | 30 ++++++++++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 libctru/source/system/init_exit_Wrappers.s diff --git a/libctru/source/system/allocateHeaps.c b/libctru/source/system/allocateHeaps.c index 4009f8e..d53625c 100644 --- a/libctru/source/system/allocateHeaps.c +++ b/libctru/source/system/allocateHeaps.c @@ -7,6 +7,7 @@ u32 __linear_heap; u32 __heapBase; extern u32 __heap_size, __linear_heap_size; +u32 __attribute__((weak)) __stacksize__ = 32 * 1024; void __attribute__((weak)) __system_allocateHeaps() { u32 tmp=0; @@ -18,7 +19,7 @@ void __attribute__((weak)) __system_allocateHeaps() { // Allocate the linear heap svcControlMemory(&__linear_heap, 0x0, 0x0, __linear_heap_size, MEMOP_ALLOC_LINEAR, 0x3); // Set up newlib heap - fake_heap_start = (char*)__heapBase; - fake_heap_end = fake_heap_start + __heap_size; + fake_heap_start = (char*)__heapBase + __stacksize__; + fake_heap_end = fake_heap_start + __heap_size - __stacksize__; -} \ No newline at end of file +} diff --git a/libctru/source/system/ctru_exit.c b/libctru/source/system/ctru_exit.c index 058ea7c..f4c99f1 100644 --- a/libctru/source/system/ctru_exit.c +++ b/libctru/source/system/ctru_exit.c @@ -11,7 +11,7 @@ void __appExit(); void __libc_fini_array(void); -void __attribute__((weak)) __attribute__((noreturn)) __ctru_exit(int rc) +void __attribute__((weak)) __attribute__((noreturn)) __libctru_exit(int rc) { u32 tmp=0; diff --git a/libctru/source/system/initSystem.c b/libctru/source/system/initSystem.c index 080d125..47b1067 100644 --- a/libctru/source/system/initSystem.c +++ b/libctru/source/system/initSystem.c @@ -18,7 +18,7 @@ void __libc_init_array(void); void __ctru_exit(int rc); -void __attribute__((weak)) initSystem(void (*retAddr)(void)) +void __attribute__((weak)) __ctru_initSystem(void (*retAddr)(void)) { // Register newlib exit() syscall diff --git a/libctru/source/system/init_exit_Wrappers.s b/libctru/source/system/init_exit_Wrappers.s new file mode 100644 index 0000000..6155b64 --- /dev/null +++ b/libctru/source/system/init_exit_Wrappers.s @@ -0,0 +1,30 @@ + .text + .arm + .cpu mpcore + + .global initSystem + .type initSystem STT_FUNC +@--------------------------------------------------------------------------------- +initSystem: +@--------------------------------------------------------------------------------- + adr r0, saved_lr + str lr, [r0] + str sp, [r0,#4] + bl __ctru_initSystem + ldr r0,=fake_heap_start + ldr sp, [r0] + ldr pc, saved_lr + +saved_lr: + .word 0 +saved_stack: + .word 0 + + + .global __ctru_exit + .type __ctru_exit STT_FUNC +@--------------------------------------------------------------------------------- +__ctru_exit: +@--------------------------------------------------------------------------------- + ldr sp, saved_stack + b __libctru_exit From 7e51b3888e67d47377287b449fd49cd81e8f1c68 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Tue, 30 Dec 2014 00:02:55 +0000 Subject: [PATCH 08/15] oops, forgot these need to be in data section --- libctru/source/system/init_exit_Wrappers.s | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/libctru/source/system/init_exit_Wrappers.s b/libctru/source/system/init_exit_Wrappers.s index 6155b64..8668ff7 100644 --- a/libctru/source/system/init_exit_Wrappers.s +++ b/libctru/source/system/init_exit_Wrappers.s @@ -7,24 +7,26 @@ @--------------------------------------------------------------------------------- initSystem: @--------------------------------------------------------------------------------- - adr r0, saved_lr + ldr r0, =saved_lr str lr, [r0] str sp, [r0,#4] bl __ctru_initSystem ldr r0,=fake_heap_start ldr sp, [r0] - ldr pc, saved_lr - -saved_lr: - .word 0 -saved_stack: - .word 0 - + ldr r0, =saved_lr + ldr pc, [r0] .global __ctru_exit .type __ctru_exit STT_FUNC @--------------------------------------------------------------------------------- __ctru_exit: @--------------------------------------------------------------------------------- - ldr sp, saved_stack + ldr r1, =saved_stack + ldr sp, [r1] b __libctru_exit + + .data +saved_lr: + .word 0 +saved_stack: + .word 0 From 5f95d314cb9ae2929a587e8cc6198bf74228b830 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Tue, 30 Dec 2014 00:04:34 +0000 Subject: [PATCH 09/15] or bss might be better --- libctru/source/system/init_exit_Wrappers.s | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libctru/source/system/init_exit_Wrappers.s b/libctru/source/system/init_exit_Wrappers.s index 8668ff7..6d92c39 100644 --- a/libctru/source/system/init_exit_Wrappers.s +++ b/libctru/source/system/init_exit_Wrappers.s @@ -25,8 +25,9 @@ __ctru_exit: ldr sp, [r1] b __libctru_exit - .data + .bss + .align 2 saved_lr: - .word 0 + .space 4 saved_stack: - .word 0 + .space 4 From 02011660d5462bca0d3aca610910a9eaa199f22d Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Tue, 30 Dec 2014 03:19:06 +0000 Subject: [PATCH 10/15] add literal pool --- libctru/source/system/init_exit_Wrappers.s | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libctru/source/system/init_exit_Wrappers.s b/libctru/source/system/init_exit_Wrappers.s index 6d92c39..608f25f 100644 --- a/libctru/source/system/init_exit_Wrappers.s +++ b/libctru/source/system/init_exit_Wrappers.s @@ -25,6 +25,8 @@ __ctru_exit: ldr sp, [r1] b __libctru_exit + .pool + .bss .align 2 saved_lr: From da496b6a893802d10435f7361e28b1107a541069 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Tue, 30 Dec 2014 04:05:12 +0000 Subject: [PATCH 11/15] revert stack changes, broken on hardware :( --- libctru/source/system/allocateHeaps.c | 7 ++--- libctru/source/system/ctru_exit.c | 2 +- libctru/source/system/initSystem.c | 2 +- libctru/source/system/init_exit_Wrappers.s | 35 ---------------------- 4 files changed, 5 insertions(+), 41 deletions(-) delete mode 100644 libctru/source/system/init_exit_Wrappers.s diff --git a/libctru/source/system/allocateHeaps.c b/libctru/source/system/allocateHeaps.c index d53625c..4009f8e 100644 --- a/libctru/source/system/allocateHeaps.c +++ b/libctru/source/system/allocateHeaps.c @@ -7,7 +7,6 @@ u32 __linear_heap; u32 __heapBase; extern u32 __heap_size, __linear_heap_size; -u32 __attribute__((weak)) __stacksize__ = 32 * 1024; void __attribute__((weak)) __system_allocateHeaps() { u32 tmp=0; @@ -19,7 +18,7 @@ void __attribute__((weak)) __system_allocateHeaps() { // Allocate the linear heap svcControlMemory(&__linear_heap, 0x0, 0x0, __linear_heap_size, MEMOP_ALLOC_LINEAR, 0x3); // Set up newlib heap - fake_heap_start = (char*)__heapBase + __stacksize__; - fake_heap_end = fake_heap_start + __heap_size - __stacksize__; + fake_heap_start = (char*)__heapBase; + fake_heap_end = fake_heap_start + __heap_size; -} +} \ No newline at end of file diff --git a/libctru/source/system/ctru_exit.c b/libctru/source/system/ctru_exit.c index f4c99f1..058ea7c 100644 --- a/libctru/source/system/ctru_exit.c +++ b/libctru/source/system/ctru_exit.c @@ -11,7 +11,7 @@ void __appExit(); void __libc_fini_array(void); -void __attribute__((weak)) __attribute__((noreturn)) __libctru_exit(int rc) +void __attribute__((weak)) __attribute__((noreturn)) __ctru_exit(int rc) { u32 tmp=0; diff --git a/libctru/source/system/initSystem.c b/libctru/source/system/initSystem.c index 47b1067..080d125 100644 --- a/libctru/source/system/initSystem.c +++ b/libctru/source/system/initSystem.c @@ -18,7 +18,7 @@ void __libc_init_array(void); void __ctru_exit(int rc); -void __attribute__((weak)) __ctru_initSystem(void (*retAddr)(void)) +void __attribute__((weak)) initSystem(void (*retAddr)(void)) { // Register newlib exit() syscall diff --git a/libctru/source/system/init_exit_Wrappers.s b/libctru/source/system/init_exit_Wrappers.s deleted file mode 100644 index 608f25f..0000000 --- a/libctru/source/system/init_exit_Wrappers.s +++ /dev/null @@ -1,35 +0,0 @@ - .text - .arm - .cpu mpcore - - .global initSystem - .type initSystem STT_FUNC -@--------------------------------------------------------------------------------- -initSystem: -@--------------------------------------------------------------------------------- - ldr r0, =saved_lr - str lr, [r0] - str sp, [r0,#4] - bl __ctru_initSystem - ldr r0,=fake_heap_start - ldr sp, [r0] - ldr r0, =saved_lr - ldr pc, [r0] - - .global __ctru_exit - .type __ctru_exit STT_FUNC -@--------------------------------------------------------------------------------- -__ctru_exit: -@--------------------------------------------------------------------------------- - ldr r1, =saved_stack - ldr sp, [r1] - b __libctru_exit - - .pool - - .bss - .align 2 -saved_lr: - .space 4 -saved_stack: - .space 4 From c1d8df8b68697476a415f25ac7e1dc57e753022e Mon Sep 17 00:00:00 2001 From: yellows8 Date: Tue, 30 Dec 2014 01:04:55 -0500 Subject: [PATCH 12/15] Implemented actual code for sdmc_stat(). --- libctru/source/sdmc_dev.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/libctru/source/sdmc_dev.c b/libctru/source/sdmc_dev.c index 2a6a833..5ec6cd2 100644 --- a/libctru/source/sdmc_dev.c +++ b/libctru/source/sdmc_dev.c @@ -527,7 +527,38 @@ sdmc_stat(struct _reent *r, const char *file, struct stat *st) { - r->_errno = ENOSYS; + Handle fd; + Result rc; + const char *pathptr = NULL; + u64 tmpsize = 0; + + pathptr = sdmc_fixpath(file); + + if(pathptr==NULL) + { + r->_errno=EINVAL; + return -1; + } + + rc = FSUSER_OpenFile(NULL, &fd, sdmcArchive, FS_makePath(PATH_CHAR, pathptr), + FS_OPEN_READ, FS_ATTRIBUTE_NONE); + + if(rc==0) + { + rc = FSFILE_GetSize(fd, &tmpsize); + + FSFILE_Close(fd); + + if(rc==0) + { + memset(st, 0, sizeof(struct stat)); + st->st_size = (off_t)tmpsize; + } + } + + if(rc==0)return 0; + + r->_errno = rc; return -1; } From c68afb5f319ae4a49d444e9e6f6e07ee4b84f798 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Tue, 30 Dec 2014 20:39:49 +0000 Subject: [PATCH 13/15] check for directory in stat too and set st_mode appropriately --- libctru/source/sdmc_dev.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/libctru/source/sdmc_dev.c b/libctru/source/sdmc_dev.c index 5ec6cd2..d02c962 100644 --- a/libctru/source/sdmc_dev.c +++ b/libctru/source/sdmc_dev.c @@ -181,7 +181,7 @@ Result sdmcExit(void) RemoveDevice("sdmc"); sdmcInitialised = false; - + return rc; } @@ -530,7 +530,6 @@ sdmc_stat(struct _reent *r, Handle fd; Result rc; const char *pathptr = NULL; - u64 tmpsize = 0; pathptr = sdmc_fixpath(file); @@ -540,11 +539,10 @@ sdmc_stat(struct _reent *r, return -1; } - rc = FSUSER_OpenFile(NULL, &fd, sdmcArchive, FS_makePath(PATH_CHAR, pathptr), - FS_OPEN_READ, FS_ATTRIBUTE_NONE); - - if(rc==0) + if( (rc = FSUSER_OpenFile(NULL, &fd, sdmcArchive, FS_makePath(PATH_CHAR, pathptr), + FS_OPEN_READ, FS_ATTRIBUTE_NONE))==0) { + u64 tmpsize = 0; rc = FSFILE_GetSize(fd, &tmpsize); FSFILE_Close(fd); @@ -553,12 +551,24 @@ sdmc_stat(struct _reent *r, { memset(st, 0, sizeof(struct stat)); st->st_size = (off_t)tmpsize; + st->st_nlink = 1; + st->st_uid = 1; + st->st_gid = 2; + st->st_mode = S_IFREG | S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH; + return 0; } } + if( (rc = FSUSER_OpenDirectory(NULL, &fd, sdmcArchive, FS_makePath(PATH_CHAR, pathptr))) == 0 ) + { + memset(st, 0, sizeof(struct stat)); + st->st_nlink = 1; + st->st_uid = 1; + st->st_gid = 2; + st->st_mode = S_IFDIR | S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH; + return 0; + } - if(rc==0)return 0; - - r->_errno = rc; + r->_errno = EBADF; return -1; } From 192b88b6a21d83345a4635deba417c191c156c80 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Tue, 30 Dec 2014 22:37:53 +0000 Subject: [PATCH 14/15] allow user configurable stack --- libctru/source/system/ctru_exit.c | 2 +- libctru/source/system/initSystem.c | 2 +- libctru/source/system/stack_adjust.s | 49 ++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 libctru/source/system/stack_adjust.s diff --git a/libctru/source/system/ctru_exit.c b/libctru/source/system/ctru_exit.c index 058ea7c..f4c99f1 100644 --- a/libctru/source/system/ctru_exit.c +++ b/libctru/source/system/ctru_exit.c @@ -11,7 +11,7 @@ void __appExit(); void __libc_fini_array(void); -void __attribute__((weak)) __attribute__((noreturn)) __ctru_exit(int rc) +void __attribute__((weak)) __attribute__((noreturn)) __libctru_exit(int rc) { u32 tmp=0; diff --git a/libctru/source/system/initSystem.c b/libctru/source/system/initSystem.c index 080d125..501cfaa 100644 --- a/libctru/source/system/initSystem.c +++ b/libctru/source/system/initSystem.c @@ -18,7 +18,7 @@ void __libc_init_array(void); void __ctru_exit(int rc); -void __attribute__((weak)) initSystem(void (*retAddr)(void)) +void __attribute__((weak)) __libctru_init(void (*retAddr)(void)) { // Register newlib exit() syscall diff --git a/libctru/source/system/stack_adjust.s b/libctru/source/system/stack_adjust.s new file mode 100644 index 0000000..e2d9b37 --- /dev/null +++ b/libctru/source/system/stack_adjust.s @@ -0,0 +1,49 @@ + + .arm + .align 2 + + .global initSystem + .type initSystem, %function + +initSystem: + ldr r2, =saved_stack + str sp, [r2] + str lr, [r2,#4] + + bl __libctru_init + + ldr r2, =fake_heap_start + ldr sp, [r2] + + ldr r3, =__stacksize__ + ldr r3, [r3] + add sp, r3, #7 + bics sp, sp, #7 + str sp, [r2] + + ldr r2, =saved_stack + ldr lr, [r2,#4] + bx lr + + + .global __ctru_exit + .type __ctru_exit, %function + +__ctru_exit: + ldr r2, =saved_stack + ldr sp, [r2] + b __libctru_exit + + .data + .align 2 +__stacksize__: + .word 32 * 1024 + .weak __stacksize__ + + + .bss + .align 2 +saved_stack: + .space 8 + + From 2712498d2d1f1ca92ee41f6d5817261e1d4b030f Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Tue, 30 Dec 2014 23:00:31 +0000 Subject: [PATCH 15/15] oops --- libctru/source/system/stack_adjust.s | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libctru/source/system/stack_adjust.s b/libctru/source/system/stack_adjust.s index e2d9b37..7c780fb 100644 --- a/libctru/source/system/stack_adjust.s +++ b/libctru/source/system/stack_adjust.s @@ -16,8 +16,9 @@ initSystem: ldr sp, [r2] ldr r3, =__stacksize__ - ldr r3, [r3] - add sp, r3, #7 + ldr r3, [r3] + add sp, sp, r3 + add sp, sp, #7 bics sp, sp, #7 str sp, [r2]