From a9399a0785407a5d20c7214dca889284505bf124 Mon Sep 17 00:00:00 2001 From: vit9696 Date: Sat, 19 May 2018 18:35:38 +0300 Subject: [PATCH] Implement replace all and add -o (--output) path to UEFIReplace --- .gitignore | 3 +++ UEFIReplace/uefireplace.cpp | 22 +++++++++++++--------- UEFIReplace/uefireplace.h | 4 ++-- UEFIReplace/uefireplace_main.cpp | 23 +++++++++++++++++++---- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 910d521..c84a076 100644 --- a/.gitignore +++ b/.gitignore @@ -235,3 +235,6 @@ UEFIFind/UEFIFind UEFIPatch/UEFIPatch UEFIReplace/UEFIReplace UEFITool +.qmake.stash +UEFITool.app/ +uefitool_plugin_import.cpp diff --git a/UEFIReplace/uefireplace.cpp b/UEFIReplace/uefireplace.cpp index f4ff1ea..97eaafb 100644 --- a/UEFIReplace/uefireplace.cpp +++ b/UEFIReplace/uefireplace.cpp @@ -25,7 +25,7 @@ UEFIReplace::~UEFIReplace() delete ffsEngine; } -UINT8 UEFIReplace::replace(QString inPath, const QByteArray & guid, const UINT8 sectionType, const QString contentPath) +UINT8 UEFIReplace::replace(const QString & inPath, const QByteArray & guid, const UINT8 sectionType, const QString & contentPath, const QString & outPath, bool replaceOnce) { QFileInfo fileInfo = QFileInfo(inPath); if (!fileInfo.exists()) @@ -57,7 +57,7 @@ UINT8 UEFIReplace::replace(QString inPath, const QByteArray & guid, const UINT8 QByteArray contents = contentFile.readAll(); contentFile.close(); - result = replaceInFile(model->index(0, 0), guid, sectionType, contents); + result = replaceInFile(model->index(0, 0), guid, sectionType, contents, replaceOnce); if (result) return result; @@ -69,7 +69,7 @@ UINT8 UEFIReplace::replace(QString inPath, const QByteArray & guid, const UINT8 return ERR_NOTHING_TO_PATCH; QFile outputFile; - outputFile.setFileName(inPath.append(".patched")); + outputFile.setFileName(outPath); if (!outputFile.open(QFile::WriteOnly)) return ERR_FILE_WRITE; @@ -80,7 +80,7 @@ UINT8 UEFIReplace::replace(QString inPath, const QByteArray & guid, const UINT8 return ERR_SUCCESS; } -UINT8 UEFIReplace::replaceInFile(const QModelIndex & index, const QByteArray & guid, const UINT8 sectionType, const QByteArray & newData) +UINT8 UEFIReplace::replaceInFile(const QModelIndex & index, const QByteArray & guid, const UINT8 sectionType, const QByteArray & newData, bool replaceOnce) { if (!model || !index.isValid()) return ERR_INVALID_PARAMETER; @@ -88,19 +88,23 @@ UINT8 UEFIReplace::replaceInFile(const QModelIndex & index, const QByteArray & g QModelIndex fileIndex = model->findParentOfType(index, Types::File); QByteArray fileGuid = model->header(fileIndex).left(sizeof(EFI_GUID)); if (fileGuid == guid) { - return ffsEngine->replace(index, newData, REPLACE_MODE_BODY); + UINT8 result = ffsEngine->replace(index, newData, REPLACE_MODE_BODY); + if (replaceOnce || (result != ERR_SUCCESS && result != ERR_NOTHING_TO_PATCH)) + return result; } } bool patched = false; if (model->rowCount(index) > 0) { for (int i = 0; i < model->rowCount(index); i++) { - UINT8 result = replaceInFile(index.child(i, 0), guid, sectionType, newData); - if (!result) { + UINT8 result = replaceInFile(index.child(i, 0), guid, sectionType, newData, replaceOnce); + if (result == ERR_SUCCESS) { patched = true; - break; - } else if (result != ERR_NOTHING_TO_PATCH) + if (replaceOnce) + break; + } else if (result != ERR_NOTHING_TO_PATCH) { return result; + } } } diff --git a/UEFIReplace/uefireplace.h b/UEFIReplace/uefireplace.h index 682a1f6..d70ac2b 100644 --- a/UEFIReplace/uefireplace.h +++ b/UEFIReplace/uefireplace.h @@ -30,10 +30,10 @@ public: explicit UEFIReplace(QObject *parent = 0); ~UEFIReplace(); - UINT8 replace(const QString inPath, const QByteArray & guid, const UINT8 sectionType, const QString contentPath); + UINT8 replace(const QString & inPath, const QByteArray & guid, const UINT8 sectionType, const QString & contentPath, const QString & outPath, bool replaceOnce); private: - UINT8 replaceInFile(const QModelIndex & index, const QByteArray & guid, const UINT8 sectionType, const QByteArray & contents); + UINT8 replaceInFile(const QModelIndex & index, const QByteArray & guid, const UINT8 sectionType, const QByteArray & contents, bool replaceOnce); FfsEngine* ffsEngine; TreeModel* model; }; diff --git a/UEFIReplace/uefireplace_main.cpp b/UEFIReplace/uefireplace_main.cpp index aae90d7..dfd8bf0 100644 --- a/UEFIReplace/uefireplace_main.cpp +++ b/UEFIReplace/uefireplace_main.cpp @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) if (args.length() < 5) { std::cout << "UEFIReplace 0.1.3 - UEFI image file replacement utility" << std::endl << std::endl << - "Usage: UEFIReplace image_file guid section_type contents_file" << std::endl; + "Usage: UEFIReplace image_file guid section_type contents_file [-o output] [-all]" << std::endl; return ERR_SUCCESS; } @@ -37,10 +37,25 @@ int main(int argc, char *argv[]) QByteArray guid = QByteArray::fromRawData((const char*)&uuid.data1, sizeof(EFI_GUID)); bool converted; UINT8 sectionType = (UINT8)args.at(3).toUShort(&converted, 16); - if (!converted) + if (!converted) { result = ERR_INVALID_PARAMETER; - else - result = r.replace(args.at(1), guid, sectionType, args.at(4)); + } else { + QString output = args.at(1) + ".patched"; + bool replaceOnce = true; + for (int i = 5, sz = args.size(); i < sz; i++) { + if ((args.at(i) == "-o" || args.at(i) == "--output") && i + 1 < sz) { + output = args.at(i+1); + i++; + } else if (args.at(i) == "-all") { + replaceOnce = false; + } else { + result = ERR_INVALID_PARAMETER; + } + } + if (result == ERR_SUCCESS) { + result = r.replace(args.at(1), guid, sectionType, args.at(4), output, replaceOnce); + } + } switch (result) { case ERR_SUCCESS: