1
0
Fork 0
mirror of https://github.com/LongSoft/UEFITool.git synced 2025-05-24 03:57:10 -04:00
UEFITool/UEFIFind/uefifind.cpp
Nikolaj Schlej 936b09dbf4 1) Added special subspecifier "h" for specifier "X" (hex values print), added corresponding menu option and application setting to UEFITool.
2) Fixed QHexView misalignment in Windows and macOS.
3) Added some more analysis to raw files and sections: raw files can (or can not) contain sections, raw sections can contain NVAR storage or PE/TE.
4) Improved CPU base address detection and propagation.
5) Improved FIT recognition.
7) ME region not displayed if it is in unknown format, fixed this because we still want to operate with it.
8) Small changes to Flash Descriptor parsing to get more cases for valid "Intel image". To get rid of cases when "Intel image" is already in tree but with parse error because of which "UEFI image" appears.
9) Added parsing of individual UEFI-files (these can be trimmed from UEFI-volume), displaying it as "UEFI volume part".
10) Added possibility to view/save contents of elements "Free volume space", "Free space" and such, because these can be non-empty.
11) Added info about blocks number and block size (with preliminary and stupid validity check) to volume info.
12) Added storage in settings of the following paths: open image file, save image file, open GUIDs file.
13) Added last opened files list.
14) Added permanent opened file name string to the end of the status bar.
15) Added opened file changes tracking: if the file was modified in other program while it is opened in UEFITool, there are 3 ways to act: a) ignore changes (but mark file path displayed in the status bar with italic font); b) ask user to reopen or ignore (if ignore, mark as in a); c) auto reopen changed file in UEFITool. If changes were in some way ignored, file path displayed in the right of the status bar will be marked with italic font and then become clickable: on click request to reopen appears again.
16) Switched to offset/size instead of byte array storing in each tree item.
17) For clarity - added icons to key tree items (compressed and with contents, contents now must be in root item only).
18) For usability - added expanding tree on open image (to depth level 1) and by menu command (expand all).
2025-04-23 21:44:00 +03:00

148 lines
4.8 KiB
C++

/* uefifind.cpp
Copyright (c) 2016, Nikolaj Schlej. All rights reserved.
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"
#include <fstream>
#include <set>
UEFIFind::UEFIFind()
{
model = new TreeModel();
ffsParser = new FfsParser(model);
initDone = false;
}
UEFIFind::~UEFIFind()
{
delete ffsParser;
delete model;
model = NULL;
}
USTATUS UEFIFind::init(const UString & path)
{
UByteArray buffer;
if (false == readFileIntoBuffer(path, buffer))
return U_FILE_OPEN;
USTATUS result = ffsParser->parse(buffer);
if (result)
return result;
initDone = true;
return U_SUCCESS;
}
USTATUS UEFIFind::findFileRecursive(const UModelIndex index, const UString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files)
{
if (!index.isValid())
return U_SUCCESS;
if (hexPattern.isEmpty())
return U_INVALID_PARAMETER;
const char *hexPatternRaw = hexPattern.toLocal8Bit();
std::vector<UINT8> pattern, patternMask;
if (!makePattern(hexPatternRaw, pattern, patternMask))
return U_INVALID_PARAMETER;
// Check for "all substrings" pattern
size_t count = 0;
for (size_t i = 0; i < patternMask.size(); i++)
if (patternMask[i] == 0)
count++;
if (count == patternMask.size())
return U_SUCCESS;
bool hasChildren = (model->rowCount(index) > 0);
for (int i = 0; i < model->rowCount(index); i++) {
findFileRecursive(index.model()->index(i, index.column(), index), hexPattern, mode, files);
}
// TODO: handle a case where an item has both compressed and uncompressed bodies
UByteArray data;
if (hasChildren) {
if (mode == SEARCH_MODE_HEADER)
data = model->header(index);
else if (mode == SEARCH_MODE_ALL)
data = model->header(index) + model->body(index);
}
else {
if (mode == SEARCH_MODE_HEADER)
data = model->header(index);
else if (mode == SEARCH_MODE_BODY)
data = model->body(index);
else
data = model->header(index) + model->body(index);
}
const UINT8 *rawData = reinterpret_cast<const UINT8 *>(data.constData());
INTN offset = findPattern(pattern.data(), patternMask.data(), pattern.size(), rawData, data.size(), 0);
// For patterns that cross header|body boundary, skip patterns entirely located in body, since
// children search above has already found them.
if (hasChildren && mode == SEARCH_MODE_ALL && offset >= model->headerSize(index)) {
offset = -1;
}
if (offset >= 0) {
if (model->type(index) != Types::File) {
UModelIndex parentFile = model->findParentOfType(index, Types::File);
if (model->type(index) == Types::Section && model->subtype(index) == EFI_SECTION_FREEFORM_SUBTYPE_GUID)
files.insert(std::pair<UModelIndex, UModelIndex>(parentFile, index));
else
files.insert(std::pair<UModelIndex, UModelIndex>(parentFile, UModelIndex()));
}
else {
files.insert(std::pair<UModelIndex, UModelIndex>(index, UModelIndex()));
}
}
return U_SUCCESS;
}
USTATUS UEFIFind::find(const UINT8 mode, const bool count, const UString & hexPattern, UString & result)
{
UModelIndex root = model->index(0, 0);
std::set<std::pair<UModelIndex, UModelIndex> > files;
result.clear();
USTATUS returned = findFileRecursive(root, hexPattern, mode, files);
if (returned)
return returned;
if (count) {
if (!files.empty())
result += usprintf("%lu\n", files.size());
return U_SUCCESS;
}
for (std::set<std::pair<UModelIndex, UModelIndex> >::const_iterator citer = files.begin(); citer != files.end(); ++citer) {
UByteArray data(16, '\x00');
std::pair<UModelIndex, UModelIndex> indexes = *citer;
if (!model->hasEmptyHeader(indexes.first))
data = model->header(indexes.first).left(16);
result += guidToUString(readUnaligned((const EFI_GUID*)data.constData()));
// Special case of freeform subtype GUID files
if (indexes.second.isValid() && model->subtype(indexes.second) == EFI_SECTION_FREEFORM_SUBTYPE_GUID) {
data = model->header(indexes.second);
result += UString(" ") + (guidToUString(readUnaligned((const EFI_GUID*)(data.constData() + sizeof(EFI_COMMON_SECTION_HEADER)))));
}
result += UString("\n");
}
return U_SUCCESS;
}