WiiUPluginLoaderBackend/source/plugin/PluginContainerPersistence.cpp

349 lines
17 KiB
C++
Raw Normal View History

#include <coreinit/cache.h>
2021-12-15 17:09:30 +01:00
#include <memory>
2022-02-04 16:25:44 +01:00
#include "DynamicLinkingHelper.h"
#include "PluginContainer.h"
#include "PluginContainerPersistence.h"
2020-05-17 20:45:10 +02:00
#include "PluginDataPersistence.h"
2022-02-04 16:25:44 +01:00
#include "PluginInformationFactory.h"
#include "PluginMetaInformationFactory.h"
2021-12-15 17:09:30 +01:00
bool PluginContainerPersistence::savePlugin(plugin_information_t *pluginInformation, const std::shared_ptr<PluginContainer> &plugin, MEMHeapHandle heapHandle) {
int32_t plugin_count = pluginInformation->number_used_plugins;
2021-12-15 17:09:30 +01:00
auto pluginName = plugin->getMetaInformation()->getName();
2020-05-03 12:30:15 +02:00
if (plugin_count >= MAXIMUM_PLUGINS - 1) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Maximum of %d plugins reached. %s won't be loaded!", MAXIMUM_PLUGINS, pluginName.c_str());
return false;
}
// Copy data to global struct.
2020-05-03 12:30:15 +02:00
plugin_information_single_t *plugin_data = &(pluginInformation->plugin_data[plugin_count]);
// Make sure everything is reset.
2020-05-03 12:30:15 +02:00
memset((void *) plugin_data, 0, sizeof(plugin_information_single_t));
2021-12-15 17:09:30 +01:00
const auto &pluginMetaInfo = plugin->getMetaInformation();
2022-02-04 16:25:44 +01:00
auto plugin_meta_data = &plugin_data->meta;
2021-12-15 17:09:30 +01:00
if (pluginMetaInfo->getName().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Warning: name will be truncated.");
}
2021-12-15 17:09:30 +01:00
strncpy(plugin_meta_data->name, pluginMetaInfo->getName().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH - 1);
if (pluginMetaInfo->getAuthor().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Warning: author will be truncated.");
}
2021-12-15 17:09:30 +01:00
strncpy(plugin_meta_data->author, pluginMetaInfo->getAuthor().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH - 1);
2021-12-15 17:09:30 +01:00
if (pluginMetaInfo->getVersion().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Warning: version will be truncated.");
}
2021-12-15 17:09:30 +01:00
strncpy(plugin_meta_data->version, pluginMetaInfo->getVersion().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH - 1);
2021-12-15 17:09:30 +01:00
if (pluginMetaInfo->getLicense().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Warning: license will be truncated.");
}
2021-12-15 17:09:30 +01:00
strncpy(plugin_meta_data->license, pluginMetaInfo->getLicense().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH - 1);
2021-12-15 17:09:30 +01:00
if (pluginMetaInfo->getBuildTimestamp().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Warning: build timestamp will be truncated.");
}
2021-12-15 17:09:30 +01:00
strncpy(plugin_meta_data->buildTimestamp, pluginMetaInfo->getBuildTimestamp().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH - 1);
2021-12-15 17:09:30 +01:00
if (pluginMetaInfo->getDescription().size() >= MAXIMUM_PLUGIN_DESCRIPTION_LENGTH) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Warning: description will be truncated.");
DEBUG_FUNCTION_LINE_ERR("%s", pluginMetaInfo->getDescription().c_str());
}
2021-12-15 17:09:30 +01:00
strncpy(plugin_meta_data->descripion, pluginMetaInfo->getDescription().c_str(), MAXIMUM_PLUGIN_DESCRIPTION_LENGTH - 1);
2021-12-15 17:09:30 +01:00
if (pluginMetaInfo->getStorageId().length() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Warning: plugin storage id will be truncated.");
}
2021-12-15 17:09:30 +01:00
strncpy(plugin_meta_data->storageId, pluginMetaInfo->getStorageId().c_str(), MAXIMUM_PLUGIN_META_FIELD_LENGTH - 1);
2021-12-15 17:09:30 +01:00
plugin_meta_data->size = pluginMetaInfo->getSize();
2021-12-15 17:09:30 +01:00
auto pluginInfo = plugin->getPluginInformation();
// Relocation
2021-12-15 17:09:30 +01:00
auto relocationData = pluginInfo->getRelocationDataList();
2022-02-04 16:25:44 +01:00
for (auto &reloc : relocationData) {
2020-05-03 12:30:15 +02:00
if (!DynamicLinkingHelper::addReloationEntry(&(pluginInformation->linking_data), plugin_data->info.linking_entries, PLUGIN_DYN_LINK_RELOCATION_LIST_LENGTH, reloc)) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Failed to add a relocation entry");
return false;
}
}
2021-12-15 17:09:30 +01:00
auto function_data_list = pluginInfo->getFunctionDataList();
2022-02-04 16:25:44 +01:00
auto hook_data_list = pluginInfo->getHookDataList();
2020-05-03 12:30:15 +02:00
if (function_data_list.size() > MAXIMUM_FUNCTION_PER_PLUGIN) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Plugin %s would replace to many function (%d, maximum is %d). It won't be loaded.", pluginName.c_str(), function_data_list.size(), MAXIMUM_FUNCTION_PER_PLUGIN);
return false;
}
2020-05-03 12:30:15 +02:00
if (hook_data_list.size() > MAXIMUM_HOOKS_PER_PLUGIN) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Plugin %s would set too many hooks (%d, maximum is %d). It won't be loaded.", pluginName.c_str(), hook_data_list.size(), MAXIMUM_HOOKS_PER_PLUGIN);
return false;
}
2020-05-03 12:30:15 +02:00
if (pluginName.length() > MAXIMUM_PLUGIN_NAME_LENGTH - 1) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Name for plugin %s was too long to be stored.", pluginName.c_str());
return false;
}
/* Store function replacement information */
uint32_t i = 0;
2022-02-04 16:25:44 +01:00
for (auto &curFunction : pluginInfo->getFunctionDataList()) {
function_replacement_data_t *function_data = &plugin_data->info.functions[i];
2021-12-15 17:09:30 +01:00
if (strlen(curFunction->getName().c_str()) > MAXIMUM_FUNCTION_NAME_LENGTH - 1) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Could not add function \"%s\" for plugin \"%s\" function name is too long.", curFunction->getName().c_str(), pluginName.c_str());
continue;
}
2021-12-15 17:09:30 +01:00
DEBUG_FUNCTION_LINE_VERBOSE("Adding function \"%s\" for plugin \"%s\"", curFunction->getName().c_str(), pluginName.c_str());
2021-12-15 17:09:30 +01:00
strncpy(function_data->function_name, curFunction->getName().c_str(), MAXIMUM_FUNCTION_NAME_LENGTH - 1);
2022-02-04 16:25:44 +01:00
function_data->VERSION = FUNCTION_REPLACEMENT_DATA_STRUCT_VERSION;
function_data->library = (function_replacement_library_type_t) curFunction->getLibrary();
function_data->replaceAddr = (uint32_t) curFunction->getReplaceAddress();
function_data->replaceCall = (uint32_t) curFunction->getReplaceCall();
function_data->physicalAddr = (uint32_t) curFunction->getPhysicalAddress();
function_data->virtualAddr = (uint32_t) curFunction->getVirtualAddress();
2021-12-15 17:09:30 +01:00
function_data->targetProcess = curFunction->getTargetProcess();
plugin_data->info.number_used_functions++;
i++;
}
i = 0;
2022-02-04 16:25:44 +01:00
for (auto &curHook : pluginInfo->getHookDataList()) {
2020-05-03 12:30:15 +02:00
replacement_data_hook_t *hook_data = &plugin_data->info.hooks[i];
2021-12-15 17:09:30 +01:00
DEBUG_FUNCTION_LINE_VERBOSE("Set hook for plugin \"%s\" of type %08X to target %08X", plugin_data->meta.name, curHook->getType(), (void *) curHook->getFunctionPointer());
2021-12-15 17:09:30 +01:00
hook_data->func_pointer = (void *) curHook->getFunctionPointer();
2022-02-04 16:25:44 +01:00
hook_data->type = curHook->getType();
plugin_data->info.number_used_hooks++;
i++;
}
/* Saving SectionInfos */
2022-02-04 16:25:44 +01:00
for (auto &curSection : pluginInfo->getSectionInfoList()) {
bool foundFreeSlot = false;
2022-02-04 16:25:44 +01:00
uint32_t slot = 0;
2021-09-25 14:26:18 +02:00
for (uint32_t j = 0; j < MAXIMUM_PLUGIN_SECTION_LENGTH; j++) {
2021-12-15 17:09:30 +01:00
auto *sectionInfo = &(plugin_data->info.sectionInfos[j]);
2020-05-03 12:30:15 +02:00
if (sectionInfo->addr == 0 && sectionInfo->size == 0) {
foundFreeSlot = true;
2022-02-04 16:25:44 +01:00
slot = j;
break;
}
}
2020-05-03 12:30:15 +02:00
if (foundFreeSlot) {
2021-12-15 17:09:30 +01:00
auto *sectionInfo = &(plugin_data->info.sectionInfos[slot]);
2021-09-25 14:26:18 +02:00
if (curSection.first.length() > MAXIMUM_PLUGIN_SECTION_NAME_LENGTH - 1) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Could not add section info \"%s\" for plugin \"%s\" section name is too long.", curSection.first.c_str(), pluginName.c_str());
break;
}
2020-05-03 12:30:15 +02:00
strncpy(sectionInfo->name, curSection.first.c_str(), MAXIMUM_PLUGIN_SECTION_NAME_LENGTH - 1);
2021-12-15 17:09:30 +01:00
sectionInfo->addr = curSection.second->getAddress();
sectionInfo->size = curSection.second->getSize();
} else {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Failed to store SectionInfos");
return false;
}
}
2022-02-04 16:25:44 +01:00
plugin_data->info.trampolineId = pluginInfo->getTrampolineId();
2021-12-15 17:09:30 +01:00
plugin_data->info.allocatedTextMemoryAddress = pluginInfo->allocatedTextMemoryAddress;
plugin_data->info.allocatedDataMemoryAddress = pluginInfo->allocatedDataMemoryAddress;
2021-12-15 17:09:30 +01:00
uint32_t entryCount = pluginInfo->getFunctionSymbolDataList().size();
if (entryCount > 0) {
2021-12-15 17:09:30 +01:00
// Saving SectionInfos
uint32_t funcSymStringLen = 1;
2022-02-04 16:25:44 +01:00
for (auto &curFuncSym : pluginInfo->getFunctionSymbolDataList()) {
2021-12-15 17:09:30 +01:00
funcSymStringLen += curFuncSym->getName().length() + 1;
}
char *stringTable = (char *) MEMAllocFromExpHeapEx(heapHandle, funcSymStringLen, 0x4);
if (stringTable == nullptr) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Failed alloc memory to store string table for function symbol data");
return false;
}
memset(stringTable, 0, funcSymStringLen);
DEBUG_FUNCTION_LINE("Allocated %d for the function symbol string table", funcSymStringLen);
auto *entryTable = (plugin_function_symbol_data_t *) MEMAllocFromExpHeapEx(heapHandle, entryCount * sizeof(plugin_function_symbol_data_t), 0x4);
if (entryTable == nullptr) {
MEMFreeToExpHeap((MEMHeapHandle) heapHandle, stringTable);
free(stringTable);
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Failed alloc memory to store function symbol data");
return false;
}
DEBUG_FUNCTION_LINE("Allocated %d for the function symbol data", entryCount * sizeof(plugin_function_symbol_data_t));
uint32_t curStringOffset = 0;
2022-02-04 16:25:44 +01:00
uint32_t curEntryIndex = 0;
for (auto &curFuncSym : pluginInfo->getFunctionSymbolDataList()) {
2021-12-15 17:09:30 +01:00
entryTable[curEntryIndex].address = curFuncSym->getAddress();
2022-02-04 16:25:44 +01:00
entryTable[curEntryIndex].name = &stringTable[curStringOffset];
entryTable[curEntryIndex].size = curFuncSym->getSize();
auto len = curFuncSym->getName().length() + 1;
2021-12-15 17:09:30 +01:00
memcpy(stringTable + curStringOffset, curFuncSym->getName().c_str(), len);
curStringOffset += len;
curEntryIndex++;
}
plugin_data->info.allocatedFuncSymStringTableAddress = stringTable;
2022-02-04 16:25:44 +01:00
plugin_data->info.function_symbol_data = entryTable;
}
plugin_data->info.number_function_symbol_data = entryCount;
/* Copy plugin data */
2022-02-04 16:25:44 +01:00
auto pluginData = plugin->getPluginData();
2020-05-03 12:30:15 +02:00
auto plugin_data_data = &plugin_data->data;
2020-05-17 20:45:10 +02:00
PluginDataPersistence::save(plugin_data_data, pluginData);
pluginInformation->number_used_plugins++;
2020-05-03 12:30:15 +02:00
DCFlushRange((void *) pluginInformation, sizeof(plugin_information_t));
ICInvalidateRange((void *) pluginInformation, sizeof(plugin_information_t));
return true;
}
2021-12-15 17:09:30 +01:00
std::vector<std::shared_ptr<PluginContainer>> PluginContainerPersistence::loadPlugins(plugin_information_t *pluginInformation) {
std::vector<std::shared_ptr<PluginContainer>> result;
2020-12-26 14:17:50 +01:00
if (pluginInformation == nullptr) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("pluginInformation == nullptr");
return result;
}
2020-05-03 12:30:15 +02:00
DCFlushRange((void *) pluginInformation, sizeof(plugin_information_t));
ICInvalidateRange((void *) pluginInformation, sizeof(plugin_information_t));
int32_t plugin_count = pluginInformation->number_used_plugins;
2020-05-03 12:30:15 +02:00
if (plugin_count > MAXIMUM_PLUGINS) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("pluginInformation->plugin_count was bigger then allowed. %d > %d. Limiting to %d", plugin_count, MAXIMUM_PLUGINS, MAXIMUM_PLUGINS);
plugin_count = MAXIMUM_PLUGINS;
}
2020-05-03 12:30:15 +02:00
for (int32_t i = 0; i < plugin_count; i++) {
// Copy data from struct.
2020-05-03 12:30:15 +02:00
plugin_information_single_t *plugin_data = &(pluginInformation->plugin_data[i]);
2021-12-15 17:09:30 +01:00
auto metaInformation = std::shared_ptr<PluginMetaInformation>(new PluginMetaInformation);
2020-05-03 12:30:15 +02:00
plugin_meta_info_t *meta = &(plugin_data->meta);
2021-12-15 17:09:30 +01:00
metaInformation->setAuthor(meta->author);
metaInformation->setVersion(meta->version);
metaInformation->setBuildTimestamp(meta->buildTimestamp);
metaInformation->setLicense(meta->license);
metaInformation->setDescription(meta->descripion);
metaInformation->setSize(meta->size);
metaInformation->setName(meta->name);
metaInformation->setStorageId(meta->storageId);
2020-05-03 12:30:15 +02:00
plugin_data_t *data = &(plugin_data->data);
2021-12-15 17:09:30 +01:00
auto pluginData = PluginDataPersistence::load(data);
2021-12-15 17:09:30 +01:00
auto curPluginInformation = std::make_shared<PluginInformation>();
2022-01-27 12:47:33 +01:00
curPluginInformation->setTrampolineId(plugin_data->info.trampolineId);
2021-12-15 17:09:30 +01:00
curPluginInformation->allocatedTextMemoryAddress = plugin_data->info.allocatedTextMemoryAddress;
curPluginInformation->allocatedDataMemoryAddress = plugin_data->info.allocatedDataMemoryAddress;
2022-02-04 16:25:44 +01:00
for (auto &curItem : plugin_data->info.sectionInfos) {
2020-12-26 14:17:50 +01:00
plugin_section_info_t *sectionInfo = &curItem;
2020-05-03 12:30:15 +02:00
if (sectionInfo->addr == 0 && sectionInfo->size == 0) {
continue;
}
2021-02-19 19:41:04 +01:00
DEBUG_FUNCTION_LINE_VERBOSE("Add SectionInfo %s", sectionInfo->name);
2020-12-26 14:17:50 +01:00
std::string name(sectionInfo->name);
2021-12-15 17:09:30 +01:00
curPluginInformation->addSectionInfo(std::make_shared<SectionInfo>(name, sectionInfo->addr, sectionInfo->size));
}
/* load hook data */
uint32_t hookCount = plugin_data->info.number_used_hooks;
2020-05-03 12:30:15 +02:00
if (hookCount > MAXIMUM_HOOKS_PER_PLUGIN) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("number_used_hooks was bigger then allowed. %d > %d. Limiting to %d", hookCount, MAXIMUM_HOOKS_PER_PLUGIN, MAXIMUM_HOOKS_PER_PLUGIN);
hookCount = MAXIMUM_HOOKS_PER_PLUGIN;
}
2020-05-03 12:30:15 +02:00
for (uint32_t j = 0; j < hookCount; j++) {
replacement_data_hook_t *hook_entry = &(plugin_data->info.hooks[j]);
2021-12-15 17:09:30 +01:00
curPluginInformation->addHookData(std::make_shared<HookData>(hook_entry->func_pointer, hook_entry->type));
}
2021-10-01 17:25:48 +02:00
bool storageHasId = true;
2022-02-04 16:25:44 +01:00
for (auto const &value : curPluginInformation->getHookDataList()) {
2021-12-15 17:09:30 +01:00
if (value->getType() == WUPS_LOADER_HOOK_INIT_STORAGE &&
metaInformation->getStorageId().empty()) {
2021-10-01 17:25:48 +02:00
storageHasId = false;
}
}
if (!storageHasId) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Plugin is using the storage API but has not set an ID");
2021-10-01 17:25:48 +02:00
continue;
}
/* load function replacement data */
uint32_t functionReplaceCount = plugin_data->info.number_used_functions;
2020-05-03 12:30:15 +02:00
if (functionReplaceCount > MAXIMUM_FUNCTION_PER_PLUGIN) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("number_used_functions was bigger then allowed. %d > %d. Limiting to %d", functionReplaceCount, MAXIMUM_FUNCTION_PER_PLUGIN, MAXIMUM_FUNCTION_PER_PLUGIN);
functionReplaceCount = MAXIMUM_FUNCTION_PER_PLUGIN;
}
2020-05-03 12:30:15 +02:00
for (uint32_t j = 0; j < functionReplaceCount; j++) {
function_replacement_data_t *entry = &(plugin_data->info.functions[j]);
2022-02-04 16:25:44 +01:00
auto func = std::make_shared<FunctionData>((void *) entry->physicalAddr, (void *) entry->virtualAddr, entry->function_name, (function_replacement_library_type_t) entry->library,
2021-12-15 17:09:30 +01:00
(void *) entry->replaceAddr,
(void *) entry->replaceCall, entry->targetProcess);
curPluginInformation->addFunctionData(func);
}
/* load relocation data */
2022-02-04 16:25:44 +01:00
for (auto &linking_entry : plugin_data->info.linking_entries) {
2021-09-25 14:26:18 +02:00
if (linking_entry.destination == nullptr) {
break;
}
2021-09-25 14:26:18 +02:00
dyn_linking_import_t *importEntry = linking_entry.importEntry;
2020-12-26 14:17:50 +01:00
if (importEntry == nullptr) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("importEntry was nullptr, skipping relocation entry");
continue;
}
2021-09-25 14:26:18 +02:00
dyn_linking_function_t *functionEntry = linking_entry.functionEntry;
2020-12-26 14:17:50 +01:00
if (functionEntry == nullptr) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("functionEntry was nullptr, skipping relocation entry");
continue;
}
2021-12-15 17:09:30 +01:00
auto rplInfo = std::make_shared<ImportRPLInformation>(importEntry->importName, importEntry->isData);
2020-12-26 14:17:50 +01:00
std::string functionName(functionEntry->functionName);
2021-12-15 17:09:30 +01:00
auto reloc = std::make_shared<RelocationData>(linking_entry.type, linking_entry.offset, linking_entry.addend, linking_entry.destination, functionName, rplInfo);
curPluginInformation->addRelocationData(reloc);
}
/* load function symbol data */
for (uint32_t j = 0; j < plugin_data->info.number_function_symbol_data; j++) {
2022-02-04 16:25:44 +01:00
auto symbol_data = &plugin_data->info.function_symbol_data[j];
std::string symbol_name = symbol_data->name;
2022-02-04 16:25:44 +01:00
auto funSymbolData = std::make_shared<FunctionSymbolData>(symbol_name, (void *) symbol_data->address, symbol_data->size);
2021-12-15 17:09:30 +01:00
curPluginInformation->addFunctionSymbolData(funSymbolData);
}
2021-12-15 17:09:30 +01:00
auto container = std::make_shared<PluginContainer>();
container->setMetaInformation(metaInformation);
container->setPluginData(pluginData);
container->setPluginInformation(curPluginInformation);
result.push_back(container);
}
return result;
}