forked from LongSoft/Universal-IFR-Extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-cli.cpp
178 lines (127 loc) · 5.79 KB
/
main-cli.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Header files
#include <string>
#include <vector>
#include <fstream>
#include "EFI.h"
#include "UEFI.h"
#include <iostream>
#include <map>
using namespace std;
// Error codes
enum {IFR_EXTRACTED, FILE_NOT_FOUND, UNKNOWN_PROTOCOL};
// Global definitions
enum type {EFI, UEFI, UNKNOWN};
// Global variables
string fileLocation, outputFile;
int errorCode;
string buffer;
type protocol;
vector<string> strings;
std::ostream& operator<<(std::ostream& out, const type value)
{
map<type, string> strs;
strs[EFI] = "EFI";
strs[UEFI] = "UEFI";
strs[UNKNOWN] = "UNKNOWN";
return out << strs[value];
}
bool fileExists(const string &file) {
// Open file
ifstream fin(file.c_str());
// Return if first character doesn't equal EOF
return fin.peek() != EOF;
}
void readFile(const string &file, string &buffer) {
// Initialize variables
ifstream fin(file.c_str(), ios::binary);
// Clear buffer
buffer.clear();
// Read in file
while(fin.peek() != EOF)
buffer.push_back(fin.get());
// Close file
fin.close();
}
type getType(const string &buffer) {
// Go through buffer
for(unsigned int i = 0; i < buffer.size() - 9; i++)
// Check if an EFI string package was found
if((buffer[i] != '\x00' || buffer[i + 1] != '\x00' || buffer[i + 2] != '\x00' || buffer[i + 3] != '\x00') && buffer[i + 4] == '\x02' && buffer[i + 5] == '\x00' && (buffer[i + 6] != '\x00' || buffer[i + 7] != '\x00' || buffer[i + 8] != '\x00' || buffer[i + 9] != '\x00') && i + static_cast<unsigned char>(buffer[i + 6]) + (static_cast<unsigned char>(buffer[i + 7]) << 8) + (static_cast<unsigned char>(buffer[i + 8]) << 16) + (static_cast<unsigned char>(buffer[i + 9]) << 24) < buffer.size() && buffer[i + static_cast<unsigned char>(buffer[i + 6]) + (static_cast<unsigned char>(buffer[i + 7]) << 8) + (static_cast<unsigned char>(buffer[i + 8]) << 16) + (static_cast<unsigned char>(buffer[i + 9]) << 24) + 4] >= 'a' && buffer[i + static_cast<unsigned char>(buffer[i + 6]) + (static_cast<unsigned char>(buffer[i + 7]) << 8) + (static_cast<unsigned char>(buffer[i + 8]) << 16) + (static_cast<unsigned char>(buffer[i + 9]) << 24) + 4] <= 'z' && i + static_cast<unsigned char>(buffer[i]) + (static_cast<unsigned char>(buffer[i + 1]) << 8) + (static_cast<unsigned char>(buffer[i + 2]) << 16) + (static_cast<unsigned char>(buffer[i + 3]) << 24) < buffer.size() && buffer[i + static_cast<unsigned char>(buffer[i]) + (static_cast<unsigned char>(buffer[i + 1]) << 8) + (static_cast<unsigned char>(buffer[i + 2]) << 16) + (static_cast<unsigned char>(buffer[i + 3]) << 24) + 4] == '\x02' && buffer[i + static_cast<unsigned char>(buffer[i]) + (static_cast<unsigned char>(buffer[i + 1]) << 8) + (static_cast<unsigned char>(buffer[i + 2]) << 16) + (static_cast<unsigned char>(buffer[i + 3]) << 24) + 5] == '\x00')
// Return EFI
return EFI;
// Otherwise check if a UEFI string package was found
else if((buffer[i] != '\x00' || buffer[i + 1] != '\x00' || buffer[i + 2] != '\x00') && buffer[i + 3] == '\x04' && buffer[i + 4] == '\x34' && buffer[i + 44] == '\x01' && buffer[i + 45] == '\x00' && buffer[i + 48] == '\x2D' && i + static_cast<unsigned char>(buffer[i]) + (static_cast<unsigned char>(buffer[i + 1]) << 8) + (static_cast<unsigned char>(buffer[i + 2]) << 16) < buffer.size() && buffer[i + static_cast<unsigned char>(buffer[i]) + (static_cast<unsigned char>(buffer[i + 1]) << 8) + (static_cast<unsigned char>(buffer[i + 2]) << 16) - 1] == '\x00' && buffer[i + static_cast<unsigned char>(buffer[i]) + (static_cast<unsigned char>(buffer[i + 1]) << 8) + (static_cast<unsigned char>(buffer[i + 2]) << 16) - 2] == '\x00')
// Return UEFI
return UEFI;
// Return unknown
return UNKNOWN;
}
int main(int argc, char **argv)
{
if (argc != 3)
{
cout << "EFI/UEFI IFR Extractor LS v0.3.2" << endl;
cout << "Usage: ifrextract input_file output_file" << endl;
return 1;
}
// Reset error code
errorCode = IFR_EXTRACTED;
fileLocation = argv[1];
outputFile = argv[2];
// Check if file exists
if(fileExists(fileLocation)) {
readFile(fileLocation, buffer);
protocol = getType(buffer);
cout << "Input: " << fileLocation << endl;
cout << "Output: " << outputFile << endl;
cout << "Protocol: " << protocol << endl;
// Check if protocol is unknown
if(protocol == UNKNOWN) {
// Set error code
errorCode = UNKNOWN_PROTOCOL;
// Display message
cout << "error: Unknown protocol" << endl;
// Break
return errorCode;
}
// Clear strings
strings.clear();
// Check if protocol is EFI
if(protocol == EFI) {
// Initialize EFI variables
vector<EFI_IFR_STRING_PACK> stringPackages;
vector<EFI_IFR_FORM_SET_PACK> formSets;
// Get EFI string packages
getEFIStringPackages(stringPackages, buffer);
// Get EFI strings
getEFIStrings(stringPackages, strings, buffer);
// Get EFI form sets
getEFIFormSets(formSets, buffer, stringPackages, strings);
// Generate EFI IFR dump
generateEFIIFRDump(outputFile, stringPackages, formSets, buffer, strings);
}
// Otherwise check if protocol is UEFI
else if(protocol == UEFI) {
// Initialize UEFI variables
vector<UEFI_IFR_STRING_PACK> stringPackages;
vector<UEFI_IFR_FORM_SET_PACK> formSets;
// Get UEFI string packages
getUEFIStringPackages(stringPackages, buffer);
// Get UEFI strings
getUEFIStrings(stringPackages, strings, buffer);
// Get UEFI form sets
getUEFIFormSets(formSets, buffer, stringPackages, strings);
// Generate UEFI IFR dump
generateUEFIIFRDump(outputFile, stringPackages, formSets, buffer, strings);
}
}
// Otherwise
else
{
// Set error code
errorCode = FILE_NOT_FOUND;
cout << "File not found!" << endl;
}
// Return
return errorCode;
}