2020-04-29 18:02:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2018-2020 Maschell
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-04 16:25:44 +01:00
|
|
|
#include "PluginMetaInformationFactory.h"
|
2024-11-27 20:44:36 +01:00
|
|
|
|
|
|
|
#include "PluginData.h"
|
|
|
|
#include "PluginMetaInformation.h"
|
2022-05-14 14:00:20 +02:00
|
|
|
#include "fs/FSUtils.h"
|
2023-01-04 02:05:57 +01:00
|
|
|
#include "utils/logger.h"
|
|
|
|
#include "utils/wiiu_zlib.hpp"
|
2024-11-27 20:44:36 +01:00
|
|
|
|
|
|
|
#include <elfio/elfio.hpp>
|
|
|
|
#include <optional>
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2024-03-24 07:40:58 +01:00
|
|
|
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(const PluginData &pluginData, PluginParseErrors &error) {
|
2023-11-04 15:32:45 +01:00
|
|
|
return loadPlugin(pluginData.getBuffer(), error);
|
2020-05-17 20:50:31 +02:00
|
|
|
}
|
2020-05-28 20:51:31 +02:00
|
|
|
|
2024-03-24 07:40:58 +01:00
|
|
|
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(std::string_view filePath, PluginParseErrors &error) {
|
2023-11-04 15:32:45 +01:00
|
|
|
std::vector<uint8_t> buffer;
|
|
|
|
if (FSUtils::LoadFileToMem(filePath, buffer) < 0) {
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to load file to memory");
|
2023-08-16 10:18:02 +02:00
|
|
|
error = PLUGIN_PARSE_ERROR_IO_ERROR;
|
2022-02-14 20:23:27 +01:00
|
|
|
return {};
|
2020-05-17 20:50:31 +02:00
|
|
|
}
|
2023-11-04 15:32:45 +01:00
|
|
|
return loadPlugin(buffer, error);
|
|
|
|
}
|
2022-02-14 20:23:27 +01:00
|
|
|
|
2024-03-24 07:40:58 +01:00
|
|
|
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(std::span<const uint8_t> buffer, PluginParseErrors &error) {
|
2023-11-04 15:32:45 +01:00
|
|
|
if (buffer.empty()) {
|
|
|
|
error = PLUGIN_PARSE_ERROR_BUFFER_EMPTY;
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Buffer is empty");
|
2022-02-14 20:23:27 +01:00
|
|
|
return {};
|
|
|
|
}
|
2023-01-04 02:05:57 +01:00
|
|
|
ELFIO::elfio reader(new wiiu_zlib);
|
|
|
|
|
2023-11-04 15:32:45 +01:00
|
|
|
if (!reader.load(reinterpret_cast<const char *>(buffer.data()), buffer.size())) {
|
2023-08-16 10:18:02 +02:00
|
|
|
error = PLUGIN_PARSE_ERROR_ELFIO_PARSE_FAILED;
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Can't find or process ELF file");
|
2024-03-24 07:40:58 +01:00
|
|
|
return {};
|
2020-05-17 20:50:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 18:02:36 +02:00
|
|
|
size_t pluginSize = 0;
|
|
|
|
|
2024-03-24 07:40:58 +01:00
|
|
|
PluginMetaInformation pluginInfo;
|
2020-05-17 20:50:31 +02:00
|
|
|
|
2020-06-03 18:34:31 +02:00
|
|
|
uint32_t sec_num = reader.sections.size();
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2024-04-27 17:46:06 +02:00
|
|
|
bool hasMetaSection = false;
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t i = 0; i < sec_num; ++i) {
|
2022-05-14 14:00:20 +02:00
|
|
|
ELFIO::section *psec = reader.sections[i];
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
// Calculate total size:
|
2023-01-04 02:05:57 +01:00
|
|
|
if ((psec->get_type() == ELFIO::SHT_PROGBITS || psec->get_type() == ELFIO::SHT_NOBITS) && (psec->get_flags() & ELFIO::SHF_ALLOC)) {
|
2020-04-29 18:02:36 +02:00
|
|
|
uint32_t sectionSize = psec->get_size();
|
2022-02-04 16:25:44 +01:00
|
|
|
auto address = (uint32_t) psec->get_address();
|
2020-05-03 12:30:15 +02:00
|
|
|
if ((address >= 0x02000000) && address < 0x10000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
pluginSize += sectionSize;
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if ((address >= 0x10000000) && address < 0xC0000000) {
|
2020-04-29 18:02:36 +02:00
|
|
|
pluginSize += sectionSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get meta information and check WUPS version:
|
2021-04-07 00:23:23 +02:00
|
|
|
if (psec->get_name() == ".wups.meta") {
|
2024-04-27 17:46:06 +02:00
|
|
|
hasMetaSection = true;
|
2020-05-03 12:30:15 +02:00
|
|
|
const void *sectionData = psec->get_data();
|
2022-02-04 16:25:44 +01:00
|
|
|
uint32_t sectionSize = psec->get_size();
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
char *curEntry = (char *) sectionData;
|
|
|
|
while ((uint32_t) curEntry < (uint32_t) sectionData + sectionSize) {
|
2020-04-29 18:02:36 +02:00
|
|
|
if (*curEntry == '\0') {
|
|
|
|
curEntry++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-07 00:23:23 +02:00
|
|
|
auto firstFound = std::string(curEntry).find_first_of('=');
|
2020-05-03 12:30:15 +02:00
|
|
|
if (firstFound != std::string::npos) {
|
2020-04-29 18:02:36 +02:00
|
|
|
curEntry[firstFound] = '\0';
|
|
|
|
std::string key(curEntry);
|
|
|
|
std::string value(curEntry + firstFound + 1);
|
|
|
|
|
2021-09-25 14:26:18 +02:00
|
|
|
if (key == "name") {
|
2024-03-24 07:40:58 +01:00
|
|
|
pluginInfo.setName(value);
|
2021-09-25 14:26:18 +02:00
|
|
|
} else if (key == "author") {
|
2024-03-24 07:40:58 +01:00
|
|
|
pluginInfo.setAuthor(value);
|
2021-09-25 14:26:18 +02:00
|
|
|
} else if (key == "version") {
|
2024-03-24 07:40:58 +01:00
|
|
|
pluginInfo.setVersion(value);
|
2021-09-25 14:26:18 +02:00
|
|
|
} else if (key == "license") {
|
2024-03-24 07:40:58 +01:00
|
|
|
pluginInfo.setLicense(value);
|
2021-09-25 14:26:18 +02:00
|
|
|
} else if (key == "buildtimestamp") {
|
2024-03-24 07:40:58 +01:00
|
|
|
pluginInfo.setBuildTimestamp(value);
|
2021-09-25 14:26:18 +02:00
|
|
|
} else if (key == "description") {
|
2024-03-24 07:40:58 +01:00
|
|
|
pluginInfo.setDescription(value);
|
2021-10-01 17:25:48 +02:00
|
|
|
} else if (key == "storage_id") {
|
2024-03-24 07:40:58 +01:00
|
|
|
pluginInfo.setStorageId(value);
|
2021-09-25 14:26:18 +02:00
|
|
|
} else if (key == "wups") {
|
2023-10-07 22:27:06 +02:00
|
|
|
if (value == "0.7.1") {
|
2024-03-24 07:40:58 +01:00
|
|
|
pluginInfo.setWUPSVersion(0, 7, 1);
|
2024-05-05 14:46:35 +02:00
|
|
|
} else if (value == "0.8.1") {
|
|
|
|
pluginInfo.setWUPSVersion(0, 8, 1);
|
2023-10-07 22:27:06 +02:00
|
|
|
} else {
|
2023-08-16 10:18:02 +02:00
|
|
|
error = PLUGIN_PARSE_ERROR_INCOMPATIBLE_VERSION;
|
2022-04-22 22:55:53 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Warning: Ignoring plugin - Unsupported WUPS version: %s.", value.c_str());
|
2024-03-24 07:40:58 +01:00
|
|
|
return {};
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curEntry += strlen(curEntry) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-27 17:46:06 +02:00
|
|
|
if (!hasMetaSection) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("File has no \".wups.meta\" section");
|
|
|
|
error = PLUGIN_PARSE_ERROR_NO_PLUGIN;
|
|
|
|
return {};
|
|
|
|
}
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2024-03-24 07:40:58 +01:00
|
|
|
pluginInfo.setSize(pluginSize);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2023-08-16 10:18:02 +02:00
|
|
|
error = PLUGIN_PARSE_ERROR_NONE;
|
|
|
|
|
2020-04-29 18:02:36 +02:00
|
|
|
return pluginInfo;
|
|
|
|
}
|