fork(29) download
  1. #!/sbin/sh
  2. #
  3. # SuperSU installer ZIP
  4. # Copyright (c) 2012-2017 - Chainfire, CCMT
  5. #
  6. # ----- GENERIC INFO ------
  7. #
  8. # The following su binary versions are included in the full package. Each
  9. # should be installed only if the system has the same or newer API level
  10. # as listed. The script may fall back to a different binary on older API
  11. # levels. supolicy are all ndk/pie/19+ for 32 bit, ndk/pie/20+ for 64 bit.
  12. #
  13. # binary ARCH/path build type API
  14. #
  15. # arm-v5te arm ndk non-pie 7+
  16. # x86 x86 ndk non-pie 7+
  17. #
  18. # x86 x86 ndk pie 17+ (su.pie, naming exception)
  19. # arm-v7a armv7 ndk pie 17+
  20. # mips mips ndk pie 17+
  21. #
  22. # arm64-v8a arm64 ndk pie 20+
  23. # mips64 mips64 ndk pie 20+
  24. # x86_64 x64 ndk pie 20+
  25. #
  26. # Non-static binaries are supported to be PIE (Position Independent
  27. # Executable) from API level 16, and required from API level 20 (which will
  28. # refuse to execute non-static non-PIE).
  29. #
  30. # The script performs several actions in various ways, sometimes
  31. # multiple times, due to different recoveries and firmwares behaving
  32. # differently, and it thus being required for the correct result.
  33. #
  34. # Overridable variables (shell):
  35. # BIN - Location of architecture specific files (native folder)
  36. # COM - Location of common files (APK folder)
  37. # LESSLOGGING - Reduce ui_print logging (true/false)
  38. # NOOVERRIDE - Do not read variables from /system/.supersu or
  39. # /data/.supersu
  40. #
  41. # Overridable variables (shell, /system/.supersu, /cache/.supersu,
  42. # /data/.supersu):
  43. # SYSTEMLESS - Do a system-less install? (true/false, 6.0+ only)
  44. # PATCHBOOTIMAGE - Automatically patch boot image? (true/false,
  45. # SYSTEMLESS only)
  46. # BOOTIMAGE - Boot image location (PATCHBOOTIMAGE only)
  47. # STOCKBOOTIMAGE - Stock boot image location (PATCHBOOTIMAGE only)
  48. # BINDSYSTEMXBIN - Poor man's overlay on /system/xbin (true/false,
  49. # SYSTEMLESS only)
  50. # PERMISSIVE - Set sepolicy to fake-permissive (true/false, PATCHBOOTIMAGE
  51. # only)
  52. # KEEPVERITY - Do not remove dm-verity (true/false, PATCHBOOTIMAGE only)
  53. # KEEPFORCEENCRYPT - Do not replace forceencrypt with encryptable (true/
  54. # false, PATCHBOOTIMAGE only)
  55. # REMOVEENCRYPTABLE - Remove the encryptable flag, needed on newer
  56. # Samsung devices to disable forced encryption
  57. # (true/false, PATCHBOOTIMAGE only)
  58. # FRP - Place files in boot image that allow root to survive a factory
  59. # reset (true/false, PATCHBOOTIMAGE only). Reverts to su binaries
  60. # from the time the ZIP was originall flashed, updates are lost.
  61. # Shell overrides all, /data/.supersu overrides /cache/.supersu overrides
  62. # /system/.supersu
  63. #
  64. # Note that if SELinux is set to enforcing, the daemonsu binary expects
  65. # to be run at startup (usually from install-recovery.sh, 99SuperSUDaemon,
  66. # app_process, or init.supersu.rc) from u:r:supersu:s0 (7.0+), u:r:init:s0 or
  67. # u:r:kernel:s0 contexts. Depending on the current policies, it can also
  68. # deal with u:r:init_shell:s0 and u:r:toolbox:s0 contexts. Any other context
  69. # will lead to issues eventually.
  70. #
  71. # ----- "SYSTEM" INSTALL -----
  72. #
  73. # "System" install puts all the files needed in /system and does not need
  74. # any boot image modifications. Default install method pre-Android-6.0
  75. # (excluding Samsung-5.1).
  76. #
  77. # Even on Android-6.0+, the script attempts to detect if the current
  78. # firmware is compatible with a system-only installation (see the
  79. # "detect_systemless_required" function), and will prefer that
  80. # (unless the SYSTEMLESS variable is set) if so. This will catch the
  81. # case of several custom ROMs that users like to use custom boot images
  82. # with - SuperSU will not need to patch these. It can also catch some
  83. # locked bootloader cases that do allow security policy updates.
  84. #
  85. # To install SuperSU properly, aside from cleaning old versions and
  86. # other superuser-type apps from the system, the following files need to
  87. # be installed:
  88. #
  89. # API source target chmod chcon required
  90. #
  91. # 7-19 common/Superuser.apk /system/app/Superuser.apk 0644 u:object_r:system_file:s0 gui
  92. # 20+ common/Superuser.apk /system/app/SuperSU/SuperSU.apk 0644 u:object_r:system_file:s0 gui
  93. #
  94. # 17+ common/install-recovery.sh /system/etc/install-recovery.sh 0755 *1 required
  95. # 17+ /system/bin/install-recovery.sh (symlink to /system/etc/...) required
  96. # *1: same as /system/bin/toolbox: u:object_r:system_file:s0 if API < 20, u:object_r:toolbox_exec:s0 if API >= 20
  97. #
  98. # 7+ ARCH/su *2 /system/xbin/su *3 u:object_r:system_file:s0 required
  99. # 7+ /system/bin/.ext/.su *3 u:object_r:system_file:s0 gui
  100. # 17+ /system/xbin/daemonsu 0755 u:object_r:system_file:s0 required
  101. # *2: su.pie for 17+ x86(_32) only
  102. # *3: 06755 if API < 18, 0755 if API >= 18
  103. #
  104. # 19+ ARCH/supolicy /system/xbin/supolicy 0755 u:object_r:system_file:s0 required
  105. # 19+ ARCH/libsupol.so /system/lib(64)/libsupol.so 0644 u:object_r:system_file:s0 required
  106. #
  107. # 21+ /system/bin/app_process32 *5 /system/bin/app_process32_original 0755 u:object_r:zygote_exec:s0 required
  108. # 21+ /system/bin/app_process64 *5 /system/bin/app_process64_original 0755 u:object_r:zygote_exec:s0 required
  109. # 21+ /system/bin/app_processXX *5 /system/bin/app_process_init 0755 u:object_r:system_file:s0 required
  110. # 21+ /system/bin/app_process (symlink to /system/xbin/daemonsu) required
  111. # 21+ *5 /system/bin/app_process32 (symlink to /system/xbin/daemonsu) required
  112. # 21+ *5 /system/bin/app_process64 (symlink to /system/xbin/daemonsu) required
  113. # *5: Only do this for the relevant bits. On a 64 bits system, leave the 32 bits files alone, or dynamic linker errors
  114. # will prevent the system from fully working in subtle ways. The bits of the su binary must also match!
  115. #
  116. # 17+ common/99SuperSUDaemon *6 /system/etc/init.d/99SuperSUDaemon 0755 u:object_r:system_file:s0 optional
  117. # *6: only place this file if /system/etc/init.d is present
  118. #
  119. # 17+ 'echo 1 >' or 'touch' *7 /system/etc/.installed_su_daemon 0644 u:object_r:system_file:s0 optional
  120. # *7: the file just needs to exist or some recoveries will nag you. Even with it there, it may still happen.
  121. #
  122. # It may seem some files are installed multiple times needlessly, but
  123. # it only seems that way. Installing files differently or symlinking
  124. # instead of copying (unless specified) will lead to issues eventually.
  125. #
  126. # After installation, run '/system/xbin/su --install', which may need to
  127. # perform some additional installation steps. Ideally, at one point,
  128. # a lot of this script will be moved there.
  129. #
  130. # The included chattr(.pie) binaries are used to remove ext2's immutable
  131. # flag on some files. This flag is no longer set by SuperSU's OTA
  132. # survival since API level 18, so there is no need for the 64 bit versions.
  133. # Note that chattr does not need to be installed to the system, it's just
  134. # used by this script, and not supported by the busybox used in older
  135. # recoveries.
  136. #
  137. # ----- "SYSTEM-LESS" INSTALL -----
  138. #
  139. # "System-less" install requires a modified boot image (the script can patch
  140. # many boot images on-the-fly), but does not touch /system at all. Instead
  141. # it keeps all the needed files in an image (/data/su.img) which is mounted
  142. # to /su. Default install method on all Android-6.0+ and Samsung-5.1+
  143. # devices.
  144. #
  145. # Note that even on 6.0+, system compatibility is checked. See the "SYSTEM"
  146. # install section above.
  147. #
  148. # An ext4 image is created as /data/su.img, or /cache/su.img if /data could
  149. # not be mounted. Similarly, the APK is placed as either /data/SuperSU.apk
  150. # or /cache/SuperSU.apk. This is so we are not dependent on /data decryption
  151. # working in recovery, which in the past has proved an issue on brand-new
  152. # Android versions and devices.
  153. #
  154. # /sbin/launch_daemonsu.sh, which is added a service to init.rc, will mount
  155. # the image at /su, and launch daemonsu from /su/bin/daemonsu. But before it
  156. # does that, it will try to merge /data/su.img and /cache/su.img (leading),
  157. # if both are present. It will also try to install the SuperSU APK.
  158. #
  159. # Files are expected at the following places (/su being the mountpoint of
  160. # the ext4 image):
  161. #
  162. # API source target chmod chcon required
  163. #
  164. # 22+ common/Superuser.apk /[data|cache]/SuperSU.apk 0644 u:object_r:system_file:s0 gui
  165. #
  166. # 22+ ARCH/su *1 /su/bin/su 0755 u:object_r:system_file:s0 required
  167. # 22+ /su/bin/daemonsu 0755 u:object_r:system_file:s0 required
  168. # *1: su.pie for 17+ x86(_32) only
  169. #
  170. # 22+ ARCH/supolicy /su/bin/supolicy_wrapped 0755 u:object_r:system_file:s0 required
  171. # 22+ /su/bin/su (symlink) *2 /su/bin/supolicy 0755 u:object_r:system_file:s0 required
  172. # 22+ ARCH/libsupol.so /su/lib/libsupol.so 0644 u:object_r:system_file:s0 required
  173. # *2: when called this way, su sets the correct LD_LIBRARY_PATH and calls supolicy_wrapped
  174. #
  175. # 22+ ARCH/sukernel /su/bin/sukernel 0755 u:object_r:system_file:s0 required
  176. #
  177. # These files are automatically created on launch by daemonsu as needed:
  178. # 22+ /system/bin/sh /su/bin/sush 0755 u:object_r:system_file:s0 required
  179. # 22+ /system/bin/app_process[64] /su/bin/app_process 0755 u:object_r:system_file:s0 required
  180. #
  181. # These files are injected into the boot image ramdisk:
  182. # 22+ common/launch_daemonsu.sh /sbin/launch_daemonsu.sh 0700 u:object_r:rootfs:s0 required
  183. #
  184. # On devices where / is in the system partition:
  185. # 22+ ARCH/suinit /init 0750 u:object_r:rootfs:s0 required
  186. #
  187. # The automated boot image patcher included makes the following modifications
  188. # to the ramdisk:
  189. #
  190. # - Uses the supolicy tool to patch the sepolicy file
  191. # - Injects /sbin/launch_daemon.sh
  192. # - Creates /su
  193. # - Removes /verity_key
  194. # - Patches /*fstab*
  195. # --- Removes support_scfs and verify flags
  196. # --- Changes forceencrypt/forcefdeorfbe into encryptable
  197. # --- Set ro mounts to use noatime
  198. # - Patches /init.rc
  199. # --- Removes 'setprop selinux.reload_policy' occurences
  200. # --- Adds a SuperSU:PATCH marker with the version of the sukernel tool
  201. # --- Adds a SuperSU:STOCK marker listed the SHA1 of the original boot image
  202. # - Adds /init.supersu.rc
  203. # --- Adds a sukernel.mount property trigger that mounts /data/su.img to /su
  204. # --- Adds the daemonsu service that launches /sbin/launch_daemon.sh
  205. # --- Adds exec /sbin/launch_daemonsu.sh on post-fs-data
  206. # - Patches /init.environ.rc
  207. # --- Adds PATH variable if it does not exist
  208. # --- Prepends /su/bin to the PATH variable
  209. # - Patches /*.rc
  210. # --- Adds a seclabel to services and execs that are missing one
  211. # - In case the device has the root directory inside the system partition:
  212. # --- /system_root contents are copied to /boot
  213. # --- All files mentioned above are modified in /boot instead of /
  214. # --- /boot/*fstab* is modified to mount / to /system_root, and
  215. # bind-mount /system to /system_root/system
  216. # --- Kernel binary is patched to load from initramfs instead of system
  217. #
  218. # In case this documentation becomes outdated, please note that the sukernel
  219. # tool is very chatty, and its output tells you exactly what it is doing
  220. # and how. In TWRP, you can view this output by catting /tmp/recovery.log
  221. # after flashing the ZIP.
  222. #
  223. # The boot image patcher creates a backup of the boot image it patches, for
  224. # future restoration. It cannot re-patch a patched boot image, it will restore
  225. # the previous boot image first. /[data|cache]/stock_boot_*.gz
  226. #
  227. # The boot image patcher currently only supports GZIP compressed ramdisks, and
  228. # boot images in the standard Android boot image format.
  229. #
  230. # During boot image patch, /data/custom_ramdisk_patch.sh will be called,
  231. # with the name of the ramdisk cpio file as parameter. The script must
  232. # replace the input file and return a 0 exit code.
  233. #
  234. # Just before flashing, the boot image patcher will call
  235. # /data/custom_boot_image_patch.sh with the name of the patched boot image
  236. # as parameter. A device-specific patcher can further patch the boot image
  237. # if needed. It must replace the input file and return a 0 exit code.
  238.  
  239. OUTFD=$2
  240. ZIP=$3
  241.  
  242. getvar() {
  243. local VARNAME=$1
  244. local VALUE=$(eval echo \$"$VARNAME");
  245. for FILE in /data/.supersu /cache/.supersu /system/.supersu; do
  246. if [ -z "$VALUE" ]; then
  247. LINE=$(cat $FILE 2>/dev/null | grep "$VARNAME=")
  248. if [ ! -z "$LINE" ]; then
  249. VALUE=${LINE#*=}
  250. fi
  251. fi
  252. done
  253. eval $VARNAME=\$VALUE
  254. }
  255.  
  256. readlink /proc/$$/fd/$OUTFD 2>/dev/null | grep /tmp >/dev/null
  257. if [ "$?" -eq "0" ]; then
  258. # rerouted to log file, we don't want our ui_print commands going there
  259. OUTFD=0
  260.  
  261. # we are probably running in embedded mode, see if we can find the right fd
  262. # we know the fd is a pipe and that the parent updater may have been started as
  263. # 'update-binary 3 fd zipfile'
  264. for FD in `ls /proc/$$/fd`; do
  265. readlink /proc/$$/fd/$FD 2>/dev/null | grep pipe >/dev/null
  266. if [ "$?" -eq "0" ]; then
  267. ps | grep " 3 $FD " | grep -v grep >/dev/null
  268. if [ "$?" -eq "0" ]; then
  269. OUTFD=$FD
  270. break
  271. fi
  272. fi
  273. done
  274. fi
  275.  
  276. ui_print_always() {
  277. echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD
  278. echo -n -e "ui_print\n" >> /proc/self/fd/$OUTFD
  279. }
  280.  
  281. if [ -z "$LESSLOGGING" ]; then
  282. LESSLOGGING=false
  283. fi
  284.  
  285. UI_PRINT_LAST=""
  286.  
  287. ui_print() {
  288. if (! $LESSLOGGING); then
  289. UI_PRINT_LAST="$1"
  290. ui_print_always "$1"
  291. fi
  292. }
  293.  
  294. ui_print_less() {
  295. if ($LESSLOGGING); then
  296. ui_print_always "$1"
  297. fi
  298. }
  299.  
  300. ch_con() {
  301. LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toybox chcon -h u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  302. LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon -h u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  303. LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon -h u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  304. chcon -h u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  305. LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toybox chcon u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  306. LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  307. LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  308. chcon u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  309. }
  310.  
  311. ch_con_ext() {
  312. LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toybox chcon $2 $1 1>/dev/null 2>/dev/null
  313. LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon $2 $1 1>/dev/null 2>/dev/null
  314. LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon $2 $1 1>/dev/null 2>/dev/null
  315. chcon $2 $1 1>/dev/null 2>/dev/null
  316. }
  317.  
  318. ln_con() {
  319. LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toybox ln -s $1 $2 1>/dev/null 2>/dev/null
  320. LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox ln -s $1 $2 1>/dev/null 2>/dev/null
  321. LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox ln -s $1 $2 1>/dev/null 2>/dev/null
  322. ln -s $1 $2 1>/dev/null 2>/dev/null
  323. ch_con $2 1>/dev/null 2>/dev/null
  324. }
  325.  
  326. set_perm() {
  327. chown $1.$2 $4
  328. chown $1:$2 $4
  329. chmod $3 $4
  330. ch_con $4
  331. ch_con_ext $4 $5
  332. }
  333.  
  334. cp_perm() {
  335. rm $5
  336. if [ -f "$4" ]; then
  337. cat $4 > $5
  338. set_perm $1 $2 $3 $5 $6
  339. fi
  340. }
  341.  
  342. is_mounted() {
  343. if [ ! -z "$2" ]; then
  344. cat /proc/mounts | grep $1 | grep $2, >/dev/null
  345. else
  346. cat /proc/mounts | grep $1 >/dev/null
  347. fi
  348. return $?
  349. }
  350.  
  351. toolbox_mount() {
  352. RW=rw
  353. if [ ! -z "$2" ]; then
  354. RW=$2
  355. fi
  356.  
  357. DEV=
  358. POINT=
  359. FS=
  360. for i in `cat /etc/fstab | grep "$1"`; do
  361. if [ -z "$DEV" ]; then
  362. DEV=$i
  363. elif [ -z "$POINT" ]; then
  364. POINT=$i
  365. elif [ -z "$FS" ]; then
  366. FS=$i
  367. break
  368. fi
  369. done
  370. if (! is_mounted $1 $RW); then mount -t $FS -o $RW $DEV $POINT; fi
  371. if (! is_mounted $1 $RW); then mount -t $FS -o $RW,remount $DEV $POINT; fi
  372.  
  373. DEV=
  374. POINT=
  375. FS=
  376. for i in `cat /etc/recovery.fstab | grep "$1"`; do
  377. if [ -z "$POINT" ]; then
  378. POINT=$i
  379. elif [ -z "$FS" ]; then
  380. FS=$i
  381. elif [ -z "$DEV" ]; then
  382. DEV=$i
  383. break
  384. fi
  385. done
  386. if [ "$FS" = "emmc" ]; then
  387. if (! is_mounted $1 $RW); then mount -t ext4 -o $RW $DEV $POINT; fi
  388. if (! is_mounted $1 $RW); then mount -t ext4 -o $RW,remount $DEV $POINT; fi
  389. if (! is_mounted $1 $RW); then mount -t f2fs -o $RW $DEV $POINT; fi
  390. if (! is_mounted $1 $RW); then mount -t f2fs -o $RW,remount $DEV $POINT; fi
  391. else
  392. if (! is_mounted $1 $RW); then mount -t $FS -o $RW $DEV $POINT; fi
  393. if (! is_mounted $1 $RW); then mount -t $FS -o $RW,remount $DEV $POINT; fi
  394. fi
  395. }
  396.  
  397. remount_system_rw() {
  398. if (! is_mounted /system rw); then mount -o rw,remount /system; fi
  399. if (! is_mounted /system rw); then mount -o rw,remount /system /system; fi
  400. if (! is_mounted /system rw); then toolbox_mount /system; fi
  401. }
  402.  
  403. # 'readlink -f' is not reliable across devices/recoveries, this works for our case
  404. resolve_link() {
  405. local RESOLVE=$1
  406. local RESOLVED=
  407. while (true); do
  408. RESOLVED=$(readlink $RESOLVE || echo $RESOLVE)
  409. if [ "$RESOLVE" = "$RESOLVED" ]; then
  410. echo $RESOLVE
  411. break
  412. else
  413. RESOLVE=$RESOLVED
  414. fi
  415. done
  416. }
  417.  
  418. wipe_system_files_if_present() {
  419. GO=false
  420. SYSTEMFILES="
  421. /system/xbin/daemonsu
  422. /system/xbin/sugote
  423. /system/xbin/sugote-mksh
  424. /system/xbin/supolicy
  425. /system/xbin/ku.sud
  426. /system/xbin/.ku
  427. /system/xbin/.su
  428. /system/lib/libsupol.so
  429. /system/lib64/libsupol.so
  430. /system/bin/.ext/.su
  431. /system/etc/init.d/99SuperSUDaemon
  432. /system/etc/.installed_su_daemon
  433. /system/app/Superuser.apk
  434. /system/app/Superuser.odex
  435. /system/app/Superuser
  436. /system/app/SuperUser.apk
  437. /system/app/SuperUser.odex
  438. /system/app/SuperUser
  439. /system/app/superuser.apk
  440. /system/app/superuser.odex
  441. /system/app/superuser
  442. /system/app/Supersu.apk
  443. /system/app/Supersu.odex
  444. /system/app/Supersu
  445. /system/app/SuperSU.apk
  446. /system/app/SuperSU.odex
  447. /system/app/SuperSU
  448. /system/app/supersu.apk
  449. /system/app/supersu.odex
  450. /system/app/supersu
  451. /system/app/VenomSuperUser.apk
  452. /system/app/VenomSuperUser.odex
  453. /system/app/VenomSuperUser
  454. "
  455. for FILE in $SYSTEMFILES; do
  456. if [ -d "$FILE" ]; then GO=true; fi
  457. if [ -f "$FILE" ]; then GO=true; fi
  458. done
  459.  
  460. RMSU=false
  461. if (! $RWSYSTEM); then
  462. if [ -f "/system/xbin/su" ]; then
  463. # only remove /system/xbin/su if it's SuperSU. Could be firmware-included version, we
  464. # do not want to cause remount for that
  465. SUPERSU_CHECK=$(cat /system/xbin/su | grep SuperSU)
  466. if [ $? -eq 0 ]; then
  467. GO=true
  468. RMSU=true
  469. fi
  470. fi
  471.  
  472. SPECIALSYSTEMFILES="
  473. /system/etc/install-recovery_original.sh
  474. /system/bin/install-recovery_original.sh
  475. /system/bin/app_process32_original
  476. /system/bin/app_process32_xposed
  477. /system/bin/app_process64_original
  478. /system/bin/app_process64_xposed
  479. /system/bin/app_process_init
  480. "
  481. for FILE in $SPECIALSYSTEMFILES; do
  482. if [ -d "$FILE" ]; then GO=true; fi
  483. done
  484. fi
  485.  
  486. if ($GO); then
  487. if (! $RWSYSTEM); then
  488. ui_print "- Remounting system r/w :("
  489. remount_system_rw
  490. fi
  491.  
  492. for FILE in $SYSTEMFILES; do
  493. if [ -d "$FILE" ]; then rm -rf $FILE; fi
  494. if [ -f "$FILE" ]; then rm -f $FILE; fi
  495. done
  496.  
  497. if (! $RWSYSTEM); then
  498. # remove wrongly placed /system/xbin/su as well
  499. if ($RMSU); then
  500. rm -f /system/xbin/su
  501. fi
  502.  
  503. # Restore install-recovery and app_process from system install
  504. # Otherwise, our system-less install will fail to boot
  505. if [ -f "/system/etc/install-recovery_original.sh" ]; then
  506. rm -f /system/etc/install-recovery.sh
  507. mv /system/etc/install-recovery_original.sh /system/etc/install-recovery.sh
  508. fi
  509. if [ -f "/system/bin/install-recovery_original.sh" ]; then
  510. rm -f /system/bin/install-recovery.sh
  511. mv /system/bin/install-recovery_original.sh /system/bin/install-recovery.sh
  512. fi
  513. if [ -f "/system/bin/app_process64_original" ]; then
  514. rm -f /system/bin/app_process64
  515. if [ -f "/system/bin/app_process64_xposed" ]; then
  516. ln -s /system/bin/app_process64_xposed /system/bin/app_process64
  517. else
  518. mv /system/bin/app_process64_original /system/bin/app_process64
  519. fi
  520. fi
  521. if [ -f "/system/bin/app_process32_original" ]; then
  522. rm -f /system/bin/app_process32
  523. if [ -f "/system/bin/app_process32_xposed" ]; then
  524. ln -s /system/bin/app_process32_xposed /system/bin/app_process32
  525. else
  526. mv /system/bin/app_process32_original /system/bin/app_process32
  527. fi
  528. fi
  529. if [ -f "/system/bin/app_process64" ]; then
  530. rm /system/bin/app_process
  531. ln -s /system/bin/app_process64 /system/bin/app_process
  532. elif [ -f "/system/bin/app_process32" ]; then
  533. rm /system/bin/app_process
  534. ln -s /system/bin/app_process32 /system/bin/app_process
  535. fi
  536. rm -f /system/bin/app_process_init
  537. fi
  538. fi
  539. }
  540.  
  541. wipe_data_competitors_and_cache() {
  542. rm -f /data/dalvik-cache/*com.noshufou.android.su*
  543. rm -f /data/dalvik-cache/*/*com.noshufou.android.su*
  544. rm -f /data/dalvik-cache/*com.koushikdutta.superuser*
  545. rm -f /data/dalvik-cache/*/*com.koushikdutta.superuser*
  546. rm -f /data/dalvik-cache/*com.mgyun.shua.su*
  547. rm -f /data/dalvik-cache/*/*com.mgyun.shua.su*
  548. rm -f /data/dalvik-cache/*com.m0narx.su*
  549. rm -f /data/dalvik-cache/*/*com.m0narx.su*
  550. rm -f /data/dalvik-cache/*com.kingroot.kinguser*
  551. rm -f /data/dalvik-cache/*/*com.kingroot.kinguser*
  552. rm -f /data/dalvik-cache/*com.kingroot.master*
  553. rm -f /data/dalvik-cache/*/*com.kingroot.master*
  554. rm -f /data/dalvik-cache/*me.phh.superuser*
  555. rm -f /data/dalvik-cache/*/*me.phh.superuser*
  556. rm -f /data/dalvik-cache/*Superuser.apk*
  557. rm -f /data/dalvik-cache/*/*Superuser.apk*
  558. rm -f /data/dalvik-cache/*SuperUser.apk*
  559. rm -f /data/dalvik-cache/*/*SuperUser.apk*
  560. rm -f /data/dalvik-cache/*superuser.apk*
  561. rm -f /data/dalvik-cache/*/*superuser.apk*
  562. rm -f /data/dalvik-cache/*VenomSuperUser.apk*
  563. rm -f /data/dalvik-cache/*/*VenomSuperUser.apk*
  564. rm -f /data/dalvik-cache/*eu.chainfire.supersu*
  565. rm -f /data/dalvik-cache/*/*eu.chainfire.supersu*
  566. rm -f /data/dalvik-cache/*Supersu.apk*
  567. rm -f /data/dalvik-cache/*/*Supersu.apk*
  568. rm -f /data/dalvik-cache/*SuperSU.apk*
  569. rm -f /data/dalvik-cache/*/*SuperSU.apk*
  570. rm -f /data/dalvik-cache/*supersu.apk*
  571. rm -f /data/dalvik-cache/*/*supersu.apk*
  572. rm -f /data/dalvik-cache/*.oat
  573. rm -rf /data/app/com.noshufou.android.su*
  574. rm -rf /data/app/com.koushikdutta.superuser*
  575. rm -rf /data/app/com.mgyun.shua.su*
  576. rm -rf /data/app/com.m0narx.su*
  577. rm -rf /data/app/com.kingroot.kinguser*
  578. rm -rf /data/app/com.kingroot.master*
  579. rm -rf /data/app/me.phh.superuser*
  580. }
  581.  
  582. # check_zero "progress_message" "success message" "failure message" "command"
  583. check_zero() {
  584. if ($CONTINUE); then
  585. if [ ! -z "$1" ]; then ui_print "$1"; fi
  586. eval "$4"
  587. if [ $? -eq 0 ]; then
  588. if [ ! -z "$2" ]; then ui_print "$2"; fi
  589. else
  590. if [ ! -z "$3" ]; then
  591. if [ ! -z "$1" ]; then
  592. ui_print_less "$1"
  593. else
  594. ui_print_less "$UI_PRINT_LAST"
  595. fi
  596. ui_print_always "$3";
  597. fi
  598. CONTINUE=false
  599. fi
  600. fi
  601. }
  602.  
  603. # check_zero_def "progress message" "command"
  604. check_zero_def() {
  605. check_zero "$1" "" "--- Failure, aborting" "$2"
  606. }
  607.  
  608. # find boot image partition if not set already
  609. find_boot_image() {
  610. # expand the detection if we find more, instead of reading from fstab, because unroot
  611. # from the SuperSU APK doesn't have the fstab to read from
  612. if [ -z "$BOOTIMAGE" ]; then
  613. for PARTITION in kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do
  614. BOOTIMAGE=$(readlink /dev/block/by-name/$PARTITION || readlink /dev/block/platform/*/by-name/$PARTITION || readlink /dev/block/platform/*/*/by-name/$PARTITION || readlink /dev/block/by-name/$PARTITION$SLOT_SUFFIX || readlink /dev/block/platform/*/by-name/$PARTITION$SLOT_SUFFIX || readlink /dev/block/platform/*/*/by-name/$PARTITION$SLOT_SUFFIX)
  615. if [ ! -z "$BOOTIMAGE" ]; then break; fi
  616. done
  617. fi
  618.  
  619. # if we found nothing, try the recovery.fstab (TWRP), and hope named partitions are actually
  620. # available from Android for APK unroot; this is know to occur, suspected to be a TWRP bug
  621. if [ -z "$BOOTIMAGE" ]; then
  622. BOOTIMAGE=$(cat /etc/recovery.fstab | grep -v "#" | grep -m 1 "^/boot" | tr -s " " | cut -f 3 -d " " | grep "/dev/block")
  623. fi
  624. }
  625.  
  626. # use only on 6.0+, tries to read current boot image and detect if we can do a system install
  627. # without any boot image patching. Requirements:
  628. # - /data readable
  629. # - not pre-patched by SuperSU
  630. # - dm-verity disabled
  631. # - init loads from /data/security/current/sepolicy
  632. # - sepolicy has init load_policy or permissive init
  633. # It symlink/patches the relevant files to /data, and sets SYSTEMLESS variable if not already set
  634. detect_systemless_required() {
  635. OLD_SYSTEMLESS=$SYSTEMLESS
  636. if [ "$OLD_SYSTEMLESS" = "detect" ]; then
  637. # we don't override a pre-set true/false value
  638. SYSTEMLESS=true
  639. fi
  640.  
  641. # check /data mounted
  642. if (! is_mounted /data); then
  643. return
  644. fi
  645.  
  646. # find boot image partition
  647. find_boot_image
  648.  
  649. CONTINUE=true
  650. if [ -z "$BOOTIMAGE" ]; then
  651. # no boot image partition detected, abort
  652. return
  653. fi
  654.  
  655. # extract ramdisk from boot image
  656. rm -rf /sutmp
  657. mkdir /sutmp
  658.  
  659. check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --bootimg-extract-ramdisk $BOOTIMAGE /sutmp/ramdisk.packed"
  660. check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --ungzip /sutmp/ramdisk.packed /sutmp/ramdisk"
  661. if (! $CONTINUE); then return; fi
  662.  
  663. # detect SuperSU patch
  664. LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --patch-test /sutmp/ramdisk
  665. if [ $? -ne 0 ]; then
  666. return
  667. fi
  668.  
  669. # detect dm-verity in use
  670. if (! $SYSTEM_ROOT_USED); then
  671. for i in `LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-ls /sutmp/ramdisk | grep fstab`; do
  672. rm -f /sutmp/fstab
  673.  
  674. check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-extract /sutmp/ramdisk $i /sutmp/fstab"
  675. if (! $CONTINUE); then return; fi
  676.  
  677. VERIFY=$(cat /sutmp/fstab | grep verify | grep system)
  678. if [ $? -eq 0 ]; then
  679. # verify flag found, dm-verity probably enabled, modifying /system may prevent boot
  680. return
  681. fi
  682. done
  683. else
  684. for i in `ls /system_root | grep fstab`; do
  685. VERIFY=$(cat /system_root/$i | grep verify | grep system)
  686. if [ $? -eq 0 ]; then
  687. # verify flag found, dm-verity probably enabled, modifying /system may prevent boot
  688. return
  689. fi
  690. done
  691. fi
  692.  
  693. # detect init loading from /data/security/current/sepolicy
  694. if (! $SYSTEM_ROOT_USED); then
  695. check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-extract /sutmp/ramdisk ${CPIO_PREFIX}init /sutmp/init"
  696. if (! $CONTINUE); then return; fi
  697. else
  698. cat /system_root/init > /sutmp/init
  699. fi
  700.  
  701. if (cat /sutmp/init 2>/dev/null | grep "/data/security/current/sepolicy" >/dev/null); then
  702. # this init doesn't load from the default sepolicy override location
  703. return
  704. fi
  705.  
  706. if (cat /sutmp/init 2>/dev/null | grep "selinux.reload_policy" >/dev/null); then
  707. # this init doesn't react to setprop selinux.reload_policy
  708. return
  709. fi
  710.  
  711. # detect selinux.reload_policy being set
  712. if (! $SYSTEM_ROOT_USED); then
  713. check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-extract /sutmp/ramdisk ${CPIO_PREFIX}init.rc /sutmp/init.rc"
  714. if (! $CONTINUE); then return; fi
  715. else
  716. cat /system_root/init.rc > /sutmp/init.rc
  717. fi
  718.  
  719. if (cat /sutmp/init.rc 2>/dev/null | grep "setprop selinux.reload_policy 1" >/dev/null); then
  720. # this init.rc doesn't setprop selinux.reload_policy 1
  721. return
  722. fi
  723.  
  724. # extract sepolicy
  725. if (! $SYSTEM_ROOT_USED); then
  726. check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-extract /sutmp/ramdisk ${CPIO_PREFIX}sepolicy /sutmp/sepolicy"
  727. if (! $CONTINUE); then return; fi
  728. else
  729. cat /system_root/sepolicy > /sutmp/sepolicy
  730. fi
  731.  
  732. GO=false
  733.  
  734. # detect init permissive
  735. if (! $GO); then
  736. INIT_PERMISSIVE=$(LD_LIBRARY_PATH=$RAMDISKLIB $BIN/supolicy --dumpav /sutmp/sepolicy | grep "[TYPE]" | grep " init (PERMISSIVE) ")
  737. if [ $? -eq 0 ]; then
  738. GO=true
  739. fi
  740. fi
  741.  
  742. # detect init load_policy
  743. if (! $GO); then
  744. INIT_LOAD_POLICY=$(LD_LIBRARY_PATH=$RAMDISKLIB $BIN/supolicy --dumpav /sutmp/sepolicy | grep "[AV]" | grep " ALLOW " | grep " init-->kernel (security) " | grep "load_policy")
  745. if [ $? -eq 0 ]; then
  746. GO=true
  747. fi
  748. fi
  749.  
  750. # copy files to /data
  751. if (! $GO); then return; fi
  752.  
  753. rm -rf /data/security/*
  754. mkdir /data/security/current
  755. set_perm 1000 1000 0755 /data/security/current u:object_r:security_file:s0
  756.  
  757. LD_LIBRARY_PATH=$RAMDISKLIB $BIN/supolicy --file /sutmp/sepolicy /data/security/current/sepolicy --sdk=$API
  758. set_perm 1000 1000 0644 /data/security/current/sepolicy u:object_r:security_file:s0
  759.  
  760. for i in seapp_contexts file_contexts file_contexts.bin property_contexts service_contexts selinux_version; do
  761. ln -s /$i /data/security/current/$i
  762. done
  763.  
  764. ln -s /system/etc/security/mac_permissions.xml /data/security/current/mac_permissions.xml
  765.  
  766. # if we reach this point, we can do a system install
  767. if [ "$OLD_SYSTEMLESS" = "detect" ]; then
  768. # we don't override a pre-set true/false value
  769. SYSTEMLESS=false
  770. fi
  771. }
  772.  
  773. ui_print " "
  774. ui_print "*****************"
  775. ui_print_always "SuperSU installer"
  776. ui_print "*****************"
  777.  
  778. # fix slow RNG on some devices, which may cause installation to cause weeks (kernel/toybox issue)
  779.  
  780. mount -o bind /dev/urandom /dev/random
  781.  
  782. # detect slot-based partition layout
  783.  
  784. SLOT_USED=false
  785. SLOT_SUFFIX=$(getprop ro.boot.slot_suffix 2>/dev/null)
  786. if [ -z "$SLOT_SUFFIX" ]; then
  787. for i in `cat /proc/cmdline`; do
  788. if [ "${i%=*}" = "androidboot.slot_suffix" ]; then
  789. SLOT_SUFFIX=${i#*=}
  790. break
  791. fi
  792. done
  793. fi
  794. if [ ! -z "$SLOT_SUFFIX" ]; then
  795. SLOT_USED=true
  796. fi
  797.  
  798. # detect system partition
  799.  
  800. # /fstab.* for stock images, which can contain slotselect:
  801. # initramfs_root: /dev/block/bootdevice/by-name/system /system ext4 ro,barrier=1,discard wait[,slotselect][,verify]
  802. # system_root-boot: /dev/block/bootdevice/by-name/system / ext4 ro,barrier=1,discard wait[,slotselect],verify
  803. # system_root-recovery: /dev/block/bootdevice/by-name/system /system_root ext4 ro,barrier=1,discard wait[,slotselect]
  804. #
  805. # /etc/fstab for TWRP, which will contain $SLOT_SUFFIX, if any
  806. # /dev/block/bootdevice/by-name/system_a /system ext4 rw 0 0
  807. SYSTEM_DEVICE=$(cat /fstab.* /etc/fstab | grep -v "#" | grep -m 1 " / \| /system \| /system_root " | tr -s " ");
  808. if ($SLOT_USED); then
  809. if (! `echo $SYSTEM_DEVICE | grep slotselect >/dev/null 2>&1`); then
  810. if (! `echo $SYSTEM_DEVICE | grep "$SLOT_SUFFIX" >/dev/null 2>&1`); then
  811. SLOT_USED=false
  812. fi
  813. fi
  814. fi
  815. for i in $SYSTEM_DEVICE; do
  816. if (! `echo $SYSTEM_DEVICE | grep "$SLOT_SUFFIX" >/dev/null 2>&1`); then
  817. SYSTEM_DEVICE=$i$SLOT_SUFFIX
  818. else
  819. SYSTEM_DEVICE=$i
  820. fi
  821. break
  822. done
  823.  
  824. ui_print "- Mounting /system, /data and rootfs"
  825.  
  826. SYSTEM_ROOT_USED=false
  827. HAD_SYSTEM=false
  828. HAD_SYSTEM_RW=false
  829. HAD_SYSTEM_ROOT=false
  830. if (`mount | grep " /system " >/dev/null 2>&1`); then
  831. HAD_SYSTEM=true
  832. if (`mount | grep " /system " | grep "rw" >/dev/null 2>&1`); then
  833. HAD_SYSTEM_RW=true
  834. fi
  835. fi
  836. if (`mount | grep " /system_root " >/dev/null 2>&1`); then
  837. HAD_SYSTEM_ROOT=true
  838. fi
  839.  
  840. if (! $HAD_SYSTEM); then
  841. if (! $HAD_SYSTEM_ROOT); then
  842. mount -o ro /system
  843. toolbox_mount /system ro
  844. fi
  845. fi
  846.  
  847. if [ -f "/system/sepolicy" ]; then
  848. SYSTEM_ROOT_USED=true
  849. elif [ -f "/system_root/sepolicy" ]; then
  850. SYSTEM_ROOT_USED=true
  851. fi
  852.  
  853. if ($SYSTEM_ROOT_USED); then
  854. if (! $HAD_SYSTEM_ROOT); then
  855. umount /system
  856. mkdir /system_root
  857. mount -o ro $SYSTEM_DEVICE /system_root
  858. mount -o bind /system_root/system /system
  859. fi
  860. fi
  861.  
  862. mount /data
  863. toolbox_mount /data
  864. mount -o rw,remount /
  865. mount -o rw,remount / /
  866.  
  867. if [ -z "$BIN" ]; then
  868. # TWRP went full retard
  869. if [ ! -f "/sbin/unzip" ]; then
  870. ui_print "- BAD RECOVERY DETECTED, NO UNZIP, ABORTING"
  871. exit 1
  872. fi
  873. fi
  874.  
  875. if ($SYSTEM_ROOT_USED); then
  876. CPIO_PREFIX=boot/
  877. else
  878. CPIO_PREFIX=
  879. fi
  880.  
  881. if [ -z "$NOOVERRIDE" ]; then
  882. # read override variables
  883. getvar SYSTEMLESS
  884. getvar PATCHBOOTIMAGE
  885. getvar BOOTIMAGE
  886. getvar STOCKBOOTIMAGE
  887. getvar BINDSYSTEMXBIN
  888. getvar PERMISSIVE
  889. getvar KEEPVERITY
  890. getvar KEEPFORCEENCRYPT
  891. getvar REMOVEENCRYPTABLE
  892. getvar FRP
  893. fi
  894. if [ -z "$SYSTEMLESS" ]; then
  895. # detect if we need systemless, based on Android version and boot image
  896. SYSTEMLESS=detect
  897. fi
  898. if [ -z "$PATCHBOOTIMAGE" ]; then
  899. # only if we end up doing a system-less install
  900. PATCHBOOTIMAGE=true
  901. fi
  902. if [ -z "$BINDSYSTEMXBIN" ]; then
  903. # causes launch_daemonsu to bind over /system/xbin, disabled by default
  904. BINDSYSTEMXBIN=false
  905. fi
  906. if [ -z "$PERMISSIVE" ]; then
  907. # don't make everything fake-permissive
  908. PERMISSIVE=false
  909. fi
  910. if [ -z "$KEEPVERITY" ]; then
  911. # we don't keep dm-verity by default
  912. KEEPVERITY=false
  913. fi
  914. if [ -z "$KEEPFORCEENCRYPT" ]; then
  915. # we don't keep forceencrypt by default
  916. KEEPFORCEENCRYPT=false
  917. fi
  918. if [ -z "$REMOVEENCRYPTABLE" ]; then
  919. # we don't remove encryptable by default
  920. REMOVEENCRYPTABLE=false
  921. fi
  922. if [ -z "$FRP" ]; then
  923. # enable FRP if we're using slots, implying large enough boot image
  924. FRP=$SLOT_USED
  925. fi
  926.  
  927. API=$(cat /system/build.prop | grep "ro.build.version.sdk=" | dd bs=1 skip=21 count=2)
  928. ABI=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi=" | dd bs=1 skip=19 count=3)
  929. ABILONG=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi=" | dd bs=1 skip=19)
  930. ABI2=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi2=" | dd bs=1 skip=20 count=3)
  931. SUMOD=06755
  932. SUPOLICY=false
  933. INSTALL_RECOVERY_CONTEXT=u:object_r:system_file:s0
  934. MKSH=/system/bin/mksh
  935. PIE=
  936. SU=su
  937. ARCH=arm
  938. APKFOLDER=false
  939. APKNAME=/system/app/Superuser.apk
  940. APPPROCESS=false
  941. APPPROCESS64=false
  942. SYSTEMLIB=/system/lib
  943. RAMDISKLIB=$SYSTEMLIB
  944. RWSYSTEM=true
  945. INIT_SUPERSU_RC=init.supersu.rc
  946.  
  947. if [ "$API" -le "21" ]; then
  948. # needed for some intermediate AOSP verions
  949.  
  950. remount_system_rw
  951.  
  952. cat /system/bin/toolbox > /system/toolbox
  953. chmod 0755 /system/toolbox
  954. ch_con /system/toolbox
  955. fi
  956.  
  957. if [ "$ABI" = "x86" ]; then ARCH=x86; fi;
  958. if [ "$ABI2" = "x86" ]; then ARCH=x86; fi;
  959. if [ "$API" -eq "$API" ]; then
  960. if [ "$API" -ge "17" ]; then
  961. PIE=.pie
  962. if [ "$ARCH" = "x86" ]; then SU=su.pie; fi;
  963. if [ "$ABILONG" = "armeabi-v7a" ]; then ARCH=armv7; fi;
  964. if [ "$ABI" = "mip" ]; then ARCH=mips; fi;
  965. if [ "$ABILONG" = "mips" ]; then ARCH=mips; fi;
  966. fi
  967. if [ "$API" -ge "18" ]; then
  968. SUMOD=0755
  969. fi
  970. if [ "$API" -ge "20" ]; then
  971. if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
  972. if [ "$ABILONG" = "mips64" ]; then ARCH=mips64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
  973. if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
  974. APKFOLDER=true
  975. APKNAME=/system/app/SuperSU/SuperSU.apk
  976. fi
  977. if [ "$API" -ge "19" ]; then
  978. SUPOLICY=true
  979. if [ "$(LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox ls -lZ /system/bin/toolbox | grep toolbox_exec > /dev/null; echo $?)" -eq "0" ]; then
  980. INSTALL_RECOVERY_CONTEXT=u:object_r:toolbox_exec:s0
  981. fi
  982. fi
  983. if [ "$API" -ge "21" ]; then
  984. APPPROCESS=true
  985. fi
  986. if [ "$API" -ge "24" ]; then
  987. INIT_SUPERSU_RC=init.supersu.rc.24
  988. fi
  989. fi
  990. if [ ! -f $MKSH ]; then
  991. MKSH=/system/bin/sh
  992. fi
  993.  
  994. #ui_print "DBG [$API] [$ABI] [$ABI2] [$ABILONG] [$ARCH] [$MKSH]"
  995.  
  996. if [ -z "$BIN" ]; then
  997. ui_print "- Extracting files"
  998.  
  999. cd /tmp
  1000. mkdir supersu
  1001. cd supersu
  1002.  
  1003. unzip -o "$ZIP"
  1004.  
  1005. BIN=/tmp/supersu/$ARCH
  1006. COM=/tmp/supersu/common
  1007. fi
  1008.  
  1009. # execute binaries from ramdisk
  1010. chmod -R 0755 $BIN/*
  1011. RAMDISKLIB=$BIN:$SYSTEMLIB
  1012.  
  1013. if [ "$API" -ge "19" ]; then
  1014. # 4.4+: permissive all teh things
  1015. LD_LIBRARY_PATH=$RAMDISKLIB $BIN/supolicy --live "permissive *"
  1016. fi
  1017.  
  1018. SAMSUNG=false
  1019. MOTOROLA=false
  1020. if [ "$API" -eq "$API" ]; then
  1021. if (cat /system/build.prop /default.prop 2>/dev/null | grep "ro.build.fingerprint=" | grep -i "samsung" >/dev/null); then
  1022. SAMSUNG=true
  1023. fi
  1024. if (cat /system/build.prop /default.prop 2>/dev/null | grep "ro.build.fingerprint=" | grep -i "motorola" >/dev/null); then
  1025. MOTOROLA=true
  1026. fi
  1027.  
  1028. if [ "$API" -ge "23" ]; then
  1029. # 6.0+
  1030.  
  1031. DETECT_SYSTEMLESS_REQUIRED=true
  1032.  
  1033. # Motorola
  1034. # Stock Motorola firmwares have all the symptoms of firmwares
  1035. # that should be able to work in system-mode, but in practise
  1036. # it doesn't work - reason unknown. So system mode is not used
  1037. # unless SYSTEMLESS=false is explicitly set.
  1038. if ($MOTOROLA); then
  1039. if [ "$SYSTEMLESS" = "detect" ]; then
  1040. SYSTEMLESS=true
  1041. DETECT_SYSTEMLESS_REQUIRED=false
  1042. fi
  1043. fi
  1044.  
  1045. if ($DETECT_SYSTEMLESS_REQUIRED); then
  1046. ui_print "- Detecting system compatibility"
  1047. detect_systemless_required
  1048. fi
  1049.  
  1050. if ($SYSTEMLESS); then
  1051. RWSYSTEM=false
  1052. fi
  1053. elif [ "$API" -ge "21" ]; then
  1054. # 5.1/Samsung
  1055. # On 5.0, auto-detect sets systemless only for 5.1/Samsung
  1056. # But we allow SYSTEMLESS=true override for 3rd party mods
  1057. # - that doesn't officially work, though!
  1058. if [ "$SYSTEMLESS" = "detect" ]; then
  1059. SYSTEMLESS=false
  1060. if [ "$API" -ge "22" ]; then
  1061. if ($SAMSUNG); then
  1062. SYSTEMLESS=true
  1063. fi
  1064. fi
  1065. fi
  1066.  
  1067. # Pre-6.0/Other
  1068. if ($SYSTEMLESS); then
  1069. RWSYSTEM=false
  1070. fi
  1071. fi
  1072. fi
  1073.  
  1074. # Do not use SYSTEMLESS after this point, but refer to RWSYSTEM
  1075.  
  1076. if ($RWSYSTEM); then
  1077. ui_print "- System mode"
  1078.  
  1079. remount_system_rw
  1080.  
  1081. ui_print "- Disabling OTA survival"
  1082. chmod 0755 $BIN/chattr$PIE
  1083. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/bin/su
  1084. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/su
  1085. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/bin/.ext/.su
  1086. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/sbin/su
  1087. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /vendor/sbin/su
  1088. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /vendor/bin/su
  1089. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /vendor/xbin/su
  1090. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/daemonsu
  1091. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/sugote
  1092. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/sugote_mksh
  1093. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/supolicy
  1094. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/ku.sud
  1095. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/.ku
  1096. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/.su
  1097. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/lib/libsupol.so
  1098. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/lib64/libsupol.so
  1099. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/etc/install-recovery.sh
  1100. LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/bin/install-recovery.sh
  1101.  
  1102. ui_print "- Removing old files"
  1103.  
  1104. if [ -f "/system/bin/install-recovery.sh" ]; then
  1105. if [ ! -f "/system/bin/install-recovery_original.sh" ]; then
  1106. mv /system/bin/install-recovery.sh /system/bin/install-recovery_original.sh
  1107. ch_con /system/bin/install-recovery_original.sh
  1108. fi
  1109. fi
  1110. if [ -f "/system/etc/install-recovery.sh" ]; then
  1111. if [ ! -f "/system/etc/install-recovery_original.sh" ]; then
  1112. mv /system/etc/install-recovery.sh /system/etc/install-recovery_original.sh
  1113. ch_con /system/etc/install-recovery_original.sh
  1114. fi
  1115. fi
  1116.  
  1117. # only wipe these files in /system install, so not part of the wipe_ functions
  1118.  
  1119. rm -f /system/bin/install-recovery.sh
  1120. rm -f /system/etc/install-recovery.sh
  1121.  
  1122. rm -f /system/bin/su
  1123. rm -f /system/xbin/su
  1124. rm -f /system/sbin/su
  1125. rm -f /vendor/sbin/su
  1126. rm -f /vendor/bin/su
  1127. rm -f /vendor/xbin/su
  1128.  
  1129. rm -rf /data/app/eu.chainfire.supersu-*
  1130. rm -rf /data/app/eu.chainfire.supersu.apk
  1131.  
  1132. wipe_system_files_if_present
  1133. wipe_data_competitors_and_cache
  1134.  
  1135. rm /data/su.img
  1136. rm /cache/su.img
  1137.  
  1138. ui_print "- Creating space"
  1139. if ($APKFOLDER); then
  1140. if [ -f "/system/app/Maps/Maps.apk" ]; then
  1141. cp /system/app/Maps/Maps.apk /Maps.apk
  1142. rm /system/app/Maps/Maps.apk
  1143. fi
  1144. if [ -f "/system/app/GMS_Maps/GMS_Maps.apk" ]; then
  1145. cp /system/app/GMS_Maps/GMS_Maps.apk /GMS_Maps.apk
  1146. rm /system/app/GMS_Maps/GMS_Maps.apk
  1147. fi
  1148. if [ -f "/system/app/YouTube/YouTube.apk" ]; then
  1149. cp /system/app/YouTube/YouTube.apk /YouTube.apk
  1150. rm /system/app/YouTube/YouTube.apk
  1151. fi
  1152. else
  1153. if [ -f "/system/app/Maps.apk" ]; then
  1154. cp /system/app/Maps.apk /Maps.apk
  1155. rm /system/app/Maps.apk
  1156. fi
  1157. if [ -f "/system/app/GMS_Maps.apk" ]; then
  1158. cp /system/app/GMS_Maps.apk /GMS_Maps.apk
  1159. rm /system/app/GMS_Maps.apk
  1160. fi
  1161. if [ -f "/system/app/YouTube.apk" ]; then
  1162. cp /system/app/YouTube.apk /YouTube.apk
  1163. rm /system/app/YouTube.apk
  1164. fi
  1165. fi
  1166.  
  1167. ui_print "- Placing files"
  1168.  
  1169. mkdir /system/bin/.ext
  1170. set_perm 0 0 0777 /system/bin/.ext
  1171. cp_perm 0 0 $SUMOD $BIN/$SU /system/bin/.ext/.su
  1172. cp_perm 0 0 $SUMOD $BIN/$SU /system/xbin/su
  1173. cp_perm 0 0 0755 $BIN/$SU /system/xbin/daemonsu
  1174. if ($SUPOLICY); then
  1175. cp_perm 0 0 0755 $BIN/supolicy /system/xbin/supolicy
  1176. cp_perm 0 0 0644 $BIN/libsupol.so $SYSTEMLIB/libsupol.so
  1177. fi
  1178. if ($APKFOLDER); then
  1179. mkdir /system/app/SuperSU
  1180. set_perm 0 0 0755 /system/app/SuperSU
  1181. fi
  1182. cp_perm 0 0 0644 $COM/Superuser.apk $APKNAME
  1183. cp_perm 0 0 0755 $COM/install-recovery.sh /system/etc/install-recovery.sh
  1184. ln_con /system/etc/install-recovery.sh /system/bin/install-recovery.sh
  1185. if ($APPPROCESS); then
  1186. rm /system/bin/app_process
  1187. ln_con /system/xbin/daemonsu /system/bin/app_process
  1188. if ($APPPROCESS64); then
  1189. if [ ! -f "/system/bin/app_process64_original" ]; then
  1190. mv /system/bin/app_process64 /system/bin/app_process64_original
  1191. else
  1192. rm /system/bin/app_process64
  1193. fi
  1194. ln_con /system/xbin/daemonsu /system/bin/app_process64
  1195. if [ ! -f "/system/bin/app_process_init" ]; then
  1196. cp_perm 0 2000 0755 /system/bin/app_process64_original /system/bin/app_process_init
  1197. fi
  1198. else
  1199. if [ ! -f "/system/bin/app_process32_original" ]; then
  1200. mv /system/bin/app_process32 /system/bin/app_process32_original
  1201. else
  1202. rm /system/bin/app_process32
  1203. fi
  1204. ln_con /system/xbin/daemonsu /system/bin/app_process32
  1205. if [ ! -f "/system/bin/app_process_init" ]; then
  1206. cp_perm 0 2000 0755 /system/bin/app_process32_original /system/bin/app_process_init
  1207. fi
  1208. fi
  1209. fi
  1210. cp_perm 0 0 0744 $COM/99SuperSUDaemon /system/etc/init.d/99SuperSUDaemon
  1211. echo 1 > /system/etc/.installed_su_daemon
  1212. set_perm 0 0 0644 /system/etc/.installed_su_daemon
  1213.  
  1214. ui_print "- Restoring files"
  1215. if ($APKFOLDER); then
  1216. if [ -f "/Maps.apk" ]; then
  1217. cp_perm 0 0 0644 /Maps.apk /system/app/Maps/Maps.apk
  1218. rm /Maps.apk
  1219. fi
  1220. if [ -f "/GMS_Maps.apk" ]; then
  1221. cp_perm 0 0 0644 /GMS_Maps.apk /system/app/GMS_Maps/GMS_Maps.apk
  1222. rm /GMS_Maps.apk
  1223. fi
  1224. if [ -f "/YouTube.apk" ]; then
  1225. cp_perm 0 0 0644 /YouTube.apk /system/app/YouTube/YouTube.apk
  1226. rm /YouTube.apk
  1227. fi
  1228. else
  1229. if [ -f "/Maps.apk" ]; then
  1230. cp_perm 0 0 0644 /Maps.apk /system/app/Maps.apk
  1231. rm /Maps.apk
  1232. fi
  1233. if [ -f "/GMS_Maps.apk" ]; then
  1234. cp_perm 0 0 0644 /GMS_Maps.apk /system/app/GMS_Maps.apk
  1235. rm /GMS_Maps.apk
  1236. fi
  1237. if [ -f "/YouTube.apk" ]; then
  1238. cp_perm 0 0 0644 /YouTube.apk /system/app/YouTube.apk
  1239. rm /YouTube.apk
  1240. fi
  1241. fi
  1242.  
  1243. ui_print "- Post-installation script"
  1244. rm /system/toybox
  1245. rm /system/toolbox
  1246. LD_LIBRARY_PATH=$SYSTEMLIB /system/xbin/su --install
  1247. else
  1248. ui_print "- System-less mode, boot image support required"
  1249.  
  1250. SUIMG=/data/su.img
  1251. HAVEDATA=true
  1252. if (! is_mounted /data); then
  1253. SUIMG=/cache/su.img
  1254. HAVEDATA=false
  1255. fi
  1256.  
  1257. ui_print "- Creating image"
  1258.  
  1259. # we want a 96M image, for SuperSU files and potential mods such as systemless xposed
  1260. # attempt smaller sizes on failure, and hope the launch_daemonsu.sh script succeeds
  1261. # in resizing to 96M later
  1262. for SUIMGSIZE in 96M 64M 32M 16M; do
  1263. if [ ! -f "$SUIMG" ]; then make_ext4fs -l $SUIMGSIZE -a /su -S $COM/file_contexts_image $SUIMG; fi
  1264. if [ ! -f "$SUIMG" ]; then LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/make_ext4fs -l $SUIMGSIZE -a /su -S $COM/file_contexts_image $SUIMG; fi
  1265. set_perm 0 0 0600 /data/su.img u:object_r:system_data_file:s0
  1266. done
  1267.  
  1268. if [ -f "$SUIMG" ]; then
  1269. LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/e2fsck -p -f $SUIMG
  1270. fi
  1271.  
  1272. ui_print "- Mounting image"
  1273.  
  1274. mkdir /su
  1275.  
  1276. # 'losetup -f' is unreliable across devices/recoveries
  1277. LOOPDEVICE=
  1278. for LOOP in 0 1 2 3 4 5 6 7; do
  1279. if (! is_mounted /su); then
  1280. LOOPDEVICE=/dev/block/loop$LOOP
  1281. HAVE_LOOPDEVICE=false
  1282. if [ -f "$LOOPDEVICE" ]; then
  1283. HAVE_LOOPDEVICE=true
  1284. elif [ -b "$LOOPDEVICE" ]; then
  1285. HAVE_LOOPDEVICE=true;
  1286. fi
  1287. if (! $HAVE_LOOPDEVICE); then
  1288. mknod $LOOPDEVICE b 7 $LOOP
  1289. fi
  1290. losetup $LOOPDEVICE $SUIMG
  1291. if [ "$?" -eq "0" ]; then
  1292. mount -t ext4 -o loop $LOOPDEVICE /su
  1293. if (! is_mounted /su); then
  1294. /system/bin/toolbox mount -t ext4 -o loop $LOOPDEVICE /su
  1295. fi
  1296. if (! is_mounted /su); then
  1297. /system/bin/toybox mount -t ext4 -o loop $LOOPDEVICE /su
  1298. fi
  1299. fi
  1300. if (is_mounted /su); then
  1301. break;
  1302. fi
  1303. fi
  1304. done
  1305.  
  1306. ui_print "- Creating paths"
  1307.  
  1308. ch_con /su
  1309.  
  1310. mkdir /su/bin
  1311. set_perm 0 0 0751 /su/bin
  1312. mkdir /su/xbin
  1313. set_perm 0 0 0755 /su/xbin
  1314. mkdir /su/lib
  1315. set_perm 0 0 0755 /su/lib
  1316. mkdir /su/etc
  1317. set_perm 0 0 0755 /su/etc
  1318. mkdir /su/su.d
  1319. set_perm 0 0 0700 /su/su.d
  1320.  
  1321. ui_print "- Removing old files"
  1322.  
  1323. wipe_system_files_if_present
  1324. wipe_data_competitors_and_cache
  1325.  
  1326. rm -rf /su/bin/app_process
  1327. rm -rf /su/bin/sush
  1328. rm -rf /su/bin/daemonsu
  1329. rm -rf /su/bin/daemonsu_*
  1330. rm -rf /su/bin/su
  1331. rm -rf /su/bin/su_*
  1332. rm -rf /su/bin/supolicy
  1333. rm -rf /su/bin/supolicy_*
  1334. rm -rf /su/lib/libsupol.so
  1335. rm -rf /su/lib/libsupol_*
  1336. rm -rf /su/bin/sukernel
  1337.  
  1338. ui_print "- Placing files"
  1339.  
  1340. # Copy binaries and utilities
  1341. cp_perm 0 0 0755 $BIN/$SU /su/bin/su
  1342. cp_perm 0 0 0755 $BIN/$SU /su/bin/daemonsu
  1343. ln_con /su/bin/su /su/bin/supolicy
  1344. cp_perm 0 0 0755 $BIN/supolicy /su/bin/supolicy_wrapped
  1345. cp_perm 0 0 0644 $BIN/libsupol.so /su/lib/libsupol.so
  1346. cp_perm 0 0 0755 $BIN/sukernel /su/bin/sukernel
  1347.  
  1348. # Copy APK, installation is done by /sbin/launch_daemonsu.sh
  1349. if ($HAVEDATA); then
  1350. cp_perm 1000 1000 0600 $COM/Superuser.apk /data/SuperSU.apk
  1351.  
  1352. # Wipe /data/security to prevent SELinux policy override
  1353. # Important to keep the folder itself
  1354. rm -rf /data/security/*
  1355. else
  1356. cp_perm 1000 1000 0600 $COM/Superuser.apk /cache/SuperSU.apk
  1357. fi
  1358.  
  1359. # Fix Samsung deep sleep issue. Affects enough millions of users to include.
  1360. if ($SAMSUNG); then
  1361. cp_perm 0 0 0700 $COM/000000deepsleep /su/su.d/000000deepsleep
  1362. fi
  1363.  
  1364. if ($BINDSYSTEMXBIN); then
  1365. mkdir /su/xbin_bind
  1366. set_perm 0 0 0755 /su/xbin_bind
  1367. else
  1368. rm -rf /su/xbin_bind
  1369. fi
  1370.  
  1371. if ($PATCHBOOTIMAGE); then
  1372. ui_print " "
  1373. ui_print "******************"
  1374. ui_print_always "Boot image patcher"
  1375. ui_print "******************"
  1376.  
  1377. ui_print "- Finding boot image"
  1378. find_boot_image
  1379.  
  1380. CONTINUE=true
  1381. if [ -z "$BOOTIMAGE" ]; then
  1382. ui_print_less "$UI_PRINT_LAST"
  1383. ui_print_always "--- Boot image: not found, aborting"
  1384. CONTINUE=false
  1385. else
  1386. ui_print "--- Boot image: $BOOTIMAGE"
  1387. fi
  1388.  
  1389. if [ -z "$STOCKBOOTIMAGE" ]; then
  1390. STOCKBOOTIMAGE=$BOOTIMAGE
  1391. fi
  1392.  
  1393. rm -rf /sutmp
  1394. mkdir /sutmp
  1395.  
  1396. IMAGETYPE=android
  1397. LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-type $BOOTIMAGE
  1398. if [ $? -eq 2 ]; then
  1399. IMAGETYPE=chromeos
  1400. fi
  1401.  
  1402. check_zero_def "- Extracting ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-extract-ramdisk $BOOTIMAGE /sutmp/ramdisk.packed"
  1403. check_zero_def "- Decompressing ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --ungzip /sutmp/ramdisk.packed /sutmp/ramdisk"
  1404.  
  1405. if ($CONTINUE); then
  1406. ui_print "- Checking patch status"
  1407. LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --patch-test /sutmp/ramdisk
  1408. if [ $? -ne 0 ]; then
  1409. ui_print "--- Already patched, attempting to find stock backup"
  1410.  
  1411. if ($CONTINUE); then
  1412. LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --restore /sutmp/ramdisk /sutmp/stock_boot.img
  1413. if [ $? -ne 0 ]; then
  1414. ui_print_always "--- Stock restore failed, attempting ramdisk restore"
  1415. CONTINUE=false
  1416. else
  1417. ui_print "--- Stock backup restored"
  1418. STOCKBOOTIMAGE=/sutmp/stock_boot.img
  1419. fi
  1420. fi
  1421.  
  1422. check_zero_def "- Extracting ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-extract-ramdisk /sutmp/stock_boot.img /sutmp/ramdisk.packed"
  1423. check_zero_def "- Decompressing ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --ungzip /sutmp/ramdisk.packed /sutmp/ramdisk"
  1424. check_zero "- Checking patch status" "" "--- Already patched, attempting ramdisk restore" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --patch-test /sutmp/ramdisk"
  1425.  
  1426. if (! $CONTINUE); then
  1427. LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-restore /sutmp/ramdisk /sutmp/ramdisk
  1428. if [ $? -ne 0 ]; then
  1429. ui_print_always "--- Ramdisk restore failed, aborting"
  1430. else
  1431. ui_print "--- Ramdisk backup restored (OTA impossible)"
  1432. CONTINUE=true
  1433. fi
  1434. check_zero "- Checking patch status" "" "--- Already patched, aborting" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --patch-test /sutmp/ramdisk"
  1435. fi
  1436. else
  1437. ui_print "- Creating backup"
  1438. rm /data/stock_boot_*.img
  1439. rm /data/stock_boot_*.img.gz
  1440. rm /cache/stock_boot_*.img
  1441. rm /cache/stock_boot_*.img.gz
  1442. LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --backup $BOOTIMAGE
  1443. if [ $? -ne 0 ]; then
  1444. ui_print "--- Backup failed"
  1445. fi
  1446. fi
  1447. fi
  1448.  
  1449. if ($CONTINUE); then
  1450. cp_perm 0 0 0644 /sutmp/ramdisk /sutmp/ramdisk.original
  1451.  
  1452. if ($SYSTEM_ROOT_USED); then
  1453. check_zero_def "- Importing system_root" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-import-system-root /sutmp/ramdisk /sutmp/ramdisk"
  1454. fi
  1455. fi
  1456.  
  1457. if ($CONTINUE); then
  1458. ui_print "- Patching sepolicy"
  1459.  
  1460. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-extract /sutmp/ramdisk ${CPIO_PREFIX}sepolicy /sutmp/sepolicy"
  1461.  
  1462. if ($CONTINUE); then
  1463. if ($PERMISSIVE); then
  1464. # once for default SuperSU rules, once to make everything fake-permissive, both are needed
  1465. LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/supolicy --file /sutmp/sepolicy /sutmp/sepolicy.tmp --sdk=$API
  1466. LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/supolicy --file /sutmp/sepolicy.tmp /sutmp/sepolicy.patched "permissive *"
  1467. else
  1468. LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/supolicy --file /sutmp/sepolicy /sutmp/sepolicy.patched --sdk=$API
  1469. fi
  1470. if [ ! -f "/sutmp/sepolicy.patched" ]; then
  1471. ui_print_less "$UI_PRINT_LAST"
  1472. ui_print_always "--- Failure, aborting"
  1473. CONTINUE=false
  1474. fi
  1475. fi
  1476.  
  1477. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 644 ${CPIO_PREFIX}sepolicy /sutmp/sepolicy.patched"
  1478. fi
  1479.  
  1480. check_zero_def "- Adding daemon launcher" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 700 ${CPIO_PREFIX}sbin/launch_daemonsu.sh $COM/launch_daemonsu.sh"
  1481. check_zero_def "- Adding init script" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 750 ${CPIO_PREFIX}init.supersu.rc $COM/$INIT_SUPERSU_RC"
  1482.  
  1483. check_zero_def "- Creating mount point" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-mkdir /sutmp/ramdisk /sutmp/ramdisk 755 ${CPIO_PREFIX}su"
  1484.  
  1485. COMMAND="LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --patch /sutmp/ramdisk /sutmp/ramdisk $STOCKBOOTIMAGE"
  1486. if ($KEEPVERITY); then
  1487. COMMAND="$COMMAND --keep-verity"
  1488. fi
  1489. if ($KEEPFORCEENCRYPT); then
  1490. COMMAND="$COMMAND --keep-forceencrypt"
  1491. fi
  1492. if ($REMOVEENCRYPTABLE); then
  1493. COMMAND="$COMMAND --remove-encryptable"
  1494. fi
  1495. check_zero_def "- Patching init.*.rc, fstabs, dm-verity" "$COMMAND"
  1496.  
  1497. if ($CONTINUE); then
  1498. if ($SYSTEM_ROOT_USED); then
  1499. ui_print "- Patching init, system_root, system"
  1500.  
  1501. LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-extract /sutmp/ramdisk sbin/twrp /sutmp/twrp
  1502. if [ -f "/sutmp/twrp" ]; then
  1503. # backup TWRP's version of init
  1504. check_zero_def "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-extract /sutmp/ramdisk init /sutmp/init_twrp"
  1505. check_zero_def "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 750 init_twrp /sutmp/init_twrp"
  1506. fi
  1507.  
  1508. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 750 init $BIN/suinit"
  1509. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-mkdir /sutmp/ramdisk /sutmp/ramdisk 755 boot/system_root"
  1510. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-mkdir /sutmp/ramdisk /sutmp/ramdisk 755 boot/system"
  1511.  
  1512. if (! $CONTINUE); then
  1513. ui_print_less "$UI_PRINT_LAST"
  1514. ui_print_always "--- Failure, aborting"
  1515. fi
  1516. fi
  1517. fi
  1518.  
  1519. if [ -f "/data/custom_ramdisk_patch.sh" ]; then
  1520. check_zero_def "- Calling user ramdisk patch script" "sh /data/custom_ramdisk_patch.sh /sutmp/ramdisk"
  1521. fi
  1522.  
  1523. if ($CONTINUE); then
  1524. if ($FRP); then
  1525. ui_print "- Factory reset protection"
  1526. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-mkdir /sutmp/ramdisk /sutmp/ramdisk 0 ${CPIO_PREFIX}.sufrp"
  1527. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 755 ${CPIO_PREFIX}.sufrp/frp_install $COM/frp_install"
  1528. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 644 ${CPIO_PREFIX}.sufrp/file_contexts_image $COM/file_contexts_image"
  1529. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 644 ${CPIO_PREFIX}.sufrp/su $BIN/su"
  1530. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 644 ${CPIO_PREFIX}.sufrp/sukernel $BIN/sukernel"
  1531. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 644 ${CPIO_PREFIX}.sufrp/supolicy $BIN/supolicy"
  1532. check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 644 ${CPIO_PREFIX}.sufrp/libsupol.so $BIN/libsupol.so"
  1533. fi
  1534. fi
  1535.  
  1536. check_zero_def "- Creating ramdisk backup" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-backup /sutmp/ramdisk.original /sutmp/ramdisk /sutmp/ramdisk"
  1537.  
  1538. check_zero_def "- Compressing ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --gzip /sutmp/ramdisk /sutmp/ramdisk.packed"
  1539.  
  1540. if [ "$IMAGETYPE" = "chromeos" ]; then
  1541. $BIN/chromeos/futility vbutil_kernel --get-vmlinuz $STOCKBOOTIMAGE --vmlinuz-out /sutmp/boot.chromeos.img
  1542. STOCKBOOTIMAGE=/sutmp/boot.chromeos.img
  1543. fi
  1544.  
  1545. check_zero_def "- Creating boot image" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-replace-ramdisk $STOCKBOOTIMAGE /sutmp/ramdisk.packed /sutmp/boot.img"
  1546.  
  1547. if ($SYSTEM_ROOT_USED); then
  1548. check_zero_def "- Extracting kernel" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-extract-kernel $BOOTIMAGE /sutmp/kernel"
  1549.  
  1550. KERNEL_COMPRESSED=false
  1551. if ($CONTINUE); then
  1552. ui_print "- Decompressing kernel"
  1553. if (`LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --ungzip /sutmp/kernel /sutmp/kernel >/dev/null 2>/dev/null`); then
  1554. KERNEL_COMPRESSED=true
  1555. fi
  1556. fi
  1557.  
  1558. check_zero_def "- Patching kernel" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --patch-slot-kernel /sutmp/kernel /sutmp/kernel"
  1559.  
  1560. if ($CONTINUE); then
  1561. if ($KERNEL_COMPRESSED); then
  1562. check_zero_def "- Compressing kernel" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --gzip /sutmp/kernel /sutmp/kernel"
  1563. fi
  1564. fi
  1565.  
  1566. if ($CONTINUE); then
  1567. check_zero_def "- Replacing kernel" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-replace-kernel /sutmp/boot.img /sutmp/kernel /sutmp/boot.img"
  1568. fi
  1569. fi
  1570.  
  1571. if [ "$IMAGETYPE" = "chromeos" ]; then
  1572. ui_print "- Signing boot image"
  1573. $BIN/chromeos/futility vbutil_kernel --pack /sutmp/boot.img.signed --keyblock $COM/chromeos/kernel.keyblock --signprivate $COM/chromeos/kernel_data_key.vbprivk --version 1 --vmlinuz /sutmp/boot.img --config $COM/chromeos/kernel.config --arch arm --bootloader $COM/chromeos/kernel.bootloader --flags 0x1
  1574. if [ -f "/sutmp/boot.img.signed" ]; then
  1575. rm -rf /sutmp/boot.img
  1576. mv /sutmp/boot.img.signed /sutmp/boot.img
  1577. else
  1578. ui_print_less "$UI_PRINT_LAST"
  1579. ui_print_always "--- Failure, aborting"
  1580. $CONTINUE=false
  1581. fi
  1582. fi
  1583.  
  1584. if ($CONTINUE); then
  1585. # might return 1 even if we do not want to abort
  1586. ui_print "- Applying hex patches"
  1587. LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --hexpatch $COM/hexpatch /sutmp/boot.img /sutmp/boot.img
  1588. fi
  1589.  
  1590. if [ -f "/data/custom_boot_image_patch.sh" ]; then
  1591. check_zero_def "- Calling user boot image patch script" "sh /data/custom_boot_image_patch.sh /sutmp/boot.img"
  1592. fi
  1593.  
  1594. if ($CONTINUE); then
  1595. if ($SAMSUNG); then
  1596. # Prevent "KERNEL IS NOT SEANDROID ENFORCING"
  1597. SAMSUNG_CHECK=$(cat /sutmp/boot.img | grep SEANDROIDENFORCE)
  1598. if [ $? -ne 0 ]; then
  1599. echo -n "SEANDROIDENFORCE" >> /sutmp/boot.img
  1600. fi
  1601. fi
  1602.  
  1603. DEV=$(echo `resolve_link $BOOTIMAGE` | grep /dev/block/)
  1604. if [ $? -eq 0 ]; then
  1605. ui_print "- Flashing boot image"
  1606. cat /sutmp/boot.img /dev/zero | dd of=$BOOTIMAGE bs=4096
  1607. else
  1608. ui_print "- Saving boot image"
  1609. dd if=/sutmp/boot.img of=$BOOTIMAGE bs=4096
  1610. fi
  1611. fi
  1612.  
  1613. rm -rf /sutmp
  1614. fi
  1615.  
  1616. umount /su
  1617. losetup -d $LOOPDEVICE
  1618.  
  1619. ui_print " "
  1620. ui_print "*************************"
  1621. ui_print " IMPORTANT NOTICES "
  1622. ui_print "*************************"
  1623.  
  1624. TWRP2=$(cat /tmp/recovery.log | grep "ro.twrp.version=2");
  1625. if [ $? -eq 0 ]; then
  1626. ui_print "If TWRP offers to install"
  1627. ui_print "SuperSU, do *NOT* let it!"
  1628. ui_print "*************************"
  1629. fi
  1630.  
  1631. ui_print "First reboot may take a "
  1632. ui_print "few minutes. It can also "
  1633. ui_print "loop a few times. Do not "
  1634. ui_print "interrupt the process! "
  1635. ui_print "*************************"
  1636. ui_print " "
  1637.  
  1638. if (! $LESSLOGGING); then
  1639. sleep 5
  1640. fi
  1641. fi
  1642.  
  1643. ui_print "- Unmounting /system"
  1644. if ($SYSTEM_ROOT_USED); then
  1645. if (! $HAD_SYSTEM_ROOT); then
  1646. umount /system
  1647. umount /system_root
  1648. if ($HAD_SYSTEM); then
  1649. if ($HAD_SYSTEM_RW); then
  1650. mount -o rw /system
  1651. else
  1652. mount -o ro /system
  1653. fi
  1654. fi
  1655. fi
  1656. else
  1657. if (! $HAD_SYSTEM); then
  1658. umount /system
  1659. fi
  1660. fi
  1661.  
  1662. # revert RNG fix
  1663.  
  1664. umount /dev/random
  1665.  
  1666. ui_print_always "- Done !"
  1667. exit 0
  1668.  
Runtime error #stdin #stdout #stderr 0s 24520KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
./prog.sh: line 277: /proc/self/fd/: Is a directory
./prog.sh: line 278: /proc/self/fd/: Is a directory
./prog.sh: line 277: /proc/self/fd/: Is a directory
./prog.sh: line 278: /proc/self/fd/: Is a directory
./prog.sh: line 277: /proc/self/fd/: Is a directory
./prog.sh: line 278: /proc/self/fd/: Is a directory
./prog.sh: line 277: /proc/self/fd/: Is a directory
./prog.sh: line 278: /proc/self/fd/: Is a directory
mount: only root can use "--options" option
cat: '/fstab.*': No such file or directory
./prog.sh: line 277: /proc/self/fd/: Is a directory
./prog.sh: line 278: /proc/self/fd/: Is a directory
mount: only root can use "--options" option
mount: only root can use "--types" option
mount: only root can use "--types" option
cat: /etc/recovery.fstab: No such file or directory
mount: only root can use "--types" option
mount: only root can use "--types" option
mount: /data: No such file or directory
mount: only root can use "--types" option
mount: only root can use "--types" option
cat: /etc/recovery.fstab: No such file or directory
mount: only root can use "--types" option
mount: only root can use "--types" option
mount: only root can use "--options" option
mount: only root can use "--options" option
./prog.sh: line 277: /proc/self/fd/: Is a directory
./prog.sh: line 278: /proc/self/fd/: Is a directory