AI手机网,短视频直播 硬改改机 一键新机 群控软件 刷机定制

 找回密码
 立即注册
搜索
查看: 1807|回复: 1

安卓6.0 root -安卓 7.1 手机 root 手动 root权限 添加方法

[复制链接]
发表于 2017-11-24 10:52:51 | 显示全部楼层 |阅读模式
安卓6.0 -7.1 手动root权限 添加方法

launch_daemonsu.sh
  1. #!/system/bin/sh

  2. MODE=$1

  3. log_print() {
  4.   echo "($MODE) $1"
  5.   echo "($MODE) $1" >> /dev/.launch_daemonsu.log
  6.   log -p i -t launch_daemonsu "($MODE) $1"
  7. }

  8. log_print "start"

  9. if [ `mount | grep " /data " >/dev/null 2>&1; echo $?` -ne 0 ]; then
  10.   # /data not mounted yet, we will be called again later
  11.   log_print "abort: /data not mounted #1"
  12.   exit
  13. fi

  14. if [ `mount | grep " /data " | grep "tmpfs" >/dev/null 2>&1; echo $?` -eq 0 ]; then
  15.   # /data not mounted yet, we will be called again later
  16.   log_print "abort: /data not mounted #2"
  17.   exit
  18. fi

  19. if [ `cat /proc/mounts | grep /su >/dev/null 2>&1; echo $?` -eq 0 ]; then
  20.   if [ -d "/su/bin" ]; then
  21.     if [ `ps | grep -v "launch_daemonsu.sh" | grep "daemonsu" >/dev/null 2>&1; echo $?` -eq 0 ]; then
  22.       # nothing to do here
  23.       log_print "abort: daemonsu already running"
  24.       exit
  25.     fi
  26.   fi
  27. fi

  28. setprop sukernel.daemonsu.launch $MODE

  29. loopsetup() {
  30.   LOOPDEVICE=
  31.   for DEV in $(ls /dev/block/loop*); do
  32.     if [ `losetup $DEV $1 >/dev/null 2>&1; echo $?` -eq 0 ]; then
  33.       LOOPDEVICE=$DEV
  34.       break
  35.     fi
  36.   done

  37.   log_print "loopsetup($1): $LOOPDEVICE"
  38. }

  39. resize() {
  40.   if [ `ls  -l /data/su.img | grep " 33554432 " >/dev/null 2>&1; echo $?` -eq 0 ]; then
  41.     log_print "resizing /data/su.img from 32M to 96M"
  42.     e2fsck -p -f /data/su.img
  43.     resize2fs /data/su.img 96M
  44.   fi
  45. }

  46. if [ ! -d "/su/bin" ]; then
  47.   log_print "/su not mounted yet"

  48.   # not mounted yet, do our thing
  49.   REBOOT=false

  50.   # copy boot image backups
  51.   log_print "copying boot image backups from /cache to /data"
  52.   cp -f /cache/stock_boot_* /data/. 2>/dev/null

  53.   if [ -f "/data/su.img" ]; then
  54.     log_print "/data/su.img found"
  55.     e2fsck -p -f /data/su.img

  56.     # make sure the image is the right size
  57.     resize
  58.   fi

  59.   # newer image in /cache ?
  60.   # only used if recovery couldn't mount /data
  61.   if [ -f "/cache/su.img" ]; then
  62.     log_print "/cache/su.img found"
  63.     e2fsck -p -f /cache/su.img
  64.     OVERWRITE=true

  65.     if [ -f "/data/su.img" ]; then
  66.       # attempt merge, this will fail pre-M
  67.       # will probably also fail with /system installed busybox,
  68.       # but then again, is there anything busybox doesn't break?
  69.       # falls back to overwrite

  70.       log_print "/data/su.img found"
  71.       log_print "attempting merge"

  72.       mkdir /cache/data_img
  73.       mkdir /cache/cache_img

  74.       # setup loop devices

  75.       loopsetup /data/su.img
  76.       LOOPDATA=$LOOPDEVICE
  77.       log_print "$LOOPDATA /data/su.img"

  78.       loopsetup /cache/su.img
  79.       LOOPCACHE=$LOOPDEVICE
  80.       log_print "$LOOPCACHE /cache/su.img"

  81.       if [ ! -z "$LOOPDATA" ]; then
  82.         if [ ! -z "$LOOPCACHE" ]; then
  83.           # if loop devices have been setup, mount images
  84.           OK=true

  85.           if [ `mount -t ext4 -o rw,noatime $LOOPDATA /cache/data_img >/dev/null 2>&1; echo $?` -ne 0 ]; then
  86.             OK=false
  87.           fi

  88.           if [ `mount -t ext4 -o rw,noatime $LOOPCACHE /cache/cache_img >/dev/null 2>&1; echo $?` -ne 0 ]; then
  89.             OK=false
  90.           fi

  91.           if ($OK); then
  92.             # if mounts have succeeded, merge the images
  93.             if [ `cp -af /cache/cache_img/. /cache/data_img >/dev/null 2>&1; echo $?` -eq 0 ]; then
  94.               log_print "merge complete"
  95.               OVERWRITE=false
  96.             fi
  97.           fi

  98.           umount /cache/data_img
  99.           umount /cache/cache_img
  100.         fi
  101.       fi

  102.       losetup -d $LOOPDATA
  103.       losetup -d $LOOPCACHE

  104.       rmdir /cache/data_img
  105.       rmdir /cache/cache_img
  106.     fi

  107.     if ($OVERWRITE); then
  108.       # no /data/su.img or merge failed, replace
  109.       log_print "replacing /data/su.img with /cache/su.img"
  110.       mv /cache/su.img /data/su.img

  111.       # make sure the new image is the right size
  112.       resize
  113.     fi

  114.     rm /cache/su.img
  115.   fi

  116.   # do we have an APK to install ?
  117.   if [ -f "/cache/SuperSU.apk" ]; then
  118.     cp /cache/SuperSU.apk /data/SuperSU.apk
  119.     rm /cache/SuperSU.apk
  120.   fi
  121.   if [ -f "/data/SuperSU.apk" ]; then
  122.     log_print "installing SuperSU APK in /data"

  123.     APKPATH=eu.chainfire.supersu-1
  124.     for i in `ls /data/app | grep eu.chainfire.supersu- | grep -v eu.chainfire.supersu.pro`; do
  125.       if [ `cat /data/system/packages.xml | grep $i >/dev/null 2>&1; echo $?` -eq 0 ]; then
  126.         APKPATH=$i
  127.         break;
  128.       fi
  129.     done
  130.     rm -rf /data/app/eu.chainfire.supersu-*

  131.     log_print "target path: /data/app/$APKPATH"

  132.     mkdir /data/app/$APKPATH
  133.     chown 1000.1000 /data/app/$APKPATH
  134.     chmod 0755 /data/app/$APKPATH
  135.     chcon u:object_r:apk_data_file:s0 /data/app/$APKPATH

  136.     cp /data/SuperSU.apk /data/app/$APKPATH/base.apk
  137.     chown 1000.1000 /data/app/$APKPATH/base.apk
  138.     chmod 0644 /data/app/$APKPATH/base.apk
  139.     chcon u:object_r:apk_data_file:s0 /data/app/$APKPATH/base.apk

  140.     rm /data/SuperSU.apk

  141.     sync

  142.     # just in case
  143.     REBOOT=true
  144.   fi

  145.   # sometimes we need to reboot, make it so
  146.   if ($REBOOT); then
  147.     log_print "rebooting"
  148.     if [ "$MODE" = "post-fs-data" ]; then
  149.       # avoid device freeze (reason unknown)
  150.       sh -c "sleep 5; reboot" &
  151.     else
  152.       reboot
  153.     fi
  154.     exit
  155.   fi

  156.   # losetup is unreliable pre-M
  157.   if [ `cat /proc/mounts | grep /su >/dev/null 2>&1; echo $?` -ne 0 ]; then
  158.     loopsetup /data/su.img
  159.     if [ ! -z "$LOOPDEVICE" ]; then
  160.       mount -t ext4 -o rw,noatime $LOOPDEVICE /su
  161.     fi
  162.   fi

  163.   # trigger mount, should also work pre-M, but on post-fs-data trigger may
  164.   # be processed only after this script runs, causing a fallback to service launch
  165.   if [ `cat /proc/mounts | grep /su >/dev/null 2>&1; echo $?` -ne 0 ]; then
  166.     chcon u:object_r:system_data_file:s0 /data/su.img
  167.     chmod 0600 /data/su.img
  168.     setprop sukernel.mount 1
  169.     sleep 1
  170.   fi

  171.   # exit if all mount attempts have failed, script is likely to be called again
  172.   if [ `cat /proc/mounts | grep /su >/dev/null 2>&1; echo $?` -ne 0 ]; then
  173.     log_print "abort: mount failed"
  174.     exit
  175.   fi

  176.   # if other su binaries exist, route them to ours
  177.   mount -o bind /su/bin/su /sbin/su 2>/dev/null
  178.   mount -o bind /su/bin/su /system/bin/su 2>/dev/null
  179.   mount -o bind /su/bin/su /system/xbin/su 2>/dev/null

  180.   # poor man's overlay on /system/xbin
  181.   if [ -d "/su/xbin_bind" ]; then
  182.     cp -f -a /system/xbin/. /su/xbin_bind
  183.     rm -rf /su/xbin_bind/su
  184.     ln -s /su/bin/su /su/xbin_bind/su
  185.     mount -o bind /su/xbin_bind /system/xbin
  186.   fi
  187. fi

  188. # start daemon
  189. if [ "$MODE" != "post-fs-data" ]; then
  190.   # if launched by service, replace this process (exec)
  191.   log_print "exec daemonsu"
  192.   exec /su/bin/daemonsu --auto-daemon
  193. else
  194.   # if launched by exec, fork (non-exec) and wait for su.d to complete executing
  195.   log_print "fork daemonsu"
  196.   /su/bin/daemonsu --auto-daemon

  197.   # wait for a while for su.d to complete
  198.   if [ -d "/su/su.d" ]; then
  199.     log_print "waiting for su.d"
  200.     for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
  201.       # su.d finished ?
  202.       if [ -f "/dev/.su.d.complete" ]; then
  203.         break
  204.       fi

  205.       for j in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
  206.         # su.d finished ?
  207.         if [ -f "/dev/.su.d.complete" ]; then
  208.           break
  209.         fi

  210.         # sleep 240ms if usleep supported, warm up the CPU if not
  211.         # 16*16*240ms=60s maximum if usleep supported, else much shorter
  212.         usleep 240000
  213.       done
  214.     done
  215.   fi
  216.   log_print "end"
  217. fi
