update-binary 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/sbin/sh
  2. OUTFD="/proc/self/fd/$2"
  3. ZIP=$3
  4. set_con() {
  5. chcon -h u:object_r:"$1":s0 $2
  6. chcon u:object_r:"$1":s0 $2
  7. }
  8. set_perm() {
  9. chmod $1 $2
  10. }
  11. set_owner() {
  12. chown $1:$2 $3
  13. }
  14. ui_print() {
  15. echo "ui_print $1" > "$OUTFD";
  16. echo "ui_print" > "$OUTFD";
  17. }
  18. getprop2() {
  19. grep -m 1 "^$2=" $1 | cut -d= -f2
  20. }
  21. nice_arch() {
  22. case $1 in
  23. aarch64*|armv8*)
  24. echo "arm64"
  25. ;;
  26. arm*)
  27. echo "arm"
  28. ;;
  29. *)
  30. echo $1
  31. ;;
  32. esac
  33. }
  34. cleanup() {
  35. ui_print "Cleaning up files"
  36. rm -rf $TMP/system
  37. rm -rf $TMP/bin
  38. rm $TMP/toybox
  39. ui_print "Unmounting partitions"
  40. umount -l "$SYSTEM_MNT"
  41. umount -l /product || true
  42. umount -l /system_ext || true
  43. }
  44. error() {
  45. ui_print "$1"
  46. cleanup
  47. exit 1
  48. }
  49. error_no_space() {
  50. error "Not enough space for GApps! Aborting"
  51. }
  52. error_mounting() {
  53. error "Could not mount $1! Aborting"
  54. }
  55. get_block_for_mount_point() {
  56. grep -v "^#" /etc/recovery.fstab | grep "[[:blank:]]$1[[:blank:]]" | tail -n1 | tr -s [:blank:] ' ' | cut -d' ' -f1
  57. }
  58. find_block() {
  59. local name="$1"
  60. local fstab_entry=$(get_block_for_mount_point "/$name")
  61. # P-SAR hacks
  62. [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/")
  63. [ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/system_root")
  64. local dev
  65. if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
  66. if [ -n "$fstab_entry" ]; then
  67. dev="${BLK_PATH}/${fstab_entry}${SLOT_SUFFIX}"
  68. else
  69. dev="${BLK_PATH}/${name}${SLOT_SUFFIX}"
  70. fi
  71. else
  72. if [ -n "$fstab_entry" ]; then
  73. dev="${fstab_entry}${SLOT_SUFFIX}"
  74. else
  75. dev="${BLK_PATH}/${name}${SLOT_SUFFIX}"
  76. fi
  77. fi
  78. if [ -b "$dev" ]; then
  79. echo "$dev"
  80. fi
  81. }
  82. compute_apps_size() {
  83. NEEDED_STORAGE_SYSTEM=$(expr $(du -cs `find -maxdepth 1 -mindepth 1 ! -name product ! -name system_ext` | tail -n1 | cut -f1) + $STORAGE_BUFFER)
  84. NEEDED_STORAGE_PRODUCT=$(expr $(du -s ./product | cut -f1) + $STORAGE_BUFFER)
  85. NEEDED_STORAGE_SYSTEM_EXT=$(expr $(du -s ./system_ext | cut -f1) + $STORAGE_BUFFER)
  86. RECLAIMABLE_STORAGE_SYSTEM=$(find . ! -path "./product/*" ! -path "./system_ext/*" -type f | sed "s|^./|$SYSTEM_OUT/|" | xargs ls -d 2>/dev/null | xargs du -cs PLACEHOLDER 2>/dev/null | tail -n1 | cut -f1)
  87. NEEDED_STORAGE_SYSTEM=$(expr $NEEDED_STORAGE_SYSTEM - $RECLAIMABLE_STORAGE_SYSTEM)
  88. RECLAIMABLE_STORAGE_PRODUCT=$(find ./product -type f | sed "s|^./|$SYSTEM_OUT/|" | xargs ls -d 2>/dev/null | xargs du -cs PLACEHOLDER 2>/dev/null | tail -n1 | cut -f1)
  89. NEEDED_STORAGE_PRODUCT=$(expr $NEEDED_STORAGE_PRODUCT - $RECLAIMABLE_STORAGE_PRODUCT)
  90. RECLAIMABLE_STORAGE_SYSTEM_EXT=$(find ./system_ext -type f | sed "s|^./|$SYSTEM_OUT/|" | xargs ls -d 2>/dev/null | xargs du -cs PLACEHOLDER 2>/dev/null | tail -n1 | cut -f1)
  91. NEEDED_STORAGE_SYSTEM_EXT=$(expr $NEEDED_STORAGE_SYSTEM_EXT - $RECLAIMABLE_STORAGE_SYSTEM_EXT)
  92. if [ -z "$PRODUCT_BLOCK" ]; then
  93. NEEDED_STORAGE_SYSTEM=$(expr $NEEDED_STORAGE_SYSTEM + $NEEDED_STORAGE_PRODUCT - $STORAGE_BUFFER)
  94. fi
  95. if [ -z "$SYSTEM_EXT_BLOCK" ]; then
  96. NEEDED_STORAGE_SYSTEM=$(expr $NEEDED_STORAGE_SYSTEM + $NEEDED_STORAGE_SYSTEM_EXT - $STORAGE_BUFFER)
  97. fi
  98. }
  99. remove_big_optional_apps() {
  100. ui_print "Low resource device detected, removing large extras"
  101. rm -rf product/app/GoogleTTS
  102. rm -rf product/app/MarkupGoogle
  103. rm -rf product/app/talkback
  104. rm -rf product/priv-app/GoogleRestore
  105. rm -rf product/priv-app/Velvet
  106. rm -rf system_ext/priv-app/SetupWizard
  107. }
  108. ui_print "**********************"
  109. ui_print "MindTheGapps installer"
  110. ui_print "**********************"
  111. ui_print "Extracting files"
  112. TMP=/tmp
  113. cd "$TMP"
  114. unzip -o "$ZIP"
  115. rm -rf META-INF
  116. # Check for arch. We need to do this before extracting our toybox, since that might be
  117. # compiled for a different architecture. Just hope that all environments have at least
  118. # a proper `grep` and `uname`.
  119. GAPPS_ARCH=$(getprop2 $TMP/build.prop arch)
  120. CPU_ARCH=$(uname -m)
  121. if [ $GAPPS_ARCH != $CPU_ARCH ]; then
  122. error "This package is built for $(nice_arch $GAPPS_ARCH) but your device is $(nice_arch $CPU_ARCH)! Aborting"
  123. fi
  124. ui_print "Setting up environment"
  125. TOYBOX="${TMP}/toybox"
  126. chmod +x "$TOYBOX"
  127. TOYS_PATH="${TMP}/bin"
  128. rm -rf "$TOYS_PATH";
  129. $TOYBOX install -d "$TOYS_PATH"
  130. for toy in $($TOYBOX); do
  131. $TOYBOX ln -sf "$TOYBOX" "${TOYS_PATH}/${toy}"
  132. done
  133. PATH="${TOYS_PATH}:${PATH}"
  134. ui_print "Mounting partitions"
  135. # Ensure system is unmounted so mounting succeeds
  136. umount /system || umount /mnt/system || true
  137. umount /product || true
  138. umount /system_ext || true
  139. # Find partitions
  140. DYNAMIC_PARTITIONS=`getprop ro.boot.dynamic_partitions`
  141. if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
  142. BLK_PATH="/dev/block/mapper"
  143. else
  144. BLK_PATH=/dev/block/bootdevice/by-name
  145. fi
  146. CURRENTSLOT=`getprop ro.boot.slot_suffix`
  147. if [ ! -z "$CURRENTSLOT" ]; then
  148. if [ "$CURRENTSLOT" == "_a" ]; then
  149. SLOT_SUFFIX="_a"
  150. else
  151. SLOT_SUFFIX="_b"
  152. fi
  153. fi
  154. SYSTEM_BLOCK=$(find_block "system")
  155. PRODUCT_BLOCK=$(find_block "product")
  156. SYSTEM_EXT_BLOCK=$(find_block "system_ext")
  157. # Disable rw protection on dynamic partitions
  158. if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
  159. blockdev --setrw "$SYSTEM_BLOCK"
  160. if [ -n "$PRODUCT_BLOCK" ]; then
  161. blockdev --setrw "$PRODUCT_BLOCK"
  162. fi
  163. if [ -n "$SYSTEM_EXT_BLOCK" ]; then
  164. blockdev --setrw "$SYSTEM_EXT_BLOCK"
  165. fi
  166. fi
  167. # Mount and define SYSTEM_OUT
  168. SYSTEM_MNT=/mnt/system
  169. mkdir -p "$SYSTEM_MNT" || true
  170. if mount -o rw "$SYSTEM_BLOCK" "$SYSTEM_MNT"; then
  171. ui_print "$SYSTEM_MNT mounted"
  172. else
  173. error_mounting "$SYSTEM_MNT"
  174. fi
  175. SYSTEM_OUT="${SYSTEM_MNT}/system"
  176. # Compare sdk version
  177. GAPPS_VERSION=$(getprop2 $TMP/build.prop version)
  178. ANDROID_VERSION=$(getprop2 $SYSTEM_OUT/build.prop ro.build.version.sdk)
  179. if [ "$GAPPS_VERSION" != "$ANDROID_VERSION" ]; then
  180. gapps_version_nice=$(getprop2 $TMP/build.prop version_nice)
  181. android_version_nice=$(getprop2 $SYSTEM_OUT/build.prop ro.build.version.release)
  182. error "This package is for Android $gapps_version_nice (SDK $GAPPS_VERSION) but your system is Android $android_version_nice (SDK $ANDROID_VERSION)! Aborting"
  183. fi
  184. # Ignore {product,system_ext} block devices in case they are symlinks
  185. # This is common on devices where maintainers have chosen not to use
  186. # real partitions because of their size being too small to be useful
  187. if [ -L "${SYSTEM_MNT}/product" ]; then
  188. PRODUCT_BLOCK=""
  189. fi
  190. if [ -L "${SYSTEM_MNT}/system_ext" ]; then
  191. SYSTEM_EXT_BLOCK=""
  192. fi
  193. if [ -n "$PRODUCT_BLOCK" ]; then
  194. mkdir /product || true
  195. if mount -o rw "$PRODUCT_BLOCK" /product; then
  196. ui_print "/product mounted"
  197. else
  198. error_mounting "/product"
  199. fi
  200. fi
  201. if [ -n "$SYSTEM_EXT_BLOCK" ]; then
  202. mkdir /system_ext || true
  203. if mount -o rw "$SYSTEM_EXT_BLOCK" /system_ext; then
  204. ui_print "/system_ext mounted"
  205. else
  206. error_mounting "/system_ext"
  207. fi
  208. fi
  209. # Compute storage requirements
  210. SYSTEM_STORAGE=`df $SYSTEM_MNT | tail -1 | tr -s ' ' | cut -d ' ' -f4`
  211. if [ -n "$PRODUCT_BLOCK" ]; then
  212. PRODUCT_STORAGE=`df /product | tail -1 | tr -s ' ' | cut -d ' ' -f4`
  213. fi
  214. if [ -n "$SYSTEM_EXT_BLOCK" ]; then
  215. SYSTEM_EXT_STORAGE=`df /system_ext | tail -1 | tr -s ' ' | cut -d ' ' -f4`
  216. fi
  217. STORAGE_BUFFER=10240
  218. cd system
  219. compute_apps_size
  220. if [ "$SYSTEM_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM" ]; then
  221. remove_big_optional_apps
  222. compute_apps_size
  223. if [ "$SYSTEM_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM" ]; then
  224. error_no_space
  225. fi
  226. fi
  227. if [ -n "$PRODUCT_BLOCK" ] && [ "$PRODUCT_STORAGE" -lt "$NEEDED_STORAGE_PRODUCT" ]; then
  228. remove_big_optional_apps
  229. compute_apps_size
  230. if [ "$PRODUCT_STORAGE" -lt "$NEEDED_STORAGE_PRODUCT" ]; then
  231. error_no_space
  232. fi
  233. fi
  234. if [ -n "$SYSTEM_EXT_BLOCK" ] && [ "$SYSTEM_EXT_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM_EXT" ]; then
  235. remove_big_optional_apps
  236. compute_apps_size
  237. if [ "$SYSTEM_EXT_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM_EXT" ]; then
  238. error_no_space
  239. fi
  240. fi
  241. ui_print "Generating addon.d file"
  242. cat addon.d/addond_head > addon.d/30-gapps.sh
  243. for f in `find . ! -path "./addon.d/*" -type f`; do
  244. line=$(echo "$f" | sed 's/\.\///')
  245. echo "$line" >> addon.d/30-gapps.sh
  246. done
  247. cat addon.d/addond_tail >> addon.d/30-gapps.sh
  248. rm addon.d/addond_head addon.d/addond_tail
  249. ui_print "Preparing files for copying"
  250. for d in `find . -mindepth 1 -type d -type d`; do
  251. set_perm 0755 $d
  252. set_owner root root $d
  253. done
  254. for f in `find . -type f`; do
  255. type=$(echo "$f" | sed 's/.*\.//')
  256. if [ "$type" == "sh" ] || [ "$type" == "$f" ]; then
  257. set_perm 0755 $f
  258. else
  259. set_perm 0644 $f
  260. fi
  261. set_owner root root $f
  262. set_con system_file $f
  263. done
  264. ui_print "Copying files"
  265. cp --preserve=a -r ./* "${SYSTEM_OUT}/"
  266. if [ -n "$PRODUCT_BLOCK" ]; then
  267. cp --preserve=a -r ./product/* /product
  268. fi
  269. if [ -n "$SYSTEM_EXT_BLOCK" ]; then
  270. cp --preserve=a -r ./system_ext/* /system_ext
  271. fi
  272. if [ -e system_ext/priv-app/SetupWizard ] ; then
  273. rm -rf "${SYSTEM_OUT}/system_ext/priv-app/Provision"
  274. fi
  275. cleanup
  276. ui_print "Done!"
  277. exit 0