-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
292 lines (236 loc) · 6.98 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "args_parser.h"
#include <fstream>
#include <iostream>
#include <cstring> // memset
#define LOG(msg) \
std::cout << msg << std::endl;
#define CHECK(cond, err_msg) \
if (!(cond)) { \
std::cout << err_msg << std::endl; \
return 1; \
}
// Little endian
struct MicrocodeInfo {
uint64_t pos;
uint16_t revision; // 0x04
uint32_t cpuid; // 0x0C
uint8_t platform; // 0x18
uint16_t size; // 0x20 + 0x21
static void printHeader()
{
printf(" CPUID | Pf | Rev | Offset | Size\n");
printf("-------+----+-----+---------+--------\n");
}
void dump()
{
printf(" %5X | %02X | %3X | 0x%05lX | 0x%04X\n",
cpuid, platform, revision, pos, size);
}
void toFileName(std::string &file)
{
file.resize(256);
int len = snprintf(&file[0], file.size(), "cpu%5X_plat%02X_rev%04X.bin",
cpuid, platform, revision);
file.resize(len);
}
};
uint16_t read2Bytes(std::istream *file)
{
static uint8_t buffer[2];
file->read((char *)buffer, 2);
if (!file->good())
return 0;
return buffer[0] | buffer[1] << 8;
}
bool readMicrocode(std::istream *file, MicrocodeInfo *info = nullptr)
{
int64_t pos = file->tellg();
char buffer[0x18];
file->read(buffer, 0x18);
file->seekg(pos);
if (!(buffer[0x00] == 0x01
&& buffer[0x01] == 0x00
&& buffer[0x02] == 0x00
&& buffer[0x03] == 0x00
&& buffer[0x14] == 0x01
&& buffer[0x15] == 0x00
&& buffer[0x16] == 0x00
&& buffer[0x17] == 0x00))
return false;
if (!info)
return true;
info->pos = pos;
file->seekg(pos + 0x04);
info->revision = read2Bytes(file);
file->seekg(pos + 0x0C);
info->cpuid = read2Bytes(file) | read2Bytes(file) << 16;
file->seekg(pos + 0x18);
info->platform = file->get();
file->seekg(pos + 0x20);
info->size = read2Bytes(file);
if (info->size == 0)
info->size = 0x800; // Default?
file->seekg(pos);
return true;
}
int extractFile(CONSTSTR rompath, int64_t pos, std::string binpath)
{
std::ifstream romfile(rompath, std::ios_base::binary);
CHECK(romfile.is_open(), "ROM: File not found");
romfile.seekg(pos);
MicrocodeInfo info;
CHECK(readMicrocode(&romfile, &info), "ROM: No microcode at pos 0x" << romfile.tellg());
info.printHeader();
info.dump();
if (binpath.empty()) // Generate one
info.toFileName(binpath);
std::ofstream binfile(binpath, std::ios_base::binary | std::ios_base::trunc);
CHECK(binfile, "BIN: Cannot create & open file.");
char *data = new char[info.size];
romfile.read(data, info.size);
binfile.write(data, info.size);
delete[] data;
binfile.flush();
LOG("Saved to '" << binpath << "'");
return 0;
}
int scanFile(CONSTSTR filename, int64_t pos)
{
std::ifstream romfile(filename, std::ios_base::binary | std::ios_base::ate);
CHECK(romfile.is_open(), "ROM: File not found");
int64_t bin_size = romfile.tellg();
char peek;
MicrocodeInfo info;
int64_t pos_end = 0;
info.printHeader();
while (pos < bin_size) {
romfile.seekg(pos);
romfile.get(peek);
romfile.unget();
if (peek == 0x01 && readMicrocode(&romfile, &info)) {
if (pos != pos_end) {
printf("\tUnknown region: 0x%lX to 0x%lX (0x%lX bytes)\n",
pos_end, pos - 1, pos - pos_end);
}
info.dump();
pos += (int64_t)info.size;
pos_end = pos;
} else {
// Speed optimization. Gaps are usually multiples of 256 bytes
pos += pos_end > 0 ? 0x100 : 0x04;
}
}
if (pos_end != bin_size)
printf("\tUnknown region: 0x%lX to 0x%lX (0x%lX bytes) [EOF]\n",
pos_end, pos - 1, pos - pos_end);
else
printf("\tNo unknown region [EOF]\n");
return 0;
}
int patchFile(CONSTSTR rompath, CONSTSTR binpath,
int64_t rompos, int64_t binpos, bool force)
{
std::cout << std::hex;
LOG("\tROM file: " << rompath);
LOG("\tMicrocode: " << binpath);
// Open and check files
std::fstream romfile(rompath,
std::ios_base::in | std::ios_base::out | std::ios_base::binary);
CHECK(romfile.is_open(), "ROM: File not found");
std::ifstream binfile(binpath, std::ios_base::binary | std::ios_base::ate);
CHECK(binfile.is_open(), "BIN: File not found");
// Move to write position
auto bin_size = binfile.tellg();
romfile.seekg(rompos);
binfile.seekg(binpos);
// Read and check microcodes
MicrocodeInfo old_uc, new_uc;
bool microcode_ok = readMicrocode(&romfile, &old_uc);
CHECK(microcode_ok || force, "ROM: No microcode at pos 0x" << romfile.tellg());
CHECK(readMicrocode(&binfile, &new_uc), "BIN: No microcode at pos 0x" << binfile.tellg());
// Verify data lengths
CHECK(bin_size - binpos >= new_uc.size, "BIN: Invalid file length");
CHECK(romfile.tellg() == rompos, "Sanity check failed");
bin_size = new_uc.size;
LOG("\nOld vs new microcode:");
old_uc.printHeader();
if (microcode_ok)
old_uc.dump();
else
LOG(" -- NONE OR INVALID MICROCODE --"); // Forced
new_uc.dump();
LOG("Please ENTER to confirm.");
getchar();
char *buffer = new char[bin_size];
binfile.read(buffer, bin_size);
romfile.write(buffer, bin_size);
delete[] buffer;
binfile.close();
romfile.close();
LOG("DONE. Patched.");
return 0;
}
int eraseFile(CONSTSTR rompath, int64_t pos, int64_t fill)
{
CHECK(fill >= 0 && fill <= 0xFF, "Fill byte is out of range");
std::fstream romfile(rompath,
std::ios_base::in | std::ios_base::out | std::ios_base::binary);
CHECK(romfile.is_open(), "ROM: File not found");
// Move to write position
romfile.seekg(pos);
// Read and check microcodes
MicrocodeInfo old_uc;
bool microcode_ok = readMicrocode(&romfile, &old_uc);
CHECK(microcode_ok, "ROM: No microcode at pos 0x" << romfile.tellg());
LOG("\nMicrocode to remove:");
old_uc.printHeader();
old_uc.dump();
LOG("Please ENTER to confirm.");
getchar();
char *buffer = new char[old_uc.size];
memset(buffer, (uint8_t)fill, old_uc.size);
romfile.write(buffer, old_uc.size);
delete[] buffer;
romfile.close();
LOG("DONE. Removed microcode.");
return 0;
}
int main(int argc, char **argv)
{
LOG("iMicroUpdate - Updater tool for Intel CPU microcode in BIOSes");
LOG("");
// Scan
CLIArgStr ca_scanfile("scan", "");
CLIArg::parseArgs(argc, argv);
// Extract
CLIArgStr ca_extractfile("extract", "");
CLIArg::parseArgs(argc, argv);
// Patch
CLIArgStr ca_patchfile("patch", "");
CLIArgStr ca_binfile("bin", "");
CLIArgS64 ca_rompos("posr", 0);
CLIArgS64 ca_binpos("posb", 0);
CLIArgFlag ca_force("force");
// Erase
CLIArgStr ca_erasefile("erase", "");
CLIArgS64 ca_fill("fill", 0);
CLIArg::parseArgs(argc, argv);
if (!ca_extractfile.get().empty())
return extractFile(ca_extractfile.get(), ca_rompos.get(),
ca_binfile.get());
if (!ca_scanfile.get().empty())
return scanFile(ca_scanfile.get(), ca_rompos.get());
if (!ca_patchfile.get().empty())
return patchFile(
ca_patchfile.get(), ca_binfile.get(),
ca_rompos.get(), ca_binpos.get(), ca_force.get());
if (!ca_erasefile.get().empty())
return eraseFile(ca_erasefile.get(), ca_rompos.get(),
ca_fill.get());
LOG(" Scan: -scan FILE [-posr 0]");
LOG(" Extract: -extract ROMFILE [-bin DESTINATION] [-posr 0]");
LOG(" Patch: -patch ROMFILE -bin MICROCODE -posr 0 [-posb 0] [-force]");
LOG(" Erase: -erase ROMFILE [-posr 0] [-fill 0]");
LOG("");
return 1;
}