-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractor.d
77 lines (66 loc) · 1.92 KB
/
extractor.d
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
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 <INPUT 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";
if(dumpdir.exists()) {
stderr.writefln("Dump folder \"%s\" already exists. Please delete the dump first", dumpdir);
return 1;
}
mkdir(dumpdir);
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;
ubyte[] data;
if(raw is null) {
switch(file.header.type) {
case FileType.Freeform:
case FileType.Application:
data = file.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))
data = file.data;
else
goto default;
}
break;
default:
stderr.writefln("WARNING: %s has no raw data", name);
printEFI(file);
continue;
}
} else {
data = raw.data;
}
name = dumpdir ~ "/" ~ name;
enforce(!exists(name));
write(name, data);
}
return 0;
}