-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathzip2img.sh
executable file
·428 lines (407 loc) · 18.7 KB
/
zip2img.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env bash
# Supported Firmwares:
# Aonly OTA
# Raw image
# tarmd5
# chunk image
# AB OTA
# Image zip
# ozip
# Sony ftf
# ZTE update.zip
# KDDI .bin
# bin images
# pac
# sign images
# sign auth DA images
# nb0
# kdz
# RUU
superimage() {
if [ -f super.img ]; then
echo "-> Creating super.img.raw ..."
$simg2img super.img super.img.raw >> /dev/null 2>&1
fi
if [[ ! -s super.img.raw ]] && [ -f super.img ]; then
mv super.img super.img.raw
fi
for partition in $PARTITIONS; do
($lpunpack --partition="$partition"_a super.img.raw || $lpunpack --partition="$partition" super.img.raw) 2>/dev/null
if [ -f "$partition"_a.img ]; then
mv "$partition"_a.img "$partition".img
else
foundpartitions=$(7z l -ba "$romzip" | rev | gawk '{ print $1 }' | rev | grep $partition.img)
7z e -y "$romzip" $foundpartitions dummypartition 2>/dev/null >> $tmpdir/zip.log
fi
done
rm -rf super.img.raw
}
usage() {
echo "Usage: $0 <Path to firmware> [Output Dir]"
echo -e "\tPath to firmware: the zip!"
echo -e "\tOutput Dir: the output dir!"
}
if [ "$1" == "" ]; then
echo "BRUH: Enter all needed parameters"
usage
exit 1
fi
LOCALDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
HOST="$(uname)"
toolsdir="$LOCALDIR/tools"
if [[ ! -d "$toolsdir/oppo_ozip_decrypt" ]]; then
git clone -q https://github.com/bkerler/oppo_ozip_decrypt.git "$toolsdir/oppo_ozip_decrypt" > /dev/null 2>&1
else
git -C "$toolsdir/oppo_ozip_decrypt" pull > /dev/null 2>&1
fi
simg2img="$toolsdir/$HOST/bin/simg2img"
unsin="$toolsdir/$HOST/bin/unsin"
payload="$toolsdir/$HOST/bin/payload-dumper-go"
payload_extractor="$toolsdir/update_payload_extractor/extract.py"
sdat2img="$toolsdir/sdat2img.py"
ozipdecrypt="$toolsdir/oppo_ozip_decrypt/ozipdecrypt.py"
lpunpack="$toolsdir/$HOST/bin/lpunpack"
splituapp="$toolsdir/splituapp"
pacextractor="$toolsdir/$HOST/bin/pacextractor"
nb0_extract="$toolsdir/$HOST/bin/nb0-extract"
kdz_extract="$toolsdir/kdztools/unkdz.py"
dz_extract="$toolsdir/kdztools/undz.py"
ruu="$toolsdir/$HOST/bin/RUU_Decrypt_Tool"
brotli_exec="$toolsdir/$HOST/bin/brotli"
romzip="$(realpath $1)"
romzipext="${romzip##*.}"
PARTITIONS="system vendor cust odm oem factory product xrom systemex oppo_product preload_common system_ext system_other opproduct reserve india my_carrier my_custom my_engineering my_preload my_manifest my_region my_odm my_stock my_version my_operator my_country my_product my_company my_heytap"
EXT4PARTITIONS="system vendor cust odm oem factory product xrom systemex oppo_product preload_common"
OTHERPARTITIONS="super_3.img:product super_5.img:odm super_2.img:system"
echo "-> Create Temp and out dir"
outdir="$LOCALDIR/out"
if [ ! "$2" == "" ]; then
outdir="$(realpath $2)"
fi
tmpdir="$outdir/tmp"
mkdir -p "$tmpdir"
mkdir -p "$outdir"
cd $tmpdir
MAGIC=$(head -c12 "$romzip" | tr -d '\0')
if [[ $MAGIC == "OPPOENCRYPT!" ]] || [[ "$romzipext" == "ozip" ]]; then
echo "-> ozip detected"
cp "$romzip" "$tmpdir/temp.ozip"
python3 $ozipdecrypt "$tmpdir/temp.ozip" >> $tmpdir/ozip.log 2>&1
if [[ -d "$tmpdir/out" ]]; then
7z a -r "$tmpdir/temp.zip" "$tmpdir/out/*" >> $tmpdir/zip.log 2>&1
fi
"$LOCALDIR/zip2img.sh" "$tmpdir/temp.zip" "$outdir"
exit
fi
if [[ $(echo "$romzip" | grep kdz) ]]; then
echo "-> KDZ detected"
python $kdz_extract -f "$romzip" -x -o "./"
dzfile=`ls -l | grep ".*.dz" | gawk '{ print $9 }'`
python3 $dz_extract -f $dzfile -s -o "./"
# Some known dz-partitions "gpt_main persist misc metadata vendor system system_other product userdata gpt_backup tz boot dtbo vbmeta cust oem odm factory modem NON-HLOS"
find . -maxdepth 1 -type f -name "*.image" | rename 's/.image/.img/g' > /dev/null 2>&1
find . -maxdepth 1 -type f -name "*_a.img" | rename 's/_a.img/.img/g' > /dev/null 2>&1
for partition in $PARTITIONS; do
[[ -e "$tmpdir/$partition.img" ]] && mv "$tmpdir/$partition.img" "$outdir/$partition.img"
done
rm -rf $tmpdir
exit 0
fi
if [[ $(echo "$romzip" | grep -i ruu_ | grep -i exe) ]]; then
echo "-> RUU detected"
cp "$romzip" $tmpdir
romzip="$tmpdir/$(basename $romzip)"
$ruu -s "$romzip" 2>/dev/null
$ruu -f "$romzip" 2>/dev/null
find "$tmpdir/OUT"* -name *.img -exec mv {} $tmpdir \;
for partition in $PARTITIONS; do
[[ -e "$tmpdir/$partition.img" ]] && mv "$tmpdir/$partition.img" "$outdir/$partition.img"
done
rm -rf $tmpdir
exit 0
fi
if [[ ! $(7z l -ba "$romzip" | grep ".*system.ext4.tar.*\|.*.tar\|.*chunk\|system\/build.prop\|system.new.dat\|system_new.img\|system.img\|system-sign.img\|system.bin\|payload.bin\|.*.zip\|.*.rar\|.*rawprogram*\|system.sin\|.*system_.*\.sin\|system-p\|super\|UPDATE.APP\|.*.pac\|.*.nb0" | grep -v ".*chunk.*\.so$") ]]; then
echo "-> BRUH: This type of firmwares not supported"
cd "$LOCALDIR"
rm -rf "$tmpdir" "$outdir"
exit 1
fi
echo "-> Extracting firmware on: $outdir"
for otherpartition in $OTHERPARTITIONS; do
filename=$(echo $otherpartition | cut -f 1 -d ":")
outname=$(echo $otherpartition | cut -f 2 -d ":")
if [[ $(7z l -ba "$romzip" | grep $filename) ]]; then
echo "-> $filename detected for $outname"
foundfiles=$(7z l -ba "$romzip" | gawk '{ print $NF }' | grep $filename)
7z e -y "$romzip" $foundfiles 2>/dev/null >> $tmpdir/zip.log
outputs=$(ls *"$filename"*)
for output in $outputs; do
[[ ! -e "$outname".img ]] && mv $output "$outname".img
$simg2img "$outname".img "$outdir/$outname".img >> /dev/null 2>&1
if [[ ! -s "$outdir/$outname".img ]] && [ -f "$outname".img ]; then
mv "$outname".img "$outdir/$outname".img
fi
done
fi
done
if [[ $(7z l -ba "$romzip" | grep firmware-update/dtbo.img) ]]; then
7z e -y "$romzip" firmware-update/dtbo.img 2>/dev/null >> $tmpdir/zip.log
fi
if [[ $(7z l -ba "$romzip" | grep system.new.dat) ]]; then
echo "-> Aonly OTA detected"
for partition in $PARTITIONS; do
7z e -y "$romzip" $partition.new.dat* $partition.transfer.list $partition.img $partition.00011011.new.dat* $partition.00011011.transfer.list $partition.00011011.img 2>/dev/null >> $tmpdir/zip.log
if [[ -f $partition.new.dat.1 ]]; then
cat $partition.new.dat.{0..999} 2>/dev/null >> $partition.new.dat
rm -rf $partition.new.dat.{0..999}
fi
find "$tmpdir" -maxdepth 1 -type f -name "*.00011011*" | rename 's/.00011011//g' > /dev/null 2>&1
ls | grep "\.new\.dat" | while read i; do
line=$(echo "$i" | cut -d"." -f1)
if [[ $(echo "$i" | grep "\.dat\.xz") ]]; then
7z e -y "$i" 2>/dev/null >> $tmpdir/zip.log
rm -rf "$i"
fi
if [[ $(echo "$i" | grep "\.dat\.br") ]]; then
echo "-> Converting brotli $partition dat to normal"
$brotli_exec -d "$i"
rm -f "$i"
fi
echo "-> Extracting $partition"
python3 $sdat2img $line.transfer.list $line.new.dat "$outdir"/$line.img > $tmpdir/extract.log
rm -rf $line.transfer.list $line.new.dat
done
done
elif [[ $(7z l -ba "$romzip" | grep nb0) ]]; then
echo "-> nb0 type detected"
to_extract=`7z l "$romzip" | grep ".*.nb0" | gawk '{ print $6 }'`
echo $to_extract
7z e -y "$romzip" $to_extract 2>/dev/null >> $tmpdir/zip.log
$nb0_extract $to_extract $tmpdir
for partition in $PARTITIONS; do
part=`ls -l | grep ".*$partition.img" | gawk '{ print $9 }'`
mv $part $partition.img
done
romzip=""
elif [[ $(7z l -ba "$romzip" | grep system | grep chunk | grep -v ".*\.so$") ]]; then
echo "-> Chunk detected"
for partition in $PARTITIONS; do
foundpartitions=$(7z l -ba "$romzip" | gawk '{ print $NF }' | grep $partition.img)
7z e -y "$romzip" *$partition*chunk* */*$partition*chunk* $foundpartitions dummypartition 2>/dev/null >> $tmpdir/zip.log
rm -f *"$partition"_b*
rm -f *"$partition"_other*
romchunk=$(ls | grep chunk | grep $partition | sort)
if [[ $(echo "$romchunk" | grep "sparsechunk") ]]; then
$simg2img $(echo "$romchunk" | tr '\n' ' ') $partition.img.raw >> /dev/null 2>&1
rm -rf *$partition*chunk*
if [[ -f $partition.img ]]; then
rm -rf $partition.img.raw
else
mv $partition.img.raw $partition.img
fi
fi
done
elif [[ $(7z l -ba "$romzip" | gawk '{print $NF}' | grep "system_new.img\|^system.img\|\/system.img\|\/system_image.emmc.img\|^system_image.emmc.img") ]]; then
echo "-> Image detected"
7z x -y "$romzip" 2>/dev/null >> $tmpdir/zip.log
find $tmpdir/ -name "* *" -type d,f | rename 's/ /_/g' > /dev/null 2>&1 # removes space from file name
find $tmpdir/ -mindepth 2 -type f -name "*_image.emmc.img" -exec mv {} . \; # move .img in sub-dir to $tmpdir
find $tmpdir/ -mindepth 2 -type f -name "*_new.img" -exec mv {} . \; # move .img in sub-dir to $tmpdir
find $tmpdir/ -maxdepth 1 -type f -name "*.img.gz" -exec gzip -d {} \; # unzip gzip
find $tmpdir/ -mindepth 2 -type f -name "*.img.ext4" -exec mv {} . \; # move .img in sub-dir to $tmpdir
find $tmpdir/ -mindepth 2 -type f -name "*.img" -exec mv {} . \; # move .img in sub-dir to $tmpdir
find $tmpdir/ -type f ! -name "*img*" -exec rm -rf {} \; # delete other files
find "$tmpdir" -maxdepth 1 -type f -name "*_image.emmc.img" | rename 's/_image.emmc.img/.img/g' > /dev/null 2>&1 # proper .img names
find "$tmpdir" -maxdepth 1 -type f -name "*_new.img" | rename 's/_new.img/.img/g' > /dev/null 2>&1 # proper .img names
find "$tmpdir" -maxdepth 1 -type f -name "*.img.ext4" | rename 's/.img.ext4/.img/g' > /dev/null 2>&1 # proper .img names
romzip=""
elif [[ $(7z l -ba "$romzip" | grep "system.sin\|.*system_.*\.sin") ]]; then
echo "-> Sin type detected"
to_remove=`7z l "$romzip" | grep ".*boot_.*\.sin" | gawk '{ print $6 }' | sed -e 's/boot_\(.*\).sin/\1/'`
if [ -z "$to_remove" ]
then
to_remove=`7z l "$romzip" | grep ".*cache_.*\.sin" | gawk '{ print $6 }' | sed -e 's/cache_\(.*\).sin/\1/'`
fi
if [ -z "$to_remove" ]
then
to_remove=`7z l "$romzip" | grep ".*vendor_.*\.sin" | gawk '{ print $6 }' | sed -e 's/vendor_\(.*\).sin/\1/'`
fi
7z x -y "$romzip" 2>/dev/null >> $tmpdir/zip.log
find $tmpdir/ -mindepth 2 -type f -name "*.sin" -exec mv {} . \; # move .img in sub-dir to $tmpdir
find "$tmpdir" -maxdepth 1 -type f -name "*_$to_remove.sin" | rename 's/_'$to_remove'.sin/.sin/g' > /dev/null 2>&1 # proper names
$unsin -d $tmpdir
find "$tmpdir" -maxdepth 1 -type f -name "*.ext4" | rename 's/.ext4/.img/g' > /dev/null 2>&1 # proper names
romzip=""
elif [[ $(7z l -ba "$romzip" | grep ".pac$") ]]; then
echo "-> Pac detected"
7z x -y "$romzip" 2>/dev/null >> $tmpdir/zip.log
find $tmpdir/ -name "* *" -type d,f | rename 's/ /_/g' > /dev/null 2>&1
pac_list=`find $tmpdir/ -type f -name "*.pac" -printf '%P\n' | sort`
for file in $pac_list; do
$pacextractor $file 2>/dev/null >> $tmpdir/pacextractor.log
done
elif [[ $(7z l -ba "$romzip" | grep "system.bin") ]]; then
echo "-> Bin images detected"
7z x -y "$romzip" 2>/dev/null >> $tmpdir/zip.log
find $tmpdir/ -mindepth 2 -type f -name "*.bin" -exec mv {} . \; # move .img in sub-dir to $tmpdir
find "$tmpdir" -maxdepth 1 -type f -name "*.bin" | rename 's/.bin/.img/g' > /dev/null 2>&1 # proper names
romzip=""
elif [[ $(7z l -ba "$romzip" | grep "system-p") ]]; then
echo "-> P suffix images detected"
for partition in $PARTITIONS; do
foundpartitions=$(7z l -ba "$romzip" | gawk '{ print $NF }' | grep $partition-p)
7z e -y "$romzip" $foundpartitions dummypartition 2>/dev/null >> $tmpdir/zip.log
if [ ! -z "$foundpartitions" ]; then
mv $(ls $partition-p*) "$partition.img"
fi
done
elif [[ $(7z l -ba "$romzip" | grep "system-sign.img") ]]; then
echo "-> Sign images detected"
7z x -y "$romzip" 2>/dev/null >> $tmpdir/zip.log
for partition in $PARTITIONS; do
[[ -e "$tmpdir/$partition.img" ]] && mv "$tmpdir/$partition.img" "$outdir/$partition.img"
done
find $tmpdir/ -name "* *" -type d,f | rename 's/ /_/g' > /dev/null 2>&1 # removes space from file name
find $tmpdir/ -mindepth 2 -type f -name "*-sign.img" -exec mv {} . \; # move .img in sub-dir to $tmpdir
find $tmpdir/ -type f ! -name "*-sign.img" -exec rm -rf {} \; # delete other files
find "$tmpdir" -maxdepth 1 -type f -name "*-sign.img" | rename 's/-sign.img/.img/g' > /dev/null 2>&1 # proper .img names
sign_list=`find "$tmpdir" -maxdepth 1 -type f -name "*.img" -printf '%P\n' | sort`
for file in $sign_list; do
rm -rf "$tmpdir/x.img"
MAGIC=$(head -c4 "$tmpdir/$file" | tr -d '\0')
if [[ $MAGIC == "SSSS" ]]
then
echo Cleaning $file with SSSS header
# This is for little_endian arch
offset_low=$(od -A n -x -j 60 -N 2 "$tmpdir/$file" | sed 's/ //g')
offset_high=$(od -A n -x -j 62 -N 2 "$tmpdir/$file" | sed 's/ //g')
offset_low=0x${offset_low:0-4}
offset_high=0x${offset_high:0-4}
offset_low=$(printf "%d" $offset_low)
offset_high=$(printf "%d" $offset_high)
offset=$((65536*${offset_high}+${offset_low}))
dd if="$tmpdir/$file" of="$tmpdir/x.img" iflag=count_bytes,skip_bytes bs=8192 skip=64 count=$offset > /dev/null 2>&1
else # header with BFBF magic or another unknowed header
dd if="$tmpdir/$file" of="$tmpdir/x.img" bs=$((0x4040)) skip=1 > /dev/null 2>&1
fi
MAGIC=$(od -A n -X -j 0 -N 4 "$tmpdir/x.img" | sed 's/ //g')
if [[ $MAGIC == "ed26ff3a" ]]; then
$simg2img "$tmpdir/x.img" "$tmpdir/$file" >> /dev/null 2>&1
else
mv "$tmpdir/x.img" "$tmpdir/$file"
fi
done
romzip=""
elif [[ $(7z l -ba "$romzip" | grep "super.img") ]]; then
echo "-> Super detected"
foundsupers=$(7z l -ba "$romzip" | gawk '{ print $NF }' | grep "super.img")
7z e -y "$romzip" $foundsupers dummypartition 2>/dev/null >> $tmpdir/zip.log
superchunk=$(ls | grep chunk | grep super | sort)
if [[ $(echo "$superchunk" | grep "sparsechunk") ]]; then
$simg2img $(echo "$superchunk" | tr '\n' ' ') super.img.raw >> /dev/null 2>&1
rm -rf *super*chunk*
fi
superimage
elif [[ $(7z l -ba "$romzip" | grep tar.md5 | gawk '{ print $NF }' | grep AP_) ]]; then
echo "-> AP tarmd5 detected"
echo "-> Extracting tarmd5"
7z e -y "$romzip" 2>/dev/null >> $tmpdir/zip.log
echo "-> Extracting images..."
for i in $(ls *.tar.md5); do
tar -xf $i || exit 1
rm -rf $i || exit 1
done
for f in $(ls *.lz4); do
lz4 -dc $f > ${f/.lz4/} || exit 1
rm -rf $f || exit 1
done
if [[ -f super.img ]]; then
superimage
fi
if [[ -f system.img.ext4 ]]; then
find "$tmpdir" -maxdepth 1 -type f -name "*.img.ext4" | rename 's/.img.ext4/.img/g' > /dev/null 2>&1
fi
if [[ ! -f system.img ]]; then
echo "-> Extract failed"
rm -rf "$tmpdir"
exit 1
fi
romzip=""
elif [[ $(7z l -ba "$romzip" | grep .tar) && ! $(7z l -ba "$romzip" | grep tar.md5 | gawk '{ print $NF }' | grep AP_) ]]; then
tar=$(7z l -ba "$romzip" | grep .tar | gawk '{ print $NF }')
echo "-> non AP tar detected"
7z e -y "$romzip" $tar 2>/dev/null >> $tmpdir/zip.log
"$LOCALDIR/zip2img.sh" $tar "$outdir"
exit
elif [[ $(7z l -ba "$romzip" | grep payload.bin) ]]; then
echo "-> AB OTA detected"
7z e -y "$romzip" payload.bin 2>/dev/null >> $tmpdir/zip.log
$payload -o $tmpdir payload.bin 2> $tmpdir/payload-dumper-go.log >> $tmpdir/payload-dumper-go.log
for partition in $PARTITIONS; do
[[ -e "$tmpdir/$partition.img" ]] && mv "$tmpdir/$partition.img" "$outdir/$partition.img"
done
rm payload.bin
rm -rf "$tmpdir"
exit
elif [[ $(7z l -ba "$romzip" | grep ".*.rar\|.*.zip") ]]; then
echo "-> Image zip firmware detected"
mkdir -p $tmpdir/zipfiles
7z e -y "$romzip" -o$tmpdir/zipfiles 2>/dev/null >> $tmpdir/zip.log
find $tmpdir/zipfiles -name "* *" -type d,f | rename 's/ /_/g' > /dev/null 2>&1
zip_list=`find $tmpdir/zipfiles -type f -size +300M \( -name "*.rar*" -o -name "*.zip*" \) -printf '%P\n' | sort`
for file in $zip_list; do
"$LOCALDIR/zip2img.sh" $tmpdir/zipfiles/$file "$outdir"
done
exit
elif [[ $(7z l -ba "$romzip" | grep "UPDATE.APP") ]]; then
echo "-> Huawei UPDATE.APP detected"
7z x "$romzip" UPDATE.APP
python3 $splituapp -f "UPDATE.APP" -l super || (
for partition in $PARTITIONS; do
python3 $splituapp -f "UPDATE.APP" -l ${partition/.img/} || echo "$partition not found in UPDATE.APP"
done)
if [ -f super.img ]; then
($simg2img super.img super.img.raw || mv super.img super.img.raw) 2>/dev/null
for partition in $PARTITIONS; do
($lpunpack --partition="$partition"_a super.img.raw || $lpunpack --partition="$partition" super.img.raw) 2>/dev/null
if [ -f "$partition"_a.img ]; then
mv "$partition"_a.img "$partition".img
else
foundpartitions=$(7z l -ba "$romzip" | gawk '{ print $NF }' | grep $partition.img)
7z e -y "$romzip" $foundpartitions dummypartition 2>/dev/null >> $tmpdir/zip.log
fi
done
rm -rf super.img.raw
fi
fi
for partition in $PARTITIONS; do
if [ -f $partition.img ]; then
$simg2img $partition.img "$outdir"/$partition.img >> /dev/null 2>&1
fi
if [[ ! -s "$outdir"/$partition.img ]] && [ -f $partition.img ]; then
mv $partition.img "$outdir"/$partition.img
fi
if [[ $EXT4PARTITIONS =~ (^|[[:space:]])"$partition"($|[[:space:]]) ]] && [ -f "$outdir"/$partition.img ]; then
MAGIC=$(head -c12 "$outdir"/$partition.img | tr -d '\0')
offset=$(LANG=C grep -aobP -m1 '\x53\xEF' "$outdir"/$partition.img | head -1 | gawk '{print $1 - 1080}')
if [[ $(echo "$MAGIC" | grep "MOTO") ]]; then
if [[ "$offset" == 128055 ]]; then
offset=131072
fi
echo "-> Moto header detected on $partition in $offset"
elif [[ $(echo "$MAGIC" | grep "ASUS") ]]; then
echo "-> Asus header detected on $partition in $offset"
else
offset=0
fi
if [ ! $offset == "0" ]; then
dd if="$outdir"/$partition.img of="$outdir"/$partition.img-2 ibs=$offset skip=1 2>/dev/null
mv "$outdir"/$partition.img-2 "$outdir"/$partition.img
fi
fi
if [ ! -s "$outdir"/$partition.img ] && [ -f "$outdir"/$partition.img ]; then
rm "$outdir"/$partition.img
fi
done
cd "$LOCALDIR"
rm -rf "$tmpdir"