-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
makeimg.sh
executable file
·228 lines (203 loc) · 9.54 KB
/
makeimg.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
#!/bin/bash
# Copyright (C) 2020 Xiaoxindada <2245062854@qq.com>
LOCALDIR=`cd "$( dirname ${BASH_SOURCE[0]} )" && pwd`
cd $LOCALDIR
source $LOCALDIR/bin.sh
source $TOOLDIR/language_helper.sh
Usage() {
cat <<EOT
Usage:
$0 <os_repackage_type>
os_repackage_type: System.img repack type: [AB|ab or A|a]
[AB_CONFIG|ab_config]: Use fs_config to repack ab system.img
[A_CONFIG|a_config]: Use fs_config to repack a-only system.img
EOT
}
os_repackage_type=$1
name=$2
mkdir -p $OUTDIR
case $os_repackage_type in
"A"|"a"|"A_CONFIG"|"a_config")
systemdir="$TARGETDIR/system/system"
system="$systemdir"
;;
"AB"|"ab"|"AB_CONFIG"|"ab_config")
systemdir="$TARGETDIR/system"
system="$systemdir/system"
;;
"-h"|"--help")
Usage
exit 1
;;
*)
Usage
exit 1
;;
esac
configdir="$TARGETDIR/config"
target_contexts="system_test_contexts"
case $os_repackage_type in
"A_CONFIG"|"a_config")
echo "/ u:object_r:system_file:s0" > $configdir/system_A_contexts
echo "/system u:object_r:system_file:s0" >> $configdir/system_A_contexts
echo "/system(/.*)? u:object_r:system_file:s0" >> $configdir/system_A_contexts
echo "/system/lost+found u:object_r:system_file:s0" >> $configdir/system_A_contexts
echo "/ 0 0 0755" > $configdir/system_A_fs
echo "system 0 0 0755" >> $configdir/system_A_fs
echo "system/lost+found 0 0 0700" >> $configdir/system_A_fs
cat $configdir/system_file_contexts | grep "system_ext" >> $configdir/system_ext_contexts
cat $configdir/system_fs_config | grep "system_ext" >> $configdir/system_ext_fs
cat $configdir/system_file_contexts | grep "/system/system/" >> $configdir/system_A_contexts
cat $configdir/system_fs_config | grep "system/system/" >> $configdir/system_A_fs
sed -i 's#/system/system/system_ext#/system/system_ext#' $configdir/system_ext_contexts
sed -i 's#system/system/system_ext#system/system_ext#' $configdir/system_ext_fs
sed -i 's#/system/system#/system#' $configdir/system_A_contexts
sed -i 's#system/system#system#' $configdir/system_A_fs
cat $configdir/system_ext_contexts >> $configdir/system_A_contexts
cat $configdir/system_ext_fs >> $configdir/system_A_fs
;;
esac
# Generate debloated file_contexts
file_contexts() {
rm -rf $configdir/$target_contexts
mkdir -p $configdir
cat $system/etc/selinux/plat_file_contexts >> $configdir/$target_contexts
partition_name="system_ext product vendor"
for partition in $partition_name ;do
if [ -d $systemdir/$partition/etc/selinux ];then
file_contexts=$(ls $system/$partition/etc/selinux | grep file_contexts*)
#echo $system/$partition/etc/selinux/$file_contexts
[ -z $(cat $system/$partition/etc/selinux/$file_contexts) ] && continue
cat $system/$partition/etc/selinux/$file_contexts >> $configdir/$target_contexts
fi
done
}
file_contexts
case $os_repackage_type in
"A"|"a"|"AB"|"ab"|"AB_CONFIG"|"ab_config")
if [[ -f $configdir/$target_contexts ]]; then
echo "/firmware(/.*)? u:object_r:firmware_file:s0" >> $configdir/$target_contexts
echo "/bt_firmware(/.*)? u:object_r:bt_firmware_file:s0" >> $configdir/$target_contexts
echo "/persist(/.*)? u:object_r:mnt_vendor_file:s0" >> $configdir/$target_contexts
echo "/dsp u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/oem u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/op1 u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/op2 u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/charger_log u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/audit_filter_table u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/keydata u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/keyrefuge u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/omr u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/publiccert.pem u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/sepolicy_version u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/cust u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/donuts_key u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/v_key u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/carrier u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/dqmdbg u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/ADF u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/APD u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/asdf u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/batinfo u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/voucher u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/xrom u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/custom u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/cpefs u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/modem u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/module_hashes u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/pds u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/tombstones u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/factory u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/oneplus(/.*)? u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/addon.d u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/op_odm u:object_r:rootfs:s0" >> $configdir/$target_contexts
echo "/avb u:object_r:rootfs:s0" >> $configdir/$target_contexts
fi
;;
esac
if [ ! -d $systemdir ];then
echo $SYSTEMDIR_NF
exit
fi
# Merge FS DATA
cd $MAKEDIR
./add_apex_fs.sh > /dev/null 2>&1 || { echo "> Failed to add apex contexts!" ; exit 1; }
./add_repack_fs.sh > /dev/null 2>&1 || { echo "> Failed to add repack contexts!" ; exit 1; }
cd $LOCALDIR
# Codename
codename=$(grep -oP "(?<=^ro.product.vendor.device=).*" -hs "$TARGETDIR/vendor/build.prop" | head -1)
[[ -z "${codename}" ]] && codename=$(grep -oP "(?<=^ro.product.system.device=).*" -hs $system/build.prop | head -1)
[[ -z "${codename}" ]] && codename=$(grep -oP "(?<=^ro.product.device=).*" -hs $system/build.prop | head -1)
[[ -z "${codename}" ]] && codename=Generic
#Out Variable
date=`date +%Y%m%d`
if [ $(cat $system/build.prop | grep "ro.build.version.sdk" | head -n 1 | cut -d "=" -f 2) = "32" ];then
outputname="$name-12.1-$date-$codename-SGSI137"
ioutputname="$name-AB-12.1-$date-$codename-SGSI137"
else
outputname="$name-12-$date-$codename-SGSI137"
ioutputname="$name-AB-12-$date-$codename-SGSI137"
fi
outputimagename="$ioutputname".img
outputtextname="Build-info-$outputname".txt
output="$OUTDIR/$outputimagename"
#Overlays
outputvendoroverlaysname="VendorOverlays-$outputname".tar.gz
outputvendoroverlays="$OUTDIR/$outputvendoroverlaysname"
if [[ -d "$TARGETDIR/vendor/overlay" && ! -f "$outputvendoroverlays" ]]; then
mkdir -p "$OUTDIR/vendorOverlays"
cp -frp $TARGETDIR/vendor/overlay/* "$OUTDIR/vendorOverlays" >> /dev/null 2>&1
if [ -d "$OUTDIR/vendorOverlays" ]; then
cd $OUTDIR/vendorOverlays
echo "├─ Extracting VOverlays..."
tar -zcvf "$outputvendoroverlays" * >> /dev/null 2>&1
cd $LOCALDIR
rm -rf "output/vendorOverlays"
fi
fi
cd $LOCALDIR
size=`du -sk $systemdir | awk '{$1*=1024;$1=int($1*1.05);printf $1}'`
bytesToHuman() {
b=${1:-0}; d=''; s=0; S=(Bytes {K,M,G,T,P,E,Z,Y}iB)
while ((b > 1024)); do
d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
b=$((b / 1024))
let s++
done
echo "$b$d ${S[$s]}"
}
echo "┠⌬ Packing Image..."
case $os_repackage_type in
"A"|"a")
$bin/mkuserimg_mke2fs.sh "$systemdir" "$output" "ext4" "/system" $size -j "0" -T "1230768000" -L "system" -I "256" -M "/system" -m "0" $configdir/$target_contexts > /dev/null 2>&1
;;
"AB"|"ab")
$bin/mkuserimg_mke2fs.sh "$systemdir" "$output" "ext4" "/" $size -j "0" -T "1230768000" -L "/" -I "256" -M "/" -m "0" $configdir/$target_contexts
;;
"A_CONFIG"|"a_config")
$bin/mkuserimg_mke2fs.sh "$systemdir" "$output" "ext4" "/system" $size -j "0" -T "1230768000" -C "$configdir/system_A_fs" -L "system" -I "256" -M "/system" -m "0" "$configdir/system_A_contexts" > /dev/null 2>&1
;;
"AB_CONFIG"|"ab_config")
$bin/mkuserimg_mke2fs.sh "$systemdir" "$output" "ext4" "/system" $size -j "0" -T "1230768000" -C "$configdir/system_fs_config" -L "system" -I "256" -M "/system" -m "0" "$configdir/system_file_contexts"
;;
esac
if [ -s $output ];then
echo "├⌬ $name ━ $(bytesToHuman $size)"
else
rm -rf $OUTDIR
exit 1
fi
echo "├─ Generating SGSI info..."
#System Tree
outputtreename="System-Tree-$outputname".txt
outputtree="$OUTDIR/$outputtreename"
if [ ! -f "$outputtree" ]; then
tree $systemdir >> "$outputtree" 2> "$outputtree"
fi
if [ -s $output ];then
rm -rf $TMPDIR
cp -frp $system/build.prop $TARGETDIR/
$SCRIPTDIR/get_build_info.sh "$TARGETDIR" "$output" > $OUTDIR/$outputtextname
rm -rf $TARGETDIR/build.prop
chmod -R 777 $LOCALDIR/output
fi