复制代码
update-binary
  1. #!/sbin/sh
  2. #
  3. # SuperSU installer ZIP
  4. # Copyright (c) 2012-2016 - Chainfire
  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. # Shell overrides all, /data/.supersu overrides /cache/.supersu overrides
  56. # /system/.supersu
  57. #
  58. # Note that if SELinux is set to enforcing, the daemonsu binary expects
  59. # to be run at startup (usually from install-recovery.sh, 99SuperSUDaemon,
  60. # or app_process) from u:r:init:s0 or u:r:kernel:s0 contexts. Depending
  61. # on the current policies, it can also deal with u:r:init_shell:s0 and
  62. # u:r:toolbox:s0 contexts. Any other context will lead to issues eventually.
  63. #
  64. # ----- "SYSTEM" INSTALL -----
  65. #
  66. # "System" install puts all the files needed in /system and does not need
  67. # any boot image modifications. Default install method pre-Android-6.0
  68. # (excluding Samsung-5.1).
  69. #
  70. # Even on Android-6.0+, the script attempts to detect if the current
  71. # firmware is compatible with a system-only installation (see the
  72. # "detect_systemless_required" function), and will prefer that
  73. # (unless the SYSTEMLESS variable is set) if so. This will catch the
  74. # case of several custom ROMs that users like to use custom boot images
  75. # with - SuperSU will not need to patch these. It can also catch some
  76. # locked bootloader cases that do allow security policy updates.
  77. #
  78. # To install SuperSU properly, aside from cleaning old versions and
  79. # other superuser-type apps from the system, the following files need to
  80. # be installed:
  81. #
  82. # API   source                        target                              chmod   chcon                       required
  83. #
  84. # 7-19  common/Superuser.apk          /system/app/Superuser.apk           0644    u:object_r:system_file:s0   gui
  85. # 20+   common/Superuser.apk          /system/app/SuperSU/SuperSU.apk     0644    u:object_r:system_file:s0   gui
  86. #
  87. # 17+   common/install-recovery.sh    /system/etc/install-recovery.sh     0755    *1                          required
  88. # 17+                                 /system/bin/install-recovery.sh     (symlink to /system/etc/...)        required
  89. # *1: same as /system/bin/toolbox: u:object_r:system_file:s0 if API < 20, u:object_r:toolbox_exec:s0 if API >= 20
  90. #
  91. # 7+    ARCH/su *2                    /system/xbin/su                     *3      u:object_r:system_file:s0   required
  92. # 7+                                  /system/bin/.ext/.su                *3      u:object_r:system_file:s0   gui
  93. # 17+                                 /system/xbin/daemonsu               0755    u:object_r:system_file:s0   required
  94. # 17-21                               /system/xbin/sugote                 0755    u:object_r:zygote_exec:s0   required
  95. # *2: su.pie for 17+ x86(_32) only
  96. # *3: 06755 if API < 18, 0755 if API >= 18
  97. #
  98. # 19+   ARCH/supolicy                 /system/xbin/supolicy               0755    u:object_r:system_file:s0   required
  99. # 19+   ARCH/libsupol.so              /system/lib(64)/libsupol.so         0644    u:object_r:system_file:s0   required
  100. #
  101. # 17-21 /system/bin/sh or mksh *4     /system/xbin/sugote-mksh            0755    u:object_r:system_file:s0   required
  102. # *4: which one (or both) are available depends on API
  103. #
  104. # 21+   /system/bin/app_process32 *5  /system/bin/app_process32_original  0755    u:object_r:zygote_exec:s0   required
  105. # 21+   /system/bin/app_process64 *5  /system/bin/app_process64_original  0755    u:object_r:zygote_exec:s0   required
  106. # 21+   /system/bin/app_processXX *5  /system/bin/app_process_init        0755    u:object_r:system_file:s0   required
  107. # 21+                                 /system/bin/app_process             (symlink to /system/xbin/daemonsu)  required
  108. # 21+                             *5  /system/bin/app_process32           (symlink to /system/xbin/daemonsu)  required
  109. # 21+                             *5  /system/bin/app_process64           (symlink to /system/xbin/daemonsu)  required
  110. # *5: Only do this for the relevant bits. On a 64 bits system, leave the 32 bits files alone, or dynamic linker errors
  111. #     will prevent the system from fully working in subtle ways. The bits of the su binary must also match!
  112. #
  113. # 17+   common/99SuperSUDaemon *6     /system/etc/init.d/99SuperSUDaemon  0755    u:object_r:system_file:s0   optional
  114. # *6: only place this file if /system/etc/init.d is present
  115. #
  116. # 17+   'echo 1 >' or 'touch' *7      /system/etc/.installed_su_daemon    0644    u:object_r:system_file:s0   optional
  117. # *7: the file just needs to exist or some recoveries will nag you. Even with it there, it may still happen.
  118. #
  119. # It may seem some files are installed multiple times needlessly, but
  120. # it only seems that way. Installing files differently or symlinking
  121. # instead of copying (unless specified) will lead to issues eventually.
  122. #
  123. # After installation, run '/system/xbin/su --install', which may need to
  124. # perform some additional installation steps. Ideally, at one point,
  125. # a lot of this script will be moved there.
  126. #
  127. # The included chattr(.pie) binaries are used to remove ext2's immutable
  128. # flag on some files. This flag is no longer set by SuperSU's OTA
  129. # survival since API level 18, so there is no need for the 64 bit versions.
  130. # Note that chattr does not need to be installed to the system, it's just
  131. # used by this script, and not supported by the busybox used in older
  132. # recoveries.
  133. #
  134. # ----- "SYSTEM-LESS" INSTALL -----
  135. #
  136. # "System-less" install requires a modified boot image (the script can patch
  137. # many boot images on-the-fly), but does not touch /system at all. Instead
  138. # it keeps all the needed files in an image (/data/su.img) which is mounted
  139. # to /su. Default install method on all Android-6.0+ and Samsung-5.1+
  140. # devices.
  141. #
  142. # Note that even on 6.0+, system compatibility is checked. See the "SYSTEM"
  143. # install section above.
  144. #
  145. # An ext4 image is created as /data/su.img, or /cache/su.img if /data could
  146. # not be mounted. Similarly, the APK is placed as either /data/SuperSU.apk
  147. # or /cache/SuperSU.apk. This is so we are not dependent on /data decryption
  148. # working in recovery, which in the past has proved an issue on brand-new
  149. # Android versions and devices.
  150. #
  151. # /sbin/launch_daemonsu.sh, which is added a service to init.rc, will mount
  152. # the image at /su, and launch daemonsu from /su/bin/daemonsu. But before it
  153. # does that, it will try to merge /data/su.img and /cache/su.img (leading),
  154. # if both are present. It will also try to install the SuperSU APK.
  155. #
  156. # Files are expected at the following places (/su being the mountpoint of
  157. # the ext4 image):
  158. #
  159. # API   source                        target                              chmod   chcon                       required
  160. #
  161. # 22+   common/Superuser.apk          /[data|cache]/SuperSU.apk           0644    u:object_r:system_file:s0   gui
  162. #
  163. # 22+   ARCH/su *1                    /su/bin/su                          0755    u:object_r:system_file:s0   required
  164. # 22+                                 /su/bin/daemonsu                    0755    u:object_r:system_file:s0   required
  165. # *1: su.pie for 17+ x86(_32) only
  166. #
  167. # 22+   ARCH/supolicy                 /su/bin/supolicy_wrapped            0755    u:object_r:system_file:s0   required
  168. # 22+   /su/bin/su (symlink) *2       /su/bin/supolicy                    0755    u:object_r:system_file:s0   required
  169. # 22+   ARCH/libsupol.so              /su/lib/libsupol.so                 0644    u:object_r:system_file:s0   required
  170. # *2: when called this way, su sets the correct LD_LIBRARY_PATH and calls supolicy_wrapped
  171. #
  172. # 22+   ARCH/sukernel                 /su/bin/sukernel                    0755    u:object_r:system_file:s0   required
  173. #
  174. # These files are automatically created on launch by daemonsu as needed:
  175. # 22+   /system/bin/sh                /su/bin/sush                        0755    u:object_r:system_file:s0   required
  176. # 22+   /system/bin/app_process[64]   /su/bin/app_process                 0755    u:object_r:system_file:s0   required
  177. #
  178. # These files are injected into the boot image ramdisk:
  179. # 22+   common/launch_daemonsu.sh     /sbin/launch_daemonsu.sh            0700    u:object_r:rootfs:s0        required
  180. #
  181. # The automated boot image patcher included makes the following modifications
  182. # to the ramdisk:
  183. #
  184. # - Uses the supolicy tool to patch the sepolicy file
  185. # - Injects /sbin/launch_daemon.sh
  186. # - Creates /su
  187. # - Removes /verity_key
  188. # - Patches /*fstab*
  189. # --- Removes support_scfs and verify flags
  190. # --- Changes forceencrypt/forcefdeorfbe into encryptable
  191. # --- Set ro mounts to use noatime
  192. # - Patches /init.rc
  193. # --- Removes 'setprop selinux.reload_policy' occurences
  194. # --- Adds a SuperSU:PATCH marker with the version of the sukernel tool
  195. # --- Adds a SuperSU:STOCK marker listed the SHA1 of the original boot image
  196. # - Adds /init.supersu.rc
  197. # --- Adds a sukernel.mount property trigger that mounts /data/su.img to /su
  198. # --- Adds the daemonsu service that launches /sbin/launch_daemon.sh
  199. # --- Adds exec /sbin/launch_daemonsu.sh on post-fs-data
  200. # - Patches /init.environ.rc
  201. # --- Adds PATH variable if it does not exist
  202. # --- Prepends /su/bin to the PATH variable
  203. # - Patches /file_contexts[.bin]
  204. # --- Adds a default context for file existing in the /su mount
  205. #
  206. # In case this documentation becomes outdated, please note that the sukernel
  207. # tool is very chatty, and its output tells you exactly what it is doing
  208. # and how. In TWRP, you can view this output by catting /tmp/recovery.log
  209. # after flashing the ZIP.
  210. #
  211. # The boot image patcher creates a backup of the boot image it patches, for
  212. # future restoration. It cannot re-patch a patched boot image, it will restore
  213. # the previous boot image first. /[data|cache]/stock_boot_*.gz
  214. #
  215. # The boot image patcher currently only supports GZIP compressed ramdisks, and
  216. # boot images in the standard Android boot image format.
  217. #
  218. # During boot image patch, /data/custom_ramdisk_patch.sh will be called,
  219. # with the name of the ramdisk cpio file as parameter. The script must
  220. # replace the input file and return a 0 exit code.
  221. #
  222. # Just before flashing, the boot image patcher will call
  223. # /data/custom_boot_image_patch.sh with the name of the patched boot image
  224. # as parameter. A device-specific patcher can further patch the boot image
  225. # if needed. It must replace the input file and return a 0 exit code.

  226. OUTFD=$2
  227. ZIP=$3

  228. getvar() {
  229.   local VARNAME=$1
  230.   local VALUE=$(eval echo \[        DISCUZ_CODE_1        ]quot;$VARNAME");
  231.   for FILE in /data/.supersu /cache/.supersu /system/.supersu; do
  232.     if [ -z "$VALUE" ]; then
  233.       LINE=$(cat $FILE 2>/dev/null | grep "$VARNAME=")
  234.       if [ ! -z "$LINE" ]; then
  235.         VALUE=${LINE#*=}
  236.       fi
  237.     fi
  238.   done
  239.   eval $VARNAME=\$VALUE
  240. }

  241. readlink /proc/$/fd/$OUTFD 2>/dev/null | grep /tmp >/dev/null
  242. if [ "$?" -eq "0" ]; then
  243.   # rerouted to log file, we don't want our ui_print commands going there
  244.   OUTFD=0

  245.   # we are probably running in embedded mode, see if we can find the right fd
  246.   # we know the fd is a pipe and that the parent updater may have been started as
  247.   # 'update-binary 3 fd zipfile'
  248.   for FD in `ls /proc/$/fd`; do
  249.     readlink /proc/$/fd/$FD 2>/dev/null | grep pipe >/dev/null
  250.     if [ "$?" -eq "0" ]; then
  251.       ps | grep " 3 $FD " | grep -v grep >/dev/null
  252.       if [ "$?" -eq "0" ]; then
  253.         OUTFD=$FD
  254.         break
  255.       fi
  256.     fi
  257.   done
  258. fi

  259. ui_print_always() {
  260.   echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD
  261.   echo -n -e "ui_print\n" >> /proc/self/fd/$OUTFD
  262. }

  263. if [ -z "$LESSLOGGING" ]; then
  264.   LESSLOGGING=false
  265. fi

  266. UI_PRINT_LAST=""

  267. ui_print() {
  268.   if (! $LESSLOGGING); then
  269.     UI_PRINT_LAST="$1"
  270.     ui_print_always "$1"
  271.   fi
  272. }

  273. ui_print_less() {
  274.   if ($LESSLOGGING); then
  275.     ui_print_always "$1"
  276.   fi
  277. }

  278. ch_con() {
  279.   LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toybox chcon -h u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  280.   LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon -h u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  281.   LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon -h u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  282.   chcon -h u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  283.   LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toybox chcon u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  284.   LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  285.   LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  286.   chcon u:object_r:system_file:s0 $1 1>/dev/null 2>/dev/null
  287. }

  288. ch_con_ext() {
  289.   LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toybox chcon $2 $1 1>/dev/null 2>/dev/null
  290.   LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon $2 $1 1>/dev/null 2>/dev/null
  291.   LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon $2 $1 1>/dev/null 2>/dev/null
  292.   chcon $2 $1 1>/dev/null 2>/dev/null
  293. }

  294. ln_con() {
  295.   LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toybox ln -s $1 $2 1>/dev/null 2>/dev/null
  296.   LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox ln -s $1 $2 1>/dev/null 2>/dev/null
  297.   LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox ln -s $1 $2 1>/dev/null 2>/dev/null
  298.   ln -s $1 $2 1>/dev/null 2>/dev/null
  299.   ch_con $2 1>/dev/null 2>/dev/null
  300. }

  301. set_perm() {
  302.   chown $1.$2 $4
  303.   chown $1:$2 $4
  304.   chmod $3 $4
  305.   ch_con $4
  306.   ch_con_ext $4 $5
  307. }

  308. cp_perm() {
  309.   rm $5
  310.   if [ -f "$4" ]; then
  311.     cat $4 > $5
  312.     set_perm $1 $2 $3 $5 $6
  313.   fi
  314. }

  315. is_mounted() {
  316.   if [ ! -z "$2" ]; then
  317.     cat /proc/mounts | grep $1 | grep $2, >/dev/null
  318.   else
  319.     cat /proc/mounts | grep $1 >/dev/null
  320.   fi
  321.   return $?
  322. }

  323. toolbox_mount() {
  324.   RW=rw
  325.   if [ ! -z "$2" ]; then
  326.     RW=$2
  327.   fi

  328.   DEV=
  329.   POINT=
  330.   FS=
  331.   for i in `cat /etc/fstab | grep "$1"`; do
  332.     if [ -z "$DEV" ]; then
  333.       DEV=$i
  334.     elif [ -z "$POINT" ]; then
  335.       POINT=$i
  336.     elif [ -z "$FS" ]; then
  337.       FS=$i
  338.       break
  339.     fi
  340.   done
  341.   if (! is_mounted $1 $RW); then mount -t $FS -o $RW $DEV $POINT; fi
  342.   if (! is_mounted $1 $RW); then mount -t $FS -o $RW,remount $DEV $POINT; fi

  343.   DEV=
  344.   POINT=
  345.   FS=
  346.   for i in `cat /etc/recovery.fstab | grep "$1"`; do
  347.     if [ -z "$POINT" ]; then
  348.       POINT=$i
  349.     elif [ -z "$FS" ]; then
  350.       FS=$i
  351.     elif [ -z "$DEV" ]; then
  352.       DEV=$i
  353.       break
  354.     fi
  355.   done
  356.   if [ "$FS" = "emmc" ]; then
  357.     if (! is_mounted $1 $RW); then mount -t ext4 -o $RW $DEV $POINT; fi
  358.     if (! is_mounted $1 $RW); then mount -t ext4 -o $RW,remount $DEV $POINT; fi
  359.     if (! is_mounted $1 $RW); then mount -t f2fs -o $RW $DEV $POINT; fi
  360.     if (! is_mounted $1 $RW); then mount -t f2fs -o $RW,remount $DEV $POINT; fi
  361.   else
  362.     if (! is_mounted $1 $RW); then mount -t $FS -o $RW $DEV $POINT; fi
  363.     if (! is_mounted $1 $RW); then mount -t $FS -o $RW,remount $DEV $POINT; fi
  364.   fi
  365. }

  366. remount_system_rw() {
  367.   if (! is_mounted /system rw); then mount -o rw,remount /system; fi
  368.   if (! is_mounted /system rw); then mount -o rw,remount /system /system; fi
  369.   if (! is_mounted /system rw); then toolbox_mount /system; fi
  370. }

  371. # 'readlink -f' is not reliable across devices/recoveries, this works for our case
  372. resolve_link() {
  373.   local RESOLVE=$1
  374.   local RESOLVED=
  375.   while (true); do
  376.     RESOLVED=$(readlink $RESOLVE || echo $RESOLVE)
  377.     if [ "$RESOLVE" = "$RESOLVED" ]; then
  378.       echo $RESOLVE
  379.       break
  380.     else
  381.       RESOLVE=$RESOLVED
  382.     fi
  383.   done
  384. }

  385. wipe_system_files_if_present() {
  386.   GO=false
  387.   SYSTEMFILES="
  388.     /system/xbin/daemonsu
  389.     /system/xbin/sugote
  390.     /system/xbin/sugote-mksh
  391.     /system/xbin/supolicy
  392.     /system/xbin/ku.sud
  393.     /system/xbin/.ku
  394.     /system/xbin/.su
  395.     /system/lib/libsupol.so
  396.     /system/lib64/libsupol.so
  397.     /system/bin/.ext/.su
  398.     /system/etc/init.d/99SuperSUDaemon
  399.     /system/etc/.installed_su_daemon
  400.     /system/app/Superuser.apk
  401.     /system/app/Superuser.odex
  402.     /system/app/Superuser
  403.     /system/app/SuperUser.apk
  404.     /system/app/SuperUser.odex
  405.     /system/app/SuperUser
  406.     /system/app/superuser.apk
  407.     /system/app/superuser.odex
  408.     /system/app/superuser
  409.     /system/app/Supersu.apk
  410.     /system/app/Supersu.odex
  411.     /system/app/Supersu
  412.     /system/app/SuperSU.apk
  413.     /system/app/SuperSU.odex
  414.     /system/app/SuperSU
  415.     /system/app/supersu.apk
  416.     /system/app/supersu.odex
  417.     /system/app/supersu
  418.     /system/app/VenomSuperUser.apk
  419.     /system/app/VenomSuperUser.odex
  420.     /system/app/VenomSuperUser
  421.   "
  422.   for FILE in $SYSTEMFILES; do
  423.     if [ -d "$FILE" ]; then GO=true; fi
  424.     if [ -f "$FILE" ]; then GO=true; fi
  425.   done

  426.   RMSU=false
  427.   if (! $RWSYSTEM); then
  428.     if [ -f "/system/xbin/su" ]; then
  429.       # only remove /system/xbin/su if it's SuperSU. Could be firmware-included version, we
  430.       # do not want to cause remount for that
  431.       SUPERSU_CHECK=$(cat /system/xbin/su | grep SuperSU)
  432.       if [ $? -eq 0 ]; then
  433.         GO=true
  434.         RMSU=true
  435.       fi
  436.     fi

  437.     SPECIALSYSTEMFILES="
  438.       /system/etc/install-recovery_original.sh
  439.       /system/bin/install-recovery_original.sh
  440.       /system/bin/app_process32_original
  441.       /system/bin/app_process32_xposed
  442.       /system/bin/app_process64_original
  443.       /system/bin/app_process64_xposed
  444.       /system/bin/app_process_init
  445.     "
  446.     for FILE in $SPECIALSYSTEMFILES; do
  447.       if [ -d "$FILE" ]; then GO=true; fi
  448.     done
  449.   fi

  450.   if ($GO); then
  451.     if (! $RWSYSTEM); then
  452.       ui_print "- Remounting system r/w :("
  453.       remount_system_rw
  454.     fi

  455.     for FILE in $SYSTEMFILES; do
  456.       if [ -d "$FILE" ]; then rm -rf $FILE; fi
  457.       if [ -f "$FILE" ]; then rm -f $FILE; fi
  458.     done

  459.     if (! $RWSYSTEM); then
  460.       # remove wrongly placed /system/xbin/su as well
  461.       if ($RMSU); then
  462.         rm -f /system/xbin/su
  463.       fi

  464.       # Restore install-recovery and app_process from system install
  465.       # Otherwise, our system-less install will fail to boot
  466.       if [ -f "/system/etc/install-recovery_original.sh" ]; then
  467.         rm -f /system/etc/install-recovery.sh
  468.         mv /system/etc/install-recovery_original.sh /system/etc/install-recovery.sh
  469.       fi
  470.       if [ -f "/system/bin/install-recovery_original.sh" ]; then
  471.         rm -f /system/bin/install-recovery.sh
  472.         mv /system/bin/install-recovery_original.sh /system/bin/install-recovery.sh
  473.       fi
  474.       if [ -f "/system/bin/app_process64_original" ]; then
  475.         rm -f /system/bin/app_process64
  476.         if [ -f "/system/bin/app_process64_xposed" ]; then
  477.           ln -s /system/bin/app_process64_xposed /system/bin/app_process64
  478.         else
  479.           mv /system/bin/app_process64_original /system/bin/app_process64
  480.         fi
  481.       fi
  482.       if [ -f "/system/bin/app_process32_original" ]; then
  483.         rm -f /system/bin/app_process32
  484.         if [ -f "/system/bin/app_process32_xposed" ]; then
  485.           ln -s /system/bin/app_process32_xposed /system/bin/app_process32
  486.         else
  487.           mv /system/bin/app_process32_original /system/bin/app_process32
  488.         fi
  489.       fi
  490.       if [ -f "/system/bin/app_process64" ]; then
  491.         rm /system/bin/app_process
  492.         ln -s /system/bin/app_process64 /system/bin/app_process
  493.       elif [ -f "/system/bin/app_process32" ]; then
  494.         rm /system/bin/app_process
  495.         ln -s /system/bin/app_process32 /system/bin/app_process
  496.       fi
  497.       rm -f /system/bin/app_process_init
  498.     fi
  499.   fi
  500. }

  501. wipe_data_competitors_and_cache() {
  502.   rm -f /data/dalvik-cache/*com.noshufou.android.su*
  503.   rm -f /data/dalvik-cache/*/*com.noshufou.android.su*
  504.   rm -f /data/dalvik-cache/*com.koushikdutta.superuser*
  505.   rm -f /data/dalvik-cache/*/*com.koushikdutta.superuser*
  506.   rm -f /data/dalvik-cache/*com.mgyun.shua.su*
  507.   rm -f /data/dalvik-cache/*/*com.mgyun.shua.su*
  508.   rm -f /data/dalvik-cache/*com.m0narx.su*
  509.   rm -f /data/dalvik-cache/*/*com.m0narx.su*
  510.   rm -f /data/dalvik-cache/*com.kingroot.kinguser*
  511.   rm -f /data/dalvik-cache/*/*com.kingroot.kinguser*
  512.   rm -f /data/dalvik-cache/*com.kingroot.master*
  513.   rm -f /data/dalvik-cache/*/*com.kingroot.master*
  514.   rm -f /data/dalvik-cache/*me.phh.superuser*
  515.   rm -f /data/dalvik-cache/*/*me.phh.superuser*
  516.   rm -f /data/dalvik-cache/*Superuser.apk*
  517.   rm -f /data/dalvik-cache/*/*Superuser.apk*
  518.   rm -f /data/dalvik-cache/*SuperUser.apk*
  519.   rm -f /data/dalvik-cache/*/*SuperUser.apk*
  520.   rm -f /data/dalvik-cache/*superuser.apk*
  521.   rm -f /data/dalvik-cache/*/*superuser.apk*
  522.   rm -f /data/dalvik-cache/*VenomSuperUser.apk*
  523.   rm -f /data/dalvik-cache/*/*VenomSuperUser.apk*
  524.   rm -f /data/dalvik-cache/*eu.chainfire.supersu*
  525.   rm -f /data/dalvik-cache/*/*eu.chainfire.supersu*
  526.   rm -f /data/dalvik-cache/*Supersu.apk*
  527.   rm -f /data/dalvik-cache/*/*Supersu.apk*
  528.   rm -f /data/dalvik-cache/*SuperSU.apk*
  529.   rm -f /data/dalvik-cache/*/*SuperSU.apk*
  530.   rm -f /data/dalvik-cache/*supersu.apk*
  531.   rm -f /data/dalvik-cache/*/*supersu.apk*
  532.   rm -f /data/dalvik-cache/*.oat
  533.   rm -rf /data/app/com.noshufou.android.su*
  534.   rm -rf /data/app/com.koushikdutta.superuser*
  535.   rm -rf /data/app/com.mgyun.shua.su*
  536.   rm -rf /data/app/com.m0narx.su*
  537.   rm -rf /data/app/com.kingroot.kinguser*
  538.   rm -rf /data/app/com.kingroot.master*
  539.   rm -rf /data/app/me.phh.superuser*
  540. }

  541. # check_zero "progress_message" "success message" "failure message" "command"
  542. check_zero() {
  543.   if ($CONTINUE); then
  544.     if [ ! -z "$1" ]; then ui_print "$1"; fi
  545.     eval "$4"
  546.     if [ $? -eq 0 ]; then
  547.       if [ ! -z "$2" ]; then ui_print "$2"; fi
  548.     else
  549.       if [ ! -z "$3" ]; then
  550.         if [ ! -z "$1" ]; then
  551.           ui_print_less "$1"
  552.         else
  553.           ui_print_less "$UI_PRINT_LAST"
  554.         fi
  555.         ui_print_always "$3";
  556.       fi
  557.       CONTINUE=false
  558.     fi
  559.   fi
  560. }

  561. # check_zero_def "progress message" "command"
  562. check_zero_def() {
  563.   check_zero "$1" "" "--- Failure, aborting" "$2"
  564. }

  565. # find boot image partition if not set already
  566. find_boot_image() {
  567.   # expand the detection if we find more, instead of reading from fstab, because unroot
  568.   # from the SuperSU APK doesn't have the fstab to read from
  569.   if [ -z "$BOOTIMAGE" ]; then
  570.     for PARTITION in kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do
  571.       BOOTIMAGE=$(readlink /dev/block/by-name/$PARTITION || readlink /dev/block/platform/*/by-name/$PARTITION || readlink /dev/block/platform/*/*/by-name/$PARTITION)
  572.       if [ ! -z "$BOOTIMAGE" ]; then break; fi
  573.     done
  574.   fi
  575. }

  576. # use only on 6.0+, tries to read current boot image and detect if we can do a system install
  577. # without any boot image patching. Requirements:
  578. # - /data readable
  579. # - not pre-patched by SuperSU
  580. # - dm-verity disabled
  581. # - init loads from /data/security/current/sepolicy
  582. # - sepolicy has init load_policy or permissive init
  583. # It symlink/patches the relevant files to /data, and sets SYSTEMLESS variable if not already set
  584. detect_systemless_required() {
  585.   OLD_SYSTEMLESS=$SYSTEMLESS
  586.   if [ "$OLD_SYSTEMLESS" = "detect" ]; then
  587.     # we don't override a pre-set true/false value
  588.     SYSTEMLESS=true
  589.   fi

  590.   # check /data mounted
  591.   if (! is_mounted /data); then
  592.     return
  593.   fi

  594.   # find boot image partition
  595.   find_boot_image

  596.   CONTINUE=true
  597.   if [ -z "$BOOTIMAGE" ]; then
  598.     # no boot image partition detected, abort
  599.     return
  600.   fi

  601.   # extract ramdisk from boot image
  602.   rm -rf /sutmp
  603.   mkdir /sutmp

  604.   check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --bootimg-extract-ramdisk $BOOTIMAGE /sutmp/ramdisk.packed"
  605.   check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --ungzip /sutmp/ramdisk.packed /sutmp/ramdisk"
  606.   if (! $CONTINUE); then return; fi

  607.   # detect SuperSU patch
  608.   LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --patch-test /sutmp/ramdisk
  609.   if [ $? -ne 0 ]; then
  610.     return
  611.   fi

  612.   # detect dm-verity in use
  613.   for i in `LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-ls /sutmp/ramdisk | grep fstab`; do
  614.     rm -f /sutmp/fstab

  615.     check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-extract /sutmp/ramdisk $i /sutmp/fstab"
  616.     if (! $CONTINUE); then return; fi

  617.     VERIFY=$(cat /sutmp/fstab | grep verify | grep system)
  618.     if [ $? -eq 0 ]; then
  619.       # verify flag found, dm-verity probably enabled, modifying /system may prevent boot
  620.       return
  621.     fi
  622.   done

  623.   # detect init loading from /data/security/current/sepolicy
  624.   check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-extract /sutmp/ramdisk init /sutmp/init"
  625.   if (! $CONTINUE); then return; fi

  626.   CURRENT=$(cat /sutmp/init | grep "/data/security/current/sepolicy")
  627.   if [ $? -ne 0 ]; then
  628.     # this init doesn't load from the default sepolicy override location
  629.     return
  630.   fi

  631.   # extract sepolicy
  632.   check_zero "" "" "" "LD_LIBRARY_PATH=$RAMDISKLIB $BIN/sukernel --cpio-extract /sutmp/ramdisk sepolicy /sutmp/sepolicy"
  633.   if (! $CONTINUE); then return; fi

  634.   GO=false

  635.   # detect init permissive
  636.   if (! $GO); then
  637.     INIT_PERMISSIVE=$(LD_LIBRARY_PATH=$RAMDISKLIB $BIN/supolicy --dumpav /sutmp/sepolicy | grep "[TYPE]" | grep " init (PERMISSIVE) ")
  638.     if [ $? -eq 0 ]; then
  639.       GO=true
  640.     fi
  641.   fi

  642.   # detect init load_policy
  643.   if (! $GO); then
  644.     INIT_LOAD_POLICY=$(LD_LIBRARY_PATH=$RAMDISKLIB $BIN/supolicy --dumpav /sutmp/sepolicy | grep "[AV]" | grep " ALLOW " | grep " init-->kernel (security) " | grep "load_policy")
  645.     if [ $? -eq 0 ]; then
  646.       GO=true
  647.     fi
  648.   fi

  649.   # copy files to /data
  650.   if (! $GO); then return; fi

  651.   rm -rf /data/security/*
  652.   mkdir /data/security/current
  653.   set_perm 1000 1000 0755 /data/security/current u:object_r:security_file:s0

  654.   LD_LIBRARY_PATH=$RAMDISKLIB $BIN/supolicy --file /sutmp/sepolicy /data/security/current/sepolicy
  655.   set_perm 1000 1000 0644 /data/security/current/sepolicy u:object_r:security_file:s0

  656.   for i in seapp_contexts file_contexts file_contexts.bin property_contexts service_contexts selinux_version; do
  657.     ln -s /$i /data/security/current/$i
  658.   done

  659.   ln -s /system/etc/security/mac_permissions.xml /data/security/current/mac_permissions.xml

  660.   # if we reach this point, we can do a system install
  661.   if [ "$OLD_SYSTEMLESS" = "detect" ]; then
  662.     # we don't override a pre-set true/false value
  663.     SYSTEMLESS=false
  664.   fi
  665. }

  666. ui_print " "
  667. ui_print        "*****************"
  668. ui_print_always "SuperSU installer"
  669. ui_print        "*****************"

  670. ui_print "- Mounting /system, /data and rootfs"

  671. mount -o ro /system
  672. toolbox_mount /system ro
  673. mount /data
  674. toolbox_mount /data
  675. mount -o rw,remount /
  676. mount -o rw,remount / /

  677. if [ -z "$BIN" ]; then
  678.   # TWRP went full retard
  679.   if [ ! -f "/sbin/unzip" ]; then
  680.     ui_print "- BAD RECOVERY DETECTED, NO UNZIP, ABORTING"
  681.     exit 1
  682.   fi
  683. fi

  684. if [ -z "$NOOVERRIDE" ]; then
  685.   # read override variables
  686.   getvar SYSTEMLESS
  687.   getvar PATCHBOOTIMAGE
  688.   getvar BOOTIMAGE
  689.   getvar STOCKBOOTIMAGE
  690.   getvar BINDSYSTEMXBIN
  691.   getvar PERMISSIVE
  692.   getvar KEEPVERITY
  693.   getvar KEEPFORCEENCRYPT
  694. fi
  695. if [ -z "$SYSTEMLESS" ]; then
  696.   # detect if we need systemless, based on Android version and boot image
  697.   SYSTEMLESS=detect
  698. fi
  699. if [ -z "$PATCHBOOTIMAGE" ]; then
  700.   # only if we end up doing a system-less install
  701.   PATCHBOOTIMAGE=true
  702. fi
  703. if [ -z "$BINDSYSTEMXBIN" ]; then
  704.   # causes launch_daemonsu to bind over /system/xbin, disabled by default
  705.   BINDSYSTEMXBIN=false
  706. fi
  707. if [ -z "$PERMISSIVE" ]; then
  708.   # don't make everything fake-permissive
  709.   PERMISSIVE=false
  710. fi
  711. if [ -z "$KEEPVERITY" ]; then
  712.   # we don't keep dm-verity by default
  713.   KEEPVERITY=false
  714. fi
  715. if [ -z "$KEEPFORCEENCRYPT" ]; then
  716.   # we don't keep forceencrypt by default
  717.   KEEPFORCEENCRYPT=false
  718. fi

  719. API=$(cat /system/build.prop | grep "ro.build.version.sdk=" | dd bs=1 skip=21 count=2)
  720. ABI=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi=" | dd bs=1 skip=19 count=3)
  721. ABILONG=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi=" | dd bs=1 skip=19)
  722. ABI2=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi2=" | dd bs=1 skip=20 count=3)
  723. SUMOD=06755
  724. SUGOTE=false
  725. SUPOLICY=false
  726. INSTALL_RECOVERY_CONTEXT=u:object_r:system_file:s0
  727. MKSH=/system/bin/mksh
  728. PIE=
  729. SU=su
  730. ARCH=arm
  731. APKFOLDER=false
  732. APKNAME=/system/app/Superuser.apk
  733. APPPROCESS=false
  734. APPPROCESS64=false
  735. SYSTEMLIB=/system/lib
  736. RAMDISKLIB=$SYSTEMLIB
  737. RWSYSTEM=true

  738. if [ "$API" -le "21" ]; then
  739.   # needed for some intermediate AOSP verions

  740.   remount_system_rw

  741.   cat /system/bin/toolbox > /system/toolbox
  742.   chmod 0755 /system/toolbox
  743.   ch_con /system/toolbox
  744. fi

  745. if [ "$ABI" = "x86" ]; then ARCH=x86; fi;
  746. if [ "$ABI2" = "x86" ]; then ARCH=x86; fi;
  747. if [ "$API" -eq "$API" ]; then
  748.   if [ "$API" -ge "17" ]; then
  749.     SUGOTE=true
  750.     PIE=.pie
  751.     if [ "$ARCH" = "x86" ]; then SU=su.pie; fi;
  752.     if [ "$ABILONG" = "armeabi-v7a" ]; then ARCH=armv7; fi;
  753.     if [ "$ABI" = "mip" ]; then ARCH=mips; fi;
  754.     if [ "$ABILONG" = "mips" ]; then ARCH=mips; fi;
  755.   fi
  756.   if [ "$API" -ge "18" ]; then
  757.     SUMOD=0755
  758.   fi
  759.   if [ "$API" -ge "20" ]; then
  760.     if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
  761.     if [ "$ABILONG" = "mips64" ]; then ARCH=mips64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
  762.     if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
  763.     APKFOLDER=true
  764.     APKNAME=/system/app/SuperSU/SuperSU.apk
  765.   fi
  766.   if [ "$API" -ge "19" ]; then
  767.     SUPOLICY=true
  768.     if [ "$(LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox ls -lZ /system/bin/toolbox | grep toolbox_exec > /dev/null; echo $?)" -eq "0" ]; then
  769.       INSTALL_RECOVERY_CONTEXT=u:object_r:toolbox_exec:s0
  770.     fi
  771.   fi
  772.   if [ "$API" -ge "21" ]; then
  773.     APPPROCESS=true
  774.   fi
  775.   if [ "$API" -ge "22" ]; then
  776.     SUGOTE=false
  777.   fi
  778. fi
  779. if [ ! -f $MKSH ]; then
  780.   MKSH=/system/bin/sh
  781. fi

  782. #ui_print "DBG [$API] [$ABI] [$ABI2] [$ABILONG] [$ARCH] [$MKSH]"

  783. if [ -z "$BIN" ]; then
  784.   ui_print "- Extracting files"

  785.   cd /tmp
  786.   mkdir supersu
  787.   cd supersu

  788.   unzip -o "$ZIP"

  789.   BIN=/tmp/supersu/$ARCH
  790.   COM=/tmp/supersu/common
  791. fi

  792. # execute binaries from ramdisk
  793. chmod -R 0755 $BIN/*
  794. RAMDISKLIB=$BIN:$SYSTEMLIB

  795. if [ "$API" -ge "19" ]; then
  796.   # 4.4+: permissive all teh things
  797.   LD_LIBRARY_PATH=$RAMDISKLIB $BIN/supolicy --live "permissive *"
  798. fi

  799. SAMSUNG=false
  800. if [ "$API" -eq "$API" ]; then
  801.   SAMSUNG_CHECK=$(cat /system/build.prop | grep "ro.build.fingerprint=" | grep -i "samsung")
  802.   if [ $? -eq 0 ]; then
  803.     SAMSUNG=true
  804.   fi

  805.   if [ "$API" -ge "23" ]; then
  806.     # 6.0+
  807.     ui_print "- Detecting system compatibility"
  808.     detect_systemless_required

  809.     if ($SYSTEMLESS); then
  810.       RWSYSTEM=false
  811.     fi
  812.   elif [ "$API" -ge "22" ]; then
  813.     if [ "$SYSTEMLESS" = "detect" ]; then
  814.       SYSTEMLESS=true
  815.     fi

  816.     # 5.1/Samsung
  817.     if ($SYSTEMLESS); then
  818.       if ($SAMSUNG); then
  819.         RWSYSTEM=false
  820.       fi
  821.     fi
  822.   fi
  823. fi

  824. # Do not use SYSTEMLESS after this point, but refer to RWSYSTEM

  825. if ($RWSYSTEM); then
  826.   ui_print "- System mode"

  827.   remount_system_rw

  828.   ui_print "- Disabling OTA survival"
  829.   chmod 0755 $BIN/chattr$PIE
  830.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/bin/su
  831.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/su
  832.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/bin/.ext/.su
  833.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/sbin/su
  834.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /vendor/sbin/su
  835.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /vendor/bin/su
  836.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /vendor/xbin/su
  837.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/daemonsu
  838.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/sugote
  839.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/sugote_mksh
  840.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/supolicy
  841.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/ku.sud
  842.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/.ku
  843.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/.su
  844.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/lib/libsupol.so
  845.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/lib64/libsupol.so
  846.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/etc/install-recovery.sh
  847.   LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/bin/install-recovery.sh

  848.   ui_print "- Removing old files"

  849.   if [ -f "/system/bin/install-recovery.sh" ]; then
  850.     if [ ! -f "/system/bin/install-recovery_original.sh" ]; then
  851.       mv /system/bin/install-recovery.sh /system/bin/install-recovery_original.sh
  852.       ch_con /system/bin/install-recovery_original.sh
  853.     fi
  854.   fi
  855.   if [ -f "/system/etc/install-recovery.sh" ]; then
  856.     if [ ! -f "/system/etc/install-recovery_original.sh" ]; then
  857.       mv /system/etc/install-recovery.sh /system/etc/install-recovery_original.sh
  858.       ch_con /system/etc/install-recovery_original.sh
  859.     fi
  860.   fi

  861.   # only wipe these files in /system install, so not part of the wipe_ functions

  862.   rm -f /system/bin/install-recovery.sh
  863.   rm -f /system/etc/install-recovery.sh

  864.   rm -f /system/bin/su
  865.   rm -f /system/xbin/su
  866.   rm -f /system/sbin/su
  867.   rm -f /vendor/sbin/su
  868.   rm -f /vendor/bin/su
  869.   rm -f /vendor/xbin/su

  870.   rm -rf /data/app/eu.chainfire.supersu-*
  871.   rm -rf /data/app/eu.chainfire.supersu.apk

  872.   wipe_system_files_if_present
  873.   wipe_data_competitors_and_cache

  874.   rm /data/su.img
  875.   rm /cache/su.img

  876.   ui_print "- Creating space"
  877.   if ($APKFOLDER); then
  878.     if [ -f "/system/app/Maps/Maps.apk" ]; then
  879.       cp /system/app/Maps/Maps.apk /Maps.apk
  880.       rm /system/app/Maps/Maps.apk
  881.     fi
  882.     if [ -f "/system/app/GMS_Maps/GMS_Maps.apk" ]; then
  883.       cp /system/app/GMS_Maps/GMS_Maps.apk /GMS_Maps.apk
  884.       rm /system/app/GMS_Maps/GMS_Maps.apk
  885.     fi
  886.     if [ -f "/system/app/YouTube/YouTube.apk" ]; then
  887.       cp /system/app/YouTube/YouTube.apk /YouTube.apk
  888.       rm /system/app/YouTube/YouTube.apk
  889.     fi
  890.   else
  891.     if [ -f "/system/app/Maps.apk" ]; then
  892.       cp /system/app/Maps.apk /Maps.apk
  893.       rm /system/app/Maps.apk
  894.     fi
  895.     if [ -f "/system/app/GMS_Maps.apk" ]; then
  896.       cp /system/app/GMS_Maps.apk /GMS_Maps.apk
  897.       rm /system/app/GMS_Maps.apk
  898.     fi
  899.     if [ -f "/system/app/YouTube.apk" ]; then
  900.       cp /system/app/YouTube.apk /YouTube.apk
  901.       rm /system/app/YouTube.apk
  902.     fi
  903.   fi

  904.   ui_print "- Placing files"

  905.   mkdir /system/bin/.ext
  906.   set_perm 0 0 0777 /system/bin/.ext
  907.   cp_perm 0 0 $SUMOD $BIN/$SU /system/bin/.ext/.su
  908.   cp_perm 0 0 $SUMOD $BIN/$SU /system/xbin/su
  909.   cp_perm 0 0 0755 $BIN/$SU /system/xbin/daemonsu
  910.   if ($SUGOTE); then
  911.     cp_perm 0 0 0755 $BIN/$SU /system/xbin/sugote u:object_r:zygote_exec:s0
  912.     cp_perm 0 0 0755 $MKSH /system/xbin/sugote-mksh
  913.   fi
  914.   if ($SUPOLICY); then
  915.     cp_perm 0 0 0755 $BIN/supolicy /system/xbin/supolicy
  916.     cp_perm 0 0 0644 $BIN/libsupol.so $SYSTEMLIB/libsupol.so
  917.   fi
  918.   if ($APKFOLDER); then
  919.     mkdir /system/app/SuperSU
  920.     set_perm 0 0 0755 /system/app/SuperSU
  921.   fi
  922.   cp_perm 0 0 0644 $COM/Superuser.apk $APKNAME
  923.   cp_perm 0 0 0755 $COM/install-recovery.sh /system/etc/install-recovery.sh
  924.   ln_con /system/etc/install-recovery.sh /system/bin/install-recovery.sh
  925.   if ($APPPROCESS); then
  926.     rm /system/bin/app_process
  927.     ln_con /system/xbin/daemonsu /system/bin/app_process
  928.     if ($APPPROCESS64); then
  929.       if [ ! -f "/system/bin/app_process64_original" ]; then
  930.         mv /system/bin/app_process64 /system/bin/app_process64_original
  931.       else
  932.         rm /system/bin/app_process64
  933.       fi
  934.       ln_con /system/xbin/daemonsu /system/bin/app_process64
  935.       if [ ! -f "/system/bin/app_process_init" ]; then
  936.         cp_perm 0 2000 0755 /system/bin/app_process64_original /system/bin/app_process_init
  937.       fi
  938.     else
  939.       if [ ! -f "/system/bin/app_process32_original" ]; then
  940.         mv /system/bin/app_process32 /system/bin/app_process32_original
  941.       else
  942.         rm /system/bin/app_process32
  943.       fi
  944.       ln_con /system/xbin/daemonsu /system/bin/app_process32
  945.       if [ ! -f "/system/bin/app_process_init" ]; then
  946.         cp_perm 0 2000 0755 /system/bin/app_process32_original /system/bin/app_process_init
  947.       fi
  948.     fi
  949.   fi
  950.   cp_perm 0 0 0744 $COM/99SuperSUDaemon /system/etc/init.d/99SuperSUDaemon
  951.   echo 1 > /system/etc/.installed_su_daemon
  952.   set_perm 0 0 0644 /system/etc/.installed_su_daemon

  953.   ui_print "- Restoring files"
  954.   if ($APKFOLDER); then
  955.     if [ -f "/Maps.apk" ]; then
  956.       cp_perm 0 0 0644 /Maps.apk /system/app/Maps/Maps.apk
  957.       rm /Maps.apk
  958.     fi
  959.     if [ -f "/GMS_Maps.apk" ]; then
  960.       cp_perm 0 0 0644 /GMS_Maps.apk /system/app/GMS_Maps/GMS_Maps.apk
  961.       rm /GMS_Maps.apk
  962.     fi
  963.     if [ -f "/YouTube.apk" ]; then
  964.       cp_perm 0 0 0644 /YouTube.apk /system/app/YouTube/YouTube.apk
  965.       rm /YouTube.apk
  966.     fi
  967.   else
  968.     if [ -f "/Maps.apk" ]; then
  969.       cp_perm 0 0 0644 /Maps.apk /system/app/Maps.apk
  970.       rm /Maps.apk
  971.     fi
  972.     if [ -f "/GMS_Maps.apk" ]; then
  973.       cp_perm 0 0 0644 /GMS_Maps.apk /system/app/GMS_Maps.apk
  974.       rm /GMS_Maps.apk
  975.     fi
  976.     if [ -f "/YouTube.apk" ]; then
  977.       cp_perm 0 0 0644 /YouTube.apk /system/app/YouTube.apk
  978.       rm /YouTube.apk
  979.     fi
  980.   fi

  981.   ui_print "- Post-installation script"
  982.   rm /system/toybox
  983.   rm /system/toolbox
  984.   LD_LIBRARY_PATH=$SYSTEMLIB /system/xbin/su --install
  985. else
  986.   ui_print "- System-less mode, boot image support required"

  987.   SUIMG=/data/su.img
  988.   HAVEDATA=true
  989.   if (! is_mounted /data); then
  990.     SUIMG=/cache/su.img
  991.     HAVEDATA=false
  992.   fi

  993.   ui_print "- Creating image"

  994.   # we want a 96M image, for SuperSU files and potential mods such as systemless xposed
  995.   # attempt smaller sizes on failure, and hope the launch_daemonsu.sh script succeeds
  996.   # in resizing to 96M later
  997.   for SUIMGSIZE in 96M 64M 32M 16M; do
  998.     if [ ! -f "$SUIMG" ]; then make_ext4fs -l $SUIMGSIZE -a /su -S $COM/file_contexts_image $SUIMG; fi
  999.     if [ ! -f "$SUIMG" ]; then LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/make_ext4fs -l $SUIMGSIZE -a /su -S $COM/file_contexts_image $SUIMG; fi
  1000.   done

  1001.   if [ -f "$SUIMG" ]; then
  1002.     LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/e2fsck -p -f $SUIMG
  1003.   fi

  1004.   ui_print "- Mounting image"

  1005.   mkdir /su

  1006.   # 'losetup -f' is unreliable across devices/recoveries
  1007.   LOOPDEVICE=
  1008.   for LOOP in 0 1 2 3 4 5 6 7; do
  1009.     if (! is_mounted /su); then
  1010.       LOOPDEVICE=/dev/block/loop$LOOP
  1011.       if [ ! -f "$LOOPDEVICE" ]; then
  1012.         mknod $LOOPDEVICE b 7 $LOOP
  1013.       fi
  1014.       losetup $LOOPDEVICE $SUIMG
  1015.       if [ "$?" -eq "0" ]; then
  1016.         mount -t ext4 -o loop $LOOPDEVICE /su
  1017.         if (! is_mounted /su); then
  1018.           /system/bin/toolbox mount -t ext4 -o loop $LOOPDEVICE /su
  1019.         fi
  1020.         if (! is_mounted /su); then
  1021.           /system/bin/toybox mount -t ext4 -o loop $LOOPDEVICE /su
  1022.         fi
  1023.       fi
  1024.       if (is_mounted /su); then
  1025.         break;
  1026.       fi
  1027.     fi
  1028.   done

  1029.   ui_print "- Creating paths"

  1030.   mkdir /su/bin
  1031.   set_perm 0 0 0751 /su/bin
  1032.   mkdir /su/xbin
  1033.   set_perm 0 0 0755 /su/xbin
  1034.   mkdir /su/lib
  1035.   set_perm 0 0 0755 /su/lib
  1036.   mkdir /su/etc
  1037.   set_perm 0 0 0755 /su/etc
  1038.   mkdir /su/su.d
  1039.   set_perm 0 0 0700 /su/su.d

  1040.   ui_print "- Removing old files"

  1041.   wipe_system_files_if_present
  1042.   wipe_data_competitors_and_cache

  1043.   rm -rf /su/bin/app_process
  1044.   rm -rf /su/bin/sush
  1045.   rm -rf /su/bin/daemonsu
  1046.   rm -rf /su/bin/daemonsu_*
  1047.   rm -rf /su/bin/su
  1048.   rm -rf /su/bin/su_*
  1049.   rm -rf /su/bin/supolicy
  1050.   rm -rf /su/bin/supolicy_*
  1051.   rm -rf /su/lib/libsupol.so
  1052.   rm -rf /su/lib/libsupol_*
  1053.   rm -rf /su/bin/sukernel

  1054.   ui_print "- Placing files"

  1055.   # Copy binaries and utilities
  1056.   cp_perm 0 0 0755 $BIN/$SU /su/bin/su
  1057.   cp_perm 0 0 0755 $BIN/$SU /su/bin/daemonsu
  1058.   ln_con /su/bin/su /su/bin/supolicy
  1059.   cp_perm 0 0 0755 $BIN/supolicy /su/bin/supolicy_wrapped
  1060.   cp_perm 0 0 0644 $BIN/libsupol.so /su/lib/libsupol.so
  1061.   cp_perm 0 0 0755 $BIN/sukernel /su/bin/sukernel

  1062.   # Copy APK, installation is done by /sbin/launch_daemonsu.sh
  1063.   if ($HAVEDATA); then
  1064.     cp_perm 1000 1000 0600 $COM/Superuser.apk /data/SuperSU.apk

  1065.     # Wipe /data/security to prevent SELinux policy override
  1066.     # Important to keep the folder itself
  1067.     rm -rf /data/security/*
  1068.   else
  1069.     cp_perm 1000 1000 0600 $COM/Superuser.apk /cache/SuperSU.apk
  1070.   fi

  1071.   # Fix Samsung deep sleep issue. Affects enough millions of users to include.
  1072.   if ($SAMSUNG); then
  1073.     cp_perm 0 0 0700 $COM/000000deepsleep /su/su.d/000000deepsleep
  1074.   fi

  1075.   if ($BINDSYSTEMXBIN); then
  1076.     mkdir /su/xbin_bind
  1077.     set_perm 0 0 0755 /su/xbin_bind
  1078.   else
  1079.     rm -rf /su/xbin_bind
  1080.   fi

  1081.   if ($PATCHBOOTIMAGE); then
  1082.     ui_print " "
  1083.     ui_print        "******************"
  1084.     ui_print_always "Boot image patcher"
  1085.     ui_print        "******************"

  1086.     ui_print "- Finding boot image"
  1087.     find_boot_image

  1088.     CONTINUE=true
  1089.     if [ -z "$BOOTIMAGE" ]; then
  1090.       ui_print_less "$UI_PRINT_LAST"
  1091.       ui_print_always "--- Boot image: not found, aborting"
  1092.       CONTINUE=false
  1093.     else
  1094.       ui_print "--- Boot image: $BOOTIMAGE"
  1095.     fi

  1096.     if [ -z "$STOCKBOOTIMAGE" ]; then
  1097.       STOCKBOOTIMAGE=$BOOTIMAGE
  1098.     fi

  1099.     rm -rf /sutmp
  1100.     mkdir /sutmp

  1101.     IMAGETYPE=android
  1102.     LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-type $BOOTIMAGE
  1103.     if [ $? -eq 2 ]; then
  1104.       IMAGETYPE=chromeos
  1105.     fi

  1106.     check_zero_def "- Extracting ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-extract-ramdisk $BOOTIMAGE /sutmp/ramdisk.packed"
  1107.     check_zero_def "- Decompressing ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --ungzip /sutmp/ramdisk.packed /sutmp/ramdisk"

  1108.     if ($CONTINUE); then
  1109.       ui_print "- Checking patch status"
  1110.       LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --patch-test /sutmp/ramdisk
  1111.       if [ $? -ne 0 ]; then
  1112.         ui_print "--- Already patched, attempting to find stock backup"

  1113.         if ($CONTINUE); then
  1114.           LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --restore /sutmp/ramdisk /sutmp/stock_boot.img
  1115.           if [ $? -ne 0 ]; then
  1116.             ui_print_always "--- Stock restore failed, attempting ramdisk restore"
  1117.             CONTINUE=false
  1118.           else
  1119.             ui_print "--- Stock backup restored"
  1120.             STOCKBOOTIMAGE=/sutmp/stock_boot.img
  1121.           fi
  1122.         fi

  1123.         check_zero_def "- Extracting ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-extract-ramdisk /sutmp/stock_boot.img /sutmp/ramdisk.packed"
  1124.         check_zero_def "- Decompressing ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --ungzip /sutmp/ramdisk.packed /sutmp/ramdisk"
  1125.         check_zero "- Checking patch status" "" "--- Already patched, attempting ramdisk restore" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --patch-test /sutmp/ramdisk"

  1126.         if (! $CONTINUE); then
  1127.           LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-restore /sutmp/ramdisk /sutmp/ramdisk
  1128.           if [ $? -ne 0 ]; then
  1129.             ui_print_always "--- Ramdisk restore failed, aborting"
  1130.           else
  1131.             ui_print "--- Ramdisk backup restored (OTA impossible)"
  1132.             CONTINUE=true
  1133.           fi
  1134.           check_zero "- Checking patch status" "" "--- Already patched, aborting" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --patch-test /sutmp/ramdisk"
  1135.         fi
  1136.       else
  1137.         ui_print "- Creating backup"
  1138.         rm /data/stock_boot_*.img
  1139.         rm /data/stock_boot_*.img.gz
  1140.         rm /cache/stock_boot_*.img
  1141.         rm /cache/stock_boot_*.img.gz
  1142.         LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --backup $BOOTIMAGE
  1143.         if [ $? -ne 0 ]; then
  1144.           ui_print "--- Backup failed"
  1145.         fi
  1146.       fi
  1147.     fi

  1148.     if ($CONTINUE); then
  1149.       cp_perm 0 0 0644 /sutmp/ramdisk /sutmp/ramdisk.original

  1150.       ui_print "- Patching sepolicy"

  1151.       check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-extract /sutmp/ramdisk sepolicy /sutmp/sepolicy"

  1152.       if ($CONTINUE); then
  1153.         if ($PERMISSIVE); then
  1154.           LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/supolicy --file /sutmp/sepolicy /sutmp/sepolicy.patched "permissive *"
  1155.         else
  1156.           LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/supolicy --file /sutmp/sepolicy /sutmp/sepolicy.patched
  1157.         fi
  1158.         if [ ! -f "/sutmp/sepolicy.patched" ]; then
  1159.           ui_print_less "$UI_PRINT_LAST"
  1160.           ui_print_always "--- Failure, aborting"
  1161.           CONTINUE=false
  1162.         fi
  1163.       fi

  1164.       check_zero_def "" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 644 sepolicy /sutmp/sepolicy.patched"
  1165.     fi

  1166.     check_zero_def "- Adding daemon launcher" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 700 sbin/launch_daemonsu.sh $COM/launch_daemonsu.sh"
  1167.     check_zero_def "- Adding init script" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-add /sutmp/ramdisk /sutmp/ramdisk 750 init.supersu.rc $COM/init.supersu.rc"

  1168.     check_zero_def "- Creating mount point" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-mkdir /sutmp/ramdisk /sutmp/ramdisk 755 su"

  1169.     COMMAND="LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --patch /sutmp/ramdisk /sutmp/ramdisk $STOCKBOOTIMAGE"
  1170.     if ($KEEPVERITY); then
  1171.       COMMAND="$COMMAND --keep-verity"
  1172.     fi
  1173.     if ($KEEPFORCEENCRYPT); then
  1174.       COMMAND="$COMMAND --keep-forceencrypt"
  1175.     fi
  1176.     check_zero_def "- Patching init.*.rc, fstabs, file_contexts, dm-verity" "$COMMAND"

  1177.     if [ -f "/data/custom_ramdisk_patch.sh" ]; then
  1178.         check_zero_def "- Calling user ramdisk patch script" "sh /data/custom_ramdisk_patch.sh /sutmp/ramdisk"
  1179.     fi

  1180.     check_zero_def "- Creating ramdisk backup" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-backup /sutmp/ramdisk.original /sutmp/ramdisk /sutmp/ramdisk"

  1181.     check_zero_def "- Compressing ramdisk" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --gzip /sutmp/ramdisk /sutmp/ramdisk.packed"

  1182.     if [ "$IMAGETYPE" = "chromeos" ]; then
  1183.       $BIN/chromeos/futility vbutil_kernel --get-vmlinuz $STOCKBOOTIMAGE --vmlinuz-out /sutmp/boot.chromeos.img
  1184.       STOCKBOOTIMAGE=/sutmp/boot.chromeos.img
  1185.     fi

  1186.     check_zero_def "- Creating boot image" "LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --bootimg-replace-ramdisk $STOCKBOOTIMAGE /sutmp/ramdisk.packed /sutmp/boot.img"

  1187.     if [ "$IMAGETYPE" = "chromeos" ]; then
  1188.       ui_print "- Signing boot image"
  1189.       $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
  1190.       if [ -f "/sutmp/boot.img.signed" ]; then
  1191.         rm -rf /sutmp/boot.img
  1192.         mv /sutmp/boot.img.signed /sutmp/boot.img
  1193.       else
  1194.         ui_print_less "$UI_PRINT_LAST"
  1195.         ui_print_always "--- Failure, aborting"
  1196.         $CONTINUE=false
  1197.       fi
  1198.     fi

  1199.     if ($CONTINUE); then
  1200.         # might return 1 even if we do not want to abort
  1201.         ui_print "- Applying hex patches"
  1202.         /su/bin/sukernel --hexpatch $COM/hexpatch /sutmp/boot.img /sutmp/boot.img
  1203.     fi

  1204.     if [ -f "/data/custom_boot_image_patch.sh" ]; then
  1205.         check_zero_def "- Calling user boot image patch script" "sh /data/custom_boot_image_patch.sh /sutmp/boot.img"
  1206.     fi

  1207.     if ($CONTINUE); then
  1208.       DEV=$(echo `resolve_link $BOOTIMAGE` | grep /dev/block/)
  1209.       if [ $? -eq 0 ]; then
  1210.         ui_print "- Flashing boot image"
  1211.         dd if=/dev/zero of=$BOOTIMAGE bs=4096
  1212.       else
  1213.         ui_print "- Saving boot image"
  1214.       fi

  1215.       if ($SAMSUNG); then
  1216.         # Prevent "KERNEL IS NOT SEANDROID ENFORCING"
  1217.         SAMSUNG_CHECK=$(cat /sutmp/boot.img | grep SEANDROIDENFORCE)
  1218.         if [ $? -ne 0 ]; then
  1219.           echo -n "SEANDROIDENFORCE" >> /sutmp/boot.img
  1220.         fi
  1221.       fi

  1222.       dd if=/sutmp/boot.img of=$BOOTIMAGE bs=4096
  1223.     fi

  1224.     rm -rf /sutmp
  1225.   fi

  1226.   umount /su
  1227.   losetup -d $LOOPDEVICE

  1228.   ui_print " "
  1229.   ui_print "*************************"
  1230.   ui_print "    IMPORTANT NOTICES    "
  1231.   ui_print "*************************"

  1232.   TWRP2=$(cat /tmp/recovery.log | grep "ro.twrp.version=2");
  1233.   if [ $? -eq 0 ]; then
  1234.     ui_print "If TWRP offers to install"
  1235.     ui_print "SuperSU, do *NOT* let it!"
  1236.     ui_print "*************************"
  1237.   fi

  1238.   ui_print "First reboot may take a  "
  1239.   ui_print "few minutes. It can also "
  1240.   ui_print "loop a few times. Do not "
  1241.   ui_print "interrupt the process!   "
  1242.   ui_print "*************************"
  1243.   ui_print " "

  1244.   if (! $LESSLOGGING); then
  1245.     sleep 5
  1246.   fi
  1247. fi

  1248. ui_print "- Unmounting /system"
  1249. umount /system

  1250. ui_print_always "- Done !"
  1251. exit 0
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
发表于 2018-9-3 19:46:40 | 显示全部楼层
学习学习,谢谢分享………………
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

技术交流售后群

QQ|小黑屋|手机版|站点找错-建议|AI手机网 |Sitemap



GMT+8, 2024-4-26 07:19 , Processed in 0.161920 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表