2014-06-19 11:45:20 +08:00
|
|
|
/* uefipatch_main.cpp
|
|
|
|
|
2018-02-15 11:04:34 +08:00
|
|
|
Copyright (c) 2018, LongSoft. All rights reserved.
|
2014-06-19 11:45:20 +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 <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <iostream>
|
2018-05-08 03:01:17 +08:00
|
|
|
#include <string>
|
2018-05-22 05:43:34 +08:00
|
|
|
|
|
|
|
#include "../version.h"
|
2014-06-19 11:45:20 +08:00
|
|
|
#include "uefipatch.h"
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication a(argc, argv);
|
2017-12-11 08:54:40 +08:00
|
|
|
a.setOrganizationName("LongSoft");
|
|
|
|
a.setOrganizationDomain("longsoft.me");
|
2014-11-09 06:30:32 +08:00
|
|
|
a.setApplicationName("UEFIPatch");
|
2014-06-19 11:45:20 +08:00
|
|
|
|
|
|
|
UEFIPatch w;
|
|
|
|
UINT8 result = ERR_SUCCESS;
|
2018-05-19 23:55:51 +08:00
|
|
|
const QStringList &args = a.arguments();
|
|
|
|
UINT32 argumentsCount = args.length();
|
2018-05-08 03:01:17 +08:00
|
|
|
|
2018-05-19 23:55:51 +08:00
|
|
|
if (argumentsCount < 2) {
|
2018-05-22 05:43:34 +08:00
|
|
|
std::cout << "UEFIPatch " PROGRAM_VERSION " - UEFI image file patching utility" << std::endl << std::endl <<
|
2019-11-03 21:15:03 +08:00
|
|
|
"Usage: UEFIPatch image_file [patches.txt] [-o output]" << std::endl <<
|
|
|
|
"Usage: UEFIPatch image_file [-p \"Guid SectionType Patch\"] [-o output]" << std::endl << std::endl <<
|
2018-05-08 03:01:17 +08:00
|
|
|
"Patches will be read from patches.txt file by default\n";
|
2014-07-05 20:56:56 +08:00
|
|
|
return ERR_SUCCESS;
|
2014-06-19 11:45:20 +08:00
|
|
|
}
|
|
|
|
|
2018-05-19 23:55:51 +08:00
|
|
|
QString inputPath = a.arguments().at(1);
|
|
|
|
QString patches = "patches.txt";
|
|
|
|
QString outputPath = inputPath + ".patched";
|
2019-11-03 21:15:03 +08:00
|
|
|
int patchFrom = PATCH_FROM_FILE;
|
2018-05-19 23:55:51 +08:00
|
|
|
for (UINT32 i = 2; i < argumentsCount; i++) {
|
2019-11-03 21:15:03 +08:00
|
|
|
if ((args.at(i) == "-p" || args.at(i) == "--patch") && i + 1 < argumentsCount) {
|
|
|
|
patchFrom = PATCH_FROM_ARG;
|
|
|
|
patches = args.at(i+1);
|
|
|
|
i++;
|
|
|
|
} else if ((args.at(i) == "-o" || args.at(i) == "--output") && i + 1 < argumentsCount) {
|
2018-05-19 23:55:51 +08:00
|
|
|
outputPath = args.at(i+1);
|
|
|
|
i++;
|
|
|
|
} else if (patches == "patches.txt") {
|
|
|
|
patches = args.at(i);
|
|
|
|
} else {
|
|
|
|
result = ERR_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == ERR_SUCCESS) {
|
2019-11-03 21:15:03 +08:00
|
|
|
if (patchFrom == PATCH_FROM_FILE) {
|
|
|
|
result = w.patchFromFile(inputPath, patches, outputPath);
|
|
|
|
} else if (patchFrom == PATCH_FROM_ARG) {
|
|
|
|
result = w.patchFromArg(inputPath, patches, outputPath);
|
|
|
|
}
|
2018-05-19 23:55:51 +08:00
|
|
|
}
|
|
|
|
|
2014-06-19 11:45:20 +08:00
|
|
|
switch (result) {
|
|
|
|
case ERR_SUCCESS:
|
|
|
|
std::cout << "Image patched" << std::endl;
|
|
|
|
break;
|
2014-07-05 20:56:56 +08:00
|
|
|
case ERR_INVALID_PARAMETER:
|
|
|
|
std::cout << "Function called with invalid parameter" << std::endl;
|
|
|
|
break;
|
|
|
|
case ERR_NOTHING_TO_PATCH:
|
|
|
|
std::cout << "No patches can be applied to input file" << std::endl;
|
|
|
|
break;
|
|
|
|
case ERR_UNKNOWN_PATCH_TYPE:
|
|
|
|
std::cout << "Unknown patch type" << std::endl;
|
|
|
|
break;
|
|
|
|
case ERR_PATCH_OFFSET_OUT_OF_BOUNDS:
|
|
|
|
std::cout << "Patch offset out of bounds" << std::endl;
|
|
|
|
break;
|
|
|
|
case ERR_INVALID_SYMBOL:
|
|
|
|
std::cout << "Pattern format mismatch" << std::endl;
|
2014-06-19 11:45:20 +08:00
|
|
|
break;
|
|
|
|
case ERR_INVALID_FILE:
|
2018-05-08 03:01:17 +08:00
|
|
|
std::cout << patches.toStdString() << " file not found or can't be read" << std::endl;
|
2014-06-19 11:45:20 +08:00
|
|
|
break;
|
|
|
|
case ERR_FILE_OPEN:
|
|
|
|
std::cout << "Input file not found" << std::endl;
|
|
|
|
break;
|
|
|
|
case ERR_FILE_READ:
|
|
|
|
std::cout << "Input file can't be read" << std::endl;
|
|
|
|
break;
|
|
|
|
case ERR_FILE_WRITE:
|
|
|
|
std::cout << "Output file can't be written" << std::endl;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
std::cout << "Error " << result << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-11-09 06:30:32 +08:00
|
|
|
}
|