-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEFIUtils.d
59 lines (52 loc) · 1.38 KB
/
EFIUtils.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
module EFIUtils;
private {
import EFI;
import std.stdio : write, writefln;
}
T calculateChecksum(T)(void *_ptr, size_t len, T start = 0) {
len /= T.sizeof;
T *ptr = cast(T*)_ptr;
foreach(i; 0..len)
start -= ptr[i];
return start;
}
T find(T)(EFIContainer container) {
foreach(ref c; container.containers) {
if(typeid(c) == typeid(T))
return cast(T)c;
if(typeid(c) == typeid(ExtendedSection) || typeid(c) == typeid(CompressedSection)) {
auto raw = find!T(c);
if(raw !is null)
return raw;
}
}
return null;
}
T[] findAll(T)(EFIContainer container) {
File[] found;
foreach(ref c; container.containers) {
if(typeid(c) == typeid(T))
found ~= cast(T)c;
found ~= findAll!T(c);
}
return found;
}
void printFileMapping(EFIContainer container) {
foreach(ref file; findAll!File(container)) {
auto name = find!UserInterfaceSection(file);
if(name !is null)
writefln("%s => %s", file.guid, name.fileName);
else
writefln("%s => <NULL>", file.guid);
}
}
void printEFI(EFIContainer[] containers, ulong depth = 0) {
foreach(c; containers)
printEFI(c, depth + 1);
}
void printEFI(EFIContainer container, ulong depth = 0) {
foreach(i; 0..depth)
write("\t");
writefln("%08X: %s (%u, 0x%08X)", container.offset, container.name(), container.length(), container.length());
printEFI(container.containers, depth);
}