diff --git a/.gitignore b/.gitignore index ee2b8b049..798c6691c 100644 --- a/.gitignore +++ b/.gitignore @@ -73,3 +73,5 @@ fusee/out/** fusee/build/** stratosphere/loader/out/** stratosphere/loader/build/** +stratosphere/sm/out/** +stratosphere/sm/build/** diff --git a/stratosphere/sm/Makefile b/stratosphere/sm/Makefile new file mode 100644 index 000000000..74ee33781 --- /dev/null +++ b/stratosphere/sm/Makefile @@ -0,0 +1,185 @@ +#--------------------------------------------------------------------------------- +.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". +# +# 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 + +DEFINES := -DDISABLE_IPC + +#--------------------------------------------------------------------------------- +# 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++17 + +ASFLAGS := -g $(ARCH) +LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) + +LIBS := -lstratosphere -lnx + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := $(PORTLIBS) $(LIBNX) $(CURDIR)/../libstratosphere + + +#--------------------------------------------------------------------------------- +# 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) + +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 + +.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) + +#--------------------------------------------------------------------------------- +# 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/stratosphere/sm/source/sm_main.cpp b/stratosphere/sm/source/sm_main.cpp new file mode 100644 index 000000000..be1e1555f --- /dev/null +++ b/stratosphere/sm/source/sm_main.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +#include +#include + +#include "sm_manager_service.hpp" +#include "sm_user_service.hpp" + +extern "C" { + extern u32 __start__; + + u32 __nx_applet_type = AppletType_None; + + #define INNER_HEAP_SIZE 0x200000 + size_t nx_inner_heap_size = INNER_HEAP_SIZE; + char nx_inner_heap[INNER_HEAP_SIZE]; + + void __libnx_initheap(void); + void __appInit(void); + void __appExit(void); +} + + +void __libnx_initheap(void) { + void* addr = nx_inner_heap; + size_t size = nx_inner_heap_size; + + /* Newlib */ + extern char* fake_heap_start; + extern char* fake_heap_end; + + fake_heap_start = (char*)addr; + fake_heap_end = (char*)addr + size; +} + +void __appInit(void) { + /* We must do nothing here, because we are sm. */ +} + +void __appExit(void) { + /* Nothing to clean up, because we're sm. */ +} + +int main(int argc, char **argv) +{ + consoleDebugInit(debugDevice_SVC); + + /* TODO: What's a good timeout value to use here? */ + WaitableManager *server_manager = new WaitableManager(U64_MAX); + + /* Add services to manager. */ + server_manager->add_waitable(new ManagedPortServer("sm:", 0x40)); + server_manager->add_waitable(new ServiceServer("sm:m", 1)); + + /* Loop forever, servicing our services. */ + server_manager->process(); + + /* Cleanup. */ + delete server_manager; + return 0; +} + diff --git a/stratosphere/sm/source/sm_manager_service.cpp b/stratosphere/sm/source/sm_manager_service.cpp new file mode 100644 index 000000000..87eeb6b47 --- /dev/null +++ b/stratosphere/sm/source/sm_manager_service.cpp @@ -0,0 +1,29 @@ +#include +#include "sm_manager_service.hpp" + +Result ManagerService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) { + Result rc = 0xF601; + + switch ((ManagerServiceCmd)cmd_id) { + case Manager_Cmd_RegisterProcess: + rc = WrapIpcCommandImpl<&ManagerService::register_process>(this, r, out_c, pointer_buffer, pointer_buffer_size); + break; + case Manager_Cmd_UnregisterProcess: + rc = WrapIpcCommandImpl<&ManagerService::unregister_process>(this, r, out_c, pointer_buffer, pointer_buffer_size); + break; + default: + break; + } + return rc; +} + + +std::tuple ManagerService::register_process(u64 pid, InBuffer acid_sac, InBuffer aci0_sac) { + /* TODO */ + return std::make_tuple(0xF601); +} + +std::tuple ManagerService::unregister_process(u64 pid) { + /* TODO */ + return std::make_tuple(0xF601); +} \ No newline at end of file diff --git a/stratosphere/sm/source/sm_manager_service.hpp b/stratosphere/sm/source/sm_manager_service.hpp new file mode 100644 index 000000000..7da8a51fe --- /dev/null +++ b/stratosphere/sm/source/sm_manager_service.hpp @@ -0,0 +1,18 @@ +#pragma once +#include +#include + +enum ManagerServiceCmd { + Manager_Cmd_RegisterProcess = 0, + Manager_Cmd_UnregisterProcess = 1 +}; + +class ManagerService : IServiceObject { + public: + Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size); + + private: + /* Actual commands. */ + std::tuple register_process(u64 pid, InBuffer acid_sac, InBuffer aci0_sac); + std::tuple unregister_process(u64 pid); +}; \ No newline at end of file diff --git a/stratosphere/sm/source/sm_user_service.cpp b/stratosphere/sm/source/sm_user_service.cpp new file mode 100644 index 000000000..d11ea908f --- /dev/null +++ b/stratosphere/sm/source/sm_user_service.cpp @@ -0,0 +1,45 @@ +#include +#include "sm_user_service.hpp" + +Result UserService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) { + Result rc = 0xF601; + + switch ((UserServiceCmd)cmd_id) { + case User_Cmd_Initialize: + rc = WrapIpcCommandImpl<&UserService::initialize>(this, r, out_c, pointer_buffer, pointer_buffer_size); + break; + case User_Cmd_GetService: + rc = WrapIpcCommandImpl<&UserService::get_service>(this, r, out_c, pointer_buffer, pointer_buffer_size); + break; + case User_Cmd_RegisterService: + rc = WrapIpcCommandImpl<&UserService::register_service>(this, r, out_c, pointer_buffer, pointer_buffer_size); + break; + case User_Cmd_UnregisterService: + rc = WrapIpcCommandImpl<&UserService::unregister_service>(this, r, out_c, pointer_buffer, pointer_buffer_size); + break; + default: + break; + } + return rc; +} + + +std::tuple UserService::initialize(PidDescriptor pid) { + /* TODO */ + return std::make_tuple(0xF601); +} + +std::tuple UserService::get_service(u64 service) { + /* TODO */ + return std::make_tuple(0xF601, MovedHandle{0}); +} + +std::tuple UserService::register_service(u64 service, u8 is_light, u32 max_sessions) { + /* TODO */ + return std::make_tuple(0xF601, MovedHandle{0}); +} + +std::tuple UserService::unregister_service(u64 service) { + /* TODO */ + return std::make_tuple(0xF601); +} diff --git a/stratosphere/sm/source/sm_user_service.hpp b/stratosphere/sm/source/sm_user_service.hpp new file mode 100644 index 000000000..df13f25da --- /dev/null +++ b/stratosphere/sm/source/sm_user_service.hpp @@ -0,0 +1,22 @@ +#pragma once +#include +#include + +enum UserServiceCmd { + User_Cmd_Initialize = 0, + User_Cmd_GetService = 1, + User_Cmd_RegisterService = 2, + User_Cmd_UnregisterService = 3 +}; + +class UserService : IServiceObject { + public: + Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size); + + private: + /* Actual commands. */ + std::tuple initialize(PidDescriptor pid); + std::tuple get_service(u64 service); + std::tuple register_service(u64 service, u8 is_light, u32 max_sessions); + std::tuple unregister_service(u64 service); +}; \ No newline at end of file