Bugfixes for #147

This commit is contained in:
vit9696 2018-10-10 17:20:00 +03:00
parent 337da5e632
commit 7e1e1ab61e
10 changed files with 64 additions and 35 deletions

View File

@ -38,16 +38,15 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path
if (guid.isEmpty() || if (guid.isEmpty() ||
(model->subtype(index) == EFI_SECTION_FREEFORM_SUBTYPE_GUID && (model->subtype(index) == EFI_SECTION_FREEFORM_SUBTYPE_GUID &&
guidToUString(*(const EFI_GUID*)(model->header(index).constData() + sizeof(EFI_COMMON_SECTION_HEADER))) == guid) || guidToUString(readMisaligned((const EFI_GUID*)(model->header(index).constData() + sizeof(EFI_COMMON_SECTION_HEADER)))) == guid) ||
guidToUString(*(const EFI_GUID*)model->header(index).constData()) == guid || guidToUString(readMisaligned((const EFI_GUID*)model->header(index).constData())) == guid ||
guidToUString(*(const EFI_GUID*)model->header(model->findParentOfType(index, Types::File)).constData()) == guid) { guidToUString(readMisaligned((const EFI_GUID*)model->header(model->findParentOfType(index, Types::File)).constData())) == guid) {
if (!changeDirectory(path) && !makeDirectory(path)) if (!changeDirectory(path) && !makeDirectory(path))
return U_DIR_CREATE; return U_DIR_CREATE;
counterHeader = counterBody = counterRaw = counterInfo = 0; counterHeader = counterBody = counterRaw = counterInfo = 0;
std::ofstream file;
if (dumpMode == DUMP_ALL || model->rowCount(index) == 0) { // Dump if leaf item or dumpAll is true if (dumpMode == DUMP_ALL || model->rowCount(index) == 0) { // Dump if leaf item or dumpAll is true
if (dumpMode == DUMP_ALL || dumpMode == DUMP_CURRENT || dumpMode == DUMP_HEADER) { if (dumpMode == DUMP_ALL || dumpMode == DUMP_CURRENT || dumpMode == DUMP_HEADER) {
if (!model->header(index).isEmpty() && (sectionType == IgnoreSectionType || model->subtype(index) == sectionType)) { if (!model->header(index).isEmpty() && (sectionType == IgnoreSectionType || model->subtype(index) == sectionType)) {
@ -57,10 +56,11 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path
else else
filename = usprintf("%s/header_%d.bin", path.toLocal8Bit(), counterHeader); filename = usprintf("%s/header_%d.bin", path.toLocal8Bit(), counterHeader);
counterHeader++; counterHeader++;
file.open(filename.toLocal8Bit(), std::ofstream::binary); std::ofstream file(filename.toLocal8Bit(), std::ofstream::binary);
if (!file)
return U_FILE_OPEN;
const UByteArray &data = model->header(index); const UByteArray &data = model->header(index);
file.write(data.constData(), data.size()); file.write(data.constData(), data.size());
file.close();
} }
} }
@ -72,10 +72,12 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path
else else
filename = usprintf("%s/body_%d.bin", path.toLocal8Bit(), counterBody); filename = usprintf("%s/body_%d.bin", path.toLocal8Bit(), counterBody);
counterBody++; counterBody++;
file.open(filename.toLocal8Bit(), std::ofstream::binary); errno = 0;
std::ofstream file(filename.toLocal8Bit(), std::ofstream::binary);
if (!file)
return U_FILE_OPEN;
const UByteArray &data = model->body(index); const UByteArray &data = model->body(index);
file.write(data.constData(), data.size()); file.write(data.constData(), data.size());
file.close();
} }
} }
@ -89,14 +91,15 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path
else else
filename = usprintf("%s/file_%d.bin", path.toLocal8Bit(), counterRaw); filename = usprintf("%s/file_%d.bin", path.toLocal8Bit(), counterRaw);
counterRaw++; counterRaw++;
file.open(filename.toLocal8Bit(), std::ofstream::binary); std::ofstream file(filename.toLocal8Bit(), std::ofstream::binary);
if (!file)
return U_FILE_OPEN;
const UByteArray &headerData = model->header(index); const UByteArray &headerData = model->header(index);
const UByteArray &bodyData = model->body(index); const UByteArray &bodyData = model->body(index);
const UByteArray &tailData = model->tail(index); const UByteArray &tailData = model->tail(index);
file.write(headerData.constData(), headerData.size()); file.write(headerData.constData(), headerData.size());
file.write(bodyData.constData(), bodyData.size()); file.write(bodyData.constData(), bodyData.size());
file.write(tailData.constData(), tailData.size()); file.write(tailData.constData(), tailData.size());
file.close();
} }
} }
@ -115,9 +118,10 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path
else else
filename = usprintf("%s/info_%d.txt", path.toLocal8Bit(), counterInfo); filename = usprintf("%s/info_%d.txt", path.toLocal8Bit(), counterInfo);
counterInfo++; counterInfo++;
file.open(filename.toLocal8Bit()); std::ofstream file(filename.toLocal8Bit());
if (!file)
return U_FILE_OPEN;
file << info.toLocal8Bit(); file << info.toLocal8Bit();
file.close();
} }
dumped = true; dumped = true;

