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,67 +85,59 @@ USTATUS UEFIDumper::recursiveDump(const UModelIndex & index)
if (!index.isValid()) if (!index.isValid())
return U_INVALID_PARAMETER; return U_INVALID_PARAMETER;
//UByteArray itemHeader = model.header(index); // Construct file name
//UByteArray fileHeader = model.header(model.findParentOfType(index, Types::File)); UString orgName = uniqueItemName(index);
UString name = orgName;
bool nameFound = false;
for (int i = 1; i < 1000; ++i) {
if (!isExistOnFs(name + UString("_info.txt"))) {
nameFound = true;
break;
}
name = orgName + UString("_") + usprintf("%03d", i);
}
//if (guid.length() == 0 || if (!nameFound) {
// (itemHeader.size() >= sizeof (EFI_GUID) && guidToUString(*(const EFI_GUID*)itemHeader.constData()) == guid) || printf("Cannot find unique name for \"%s\".\n", (const char*)orgName.toLocal8Bit());
// (fileHeader.size() >= sizeof(EFI_GUID) && guidToUString(*(const EFI_GUID*)fileHeader.constData()) == guid)) { return U_INVALID_PARAMETER; //TODO: replace with proper errorCode
}
// Construct file name // Add header and body only for leaf sections
UString orgName = uniqueItemName(index); if (model.rowCount(index) == 0) {
UString name = orgName; // Header
bool nameFound = false; UByteArray data = model.header(index);
for (int i = 1; i < 1000; ++i) { if (!data.isEmpty()) {
if (!isExistOnFs(name + UString("_info.txt"))) { std::ofstream file;
nameFound = true; UString str = name + UString("_header.bin");
break; file.open(str.toLocal8Bit(), std::ios::out | std::ios::binary);
} file.write(data.constData(), data.size());
name = orgName + UString("_") + usprintf("%03d", i); file.close();
} }
if (!nameFound) { // Body
printf("Cannot find unique name for \"%s\".\n", (const char*)orgName.toLocal8Bit()); data = model.body(index);
return U_INVALID_PARAMETER; //TODO: replace with proper errorCode if (!data.isEmpty()) {
std::ofstream file;
UString str = name + UString("_body.bin");
file.open(str.toLocal8Bit(), std::ios::out | std::ios::binary);
file.write(data.constData(), data.size());
file.close();
} }
}
// Info
UString info = "Type: " + itemTypeToUString(model.type(index)) + "\n" +
"Subtype: " + itemSubtypeToUString(model.type(index), model.subtype(index)) + "\n";
if (model.text(index).length() > 0)
info += "Text: " + model.text(index) + "\n";
info += model.info(index) + "\n";
// Add header and body only for leaf sections std::ofstream file;
if (model.rowCount(index) == 0) { UString str = name + UString("_info.txt");
// Header file.open(str.toLocal8Bit(), std::ios::out);
UByteArray data = model.header(index); file.write(info.toLocal8Bit(), info.length());
if (!data.isEmpty()) { file.close();
std::ofstream file;
UString str = name + UString("_header.bin");
file.open(str.toLocal8Bit(), std::ios::out | std::ios::binary);
file.write(data.constData(), data.size());
file.close();
}
// Body dumped = true;
data = model.body(index);
if (!data.isEmpty()) {
std::ofstream file;
UString str = name + UString("_body.bin");
file.open(str.toLocal8Bit(), std::ios::out | std::ios::binary);
file.write(data.constData(), data.size());
file.close();
}
}
// Info
UString info = "Type: " + itemTypeToUString(model.type(index)) + "\n" +
"Subtype: " + itemSubtypeToUString(model.type(index), model.subtype(index)) + "\n";
if (model.text(index).length() > 0)
info += "Text: " + model.text(index) + "\n";
info += model.info(index) + "\n";
std::ofstream file;
UString str = name + UString("_info.txt");
file.open(str.toLocal8Bit(), std::ios::out);
file.write(info.toLocal8Bit(), info.length());
file.close();
dumped = true;
//}
// Process child items // Process child items
USTATUS result; USTATUS result;

View File

