From 8ea96ffd5d4faf93954088401baa565ad8a4210b Mon Sep 17 00:00:00 2001 From: vit9696 Date: Mon, 21 May 2018 10:07:33 +0300 Subject: [PATCH] Support replacing raw sections and ffs via -asis --- UEFIReplace/uefireplace.cpp | 18 +++++++++++------- UEFIReplace/uefireplace.h | 4 ++-- UEFIReplace/uefireplace_main.cpp | 7 +++++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/UEFIReplace/uefireplace.cpp b/UEFIReplace/uefireplace.cpp index 68eca38..d4ba092 100644 --- a/UEFIReplace/uefireplace.cpp +++ b/UEFIReplace/uefireplace.cpp @@ -25,7 +25,7 @@ UEFIReplace::~UEFIReplace() delete ffsEngine; } -UINT8 UEFIReplace::replace(const QString & inPath, const QByteArray & guid, const UINT8 sectionType, const QString & contentPath, const QString & outPath, bool replaceOnce) +UINT8 UEFIReplace::replace(const QString & inPath, const QByteArray & guid, const UINT8 sectionType, const QString & contentPath, const QString & outPath, bool replaceAsIs, bool replaceOnce) { QFileInfo fileInfo = QFileInfo(inPath); if (!fileInfo.exists()) @@ -57,7 +57,8 @@ UINT8 UEFIReplace::replace(const QString & inPath, const QByteArray & guid, cons QByteArray contents = contentFile.readAll(); contentFile.close(); - result = replaceInFile(model->index(0, 0), guid, sectionType, contents, replaceOnce); + result = replaceInFile(model->index(0, 0), guid, sectionType, contents, + replaceAsIs ? REPLACE_MODE_AS_IS : REPLACE_MODE_BODY, replaceOnce); if (result) return result; @@ -80,21 +81,24 @@ UINT8 UEFIReplace::replace(const QString & inPath, const QByteArray & guid, cons return ERR_SUCCESS; } -UINT8 UEFIReplace::replaceInFile(const QModelIndex & index, const QByteArray & guid, const UINT8 sectionType, const QByteArray & newData, bool replaceOnce) +UINT8 UEFIReplace::replaceInFile(const QModelIndex & index, const QByteArray & guid, const UINT8 sectionType, const QByteArray & newData, const UINT8 mode, bool replaceOnce) { if (!model || !index.isValid()) return ERR_INVALID_PARAMETER; bool patched = false; if (model->subtype(index) == sectionType) { - QModelIndex fileIndex = model->findParentOfType(index, Types::File); + QModelIndex fileIndex = index; + if (model->type(index) != Types::File) + fileIndex = model->findParentOfType(index, Types::File); QByteArray fileGuid = model->header(fileIndex).left(sizeof(EFI_GUID)); + bool guidMatch = fileGuid == guid; if (!guidMatch && sectionType == EFI_SECTION_FREEFORM_SUBTYPE_GUID) { - QByteArray subGuid = model->header(index).mid(sizeof(uint32_t), sizeof(EFI_GUID)); + QByteArray subGuid = model->header(index).mid(sizeof(UINT32), sizeof(EFI_GUID)); guidMatch = subGuid == guid; } if (guidMatch && model->action(index) != Actions::Replace) { - UINT8 result = ffsEngine->replace(index, newData, REPLACE_MODE_BODY); + UINT8 result = ffsEngine->replace(index, newData, mode); if (replaceOnce || (result != ERR_SUCCESS && result != ERR_NOTHING_TO_PATCH)) return result; patched = result == ERR_SUCCESS; @@ -103,7 +107,7 @@ UINT8 UEFIReplace::replaceInFile(const QModelIndex & index, const QByteArray & g if (model->rowCount(index) > 0) { for (int i = 0; i < model->rowCount(index); i++) { - UINT8 result = replaceInFile(index.child(i, 0), guid, sectionType, newData, replaceOnce); + UINT8 result = replaceInFile(index.child(i, 0), guid, sectionType, newData, mode, replaceOnce); if (result == ERR_SUCCESS) { patched = true; if (replaceOnce) diff --git a/UEFIReplace/uefireplace.h b/UEFIReplace/uefireplace.h index d70ac2b..c79ae52 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, const QString & outPath, bool replaceOnce); + UINT8 replace(const QString & inPath, const QByteArray & guid, const UINT8 sectionType, const QString & contentPath, const QString & outPath, bool replaceAsIs, bool replaceOnce); private: - UINT8 replaceInFile(const QModelIndex & index, const QByteArray & guid, const UINT8 sectionType, const QByteArray & contents, bool replaceOnce); + UINT8 replaceInFile(const QModelIndex & index, const QByteArray & guid, const UINT8 sectionType, const QByteArray & contents, const UINT8 mode, bool replaceOnce); FfsEngine* ffsEngine; TreeModel* model; }; diff --git a/UEFIReplace/uefireplace_main.cpp b/UEFIReplace/uefireplace_main.cpp index dfd8bf0..93c72ec 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 [-o output] [-all]" << std::endl; + "Usage: UEFIReplace image_file guid section_type contents_file [-o output] [-all] [-asis]" << std::endl; return ERR_SUCCESS; } @@ -42,18 +42,21 @@ int main(int argc, char *argv[]) } else { QString output = args.at(1) + ".patched"; bool replaceOnce = true; + bool replaceAsIs = false; 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 if (args.at(i) == "-asis") { + replaceAsIs = true; } else { result = ERR_INVALID_PARAMETER; } } if (result == ERR_SUCCESS) { - result = r.replace(args.at(1), guid, sectionType, args.at(4), output, replaceOnce); + result = r.replace(args.at(1), guid, sectionType, args.at(4), output, replaceAsIs, replaceOnce); } }