From e834fa830e47147464e443478025f4bf1de5e1ed Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 9 Jun 2022 19:11:30 +0200 Subject: [PATCH] first commit --- .clang-format | 67 +++++++++ .github/workflows/pr.yml | 25 ++++ .github/workflows/push_image.yml | 33 ++++ .gitignore | 9 ++ Dockerfile | 11 ++ Dockerfile.buildlocal | 5 + Makefile | 157 +++++++++++++++++++ README.md | 25 ++++ include/mocha/commands.h | 29 ++++ include/mocha/fsa.h | 36 +++++ include/mocha/mocha.h | 116 ++++++++++++++ source/fsa.cpp | 237 +++++++++++++++++++++++++++++ source/utils.cpp | 249 +++++++++++++++++++++++++++++++ source/utils.h | 12 ++ 14 files changed, 1011 insertions(+) create mode 100644 .clang-format create mode 100644 .github/workflows/pr.yml create mode 100644 .github/workflows/push_image.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 Dockerfile.buildlocal create mode 100644 Makefile create mode 100644 README.md create mode 100644 include/mocha/commands.h create mode 100644 include/mocha/fsa.h create mode 100644 include/mocha/mocha.h create mode 100644 source/fsa.cpp create mode 100644 source/utils.cpp create mode 100644 source/utils.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..56cc685 --- /dev/null +++ b/.clang-format @@ -0,0 +1,67 @@ +# Generated from CLion C/C++ Code Style settings +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: Consecutive +AlignConsecutiveMacros: AcrossEmptyLinesAndComments +AlignOperands: Align +AllowAllArgumentsOnNextLine: false +AllowAllConstructorInitializersOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Always +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterReturnType: None +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: true +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +ColumnLimit: 0 +CompactNamespaces: false +ContinuationIndentWidth: 8 +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 4 +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: All +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PointerAlignment: Right +ReflowComments: false +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Never diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 0000000..54f5094 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,25 @@ +name: CI-PR + +on: [pull_request] + +jobs: + clang-format: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - name: clang-format + run: | + docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source ./include + build-lib: + runs-on: ubuntu-18.04 + needs: clang-format + steps: + - uses: actions/checkout@v2 + - name: build lib + run: | + docker build . -f Dockerfile.buildlocal -t builder + docker run --rm -v ${PWD}:/project builder make + - uses: actions/upload-artifact@master + with: + name: lib + path: "lib/*.a" \ No newline at end of file diff --git a/.github/workflows/push_image.yml b/.github/workflows/push_image.yml new file mode 100644 index 0000000..fa316c9 --- /dev/null +++ b/.github/workflows/push_image.yml @@ -0,0 +1,33 @@ +name: Publish Docker Image +on: + push: + branches: + - main +jobs: + clang-format: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - name: clang-format + run: | + docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source ./include + build: + runs-on: ubuntu-latest + needs: clang-format + steps: + - uses: actions/checkout@master + - name: Get release version + id: get_release_tag + run: | + echo RELEASE_VERSION=$(echo $(date '+%Y%m%d')) >> $GITHUB_ENV + echo REPOSITORY_NAME=$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $2}' | sed -e "s/:refs//" | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV + echo REPOSITORY_OWNER=$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $1}' | sed 's/[^a-zA-Z0-9]//g' | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV + - name: Publish to Registry + uses: elgohr/Publish-Docker-Github-Action@master + with: + name: ${{ env.REPOSITORY_OWNER }}/${{ env.REPOSITORY_NAME }} + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + snapshot: true + cache: true + tags: "latest, ${{ env.RELEASE_VERSION }}" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f6ace4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/*.a +/build +*.bz2 +release/ +lib/ +CMakeLists.txt +.idea/ +cmake-build-debug/ +share/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..76f3b4f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM wiiuenv/devkitppc:20220605 + +RUN git clone --depth 1 --single-branch -b filesystemstructs https://github.com/Maschell/wut && cd wut && git reset --hard a060c981e23cdd7e622624692df2d789693432ce && make install && cd .. && rm -rf wut + +WORKDIR tmp_build +COPY . . +RUN make clean && make && mkdir -p /artifacts/wut/usr && cp -r lib /artifacts/wut/usr && cp -r include /artifacts/wut/usr +WORKDIR /artifacts + +FROM scratch +COPY --from=0 /artifacts /artifacts \ No newline at end of file diff --git a/Dockerfile.buildlocal b/Dockerfile.buildlocal new file mode 100644 index 0000000..ae89e17 --- /dev/null +++ b/Dockerfile.buildlocal @@ -0,0 +1,5 @@ +FROM wiiuenv/devkitppc:20220605 + +RUN git clone --depth 1 --single-branch -b filesystemstructs https://github.com/Maschell/wut && cd wut && git reset --hard a060c981e23cdd7e622624692df2d789693432ce && make install && cd .. && rm -rf wut + +WORKDIR project \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..99488d1 --- /dev/null +++ b/Makefile @@ -0,0 +1,157 @@ +#------------------------------------------------------------------------------- +.SUFFIXES: +#------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITPRO)),) +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") +endif + +TOPDIR ?= $(CURDIR) + +include $(DEVKITPRO)/wut/share/wut_rules + +export VER_MAJOR := 1 +export VER_MINOR := 0 +export VER_PATCH := 0 + +VERSION := $(VER_MAJOR).$(VER_MINOR).$(VER_PATCH) + +#------------------------------------------------------------------------------- +# 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 +#------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)) +BUILD := build +SOURCES := source +DATA := data +INCLUDES := source \ + include \ + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- +CFLAGS := -Wall -Werror -save-temps \ + -ffunction-sections -fdata-sections \ + $(MACHDEP) \ + $(BUILD_CFLAGS) + +CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__ + +CXXFLAGS := $(CFLAGS) -std=gnu++17 + +ASFLAGS := $(MACHDEP) + +LDFLAGS = $(ARCH) -Wl,--gc-sections + + +LIBS := + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := $(PORTLIBS) $(WUT_ROOT) + +#--------------------------------------------------------------------------------- +# 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 TOPDIR := $(CURDIR) + +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +DEFFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.def))) +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 := $(DEFFILES:.def=.o) $(SFILES:.s=.o) $(CFILES:.c=.o) $(CPPFILES:.cpp=.o) +export OFILES := $(OFILES_BIN) $(OFILES_SRC) +export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) + +export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + -I. + +.PHONY: all dist-bin dist-src dist install clean + +#--------------------------------------------------------------------------------- +all: lib/libmocha.a + +dist-bin: all + @tar --exclude=*~ -cjf libmocha-$(VERSION).tar.bz2 include lib + +dist-src: + @tar --exclude=*~ -cjf libmocha-src-$(VERSION).tar.bz2 include source Makefile + +dist: dist-src dist-bin + +install: dist-bin + mkdir -p $(DESTDIR)$(DEVKITPRO)/wut/usr + bzip2 -cd libmocha-$(VERSION).tar.bz2 | tar -xf - -C $(DESTDIR)$(DEVKITPRO)/wut/usr + +lib: + @[ -d $@ ] || mkdir -p $@ + +release: + @[ -d $@ ] || mkdir -p $@ + +lib/libmocha.a :$(SOURCES) $(INCLUDES) | lib release + @$(MAKE) BUILD=release OUTPUT=$(CURDIR)/$@ \ + BUILD_CFLAGS="-DNDEBUG=1 -O2 -s" \ + DEPSDIR=$(CURDIR)/release \ + --no-print-directory -C release \ + -f $(CURDIR)/Makefile + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... + @rm -rf release lib + +#--------------------------------------------------------------------------------- +else + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +$(OUTPUT) : $(OFILES) + +$(OFILES_SRC) : $(HFILES) + +#--------------------------------------------------------------------------------- +%_bin.h %.bin.o : %.bin +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- diff --git a/README.md b/README.md new file mode 100644 index 0000000..d81bdcb --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +[![Publish Docker Image](https://github.com/wiiu-env/libmocha/actions/workflows/push_image.yml/badge.svg)](https://github.com/wiiu-env/libmocha/actions/workflows/push_image.yml) + +# libmocha +Requires the [MochaPayload](https://github.com/wiiu-env/MochaPayload) to be running via [EnvironmentLoader](https://github.com/wiiu-env/EnvironmentLoader). +Requires [wut](https://github.com/devkitPro/wut) for building. +Install via `make install`. + +## Usage +Make sure to add `-lmocha` to `LIBS` and `$(WUT_ROOT)/usr` to `LIBDIRS` in your makefile. + +After that you can simply include `` to get access to the mocha functions after calling `Mocha_InitLibrary()`. + +## Use this lib in Dockerfiles. +A prebuilt version of this lib can found on dockerhub. To use it for your projects, add this to your Dockerfile. +``` +[...] +COPY --from=wiiuenv/libmocha:[tag] /artifacts $DEVKITPRO +[...] +``` +Replace [tag] with a tag you want to use, a list of tags can be found [here](https://hub.docker.com/r/wiiuenv/libmocha/tags). +It's highly recommended to pin the version to the **latest date** instead of using `latest`. + +## Format the code via docker + +`docker run --rm -v ${PWD}:/src wiiuenv/clang-format:13.0.0-2 -r ./source ./include -i` diff --git a/include/mocha/commands.h b/include/mocha/commands.h new file mode 100644 index 0000000..b08e95f --- /dev/null +++ b/include/mocha/commands.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define IPC_CUSTOM_START_MCP_THREAD 0xFE +#define IPC_CUSTOM_MEN_RPX_HOOK_COMPLETED 0xFD +#define IPC_CUSTOM_LOAD_CUSTOM_RPX 0xFC +#define IPC_CUSTOM_START_USB_LOGGING 0xFA +#define IPC_CUSTOM_COPY_ENVIRONMENT_PATH 0xF9 +#define IPC_CUSTOM_GET_MOCHA_API_VERSION 0xF8 + +typedef enum LoadRPXTargetEnum { + LOAD_RPX_TARGET_SD_CARD = 0, +} LoadRPXTargetEnum; + +typedef struct __attribute((packed)) { + LoadRPXTargetEnum target; // Target where the file will be loaded from. + uint32_t filesize; // Size of RPX inside given file. 0 for full filesize. + uint32_t fileoffset; // Offset of RPX inside given file. + char path[256]; // Relative path on target device +} MochaRPXLoadInfo; + +#ifdef __cplusplus +} // extern "C" +#endif \ No newline at end of file diff --git a/include/mocha/fsa.h b/include/mocha/fsa.h new file mode 100644 index 0000000..9c64c82 --- /dev/null +++ b/include/mocha/fsa.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +FSError FSAEx_Mount(FSClient *client, const char *source, const char *target, uint32_t flags, void *arg_buf, uint32_t arg_len); + +FSError FSAEx_MountEx(int clientHandle, const char *source, const char *target, uint32_t flags, void *arg_buf, uint32_t arg_len); + +FSError FSAEx_Unmount(FSClient *client, const char *mountedTarget); + +FSError FSAEx_UnmountEx(int clientHandle, const char *mountedTarget); + +FSError FSAEx_RawOpen(FSClient *client, char *device_path, int32_t *outHandle); + +FSError FSAEx_RawOpenEx(int clientHandle, char *device_path, int32_t *outHandle); + +FSError FSAEx_RawClose(FSClient *client, int32_t device_handle); + +FSError FSAEx_RawCloseEx(int clientHandle, int32_t device_handle); + +FSError FSAEx_RawRead(FSClient *client, void *data, uint32_t size_bytes, uint32_t cnt, uint64_t blocks_offset, int device_handle); + +FSError FSAEx_RawReadEx(int clientHandle, void *data, uint32_t size_bytes, uint32_t cnt, uint64_t blocks_offset, int device_handle); + +FSError FSAEx_RawWrite(FSClient *client, const void *data, uint32_t size_bytes, uint32_t cnt, uint64_t blocks_offset, int device_handle); + +FSError FSAEx_RawWriteEx(int clientHandle, const void *data, uint32_t size_bytes, uint32_t cnt, uint64_t blocks_offset, int device_handle); + +#ifdef __cplusplus +} // extern "C" +#endif \ No newline at end of file diff --git a/include/mocha/mocha.h b/include/mocha/mocha.h new file mode 100644 index 0000000..909aec7 --- /dev/null +++ b/include/mocha/mocha.h @@ -0,0 +1,116 @@ +#pragma once + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +enum MochaUtilsStatus { + MOCHA_RESULT_SUCCESS = 0, + MOCHA_RESULT_INVALID_ARGUMENT = -0x01, + MOCHA_RESULT_MAX_CLIENT = -0x02, + MOCHA_RESULT_UNSUPPORTED_API_VERSION = -0x10, + MOCHA_RESULT_UNSUPPORTED_COMMAND = -0x11, + MOCHA_RESULT_LIB_UNINITIALIZED = -0x20, + MOCHA_RESULT_UNKNOWN_ERROR = -0x100, +}; + +/** + * Initializes the mocha lib. Needs to be called before any other functions can be used + * @return MOCHA_RESULT_SUCCESS: Library has been successfully initialized
+ * MOCHA_RESULT_UNSUPPORTED_COMMAND: Failed to initialize the library caused by an outdated mocha version. + */ +MochaUtilsStatus Mocha_InitLibrary(); + +MochaUtilsStatus Mocha_DeInitLibrary(); + +/** + * Retrieves the API Version of the running mocha. + * + * @param outVersion pointer to the variable where the version will be stored. + * + * @return MOCHA_RESULT_SUCCESS: The API version has been store in the version ptr
+ * MOCHA_RESULT_INVALID_ARGUMENT: invalid version pointer
+ * MOCHA_RESULT_UNSUPPORTED_API_VERSION: Failed to get the API version caused by an outdated mocha version. + */ +MochaUtilsStatus Mocha_CheckAPIVersion(uint32_t *outVersion); + +/*** + * Returns the path of the currently loaded environment + * @param environmentPathBuffer: buffer where the result will be stored + * @param bufferLen: length of the buffer. Required to be >= 0x100 +* @return MOCHA_RESULT_SUCCESS: The environment path has been stored in environmentPathBuffer
+* MOCHA_RESULT_INVALID_ARGUMENT: invalid environmentPathBuffer pointer or bufferLen \< 0x100
+* MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.
+* MOCHA_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded mocha version.
+* MOCHA_RESULT_UNKNOWN_ERROR: Failed to retrieve the environment path. + */ +MochaUtilsStatus Mocha_GetEnvironmentPath(char *environmentPathBuffer, uint32_t bufferLen); + +/*** + * Signals Mocha to not redirect the men.rpx to root.rpx anymore +* @return MOCHA_RESULT_SUCCESS +* MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.
+* MOCHA_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded mocha version.
+* MOCHA_RESULT_UNKNOWN_ERROR: Failed to retrieve the environment path. + */ +MochaUtilsStatus Mocha_RPXHookCompleted(); + +/*** +* Starts the MCP Thread in mocha to allows usage of /dev/iosuhax and wupclient +* @return MOCHA_RESULT_SUCCESS +* MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.
+* MOCHA_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded mocha version.
+* MOCHA_RESULT_UNKNOWN_ERROR: Failed to retrieve the environment path. + */ +MochaUtilsStatus Mocha_StartMCPThread(); + +/*** +* Starts the MCP Thread in mocha to allows usage of /dev/iosuhax and wupclient +* @return MOCHA_RESULT_SUCCESS +* MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.
+* MOCHA_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded mocha version.
+* MOCHA_RESULT_UNKNOWN_ERROR: Failed to retrieve the environment path. + */ +MochaUtilsStatus Mocha_StartUSBLogging(bool avoidLogCatchup); + +/** + * Gives a FSClient full permissions.
+ * Requires Mocha API Version: 1 + * @param client The FSClient that should have full permission + * @return MOCHA_RESULT_SUCCESS: The has been unlocked successfully. + * MOCHA_RESULT_MAX_CLIENT: The maximum number of FS Clients have been unlocked.
+ * MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.
+ * MOCHA_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded mocha version.
+ * MOCHA_RESULT_UNKNOWN_ERROR: Failed to retrieve the environment path. + */ +MochaUtilsStatus Mocha_UnlockFSClient(FSClient *client); + +/** + * Gives a /dev/fsa handle full permissions.
+ * Requires Mocha API Version: 1 + * @param client The /dev/fsa handle that should have full permission + * @return MOCHA_RESULT_SUCCESS: The has been unlocked successfully. + * MOCHA_RESULT_MAX_CLIENT: The maximum number of FS Clients have been unlocked.
+ * MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.
+ * MOCHA_RESULT_UNSUPPORTED_COMMAND: Command not supported by the currently loaded mocha version.
+ * MOCHA_RESULT_UNKNOWN_ERROR: Failed to retrieve the environment path. + */ +MochaUtilsStatus Mocha_UnlockFSClientEx(int clientHandle); + +MochaUtilsStatus Mocha_LoadRPXOnNextLaunch(MochaRPXLoadInfo *loadInfo); + +typedef struct WUDDiscKey { + uint8_t key[0x10]; +} WUDDiscKey; + +MochaUtilsStatus Mocha_ODMGetDiscKey(WUDDiscKey *discKey); + +MochaUtilsStatus Mocha_SEEPROMRead(uint8_t *out_buffer, uint32_t offset, uint32_t size); + +#ifdef __cplusplus +} // extern "C" +#endif \ No newline at end of file diff --git a/source/fsa.cpp b/source/fsa.cpp new file mode 100644 index 0000000..ce0d089 --- /dev/null +++ b/source/fsa.cpp @@ -0,0 +1,237 @@ +#include "utils.h" +#include +#include +#include +#include +#include +#include + +FSError FSAEx_Mount(FSClient *client, const char *source, const char *target, uint32_t flags, void *arg_buf, uint32_t arg_len) { + if (!client) { + return FS_ERROR_INVALID_CLIENTHANDLE; + } + return FSAEx_MountEx(FSGetClientBody(client)->clientHandle, source, target, flags, arg_buf, arg_len); +} + +FSError FSAEx_MountEx(int clientHandle, const char *source, const char *target, uint32_t flags, void *arg_buf, uint32_t arg_len) { + auto *buffer = (FSAShimBuffer *) memalign(0x40, sizeof(FSAShimBuffer)); + if (!buffer) { + return FS_ERROR_INVALID_BUFFER; + } + auto res = __FSAShimSetupRequestMount(buffer, clientHandle, source, target, 2, arg_buf, arg_len); + if (res != 0) { + free(buffer); + return res; + } + res = __FSAShimSend(buffer, 0); + free(buffer); + return res; +} + +FSError FSAEx_Unmount(FSClient *client, const char *mountedTarget, int flags) { + if (!client) { + return FS_ERROR_INVALID_CLIENTHANDLE; + } + return FSAEx_UnmountEx(FSGetClientBody(client)->clientHandle, mountedTarget); +} + +FSError FSAEx_UnmountEx(int clientHandle, const char *mountedTarget, int flags) { + auto *buffer = (FSAShimBuffer *) memalign(0x40, sizeof(FSAShimBuffer)); + if (!buffer) { + return FS_ERROR_INVALID_BUFFER; + } + + auto res = __FSAShimSetupRequestUnmount(buffer, clientHandle, mountedTarget, flags); + if (res != 0) { + free(buffer); + return res; + } + res = __FSAShimSend(buffer, 0); + free(buffer); + return res; +} + +FSError FSAEx_RawOpen(FSClient *client, char *device_path, int32_t *outHandle) { + if (!client) { + return FS_ERROR_INVALID_CLIENTHANDLE; + } + return FSAEx_RawOpenEx(FSGetClientBody(client)->clientHandle, device_path, outHandle); +} + +FSError FSAEx_RawOpenEx(int clientHandle, char *device_path, int32_t *outHandle) { + if (!outHandle) { + return FS_ERROR_INVALID_BUFFER; + } + if (!device_path) { + return FS_ERROR_INVALID_PATH; + } + auto *shim = (FSAShimBuffer *) memalign(0x40, sizeof(FSAShimBuffer)); + if (!shim) { + return FS_ERROR_INVALID_BUFFER; + } + + shim->clientHandle = clientHandle; + shim->command = FSA_COMMAND_RAW_OPEN; + shim->ipcReqType = FSA_IPC_REQUEST_IOCTL; + shim->response.word0 = 0xFFFFFFFF; + + FSARequestRawOpen *requestBuffer = &shim->request.rawOpen; + + strncpy(requestBuffer->path, device_path, 0x27F); + + auto res = __FSAShimSend(shim, 0); + if (res >= 0) { + *outHandle = shim->response.rawOpen.handle; + } + free(shim); + return res; +} + +FSError FSAEx_RawClose(FSClient *client, int32_t device_handle) { + if (!client) { + return FS_ERROR_INVALID_CLIENTHANDLE; + } + return FSAEx_RawCloseEx(FSGetClientBody(client)->clientHandle, device_handle); +} + +FSError FSAEx_RawCloseEx(int clientHandle, int32_t device_handle) { + auto *buffer = (FSAShimBuffer *) memalign(0x40, sizeof(FSAShimBuffer)); + if (!buffer) { + return FS_ERROR_INVALID_BUFFER; + } + + buffer->clientHandle = clientHandle; + buffer->command = FSA_COMMAND_RAW_CLOSE; + buffer->ipcReqType = FSA_IPC_REQUEST_IOCTL; + buffer->response.word0 = 0xFFFFFFFF; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Waddress-of-packed-member" + auto *requestBuffer = &buffer->request.rawClose; +#pragma GCC diagnostic pop + + requestBuffer->handle = device_handle; + + auto res = __FSAShimSend(buffer, 0); + free(buffer); + return res; +} + +FSError FSAEx_RawRead(FSClient *client, void *data, uint32_t size_bytes, uint32_t cnt, uint64_t blocks_offset, int device_handle) { + if (!client) { + return FS_ERROR_INVALID_CLIENTHANDLE; + } + return FSAEx_RawReadEx(FSGetClientBody(client)->clientHandle, data, size_bytes, cnt, blocks_offset, device_handle); +} + +FSError FSAEx_RawReadEx(int clientHandle, void *data, uint32_t size_bytes, uint32_t cnt, uint64_t blocks_offset, int device_handle) { + if (data == nullptr) { + return FS_ERROR_INVALID_BUFFER; + } + auto *shim = (FSAShimBuffer *) memalign(0x40, sizeof(FSAShimBuffer)); + if (!shim) { + return FS_ERROR_INVALID_BUFFER; + } + + shim->clientHandle = clientHandle; + shim->ipcReqType = FSA_IPC_REQUEST_IOCTLV; + shim->command = FSA_COMMAND_RAW_READ; + + shim->ioctlvVecIn = uint8_t{1}; + shim->ioctlvVecOut = uint8_t{2}; + + shim->ioctlvVec[0].vaddr = &shim->request; + shim->ioctlvVec[0].len = sizeof(FSARequest); + + auto *tmp = data; + + if ((uint32_t) data & 0x3F) { + auto *alignedBuffer = memalign(0x40, ROUNDUP(size_bytes * cnt, 0x40)); + if (!alignedBuffer) { + OSReport("## ERROR: FSAEx_RawReadEx buffer not aligned (%08X).\n", data); + return FS_ERROR_INVALID_ALIGNMENT; + } + OSReport("## WARNING: FSAEx_RawReadEx buffer not aligned (%08X). Align to 0x40 for best performance\n", data); + tmp = alignedBuffer; + } + + shim->ioctlvVec[1].vaddr = (void *) tmp; + shim->ioctlvVec[1].len = size_bytes * cnt; + + shim->ioctlvVec[2].vaddr = &shim->response; + shim->ioctlvVec[2].len = sizeof(FSAResponse); + + auto &request = shim->request.rawRead; + request.blocks_offset = blocks_offset; + request.count = cnt; + request.size = size_bytes; + request.device_handle = device_handle; + + auto res = __FSAShimSend(shim, 0); + if (res >= 0 && tmp != data) { + memcpy(data, tmp, size_bytes * cnt); + } + if (tmp != data) { + free(tmp); + } + + free(shim); + return res; +} + +FSError FSAEx_RawWrite(FSClient *client, const void *data, uint32_t size_bytes, uint32_t cnt, uint64_t blocks_offset, int device_handle) { + if (!client) { + return FS_ERROR_INVALID_CLIENTHANDLE; + } + return FSAEx_RawWriteEx(FSGetClientBody(client)->clientHandle, data, size_bytes, cnt, blocks_offset, device_handle); +} + +FSError FSAEx_RawWriteEx(int clientHandle, const void *data, uint32_t size_bytes, uint32_t cnt, uint64_t blocks_offset, int device_handle) { + auto *shim = (FSAShimBuffer *) memalign(0x40, sizeof(FSAShimBuffer)); + if (!shim) { + return FS_ERROR_INVALID_BUFFER; + } + + shim->clientHandle = clientHandle; + shim->ipcReqType = FSA_IPC_REQUEST_IOCTLV; + shim->command = FSA_COMMAND_RAW_WRITE; + + shim->ioctlvVecIn = uint8_t{2}; + shim->ioctlvVecOut = uint8_t{1}; + + shim->ioctlvVec[0].vaddr = &shim->request; + shim->ioctlvVec[0].len = sizeof(FSARequest); + + void *tmp = (void *) data; + if ((uint32_t) data & 0x3F) { + auto *alignedBuffer = memalign(0x40, ROUNDUP(size_bytes * cnt, 0x40)); + if (!alignedBuffer) { + OSReport("## ERROR: FSAEx_RawWriteEx buffer not aligned (%08X).\n", data); + return FS_ERROR_INVALID_ALIGNMENT; + } + OSReport("## WARNING: FSAEx_RawWriteEx buffer not aligned (%08X). Align to 0x40 for best performance\n", data); + tmp = alignedBuffer; + memcpy(tmp, data, size_bytes * cnt); + } + + shim->ioctlvVec[1].vaddr = tmp; + shim->ioctlvVec[1].len = size_bytes * cnt; + + shim->ioctlvVec[2].vaddr = &shim->response; + shim->ioctlvVec[2].len = sizeof(FSAResponse); + + auto &request = shim->request.rawRead; + request.blocks_offset = blocks_offset; + request.count = cnt; + request.size = size_bytes; + request.device_handle = device_handle; + + auto res = __FSAShimSend(shim, 0); + + if (tmp != data) { + free(tmp); + } + + free(shim); + return res; +} \ No newline at end of file diff --git a/source/utils.cpp b/source/utils.cpp new file mode 100644 index 0000000..53b0e7a --- /dev/null +++ b/source/utils.cpp @@ -0,0 +1,249 @@ +#include "utils.h" +#include "mocha/commands.h" +#include "mocha/mocha.h" +#include +#include +#include + +int mochaInitDone = 0; +uint32_t mochaApiVersion = 0; + +MochaUtilsStatus Mocha_InitLibrary() { + mochaInitDone = 1; + mochaApiVersion = 0; + uint32_t version = 0; + if (Mocha_CheckAPIVersion(&version) != MOCHA_RESULT_SUCCESS) { + return MOCHA_RESULT_SUCCESS; + } + + mochaApiVersion = version; + + return MOCHA_RESULT_SUCCESS; +} + +MochaUtilsStatus Mocha_DeInitLibrary() { + mochaInitDone = 0; + mochaApiVersion = 0; + + return MOCHA_RESULT_SUCCESS; +} + +MochaUtilsStatus Mocha_CheckAPIVersion(uint32_t *version) { + if (!version) { + return MOCHA_RESULT_INVALID_ARGUMENT; + } + MochaUtilsStatus res = MOCHA_RESULT_UNKNOWN_ERROR; + int mcpFd = IOS_Open("/dev/mcp", IOS_OPEN_READ); + if (mcpFd >= 0) { + ALIGN_0x40 uint32_t io_buffer[0x100 / 4]; + io_buffer[0] = IPC_CUSTOM_GET_MOCHA_API_VERSION; + + if (IOS_Ioctl(mcpFd, 100, io_buffer, 4, io_buffer, 4) == IOS_ERROR_OK) { + *version = io_buffer[0]; + res = MOCHA_RESULT_SUCCESS; + } else { + res = MOCHA_RESULT_UNSUPPORTED_API_VERSION; + } + + IOS_Close(mcpFd); + } else { + return res; + } + + return res; +} + +MochaUtilsStatus Mocha_GetEnvironmentPath(char *environmentPathBuffer, uint32_t bufferLen) { + if (!mochaInitDone) { + return MOCHA_RESULT_LIB_UNINITIALIZED; + } + if (mochaApiVersion < 1) { + return MOCHA_RESULT_UNSUPPORTED_COMMAND; + } + if (!environmentPathBuffer || bufferLen < 0x100) { + return MOCHA_RESULT_INVALID_ARGUMENT; + } + MochaUtilsStatus res = MOCHA_RESULT_UNKNOWN_ERROR; + int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode) 0); + if (mcpFd >= 0) { + ALIGN_0x40 uint32_t io_buffer[0x100 / 4]; + io_buffer[0] = IPC_CUSTOM_COPY_ENVIRONMENT_PATH; + + if (IOS_Ioctl(mcpFd, 100, io_buffer, 4, io_buffer, 0x100) == IOS_ERROR_OK) { + memcpy(environmentPathBuffer, reinterpret_cast(io_buffer), 0xFF); + res = MOCHA_RESULT_SUCCESS; + } + + IOS_Close(mcpFd); + } + + return res; +} + +MochaUtilsStatus Mocha_SimpleCommand(uint32_t command, uint32_t apiVersion) { + if (!mochaInitDone) { + return MOCHA_RESULT_LIB_UNINITIALIZED; + } + if (mochaApiVersion < apiVersion) { + return MOCHA_RESULT_UNSUPPORTED_COMMAND; + } + MochaUtilsStatus res = MOCHA_RESULT_UNKNOWN_ERROR; + int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode) 0); + if (mcpFd >= 0) { + ALIGN_0x40 uint32_t io_buffer[0x40 / 4]; + io_buffer[0] = command; + + if (IOS_Ioctl(mcpFd, 100, io_buffer, 4, io_buffer, 0x4) == IOS_ERROR_OK) { + res = MOCHA_RESULT_SUCCESS; + } + + IOS_Close(mcpFd); + } + + return res; +} + +MochaUtilsStatus Mocha_RPXHookCompleted() { + return Mocha_SimpleCommand(IPC_CUSTOM_MEN_RPX_HOOK_COMPLETED, 1); +} + +MochaUtilsStatus Mocha_StartMCPThread() { + return Mocha_SimpleCommand(IPC_CUSTOM_START_MCP_THREAD, 1); +} + +MochaUtilsStatus Mocha_StartUSBLogging(bool avoidLogCatchup) { + if (!mochaInitDone) { + return MOCHA_RESULT_LIB_UNINITIALIZED; + } + if (mochaApiVersion < 1) { + return MOCHA_RESULT_UNSUPPORTED_COMMAND; + } + MochaUtilsStatus res = MOCHA_RESULT_UNKNOWN_ERROR; + int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode) 0); + if (mcpFd >= 0) { + ALIGN_0x40 uint32_t io_buffer[0x40 / 4]; + io_buffer[0] = IPC_CUSTOM_START_USB_LOGGING; + io_buffer[1] = avoidLogCatchup; + + if (IOS_Ioctl(mcpFd, 100, io_buffer, 8, io_buffer, 0x4) == IOS_ERROR_OK) { + res = MOCHA_RESULT_SUCCESS; + } + + IOS_Close(mcpFd); + } + + return res; +} + +MochaUtilsStatus Mocha_UnlockFSClient(FSClient *client) { + if (!client) { + return MOCHA_RESULT_INVALID_ARGUMENT; + } + + return Mocha_UnlockFSClientEx(FSGetClientBody(client)->clientHandle); +} + +MochaUtilsStatus Mocha_UnlockFSClientEx(int clientHandle) { + if (!mochaInitDone) { + return MOCHA_RESULT_LIB_UNINITIALIZED; + } + if (mochaApiVersion < 1) { + return MOCHA_RESULT_UNSUPPORTED_COMMAND; + } + ALIGN_0x40 int dummy[0x40 >> 2]; + + auto res = IOS_Ioctl(clientHandle, 0x28, dummy, sizeof(dummy), dummy, sizeof(dummy)); + if (res == 0) { + return MOCHA_RESULT_SUCCESS; + } + if (res == -5) { + return MOCHA_RESULT_MAX_CLIENT; + } + return MOCHA_RESULT_UNKNOWN_ERROR; +} + +MochaUtilsStatus Mocha_LoadRPXOnNextLaunch(MochaRPXLoadInfo *loadInfo) { + if (!mochaInitDone) { + return MOCHA_RESULT_LIB_UNINITIALIZED; + } + if (mochaApiVersion < 1) { + return MOCHA_RESULT_UNSUPPORTED_COMMAND; + } + MochaUtilsStatus res = MOCHA_RESULT_UNKNOWN_ERROR; + int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode) 0); + if (mcpFd >= 0) { + ALIGN_0x40 uint32_t io_buffer[ROUNDUP(sizeof(MochaRPXLoadInfo) + 4, 0x40)]; + io_buffer[0] = IPC_CUSTOM_LOAD_CUSTOM_RPX; + memcpy(&io_buffer[1], loadInfo, sizeof(MochaRPXLoadInfo)); + + if (IOS_Ioctl(mcpFd, 100, io_buffer, sizeof(MochaRPXLoadInfo) + 4, io_buffer, 0x4) == IOS_ERROR_OK) { + res = MOCHA_RESULT_SUCCESS; + } + + IOS_Close(mcpFd); + } + + return res; +} + +MochaUtilsStatus Mocha_ODMGetDiscKey(WUDDiscKey *discKey) { + if (!mochaInitDone) { + return MOCHA_RESULT_LIB_UNINITIALIZED; + } + if (mochaApiVersion < 1) { + return MOCHA_RESULT_UNSUPPORTED_COMMAND; + } + if (!discKey) { + return MOCHA_RESULT_INVALID_ARGUMENT; + } + int odm_handle = IOS_Open("/dev/odm", IOS_OPEN_READ); + MochaUtilsStatus res = MOCHA_RESULT_UNKNOWN_ERROR; + if (odm_handle >= 0) { + ALIGN_0x40 uint32_t io_buffer[0x40 / 4]; + // disc encryption key, only works with patched IOSU + io_buffer[0] = 3; + + if (IOS_Ioctl(odm_handle, 0x06, io_buffer, 0x14, io_buffer, 0x20) == IOS_ERROR_OK) { + memcpy(discKey, io_buffer, 16); + res = MOCHA_RESULT_SUCCESS; + } + IOS_Close(odm_handle); + } + return res; +} + +extern int bspRead(const char *, uint32_t, const char *, uint32_t, uint16_t *); +MochaUtilsStatus Mocha_SEEPROMRead(uint8_t *out_buffer, uint32_t offset, uint32_t size) { + if (!mochaInitDone) { + return MOCHA_RESULT_LIB_UNINITIALIZED; + } + if (mochaApiVersion < 1) { + return MOCHA_RESULT_UNSUPPORTED_COMMAND; + } + if (out_buffer == nullptr || offset > 0x200 || offset & 0x01) { + return MOCHA_RESULT_INVALID_ARGUMENT; + } + + uint32_t sizeInShorts = size >> 1; + uint32_t offsetInShorts = offset >> 1; + int32_t maxReadCount = 0x100 - offsetInShorts; + + if (maxReadCount <= 0) { + return MOCHA_RESULT_SUCCESS; + } + + uint32_t count = sizeInShorts > (uint32_t) maxReadCount ? (uint32_t) maxReadCount : sizeInShorts; + auto *ptr = (uint16_t *) out_buffer; + + int res = 0; + + for (uint32_t i = 0; i < count; i++) { + if (bspRead("EE", offsetInShorts + i, "access", 2, ptr) != 0) { + return MOCHA_RESULT_UNKNOWN_ERROR; + } + res += 2; + ptr++; + } + + return static_cast(res); +} diff --git a/source/utils.h b/source/utils.h new file mode 100644 index 0000000..c8c2a19 --- /dev/null +++ b/source/utils.h @@ -0,0 +1,12 @@ +#pragma once +#include +#include +#include + +#define ALIGN(align) __attribute__((aligned(align))) +#define ALIGN_0x40 ALIGN(0x40) +#define ROUNDUP(x, align) (((x) + ((align) -1)) & ~((align) -1)) + +#define __FSAShimSetupRequestMount ((FSError(*)(FSAShimBuffer *, uint32_t, const char *, const char *, uint32_t, void *, uint32_t))(0x101C400 + 0x042f88)) +#define __FSAShimSetupRequestUnmount ((FSError(*)(FSAShimBuffer *, uint32_t, const char *, uint32_t))(0x101C400 + 0x43130)) +#define __FSAShimSend ((FSError(*)(FSAShimBuffer *, uint32_t))(0x101C400 + 0x042d90))