diff --git a/AnyKernel3/LICENSE b/AnyKernel3/LICENSE new file mode 100644 index 000000000000..5b6c68220fc1 --- /dev/null +++ b/AnyKernel3/LICENSE @@ -0,0 +1,195 @@ +## AnyKernel3 (AK3), and AnyKernel2/AnyKernel 2.0 (AK2) Scripts License: + + AnyKernel (versions 2.0/2 and later) Android image modifying scripts. + Copyright (c) 2019 Chris Renshaw (osm0sis @ xda-developers), + and additional contributors per readily available commit history/credits. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted (subject to the limitations in the disclaimer + below) provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + + NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY + THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + +## Included Binary Licenses: + +magiskboot, magiskpolicy (Magisk): GPLv3+ + + Magisk, including all git submodules are free software: + you can redistribute it and/or modify it under the terms of the + GNU General Public License as published by the Free Software Foundation, + either version 3 of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + Per Section 6(d), official compiled binaries from unmodified source: + https://github.com/topjohnwu/Magisk + +busybox: GPLv2 + + BusyBox is distributed under version 2 of the General Public + License. Version 2 is the only version of this license which this + version of BusyBox (or modified versions derived from this one) may + be distributed under. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + Per Section 3(b), self-compiled binary from modified source: + https://git.busybox.net/busybox/ + https://github.com/osm0sis/android-busybox-ndk + (pre-patched source tree used to build available upon request) + + +## Optional Binary Licenses: + +mkbootfs, mkbootimg: Apache License 2.0 +mkmtkhdr: Apache License 2.0, implied (AOSP mkbootimg derived) +boot_signer*.jar: Apache License 2.0 + + Copyright (c) 2008 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + Source not required, however, respective sources are provided: + https://github.com/osm0sis/mkbootfs + https://github.com/osm0sis/mkbootimg + https://github.com/osm0sis/mkmtkhdr + https://android.googlesource.com/platform/system/extras/+/master + +flash_erase, nanddump, nandwrite (mtd-utils): GPLv2 +dumpimage, mkimage (U-Boot): GPLv2+ +mboot: GPLv2 (Intel mboot.py derived) + + Copyright their respective authors, (linked below). + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + Per Section 3(b), self-compiled binaries from unmodified respective sources: + http://git.infradead.org/mtd-utils.git + https://gitlab.denx.de/u-boot/u-boot + https://github.com/osm0sis/mboot + +futility: BSD 3-Clause License (Chromium OS) +unpackelf, elftool: BSD 3-Clause License, implied (Sony mkelf.py derived) + + Copyright their respective authors, (linked below). + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Source not required, however, respective sources are provided: + https://github.com/osm0sis/futility + https://github.com/osm0sis/unpackelf + https://github.com/osm0sis/elftool + (https://github.com/sonyxperiadev/device-sony-lt26/tree/master/tools) + +rkcrc: BSD 2-Clause License + + Copyright (c) 2010, 2011 Fukaumi Naoki + Copyright (c) 2013 Ivo van Poorten + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + Source not required, however, respective source is provided: + https://github.com/linux-rockchip/rkflashtool + + +## Additional Build Scripts for Listed Binaries (where used): + +osm0sis' Odds and Ends Thread - Knowledge Base: +https://forum.xda-developers.com/showthread.php?p=53554719 + diff --git a/AnyKernel3/META-INF/com/google/android/update-binary b/AnyKernel3/META-INF/com/google/android/update-binary new file mode 100755 index 000000000000..c6d790ffd7d8 --- /dev/null +++ b/AnyKernel3/META-INF/com/google/android/update-binary @@ -0,0 +1,499 @@ +#!/sbin/sh +# AnyKernel3 Backend (DO NOT CHANGE) +# osm0sis @ xda-developers + +OUTFD=/proc/self/fd/$2; +ZIPFILE="$3"; + +BOOTMODE=false; +ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true; +$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true; + +DIR=/sdcard; +$BOOTMODE || DIR=$(dirname "$ZIPFILE"); + +[ -d /postinstall/tmp ] && POSTINSTALL=/postinstall; +[ "$AKHOME" ] || AKHOME=$POSTINSTALL/tmp/anykernel; +[ "$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-; } +find_slot() { + local slot=$(getprop ro.boot.slot_suffix 2>/dev/null); + [ "$slot" ] || slot=$($BB grep -o 'androidboot.slot_suffix=.*$' /proc/cmdline | $BB cut -d\ -f1 | $BB cut -d= -f2); + if [ ! "$slot" ]; then + slot=$(getprop ro.boot.slot 2>/dev/null); + [ "$slot" ] || slot=$($BB grep -o 'androidboot.slot=.*$' /proc/cmdline | $BB cut -d\ -f1 | $BB cut -d= -f2); + [ "$slot" ] && slot=_$slot; + fi; + [ "$slot" ] && echo "$slot"; +} +setup_mountpoint() { + [ -L $1 ] && $BB mv -f $1 ${1}_link; + if [ ! -d $1 ]; then + $BB rm -f $1; + $BB mkdir -p $1; + fi; +} +is_mounted() { $BB mount | $BB grep -q " $1 "; } +mount_apex() { + [ -d /system_root/system/apex ] || return 1; + local apex dest loop minorx num var; + setup_mountpoint /apex; + minorx=1; + [ -e /dev/block/loop1 ] && minorx=$($BB ls -l /dev/block/loop1 | $BB awk '{ print $6 }'); + num=0; + for apex in /system_root/system/apex/*; do + dest=/apex/$($BB basename $apex .apex); + case $dest in + *.current|*.release) dest=$(echo $dest | $BB rev | $BB cut -d. -f2- | $BB rev);; + esac; + $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; + for var in $($BB grep -o 'export .* /.*' /system_root/init.environ.rc | $BB awk '{ print $2 }'); do + eval OLD_${var}=\$$var; + done; + $($BB grep -o 'export .* /.*' /system_root/init.environ.rc | $BB sed 's; /;=/;'); unset export; +} +umount_apex() { + [ -d /apex/com.android.runtime ] || return 1; + local dest loop var; + for var in $($BB grep -o 'export .* /.*' /system_root/init.environ.rc | $BB awk '{ print $2 }'); do + if [ "$(eval echo \$OLD_$var)" ]; then + eval $var=\$OLD_${var}; + else + eval unset $var; + fi; + unset OLD_${var}; + done; + 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; +} +mount_all() { + local byname mount slot system; + if ! is_mounted /cache; then + $BB mount /cache 2>/dev/null && UMOUNT_CACHE=1; + fi; + if ! is_mounted /data; then + $BB mount /data && UMOUNT_DATA=1; + fi; + (for mount in /vendor /product /system_ext /persist; do + $BB mount -o ro -t auto $mount; + done) 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; + byname=bootdevice/by-name; + [ -d /dev/block/$byname ] || byname=$($BB find /dev/block/platform -type d -name by-name 2>/dev/null | $BB head -n1 | $BB cut -d/ -f4-); + [ -d /dev/block/mapper ] && byname=mapper; + [ -e /dev/block/$byname/system ] || slot=$(find_slot); + 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; + $BB mount -o ro -t auto /dev/block/$byname/system$slot /system_root; + fi; + ;; + esac; + [ -f /system_root/system/build.prop ] && system=/system; + for mount in /vendor /product /system_ext; do + if ! is_mounted $mount && [ -L /system$mount -o -L /system_root$system$mount ]; then + setup_mountpoint $mount; + $BB mount -o ro -t auto /dev/block/$byname$mount$slot $mount; + fi; + done; + if is_mounted /system_root; then + mount_apex; + $BB mount -o bind /system_root$system /system; + fi; + if ! is_mounted /persist && [ -e /dev/block/bootdevice/by-name/persist ]; then + setup_mountpoint /persist; + $BB mount -o ro -t auto /dev/block/bootdevice/by-name/persist /persist; + fi; +} +umount_all() { + local mount; + (if [ ! -d /postinstall/tmp ]; then + $BB umount /system; + $BB umount -l /system; + fi) 2>/dev/null; + umount_apex; + (if [ ! -d /postinstall/tmp ]; then + $BB umount /system_root; + $BB umount -l /system_root; + fi; + umount /vendor; # busybox umount /vendor breaks recovery on some hacky devices + umount -l /vendor; + for mount in /mnt/system /mnt/vendor /product /mnt/product /system_ext /mnt/system_ext /persist; do + $BB umount $mount; + $BB umount -l $mount; + done; + if [ "$UMOUNT_DATA" ]; then + $BB umount /data; + $BB umount -l /data; + fi; + if [ "$UMOUNT_CACHE" ]; then + $BB umount /cache; + $BB umount -l /cache; + fi) 2>/dev/null; +} +setup_env() { + $BOOTMODE && return 1; + $BB mount -o bind /dev/urandom /dev/random; + if [ -L /etc ]; then + setup_mountpoint /etc; + $BB cp -af /etc_link/* /etc; + $BB sed -i 's; / ; /system_root ;' /etc/fstab; + fi; + 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 /product /system_ext /odm; do + for propfile in default.prop build.prop; do + if [ "$propval" ]; then + break 2; + else + propval="$(file_getprop $propdir/$propfile $1 2>/dev/null)"; + fi; + done; + done; + if [ "$propval" ]; then + echo "$propval"; + else + echo ""; + fi; + } + 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; + [ "$OLD_LD_PATH" ] && export LD_LIBRARY_PATH=$OLD_LD_PATH; + [ "$OLD_LD_PRE" ] && export LD_PRELOAD=$OLD_LD_PRE; + [ "$OLD_LD_CFG" ] && export LD_CONFIG_FILE=$OLD_LD_CFG; + unset OLD_LD_PATH OLD_LD_PRE OLD_LD_CFG; + umount_all; + [ -L /etc_link ] && $BB rm -rf /etc/*; + (for dir in /etc /apex /system_root /system /vendor /product /system_ext /persist; 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..."; + [ -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() { + [ "$(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() { + [ "$(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() { + [ "$(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 < $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() { + [ "$(file_getprop anykernel.sh do.modules)" == 1 ] || return 1; + local block modcon moddir modtarget module slot 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/magisk_patched ]; then + umask=$(umask); + umask 022; + moddir=/data/adb/modules/ak3-helper; + $BB 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..."; + if [ -d /dev/block/mapper ]; then + for block in system vendor product; do + for slot in "" _a _b; do + $BB blockdev --setrw /dev/block/mapper/$block$slot 2>/dev/null; + done; + done; + fi; + if [ ! -d /postinstall/tmp ]; then + $BB mount -o rw,remount -t auto /system; + ($BB mount -o rw,remount -t auto /vendor; + $BB mount -o rw,remount -t auto /product) 2>/dev/null; + fi; + for module in $(find . -name '*.ko'); do + modtarget=$POSTINSTALL$(echo $module | $BB cut -c2-); + if [ ! -e $modtarget ]; then + case $module in + */vendor/*) modcon=vendor;; + */product/*) modcon=product;; + *) 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; + if [ ! -d /postinstall/tmp ]; then + $BB mount -o ro,remount -t auto /system; + ($BB mount -o ro,remount -t auto /vendor; + $BB mount -o ro,remount -t auto /product) 2>/dev/null; + fi; + 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); +[ -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!"; diff --git a/AnyKernel3/META-INF/com/google/android/updater-script b/AnyKernel3/META-INF/com/google/android/updater-script new file mode 100644 index 000000000000..e140e9e4ed52 --- /dev/null +++ b/AnyKernel3/META-INF/com/google/android/updater-script @@ -0,0 +1,6 @@ +#FLASHAFTERUPDATEV2 +# +# Dummy file; update-binary is a shell script (DO NOT CHANGE) +# +# +# AK_BASE_VERSION=20210915 diff --git a/AnyKernel3/README.md b/AnyKernel3/README.md new file mode 100644 index 000000000000..a5d77bc46f71 --- /dev/null +++ b/AnyKernel3/README.md @@ -0,0 +1,185 @@ +---------------------------------------------------------------------------------- +AnyKernel3 - Flashable Zip Template for Kernel Releases with Ramdisk Modifications +---------------------------------------------------------------------------------- +### by osm0sis @ xda-developers ### + +"AnyKernel is a template for an update.zip that can apply any kernel to any ROM, regardless of ramdisk." - Koush + +AnyKernel2 pushed the format further by allowing kernel developers to modify the underlying ramdisk for kernel feature support easily using a number of included command methods along with properties and variables to customize the installation experience to their kernel. AnyKernel3 adds the power of topjohnwu's magiskboot for wider format support by default, and to automatically detect and retain Magisk root by patching the new Image.*-dtb as Magisk would. + +_A script based on Galaxy Nexus (tuna) is included for reference. Everything to edit is self-contained in __anykernel.sh__._ + +## // Properties / Variables ## +``` +kernel.string=KernelName by YourName @ xda-developers +do.devicecheck=1 +do.modules=1 +do.systemless=1 +do.cleanup=1 +do.cleanuponabort=0 +device.name1=maguro +device.name2=toro +device.name3=toroplus +device.name4=tuna +supported.versions=6.0 - 7.1.2 +supported.patchlevels=2019-07 - + +block=/dev/block/platform/omap/omap_hsmmc.0/by-name/boot; +is_slot_device=0; +ramdisk_compression=auto; +``` + +__do.devicecheck=1__ specified requires at least device.name1 to be present. This should match ro.product.device, ro.build.product, ro.product.vendor.device or ro.vendor.product.device from the build.prop files for your device. There is support for as many device.name# properties as needed. You may remove any empty ones that aren't being used. + +__do.modules=1__ will push the .ko contents of the modules directory to the same location relative to root (/) and apply correct permissions. On A/B devices this can only be done to the active slot. + +__do.systemless=1__ (with __do.modules=1__) will instead push the full contents of the modules directory to create a simple "ak3-helper" Magisk module, allowing developers to effectively replace system files, including .ko files. If the current kernel is changed then the kernel helper module automatically removes itself to prevent conflicts. + +__do.cleanup=0__ will keep the zip from removing its working directory in /tmp/anykernel (by default) - this can be useful if trying to debug in adb shell whether the patches worked correctly. + +__do.cleanuponabort=0__ will keep the zip from removing its working directory in /tmp/anykernel (by default) in case of installation abort. + +__supported.versions=__ will match against ro.build.version.release from the current ROM's build.prop. It can be set to a list or range. As a list of one or more entries, e.g. `7.1.2` or `8.1.0, 9` it will look for exact matches, as a range, e.g. `7.1.2 - 9` it will check to make sure the current version falls within those limits. Whitespace optional, and supplied version values should be in the same number format they are in the build.prop value for that Android version. + +__supported.patchlevels=__ will match against ro.build.version.security_patch from the current ROM's build.prop. It can be set as a closed or open-ended range of dates in the format YYYY-MM, whitespace optional, e.g. `2019-04 - 2019-06`, `2019-04 -` or `- 2019-06` where the last two examples show setting a minimum and maximum, respectively. + +`block=auto` instead of a direct block filepath enables detection of the device boot partition for use with broad, device non-specific zips. Also accepts specifically `boot`, `recovery` or `vendor_boot`. + +`is_slot_device=1` enables detection of the suffix for the active boot partition on slot-based devices and will add this to the end of the supplied `block=` path. Also accepts `auto` for use with broad, device non-specific zips. + +`ramdisk_compression=auto` allows automatically repacking the ramdisk with the format detected during unpack. Changing `auto` to `gz`, `lzo`, `lzma`, `xz`, `bz2`, `lz4`, or `lz4-l` (for lz4 legacy) instead forces the repack as that format, and using `cpio` or `none` will (attempt to) force the repack as uncompressed. + +`customdd=""` may be added to allow specifying additional dd parameters for devices that need to hack their kernel directly into a large partition like mmcblk0, or force use of dd for flashing. + +`slot_select=active|inactive` may be added to allow specifying the target slot. If omitted the default remains `active`. + +`no_block_display=1` may be added to disable output of the detected final used partition+slot path for zips which choose to include their own custom output instead. + +## // Command Methods ## +``` +ui_print "" [...] +abort ["" [...]] +contains +file_getprop + +set_perm [ ...] +set_perm_recursive [ ...] + +dump_boot +split_boot +unpack_ramdisk + +backup_file +restore_file +replace_string +replace_section +remove_section +insert_line +replace_line +remove_line +prepend_file +insert_file +append_file +replace_file +patch_fstab block|mount|fstype|options|flags +patch_cmdline +patch_prop +patch_ueventd + +repack_ramdisk +flash_boot +flash_dtbo +write_boot + +reset_ak [keep] +setup_ak +``` + +__"if search string"__ is the string it looks for to decide whether it needs to add the tweak or not, so generally something to indicate the tweak already exists. __"cmdline entry name"__ behaves somewhat like this as a match check for the name of the cmdline entry to be changed/added by the _patch_cmdline_ function, followed by the full entry to replace it. __"prop name"__ also serves as a match check in _patch_prop_ for a property in the given prop file, but is only the prop name as the prop value is specified separately. + +Similarly, __"line match string"__ and __"line replace string"__ are the search strings that locate where the modification needs to be made for those commands, __"begin search string"__ and __"end search string"__ are both required to select the first and last lines of the script block to be replaced for _replace_section_, and __"mount match name"__ and __"fs match type"__ are both required to narrow the _patch_fstab_ command down to the correct entry. + +__"scope"__ may be specified as __"global"__ to force all instances of the string/line targeted by _replace_string_, _replace_line_ or _remove_line_ to be replaced/removed accordingly. Omitted or set to anything else and it will perform the default first-match action. + +__"before|after"__ requires you simply specify __"before"__ or __"after"__ for the placement of the inserted line, in relation to __"line match string"__. + +__"block|mount|fstype|options|flags"__ requires you specify which part (listed in order) of the fstab entry you want to check and alter. + +_dump_boot_ and _write_boot_ are the default method of unpacking/repacking, but for more granular control, or omitting ramdisk changes entirely ("OG AK" mode), these can be separated into _split_boot; unpack_ramdisk_ and _repack_ramdisk; flash_boot_ respectively. _flash_dtbo_ can be used to flash a dtbo image. It is automatically included in _write_boot_ but can be called separately if using "OG AK" mode or creating a dtbo only zip. + +Multi-partition zips can be created by removing the ramdisk and patch folders from the zip and including instead "-files" folders named for the partition (without slot suffix), e.g. boot-files + recovery-files, or kernel-files + ramdisk-files (on some Treble devices). These then contain Image.gz, and ramdisk, patch, etc. subfolders for each partition. To setup for the next partition, simply set `block=` (without slot suffix) and `ramdisk_compression=` for the new target partition and use the _reset_ak_ command. + +Similarly, multi-slot zips can be created with the normal zip layout for the active (current) slot, then resetting for the inactive slot by setting `block=` (without slot suffix) again, `slot_select=inactive` and `ramdisk_compression=` for the target slot and using the _reset_ak keep_ command, which will retain the patch and any added ramdisk files for the next slot. + +_backup_file_ may be used for testing to ensure ramdisk changes are made correctly, transparency for the end-user, or in a ramdisk-only "mod" zip. In the latter case _restore_file_ could also be used to create a "restore" zip to undo the changes, but should be used with caution since the underlying patched files could be changed with ROM/kernel updates. + +You may also use _ui_print "\"_ to write messages back to the recovery during the modification process, _abort "\"_ to abort with optional message, and _file_getprop "\" "\"_ and _contains "\" "\"_ to simplify string testing logic you might want in your script. + +## // Binary Inclusion ## + +The AK3 repo includes current ARM builds of `magiskboot`, `magiskpolicy` and `busybox` by default to keep the basic package small. Builds for other architectures and optional binaries (see below) are available from the latest Magisk zip, or my latest AIK-mobile and FlashIt packages, respectively, here: + +https://forum.xda-developers.com/showthread.php?t=2073775 (Android Image Kitchen thread) +https://forum.xda-developers.com/showthread.php?t=2239421 (Odds and Ends thread) + +Optional supported binaries which may be placed in /tools to enable built-in expanded functionality are as follows: +* `mkbootfs` - for broken recoveries, or, booted flash support for a script/app via bind mount to /tmp (deprecated/use with caution) +* `flash_erase`, `nanddump`, `nandwrite` - MTD block device support for devices where the `dd` command is not sufficient +* `dumpimage`, `mkimage` - DENX U-Boot uImage format support +* `mboot` - Intel OSIP Android image format support +* `unpackelf`, `mkbootimg` - Sony ELF kernel.elf format support, repacking as AOSP standard boot.img for unlocked bootloaders +* `elftool` (with `unpackelf`) - Sony ELF kernel.elf format support, repacking as ELF for older Sony devices +* `mkmtkhdr` (with `unpackelf`) - MTK device boot image section headers support for Sony devices +* `futility` + `chromeos` test keys directory - Google ChromeOS signature support +* `boot_signer-dexed.jar` + `avb` keys directory - Google Android Verified Boot 1.0 (AVBv1) signature support +* `rkcrc` - Rockchip KRNL ramdisk image support + +Optionally moving ARM builds to tools/arm and putting x86 builds in tools/x86 will enable architecture detection for use with broad, device non-specific zips. + +## // Instructions ## + +1. Place final kernel build product, e.g. Image.gz-dtb or zImage to name a couple, in the zip root (any separate dt, dtb or recovery_dtbo, and/or dtbo should also go here for devices that require custom ones, each will fallback to the original if not included) + +2. Place any required ramdisk files in /ramdisk (/vendor_ramdisk for simple multi-partition vendor_boot support) and module files in /modules (with the full path like /modules/system/lib/modules) + +3. Place any required patch files (generally partial files which go with AK3 file editing commands) in /patch (/vendor_patch for simple multi-partition vendor_boot support) + +4. Modify the anykernel.sh to add your kernel's name, boot partition location, permissions for any added ramdisk files, and use methods for any required ramdisk modifications (optionally, also place banner and/or version files in the root to have these displayed during flash) + +5. `zip -r9 UPDATE-AnyKernel3.zip * -x .git README.md *placeholder` + +_The LICENSE file must remain in the final zip to comply with licenses for binary redistribution and the license of the AK3 scripts._ + +If supporting a recovery that forces zip signature verification (like Cyanogen Recovery) then you will need to also sign your zip using the method I describe here: + +http://forum.xda-developers.com/android/software-hacking/dev-complete-shell-script-flashable-zip-t2934449 + +Not required, but any tweaks you can't hardcode into the source (best practice) should be added with an additional init.tweaks.rc or bootscript.sh to minimize the necessary ramdisk changes. On newer devices Magisk allows these within /overlay.d - see examples. + +It is also extremely important to note that for the broadest AK3 compatibility it is always better to modify a ramdisk file rather than replace it. + +___If running into trouble when flashing an AK3 zip, the suffix -debugging may be added to the zip's filename to enable creation of a debug .tgz of /tmp for later examination while booted or on desktop.___ + +## // Staying Up-To-Date ## + +Now that you've got a ready zip for your device, you might be wondering how to keep it up-to-date with the latest AnyKernel commits. AnyKernel2 and AnyKernel3 have been painstakingly developed to allow you to just drop in the latest update-binary and tools directory and have everything "just work" for beginners not overly git or script savvy, but the best practice way is as follows: + +1. Fork my AnyKernel3 repo on GitHub + +2. `git clone https://github.com//AnyKernel3` + +3. `git remote add upstream https://github.com/osm0sis/AnyKernel3` + +4. `git checkout -b ` + +5. Set it up like your zip (i.e. remove any folders you don't use like ramdisk or patch, delete README.md, and add your anykernel.sh and optionally your Image.*-dtb if you want it up there) then commit all those changes + +6. `git push --set-upstream origin ` + +7. `git checkout master` then repeat steps 4-6 for any other devices you support + +Then you should be able to `git pull upstream master` from your master branch and either merge or cherry-pick the new AK3 commits into your device branches as needed. + +___For further support and usage examples please see the AnyKernel3 XDA thread:___ _https://forum.xda-developers.com/showthread.php?t=2670512_ + +__Have fun!__ diff --git a/AnyKernel3/anykernel.sh b/AnyKernel3/anykernel.sh new file mode 100755 index 000000000000..be535adea341 --- /dev/null +++ b/AnyKernel3/anykernel.sh @@ -0,0 +1,76 @@ +# AnyKernel3 Ramdisk Mod Script +# osm0sis @ xda-developers + +## AnyKernel setup +# begin properties +properties() { ' +kernel.string=Cherry Kernel - by @AkiraNoSushi & @Flopster101 +do.devicecheck=1 +do.modules=0 +do.systemless=0 +do.cleanup=1 +do.cleanuponabort=0 +device.name1=pine +device.name2=olive +device.name3=olivelite +device.name4=olivewood +supported.versions=11,12 +supported.patchlevels= +'; } # end properties + +# shell variables +block=/dev/block/by-name/boot; +is_slot_device=0; +ramdisk_compression=auto; + + +## AnyKernel methods (DO NOT CHANGE) +# import patching functions/variables - see for reference +. tools/ak3-core.sh; + +## AnyKernel install +split_boot; + +ui_print "Mounting /vendor..." +mount -o rw,remount /vendor + +vndk_version=$(file_getprop /vendor/build.prop ro.vendor.build.version.sdk) + +if [ $vndk_version -lt 30 ]; then + abort "Unsupported VNDK version. Aborting..." +fi + +ui_print "Detecting WiFi HAL..." + +wifi_hal=$(find /vendor/lib64 /vendor/lib -name "libwifi-hal.so" | head -n 1) +if grep -q "pronto_wlan.ko" $wifi_hal; then + abort "Unsupported WiFi HAL. Aborting..." +fi + +flash_boot; +flash_dtbo; + +ui_print " " + +ui_print "Mounting /system..." +mount -o rw,remount /system + +## Patches +# Prevent init from overriding kernel tweaks. +ui_print "Patching system's init..." +# IMO this is kinda destructive but works +find /system/etc/init/ -type f | while read file; do +sed -Ei 's;[^#](write /proc/sys/(kernel|vm)/(sched|dirty|perf_cpu|page-cluster|stat|swappiness|vfs));#\1;g' $file +done +# IORap +ui_print "Patching system's build.prop..." +patch_prop /system/build.prop "ro.iorapd.enable" "true" +patch_prop /system/build.prop "iorapd.perfetto.enable" "true" +patch_prop /system/build.prop "iorapd.readahead.enable" "true" +# Prevent init from overriding ZRAM algorithm +ui_print "Patching vendor's init..." +remove_line "/vendor/etc/init/hw/init.qcom.rc" "comp_algorithm" "global" +# Replace post_boot with ours. +ui_print "Pushing init.qcom.post_boot.sh..." +replace_file "/vendor/bin/init.qcom.post_boot.sh" "0755" "init.qcom.post_boot.sh" +## end install diff --git a/AnyKernel3/banner b/AnyKernel3/banner new file mode 100644 index 000000000000..aa5b719fd7cf --- /dev/null +++ b/AnyKernel3/banner @@ -0,0 +1,16 @@ + _ + | | + ___| |__ ___ _ __ _ __ _ _ + / __| '_ \ / _ \ '__| '__| | | | +| (__| | | | __/ | | | | |_| | + \___|_| |_|\___|_| |_| \__, | + __/ | + |___/ + __.--~~.,-.__ + `~-._.-(`-.__`-. + \ `~~` + .--./ \ + /# \ \.--. + \ / /# \ + '--' \ / + '--' \ No newline at end of file diff --git a/AnyKernel3/modules/system/lib/modules/placeholder b/AnyKernel3/modules/system/lib/modules/placeholder new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/AnyKernel3/patch/init.qcom.post_boot.sh b/AnyKernel3/patch/init.qcom.post_boot.sh new file mode 100644 index 000000000000..b6ad84eb4c87 --- /dev/null +++ b/AnyKernel3/patch/init.qcom.post_boot.sh @@ -0,0 +1,206 @@ +#!/vendor/bin/sh +function configure_zram_parameters() { + MemTotalStr=`cat /proc/meminfo | grep MemTotal` + MemTotal=${MemTotalStr:16:8} + + # Zram disk - 75% for Go devices. + # For 512MB Go device, size = 384MB, set same for Non-Go. + # For 1GB Go device, size = 768MB, set same for Non-Go. + # For >=2GB Non-Go devices, size = 50% of RAM size. Limit the size to 4GB. + + RamSizeGB=`echo "($MemTotal / 1048576 ) + 1" | bc` + zRamSizeBytes=`echo "$RamSizeGB * 1024 * 1024 * 1024 / 2" | bc` + if [ $zRamSizeBytes -gt 4294967296 ]; then + zRamSizeBytes=4294967296 + fi + + if [ -f /sys/block/zram0/disksize ]; then + if [ -f /sys/block/zram0/use_dedup ]; then + echo 1 > /sys/block/zram0/use_dedup + fi + if [ $MemTotal -le 524288 ]; then + echo 402653184 > /sys/block/zram0/disksize + elif [ $MemTotal -le 1048576 ]; then + echo 805306368 > /sys/block/zram0/disksize + else + # modify by zfc 18-12-26 for C3H-391 to change zram to 1.5G + echo 1610612736 > /sys/block/zram0/disksize + fi + mkswap /dev/block/zram0 + swapon /dev/block/zram0 -p 32758 + fi +} + +function configure_memory_parameters() { + # Set Memory parameters. + # + # Set per_process_reclaim tuning parameters + # All targets will use vmpressure range 50-70, + # All targets will use 512 pages swap size. + # + # Set Low memory killer minfree parameters + # 32 bit Non-Go, all memory configurations will use 15K series + # 32 bit Go, all memory configurations will use uLMK + Memcg + # 64 bit will use Google default LMK series. + # + # Set ALMK parameters (usually above the highest minfree values) + # vmpressure_file_min threshold is always set slightly higher + # than LMK minfree's last bin value for all targets. It is calculated as + # vmpressure_file_min = (last bin - second last bin ) + last bin + # + # Set allocstall_threshold to 0 for all targets. + # + + arch_type=`uname -m` + + echo "4689,9378,14067,18756,23445,28134" > /sys/module/lowmemorykiller/parameters/minfree + + # Calculate vmpressure_file_min as below & set for 64 bit: + # vmpressure_file_min = last_lmk_bin + (last_lmk_bin - last_but_one_lmk_bin) + if [ "$arch_type" == "aarch64" ]; then + minfree_series=`cat /sys/module/lowmemorykiller/parameters/minfree` + minfree_1="${minfree_series#*,}" ; rem_minfree_1="${minfree_1%%,*}" + minfree_2="${minfree_1#*,}" ; rem_minfree_2="${minfree_2%%,*}" + minfree_3="${minfree_2#*,}" ; rem_minfree_3="${minfree_3%%,*}" + minfree_4="${minfree_3#*,}" ; rem_minfree_4="${minfree_4%%,*}" + minfree_5="${minfree_4#*,}" + + vmpres_file_min=$((minfree_5 + (minfree_5 - rem_minfree_4))) + echo $vmpres_file_min > /sys/module/lowmemorykiller/parameters/vmpressure_file_min + else + echo 53059 > /sys/module/lowmemorykiller/parameters/vmpressure_file_min + fi + + # Disable adaptive LMK for all targets & + # use Google default LMK series for all 64-bit targets >=2GB. + echo 0 > /sys/module/lowmemorykiller/parameters/enable_adaptive_lmk + + # Enable oom_reaper + if [ -f /sys/module/lowmemorykiller/parameters/oom_reaper ]; then + echo 1 > /sys/module/lowmemorykiller/parameters/oom_reaper + fi + + # Set allocstall_threshold to 0 for all targets. + # Set swappiness to 100 for all targets + echo 0 > /sys/module/vmpressure/parameters/allocstall_threshold + echo 100 > /proc/sys/vm/swappiness + + # Disable wsf for all targets beacause we are using efk. + # wsf Range : 1..1000 So set to bare minimum value 1. + echo 1 > /proc/sys/vm/watermark_scale_factor + + configure_zram_parameters +} + +bootmode=`getprop ro.bootmode` +if [ "charger" != $bootmode ]; then + start vendor.hbtp +fi + +# Apply settings for sdm429/sda429/sdm439/sda439 + +for cpubw in /sys/class/devfreq/*qcom,mincpubw* +do + echo "cpufreq" > $cpubw/governor +done + +for cpubw in /sys/class/devfreq/*qcom,cpubw* +do + echo "bw_hwmon" > $cpubw/governor + echo 20 > $cpubw/bw_hwmon/io_percent + echo 30 > $cpubw/bw_hwmon/guard_band_mbps +done + +for gpu_bimc_io_percent in /sys/class/devfreq/soc:qcom,gpubw/bw_hwmon/io_percent +do + echo 40 > $gpu_bimc_io_percent +done + +# Apply settings for sdm439/sda439 + +echo "schedutil" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor +echo "schedutil" > /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor + +# sched_load_boost as -6 is equivalent to target load as 85. +echo -6 > /sys/devices/system/cpu/cpu0/sched_load_boost +echo -6 > /sys/devices/system/cpu/cpu1/sched_load_boost +echo -6 > /sys/devices/system/cpu/cpu2/sched_load_boost +echo -6 > /sys/devices/system/cpu/cpu3/sched_load_boost + +# sched_load_boost as -6 is equivalent to target load as 85. +echo -6 > /sys/devices/system/cpu/cpu4/sched_load_boost +echo -6 > /sys/devices/system/cpu/cpu5/sched_load_boost +echo -6 > /sys/devices/system/cpu/cpu6/sched_load_boost +echo -6 > /sys/devices/system/cpu/cpu7/sched_load_boost + +# EAS scheduler (big.Little cluster related) settings +echo 93 > /proc/sys/kernel/sched_upmigrate +echo 83 > /proc/sys/kernel/sched_downmigrate +echo 140 > /proc/sys/kernel/sched_group_upmigrate +echo 120 > /proc/sys/kernel/sched_group_downmigrate + +# Bring up all cores online +echo 1 > /sys/devices/system/cpu/cpu1/online +echo 1 > /sys/devices/system/cpu/cpu2/online +echo 1 > /sys/devices/system/cpu/cpu3/online +echo 1 > /sys/devices/system/cpu/cpu4/online +echo 1 > /sys/devices/system/cpu/cpu5/online +echo 1 > /sys/devices/system/cpu/cpu6/online +echo 1 > /sys/devices/system/cpu/cpu7/online + +# Enable core control +echo 2 > /sys/devices/system/cpu/cpu0/core_ctl/min_cpus +echo 4 > /sys/devices/system/cpu/cpu0/core_ctl/max_cpus +echo 68 > /sys/devices/system/cpu/cpu0/core_ctl/busy_up_thres +echo 40 > /sys/devices/system/cpu/cpu0/core_ctl/busy_down_thres +echo 100 > /sys/devices/system/cpu/cpu0/core_ctl/offline_delay_ms +echo 1 > /sys/devices/system/cpu/cpu0/core_ctl/is_big_cluster +echo 4 > /sys/devices/system/cpu/cpu0/core_ctl/task_thres + +# Set Memory parameters +configure_memory_parameters + +#disable sched_boost +echo 0 > /proc/sys/kernel/sched_boost + +# Disable L2-GDHS low power modes +echo N > /sys/module/lpm_levels/system/pwr/pwr-l2-gdhs/idle_enabled +echo N > /sys/module/lpm_levels/system/pwr/pwr-l2-gdhs/suspend_enabled +echo N > /sys/module/lpm_levels/system/perf/perf-l2-gdhs/idle_enabled +echo N > /sys/module/lpm_levels/system/perf/perf-l2-gdhs/suspend_enabled + +# Enable low power modes +echo 0 > /sys/module/lpm_levels/parameters/sleep_disabled + +# Let kernel know our image version/variant/crm_version +if [ -f /sys/devices/soc0/select_image ]; then + image_version="10:" + image_version+=`getprop ro.build.id` + image_version+=":" + image_version+=`getprop ro.build.version.incremental` + image_variant=`getprop ro.product.name` + image_variant+="-" + image_variant+=`getprop ro.build.type` + oem_version=`getprop ro.build.version.codename` + echo 10 > /sys/devices/soc0/select_image + echo $image_version > /sys/devices/soc0/image_version + echo $image_variant > /sys/devices/soc0/image_variant + echo $oem_version > /sys/devices/soc0/image_crm_version +fi + +# Change console log level as per console config property +console_config=`getprop persist.console.silent.config` +case "$console_config" in + "1") + echo "Enable console config to $console_config" + echo 0 > /proc/sys/kernel/printk + ;; + *) + echo "Enable console config to $console_config" + ;; +esac + +# Parse misc partition path and set property +misc_link=$(ls -l /dev/block/bootdevice/by-name/misc) +real_path=${misc_link##*>} +setprop persist.vendor.mmi.misc_dev_path $real_path diff --git a/AnyKernel3/patch/placeholder b/AnyKernel3/patch/placeholder new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/AnyKernel3/ramdisk/placeholder b/AnyKernel3/ramdisk/placeholder new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/AnyKernel3/tools/ak3-core.sh b/AnyKernel3/tools/ak3-core.sh new file mode 100755 index 000000000000..274926a47b2c --- /dev/null +++ b/AnyKernel3/tools/ak3-core.sh @@ -0,0 +1,796 @@ +### AnyKernel methods (DO NOT CHANGE) +## osm0sis @ xda-developers + +OUTFD=$1; + +# set up working directory variables +[ "$home" ] || home=$PWD; +bootimg=$home/boot.img; +bin=$home/tools; +patch=$home/patch; +ramdisk=$home/ramdisk; +split_img=$home/split_img; + +### output/testing functions: +# ui_print "" [...] +ui_print() { + until [ ! "$1" ]; do + echo "ui_print $1 + ui_print" >> /proc/self/fd/$OUTFD; + shift; + done; +} + +# abort ["" [...]] +abort() { + ui_print " " "$@"; + exit 1; +} + +# contains +contains() { + [ "${1#*$2}" != "$1" ]; +} + +# file_getprop +file_getprop() { + grep "^$2=" "$1" | cut -d= -f2-; +} +### + +### file/directory attributes functions: +# set_perm [ ...] +set_perm() { + local uid gid mod; + uid=$1; gid=$2; mod=$3; + shift 3; + chown $uid:$gid "$@" || chown $uid.$gid "$@"; + chmod $mod "$@"; +} + +# set_perm_recursive [ ...] +set_perm_recursive() { + local uid gid dmod fmod; + uid=$1; gid=$2; dmod=$3; fmod=$4; + shift 4; + while [ "$1" ]; do + chown -R $uid:$gid "$1" || chown -R $uid.$gid "$1"; + find "$1" -type d -exec chmod $dmod {} +; + find "$1" -type f -exec chmod $fmod {} +; + shift; + done; +} +### + +### dump_boot functions: +# split_boot (dump and split image only) +split_boot() { + local dumpfail; + + if [ ! -e "$(echo $block | cut -d\ -f1)" ]; then + abort "Invalid partition. Aborting..."; + fi; + if [ "$(echo $block | grep ' ')" ]; then + block=$(echo $block | cut -d\ -f1); + customdd=$(echo $block | cut -d\ -f2-); + elif [ ! "$customdd" ]; then + local customdd="bs=1048576"; + fi; + if [ -f "$bin/nanddump" ]; then + $bin/nanddump -f $bootimg $block; + else + dd if=$block of=$bootimg $customdd; + fi; + [ $? != 0 ] && dumpfail=1; + + mkdir -p $split_img; + cd $split_img; + if [ -f "$bin/unpackelf" ] && $bin/unpackelf -i $bootimg -h -q 2>/dev/null; then + if [ -f "$bin/elftool" ]; then + mkdir elftool_out; + $bin/elftool unpack -i $bootimg -o elftool_out; + fi; + $bin/unpackelf -i $bootimg; + [ $? != 0 ] && dumpfail=1; + mv -f boot.img-kernel kernel.gz; + mv -f boot.img-ramdisk ramdisk.cpio.gz; + mv -f boot.img-cmdline cmdline.txt 2>/dev/null; + if [ -f boot.img-dt -a ! -f "$bin/elftool" ]; then + case $(od -ta -An -N4 boot.img-dt | sed -e 's/ del//' -e 's/ //g') in + QCDT|ELF) mv -f boot.img-dt dt;; + *) + gzip -c kernel.gz > kernel.gz-dtb; + cat boot.img-dt >> kernel.gz-dtb; + rm -f boot.img-dt kernel.gz; + ;; + esac; + fi; + elif [ -f "$bin/mboot" ]; then + $bin/mboot -u -f $bootimg; + elif [ -f "$bin/dumpimage" ]; then + dd bs=$(($(printf '%d\n' 0x$(hexdump -n 4 -s 12 -e '16/1 "%02x""\n"' $bootimg)) + 64)) count=1 conv=notrunc if=$bootimg of=boot-trimmed.img; + $bin/dumpimage -l boot-trimmed.img > header; + grep "Name:" header | cut -c15- > boot.img-name; + grep "Type:" header | cut -c15- | cut -d\ -f1 > boot.img-arch; + grep "Type:" header | cut -c15- | cut -d\ -f2 > boot.img-os; + grep "Type:" header | cut -c15- | cut -d\ -f3 | cut -d- -f1 > boot.img-type; + grep "Type:" header | cut -d\( -f2 | cut -d\) -f1 | cut -d\ -f1 | cut -d- -f1 > boot.img-comp; + grep "Address:" header | cut -c15- > boot.img-addr; + grep "Point:" header | cut -c15- > boot.img-ep; + $bin/dumpimage -p 0 -o kernel.gz boot-trimmed.img; + [ $? != 0 ] && dumpfail=1; + case $(cat boot.img-type) in + Multi) $bin/dumpimage -p 1 -o ramdisk.cpio.gz boot-trimmed.img;; + RAMDisk) mv -f kernel.gz ramdisk.cpio.gz;; + esac; + elif [ -f "$bin/rkcrc" ]; then + dd bs=4096 skip=8 iflag=skip_bytes conv=notrunc if=$bootimg of=ramdisk.cpio.gz; + else + $bin/magiskboot unpack -h $bootimg; + case $? in + 1) dumpfail=1;; + 2) touch chromeos;; + esac; + fi; + + if [ $? != 0 -o "$dumpfail" ]; then + abort "Dumping/splitting image failed. Aborting..."; + fi; + cd $home; +} + +# unpack_ramdisk (extract ramdisk only) +unpack_ramdisk() { + local comp; + + cd $split_img; + if [ -f ramdisk.cpio.gz ]; then + if [ -f "$bin/mkmtkhdr" ]; then + mv -f ramdisk.cpio.gz ramdisk.cpio.gz-mtk; + dd bs=512 skip=1 conv=notrunc if=ramdisk.cpio.gz-mtk of=ramdisk.cpio.gz; + fi; + mv -f ramdisk.cpio.gz ramdisk.cpio; + fi; + + if [ -f ramdisk.cpio ]; then + comp=$($bin/magiskboot decompress ramdisk.cpio 2>&1 | grep -v 'raw' | sed -n 's;.*\[\(.*\)\];\1;p'); + else + abort "No ramdisk found to unpack. Aborting..."; + fi; + if [ "$comp" ]; then + mv -f ramdisk.cpio ramdisk.cpio.$comp; + $bin/magiskboot decompress ramdisk.cpio.$comp ramdisk.cpio; + if [ $? != 0 ]; then + echo "Attempting ramdisk unpack with busybox $comp..." >&2; + $comp -dc ramdisk.cpio.$comp > ramdisk.cpio; + fi; + fi; + + [ -d $ramdisk ] && mv -f $ramdisk $home/rdtmp; + mkdir -p $ramdisk; + chmod 755 $ramdisk; + + cd $ramdisk; + EXTRACT_UNSAFE_SYMLINKS=1 cpio -d -F $split_img/ramdisk.cpio -i; + if [ $? != 0 -o ! "$(ls)" ]; then + abort "Unpacking ramdisk failed. Aborting..."; + fi; + if [ -d "$home/rdtmp" ]; then + cp -af $home/rdtmp/* .; + fi; +} +### dump_boot (dump and split image, then extract ramdisk) +dump_boot() { + split_boot; + unpack_ramdisk; +} +### + +### write_boot functions: +# repack_ramdisk (repack ramdisk only) +repack_ramdisk() { + local comp packfail mtktype; + + cd $home; + case $ramdisk_compression in + auto|"") comp=$(ls $split_img/ramdisk.cpio.* 2>/dev/null | grep -v 'mtk' | rev | cut -d. -f1 | rev);; + none|cpio) comp="";; + gz) comp=gzip;; + lzo) comp=lzop;; + bz2) comp=bzip2;; + lz4-l) comp=lz4_legacy;; + *) comp=$ramdisk_compression;; + esac; + + if [ -f "$bin/mkbootfs" ]; then + $bin/mkbootfs $ramdisk > ramdisk-new.cpio; + else + cd $ramdisk; + find . | cpio -H newc -o > $home/ramdisk-new.cpio; + fi; + [ $? != 0 ] && packfail=1; + + cd $home; + $bin/magiskboot cpio ramdisk-new.cpio test; + magisk_patched=$?; + [ $((magisk_patched & 3)) -eq 1 ] && $bin/magiskboot cpio ramdisk-new.cpio "extract .backup/.magisk $split_img/.magisk"; + if [ "$comp" ]; then + $bin/magiskboot compress=$comp ramdisk-new.cpio; + if [ $? != 0 ]; then + echo "Attempting ramdisk repack with busybox $comp..." >&2; + $comp -9c ramdisk-new.cpio > ramdisk-new.cpio.$comp; + [ $? != 0 ] && packfail=1; + rm -f ramdisk-new.cpio; + fi; + fi; + if [ "$packfail" ]; then + abort "Repacking ramdisk failed. Aborting..."; + fi; + + if [ -f "$bin/mkmtkhdr" -a -f "$split_img/boot.img-base" ]; then + mtktype=$(od -ta -An -N8 -j8 $split_img/ramdisk.cpio.gz-mtk | sed -e 's/ nul//g' -e 's/ //g' | tr '[:upper:]' '[:lower:]'); + case $mtktype in + rootfs|recovery) $bin/mkmtkhdr --$mtktype ramdisk-new.cpio*;; + esac; + fi; +} + +# flash_boot (build, sign and write image only) +flash_boot() { + local varlist i kernel ramdisk fdt cmdline comp part0 part1 nocompflag signfail pk8 cert avbtype; + + cd $split_img; + if [ -f "$bin/mkimage" ]; then + varlist="name arch os type comp addr ep"; + elif [ -f "$bin/mkbootimg" -a -f "$bin/unpackelf" -a -f boot.img-base ]; then + mv -f cmdline.txt boot.img-cmdline 2>/dev/null; + varlist="cmdline base pagesize kernel_offset ramdisk_offset tags_offset"; + fi; + for i in $varlist; do + if [ -f boot.img-$i ]; then + eval local $i=\"$(cat boot.img-$i)\"; + fi; + done; + + cd $home; + for i in zImage zImage-dtb Image Image-dtb Image.gz Image.gz-dtb Image.bz2 Image.bz2-dtb Image.lzo Image.lzo-dtb Image.lzma Image.lzma-dtb Image.xz Image.xz-dtb Image.lz4 Image.lz4-dtb Image.fit; do + if [ -f $i ]; then + kernel=$home/$i; + break; + fi; + done; + if [ "$kernel" ]; then + if [ -f "$bin/mkmtkhdr" -a -f "$split_img/boot.img-base" ]; then + $bin/mkmtkhdr --kernel $kernel; + kernel=$kernel-mtk; + fi; + elif [ "$(ls $split_img/kernel* 2>/dev/null)" ]; then + kernel=$(ls $split_img/kernel* | grep -v 'kernel_dtb' | tail -n1); + fi; + if [ "$(ls ramdisk-new.cpio* 2>/dev/null)" ]; then + ramdisk=$home/$(ls ramdisk-new.cpio* | tail -n1); + elif [ -f "$bin/mkmtkhdr" -a -f "$split_img/boot.img-base" ]; then + ramdisk=$split_img/ramdisk.cpio.gz-mtk; + else + ramdisk=$(ls $split_img/ramdisk.cpio* 2>/dev/null | tail -n1); + fi; + for fdt in dt recovery_dtbo dtb; do + for i in $home/$fdt $home/$fdt.img $split_img/$fdt; do + if [ -f $i ]; then + eval local $fdt=$i; + break; + fi; + done; + done; + + cd $split_img; + if [ -f "$bin/mkimage" ]; then + [ "$comp" == "uncompressed" ] && comp=none; + part0=$kernel; + case $type in + Multi) part1=":$ramdisk";; + RAMDisk) part0=$ramdisk;; + esac; + $bin/mkimage -A $arch -O $os -T $type -C $comp -a $addr -e $ep -n "$name" -d $part0$part1 $home/boot-new.img; + elif [ -f "$bin/elftool" ]; then + [ "$dt" ] && dt="$dt,rpm"; + [ -f cmdline.txt ] && cmdline="cmdline.txt@cmdline"; + $bin/elftool pack -o $home/boot-new.img header=elftool_out/header $kernel $ramdisk,ramdisk $dt $cmdline; + elif [ -f "$bin/mboot" ]; then + cp -f $kernel kernel; + cp -f $ramdisk ramdisk.cpio.gz; + $bin/mboot -d $split_img -f $home/boot-new.img; + elif [ -f "$bin/rkcrc" ]; then + $bin/rkcrc -k $ramdisk $home/boot-new.img; + elif [ -f "$bin/mkbootimg" -a -f "$bin/unpackelf" -a -f boot.img-base ]; then + [ "$dt" ] && dt="--dt $dt"; + $bin/mkbootimg --kernel $kernel --ramdisk $ramdisk --cmdline "$cmdline" --base $base --pagesize $pagesize --kernel_offset $kernel_offset --ramdisk_offset $ramdisk_offset --tags_offset "$tags_offset" $dt --output $home/boot-new.img; + else + [ "$kernel" ] && cp -f $kernel kernel; + [ "$ramdisk" ] && cp -f $ramdisk ramdisk.cpio; + [ "$dt" -a -f extra ] && cp -f $dt extra; + for i in dtb recovery_dtbo; do + [ "$(eval echo \$$i)" -a -f $i ] && cp -f $(eval echo \$$i) $i; + done; + case $kernel in + *Image*) + if [ ! "$magisk_patched" ]; then + $bin/magiskboot cpio ramdisk.cpio test; + magisk_patched=$?; + fi; + if [ $((magisk_patched & 3)) -eq 1 ]; then + ui_print " " "Magisk detected! Patching kernel so reflashing Magisk is not necessary..."; + comp=$($bin/magiskboot decompress kernel 2>&1 | grep -v 'raw' | sed -n 's;.*\[\(.*\)\];\1;p'); + ($bin/magiskboot split $kernel || $bin/magiskboot decompress $kernel kernel) 2>/dev/null; + if [ $? != 0 -a "$comp" ]; then + echo "Attempting kernel unpack with busybox $comp..." >&2; + $comp -dc $kernel > kernel; + fi; + $bin/magiskboot hexpatch kernel 736B69705F696E697472616D667300 77616E745F696E697472616D667300; + if [ "$(file_getprop $home/anykernel.sh do.systemless)" == 1 ]; then + strings kernel | grep -E 'Linux version.*#' > $home/vertmp; + fi; + if [ "$comp" ]; then + $bin/magiskboot compress=$comp kernel kernel.$comp; + if [ $? != 0 ]; then + echo "Attempting kernel repack with busybox $comp..." >&2; + $comp -9c kernel > kernel.$comp; + fi; + mv -f kernel.$comp kernel; + fi; + [ ! -f .magisk ] && $bin/magiskboot cpio ramdisk.cpio "extract .backup/.magisk .magisk"; + export $(cat .magisk); + [ $((magisk_patched & 8)) -ne 0 ] && export TWOSTAGEINIT=true; + for fdt in dtb extra kernel_dtb recovery_dtbo; do + [ -f $fdt ] && $bin/magiskboot dtb $fdt patch; + done; + else + case $kernel in + *-dtb) rm -f kernel_dtb;; + esac; + fi; + unset magisk_patched KEEPFORCEENCRYPT KEEPVERITY SHA1 TWOSTAGEINIT; + ;; + esac; + case $ramdisk_compression in + none|cpio) nocompflag="-n";; + esac; + $bin/magiskboot repack $nocompflag $bootimg $home/boot-new.img; + fi; + if [ $? != 0 ]; then + abort "Repacking image failed. Aborting..."; + fi; + [ -f .magisk ] && touch $home/magisk_patched; + + cd $home; + if [ -f "$bin/futility" -a -d "$bin/chromeos" ]; then + if [ -f "$split_img/chromeos" ]; then + echo "Signing with CHROMEOS..." >&2; + $bin/futility vbutil_kernel --pack boot-new-signed.img --keyblock $bin/chromeos/kernel.keyblock --signprivate $bin/chromeos/kernel_data_key.vbprivk --version 1 --vmlinuz boot-new.img --bootloader $bin/chromeos/empty --config $bin/chromeos/empty --arch arm --flags 0x1; + fi; + [ $? != 0 ] && signfail=1; + fi; + if [ -f "$bin/boot_signer-dexed.jar" -a -d "$bin/avb" ]; then + pk8=$(ls $bin/avb/*.pk8); + cert=$(ls $bin/avb/*.x509.*); + case $block in + *recovery*|*SOS*) avbtype=recovery;; + *) avbtype=boot;; + esac; + if [ "$(/system/bin/dalvikvm -Xnoimage-dex2oat -cp $bin/boot_signer-dexed.jar com.android.verity.BootSignature -verify boot.img 2>&1 | grep VALID)" ]; then + echo "Signing with AVBv1..." >&2; + /system/bin/dalvikvm -Xnoimage-dex2oat -cp $bin/boot_signer-dexed.jar com.android.verity.BootSignature /$avbtype boot-new.img $pk8 $cert boot-new-signed.img; + fi; + fi; + if [ $? != 0 -o "$signfail" ]; then + abort "Signing image failed. Aborting..."; + fi; + mv -f boot-new-signed.img boot-new.img 2>/dev/null; + + if [ ! -f boot-new.img ]; then + abort "No repacked image found to flash. Aborting..."; + elif [ "$(wc -c < boot-new.img)" -gt "$(wc -c < boot.img)" ]; then + abort "New image larger than boot partition. Aborting..."; + fi; + blockdev --setrw $block 2>/dev/null; + if [ -f "$bin/flash_erase" -a -f "$bin/nandwrite" ]; then + $bin/flash_erase $block 0 0; + $bin/nandwrite -p $block boot-new.img; + elif [ "$customdd" ]; then + dd if=/dev/zero of=$block $customdd 2>/dev/null; + dd if=boot-new.img of=$block $customdd; + else + cat boot-new.img /dev/zero > $block 2>/dev/null || true; + fi; + if [ $? != 0 ]; then + abort "Flashing image failed. Aborting..."; + fi; +} + +# flash_dtbo (flash dtbo only) +flash_dtbo() { + local i dtbo dtboblock; + + cd $home; + for i in dtbo dtbo.img; do + if [ -f $i ]; then + dtbo=$i; + break; + fi; + done; + + if [ "$dtbo" -a ! -f dtbo_flashed ]; then + dtboblock=/dev/block/bootdevice/by-name/dtbo$slot; + if [ ! -e "$dtboblock" ]; then + abort "dtbo partition could not be found. Aborting..."; + fi; + blockdev --setrw $dtboblock 2>/dev/null; + ui_print " " "$dtboblock"; + if [ -f "$bin/flash_erase" -a -f "$bin/nandwrite" ]; then + $bin/flash_erase $dtboblock 0 0; + $bin/nandwrite -p $dtboblock $dtbo; + elif [ "$customdd" ]; then + dd if=/dev/zero of=$dtboblock 2>/dev/null; + dd if=$dtbo of=$dtboblock; + else + cat $dtbo /dev/zero > $dtboblock 2>/dev/null || true; + fi; + if [ $? != 0 ]; then + abort "Flashing dtbo failed. Aborting..."; + fi; + touch dtbo_flashed; + fi; +} +### write_boot (repack ramdisk then build, sign and write image and dtbo) +write_boot() { + repack_ramdisk; + flash_boot; + flash_dtbo; +} +### + +### file editing functions: +# backup_file +backup_file() { [ ! -f $1~ ] && cp -fp $1 $1~; } + +# restore_file +restore_file() { [ -f $1~ ] && cp -fp $1~ $1; rm -f $1~; } + +# replace_string +replace_string() { + [ "$5" == "global" ] && local scope=g; + if ! grep -q "$2" $1; then + sed -i "s;${3};${4};${scope}" $1; + fi; +} + +# replace_section +replace_section() { + local begin endstr last end; + begin=$(grep -n "$2" $1 | head -n1 | cut -d: -f1); + if [ "$begin" ]; then + if [ "$3" == " " -o ! "$3" ]; then + endstr='^[[:space:]]*$'; + last=$(wc -l $1 | cut -d\ -f1); + else + endstr="$3"; + fi; + for end in $(grep -n "$endstr" $1 | cut -d: -f1) $last; do + if [ "$end" ] && [ "$begin" -lt "$end" ]; then + sed -i "${begin},${end}d" $1; + [ "$end" == "$last" ] && echo >> $1; + sed -i "${begin}s;^;${4}\n;" $1; + break; + fi; + done; + fi; +} + +# remove_section +remove_section() { + local begin endstr last end; + begin=$(grep -n "$2" $1 | head -n1 | cut -d: -f1); + if [ "$begin" ]; then + if [ "$3" == " " -o ! "$3" ]; then + endstr='^[[:space:]]*$'; + last=$(wc -l $1 | cut -d\ -f1); + else + endstr="$3"; + fi; + for end in $(grep -n "$endstr" $1 | cut -d: -f1) $last; do + if [ "$end" ] && [ "$begin" -lt "$end" ]; then + sed -i "${begin},${end}d" $1; + break; + fi; + done; + fi; +} + +# insert_line +insert_line() { + local offset line; + if ! grep -q "$2" $1; then + case $3 in + before) offset=0;; + after) offset=1;; + esac; + line=$((`grep -n "$4" $1 | head -n1 | cut -d: -f1` + offset)); + if [ -f $1 -a "$line" ] && [ "$(wc -l $1 | cut -d\ -f1)" -lt "$line" ]; then + echo "$5" >> $1; + else + sed -i "${line}s;^;${5}\n;" $1; + fi; + fi; +} + +# replace_line +replace_line() { + local lines line; + if grep -q "$2" $1; then + lines=$(grep -n "$2" $1 | cut -d: -f1 | sort -nr); + [ "$4" == "global" ] || lines=$(echo "$lines" | tail -n1); + for line in $lines; do + sed -i "${line}s;.*;${3};" $1; + done; + fi; +} + +# remove_line +remove_line() { + local lines line; + if grep -q "$2" $1; then + lines=$(grep -n "$2" $1 | cut -d: -f1 | sort -nr); + [ "$3" == "global" ] || lines=$(echo "$lines" | tail -n1); + for line in $lines; do + sed -i "${line}d" $1; + done; + fi; +} + +# prepend_file +prepend_file() { + if ! grep -q "$2" $1; then + echo "$(cat $patch/$3 $1)" > $1; + fi; +} + +# insert_file +insert_file() { + local offset line; + if ! grep -q "$2" $1; then + case $3 in + before) offset=0;; + after) offset=1;; + esac; + line=$((`grep -n "$4" $1 | head -n1 | cut -d: -f1` + offset)); + sed -i "${line}s;^;\n;" $1; + sed -i "$((line - 1))r $patch/$5" $1; + fi; +} + +# append_file +append_file() { + if ! grep -q "$2" $1; then + echo -ne "\n" >> $1; + cat $patch/$3 >> $1; + echo -ne "\n" >> $1; + fi; +} + +# replace_file +replace_file() { + cp -pf $patch/$3 $1; + chmod $2 $1; +} + +# patch_fstab block|mount|fstype|options|flags +patch_fstab() { + local entry part newpart newentry; + entry=$(grep "$2" $1 | grep "$3"); + if [ ! "$(echo "$entry" | grep "$6")" -o "$6" == " " -o ! "$6" ]; then + case $4 in + block) part=$(echo "$entry" | awk '{ print $1 }');; + mount) part=$(echo "$entry" | awk '{ print $2 }');; + fstype) part=$(echo "$entry" | awk '{ print $3 }');; + options) part=$(echo "$entry" | awk '{ print $4 }');; + flags) part=$(echo "$entry" | awk '{ print $5 }');; + esac; + newpart=$(echo "$part" | sed -e "s;${5};${6};" -e "s; ;;g" -e 's;,\{2,\};,;g' -e 's;,*$;;g' -e 's;^,;;g'); + newentry=$(echo "$entry" | sed "s;${part};${newpart};"); + sed -i "s;${entry};${newentry};" $1; + fi; +} + +# patch_cmdline +patch_cmdline() { + local cmdfile cmdtmp match; + if [ -f "$split_img/cmdline.txt" ]; then + cmdfile=$split_img/cmdline.txt; + else + cmdfile=$home/cmdtmp; + grep "^cmdline=" $split_img/header | cut -d= -f2- > $cmdfile; + fi; + if ! grep -q "$1" $cmdfile; then + cmdtmp=$(cat $cmdfile); + echo "$cmdtmp $2" > $cmdfile; + sed -i -e 's; *; ;g' -e 's;[ \t]*$;;' $cmdfile; + else + match=$(grep -o "$1.*$" $cmdfile | cut -d\ -f1); + sed -i -e "s;${match};${2};" -e 's; *; ;g' -e 's;[ \t]*$;;' $cmdfile; + fi; + if [ -f "$home/cmdtmp" ]; then + sed -i "s|^cmdline=.*|cmdline=$(cat $cmdfile)|" $split_img/header; + rm -f $cmdfile; + fi; +} + +# patch_prop +patch_prop() { + if ! grep -q "^$2=" $1; then + echo -ne "\n$2=$3\n" >> $1; + else + local line=$(grep -n "^$2=" $1 | head -n1 | cut -d: -f1); + sed -i "${line}s;.*;${2}=${3};" $1; + fi; +} + +# patch_ueventd +patch_ueventd() { + local file dev perm user group newentry line; + file=$1; dev=$2; perm=$3; user=$4; + shift 4; + group="$@"; + newentry=$(printf "%-23s %-4s %-8s %s\n" "$dev" "$perm" "$user" "$group"); + line=$(grep -n "$dev" $file | head -n1 | cut -d: -f1); + if [ "$line" ]; then + sed -i "${line}s;.*;${newentry};" $file; + else + echo -ne "\n$newentry\n" >> $file; + fi; +} +### + +### configuration/setup functions: +# reset_ak [keep] +reset_ak() { + local current i; + + current=$(dirname $home/*-files/current); + if [ -d "$current" ]; then + for i in $bootimg $home/boot-new.img; do + [ -e $i ] && cp -af $i $current; + done; + for i in $current/*; do + [ -f $i ] && rm -f $home/$(basename $i); + done; + fi; + [ -d $split_img ] && rm -rf $ramdisk; + rm -rf $bootimg $split_img $home/*-new* $home/*-files/current; + + if [ "$1" == "keep" ]; then + [ -d $home/rdtmp ] && mv -f $home/rdtmp $ramdisk; + else + rm -rf $patch $home/rdtmp; + fi; + if [ ! "$no_block_display" ]; then + ui_print " "; + fi; + setup_ak; +} + +# setup_ak +setup_ak() { + local blockfiles parttype name part mtdmount mtdpart mtdname target; + + # slot detection enabled by is_slot_device=1 or auto (from anykernel.sh) + case $is_slot_device in + 1|auto) + slot=$(getprop ro.boot.slot_suffix 2>/dev/null); + [ "$slot" ] || slot=$(grep -o 'androidboot.slot_suffix=.*$' /proc/cmdline | cut -d\ -f1 | cut -d= -f2); + if [ ! "$slot" ]; then + slot=$(getprop ro.boot.slot 2>/dev/null); + [ "$slot" ] || slot=$(grep -o 'androidboot.slot=.*$' /proc/cmdline | cut -d\ -f1 | cut -d= -f2); + [ "$slot" ] && slot=_$slot; + fi; + if [ "$slot" ]; then + if [ -d /postinstall/tmp -a ! "$slot_select" ]; then + slot_select=inactive; + fi; + case $slot_select in + inactive) + case $slot in + _a) slot=_b;; + _b) slot=_a;; + esac; + ;; + esac; + fi; + if [ ! "$slot" -a "$is_slot_device" == 1 ]; then + abort "Unable to determine active slot. Aborting..."; + fi; + ;; + esac; + + # automate simple multi-partition setup for boot_img_hdr_v3 + vendor_boot + cd $home; + if [ -e "/dev/block/bootdevice/by-name/vendor_boot$slot" -a ! -f vendor_setup ] && [ -f dtb -o -d vendor_ramdisk -o -d vendor_patch ]; then + echo "Setting up for simple automatic vendor_boot flashing..." >&2; + (mkdir boot-files; + mv -f Image* ramdisk patch boot-files; + mkdir vendor_boot-files; + mv -f dtb vendor_boot-files; + mv -f vendor_ramdisk vendor_boot-files/ramdisk; + mv -f vendor_patch vendor_boot-files/patch) 2>/dev/null; + touch vendor_setup; + fi; + + # allow multi-partition ramdisk modifying configurations (using reset_ak) + if [ "$block" ] && [ ! -d "$ramdisk" -a ! -d "$patch" ]; then + blockfiles=$home/$(basename $block)-files; + if [ "$(ls $blockfiles 2>/dev/null)" ]; then + cp -af $blockfiles/* $home; + else + mkdir $blockfiles; + fi; + touch $blockfiles/current; + fi; + + # target block partition detection enabled by block=boot recovery or auto (from anykernel.sh) + case $block in + auto|"") block=boot;; + esac; + case $block in + boot|recovery|vendor_boot) + case $block in + boot) parttype="ramdisk boot BOOT LNX android_boot bootimg KERN-A kernel KERNEL";; + recovery) parttype="ramdisk_recovery recovery RECOVERY SOS android_recovery";; + vendor_boot) parttype="vendor_boot";; + esac; + for name in $parttype; do + for part in $name$slot $name; do + if [ "$(grep -w "$part" /proc/mtd 2> /dev/null)" ]; then + mtdmount=$(grep -w "$part" /proc/mtd); + mtdpart=$(echo $mtdmount | cut -d\" -f2); + if [ "$mtdpart" == "$part" ]; then + mtdname=$(echo $mtdmount | cut -d: -f1); + else + abort "Unable to determine mtd $block partition. Aborting..."; + fi; + if [ -e /dev/mtd/$mtdname ]; then + target=/dev/mtd/$mtdname; + fi; + elif [ -e /dev/block/by-name/$part ]; then + target=/dev/block/by-name/$part; + elif [ -e /dev/block/bootdevice/by-name/$part ]; then + target=/dev/block/bootdevice/by-name/$part; + elif [ -e /dev/block/platform/*/by-name/$part ]; then + target=/dev/block/platform/*/by-name/$part; + elif [ -e /dev/block/platform/*/*/by-name/$part ]; then + target=/dev/block/platform/*/*/by-name/$part; + elif [ -e /dev/$part ]; then + target=/dev/$part; + fi; + [ "$target" ] && break 2; + done; + done; + if [ "$target" ]; then + block=$(ls $target 2>/dev/null); + else + abort "Unable to determine $block partition. Aborting..."; + fi; + ;; + *) + if [ "$slot" ]; then + [ -e "$block$slot" ] && block=$block$slot; + fi; + ;; + esac; + if [ ! "$no_block_display" ]; then + ui_print "$block"; + fi; +} +### + +### end methods + +setup_ak; diff --git a/AnyKernel3/tools/busybox b/AnyKernel3/tools/busybox new file mode 100755 index 000000000000..bbc7e65c3907 Binary files /dev/null and b/AnyKernel3/tools/busybox differ diff --git a/AnyKernel3/tools/magiskboot b/AnyKernel3/tools/magiskboot new file mode 100755 index 000000000000..709773a7ea0f Binary files /dev/null and b/AnyKernel3/tools/magiskboot differ diff --git a/AnyKernel3/tools/magiskpolicy b/AnyKernel3/tools/magiskpolicy new file mode 100755 index 000000000000..881014267310 Binary files /dev/null and b/AnyKernel3/tools/magiskpolicy differ