-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathromdiver.sh
More file actions
executable file
·136 lines (106 loc) · 3.8 KB
/
romdiver.sh
File metadata and controls
executable file
·136 lines (106 loc) · 3.8 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
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
#!/bin/bash
TOOLS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/bin"
IFDTOOL="$TOOLS_DIR/ich_descriptors_tool"
ME_CLEANER="$TOOLS_DIR/me_cleaner.py"
ROM_HEADERS="$TOOLS_DIR/romheaders"
UEFI_EXTRACT="$TOOLS_DIR/uefiextract"
BIOS_EXTRACT="$TOOLS_DIR/bios_extract"
PHOENIX_EXTRACT="$TOOLS_DIR/phoenix_extract.py"
OLD_VGABIOS_PATTERN="PCIR"
INTEL_VGABIOS_PATTERN="pci8086,"
INTEL_NVIDIA_PATTERN="VGA Compatible"
GOP_DRIVER_PATTERN="IntelGopDriver"
GOP_VBT_PATTERN="IntelGopVbt"
declare -a INTEL_VGABIOS_DEVICE_ID_LIST=("0406" "0106")
ROM_FILE=""
OUTPUT_DIR=""
VERBOSE=0
USER=""
if $VERBOSE ; then
set -x
fi
function is_new_x86_layout() {
local src="$1"
$IFDTOOL -f "$src"
}
function get_real_mac() {
local src="$1"
$IFDTOOL -f "$src" | awk -F: -v key="The MAC address might be at offset 0x1000" \
"\$1==key {printf(\"%s:%s:%s:%s:%s:%s\", \$2, \$3, \$4, \$5, \$6, \$7)}" | tr -d '[:space:]' > $OUTPUT_DIR/macaddress
chown "$USER:" "$OUTPUT_DIR/macaddress"
}
function get_vgabios_name() {
local src="$1"
echo -n "VGABIOS_NAME=pci" > vgabios_pci.name && "$ROM_HEADERS" "$src" | grep 'Vendor ID:' | cut -d ':' -f 2 | tr -d '[:space:]' | sed -e "s/^0x//" >> vgabios_pci.name && \
echo -n "," >> vgabios_pci.name && "$ROM_HEADERS" "$src" | grep 'Device ID:' | cut -d ':' -f 2 | tr -d '[:space:]' | sed -e "s/^0x//" >> vgabios_pci.name && echo ".rom" >> vgabios_pci.name
}
function extract_x86_blobs() {
local src="$1"
cp -f "$src" "$OUTPUT_DIR/rom.bin"
$IFDTOOL -d -f "$OUTPUT_DIR/rom.bin"
mv "$OUTPUT_DIR/rom.bin.BIOS.bin" "$OUTPUT_DIR/uefi.bin"
chown "$USER:" "$OUTPUT_DIR/uefi.bin"
mv "$OUTPUT_DIR/rom.bin.ME.bin" "$OUTPUT_DIR/me.bin"
chown "$USER:" "$OUTPUT_DIR/me.bin"
mv "$OUTPUT_DIR/rom.bin.GbE.bin" "$OUTPUT_DIR/gbe.bin"
chown "$USER:" "$OUTPUT_DIR/gbe.bin"
mv "$OUTPUT_DIR/rom.bin.Descriptor.bin" "$OUTPUT_DIR/descriptor.bin"
chown "$USER:" "$OUTPUT_DIR/descriptor.bin"
rm "$OUTPUT_DIR/rom.bin"
}
function extract_vgabios() {
local src="$1"
local pattern="$2"
$UEFI_EXTRACT "$src" dump
grep -rl "$pattern" "$(basename "$src.dump")" > vgabios.list
while IFS=$'\n' read -r p < vgabios.list
do
cp -f "$p" vgabios.bin
get_vgabios_name vgabios.bin
source vgabios_pci.name
rm vgabios_pci.name
mv vgabios.bin "$OUTPUT_DIR/$VGABIOS_NAME"
chown "$USER:" "$OUTPUT_DIR/$VGABIOS_NAME"
if [[ "$VGABIOS_NAME" == "$INTEL_VGABIOS_PATTERN"* ]] ; then
for id in "${INTEL_VGABIOS_DEVICE_ID_LIST[@]}"
do
cp -f "$OUTPUT_DIR/$VGABIOS_NAME" "$OUTPUT_DIR/$INTEL_VGABIOS_PATTERN$id.rom"
done
fi
sed -i '1d' vgabios.list
done
rm vgabios.list
rm -rf "$(basename "$src.dump")"
}
function extract_gop() {
local src="$1"
local gop_pattern="*$2*"
local vbt_pattern="*$3*"
$UEFI_EXTRACT "$src" dump
gop_root=$(find "$(basename "$src.dump")" -type d -name "$gop_pattern")
gop_file="$gop_root/0 PE32 image section/body.bin"
vbt_root=$(find "$(basename "$src.dump")" -type d -name "$vbt_pattern")
vbt_file="$vbt_root/0 Raw section/body.bin"
cp -f "$gop_file" "$OUTPUT_DIR/IntelGopDriver.efi"
cp -f "$vbt_file" "$OUTPUT_DIR/IntelGopVbt"
rm -rf "$(basename "$src.dump")"
}
if ( ! getopts "r:x:u:vh" opt); then
echo "Usage: $(basename "$0") options ( -r rom.bin ) ( -x output dir ) ( -u perms for user ) ( -v verbose ) -h for help";
exit $E_OPTERROR
fi
while getopts "r:x:u:vh" opt; do
case $opt in
v) export VERBOSE=1 ;;
r) export ROM_FILE="$OPTARG" ;;
x) export OUTPUT_DIR="$OPTARG" ;;
u) export USER="$OPTARG" ;;
esac
done
mkdir -p "$OUTPUT_DIR" || true
if is_new_x86_layout "$ROM_FILE" ; then
get_real_mac "$ROM_FILE"
extract_x86_blobs "$ROM_FILE"
extract_vgabios "$OUTPUT_DIR/uefi.bin" "$INTEL_NVIDIA_PATTERN"
extract_gop "$OUTPUT_DIR/uefi.bin" "$GOP_DRIVER_PATTERN" "$GOP_VBT_PATTERN"
fi