diff --git a/Dockerfile b/Dockerfile index 7f29317..6a9d904 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,9 @@ -FROM wiiuenv/devkitppc:20220212 +FROM wiiuenv/devkitppc:20220213 COPY --from=wiiuenv/wiiumodulesystem:20220127 /artifacts $DEVKITPRO COPY --from=wiiuenv/wiiupluginsystem:20220123 /artifacts $DEVKITPRO COPY --from=wiiuenv/libfunctionpatcher:20210924 /artifacts $DEVKITPRO COPY --from=wiiuenv/libmappedmemory:20210924 /artifacts $DEVKITPRO -COPY --from=wiiuenv/libwupsbackend:20211001 /artifacts $DEVKITPRO +COPY --from=wiiuenv/libwupsbackend:20220213 /artifacts $DEVKITPRO WORKDIR project \ No newline at end of file diff --git a/source/fs/CFile.cpp b/source/fs/CFile.cpp index ad0f902..e4b0fe8 100644 --- a/source/fs/CFile.cpp +++ b/source/fs/CFile.cpp @@ -1,4 +1,5 @@ +#include "utils/utils.h" #include #include #include @@ -156,7 +157,7 @@ int32_t CFile::seek(long int offset, int32_t origin) { } int32_t CFile::fwrite(const char *format, ...) { - char tmp[512]; + ALIGN_DATA_0x40 char tmp[512]; tmp[0] = 0; int32_t result = -1; diff --git a/source/fs/FSUtils.cpp b/source/fs/FSUtils.cpp index 52fc4c1..275c738 100644 --- a/source/fs/FSUtils.cpp +++ b/source/fs/FSUtils.cpp @@ -1,32 +1,35 @@ #include "fs/FSUtils.h" #include "fs/CFile.hpp" #include "utils/logger.h" +#include "utils/utils.h" +#include +#include #include #include -#include -#include #include int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size) { //! always initialze input - *inbuffer = nullptr; - if (size) + *inbuffer = NULL; + if (size) { *size = 0; + } int32_t iFd = open(filepath, O_RDONLY); - if (iFd < 0) + if (iFd < 0) { return -1; + } uint32_t filesize = lseek(iFd, 0, SEEK_END); lseek(iFd, 0, SEEK_SET); - uint8_t *buffer = (uint8_t *) malloc(filesize); + auto *buffer = (uint8_t *) memalign(0x40, ROUNDUP(filesize, 0x40)); if (buffer == nullptr) { close(iFd); return -2; } - uint32_t blocksize = 0x4000; + uint32_t blocksize = 0x20000; uint32_t done = 0; int32_t readBytes = 0; @@ -40,7 +43,7 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_ done += readBytes; } - close(iFd); + ::close(iFd); if (done != filesize) { free(buffer); diff --git a/source/plugin/PluginDataFactory.cpp b/source/plugin/PluginDataFactory.cpp index 4c9b96f..1d87433 100644 --- a/source/plugin/PluginDataFactory.cpp +++ b/source/plugin/PluginDataFactory.cpp @@ -15,14 +15,12 @@ * along with this program. If not, see . ****************************************************************************/ #include "PluginDataFactory.h" +#include "../fs/FSUtils.h" #include "../utils/StringTools.h" -#include "../utils/logger.h" #include -#include #include #include - std::vector> PluginDataFactory::loadDir(const std::string &path, MEMHeapHandle heapHandle) { std::vector> result; struct dirent *dp; @@ -65,28 +63,17 @@ std::vector> PluginDataFactory::loadDir(const std::s } std::optional> PluginDataFactory::load(const std::string &filename, MEMHeapHandle heapHandle) { - // Not going to explicitly check these. - // The use of gcount() below will compensate for a failure here. - std::ifstream is(filename, std::ios::binary); - - is.seekg(0, std::ios::end); - std::streampos length = is.tellg(); - is.seekg(0, std::ios::beg); - - // reading into a 0x40 aligned buffer increases reading speed. - char *data = (char *) memalign(0x40, length); - if (!data) { - is.close(); - DEBUG_FUNCTION_LINE("Failed to alloc memory for holding the plugin"); + uint8_t *buffer = nullptr; + uint32_t fsize = 0; + if (FSUtils::LoadFileToMem(filename.c_str(), &buffer, &fsize) < 0) { + DEBUG_FUNCTION_LINE("Failed to load file"); return {}; } - is.read(data, length); std::vector result; - result.resize(length); - memcpy(&result[0], data, length); - free(data); - is.close(); + result.resize(fsize); + memcpy(&result[0], buffer, fsize); + free(buffer); DEBUG_FUNCTION_LINE_VERBOSE("Loaded file!"); diff --git a/source/plugin/PluginMetaInformationFactory.cpp b/source/plugin/PluginMetaInformationFactory.cpp index 83098b4..e5f8ab1 100644 --- a/source/plugin/PluginMetaInformationFactory.cpp +++ b/source/plugin/PluginMetaInformationFactory.cpp @@ -16,6 +16,7 @@ ****************************************************************************/ #include "PluginMetaInformationFactory.h" +#include "../fs/FSUtils.h" #include "../utils/StringTools.h" #include #include @@ -32,18 +33,28 @@ std::optional> PluginMetaInformationFacto elfio reader; if (!reader.load((char *) pluginData->buffer, pluginData->length)) { DEBUG_FUNCTION_LINE("Can't process PluginData in elfio"); - return std::nullopt; + return {}; } return loadPlugin(reader); } std::optional> PluginMetaInformationFactory::loadPlugin(std::string &filePath) { elfio reader; - if (!reader.load(filePath)) { - DEBUG_FUNCTION_LINE("Can't find or process ELF file"); - return std::nullopt; + + uint8_t *buffer = nullptr; + uint32_t length = 0; + if (FSUtils::LoadFileToMem(filePath.c_str(), &buffer, &length) < 0) { + DEBUG_FUNCTION_LINE("Failed to load file to memory"); + return {}; } - return loadPlugin(reader); + + if (!reader.load((char *) buffer, length)) { + DEBUG_FUNCTION_LINE("Can't process PluginData in elfio"); + return {}; + } + auto res = loadPlugin(reader); + free(buffer); + return res; } std::optional> PluginMetaInformationFactory::loadPlugin(char *buffer, size_t size) { diff --git a/source/utils/StorageUtils.cpp b/source/utils/StorageUtils.cpp index 18ed01f..f6fb2e4 100644 --- a/source/utils/StorageUtils.cpp +++ b/source/utils/StorageUtils.cpp @@ -3,6 +3,7 @@ #include "fs/CFile.hpp" #include "fs/FSUtils.h" +#include "utils.h" #include "utils/json.hpp" #include "utils/logger.h" @@ -58,7 +59,7 @@ int StorageUtils::OpenStorage(const char *plugin_id, wups_storage_item_t *items) nlohmann::json j; CFile file(filePath, CFile::ReadOnly); if (file.isOpen() && file.size() > 0) { - uint8_t *json_data = new uint8_t[file.size() + 1]; + auto *json_data = (uint8_t *) memalign(0x40, ROUNDUP(file.size() + 1, 0x40)); json_data[file.size()] = '\0'; file.read(json_data, file.size()); @@ -127,7 +128,10 @@ int StorageUtils::CloseStorage(const char *plugin_id, wups_storage_item_t *items j["storageitems"] = processItems(items); std::string jsonString = j.dump(4); - file.write((const uint8_t *) jsonString.c_str(), jsonString.size()); + auto writeSize = jsonString.size(); + auto *data = (uint8_t *) memalign(0x40, ROUNDUP(writeSize, 0x40)); + memcpy(data, jsonString.c_str(), writeSize); + file.write(data, writeSize); file.close(); return WUPS_STORAGE_ERROR_SUCCESS; } diff --git a/source/utils/utils.h b/source/utils/utils.h index 2890779..201b575 100644 --- a/source/utils/utils.h +++ b/source/utils/utils.h @@ -22,6 +22,9 @@ extern "C" { #define ALIGN4(x) (((x) + 3) & ~3) #define ALIGN32(x) (((x) + 31) & ~31) +#define ALIGN_DATA(align) __attribute__((aligned(align))) +#define ALIGN_DATA_0x40 ALIGN_DATA(0x40) + // those work only in powers of 2 #define ROUNDDOWN(val, align) ((val) & ~(align - 1)) #define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align)