View File

@ -19,6 +19,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include "../common/treemodel.h" #include "../common/treemodel.h"
#include "../common/ffs.h" #include "../common/ffs.h"
#include "../common/filesystem.h" #include "../common/filesystem.h"
#include "../common/utility.h"
class FfsDumper class FfsDumper
{ {

View File

@ -38,7 +38,7 @@ int main(int argc, char *argv[])
// Check that input file exists // Check that input file exists
USTATUS result; USTATUS result;
UByteArray buffer; UByteArray buffer;
UString path = argv[1]; UString path = getAbsPath(argv[1]);
result = readFileIntoBuffer(path, buffer); result = readFileIntoBuffer(path, buffer);
if (result) if (result)
return result; return result;
@ -112,7 +112,7 @@ int main(int argc, char *argv[])
if (readType == READ_INPUT) { if (readType == READ_INPUT) {
inputs.push_back(arg); inputs.push_back(arg);
} else if (readType == READ_OUTPUT) { } else if (readType == READ_OUTPUT) {
outputs.push_back(arg); outputs.push_back(getAbsPath(arg));
} else if (readType == READ_MODE) { } else if (readType == READ_MODE) {
if (!std::strcmp(arg, "all")) if (!std::strcmp(arg, "all"))
modes.push_back(FfsDumper::DUMP_ALL); modes.push_back(FfsDumper::DUMP_ALL);

View File

@ -67,12 +67,12 @@ USTATUS UEFIFind::find(const UINT8 mode, const bool count, const UString & hexPa
std::pair<UModelIndex, UModelIndex> indexes = *citer; std::pair<UModelIndex, UModelIndex> indexes = *citer;
if (!model->hasEmptyHeader(indexes.first)) if (!model->hasEmptyHeader(indexes.first))
data = model->header(indexes.first).left(16); data = model->header(indexes.first).left(16);
result += guidToUString(*(const EFI_GUID*)data.constData()); result += guidToUString(readMisaligned((const EFI_GUID*)data.constData()));
// Special case of freeform subtype GUID files // Special case of freeform subtype GUID files
if (indexes.second.isValid() && model->subtype(indexes.second) == EFI_SECTION_FREEFORM_SUBTYPE_GUID) { if (indexes.second.isValid() && model->subtype(indexes.second) == EFI_SECTION_FREEFORM_SUBTYPE_GUID) {
data = model->header(indexes.second); data = model->header(indexes.second);
result += UString(" ") + (guidToUString(*(const EFI_GUID*)(data.constData() + sizeof(EFI_COMMON_SECTION_HEADER)))); result += UString(" ") + (guidToUString(readMisaligned((const EFI_GUID*)(data.constData() + sizeof(EFI_COMMON_SECTION_HEADER)))));
} }
result += UString("\n"); result += UString("\n");

View File

@ -368,8 +368,15 @@ struct CBString : public tagbstring {
void clear() { *this = ""; } void clear() { *this = ""; }
CBString left(int len) const { return midstr(0, len); } CBString left(int len) const { return midstr(0, len); }
CBString mid(int pos, int len) const { return midstr(pos, len); } CBString mid(int pos, int len) const { return midstr(pos, len); }
static CBString fromUtf16(const unsigned short* str) { // Naive implementation assuming that only ASCII part of UCS2 is used static CBString fromUtf16(const unsigned short* str) {
CBString msg; while (*str) { msg += *(char*)str; str++; } return msg; // Naive implementation assuming that only ASCII LE part of UCS2 is used, str may not be aligned.
CBString msg;
const char *str8 = reinterpret_cast<const char *>(str);
while (str8[0]) {
msg += str8[0];
str8 += 2;
}
return msg;
} }
CBString leftJustified(int length) { if (length > slen) { return *this + CBString(' ', length - slen); } return *this; } CBString leftJustified(int length) { if (length > slen) { return *this + CBString(' ', length - slen); } return *this; }
}; };

View File

@ -2449,8 +2449,8 @@ USTATUS FfsParser::parseCompressedSectionBody(const UModelIndex & index)
if (model->hasEmptyParsingData(index) == false) { if (model->hasEmptyParsingData(index) == false) {
UByteArray data = model->parsingData(index); UByteArray data = model->parsingData(index);
const COMPRESSED_SECTION_PARSING_DATA* pdata = (const COMPRESSED_SECTION_PARSING_DATA*)data.constData(); const COMPRESSED_SECTION_PARSING_DATA* pdata = (const COMPRESSED_SECTION_PARSING_DATA*)data.constData();
compressionType = pdata->compressionType; compressionType = readMisaligned(pdata).compressionType;
uncompressedSize = pdata->uncompressedSize; uncompressedSize = readMisaligned(pdata).uncompressedSize;
} }
// Decompress section // Decompress section

View File

@ -22,6 +22,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#ifdef WIN32 #ifdef WIN32
#include <direct.h> #include <direct.h>
#include <stdlib.h>
static inline bool isExistOnFs(const UString & path) { static inline bool isExistOnFs(const UString & path) {
struct _stat buf; struct _stat buf;
return (_stat(path.toLocal8Bit(), &buf) == 0); return (_stat(path.toLocal8Bit(), &buf) == 0);
@ -34,8 +35,16 @@ static inline bool makeDirectory(const UString & dir) {
static inline bool changeDirectory(const UString & dir) { static inline bool changeDirectory(const UString & dir) {
return (_chdir(dir.toLocal8Bit()) == 0); return (_chdir(dir.toLocal8Bit()) == 0);
} }
static inline UString getAbsPath(const UString * path) {
char abs[1024] = {};
if (_fullpath(abs, path.toLocal8Bit(), sizeof(abs)))
UString(abs);
return path;
}
#else #else
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
static inline bool isExistOnFs(const UString & path) { static inline bool isExistOnFs(const UString & path) {
struct stat buf; struct stat buf;
return (stat(path.toLocal8Bit(), &buf) == 0); return (stat(path.toLocal8Bit(), &buf) == 0);
@ -48,6 +57,14 @@ static inline bool makeDirectory(const UString & dir) {
static inline bool changeDirectory(const UString & dir) { static inline bool changeDirectory(const UString & dir) {
return (chdir(dir.toLocal8Bit()) == 0); return (chdir(dir.toLocal8Bit()) == 0);
} }
static inline UString getAbsPath(const UString & path) {
char abs[PATH_MAX] = {};
// Last is a non-standard extension for non-existent files.
if (realpath(path.toLocal8Bit(), abs) || abs[0] != '\0')
return UString(abs);
return path;
}
#endif #endif
static inline USTATUS readFileIntoBuffer(const UString & inPath, UByteArray &buf) { static inline USTATUS readFileIntoBuffer(const UString & inPath, UByteArray &buf) {

View File

@ -39,7 +39,7 @@ USTATUS NvramParser::parseNvarStore(const UModelIndex & index)
if (parentFileIndex.isValid() && model->hasEmptyParsingData(parentFileIndex) == false) { if (parentFileIndex.isValid() && model->hasEmptyParsingData(parentFileIndex) == false) {
UByteArray data = model->parsingData(parentFileIndex); UByteArray data = model->parsingData(parentFileIndex);
const FILE_PARSING_DATA* pdata = (const FILE_PARSING_DATA*)data.constData(); const FILE_PARSING_DATA* pdata = (const FILE_PARSING_DATA*)data.constData();
emptyByte = pdata->emptyByte; emptyByte = readMisaligned(pdata).emptyByte;
} }
// Rename parent file // Rename parent file
@ -365,7 +365,7 @@ USTATUS NvramParser::parseNvarStore(const UModelIndex & index)
// Try parsing the entry data as NVAR storage if it begins with NVAR signature // Try parsing the entry data as NVAR storage if it begins with NVAR signature
if ((subtype == Subtypes::DataNvarEntry || subtype == Subtypes::FullNvarEntry) if ((subtype == Subtypes::DataNvarEntry || subtype == Subtypes::FullNvarEntry)
&& body.size() >= 4 && *(const UINT32*)body.constData() == NVRAM_NVAR_ENTRY_SIGNATURE) && body.size() >= 4 && readMisaligned((const UINT32*)body.constData()) == NVRAM_NVAR_ENTRY_SIGNATURE)
parseNvarStore(varIndex); parseNvarStore(varIndex);
// Move to next exntry // Move to next exntry

View File

@ -384,7 +384,7 @@ static inline int char2hex(char c) {
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
return c - '0'; return c - '0';
if (c >= 'A' && c <= 'F') if (c >= 'A' && c <= 'F')
return c - 'A'; return c - 'A' + 10;
if (c == '.') if (c == '.')
return -2; return -2;
return -1; return -1;