From 746bf8530158232ad15627099598a0ef4a412835 Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 31 Aug 2022 13:20:59 +0200 Subject: [PATCH] Fix IOSUKernelWrite32/IOSUKernelRead32, renamed IOSUKernelWrite/IOSUKernelRead to IOSUMemoryWrite/IOSUMemoryRead to match the actual behaviour --- include/mocha/mocha.h | 16 +++++++-------- source/utils.cpp | 47 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/include/mocha/mocha.h b/include/mocha/mocha.h index 0815ef4..d09b293 100644 --- a/include/mocha/mocha.h +++ b/include/mocha/mocha.h @@ -52,7 +52,7 @@ MochaUtilsStatus Mocha_DeinitLibrary(); MochaUtilsStatus Mocha_CheckAPIVersion(uint32_t *outVersion); /** - * Copies data within IOSU with kernel permission. + * Copies data within IOSU with MCP permission. * @param dst - Destination address * @param src - Source address * @param size - Bytes to copy. @@ -61,10 +61,10 @@ MochaUtilsStatus Mocha_CheckAPIVersion(uint32_t *outVersion); * MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.
* MOCHA_RESULT_UNKNOWN_ERROR: Unknown error */ -MochaUtilsStatus Mocha_IOSUKernelMemcpy(uint32_t dst, uint32_t src, uint32_t size); +MochaUtilsStatus Mocha_IOSUMemoryMemcpy(uint32_t dst, uint32_t src, uint32_t size); /** - * Writes data to a given address with kernel permission. + * Writes data to a given address with MCP permission. * @param address - Address where the data will be written to. * @param buffer - Pointer to the data which should be written. * @param size - Bytes to write. @@ -74,10 +74,10 @@ MochaUtilsStatus Mocha_IOSUKernelMemcpy(uint32_t dst, uint32_t src, uint32_t siz * MOCHA_RESULT_OUT_OF_MEMORY: Not enough memory
* MOCHA_RESULT_UNKNOWN_ERROR: Unknown error */ -MochaUtilsStatus Mocha_IOSUKernelWrite(uint32_t address, const uint8_t *buffer, uint32_t size); +MochaUtilsStatus Mocha_IOSUMemoryWrite(uint32_t address, const uint8_t *buffer, uint32_t size); /** - * Reads data from a given address with kernel permission. + * Reads data from a given address with MCP permission. * @param address - Address where the data will be read from. * @param buffer - Pointer to the buffer where the read will be stored * @param size - Bytes to read. @@ -86,10 +86,10 @@ MochaUtilsStatus Mocha_IOSUKernelWrite(uint32_t address, const uint8_t *buffer, * MOCHA_RESULT_LIB_UNINITIALIZED: Library was not initialized. Call Mocha_InitLibrary() before using this function.
* MOCHA_RESULT_UNKNOWN_ERROR: Unknown error */ -MochaUtilsStatus Mocha_IOSUKernelRead(uint32_t address, uint8_t *out_buffer, uint32_t size); +MochaUtilsStatus Mocha_IOSUMemoryRead(uint32_t address, uint8_t *out_buffer, uint32_t size); /** - * Write 4 bytes with IOSU kernel permission + * Writes 4 bytes with IOSU kernel permission * @param address Address where the value will be written. * @param value Value that will be written to address. * @return MOCHA_RESULT_SUCCESS: The data has been written successfully
@@ -112,7 +112,7 @@ MochaUtilsStatus Mocha_IOSUKernelWrite32(uint32_t address, uint32_t value); MochaUtilsStatus Mocha_IOSUKernelRead32(uint32_t address, uint32_t *out_buffer); /** - * Read the consoles OTP into the given buffer. + * Reads the consoles OTP into the given buffer. * * @param out_buffer Buffer where the result will be stored. * @return MOCHA_RESULT_SUCCESS: The OTP has been read into the buffer
diff --git a/source/utils.cpp b/source/utils.cpp index c89d967..2b3d6c9 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -150,7 +150,7 @@ MochaUtilsStatus Mocha_IOSUKernelMemcpy(uint32_t dst, uint32_t src, uint32_t siz return res >= 0 ? MOCHA_RESULT_SUCCESS : MOCHA_RESULT_UNKNOWN_ERROR; } -MochaUtilsStatus Mocha_IOSUKernelWrite(uint32_t address, const uint8_t *buffer, uint32_t size) { +MochaUtilsStatus Mocha_IOSUMemoryWrite(uint32_t address, const uint8_t *buffer, uint32_t size) { if (size == 0) { return MOCHA_RESULT_SUCCESS; } @@ -175,7 +175,7 @@ MochaUtilsStatus Mocha_IOSUKernelWrite(uint32_t address, const uint8_t *buffer, return res >= 0 ? MOCHA_RESULT_SUCCESS : MOCHA_RESULT_UNKNOWN_ERROR; } -MochaUtilsStatus Mocha_IOSUKernelRead(uint32_t address, uint8_t *out_buffer, uint32_t size) { +MochaUtilsStatus Mocha_IOSUMemoryRead(uint32_t address, uint8_t *out_buffer, uint32_t size) { if (size == 0) { return MOCHA_RESULT_SUCCESS; } @@ -209,11 +209,50 @@ MochaUtilsStatus Mocha_IOSUKernelRead(uint32_t address, uint8_t *out_buffer, uin } MochaUtilsStatus Mocha_IOSUKernelWrite32(uint32_t address, uint32_t value) { - return Mocha_IOSUKernelWrite(address, reinterpret_cast(&value), 4); + if (address == 0) { + return MOCHA_RESULT_INVALID_ARGUMENT; + } + if (!mochaInitDone || iosuhaxHandle < 0) { + return MOCHA_RESULT_LIB_UNINITIALIZED; + } + + ALIGN_0x40 uint32_t io_buf[0x40 >> 2]; + io_buf[0] = address; + io_buf[1] = value; + + auto res = IOS_Ioctl(iosuhaxHandle, IOCTL_KERN_WRITE32, io_buf, 2 * sizeof(uint32_t), 0, 0); + return res >= 0 ? MOCHA_RESULT_SUCCESS : MOCHA_RESULT_UNKNOWN_ERROR; } MochaUtilsStatus Mocha_IOSUKernelRead32(uint32_t address, uint32_t *out_buffer) { - return Mocha_IOSUKernelRead(address, reinterpret_cast(out_buffer), 4); + if (address == 0 || out_buffer == nullptr) { + return MOCHA_RESULT_INVALID_ARGUMENT; + } + if (!mochaInitDone || iosuhaxHandle < 0) { + return MOCHA_RESULT_LIB_UNINITIALIZED; + } + + ALIGN_0x40 uint32_t io_buf[0x40 >> 2]; + io_buf[0] = address; + + void *tmp_buf = NULL; + int32_t count = 1; + + if (((uintptr_t) out_buffer & 0x3F) || ((count * 4) & 0x3F)) { + tmp_buf = (uint32_t *) memalign(0x40, ROUNDUP((count * 4), 0x40)); + if (!tmp_buf) { + return MOCHA_RESULT_OUT_OF_MEMORY; + } + } + + int res = IOS_Ioctl(iosuhaxHandle, IOCTL_KERN_READ32, io_buf, sizeof(address), tmp_buf ? tmp_buf : out_buffer, count * 4); + + if (res >= 0 && tmp_buf) { + memcpy(out_buffer, tmp_buf, count * 4); + } + + free(tmp_buf); + return res >= 0 ? MOCHA_RESULT_SUCCESS : MOCHA_RESULT_UNKNOWN_ERROR; } MochaUtilsStatus Mocha_ReadOTP(WiiUConsoleOTP *out_buffer) {