Refactor UEFIExtract a bit

This commit is contained in:
Nikolaj Schlej 2023-04-23 16:46:59 -07:00
parent ddf40c9260
commit 1a1a20895b
2 changed files with 202 additions and 188 deletions

View file

@ -85,13 +85,6 @@ USTATUS UEFIDumper::recursiveDump(const UModelIndex & index)
if (!index.isValid())
return U_INVALID_PARAMETER;
//UByteArray itemHeader = model.header(index);
//UByteArray fileHeader = model.header(model.findParentOfType(index, Types::File));
//if (guid.length() == 0 ||
// (itemHeader.size() >= sizeof (EFI_GUID) && guidToUString(*(const EFI_GUID*)itemHeader.constData()) == guid) ||
// (fileHeader.size() >= sizeof(EFI_GUID) && guidToUString(*(const EFI_GUID*)fileHeader.constData()) == guid)) {
// Construct file name
UString orgName = uniqueItemName(index);
UString name = orgName;
@ -145,7 +138,6 @@ USTATUS UEFIDumper::recursiveDump(const UModelIndex & index)
file.close();
dumped = true;
//}
// Process child items
USTATUS result;

View file

@ -34,14 +34,14 @@ void print_usage()
{
std::cout << "UEFIExtract " PROGRAM_VERSION << std::endl
<< "Usage: UEFIExtract {-h | --help | -v | --version} - show help and/or version information." << std::endl
<< " UEFIExtract imagefile - generate report and dump only leaf tree items into .dump folder." << std::endl
<< " UEFIExtract imagefile all - generate report and dump all tree items." << std::endl
<< " UEFIExtract imagefile unpack - generate report and dump all tree items into a single folder." << std::endl
<< " UEFIExtract imagefile dump - only generate dump, no report needed." << std::endl
<< " UEFIExtract imagefile report - only generate report, no dump needed." << std::endl
<< " UEFIExtract imagefile guids - generate a CSV file with named GUIDs present in the image." << std::endl
<< " UEFIExtract imagefile - generate report and GUID database, then dump only leaf tree items into .dump folder." << std::endl
<< " UEFIExtract imagefile all - generate report and GUID database, then dump all tree items into .dump folder." << std::endl
<< " UEFIExtract imagefile unpack - generate report, then dump all tree items into a single .dump folder (legacy UEFIDump compatibility mode)." << std::endl
<< " UEFIExtract imagefile dump - only generate dump, no report or GUID database needed." << std::endl
<< " UEFIExtract imagefile report - only generate report, no dump or GUID database needed." << std::endl
<< " UEFIExtract imagefile guids - only generate GUID database, no dump or report needed." << std::endl
<< " UEFIExtract imagefile GUID_1 ... [ -o FILE_1 ... ] [ -m MODE_1 ... ] [ -t TYPE_1 ... ] -" << std::endl
<< " Dump only FFS file(s) with specific GUID(s), without report." << std::endl
<< " Dump only FFS file(s) with specific GUID(s), without report or GUID database." << std::endl
<< " Type is section type or FF to ignore. Mode is one of: all, body, header, info, file." << std::endl
<< " Return value is a bit mask where 0 at position N means that file with GUID_N was found and unpacked, 1 otherwise." << std::endl;
}
@ -50,16 +50,21 @@ int main(int argc, char *argv[])
{
initGuidDatabase("guids.csv");
if (argc > 1) {
if (argc <= 1) {
print_usage();
return 1;
}
// Help and version
if (argc == 2) {
UString arg = UString(argv[1]);
if (arg == UString("-h") || arg == UString("--help")) {
print_usage();
return U_SUCCESS;
return 0;
}
else if (arg == UString("-v") || arg == UString("--version")) {
std::cout << PROGRAM_VERSION << std::endl;
return U_SUCCESS;
return 0;
}
}
@ -71,7 +76,7 @@ int main(int argc, char *argv[])
if (result)
return result;
// Hack to support legacy UEFIDump mode.
// Hack to support legacy UEFIDump mode
if (argc == 3 && !std::strcmp(argv[2], "unpack")) {
UEFIDumper uefidumper;
return (uefidumper.dump(buffer, UString(argv[1])) != U_SUCCESS);
@ -90,19 +95,58 @@ int main(int argc, char *argv[])
// Create ffsDumper
FfsDumper ffsDumper(&model);
// Dump only leaf elements, no report
// Dump only leaf elements, no report or GUID database
if (argc == 3 && !std::strcmp(argv[2], "dump")) {
return (ffsDumper.dump(model.index(0, 0), path + UString(".dump")) != U_SUCCESS);
}
// Dump named GUIDs found in the image
// Dump named GUIDs found in the image, no dump or report
else if (argc == 3 && !std::strcmp(argv[2], "guids")) {
GuidDatabase db = guidDatabaseFromTreeRecursive(&model, model.index(0, 0));
if (!db.empty()) {
return guidDatabaseExportToFile(path + UString(".guids.csv"), db);
}
}
else if (argc > 3 ||
(argc == 3 && std::strcmp(argv[2], "all") != 0 && std::strcmp(argv[2], "report") != 0)) { // Dump specific files, without report
// Generate report, no dump or GUID database
else if (argc == 3 && !std::strcmp(argv[2], "report")) {
FfsReport ffsReport(&model);
std::vector<UString> report = ffsReport.generate();
if (report.size()) {
std::ofstream file;
file.open((path + UString(".report.txt")).toLocal8Bit());
for (size_t i = 0; i < report.size(); i++)
file << report[i].toLocal8Bit() << '\n';
return 0;
}
return 1;
}
// Either default or all mode
else if (argc == 2 || (argc == 3 && !std::strcmp(argv[2], "all"))) {
// Generate report
FfsReport ffsReport(&model);
std::vector<UString> report = ffsReport.generate();
if (report.size()) {
std::ofstream file;
file.open((path + UString(".report.txt")).toLocal8Bit());
for (size_t i = 0; i < report.size(); i++)
file << report[i].toLocal8Bit() << '\n';
}
// Create GUID database
GuidDatabase db = guidDatabaseFromTreeRecursive(&model, model.index(0, 0));
if (!db.empty()) {
guidDatabaseExportToFile(path + UString(".guids.csv"), db);
}
// Dump all non-leaf elements, with report and GUID database, default
if (argc == 2) {
return (ffsDumper.dump(model.index(0, 0), path + UString(".dump")) != U_SUCCESS);
}
else if (argc == 3 && !std::strcmp(argv[2], "all")) { // Dump every element with report and GUID database
return (ffsDumper.dump(model.index(0, 0), path + UString(".dump"), FfsDumper::DUMP_ALL) != U_SUCCESS);
}
}
// Dump specific files, without report or GUID database
else {
std::vector<UString> inputs, outputs;
std::vector<FfsDumper::DumpMode> modes;
std::vector<UINT8> sectionTypes;
@ -168,28 +212,6 @@ int main(int argc, char *argv[])
return lastError;
}
// Create ffsReport
FfsReport ffsReport(&model);
std::vector<UString> report = ffsReport.generate();
if (report.size()) {
std::ofstream file;
file.open((path + UString(".report.txt")).toLocal8Bit());
for (size_t i = 0; i < report.size(); i++)
file << report[i].toLocal8Bit() << '\n';
}
// Dump all non-leaf elements, with report, default
if (argc == 2) {
return (ffsDumper.dump(model.index(0, 0), path + UString(".dump")) != U_SUCCESS);
}
else if (argc == 3 && !std::strcmp(argv[2], "all")) { // Dump every element with report
return (ffsDumper.dump(model.index(0, 0), path + UString(".dump"), FfsDumper::DUMP_ALL) != U_SUCCESS);
}
else if (argc == 3 && !std::strcmp(argv[2], "report")) { // Skip dumping
return 0;
}
}
// If parameters are different, show version and usage information
print_usage();
return 1;