-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
extract_images.sh
176 lines (143 loc) · 6.04 KB
/
extract_images.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# Using util_functions.sh
[ -f "util_functions.sh" ] && . ./util_functions.sh || { echo "util_functions.sh not found" && exit 1; }
# Extracted image paths
EI="./extracted_images"
EAI="./extracted_archive_images"
EI_BP="${EI##"./"}"
EAI_BP="${EAI##"./"}"
# Extract payload as ota or system image if factory
[ -d "dl" ] && print_message "Extracting images from the Android OTA update package or factory image…\n" info
for file in ./dl/*; do # Iterate through files in the directory ./dl/*
if [ -f "${file:?}" ] && [ "${file: -4}" == ".zip" ]; then # Check if it is a ZIP file
filename="${file##*/}" # Extract the filename (remove the path)
basename="${filename%.*}" # Extract the basename (remove the extension)
# devicename="${basename%%-*}" # Extract the device name (remove the extension)
print_message "Processing \"$filename\"…" info
# Time the extraction
extraction_start=$(date +%s)
# Extract images
if unzip -l "$file" | grep -q "payload.bin"; then
print_message "Extracting OTA image…" debug
# Skip image if it failed to get extracted
if ! 7z e "$file" -o"$EAI" "payload.bin" -r &>/dev/null; then
print_message "Failed to extract payload.bin from $file using 7z. Skipping…\n" warning
continue
fi
# If it managed to dump the payload then move it correspondingly.
mv -f "$EAI_BP/payload.bin" "$EAI/$basename.bin"
print_message "Saved to \"$EAI/$basename.bin\"." debug
else
print_message "Detected Factory image, Extracting everything…" debug
# Skip image if it failed to get extracted
if ! 7z e "$file" -o"$EAI_BP" -r -y &>/dev/null; then
print_message "Failed to extract everything from $file. Skipping…" warning
continue
fi
fi
# We dont need the archive anymore
# rm "$file"
# Time the extraction
extraction_end=$(date +%s)
extraction_runtime=$((extraction_end - extraction_start))
# Print the time
print_message "Extraction time: $extraction_runtime seconds\n" debug
fi
done
if [ -d "$EAI_BP" ]; then
if [ -n "$(ls -A "$EAI_BP"/*.{zip,bin} 2>/dev/null)" ]; then
print_message "Dumping images from \"$EAI_BP\"…\n" info
for file in "$EAI"/*.{zip,bin}; do # List directory zip & bin files
if [ -f "${file:?}" ] && [[ "$file" == *.zip || "$file" == *.bin ]]; then # Check if file is a zip or bin file
filename="${file##*/}" # Remove the path
basename="${filename%.*}" # Remove the extension
print_message "Processing \"$filename\"…" info
# Time the extraction
extraction_start=$(date +%s)
# Extract/Dump
if [ "${file: -4}" == ".bin" ]; then # If is payload use the Android OTA Dumper
# Format the patitions to dump for argument usage
partitionsArgs=$(
IFS=,
echo "${PARTITIONS2EXTRACT[*]}"
)
# Skip image if it failed to get extracted
if ! payload_dumper "$file" --partitions="$partitionsArgs" --out="$EI_BP/$basename" 2>/dev/null; then
print_message "Failed to extract $file using Android OTA Dumper. Skipping…\n" warning
rm -rf "$EI_BP/$basename" # TODO: Use "${var:?}" to ensure this never expands to / .
continue
fi
else # Else directly extract all the required image using 7z
for image_name in "${PARTITIONS2EXTRACT[@]}"; do
print_message "Extracting \"$image_name\"…" debug
# Skip image if it failed to get extracted
if ! 7z e "$file" -o"$EI_BP/$basename" "$image_name.img" -r &>/dev/null; then
print_message "Failed to extract $image_name.img from $file using 7z. Skipping…\n" warning
rm -rf "$EI_BP/$basename/$image_name.img"
continue
fi
done
fi
# We dont need the image anymore
# rm "$file"
# Time the extraction
extraction_end=$(date +%s)
extraction_runtime=$((extraction_end - extraction_start))
# Print the time
print_message "Extraction time: $extraction_runtime seconds\n" debug
fi
done
else
print_message "The directory \"$EAI_BP\" does not have any ZIP or BIN files.\n" error
fi
fi
# Extract the images directories
[ -d "$EI" ] && print_message "Extracting images…\n" info
for dir in "$EI"/*; do # List directory ./*
if [ -d "$dir" ]; then # Check if it is a directory
dir=${dir%*/} # Remove last /
print_message "Processing \"${dir##*/}\"…" info
# Time the extraction
extraction_start=$(date +%s)
# Extract all and clean
for image_name in "${PARTITIONS2EXTRACT[@]}"; do
if [ -f "$dir/$image_name.img" ]; then
extract_image "$dir" "$image_name"
rm "$dir/$image_name.img"
fi
done
# Time the extraction
extraction_end=$(date +%s)
extraction_runtime=$((extraction_end - extraction_start))
# Print the extraction time
print_message "Extraction time: $extraction_runtime seconds\n" debug
fi
done
# Build props, feature and module files after extraction
[ -d "$EI" ] && print_message "Building module props and features…\n" info
for dir in "$EI"/*; do # List directory ./*
if [ -d "$dir" ]; then # Check if it is a directory
dir=${dir%*/} # Remove last /
print_message "Processing \"${dir##*/}\"…" info
# Time the extraction
extraction_start=$(date +%s)
# Build system.prop
print_message "Building props…" info
./build_props.sh "$dir"
# Build product sysconfig for Pixel Experience features
print_message "Building sysconfig features…" info
./build_sysconfig.sh "$dir"
# Build media bootanimation
# (Optional) It increases the module size significantly!
# print_message "Building media bootanimation…" info
# ./build_bootanimation.sh "$dir"
# Build Magisk module
print_message "Building module…" info
./build_magisk_module.sh "$dir"
# Time the extraction
extraction_end=$(date +%s)
extraction_runtime=$((extraction_end - extraction_start))
# Print the build time
print_message "Build time: $extraction_runtime seconds\n" debug
fi
done