@ -34,141 +34,94 @@ void print_usage()
{ {
std::cout << "UEFIExtract " PROGRAM_VERSION << std::endl std::cout << "UEFIExtract " PROGRAM_VERSION << std::endl
<< "Usage: UEFIExtract {-h | --help | -v | --version} - show help and/or version information." << 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 - generate report and GUID database, then dump only leaf tree items into .dump folder." << std::endl
<< " UEFIExtract imagefile all - generate report and dump all tree items." << 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 and dump all tree items into a single 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 needed." << std::endl << " UEFIExtract imagefile dump - only generate dump, no report or GUID database needed." << std::endl
<< " UEFIExtract imagefile report - only generate report, no dump needed." << std::endl << " UEFIExtract imagefile report - only generate report, no dump or GUID database needed." << std::endl
<< " UEFIExtract imagefile guids - generate a CSV file with named GUIDs present in the image." << 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 << " 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 << " 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; << " 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;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
initGuidDatabase("guids.csv"); initGuidDatabase("guids.csv");
if (argc > 1) { if (argc <= 1) {
if (argc == 2) { print_usage();
UString arg = UString(argv[1]); return 1;
if (arg == UString("-h") || arg == UString("--help")) { }
print_usage();
return U_SUCCESS; // Help and version
} if (argc == 2) {
else if (arg == UString("-v") || arg == UString("--version")) { UString arg = UString(argv[1]);
std::cout << PROGRAM_VERSION << std::endl; if (arg == UString("-h") || arg == UString("--help")) {
return U_SUCCESS; print_usage();
} return 0;
} }
else if (arg == UString("-v") || arg == UString("--version")) {
// Check that input file exists std::cout << PROGRAM_VERSION << std::endl;
USTATUS result; return 0;
UByteArray buffer;
UString path = getAbsPath(argv[1]);
result = readFileIntoBuffer(path, buffer);
if (result)
return result;
// 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);
} }
}
// Create model and ffsParser // Check that input file exists
TreeModel model; USTATUS result;
FfsParser ffsParser(&model); UByteArray buffer;
// Parse input buffer UString path = getAbsPath(argv[1]);
result = ffsParser.parse(buffer); result = readFileIntoBuffer(path, buffer);
if (result) if (result)
return result; return result;
ffsParser.outputInfo(); // 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);
}
// Create ffsDumper // Create model and ffsParser
FfsDumper ffsDumper(&model); TreeModel model;
FfsParser ffsParser(&model);
// Parse input buffer
result = ffsParser.parse(buffer);
if (result)
return result;
// Dump only leaf elements, no report ffsParser.outputInfo();
if (argc == 3 && !std::strcmp(argv[2], "dump")) {
return (ffsDumper.dump(model.index(0, 0), path + UString(".dump")) != U_SUCCESS); // Create ffsDumper
FfsDumper ffsDumper(&model);
// 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, 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);
} }
// Dump named GUIDs found in the image }
else if (argc == 3 && !std::strcmp(argv[2], "guids")) { // Generate report, no dump or GUID database
GuidDatabase db = guidDatabaseFromTreeRecursive(&model, model.index(0, 0)); else if (argc == 3 && !std::strcmp(argv[2], "report")) {
if (!db.empty()) { FfsReport ffsReport(&model);
return guidDatabaseExportToFile(path + UString(".guids.csv"), db); 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;
} }
else if (argc > 3 || return 1;
(argc == 3 && std::strcmp(argv[2], "all") != 0 && std::strcmp(argv[2], "report") != 0)) { // Dump specific files, without report }
std::vector<UString> inputs, outputs; // Either default or all mode
std::vector<FfsDumper::DumpMode> modes; else if (argc == 2 || (argc == 3 && !std::strcmp(argv[2], "all"))) {
std::vector<UINT8> sectionTypes; // Generate report
ReadType readType = READ_INPUT;
for (int i = 2; i < argc; i++) {
const char *arg = argv[i];
if (!std::strcmp(arg, "-i")) {
readType = READ_INPUT;
continue;
} else if (!std::strcmp(arg, "-o")) {
readType = READ_OUTPUT;
continue;
} else if (!std::strcmp(arg, "-m")) {
readType = READ_MODE;
continue;
} else if (!std::strcmp(arg, "-t")) {
readType = READ_SECTION;
continue;
}
if (readType == READ_INPUT) {
inputs.push_back(arg);
} else if (readType == READ_OUTPUT) {
outputs.push_back(getAbsPath(arg));
} else if (readType == READ_MODE) {
if (!std::strcmp(arg, "all"))
modes.push_back(FfsDumper::DUMP_ALL);
else if (!std::strcmp(arg, "body"))
modes.push_back(FfsDumper::DUMP_BODY);
else if (!std::strcmp(arg, "header"))
modes.push_back(FfsDumper::DUMP_HEADER);
else if (!std::strcmp(arg, "info"))
modes.push_back(FfsDumper::DUMP_INFO);
else if (!std::strcmp(arg, "file"))
modes.push_back(FfsDumper::DUMP_FILE);
else
return U_INVALID_PARAMETER;
} else if (readType == READ_SECTION) {
char *converted = const_cast<char *>(arg);
UINT8 sectionType = (UINT8)std::strtol(arg, &converted, 16);
if (converted == arg)
return U_INVALID_PARAMETER;
sectionTypes.push_back(sectionType);
}
}
if (inputs.empty() || (!outputs.empty() && inputs.size() != outputs.size()) ||
(!modes.empty() && inputs.size() != modes.size()) ||
(!sectionTypes.empty() && inputs.size() != sectionTypes.size()))
return U_INVALID_PARAMETER;
USTATUS lastError = U_SUCCESS;
for (size_t i = 0; i < inputs.size(); i++) {
UString outPath = outputs.empty() ? path + UString(".dump") : outputs[i];
FfsDumper::DumpMode mode = modes.empty() ? FfsDumper::DUMP_ALL : modes[i];
UINT8 type = sectionTypes.empty() ? FfsDumper::IgnoreSectionType : sectionTypes[i];
result = ffsDumper.dump(model.index(0, 0), outPath, mode, type, inputs[i]);
if (result) {
std::cout << "Guid " << inputs[i].toLocal8Bit() << " failed with " << result << " code!" << std::endl;
lastError = result;
}
}
return lastError;
}
// Create ffsReport
FfsReport ffsReport(&model); FfsReport ffsReport(&model);
std::vector<UString> report = ffsReport.generate(); std::vector<UString> report = ffsReport.generate();
if (report.size()) { if (report.size()) {
@ -178,16 +131,85 @@ int main(int argc, char *argv[])
file << report[i].toLocal8Bit() << '\n'; file << report[i].toLocal8Bit() << '\n';
} }
// Dump all non-leaf elements, with report, default // 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) { if (argc == 2) {
return (ffsDumper.dump(model.index(0, 0), path + UString(".dump")) != U_SUCCESS); 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 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); 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; // Dump specific files, without report or GUID database
else {
std::vector<UString> inputs, outputs;
std::vector<FfsDumper::DumpMode> modes;
std::vector<UINT8> sectionTypes;
ReadType readType = READ_INPUT;
for (int i = 2; i < argc; i++) {
const char *arg = argv[i];
if (!std::strcmp(arg, "-i")) {
readType = READ_INPUT;
continue;
} else if (!std::strcmp(arg, "-o")) {
readType = READ_OUTPUT;
continue;
} else if (!std::strcmp(arg, "-m")) {
readType = READ_MODE;
continue;
} else if (!std::strcmp(arg, "-t")) {
readType = READ_SECTION;
continue;
}
if (readType == READ_INPUT) {
inputs.push_back(arg);
} else if (readType == READ_OUTPUT) {
outputs.push_back(getAbsPath(arg));
} else if (readType == READ_MODE) {
if (!std::strcmp(arg, "all"))
modes.push_back(FfsDumper::DUMP_ALL);
else if (!std::strcmp(arg, "body"))
modes.push_back(FfsDumper::DUMP_BODY);
else if (!std::strcmp(arg, "header"))
modes.push_back(FfsDumper::DUMP_HEADER);
else if (!std::strcmp(arg, "info"))
modes.push_back(FfsDumper::DUMP_INFO);
else if (!std::strcmp(arg, "file"))
modes.push_back(FfsDumper::DUMP_FILE);
else
return U_INVALID_PARAMETER;
} else if (readType == READ_SECTION) {
char *converted = const_cast<char *>(arg);
UINT8 sectionType = (UINT8)std::strtol(arg, &converted, 16);
if (converted == arg)
return U_INVALID_PARAMETER;
sectionTypes.push_back(sectionType);
}
} }
if (inputs.empty() || (!outputs.empty() && inputs.size() != outputs.size()) ||
(!modes.empty() && inputs.size() != modes.size()) ||
(!sectionTypes.empty() && inputs.size() != sectionTypes.size()))
return U_INVALID_PARAMETER;
USTATUS lastError = U_SUCCESS;
for (size_t i = 0; i < inputs.size(); i++) {
UString outPath = outputs.empty() ? path + UString(".dump") : outputs[i];
FfsDumper::DumpMode mode = modes.empty() ? FfsDumper::DUMP_ALL : modes[i];
UINT8 type = sectionTypes.empty() ? FfsDumper::IgnoreSectionType : sectionTypes[i];
result = ffsDumper.dump(model.index(0, 0), outPath, mode, type, inputs[i]);
if (result) {
std::cout << "Guid " << inputs[i].toLocal8Bit() << " failed with " << result << " code!" << std::endl;
lastError = result;
}
}
return lastError;
} }
// If parameters are different, show version and usage information // If parameters are different, show version and usage information