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/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <whb/file.h>
|
|
|
|
#include "utils/StringTools.h"
|
|
|
|
#include "PluginMetaInformationFactory.h"
|
|
|
|
#include "PluginMetaInformation.h"
|
|
|
|
#include "elfio/elfio.hpp"
|
|
|
|
|
|
|
|
using namespace ELFIO;
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(const PluginData &pluginData) {
|
2020-04-29 18:02:36 +02:00
|
|
|
auto readerOpt = pluginData.getReader();
|
|
|
|
|
|
|
|
// Load ELF data
|
|
|
|
if (!readerOpt) {
|
|
|
|
DEBUG_FUNCTION_LINE("Can't find or process ELF file");
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2020-05-17 20:50:31 +02:00
|
|
|
return loadPlugin(readerOpt.value());
|
|
|
|
}
|
|
|
|
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(const std::string filePath) {
|
|
|
|
auto reader = new elfio;
|
|
|
|
if (reader == NULL || !reader->load(filePath)) {
|
|
|
|
DEBUG_FUNCTION_LINE("Can't find or process ELF file\n");
|
|
|
|
delete reader;
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
return loadPlugin(reader);
|
|
|
|
}
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-05-17 20:50:31 +02:00
|
|
|
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(char *buffer, size_t size) {
|
|
|
|
auto reader = new elfio;
|
|
|
|
if (reader == NULL || !reader->load(buffer, size)) {
|
|
|
|
DEBUG_FUNCTION_LINE("Can't find or process ELF file\n");
|
|
|
|
delete reader;
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadPlugin(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<PluginMetaInformation> PluginMetaInformationFactory::loadPlugin(elfio *reader) {
|
2020-04-29 18:02:36 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Found elfio reader");
|
|
|
|
|
|
|
|
size_t pluginSize = 0;
|
|
|
|
|
|
|
|
PluginMetaInformation pluginInfo;
|
2020-05-17 20:50:31 +02:00
|
|
|
|
2020-04-29 18:02:36 +02:00
|
|
|
uint32_t sec_num = reader->sections.size();
|
|
|
|
|
|
|
|
DEBUG_FUNCTION_LINE("%d number of sections", sec_num);
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
for (uint32_t i = 0; i < sec_num; ++i) {
|
|
|
|
section *psec = reader->sections[i];
|
2020-04-29 18:02:36 +02:00
|
|
|
|
|
|
|
// Calculate total size:
|
|
|
|
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
|
|
|
|
uint32_t sectionSize = psec->get_size();
|
|
|
|
uint32_t 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:
|
|
|
|
if (psec->get_name().compare(".wups.meta") == 0) {
|
2020-05-03 12:30:15 +02:00
|
|
|
const void *sectionData = psec->get_data();
|
2020-04-29 18:02:36 +02:00
|
|
|
uint32_t sectionSize = psec->get_size();
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-05-03 12:30:15 +02:00
|
|
|
if (key.compare("name") == 0) {
|
2020-04-29 18:02:36 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Name = %s", value.c_str());
|
|
|
|
pluginInfo.setName(value);
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (key.compare("author") == 0) {
|
2020-04-29 18:02:36 +02:00
|
|
|
pluginInfo.setAuthor(value);
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (key.compare("version") == 0) {
|
2020-04-29 18:02:36 +02:00
|
|
|
pluginInfo.setVersion(value);
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (key.compare("license") == 0) {
|
2020-04-29 18:02:36 +02:00
|
|
|
pluginInfo.setLicense(value);
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (key.compare("buildtimestamp") == 0) {
|
2020-04-29 18:02:36 +02:00
|
|
|
pluginInfo.setBuildTimestamp(value);
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (key.compare("description") == 0) {
|
2020-04-29 18:02:36 +02:00
|
|
|
pluginInfo.setDescription(value);
|
2020-05-03 12:30:15 +02:00
|
|
|
} else if (key.compare("wups") == 0) {
|
|
|
|
if (value.compare("0.2") != 0) {
|
2020-05-17 20:50:05 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Warning: Ignoring plugin - Unsupported WUPS version: %s.\n", value.c_str());
|
2020-04-29 18:02:36 +02:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curEntry += strlen(curEntry) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginInfo.setSize(pluginSize);
|
|
|
|
|
|
|
|
return pluginInfo;
|
|
|
|
}
|