2015-04-02 16:04:37 +08:00
|
|
|
/* uefiextract_main.cpp
|
|
|
|
|
2016-02-02 09:08:08 +08:00
|
|
|
Copyright (c) 2016, Nikolaj Schlej. All rights reserved.
|
2015-04-02 16:04:37 +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 <QCoreApplication>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <iostream>
|
2016-06-26 16:05:45 +08:00
|
|
|
#include <fstream>
|
2015-04-02 16:04:37 +08:00
|
|
|
|
2016-06-26 11:54:21 +08:00
|
|
|
#include <../common/ustring.h>
|
2015-04-04 21:22:52 +08:00
|
|
|
#include "../common/ffsparser.h"
|
2016-06-25 18:22:28 +08:00
|
|
|
#include "../common/ffsreport.h"
|
2016-06-21 02:53:16 +08:00
|
|
|
#include "../common/fitparser.h"
|
2015-04-02 16:04:37 +08:00
|
|
|
#include "ffsdumper.h"
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication a(argc, argv);
|
|
|
|
a.setOrganizationName("CodeRush");
|
|
|
|
a.setOrganizationDomain("coderush.me");
|
|
|
|
a.setApplicationName("UEFIExtract");
|
|
|
|
|
2015-05-17 17:45:32 +08:00
|
|
|
if (a.arguments().length() > 32) {
|
|
|
|
std::cout << "Too many arguments" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-02 16:04:37 +08:00
|
|
|
if (a.arguments().length() > 1) {
|
2016-06-21 02:53:16 +08:00
|
|
|
// Check that input file exists
|
2016-06-26 16:05:45 +08:00
|
|
|
UString path(a.arguments().at(1).toLatin1());
|
|
|
|
QString qpath = QString(path);
|
|
|
|
QFileInfo fileInfo(qpath);
|
2015-04-02 16:04:37 +08:00
|
|
|
if (!fileInfo.exists())
|
2016-06-26 11:54:21 +08:00
|
|
|
return U_FILE_OPEN;
|
2015-04-02 16:04:37 +08:00
|
|
|
|
2016-06-21 02:53:16 +08:00
|
|
|
// Open the input file
|
2015-04-02 16:04:37 +08:00
|
|
|
QFile inputFile;
|
2016-06-26 16:05:45 +08:00
|
|
|
inputFile.setFileName(qpath);
|
2015-04-02 16:04:37 +08:00
|
|
|
if (!inputFile.open(QFile::ReadOnly))
|
2016-06-26 11:54:21 +08:00
|
|
|
return U_FILE_OPEN;
|
2016-06-21 02:53:16 +08:00
|
|
|
|
|
|
|
// Read and close the file
|
2016-07-05 23:22:03 +08:00
|
|
|
QByteArray b = inputFile.readAll();
|
2015-04-02 16:04:37 +08:00
|
|
|
inputFile.close();
|
|
|
|
|
2016-07-05 23:22:03 +08:00
|
|
|
UByteArray buffer(b.constData(), b.size());
|
|
|
|
|
2016-06-21 02:53:16 +08:00
|
|
|
// Create model and ffsParser
|
2015-04-02 16:04:37 +08:00
|
|
|
TreeModel model;
|
|
|
|
FfsParser ffsParser(&model);
|
2016-06-21 02:53:16 +08:00
|
|
|
// Parse input buffer
|
2016-06-26 11:54:21 +08:00
|
|
|
USTATUS result = ffsParser.parse(buffer);
|
2015-04-02 16:04:37 +08:00
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
2016-06-21 02:53:16 +08:00
|
|
|
// Show ffsParser's messages
|
2016-06-26 11:54:21 +08:00
|
|
|
std::vector<std::pair<UString, UModelIndex> > messages = ffsParser.getMessages();
|
2016-03-01 16:12:37 +08:00
|
|
|
for (size_t i = 0; i < messages.size(); i++) {
|
2016-07-05 22:18:55 +08:00
|
|
|
std::cout << (const char*)messages[i].first.toLocal8Bit() << std::endl;
|
2015-04-02 16:04:37 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 02:53:16 +08:00
|
|
|
// Get last VTF
|
2016-06-26 11:54:21 +08:00
|
|
|
UModelIndex lastVtf = ffsParser.getLastVtf();
|
2016-06-21 02:53:16 +08:00
|
|
|
if (lastVtf.isValid()) {
|
|
|
|
// Create fitParser
|
|
|
|
FitParser fitParser(&model);
|
|
|
|
// Find and parse FIT table
|
|
|
|
result = fitParser.parse(model.index(0, 0), lastVtf);
|
2016-06-26 11:54:21 +08:00
|
|
|
if (U_SUCCESS == result) {
|
2016-06-21 02:53:16 +08:00
|
|
|
// Show fitParser's messages
|
2016-06-26 11:54:21 +08:00
|
|
|
std::vector<std::pair<UString, UModelIndex> > fitMessages = fitParser.getMessages();
|
2016-06-21 02:53:16 +08:00
|
|
|
for (size_t i = 0; i < fitMessages.size(); i++) {
|
2016-07-05 22:18:55 +08:00
|
|
|
std::cout << (const char*)fitMessages[i].first.toLocal8Bit() << std::endl;
|
2016-06-21 02:53:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show FIT table
|
2016-06-26 11:54:21 +08:00
|
|
|
std::vector<std::vector<UString> > fitTable = fitParser.getFitTable();
|
2016-06-21 02:53:16 +08:00
|
|
|
if (fitTable.size()) {
|
|
|
|
std::cout << "-------------------------------------------------------------------" << std::endl;
|
|
|
|
std::cout << " Address | Size | Ver | Type | CS " << std::endl;
|
|
|
|
std::cout << "-------------------------------------------------------------------" << std::endl;
|
|
|
|
for (size_t i = 0; i < fitTable.size(); i++) {
|
2016-07-05 22:18:55 +08:00
|
|
|
std::cout << (const char*)fitTable[i][0].toLocal8Bit() << " | "
|
|
|
|
<< (const char*)fitTable[i][1].toLocal8Bit() << " | "
|
|
|
|
<< (const char*)fitTable[i][2].toLocal8Bit() << " | "
|
|
|
|
<< (const char*)fitTable[i][3].toLocal8Bit() << " | "
|
|
|
|
<< (const char*)fitTable[i][4].toLocal8Bit() << std::endl;
|
2016-06-21 02:53:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 18:22:28 +08:00
|
|
|
// Create ffsReport
|
|
|
|
FfsReport ffsReport(&model);
|
2016-06-26 11:54:21 +08:00
|
|
|
std::vector<UString> report = ffsReport.generate();
|
2016-06-25 18:22:28 +08:00
|
|
|
if (report.size()) {
|
2016-06-26 16:05:45 +08:00
|
|
|
std::ofstream ofs;
|
|
|
|
ofs.open("report.txt", std::ofstream::out);
|
|
|
|
for (size_t i = 0; i < report.size(); i++) {
|
2016-07-05 22:18:55 +08:00
|
|
|
ofs << (const char*)report[i].toLocal8Bit() << std::endl;
|
2016-06-25 18:22:28 +08:00
|
|
|
}
|
2016-06-26 16:05:45 +08:00
|
|
|
ofs.close();
|
2016-06-25 18:22:28 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 02:53:16 +08:00
|
|
|
// Create ffsDumper
|
2015-04-02 16:04:37 +08:00
|
|
|
FfsDumper ffsDumper(&model);
|
2015-05-17 17:45:32 +08:00
|
|
|
|
2016-06-21 03:20:47 +08:00
|
|
|
// Dump all non-leaf elements
|
2015-05-17 17:45:32 +08:00
|
|
|
if (a.arguments().length() == 2) {
|
2016-06-26 16:05:45 +08:00
|
|
|
return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump2").toLatin1().constData()) != U_SUCCESS);
|
2015-05-17 17:45:32 +08:00
|
|
|
}
|
2016-06-26 11:54:21 +08:00
|
|
|
else if (a.arguments().length() == 3 && a.arguments().at(2) == UString("all")) { // Dump everything
|
2016-06-26 16:05:45 +08:00
|
|
|
return (ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump2").toLatin1().constData(), true) != U_SUCCESS);
|
2016-06-21 03:20:47 +08:00
|
|
|
}
|
2016-06-26 11:54:21 +08:00
|
|
|
else if (a.arguments().length() == 3 && a.arguments().at(2) == UString("none")) { // Skip dumping
|
2016-06-25 18:22:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-21 02:53:16 +08:00
|
|
|
else { // Dump specific files
|
2015-05-17 17:45:32 +08:00
|
|
|
UINT32 returned = 0;
|
|
|
|
for (int i = 2; i < a.arguments().length(); i++) {
|
2016-06-26 16:05:45 +08:00
|
|
|
result = ffsDumper.dump(model.index(0, 0), fileInfo.fileName().append(".dump2").toLatin1().constData(), true, a.arguments().at(i).toLatin1().constData());
|
2015-05-17 17:45:32 +08:00
|
|
|
if (result)
|
|
|
|
returned |= (1 << (i - 1));
|
|
|
|
}
|
|
|
|
return returned;
|
|
|
|
}
|
2016-06-25 18:22:28 +08:00
|
|
|
|
|
|
|
return 0;
|
2015-04-02 16:04:37 +08:00
|
|
|
}
|
2016-06-21 02:53:16 +08:00
|
|
|
else { // Show version and usage information
|
2016-07-09 16:42:49 +08:00
|
|
|
std::cout << "UEFIExtract 0.12.1" << std::endl << std::endl
|
2016-06-25 18:22:28 +08:00
|
|
|
<< "Usage: 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 none - only generate report, no dump needed" << std::endl
|
|
|
|
<< " UIFIExtract imagefile GUID_1 GUID_2 ... GUID_31 - dump only FFS file(s) with specific GUID(s)" << std::endl
|
2016-07-09 16:42:49 +08:00
|
|
|
<< "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 << std::endl;
|
2015-04-02 16:04:37 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-06-26 16:05:45 +08:00
|
|
|
|
|
|
|
return 0;
|
2015-04-02 16:04:37 +08:00
|
|
|
}
|