-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextract-files.sh
executable file
·144 lines (108 loc) · 3.75 KB
/
extract-files.sh
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
#!/bin/bash
set -e
EXTRACT_OTA=../../../prebuilts/extract-tools/linux-x86/bin/ota_extractor
MKDTBOIMG=../../../system/libufdt/utils/src/mkdtboimg.py
UNPACKBOOTIMG=../../../system/tools/mkbootimg/unpack_bootimg.py
ROM_ZIP=$1
error_handler() {
if [[ -d $extract_out ]]; then
echo "Error detected, cleaning temporal working directory $extract_out"
rm -rf $extract_out
fi
}
trap error_handler ERR
function usage() {
echo "Usage: ./extract-files.sh <rom-zip>"
exit 1
}
function get_path() {
echo "$extract_out/$1"
}
function unpackbootimg() {
$UNPACKBOOTIMG $@
}
function extract_ota() {
$EXTRACT_OTA $@
}
if [[ ! -f $UNPACKBOOTIMG ]]; then
echo "Missing $UNPACKBOOTIMG, are you on the correct directory?"
exit 1
fi
if [[ ! -f $EXTRACT_OTA ]]; then
echo "Missing $EXTRACT_OTA, are you on the correct directory and have built the ota_extractor target?"
exit 1
fi
if [[ -z $ROM_ZIP ]] || [[ ! -f $ROM_ZIP ]]; then
usage
fi
# Clean and create needed directories
for dir in ./modules/vendor_dlkm ./modules/system_dlkm ./modules/vendor_boot ./images ./images/dtbs; do
rm -rf $dir
mkdir -p $dir
done
# Extract the OTA package
extract_out=$(mktemp -d)
echo "Using $extract_out as working directory"
echo "Extracting the payload from $ROM_ZIP"
unzip $ROM_ZIP payload.bin -d $extract_out
echo "Extracting OTA images"
extract_ota -payload $extract_out/payload.bin -output_dir $extract_out -partitions boot,dtbo,vendor_boot,vendor_dlkm,system_dlkm
# BOOT
echo "Extracting the kernel image from boot.img"
out=$extract_out/boot-out
mkdir $out
echo "Extracting at $out"
unpackbootimg --boot_img $(get_path boot.img) --out $out --format mkbootimg
echo "Done. Copying the kernel"
cp $out/kernel ./images/kernel
echo "Done"
# VENDOR_BOOT
echo "Extracting the ramdisk kernel modules and DTB"
out=$extract_out/vendor_boot-out
mkdir $out
echo "Extracting at $out"
unpackbootimg --boot_img $(get_path vendor_boot.img) --out $out --format mkbootimg
echo "Done. Extracting the ramdisk"
mkdir $out/ramdisk
unlz4 $out/vendor_ramdisk00 $out/vendor_ramdisk
cpio -i -F $out/vendor_ramdisk -D $out/ramdisk
echo "Copying all ramdisk modules"
for module in $(find $out/ramdisk -name "*.ko" -o -name "modules.load*" -o -name "modules.blocklist"); do
cp $module ./modules/vendor_boot/
done
# VENDOR_DLKM
echo "Extracting the dlkm kernel modules"
out=$extract_out/vendor_dlkm
echo "Extracting at $out"
fsck.erofs --extract="$out" $(get_path vendor_dlkm.img)
echo "Done. Extracting the vendor dlkm"
echo "Copying all vendor dlkm modules"
for module in $(find $out/lib -name "*.ko" -o -name "modules.load*" -o -name "modules.blocklist"); do
cp $module ./modules/vendor_dlkm/
done
# SYSTEM_DLKM
echo "Extracting the dlkm kernel modules"
out=$extract_out/system_dlkm
echo "Extracting at $out"
fsck.erofs --extract="$out" $(get_path system_dlkm.img)
echo "Done. Extracting the system dlkm"
echo "Copying all system dlkm modules"
cp -r $out/lib/modules/6.1* ./modules/system_dlkm/
# Extract DTBO and DTBs
echo "Extracting DTBO and DTBs"
curl -sSL "https://raw.githubusercontent.com/PabloCastellano/extract-dtb/master/extract_dtb/extract_dtb.py" > ${extract_out}/extract_dtb.py
# Copy DTB
python3 "${extract_out}/extract_dtb.py" "${extract_out}/vendor_boot-out/dtb" -o "${extract_out}/dtbs" > /dev/null
find "${extract_out}/dtbs" -type f -name "*.dtb" \
-exec cp {} ./images/dtbs/ \; \
-exec printf " - dtbs/" \; \
-exec basename {} \;
cp -f "${extract_out}/dtbo.img" ./images/dtbo.img
echo "Done"
# Add touch modules to vendorboot for recovery
for module in xiaomi_touch.ko goodix_core.ko focaltech_touch.ko; do
cp modules/vendor_dlkm/$module modules/vendor_boot/
echo $module >> modules/vendor_boot/modules.load.recovery
done
rm -rf $extract_out
echo "Extracted files successfully"