2015-09-13 20:15:26 +08:00
|
|
|
/* uefifind.cpp
|
|
|
|
|
2016-02-02 09:08:08 +08:00
|
|
|
Copyright (c) 2016, Nikolaj Schlej. All rights reserved.
|
2015-09-13 20:15:26 +08:00
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uefifind.h"
|
|
|
|
|
2016-03-01 16:12:37 +08:00
|
|
|
UEFIFind::UEFIFind()
|
2015-09-13 20:15:26 +08:00
|
|
|
{
|
|
|
|
model = new TreeModel();
|
2016-03-01 16:12:37 +08:00
|
|
|
ffsParser = new FfsParser(model);
|
2015-09-13 20:15:26 +08:00
|
|
|
initDone = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UEFIFind::~UEFIFind()
|
|
|
|
{
|
|
|
|
delete ffsParser;
|
|
|
|
delete model;
|
|
|
|
model = NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-09 16:01:41 +08:00
|
|
|
USTATUS UEFIFind::init(const QString & path)
|
2015-09-13 20:15:26 +08:00
|
|
|
{
|
2016-07-09 16:01:41 +08:00
|
|
|
USTATUS result;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
fileInfo = QFileInfo(path);
|
|
|
|
|
|
|
|
if (!fileInfo.exists())
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_FILE_OPEN;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
QFile inputFile;
|
|
|
|
inputFile.setFileName(path);
|
|
|
|
|
|
|
|
if (!inputFile.open(QFile::ReadOnly))
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_FILE_OPEN;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
QByteArray buffer = inputFile.readAll();
|
|
|
|
inputFile.close();
|
|
|
|
|
2016-02-02 09:08:08 +08:00
|
|
|
result = ffsParser->parse(buffer);
|
2015-09-13 20:15:26 +08:00
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
initDone = true;
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_SUCCESS;
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|
|
|
|
|
2016-07-09 16:01:41 +08:00
|
|
|
USTATUS UEFIFind::find(const UINT8 mode, const bool count, const QString & hexPattern, QString & result)
|
2015-09-13 20:15:26 +08:00
|
|
|
{
|
2018-07-13 05:31:46 +08:00
|
|
|
UModelIndex root = model->index(0, 0);
|
|
|
|
std::set<std::pair<UModelIndex, UModelIndex> > files;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
result.clear();
|
|
|
|
|
2016-07-09 16:01:41 +08:00
|
|
|
USTATUS returned = findFileRecursive(root, hexPattern, mode, files);
|
2015-09-13 20:15:26 +08:00
|
|
|
if (returned)
|
|
|
|
return returned;
|
|
|
|
|
|
|
|
if (count) {
|
2016-03-01 16:12:37 +08:00
|
|
|
if (!files.empty())
|
|
|
|
result.append(QString("%1\n").arg(files.size()));
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_SUCCESS;
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|
|
|
|
|
2018-07-13 05:31:46 +08:00
|
|
|
for (std::set<std::pair<UModelIndex, UModelIndex> >::const_iterator citer = files.begin(); citer != files.end(); ++citer) {
|
2015-12-13 23:34:56 +08:00
|
|
|
QByteArray data(16, '\x00');
|
2018-07-13 05:31:46 +08:00
|
|
|
std::pair<UModelIndex, UModelIndex> indexes = *citer;
|
2015-12-13 23:34:56 +08:00
|
|
|
if (!model->hasEmptyHeader(indexes.first))
|
|
|
|
data = model->header(indexes.first).left(16);
|
2016-07-09 16:01:41 +08:00
|
|
|
result.append(guidToUString(*(const EFI_GUID*)data.constData()));
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
// Special case of freeform subtype GUID files
|
|
|
|
if (indexes.second.isValid() && model->subtype(indexes.second) == EFI_SECTION_FREEFORM_SUBTYPE_GUID) {
|
2017-07-23 02:43:20 +08:00
|
|
|
data = model->header(indexes.second);
|
2016-07-09 16:01:41 +08:00
|
|
|
result.append(" ").append(guidToUString(*(const EFI_GUID*)(data.constData() + sizeof(EFI_COMMON_SECTION_HEADER))));
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result.append("\n");
|
|
|
|
}
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_SUCCESS;
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|
|
|
|
|
2018-07-13 05:31:46 +08:00
|
|
|
USTATUS UEFIFind::findFileRecursive(const UModelIndex index, const QString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files)
|
2015-09-13 20:15:26 +08:00
|
|
|
{
|
|
|
|
if (!index.isValid())
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_SUCCESS;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
if (hexPattern.isEmpty())
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_INVALID_PARAMETER;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
// Check for "all substrings" pattern
|
|
|
|
if (hexPattern.count('.') == hexPattern.length())
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_SUCCESS;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
bool hasChildren = (model->rowCount(index) > 0);
|
|
|
|
for (int i = 0; i < model->rowCount(index); i++) {
|
|
|
|
findFileRecursive(index.child(i, index.column()), hexPattern, mode, files);
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
if (hasChildren) {
|
|
|
|
if (mode == SEARCH_MODE_HEADER || mode == SEARCH_MODE_ALL)
|
|
|
|
data.append(model->header(index));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (mode == SEARCH_MODE_HEADER)
|
|
|
|
data.append(model->header(index));
|
|
|
|
else if (mode == SEARCH_MODE_BODY)
|
|
|
|
data.append(model->body(index));
|
|
|
|
else
|
|
|
|
data.append(model->header(index)).append(model->body(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString hexBody = QString(data.toHex());
|
|
|
|
QRegExp regexp = QRegExp(QString(hexPattern), Qt::CaseInsensitive);
|
|
|
|
INT32 offset = regexp.indexIn(hexBody);
|
|
|
|
while (offset >= 0) {
|
|
|
|
if (offset % 2 == 0) {
|
|
|
|
if (model->type(index) != Types::File) {
|
2018-07-13 05:31:46 +08:00
|
|
|
UModelIndex ffs = model->findParentOfType(index, Types::File);
|
2015-09-13 20:15:26 +08:00
|
|
|
if (model->type(index) == Types::Section && model->subtype(index) == EFI_SECTION_FREEFORM_SUBTYPE_GUID)
|
2018-07-13 05:31:46 +08:00
|
|
|
files.insert(std::pair<UModelIndex, UModelIndex>(ffs, index));
|
2015-09-13 20:15:26 +08:00
|
|
|
else
|
2018-07-13 05:31:46 +08:00
|
|
|
files.insert(std::pair<UModelIndex, UModelIndex>(ffs, UModelIndex()));
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|
|
|
|
else
|
2018-07-13 05:31:46 +08:00
|
|
|
files.insert(std::pair<UModelIndex, UModelIndex>(index, UModelIndex()));
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset = regexp.indexIn(hexBody, offset + 1);
|
|
|
|
}
|
|
|
|
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_SUCCESS;
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|