summaryrefslogtreecommitdiff
path: root/META-INF/com/google/android/update-binary
blob: ad1cb8efa089109218913705ca2993eedf9867c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/sbin/sh
# AnyKernel3 Backend (DO NOT CHANGE)
# osm0sis @ xda-developers

OUTFD=/proc/self/fd/$2;
ZIPFILE="$3";

ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false;
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true;

$BOOTMODE && DIR=/sdcard || DIR=$(dirname "$ZIPFILE");

test -d /postinstall/tmp && POSTINSTALL=/postinstall;
test "$AKHOME" || AKHOME=$POSTINSTALL/tmp/anykernel;
test "$ANDROID_ROOT" || ANDROID_ROOT=/system;

ui_print() {
  until [ ! "$1" ]; do
    echo "ui_print $1
      ui_print" >> $OUTFD;
    shift;
  done;
}
ui_printfile() {
  while IFS='' read -r line || $BB [[ -n "$line" ]]; do
    ui_print "$line";
  done < $1;
}
show_progress() { echo "progress $1 $2" >> $OUTFD; }
file_getprop() { $BB grep "^$2=" "$1" | $BB cut -d= -f2-; }
setup_mountpoint() {
  test -L $1 && $BB mv -f $1 ${1}_link;
  if [ ! -d $1 ]; then
    rm -f $1;
    mkdir -p $1;
  fi;
}
is_mounted() { $BB mount | $BB grep -q " $1 "; }
mount_apex() {
  test -d /system/apex || return 1;
  local apex dest loop minorx num;
  setup_mountpoint /apex;
  test -e /dev/block/loop1 && minorx=$($BB ls -l /dev/block/loop1 | $BB awk '{ print $6 }') || minorx=1;
  num=0;
  for apex in /system/apex/*; do
    dest=/apex/$($BB basename $apex .apex);
    test "$dest" == /apex/com.android.runtime.release && dest=/apex/com.android.runtime;
    $BB mkdir -p $dest;
    case $apex in
      *.apex)
        $BB unzip -qo $apex apex_payload.img -d /apex;
        $BB mv -f /apex/apex_payload.img $dest.img;
        $BB mount -t ext4 -o ro,noatime $dest.img $dest 2>/dev/null;
        if [ $? != 0 ]; then
          while [ $num -lt 64 ]; do
            loop=/dev/block/loop$num;
            ($BB mknod $loop b 7 $((num * minorx));
            $BB losetup $loop $dest.img) 2>/dev/null;
            num=$((num + 1));
            $BB losetup $loop | $BB grep -q $dest.img && break;
          done;
          $BB mount -t ext4 -o ro,loop,noatime $loop $dest;
          if [ $? != 0 ]; then
            $BB losetup -d $loop 2>/dev/null;
          fi;
        fi;
      ;;
      *) $BB mount -o bind $apex $dest;;
    esac;
  done;
  export ANDROID_RUNTIME_ROOT=/apex/com.android.runtime;
  export ANDROID_TZDATA_ROOT=/apex/com.android.tzdata;
  export BOOTCLASSPATH=/apex/com.android.runtime/javalib/core-oj.jar:/apex/com.android.runtime/javalib/core-libart.jar:/apex/com.android.runtime/javalib/okhttp.jar:/apex/com.android.runtime/javalib/bouncycastle.jar:/apex/com.android.runtime/javalib/apache-xml.jar:/system/framework/framework.jar:/system/framework/ext.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/android.test.base.jar:/system/framework/telephony-ext.jar:/apex/com.android.conscrypt/javalib/conscrypt.jar:/apex/com.android.media/javalib/updatable-media.jar;
}
umount_apex() {
  test -d /apex || return 1;
  local dest loop;
  for dest in $($BB find /apex -type d -mindepth 1 -maxdepth 1); do
    if [ -f $dest.img ]; then
      loop=$($BB mount | $BB grep $dest | $BB cut -d" " -f1);
    fi;
    ($BB umount -l $dest;
    $BB losetup -d $loop) 2>/dev/null;
  done;
  $BB rm -rf /apex 2>/dev/null;
  unset ANDROID_RUNTIME_ROOT ANDROID_TZDATA_ROOT BOOTCLASSPATH;
}
mount_all() {
  if ! is_mounted /data; then
    $BB mount /data;
    UMOUNT_DATA=1;
  fi;
  $BB mount -o ro -t auto /vendor 2>/dev/null;
  setup_mountpoint $ANDROID_ROOT;
  if ! is_mounted $ANDROID_ROOT; then
    $BB mount -o ro -t auto $ANDROID_ROOT 2>/dev/null;
  fi;
  case $ANDROID_ROOT in
    /system_root) setup_mountpoint /system;;
    /system)
      if ! is_mounted /system && ! is_mounted /system_root; then
        setup_mountpoint /system_root;
        $BB mount -o ro -t auto /system_root;
      elif [ -f /system/system/build.prop ]; then
        setup_mountpoint /system_root;
        $BB mount --move /system /system_root;
      fi;
      if [ $? != 0 ]; then
        $BB umount /system;
        $BB umount -l /system 2>/dev/null;
        test -e /dev/block/bootdevice/by-name/system || local slot=$(getprop ro.boot.slot_suffix 2>/dev/null);
        $BB mount -o ro -t auto /dev/block/bootdevice/by-name/system$slot /system_root;
      fi;
    ;;
  esac;
  if is_mounted /system_root; then
    if [ -f /system_root/build.prop ]; then
      $BB mount -o bind /system_root /system;
    else
      $BB mount -o bind /system_root/system /system;
    fi;
  fi;
  mount_apex;
}
umount_all() {
  (umount_apex;
  if [ ! -d /postinstall/tmp ]; then
    $BB umount /system;
    $BB umount -l /system;
    if [ -e /system_root ]; then
      $BB umount /system_root;
      $BB umount -l /system_root;
    fi;
  fi;
  umount /vendor;
  umount -l /vendor;
  if [ "$UMOUNT_DATA" ]; then
    $BB umount /data;
    $BB umount -l /data;
  fi) 2>/dev/null;
}
setup_env() {
  $BOOTMODE && return 1;
  $BB mount -o bind /dev/urandom /dev/random;
  umount_all;
  mount_all;
  OLD_LD_PATH=$LD_LIBRARY_PATH;
  OLD_LD_PRE=$LD_PRELOAD;
  OLD_LD_CFG=$LD_CONFIG_FILE;
  unset LD_LIBRARY_PATH LD_PRELOAD LD_CONFIG_FILE;
  if [ ! "$(getprop 2>/dev/null)" ]; then
    getprop() {
      local propdir propfile propval;
      for propdir in / /system_root /system /vendor /odm /product; do
        for propfile in default.prop build.prop; do
          test "$propval" && break 2 || propval="$(file_getprop $propdir/$propfile $1 2>/dev/null)";
        done;
      done;
      test "$propval" && echo "$propval" || echo "";
    }
  elif [ ! "$(getprop ro.build.type 2>/dev/null)" ]; then
    getprop() {
      ($(which getprop) | $BB grep "$1" | $BB cut -d[ -f3 | $BB cut -d] -f1) 2>/dev/null;
    }
  fi;
}
restore_env() {
  $BOOTMODE && return 1;
  local dir;
  unset -f getprop;
  test "$OLD_LD_PATH" && export LD_LIBRARY_PATH=$OLD_LD_PATH;
  test "$OLD_LD_PRE" && export LD_PRELOAD=$OLD_LD_PRE;
  test "$OLD_LD_CFG" && export LD_CONFIG_FILE=$OLD_LD_CFG;
  umount_all;
  (for dir in /apex /system /system_root; do
    if [ -L "${dir}_link" ]; then
      rmdir $dir;
      $BB mv -f ${dir}_link $dir;
    fi;
  done;
  $BB umount -l /dev/random) 2>/dev/null;
}
debugging() {
  case $(basename "$ZIPFILE" .zip) in
    *-debugging)
      ui_print " " "Creating debugging archive in $DIR...";
      test -f /tmp/recovery.log && local log=/tmp/recovery.log;
      $BB tar -czf "$DIR/anykernel3-$(date +%Y-%m-%d_%H%M%S)-debug.tgz" $AKHOME $log;
    ;;
  esac;
}
cleanup() {
  cd $(dirname $AKHOME);
  rm -rf $AKHOME;
}
abort() {
  ui_print "$@";
  debugging;
  restore_env;
  if [ ! -f anykernel.sh -o "$(file_getprop anykernel.sh do.cleanuponabort 2>/dev/null)" == 1 ]; then
    cleanup;
  fi;
  exit 1;
}
do_devicecheck() {
  test "$(file_getprop anykernel.sh do.devicecheck)" == 1 || return 1;
  local device devicename match product testname vendordevice vendorproduct;
  ui_print "Checking device...";
  device=$(getprop ro.product.device 2>/dev/null);
  product=$(getprop ro.build.product 2>/dev/null);
  vendordevice=$(getprop ro.product.vendor.device 2>/dev/null);
  vendorproduct=$(getprop ro.vendor.product.device 2>/dev/null);
  for testname in $(file_getprop anykernel.sh 'device.name.*'); do
    for devicename in $device $product $vendordevice $vendorproduct; do
      if [ "$devicename" == "$testname" ]; then
        ui_print "$testname" " ";
        match=1;
        break 2;
      fi;
    done;
  done;
  if [ ! "$match" ]; then
    abort " " "Unsupported device. Aborting...";
  fi;
}
int2ver() {
  if $BB [ "$1" -eq "$1" ] 2>/dev/null; then
    echo "$1.0.0";
  elif [ ! "$(echo "$1" | $BB cut -d. -f3)" ]; then
    echo "$1.0";
  else
    echo "$1";
  fi;
}
do_versioncheck() {
  test "$(file_getprop anykernel.sh supported.versions)" || return 1;
  local android_ver hi_ver lo_ver parsed_ver supported supported_ver;
  ui_print "Checking Android version...";
  supported_ver=$(file_getprop anykernel.sh supported.versions | $BB tr -d '[:space:]');
  android_ver=$(file_getprop /system/build.prop ro.build.version.release);
  parsed_ver=$(int2ver $android_ver);
  if echo $supported_ver | $BB grep -q '-'; then
    lo_ver=$(int2ver "$(echo $supported_ver | $BB cut -d- -f1)");
    hi_ver=$(int2ver "$(echo $supported_ver | $BB cut -d- -f2)");
    if echo -e "$hi_ver\n$lo_ver\n$parsed_ver" | $BB sort -g | $BB grep -n "$parsed_ver" | $BB grep -q '^2:'; then
      supported=1;
    fi;
  else
    for ver in $(echo $supported_ver | $BB sed 's;,; ;g'); do
      if [ "$(int2ver $ver)" == "$parsed_ver" ]; then
        supported=1;
        break;
      fi;
    done;
  fi;
  if [ "$supported" ]; then
    ui_print "$android_ver" " ";
  else
    abort " " "Unsupported Android version. Aborting...";
  fi;
}
do_levelcheck() {
  test "$(file_getprop anykernel.sh supported.patchlevels)" || return 1;
  local android_lvl hi_lvl lo_lvl parsed_lvl supported_lvl;
  ui_print "Checking Android security patch level...";
  supported_lvl=$(file_getprop anykernel.sh supported.patchlevels | $BB grep -oE '[0-9]{4}-[0-9]{2}|-');
  android_lvl=$(file_getprop /system/build.prop ro.build.version.security_patch);
  parsed_lvl=$(echo $android_lvl | $BB grep -oE '[0-9]{4}-[0-9]{2}');
  if echo $supported_lvl | $BB grep -q '^\-'; then
    lo_lvl=0000-00;
    hi_lvl=$(echo $supported_lvl | $BB awk '{ print $2 }');
  elif echo $supported_lvl | $BB grep -q ' - '; then
    lo_lvl=$(echo $supported_lvl | $BB awk '{ print $1 }');
    hi_lvl=$(echo $supported_lvl | $BB awk '{ print $3 }');
  elif echo $supported_lvl | $BB grep -q '\-$'; then
    lo_lvl=$(echo $supported_lvl | $BB awk '{ print $1 }');
    hi_lvl=9999-99;
  fi;
  if echo -e "$hi_lvl\n$lo_lvl\n$parsed_lvl" | $BB sort -g | $BB grep -n "$parsed_lvl" | $BB grep -q '^2:'; then
    ui_print "$android_lvl" " ";
  else
    abort " " "Unsupported Android security patch level. Aborting...";
  fi;
}
dump_moduleinfo() {
cat <<EOF > $1;
name=AK3 Helper Module
version=$($BB awk '{ print $3 }' $AKHOME/vertmp) $($BB grep -oE '#.[0-9]' $AKHOME/vertmp)
versionCode=1
author=AnyKernel3
description=$KERNEL_STRING
EOF
}
dump_moduleremover() {
cat <<'EOF' > $1;
#!/system/bin/sh
MODDIR=${0%/*};
if [ "$(cat /proc/version)" != "$(cat $MODDIR/version)" ]; then
  rm -rf $MODDIR;
fi;
EOF
}
do_modules() {
  test "$(file_getprop anykernel.sh do.modules)" == 1 || return 1;
  local modcon moddir modtarget module umask;
  if [ "$(file_getprop anykernel.sh do.systemless)" == 1 ]; then
    cd $AKHOME/modules;
    ui_print " " "Creating kernel helper Magisk module...";
    if [ -d /data/adb/magisk -a -f $AKHOME/split_img/.magisk ]; then
      umask=$(umask);
      umask 022;
      moddir=/data/adb/modules/ak3-helper;
      rm -rf $moddir;
      mkdir -p system $moddir;
      ($BB mv -f product system;
      $BB mv -f vendor system) 2>/dev/null;
      $BB cp -rLf * $moddir;
      dump_moduleinfo $moddir/module.prop;
      dump_moduleremover $moddir/post-fs-data.sh;
      cp -f $AKHOME/vertmp $moddir/version;
      umask $umask;
    else
      ui_print "Magisk installation not found. Skipped!";
    fi;
  else
    cd $AKHOME/modules;
    ui_print " " "Pushing modules...";
    $BB mount -o rw,remount -t auto /system;
    $BB mount -o rw,remount -t auto /vendor 2>/dev/null;
    for module in $(find . -name '*.ko'); do
      modtarget=$POSTINSTALL$(echo $module | $BB cut -c2-);
      if [ ! -e $modtarget ]; then
        case $module in
          */vendor/*) modcon=vendor;;
          *) modcon=system;;
        esac;
      fi;
      if is_mounted $modtarget; then
        $BB mount -o rw,remount -t auto $modtarget;
      fi;
      mkdir -p $(dirname $modtarget);
      $BB cp -rLf $module $modtarget;
      $BB chown 0:0 $modtarget;
      $BB chmod 644 $modtarget;
      if [ "$modcon" ]; then
        chcon "u:object_r:${modcon}_file:s0" $modtarget;
      fi;
      if is_mounted $modtarget; then
        $BB mount -o ro,remount -t auto $modtarget;
      fi;
    done;
    $BB mount -o ro,remount -t auto /system;
    $BB mount -o ro,remount -t auto /vendor 2>/dev/null;
  fi;
  cd $AKHOME;
}

show_progress 1.34 4;
ui_print " ";
cleanup;
mkdir -p $AKHOME/bin;
cd $AKHOME;
unzip -o "$ZIPFILE";
if [ $? != 0 -o ! "$(ls tools)" ]; then
  abort "Unzip failed. Aborting...";
fi;
for ARCH32 in x86 arm; do
  if [ -d $AKHOME/tools/$ARCH32 ]; then
    BB=$AKHOME/tools/$ARCH32/busybox;
    chmod 755 $BB;
    $BB >/dev/null 2>&1;
    if [ $? == 0 ]; then
      $BB mv -f $AKHOME/tools/$ARCH32/* $AKHOME/tools;
      break;
    fi;
  fi;
done;
BB=$AKHOME/tools/busybox;
chmod 755 $BB;
$BB chmod -R 755 tools bin;
$BB --install -s bin;
if [ $? != 0 -o -z "$(ls bin)" ]; then
  abort "Busybox setup failed. Aborting...";
fi;

if [ -f banner ]; then
  ui_printfile banner;
  ui_print " " " ";
fi;

KERNEL_STRING="$(file_getprop anykernel.sh kernel.string)";
ui_print "$KERNEL_STRING";
if [ -f version ]; then
  ui_print " ";
  ui_printfile version;
  ui_print " ";
fi;
ui_print " " "AnyKernel3 by osm0sis @ xda-developers" " " " ";

setup_env;

do_devicecheck;
do_versioncheck;
do_levelcheck;

ui_print "Installing...";
CORE=$($BB grep -oE 'ak.*core.sh' anykernel.sh);
test -f tools/$CORE || $BB ln -s $AKHOME/tools/ak*-core.sh $AKHOME/tools/$CORE;
PATH="$AKHOME/bin:$PATH" home=$AKHOME $BB ash anykernel.sh $2;
if [ $? != 0 ]; then
  abort;
fi;

do_modules;

debugging;
restore_env;

if [ "$(file_getprop anykernel.sh do.cleanup)" == 1 ]; then
  cleanup;
fi;

ui_print " " " " "Done!";