-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinjector.d
More file actions
81 lines (69 loc) · 2.14 KB
/
injector.d
File metadata and controls
81 lines (69 loc) · 2.14 KB
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
import Console : Console;
import EFI : EFI, File, FileType, Unknown, UserInterfaceSection, RawSection;
import EFIUtils : find, findAll, printEFI, printFileMapping;
import std.stdio : stderr;
import std.file : read, write, mkdir, chdir, exists;
import std.exception : enforce;
import std.string : format;
int main(string[] args) {
Console.Init(args, format("USAGE: %s <ORIGINAL WPH> <OUTPUT WPH>", args[0]));
string filename = Console.GetInput!string("Please enter a filename");
auto container = EFI.parseCapsule(filename);
debug printEFI(container);
debug printFileMapping(container);
//Sanity check
debug {
ubyte[] newEFI = EFI.getBinary(container);
ubyte[] oldEFI = cast(ubyte[])read(filename);
enforce(newEFI.length == oldEFI.length);
enforce(newEFI == oldEFI);
}
auto dumpdir = filename ~ " dump";
enforce(dumpdir.exists());
foreach(file; findAll!File(container)) {
auto ui = find!UserInterfaceSection(file);
auto raw = find!RawSection(file);
string name;
if(ui is null)
name = file.guid.toString() ~ ".bin";
else
name = ui.fileName;
name = dumpdir ~ "/" ~ name;
enforce(exists(name));
auto data = cast(ubyte[])read(name);
if(raw is null) {
switch(file.header.type) {
case FileType.Freeform:
case FileType.Application:
file.data = data;
break;
case FileType.Raw:
//Workaround for bug
//if(file.containers.length == 1 && typeid(file.containers[0]) == typeid(Unknown))
if(file.containers.length == 1) {
foreach(c; file.containers)
if(typeid(c) == typeid(Unknown))
file.data = data;
else
goto default;
}
break;
default:
stderr.writefln("WARNING: %s has no raw data", name);
printEFI(file);
continue;
}
} else {
raw.data = data;
}
}
string modfile = Console.GetInput!string("Please enter an output filename");
if(modfile.exists()) {
stderr.writefln("ERROR: \"%s\" exists already. To regenerate the file, delete it", modfile);
return 1;
}
auto modEFI = EFI.getBinary(container);
enforce(modEFI.length == read(filename).length);
write(modfile, modEFI);
return 0;
}