blob: 2bca4c3c26c5ccd891a9fa0517942df21ea89eed [file] [log] [blame]
Ethan Yonkere3e88292014-12-10 16:17:55 -06001#!/sbin/sh
2#
3# SuperSU installer ZIP
4# Copyright (c) 2012-2014 - Chainfire
5#
6# To install SuperSU properly, aside from cleaning old versions and
7# other superuser-type apps from the system, the following files need to
8# be installed:
9#
10# API source target chmod chcon required
11#
12# 7-19 common/Superuser.apk /system/app/Superuser.apk 0644 u:object_r:system_file:s0 gui
13# 20+ common/Superuser.apk /system/app/SuperSU/SuperSU.apk 0644 u:object_r:system_file:s0 gui
14#
15# 17+ common/install-recovery.sh /system/etc/install-recovery.sh 0755 *1 required
16# 17+ /system/bin/install-recovery.sh (symlink to /system/etc/...) required
17# *1: same as /system/bin/toolbox: u:object_r:system_file:s0 if API < 20, u:object_r:toolbox_exec:s0 if API >= 20
18#
19# 7+ ARCH/su /system/xbin/su *2 u:object_r:system_file:s0 required
20# 7+ /system/bin/.ext/.su *2 u:object_r:system_file:s0 gui
21# 17+ /system/xbin/daemonsu 0755 u:object_r:system_file:s0 required
22# 17+ /system/xbin/sugote 0755 u:object_r:zygote_exec:s0 required
23# *2: 06755 if API < 18, 0755 if API >= 18
24#
25# 19+ ARCH/supolicy /system/xbin/supolicy 0755 u:object_r:system_file:s0 required
26# 19+ ARCH/libsupol.so /system/lib(64)/libsupol.so 0644 u:object_r:system_file:s0 required
27#
28# 17+ /system/bin/sh or mksh *3 /system/xbin/sugote-mksh 0755 u:object_r:system_file:s0 required
29# *3: which one (or both) are available depends on API
30#
31# 21+ /system/bin/app_process32 *4 /system/bin/app_process32_original 0755 u:object_r:zygote_exec:s0 required
32# 21+ /system/bin/app_process64 *4 /system/bin/app_process64_original 0755 u:object_r:zygote_exec:s0 required
33# 21+ /system/bin/app_processXX *4 /system/bin/app_process_init 0755 u:object_r:system_file:s0 required
34# 21+ /system/bin/app_process (symlink to /system/xbin/daemonsu) required
35# 21+ *4 /system/bin/app_process32 (symlink to /system/xbin/daemonsu) required
36# 21+ *4 /system/bin/app_process64 (symlink to /system/xbin/daemonsu) required
37# *4: Only do this for the relevant bits. On a 64 bits system, leave the 32 bits files alone, or dynamic linker errors
38# will prevent the system from fully working in subtle ways. The bits of the su binary must also match!
39#
40# 17+ common/99SuperSUDaemon *5 /system/etc/init.d/99SuperSUDaemon 0755 u:object_r:system_file:s0 optional
41# *5: only place this file if /system/etc/init.d is present
42#
43# 17+ 'echo 1 >' or 'touch' *6 /system/etc/.installed_su_daemon 0644 u:object_r:system_file:s0 optional
44# *6: the file just needs to exist or some recoveries will nag you. Even with it there, it may still happen.
45#
46# It may seem some files are installed multiple times needlessly, but
47# it only seems that way. Installing files differently or symlinking
48# instead of copying (unless specified) will lead to issues eventually.
49#
50# The following su binary versions are included in the full package. Each
51# should be installed only if the system has the same or newer API level
52# as listed. The script may fall back to a different binary on older API
53# levels. supolicy are all ndk/pie/19+ for 32 bit, ndk/pie/20+ for 64 bit.
54#
55# binary ARCH/path build type API
56#
57# arm-v5te arm aosp static 7+
58# x86 x86 aosp static 7+
59#
60# arm-v7a armv7 ndk pie 17+
61# mips mips ndk pie 17+
62#
63# arm64-v8a arm64 ndk pie 20+
64# mips64 mips64 ndk pie 20+
65# x86_64 x64 ndk pie 20+
66#
67# Note that if SELinux is set to enforcing, the daemonsu binary expects
68# to be run at startup (usually from install-recovery.sh, 99SuperSUDaemon,
69# or app_process) from u:r:init:s0 or u:r:kernel:s0 contexts. Depending
70# on the current policies, it can also deal with u:r:init_shell:s0 and
71# u:r:toolbox:s0 contexts. Any other context will lead to issues eventually.
72#
73# After installation, run '/system/xbin/su --install', which may need to
74# perform some additional installation steps. Ideally, at one point,
75# a lot of this script will be moved there.
76#
77# The included chattr(.pie) binaries are used to remove ext2's immutable
78# flag on some files. This flag is no longer set by SuperSU's OTA
79# survival since API level 18, so there is no need for the 64 bit versions.
80# Note that chattr does not need to be installed to the system, it's just
81# used by this script, and not supported by the busybox used in older
82# recoveries.
83#
84# Non-static binaries are supported to be PIE (Position Independent
85# Executable) from API level 16, and required from API level 20 (which will
86# refuse to execute non-static non-PIE).
87#
88# The script performs serveral actions in various ways, sometimes
89# multiple times, due to different recoveries and firmwares behaving
90# differently, and it thus being required for the correct result.
91
92OUTFD=$2
93ZIP=$3
94
95SYSTEMLIB=/system/lib
96
97ui_print() {
98 echo -n -e "echo $1\n" > /proc/self/fd/$OUTFD
99 echo -n -e "echo\n" > /proc/self/fd/$OUTFD
100}
101
102ch_con() {
103 LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon -h u:object_r:system_file:s0 $1
104 LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon -h u:object_r:system_file:s0 $1
105 chcon -h u:object_r:system_file:s0 $1
106 LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon u:object_r:system_file:s0 $1
107 LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon u:object_r:system_file:s0 $1
108 chcon u:object_r:system_file:s0 $1
109}
110
111ch_con_ext() {
112 LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon $2 $1
113 LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon $2 $1
114 chcon $2 $1
115}
116
117ln_con() {
118 LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox ln -s $1 $2
119 LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox ln -s $1 $2
120 ln -s $1 $2
121 ch_con $2
122}
123
124set_perm() {
125 chown $1.$2 $4
126 chown $1:$2 $4
127 chmod $3 $4
128 ch_con $4
129 ch_con_ext $4 $5
130}
131
132cp_perm() {
133 rm $5
134 cat $4 > $5
135 set_perm $1 $2 $3 $5 $6
136}
137
138echo "*********************"
139echo "SuperSU installer ZIP"
140echo "*********************"
141
142echo "- Mounting /system, /data and rootfs"
143mount /system
144mount /data
145mount -o rw,remount /system
146mount -o rw,remount /system /system
147mount -o rw,remount /
148mount -o rw,remount / /
149
150cat /system/bin/toolbox > /system/toolbox
151chmod 0755 /system/toolbox
152ch_con /system/toolbox
153
154API=$(cat /system/build.prop | grep "ro.build.version.sdk=" | dd bs=1 skip=21 count=2)
155ABI=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi=" | dd bs=1 skip=19 count=3)
156ABILONG=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi=" | dd bs=1 skip=19)
157ABI2=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi2=" | dd bs=1 skip=20 count=3)
158SUMOD=06755
159SUGOTE=false
160SUPOLICY=false
161INSTALL_RECOVERY_CONTEXT=u:object_r:system_file:s0
162MKSH=/system/bin/mksh
163PIE=
164ARCH=arm
165APKFOLDER=false
166APKNAME=/system/app/Superuser.apk
167APPPROCESS=false
168APPPROCESS64=false
169if [ "$ABI" = "x86" ]; then ARCH=x86; fi;
170if [ "$ABI2" = "x86" ]; then ARCH=x86; fi;
171if [ "$API" -eq "$API" ]; then
172 if [ "$API" -ge "17" ]; then
173 SUGOTE=true
174 PIE=.pie
175 if [ "$ABILONG" = "armeabi-v7a" ]; then ARCH=armv7; fi;
176 if [ "$ABI" = "mip" ]; then ARCH=mips; fi;
177 if [ "$ABILONG" = "mips" ]; then ARCH=mips; fi;
178 fi
179 if [ "$API" -ge "18" ]; then
180 SUMOD=0755
181 fi
182 if [ "$API" -ge "20" ]; then
183 if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
184 if [ "$ABILONG" = "mips64" ]; then ARCH=mips64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
185 if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
186 APKFOLDER=true
187 APKNAME=/system/app/SuperSU/SuperSU.apk
188 fi
189 if [ "$API" -ge "19" ]; then
190 SUPOLICY=true
191 if [ "$(LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox ls -lZ /system/bin/toolbox | grep toolbox_exec > /dev/null; echo $?)" -eq "0" ]; then
192 INSTALL_RECOVERY_CONTEXT=u:object_r:toolbox_exec:s0
193 fi
194 fi
195 if [ "$API" -ge "21" ]; then
196 APPPROCESS=true
197 fi
198fi
199if [ ! -f $MKSH ]; then
200 MKSH=/system/bin/sh
201fi
202
203#echo "DBG [$API] [$ABI] [$ABI2] [$ABILONG] [$ARCH] [$MKSH]"
204
205# Don't extract in TWRP
206#echo "- Extracting files"
207#cd /tmp
208#mkdir supersu
209#cd supersu
210#unzip -o "$ZIP"
211
212BIN=/supersu
213COM=/supersu
214
215echo "- Disabling OTA survival"
216chmod 0755 /supersu/chattr$PIE
217LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/bin/su
218LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/xbin/su
219LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/bin/.ext/.su
220LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/xbin/daemonsu
221LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/xbin/sugote
222LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/xbin/sugote_mksh
223LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/xbin/supolicy
224LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/lib/libsupol.so
225LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/lib64/libsupol.so
226LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/etc/install-recovery.sh
227LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -i /system/bin/install-recovery.sh
228
229echo "- Removing old files"
230
231if [ -f "/system/bin/install-recovery.sh" ]; then
232 if [ ! -f "/system/bin/install-recovery_original.sh" ]; then
233 mv /system/bin/install-recovery.sh /system/bin/install-recovery_original.sh
234 ch_con /system/bin/install-recovery_original.sh
235 fi
236fi
237if [ -f "/system/etc/install-recovery.sh" ]; then
238 if [ ! -f "/system/etc/install-recovery_original.sh" ]; then
239 mv /system/etc/install-recovery.sh /system/etc/install-recovery_original.sh
240 ch_con /system/etc/install-recovery_original.sh
241 fi
242fi
243
244rm -f /system/bin/su
245rm -f /system/xbin/su
246rm -f /system/xbin/daemonsu
247rm -f /system/xbin/sugote
248rm -f /system/xbin/sugote-mksh
249rm -f /system/xbin/supolicy
250rm -f /system/lib/libsupol.so
251rm -f /system/lib64/libsupol.so
252rm -f /system/bin/.ext/.su
253rm -f /system/bin/install-recovery.sh
254rm -f /system/etc/install-recovery.sh
255rm -f /system/etc/init.d/99SuperSUDaemon
256rm -f /system/etc/.installed_su_daemon
257
258rm -f /system/app/Superuser.apk
259rm -f /system/app/Superuser.odex
260rm -rf /system/app/Superuser
261rm -f /system/app/SuperUser.apk
262rm -f /system/app/SuperUser.odex
263rm -rf /system/app/SuperUser
264rm -f /system/app/superuser.apk
265rm -f /system/app/superuser.odex
266rm -rf /system/app/superuser
267rm -f /system/app/Supersu.apk
268rm -f /system/app/Supersu.odex
269rm -rf /system/app/Supersu
270rm -f /system/app/SuperSU.apk
271rm -f /system/app/SuperSU.odex
272rm -rf /system/app/SuperSU
273rm -f /system/app/supersu.apk
274rm -f /system/app/supersu.odex
275rm -rf /system/app/supersu
276rm -f /system/app/VenomSuperUser.apk
277rm -f /system/app/VenomSuperUser.odex
278rm -rf /system/app/VenomSuperUser
279rm -f /data/dalvik-cache/*com.noshufou.android.su*
280rm -f /data/dalvik-cache/*/*com.noshufou.android.su*
281rm -f /data/dalvik-cache/*com.koushikdutta.superuser*
282rm -f /data/dalvik-cache/*/*com.koushikdutta.superuser*
283rm -f /data/dalvik-cache/*com.mgyun.shua.su*
284rm -f /data/dalvik-cache/*/*com.mgyun.shua.su*
285rm -f /data/dalvik-cache/*com.m0narx.su*
286rm -f /data/dalvik-cache/*/*com.m0narx.su*
287rm -f /data/dalvik-cache/*Superuser.apk*
288rm -f /data/dalvik-cache/*/*Superuser.apk*
289rm -f /data/dalvik-cache/*SuperUser.apk*
290rm -f /data/dalvik-cache/*/*SuperUser.apk*
291rm -f /data/dalvik-cache/*superuser.apk*
292rm -f /data/dalvik-cache/*/*superuser.apk*
293rm -f /data/dalvik-cache/*VenomSuperUser.apk*
294rm -f /data/dalvik-cache/*/*VenomSuperUser.apk*
295rm -f /data/dalvik-cache/*eu.chainfire.supersu*
296rm -f /data/dalvik-cache/*/*eu.chainfire.supersu*
297rm -f /data/dalvik-cache/*Supersu.apk*
298rm -f /data/dalvik-cache/*/*Supersu.apk*
299rm -f /data/dalvik-cache/*SuperSU.apk*
300rm -f /data/dalvik-cache/*/*SuperSU.apk*
301rm -f /data/dalvik-cache/*supersu.apk*
302rm -f /data/dalvik-cache/*/*supersu.apk*
303rm -f /data/dalvik-cache/*.oat
304rm -f /data/app/com.noshufou.android.su*
305rm -f /data/app/com.koushikdutta.superuser*
306rm -f /data/app/com.mgyun.shua.su*
307rm -f /data/app/com.m0narx.su*
308rm -f /data/app/eu.chainfire.supersu-*
309rm -f /data/app/eu.chainfire.supersu.apk
310
311echo "- Creating space"
312if ($APKFOLDER); then
313 cp /system/app/Maps/Maps.apk /Maps.apk
314 cp /system/app/GMS_Maps/GMS_Maps.apk /GMS_Maps.apk
315 cp /system/app/YouTube/YouTube.apk /YouTube.apk
316 rm /system/app/Maps/Maps.apk
317 rm /system/app/GMS_Maps/GMS_Maps.apk
318 rm /system/app/YouTube/YouTube.apk
319else
320 cp /system/app/Maps.apk /Maps.apk
321 cp /system/app/GMS_Maps.apk /GMS_Maps.apk
322 cp /system/app/YouTube.apk /YouTube.apk
323 rm /system/app/Maps.apk
324 rm /system/app/GMS_Maps.apk
325 rm /system/app/YouTube.apk
326fi
327
328echo "- Placing files"
329
330mkdir /system/bin/.ext
331set_perm 0 0 0777 /system/bin/.ext
332cp_perm 0 0 $SUMOD $BIN/su /system/bin/.ext/.su
333cp_perm 0 0 $SUMOD $BIN/su /system/xbin/su
334cp_perm 0 0 0755 $BIN/su /system/xbin/daemonsu
335if ($SUGOTE); then
336 cp_perm 0 0 0755 $BIN/su /system/xbin/sugote u:object_r:zygote_exec:s0
337 cp_perm 0 0 0755 $MKSH /system/xbin/sugote-mksh
338fi
339if ($SUPOLICY); then
340 cp_perm 0 0 0755 $BIN/supolicy /system/xbin/supolicy
341 cp_perm 0 0 0644 $BIN/libsupol.so $SYSTEMLIB/libsupol.so
342fi
343if ($APKFOLDER); then
344 mkdir /system/app/SuperSU
345 set_perm 0 0 0755 /system/app/SuperSU
346fi
347cp_perm 0 0 0644 $COM/Superuser.apk $APKNAME
348cp_perm 0 0 0755 $COM/install-recovery.sh /system/etc/install-recovery.sh
349ln_con /system/etc/install-recovery.sh /system/bin/install-recovery.sh
350if ($APPPROCESS); then
351 rm /system/bin/app_process
352 ln_con /system/xbin/daemonsu /system/bin/app_process
353 if ($APPPROCESS64); then
354 if [ ! -f "/system/bin/app_process64_original" ]; then
355 mv /system/bin/app_process64 /system/bin/app_process64_original
356 else
357 rm /system/bin/app_process64
358 fi
359 ln_con /system/xbin/daemonsu /system/bin/app_process64
360 if [ ! -f "/system/bin/app_process_init" ]; then
361 cp_perm 0 2000 0755 /system/bin/app_process64_original /system/bin/app_process_init
362 fi
363 else
364 if [ ! -f "/system/bin/app_process32_original" ]; then
365 mv /system/bin/app_process32 /system/bin/app_process32_original
366 else
367 rm /system/bin/app_process32
368 fi
369 ln_con /system/xbin/daemonsu /system/bin/app_process32
370 if [ ! -f "/system/bin/app_process_init" ]; then
371 cp_perm 0 2000 0755 /system/bin/app_process32_original /system/bin/app_process_init
372 fi
373 fi
374fi
375cp_perm 0 0 0744 $COM/99SuperSUDaemon /system/etc/init.d/99SuperSUDaemon
376echo 1 > /system/etc/.installed_su_daemon
377set_perm 0 0 0644 /system/etc/.installed_su_daemon
378
379echo "- Restoring files"
380if ($APKFOLDER); then
381 cp_perm 0 0 0644 /Maps.apk /system/app/Maps/Maps.apk
382 cp_perm 0 0 0644 /GMS_Maps.apk /system/app/GMS_Maps/GMS_Maps.apk
383 cp_perm 0 0 0644 /YouTube.apk /system/app/YouTube/YouTube.apk
384 rm /Maps.apk
385 rm /GMS_Maps.apk
386 rm /YouTube.apk
387else
388 cp_perm 0 0 0644 /Maps.apk /system/app/Maps.apk
389 cp_perm 0 0 0644 /GMS_Maps.apk /system/app/GMS_Maps.apk
390 cp_perm 0 0 0644 /YouTube.apk /system/app/YouTube.apk
391 rm /Maps.apk
392 rm /GMS_Maps.apk
393 rm /YouTube.apk
394fi
395
396echo "- Post-installation script"
397rm /system/toolbox
398LD_LIBRARY_PATH=$SYSTEMLIB /system/xbin/su --install
399
400echo "- Unmounting /system and /data"
401umount /system
402umount /data
403
404echo "- Done !"
405exit 0