├── LICENSE ├── META-INF └── com │ └── google │ └── android │ ├── update-binary │ └── updater-script ├── anykernel.sh └── tools ├── ak3-core.sh ├── busybox ├── fec ├── httools_static ├── lptools_static ├── magiskboot ├── magiskpolicy └── snapshotupdater_static /LICENSE: -------------------------------------------------------------------------------- 1 | ## AnyKernel3 (AK3), and AnyKernel2/AnyKernel 2.0 (AK2) Scripts License: 2 | 3 | AnyKernel (versions 2.0/2 and later) Android image modifying scripts. 4 | Copyright (c) 2019 Chris Renshaw (osm0sis @ xda-developers), 5 | and additional contributors per readily available commit history/credits. 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without 9 | modification, are permitted (subject to the limitations in the disclaimer 10 | below) provided that the following conditions are met: 11 | 12 | * Redistributions of source code must retain the above copyright notice, 13 | this list of conditions and the following disclaimer. 14 | 15 | * Redistributions in binary form must reproduce the above copyright 16 | notice, this list of conditions and the following disclaimer in the 17 | documentation and/or other materials provided with the distribution. 18 | 19 | * Neither the name of the copyright holder nor the names of its 20 | contributors may be used to endorse or promote products derived from this 21 | software without specific prior written permission. 22 | 23 | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY 24 | THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 25 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 27 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 28 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 29 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 31 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 32 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 | POSSIBILITY OF SUCH DAMAGE. 35 | 36 | 37 | ## Included Binary Licenses: 38 | 39 | magiskboot, magiskpolicy (Magisk): GPLv3+ 40 | 41 | Magisk, including all git submodules are free software: 42 | you can redistribute it and/or modify it under the terms of the 43 | GNU General Public License as published by the Free Software Foundation, 44 | either version 3 of the License, or (at your option) any later version. 45 | 46 | This program is distributed in the hope that it will be useful, 47 | but WITHOUT ANY WARRANTY; without even the implied warranty of 48 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 49 | GNU General Public License for more details. 50 | 51 | You should have received a copy of the GNU General Public License 52 | along with this program. If not, see . 53 | 54 | Per Section 6(d), official compiled binaries from unmodified source: 55 | https://github.com/topjohnwu/Magisk 56 | 57 | busybox: GPLv2 58 | 59 | BusyBox is distributed under version 2 of the General Public 60 | License. Version 2 is the only version of this license which this 61 | version of BusyBox (or modified versions derived from this one) may 62 | be distributed under. 63 | 64 | This program is free software; you can redistribute it and/or modify 65 | it under the terms of the GNU General Public License as published by 66 | the Free Software Foundation. 67 | 68 | This program is distributed in the hope that it will be useful, 69 | but WITHOUT ANY WARRANTY; without even the implied warranty of 70 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 71 | GNU General Public License for more details. 72 | 73 | Per Section 3(b), self-compiled binary from modified source: 74 | https://git.busybox.net/busybox/ 75 | https://github.com/osm0sis/android-busybox-ndk 76 | (pre-patched source tree used to build available upon request) 77 | 78 | lptools_static: Apache License 2.0 79 | fec: Apache License 2.0 80 | snapshotupdater_static: Apache License 2.0 81 | 82 | Copyright their respective authors, (linked below). 83 | 84 | Licensed under the Apache License, Version 2.0 (the "License"); 85 | you may not use this file except in compliance with the License. 86 | You may obtain a copy of the License at 87 | 88 | http://www.apache.org/licenses/LICENSE-2.0 89 | 90 | Unless required by applicable law or agreed to in writing, software 91 | distributed under the License is distributed on an "AS IS" BASIS, 92 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 93 | See the License for the specific language governing permissions and 94 | limitations under the License. 95 | 96 | Source not required, however, respective sources are provided: 97 | https://github.com/phhusson/vendor_lptools 98 | https://android.googlesource.com/platform/system/extras/+/master/verity/fec/ 99 | https://github.com/capntrips/SnapshotUpdater 100 | 101 | httools_static: MIT License 102 | 103 | Copyright (c) 2022 capntrips 104 | 105 | Permission is hereby granted, free of charge, to any person obtaining a copy 106 | of this software and associated documentation files (the "Software"), to deal 107 | in the Software without restriction, including without limitation the rights 108 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 109 | copies of the Software, and to permit persons to whom the Software is 110 | furnished to do so, subject to the following conditions: 111 | 112 | The above copyright notice and this permission notice shall be included in all 113 | copies or substantial portions of the Software. 114 | 115 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 116 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 117 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 118 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 119 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 120 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 121 | SOFTWARE. 122 | 123 | Source not required, however, respective source is provided: 124 | https://github.com/capntrips/HashtreePatcher 125 | 126 | 127 | ## Optional Binary Licenses: 128 | 129 | mkbootfs, mkbootimg: Apache License 2.0 130 | mkmtkhdr: Apache License 2.0, implied (AOSP mkbootimg derived) 131 | boot_signer*.jar: Apache License 2.0 132 | 133 | Copyright (c) 2008 The Android Open Source Project 134 | 135 | Licensed under the Apache License, Version 2.0 (the "License"); 136 | you may not use this file except in compliance with the License. 137 | You may obtain a copy of the License at 138 | 139 | http://www.apache.org/licenses/LICENSE-2.0 140 | 141 | Unless required by applicable law or agreed to in writing, software 142 | distributed under the License is distributed on an "AS IS" BASIS, 143 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 144 | See the License for the specific language governing permissions and 145 | limitations under the License. 146 | 147 | Source not required, however, respective sources are provided: 148 | https://github.com/osm0sis/mkbootfs 149 | https://github.com/osm0sis/mkbootimg 150 | https://github.com/osm0sis/mkmtkhdr 151 | https://android.googlesource.com/platform/system/extras/+/master/verity/ 152 | 153 | flash_erase, nanddump, nandwrite (mtd-utils): GPLv2 154 | dumpimage, mkimage (U-Boot): GPLv2+ 155 | mboot: GPLv2 (Intel mboot.py derived) 156 | 157 | Copyright their respective authors, (linked below). 158 | 159 | This program is free software; you can redistribute it and/or modify 160 | it under the terms of the GNU General Public License as published by 161 | the Free Software Foundation. 162 | 163 | This program is distributed in the hope that it will be useful, 164 | but WITHOUT ANY WARRANTY; without even the implied warranty of 165 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 166 | GNU General Public License for more details. 167 | 168 | Per Section 3(b), self-compiled binaries from unmodified respective sources: 169 | http://git.infradead.org/mtd-utils.git 170 | https://gitlab.denx.de/u-boot/u-boot 171 | https://github.com/osm0sis/mboot 172 | 173 | futility: BSD 3-Clause License (Chromium OS) 174 | unpackelf, elftool: BSD 3-Clause License, implied (Sony mkelf.py derived) 175 | 176 | Copyright their respective authors, (linked below). 177 | 178 | Redistribution and use in source and binary forms, with or without 179 | modification, are permitted provided that the following conditions are 180 | met: 181 | * Redistributions of source code must retain the above copyright 182 | notice, this list of conditions and the following disclaimer. 183 | * Redistributions in binary form must reproduce the above copyright 184 | notice, this list of conditions and the following disclaimer in 185 | the documentation and/or other materials provided with the 186 | distribution. 187 | * Neither the name of the copyright holder nor the names of its 188 | contributors may be used to endorse or promote products derived 189 | from this software without specific prior written permission. 190 | 191 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 192 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 193 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 194 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 195 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 196 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 197 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 198 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 199 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 200 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 201 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 202 | 203 | Source not required, however, respective sources are provided: 204 | https://github.com/osm0sis/futility 205 | https://github.com/osm0sis/unpackelf 206 | https://github.com/osm0sis/elftool 207 | (https://github.com/sonyxperiadev/device-sony-lt26/tree/master/tools) 208 | 209 | rkcrc: BSD 2-Clause License 210 | 211 | Copyright (c) 2010, 2011 Fukaumi Naoki 212 | Copyright (c) 2013 Ivo van Poorten 213 | All rights reserved. 214 | 215 | Redistribution and use in source and binary forms, with or without 216 | modification, are permitted provided that the following conditions are 217 | met: 218 | 1. Redistributions of source code must retain the above copyright 219 | notice, this list of conditions and the following disclaimer. 220 | 2. Redistributions in binary form must reproduce the above copyright 221 | notice, this list of conditions and the following disclaimer in the 222 | documentation and/or other materials provided with the distribution. 223 | 224 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR 225 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 226 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 227 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 228 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 229 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 230 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 231 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 232 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 233 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 234 | 235 | Source not required, however, respective source is provided: 236 | https://github.com/linux-rockchip/rkflashtool 237 | 238 | 239 | ## Additional Build Scripts for Listed Binaries (where used): 240 | 241 | osm0sis' Odds and Ends Thread - Knowledge Base: 242 | https://forum.xda-developers.com/t/tools-zips-scripts-osm0sis-odds-and-ends-multiple-devices-platforms.2239421/#post-53554719 243 | 244 | -------------------------------------------------------------------------------- /META-INF/com/google/android/update-binary: -------------------------------------------------------------------------------- 1 | #!/sbin/sh 2 | # AnyKernel3 Backend (DO NOT CHANGE) 3 | # osm0sis @ xda-developers 4 | 5 | export OUTFD=$2; 6 | export ZIPFILE="$3"; 7 | 8 | BOOTMODE=false; 9 | ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true; 10 | $BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true; 11 | 12 | DIR=/sdcard; 13 | $BOOTMODE || DIR=$(dirname "$ZIPFILE"); 14 | [ $DIR == "/sideload" ] && DIR=/tmp; 15 | 16 | [ -d /postinstall/tmp ] && POSTINSTALL=/postinstall; 17 | [ "$AKHOME" ] || export AKHOME=$POSTINSTALL/tmp/anykernel; 18 | [ "$ANDROID_ROOT" ] || ANDROID_ROOT=/system; 19 | 20 | ui_print() { 21 | until [ ! "$1" ]; do 22 | echo "ui_print $1 23 | ui_print" >> /proc/self/fd/$OUTFD; 24 | shift; 25 | done; 26 | } 27 | ui_printfile() { 28 | local line losrpad; 29 | $BOOTMODE || [ -e /twres ] || losrpad='| '; # work around LOS Recovery eating leading whitespace 30 | while IFS='' read -r line || [[ -n "$line" ]]; do 31 | ui_print "$losrpad$line"; 32 | done < $1; 33 | } 34 | show_progress() { echo "progress $1 $2" >> /proc/self/fd/$OUTFD; } 35 | file_getprop() { grep "^$2=" "$1" | tail -n1 | cut -d= -f2-; } 36 | find_slot() { 37 | local slot=$(getprop ro.boot.slot_suffix 2>/dev/null); 38 | [ "$slot" ] || slot=$(grep -o 'androidboot.slot_suffix=.*$' /proc/cmdline | cut -d\ -f1 | cut -d= -f2); 39 | if [ ! "$slot" ]; then 40 | slot=$(getprop ro.boot.slot 2>/dev/null); 41 | [ "$slot" ] || slot=$(grep -o 'androidboot.slot=.*$' /proc/cmdline | cut -d\ -f1 | cut -d= -f2); 42 | [ "$slot" ] && slot=_$slot; 43 | fi; 44 | [ "$slot" == "normal" ] && unset slot; 45 | [ "$slot" ] && echo "$slot"; 46 | } 47 | setup_mountpoint() { 48 | [ -L $1 ] && mv -f $1 ${1}_link; 49 | if [ ! -d $1 ]; then 50 | rm -f $1; 51 | mkdir -p $1; 52 | fi; 53 | } 54 | is_mounted() { mount | grep -q " $1 "; } 55 | mount_apex() { 56 | [ -d /system_root/system/apex ] || return 1; 57 | local apex dest loop minorx num shcon var; 58 | setup_mountpoint /apex; 59 | mount -t tmpfs tmpfs /apex -o mode=755 && touch /apex/apextmp; 60 | shcon=$(cat /proc/self/attr/current); 61 | echo "u:r:su:s0" > /proc/self/attr/current 2>/dev/null; # work around LOS Recovery not allowing loop mounts in recovery context 62 | minorx=1; 63 | [ -e /dev/block/loop1 ] && minorx=$(ls -l /dev/block/loop1 | awk '{ print $6 }'); 64 | num=0; 65 | for apex in /system_root/system/apex/*; do 66 | dest=/apex/$(basename $apex | sed -E -e 's;\.apex$|\.capex$;;' -e 's;\.current$|\.release$;;'); 67 | mkdir -p $dest; 68 | case $apex in 69 | *.apex|*.capex) 70 | unzip -qo $apex original_apex -d /apex; 71 | [ -f /apex/original_apex ] && apex=/apex/original_apex; 72 | unzip -qo $apex apex_payload.img -d /apex; 73 | mv -f /apex/original_apex $dest.apex 2>/dev/null; 74 | mv -f /apex/apex_payload.img $dest.img; 75 | mount -t ext4 -o ro,noatime $dest.img $dest 2>/dev/null && echo "$dest (direct)" >&2; 76 | if [ $? != 0 ]; then 77 | while [ $num -lt 64 ]; do 78 | loop=/dev/block/loop$num; 79 | [ -e $loop ] || mknod $loop b 7 $((num * minorx)); 80 | losetup $loop $dest.img 2>/dev/null; 81 | num=$((num + 1)); 82 | losetup $loop | grep -q $dest.img && break; 83 | done; 84 | mount -t ext4 -o ro,loop,noatime $loop $dest && echo "$dest (loop)" >&2; 85 | if [ $? != 0 ]; then 86 | losetup -d $loop 2>/dev/null; 87 | if [ $num -eq 64 -a $(losetup -f) == "/dev/block/loop0" ]; then 88 | echo "Aborting apex mounts due to broken environment..." >&2; 89 | break; 90 | fi; 91 | fi; 92 | fi; 93 | ;; 94 | *) mount -o bind $apex $dest && echo "$dest (bind)" >&2;; 95 | esac; 96 | done; 97 | echo "$shcon" > /proc/self/attr/current 2>/dev/null; 98 | for var in $(grep -o 'export .* /.*' /system_root/init.environ.rc | awk '{ print $2 }'); do 99 | eval OLD_${var}=\$$var; 100 | done; 101 | $(grep -o 'export .* /.*' /system_root/init.environ.rc | sed 's; /;=/;'); unset export; 102 | touch /apex/apexak3; 103 | } 104 | umount_apex() { 105 | [ -f /apex/apexak3 ] || return 1; 106 | echo "Unmounting apex..." >&2; 107 | local dest loop var; 108 | for var in $(grep -o 'export .* /.*' /system_root/init.environ.rc 2>/dev/null | awk '{ print $2 }'); do 109 | if [ "$(eval echo \$OLD_$var)" ]; then 110 | eval $var=\$OLD_${var}; 111 | else 112 | eval unset $var; 113 | fi; 114 | unset OLD_${var}; 115 | done; 116 | for dest in $(find /apex -type d -mindepth 1 -maxdepth 1); do 117 | loop=$(mount | grep $dest | grep loop | cut -d\ -f1); 118 | umount -l $dest; 119 | losetup $loop >/dev/null 2>&1 && losetup -d $loop; 120 | done; 121 | [ -f /apex/apextmp ] && umount /apex; 122 | rm -rf /apex/apexak3 /apex 2>/dev/null; 123 | } 124 | mount_all() { 125 | local byname mount slot system; 126 | echo "Mounting..." >&2; 127 | byname=bootdevice/by-name; 128 | [ -d /dev/block/$byname ] || byname=$(find /dev/block/platform -type d -name by-name 2>/dev/null | head -n1 | cut -d/ -f4-); 129 | [ -e /dev/block/$byname/super -a -d /dev/block/mapper ] && byname=mapper; 130 | [ -e /dev/block/$byname/system ] || slot=$(find_slot); 131 | for mount in /cache /data /metadata /persist; do 132 | if ! is_mounted $mount; then 133 | mount $mount 2>/dev/null && echo "$mount (fstab)" >&2 && UMOUNTLIST="$UMOUNTLIST $mount"; 134 | if [ $? != 0 -a -e /dev/block/$byname$mount ]; then 135 | setup_mountpoint $mount; 136 | mount -o ro -t auto /dev/block/$byname$mount $mount && echo "$mount (direct)" >&2 && UMOUNTLIST="$UMOUNTLIST $mount"; 137 | fi; 138 | fi; 139 | done; 140 | setup_mountpoint $ANDROID_ROOT; 141 | if ! is_mounted $ANDROID_ROOT; then 142 | mount -o ro -t auto $ANDROID_ROOT 2>/dev/null && echo "$ANDROID_ROOT (\$ANDROID_ROOT)" >&2; 143 | fi; 144 | case $ANDROID_ROOT in 145 | /system_root) setup_mountpoint /system;; 146 | /system) 147 | if ! is_mounted /system && ! is_mounted /system_root; then 148 | setup_mountpoint /system_root; 149 | mount -o ro -t auto /system_root && echo "/system_root (fstab)" >&2; 150 | elif [ -f /system/system/build.prop ]; then 151 | setup_mountpoint /system_root; 152 | mount --move /system /system_root && echo "/system_root (moved)" >&2; 153 | fi; 154 | if [ $? != 0 ]; then 155 | (umount /system; 156 | umount -l /system) 2>/dev/null; 157 | mount -o ro -t auto /dev/block/$byname/system$slot /system_root && echo "/system_root (direct)" >&2; 158 | fi; 159 | ;; 160 | esac; 161 | [ -f /system_root/system/build.prop ] && system=/system; 162 | for mount in /vendor /product /system_ext; do 163 | mount -o ro -t auto $mount 2>/dev/null && echo "$mount (fstab)" >&2; 164 | if [ $? != 0 ] && [ -L /system$mount -o -L /system_root$system$mount ]; then 165 | setup_mountpoint $mount; 166 | mount -o ro -t auto /dev/block/$byname$mount$slot $mount && echo "$mount (direct)" >&2; 167 | fi; 168 | done; 169 | if is_mounted /system_root; then 170 | mount_apex; 171 | mount -o bind /system_root$system /system && echo "/system (bind)" >&2; 172 | fi; 173 | echo " " >&2; 174 | } 175 | umount_all() { 176 | local mount; 177 | echo "Unmounting..." >&2; 178 | (if [ ! -d /postinstall/tmp ]; then 179 | umount /system; 180 | umount -l /system; 181 | fi) 2>/dev/null; 182 | umount_apex; 183 | (if [ ! -d /postinstall/tmp ]; then 184 | umount /system_root; 185 | umount -l /system_root; 186 | fi; 187 | PATH="$OLD_PATH" umount /vendor; # busybox umount /vendor breaks recovery on some hacky devices 188 | PATH="$OLD_PATH" umount -l /vendor; 189 | for mount in /mnt/system /mnt/vendor /product /mnt/product /system_ext /mnt/system_ext $UMOUNTLIST; do 190 | umount $mount; 191 | umount -l $mount; 192 | done) 2>/dev/null; 193 | } 194 | setup_env() { 195 | $BOOTMODE && return 1; 196 | mount -o bind /dev/urandom /dev/random; 197 | if [ -L /etc ]; then 198 | setup_mountpoint /etc; 199 | cp -af /etc_link/* /etc; 200 | sed -i 's; / ; /system_root ;' /etc/fstab; 201 | fi; 202 | umount_all; 203 | mount_all; 204 | OLD_LD_PATH=$LD_LIBRARY_PATH; 205 | OLD_LD_PRE=$LD_PRELOAD; 206 | OLD_LD_CFG=$LD_CONFIG_FILE; 207 | unset LD_LIBRARY_PATH LD_PRELOAD LD_CONFIG_FILE; 208 | if [ ! "$(getprop 2>/dev/null)" ]; then 209 | getprop() { 210 | local propdir propfile propval; 211 | for propdir in / /system_root /system /vendor /product /product/etc /system_ext/etc /odm/etc; do 212 | for propfile in default.prop build.prop; do 213 | if [ "$propval" ]; then 214 | break 2; 215 | else 216 | propval="$(file_getprop $propdir/$propfile $1 2>/dev/null)"; 217 | fi; 218 | done; 219 | done; 220 | echo "$propval"; 221 | } 222 | elif [ ! "$(getprop ro.build.type 2>/dev/null)" ]; then 223 | getprop() { 224 | ($(which getprop) | grep "$1" | cut -d[ -f3 | cut -d] -f1) 2>/dev/null; 225 | } 226 | fi; 227 | } 228 | restore_env() { 229 | $BOOTMODE && return 1; 230 | local dir; 231 | unset -f getprop; 232 | [ "$OLD_LD_PATH" ] && export LD_LIBRARY_PATH=$OLD_LD_PATH; 233 | [ "$OLD_LD_PRE" ] && export LD_PRELOAD=$OLD_LD_PRE; 234 | [ "$OLD_LD_CFG" ] && export LD_CONFIG_FILE=$OLD_LD_CFG; 235 | unset OLD_LD_PATH OLD_LD_PRE OLD_LD_CFG; 236 | sleep 1; 237 | umount_all; 238 | [ -L /etc_link ] && rm -rf /etc/*; 239 | (for dir in /etc /apex /system_root /system /vendor /product /system_ext /metadata /persist; do 240 | if [ -L "${dir}_link" ]; then 241 | rmdir $dir; 242 | mv -f ${dir}_link $dir; 243 | fi; 244 | done; 245 | umount -l /dev/random) 2>/dev/null; 246 | } 247 | setup_bb() { 248 | local arch32 bb; 249 | for arch32 in x86 arm; do 250 | if [ -d $AKHOME/tools/$arch32 ]; then 251 | bb=$AKHOME/tools/$arch32/busybox; 252 | chmod 755 $bb; 253 | $bb >/dev/null 2>&1; 254 | if [ $? == 0 ]; then 255 | $bb mv -f $AKHOME/tools/$arch32/* $AKHOME/tools; 256 | break; 257 | fi; 258 | fi; 259 | done; 260 | bb=$AKHOME/tools/busybox; 261 | chmod 755 $bb; 262 | $bb chmod -R 755 tools bin; 263 | $bb --install -s bin; 264 | } 265 | debugging() { 266 | local debug log path; 267 | case $(basename "$ZIPFILE" .zip) in 268 | *-debugging) debug=1;; 269 | esac; 270 | for path in /tmp /cache /metadata /persist; do 271 | [ -f $path/.ak3-debugging ] && debug=1; 272 | done; 273 | if [ "$debug" ]; then 274 | ui_print " " "Creating debugging archive in $DIR..."; 275 | [ -f /tmp/recovery.log ] && log=/tmp/recovery.log; 276 | tar -czf "$DIR/anykernel3-$(date +%Y-%m-%d_%H%M%S)-debug.tgz" $AKHOME $log; 277 | fi; 278 | } 279 | cleanup() { 280 | cd $AKHOME/../; 281 | rm -rf $AKHOME; 282 | } 283 | abort() { 284 | ui_print "$@"; 285 | debugging; 286 | restore_env; 287 | if [ ! -f anykernel.sh -o "$(file_getprop anykernel.sh do.cleanuponabort 2>/dev/null)" == 1 ]; then 288 | cleanup; 289 | fi; 290 | exit 1; 291 | } 292 | do_devicecheck() { 293 | [ "$(file_getprop anykernel.sh do.devicecheck)" == 1 ] || return 1; 294 | local device devicename match product testname vendordevice vendorproduct; 295 | ui_print "Checking device..."; 296 | device=$(getprop ro.product.device 2>/dev/null); 297 | product=$(getprop ro.build.product 2>/dev/null); 298 | vendordevice=$(getprop ro.product.vendor.device 2>/dev/null); 299 | vendorproduct=$(getprop ro.vendor.product.device 2>/dev/null); 300 | for testname in $(grep '^device.name.*=' anykernel.sh | cut -d= -f2-); do 301 | for devicename in $device $product $vendordevice $vendorproduct; do 302 | if [ "$devicename" == "$testname" ]; then 303 | ui_print "$testname" " "; 304 | match=1; 305 | break 2; 306 | fi; 307 | done; 308 | done; 309 | if [ ! "$match" ]; then 310 | abort " " "Unsupported device. Aborting..."; 311 | fi; 312 | } 313 | int2ver() { 314 | if [ "$1" -eq "$1" ] 2>/dev/null; then 315 | echo "$1.0.0"; 316 | elif [ ! "$(echo "$1" | cut -d. -f3)" ]; then 317 | echo "$1.0"; 318 | else 319 | echo "$1"; 320 | fi; 321 | } 322 | do_versioncheck() { 323 | [ "$(file_getprop anykernel.sh supported.versions)" ] || return 1; 324 | local android_ver hi_ver lo_ver parsed_ver supported supported_ver; 325 | ui_print "Checking Android version..."; 326 | supported_ver=$(file_getprop anykernel.sh supported.versions | tr -d '[:space:]'); 327 | android_ver=$(file_getprop /system/build.prop ro.build.version.release); 328 | parsed_ver=$(int2ver $android_ver); 329 | if echo $supported_ver | grep -q '-'; then 330 | lo_ver=$(int2ver "$(echo $supported_ver | cut -d- -f1)"); 331 | hi_ver=$(int2ver "$(echo $supported_ver | cut -d- -f2)"); 332 | if echo -e "$hi_ver\n$lo_ver\n$parsed_ver" | sort -g | grep -n "$parsed_ver" | grep -q '^2:'; then 333 | supported=1; 334 | fi; 335 | else 336 | for ver in $(echo $supported_ver | sed 's;,; ;g'); do 337 | if [ "$(int2ver $ver)" == "$parsed_ver" ]; then 338 | supported=1; 339 | break; 340 | fi; 341 | done; 342 | fi; 343 | if [ "$supported" ]; then 344 | ui_print "$android_ver" " "; 345 | else 346 | abort " " "Unsupported Android version. Aborting..."; 347 | fi; 348 | } 349 | do_levelcheck() { 350 | [ "$(file_getprop anykernel.sh supported.patchlevels)" ] || return 1; 351 | local android_lvl hi_lvl lo_lvl parsed_lvl supported_lvl; 352 | ui_print "Checking Android security patch level..."; 353 | supported_lvl=$(file_getprop anykernel.sh supported.patchlevels | grep -oE '[0-9]{4}-[0-9]{2}|-'); 354 | android_lvl=$(file_getprop /system/build.prop ro.build.version.security_patch); 355 | parsed_lvl=$(echo $android_lvl | grep -oE '[0-9]{4}-[0-9]{2}'); 356 | if echo $supported_lvl | grep -q '^\-'; then 357 | lo_lvl=0000-00; 358 | hi_lvl=$(echo $supported_lvl | awk '{ print $2 }'); 359 | elif echo $supported_lvl | grep -q ' - '; then 360 | lo_lvl=$(echo $supported_lvl | awk '{ print $1 }'); 361 | hi_lvl=$(echo $supported_lvl | awk '{ print $3 }'); 362 | elif echo $supported_lvl | grep -q '\-$'; then 363 | lo_lvl=$(echo $supported_lvl | awk '{ print $1 }'); 364 | hi_lvl=9999-99; 365 | fi; 366 | if echo -e "$hi_lvl\n$lo_lvl\n$parsed_lvl" | sort -g | grep -n "$parsed_lvl" | grep -q '^2:'; then 367 | ui_print "$android_lvl" " "; 368 | else 369 | abort " " "Unsupported Android security patch level. Aborting..."; 370 | fi; 371 | } 372 | do_vendorlevelcheck() { 373 | [ "$(file_getprop anykernel.sh supported.vendorpatchlevels)" ] || return 1; 374 | local vendor_lvl hi_lvl lo_lvl parsed_lvl supported_lvl; 375 | ui_print "Checking Vendor security patch level..."; 376 | supported_lvl=$(file_getprop anykernel.sh supported.vendorpatchlevels | grep -oE '[0-9]{4}-[0-9]{2}|-'); 377 | vendor_lvl=$(file_getprop /vendor/build.prop ro.vendor.build.security_patch); 378 | parsed_lvl=$(echo $vendor_lvl | grep -oE '[0-9]{4}-[0-9]{2}'); 379 | if echo $supported_lvl | grep -q '^\-'; then 380 | lo_lvl=0000-00; 381 | hi_lvl=$(echo $supported_lvl | awk '{ print $2 }'); 382 | elif echo $supported_lvl | grep -q ' - '; then 383 | lo_lvl=$(echo $supported_lvl | awk '{ print $1 }'); 384 | hi_lvl=$(echo $supported_lvl | awk '{ print $3 }'); 385 | elif echo $supported_lvl | grep -q '\-$'; then 386 | lo_lvl=$(echo $supported_lvl | awk '{ print $1 }'); 387 | hi_lvl=9999-99; 388 | fi; 389 | if echo -e "$hi_lvl\n$lo_lvl\n$parsed_lvl" | sort -g | grep -n "$parsed_lvl" | grep -q '^2:'; then 390 | ui_print "$vendor_lvl" " "; 391 | else 392 | abort " " "Unsupported Vendor security patch level. Aborting..."; 393 | fi; 394 | } 395 | dump_moduleinfo() { 396 | cat < $1; 397 | id=ak3-helper 398 | name=AK3 Helper Module 399 | version=$(awk '{ print $3 }' $AKHOME/vertmp) $(grep -oE '#.[0-9]' $AKHOME/vertmp) 400 | versionCode=1 401 | author=AnyKernel3 402 | description=$KERNEL_STRING 403 | EOF 404 | } 405 | dump_moduleremover() { 406 | cat <<'EOF' > $1; 407 | #!/system/bin/sh 408 | MODDIR=${0%/*}; 409 | if [ "$(cat /proc/version)" != "$(cat $MODDIR/version)" ]; then 410 | rm -rf $MODDIR; 411 | exit; 412 | fi; 413 | rm -f $MODDIR/update; 414 | . $MODDIR/post-fs-data.2.sh; 415 | EOF 416 | } 417 | do_modules() { 418 | [ "$(file_getprop anykernel.sh do.modules)" == 1 ] || return 1; 419 | local block modcon moddir modtarget module mount slot umask umountksu; 420 | if [ "$(file_getprop anykernel.sh do.systemless)" == 1 ]; then 421 | if [ ! -d /data/adb -o ! -d /data/data/android ]; then 422 | ui_print " " "Warning: No /data access for kernel helper systemless module!"; 423 | return 1; 424 | fi; 425 | cd $AKHOME/modules; 426 | ui_print " " "Creating kernel helper systemless module..."; 427 | if [ -d /data/adb/magisk -a -f $AKHOME/magisk_patched ] || [ -d /data/data/me.weishu.kernelsu -a -f $AKHOME/kernelsu_patched ]; then 428 | umask=$(umask); 429 | moddir=/data/adb/modules/ak3-helper; 430 | # this may be the initial KernelSU install or first module so setup as needed 431 | if [ -f $AKHOME/kernelsu_patched ]; then 432 | umask 077; 433 | if [ ! -f /data/adb/ksud ]; then 434 | cp -f /data/app/*/me.weishu.kernelsu*/lib/*/libksud.so /data/adb/ksud; 435 | chmod 755 /data/adb/ksud; 436 | fi; 437 | if [ ! -d /data/adb/modules ]; then 438 | mkdir -p /data/adb/modules; 439 | chmod 777 /data/adb/modules; 440 | fi; 441 | [ -d /data/adb/modules_update ] || mkdir -p /data/adb/modules_update; 442 | [ -d /data/adb/ksu ] || mkdir -p /data/adb/ksu; 443 | [ -f /data/adb/ksu/modules.img ] && cp -f /data/adb/ksu/modules.img /data/adb/ksu/modules_update.img; 444 | if [ ! -f /data/adb/ksu/modules_update.img ]; then 445 | /system/bin/make_ext4fs -b 1024 -l 256M /data/adb/ksu/modules_update.img 2>/dev/null \ 446 | || /system/bin/mke2fs -b 1024 -t ext4 /data/adb/ksu/modules_update.img 256M; 447 | fi; 448 | mount -t ext4 -o rw /data/adb/ksu/modules_update.img /data/adb/modules_update && umountksu=1; 449 | touch /data/adb/ksu/update; 450 | umask 022; 451 | rm -rf $moddir; 452 | mkdir -p $moddir; 453 | dump_moduleinfo $moddir/module.prop; 454 | touch $moddir/update; 455 | moddir=/data/adb/modules_update/ak3-helper; 456 | fi; 457 | umask 022; 458 | rm -rf $moddir; 459 | mkdir -p system $moddir; 460 | (mv -f product system; 461 | mv -f vendor system; 462 | mv -f system_ext system; 463 | mv -f post-fs-data.sh post-fs-data.2.sh) 2>/dev/null; 464 | cp -rLf * $moddir; 465 | dump_moduleinfo $moddir/module.prop; 466 | dump_moduleremover $moddir/post-fs-data.sh; 467 | cp -f $AKHOME/vertmp $moddir/version; 468 | touch $moddir/update; 469 | umask $umask; 470 | /system/bin/chcon -hR "u:object_r:system_file:s0" $moddir; 471 | [ "$umountksu" ] && umount /data/adb/modules_update; 472 | else 473 | ui_print "Magisk/KernelSU installation not found. Skipped!"; 474 | fi; 475 | else 476 | cd $AKHOME/modules; 477 | ui_print " " "Pushing modules..."; 478 | if [ -d /dev/block/mapper ]; then 479 | for block in system vendor product; do 480 | for slot in "" _a _b; do 481 | blockdev --setrw /dev/block/mapper/$block$slot 2>/dev/null; 482 | done; 483 | done; 484 | fi; 485 | if [ ! -d /postinstall/tmp ]; then 486 | for mount in /system /vendor /product; do 487 | if is_mounted $mount; then 488 | mount -o rw,remount -t auto $mount && echo "$mount (rw)" >&2; 489 | fi; 490 | done; 491 | fi; 492 | for module in $(find . -name '*.ko'); do 493 | modtarget=$POSTINSTALL$(echo $module | cut -c2-); 494 | if [ ! -e $modtarget ]; then 495 | case $module in 496 | */vendor/*) modcon=vendor;; 497 | */product/*) modcon=product;; 498 | *) modcon=system;; 499 | esac; 500 | fi; 501 | if is_mounted $modtarget; then 502 | mount -o rw,remount -t auto $modtarget && echo "$modtarget (rw)" >&2; 503 | fi; 504 | mkdir -p $(dirname $modtarget); 505 | cp -rLf $module $modtarget; 506 | chown 0:0 $modtarget; 507 | chmod 644 $modtarget; 508 | if [ "$modcon" ]; then 509 | /system/bin/chcon "u:object_r:${modcon}_file:s0" $modtarget; 510 | fi; 511 | if is_mounted $modtarget; then 512 | mount -o ro,remount -t auto $modtarget && echo "$modtarget (ro)" >&2; 513 | fi; 514 | done; 515 | if [ ! -d /postinstall/tmp ]; then 516 | for mount in /system /vendor /product; do 517 | if is_mounted $mount; then 518 | mount -o ro,remount -t auto $mount && echo "$mount (ro)" >&2; 519 | fi; 520 | done; 521 | fi; 522 | fi; 523 | cd $AKHOME; 524 | } 525 | 526 | show_progress 1.34 25; 527 | ui_print " "; 528 | cleanup; 529 | mkdir -p $AKHOME/bin; 530 | cd $AKHOME; 531 | unzip -o "$ZIPFILE"; 532 | if [ $? != 0 -o ! "$(ls tools)" ]; then 533 | abort "Unzip failed. Aborting..."; 534 | fi; 535 | setup_bb; 536 | if [ $? != 0 -o -z "$(ls bin)" ]; then 537 | abort "Busybox setup failed. Aborting..."; 538 | fi; 539 | OLD_PATH="$PATH"; 540 | export PATH="$AKHOME/bin:$AKHOME/tools:$PATH"; 541 | 542 | if [ -f banner ]; then 543 | ui_printfile banner; 544 | ui_print " " " "; 545 | fi; 546 | 547 | KERNEL_STRING="$(file_getprop anykernel.sh kernel.string)"; 548 | ui_print "$KERNEL_STRING"; 549 | if [ -f version ]; then 550 | ui_print " "; 551 | ui_printfile version; 552 | ui_print " "; 553 | fi; 554 | ui_print " " "AnyKernel3 by osm0sis @ xda-developers" " "; 555 | grep -oE 'AK_BASE.*' META-INF/com/google/android/updater-script >&2; 556 | ui_print " "; 557 | 558 | setup_env; 559 | 560 | do_devicecheck; 561 | do_versioncheck; 562 | do_levelcheck; 563 | do_vendorlevelcheck; 564 | 565 | ui_print "Installing..."; 566 | # hack to maintain support for anykernel.sh files that weren't updated correctly along with the AK core 567 | CORE=$(grep -oE 'ak.*core.sh' anykernel.sh); 568 | [ -f tools/$CORE ] || ln -s $AKHOME/tools/ak*-core.sh $AKHOME/tools/$CORE; 569 | ash anykernel.sh; 570 | if [ $? != 0 ]; then 571 | abort; 572 | fi; 573 | 574 | do_modules; 575 | 576 | debugging; 577 | ui_print " "; 578 | restore_env; 579 | 580 | if [ "$(file_getprop anykernel.sh do.cleanup)" == 1 ]; then 581 | cleanup; 582 | fi; 583 | 584 | ui_print " " "Done!"; 585 | export PATH="$OLD_PATH"; 586 | unset OLD_PATH OUTFD ZIPFILE; 587 | -------------------------------------------------------------------------------- /META-INF/com/google/android/updater-script: -------------------------------------------------------------------------------- 1 | #FLASHAFTERUPDATEV2 2 | # 3 | # Dummy file; update-binary is a shell script (DO NOT CHANGE) 4 | # 5 | # 6 | # AK_BASE_VERSION=20231020 7 | -------------------------------------------------------------------------------- /anykernel.sh: -------------------------------------------------------------------------------- 1 | ### AnyKernel3 Ramdisk Mod Script 2 | ## osm0sis @ xda-developers 3 | 4 | ### AnyKernel setup 5 | # global properties 6 | properties() { ' 7 | kernel.string=KernelSU by KernelSU Developers 8 | do.devicecheck=0 9 | do.modules=0 10 | do.systemless=0 11 | do.cleanup=1 12 | do.cleanuponabort=0 13 | device.name1= 14 | device.name2= 15 | device.name3= 16 | device.name4= 17 | device.name5= 18 | supported.versions= 19 | supported.patchlevels= 20 | supported.vendorpatchlevels= 21 | '; } # end properties 22 | 23 | 24 | ### AnyKernel install 25 | ## boot shell variables 26 | block=boot 27 | is_slot_device=auto 28 | ramdisk_compression=auto 29 | patch_vbmeta_flag=auto 30 | no_magisk_check=1 31 | 32 | # import functions/variables and setup patching - see for reference (DO NOT REMOVE) 33 | . tools/ak3-core.sh 34 | 35 | kernel_version=$(cat /proc/version | awk -F '-' '{print $1}' | awk '{print $3}') 36 | case $kernel_version in 37 | 5.1*) ksu_supported=true ;; 38 | 6.1*) ksu_supported=true ;; 39 | 6.6*) ksu_supported=true ;; 40 | *) ksu_supported=false ;; 41 | esac 42 | 43 | ui_print " " " -> ksu_supported: $ksu_supported" 44 | $ksu_supported || abort " -> Non-GKI device, abort." 45 | 46 | # boot install 47 | if [ -L "/dev/block/bootdevice/by-name/init_boot_a" -o -L "/dev/block/by-name/init_boot_a" ]; then 48 | split_boot # for devices with init_boot ramdisk 49 | flash_boot # for devices with init_boot ramdisk 50 | else 51 | dump_boot # use split_boot to skip ramdisk unpack, e.g. for devices with init_boot ramdisk 52 | write_boot # use flash_boot to skip ramdisk repack, e.g. for devices with init_boot ramdisk 53 | fi 54 | ## end boot install 55 | -------------------------------------------------------------------------------- /tools/ak3-core.sh: -------------------------------------------------------------------------------- 1 | ### AnyKernel methods (DO NOT CHANGE) 2 | ## osm0sis @ xda-developers 3 | 4 | [ "$OUTFD" ] || OUTFD=$1; 5 | 6 | # set up working directory variables 7 | [ "$AKHOME" ] && home=$AKHOME; 8 | [ "$home" ] || home=$PWD; 9 | bootimg=$home/boot.img; 10 | bin=$home/tools; 11 | patch=$home/patch; 12 | ramdisk=$home/ramdisk; 13 | split_img=$home/split_img; 14 | 15 | ### output/testing functions: 16 | # ui_print "" [...] 17 | ui_print() { 18 | until [ ! "$1" ]; do 19 | echo "ui_print $1 20 | ui_print" >> /proc/self/fd/$OUTFD; 21 | shift; 22 | done; 23 | } 24 | 25 | # abort ["" [...]] 26 | abort() { 27 | ui_print " " "$@"; 28 | exit 1; 29 | } 30 | 31 | # contains 32 | contains() { 33 | [ "${1#*$2}" != "$1" ]; 34 | } 35 | 36 | # file_getprop 37 | file_getprop() { 38 | grep "^$2=" "$1" | tail -n1 | cut -d= -f2-; 39 | } 40 | ### 41 | 42 | ### file/directory attributes functions: 43 | # set_perm [ ...] 44 | set_perm() { 45 | local uid gid mod; 46 | uid=$1; gid=$2; mod=$3; 47 | shift 3; 48 | chown $uid:$gid "$@" || chown $uid.$gid "$@"; 49 | chmod $mod "$@"; 50 | } 51 | 52 | # set_perm_recursive [ ...] 53 | set_perm_recursive() { 54 | local uid gid dmod fmod; 55 | uid=$1; gid=$2; dmod=$3; fmod=$4; 56 | shift 4; 57 | while [ "$1" ]; do 58 | chown -R $uid:$gid "$1" || chown -R $uid.$gid "$1"; 59 | find "$1" -type d -exec chmod $dmod {} +; 60 | find "$1" -type f -exec chmod $fmod {} +; 61 | shift; 62 | done; 63 | } 64 | ### 65 | 66 | ### dump_boot functions: 67 | # split_boot (dump and split image only) 68 | split_boot() { 69 | local splitfail; 70 | 71 | if [ ! -e "$(echo $block | cut -d\ -f1)" ]; then 72 | abort "Invalid partition. Aborting..."; 73 | fi; 74 | if [ "$(echo $block | grep ' ')" ]; then 75 | block=$(echo $block | cut -d\ -f1); 76 | customdd=$(echo $block | cut -d\ -f2-); 77 | elif [ ! "$customdd" ]; then 78 | local customdd="bs=1048576"; 79 | fi; 80 | if [ -f "$bin/nanddump" ]; then 81 | $bin/nanddump -f $bootimg $block; 82 | else 83 | dd if=$block of=$bootimg $customdd; 84 | fi; 85 | if [ $? != 0 ]; then 86 | abort "Dumping image failed. Aborting..."; 87 | fi; 88 | 89 | mkdir -p $split_img; 90 | cd $split_img; 91 | if [ -f "$bin/unpackelf" ] && $bin/unpackelf -i $bootimg -h -q 2>/dev/null; then 92 | if [ -f "$bin/elftool" ]; then 93 | mkdir elftool_out; 94 | $bin/elftool unpack -i $bootimg -o elftool_out; 95 | fi; 96 | $bin/unpackelf -i $bootimg; 97 | [ $? != 0 ] && splitfail=1; 98 | mv -f boot.img-kernel kernel.gz; 99 | mv -f boot.img-ramdisk ramdisk.cpio.gz; 100 | mv -f boot.img-cmdline cmdline.txt 2>/dev/null; 101 | if [ -f boot.img-dt -a ! -f "$bin/elftool" ]; then 102 | case $(od -ta -An -N4 boot.img-dt | sed -e 's/ del//' -e 's/ //g') in 103 | QCDT|ELF) mv -f boot.img-dt dt;; 104 | *) 105 | gzip -c kernel.gz > kernel.gz-dtb; 106 | cat boot.img-dt >> kernel.gz-dtb; 107 | rm -f boot.img-dt kernel.gz; 108 | ;; 109 | esac; 110 | fi; 111 | elif [ -f "$bin/mboot" ]; then 112 | $bin/mboot -u -f $bootimg; 113 | elif [ -f "$bin/dumpimage" ]; then 114 | 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; 115 | $bin/dumpimage -l boot-trimmed.img > header; 116 | grep "Name:" header | cut -c15- > boot.img-name; 117 | grep "Type:" header | cut -c15- | cut -d\ -f1 > boot.img-arch; 118 | grep "Type:" header | cut -c15- | cut -d\ -f2 > boot.img-os; 119 | grep "Type:" header | cut -c15- | cut -d\ -f3 | cut -d- -f1 > boot.img-type; 120 | grep "Type:" header | cut -d\( -f2 | cut -d\) -f1 | cut -d\ -f1 | cut -d- -f1 > boot.img-comp; 121 | grep "Address:" header | cut -c15- > boot.img-addr; 122 | grep "Point:" header | cut -c15- > boot.img-ep; 123 | $bin/dumpimage -p 0 -o kernel.gz boot-trimmed.img; 124 | [ $? != 0 ] && splitfail=1; 125 | case $(cat boot.img-type) in 126 | Multi) $bin/dumpimage -p 1 -o ramdisk.cpio.gz boot-trimmed.img;; 127 | RAMDisk) mv -f kernel.gz ramdisk.cpio.gz;; 128 | esac; 129 | elif [ -f "$bin/rkcrc" ]; then 130 | dd bs=4096 skip=8 iflag=skip_bytes conv=notrunc if=$bootimg of=ramdisk.cpio.gz; 131 | else 132 | (set -o pipefail; $bin/magiskboot unpack -h $bootimg 2>&1 | tee infotmp >&2); 133 | case $? in 134 | 1) splitfail=1;; 135 | 2) touch chromeos;; 136 | esac; 137 | fi; 138 | 139 | if [ $? != 0 -o "$splitfail" ]; then 140 | abort "Splitting image failed. Aborting..."; 141 | fi; 142 | cd $home; 143 | } 144 | 145 | # unpack_ramdisk (extract ramdisk only) 146 | unpack_ramdisk() { 147 | local comp; 148 | 149 | cd $split_img; 150 | if [ -f ramdisk.cpio.gz ]; then 151 | if [ -f "$bin/mkmtkhdr" ]; then 152 | mv -f ramdisk.cpio.gz ramdisk.cpio.gz-mtk; 153 | dd bs=512 skip=1 conv=notrunc if=ramdisk.cpio.gz-mtk of=ramdisk.cpio.gz; 154 | fi; 155 | mv -f ramdisk.cpio.gz ramdisk.cpio; 156 | fi; 157 | 158 | if [ -f ramdisk.cpio ]; then 159 | comp=$($bin/magiskboot decompress ramdisk.cpio 2>&1 | grep -v 'raw' | sed -n 's;.*\[\(.*\)\];\1;p'); 160 | else 161 | abort "No ramdisk found to unpack. Aborting..."; 162 | fi; 163 | if [ "$comp" ]; then 164 | mv -f ramdisk.cpio ramdisk.cpio.$comp; 165 | $bin/magiskboot decompress ramdisk.cpio.$comp ramdisk.cpio; 166 | if [ $? != 0 ] && $comp --help 2>/dev/null; then 167 | echo "Attempting ramdisk unpack with busybox $comp..." >&2; 168 | $comp -dc ramdisk.cpio.$comp > ramdisk.cpio; 169 | fi; 170 | fi; 171 | 172 | [ -d $ramdisk ] && mv -f $ramdisk $home/rdtmp; 173 | mkdir -p $ramdisk; 174 | chmod 755 $ramdisk; 175 | 176 | cd $ramdisk; 177 | EXTRACT_UNSAFE_SYMLINKS=1 cpio -d -F $split_img/ramdisk.cpio -i; 178 | if [ $? != 0 -o ! "$(ls)" ]; then 179 | abort "Unpacking ramdisk failed. Aborting..."; 180 | fi; 181 | if [ -d "$home/rdtmp" ]; then 182 | cp -af $home/rdtmp/* .; 183 | fi; 184 | } 185 | ### dump_boot (dump and split image, then extract ramdisk) 186 | dump_boot() { 187 | split_boot; 188 | unpack_ramdisk; 189 | } 190 | ### 191 | 192 | ### write_boot functions: 193 | # repack_ramdisk (repack ramdisk only) 194 | repack_ramdisk() { 195 | local comp packfail mtktype; 196 | 197 | cd $home; 198 | if [ "$ramdisk_compression" != "auto" ] && [ "$(grep HEADER_VER $split_img/infotmp | sed -n 's;.*\[\(.*\)\];\1;p')" -gt 3 ]; then 199 | ui_print " " "Warning: Only lz4-l ramdisk compression is allowed with hdr v4+ images. Resetting to auto..."; 200 | ramdisk_compression=auto; 201 | fi; 202 | case $ramdisk_compression in 203 | auto|"") comp=$(ls $split_img/ramdisk.cpio.* 2>/dev/null | grep -v 'mtk' | rev | cut -d. -f1 | rev);; 204 | none|cpio) comp="";; 205 | gz) comp=gzip;; 206 | lzo) comp=lzop;; 207 | bz2) comp=bzip2;; 208 | lz4-l) comp=lz4_legacy;; 209 | *) comp=$ramdisk_compression;; 210 | esac; 211 | 212 | if [ -f "$bin/mkbootfs" ]; then 213 | $bin/mkbootfs $ramdisk > ramdisk-new.cpio; 214 | else 215 | cd $ramdisk; 216 | find . | cpio -H newc -o > $home/ramdisk-new.cpio; 217 | fi; 218 | [ $? != 0 ] && packfail=1; 219 | 220 | cd $home; 221 | if [ ! "$no_magisk_check" ]; then 222 | $bin/magiskboot cpio ramdisk-new.cpio test; 223 | magisk_patched=$?; 224 | fi; 225 | [ $((magisk_patched & 3)) -eq 1 ] && $bin/magiskboot cpio ramdisk-new.cpio "extract .backup/.magisk $split_img/.magisk"; 226 | if [ "$comp" ]; then 227 | $bin/magiskboot compress=$comp ramdisk-new.cpio; 228 | if [ $? != 0 ] && $comp --help 2>/dev/null; then 229 | echo "Attempting ramdisk repack with busybox $comp..." >&2; 230 | $comp -9c ramdisk-new.cpio > ramdisk-new.cpio.$comp; 231 | [ $? != 0 ] && packfail=1; 232 | rm -f ramdisk-new.cpio; 233 | fi; 234 | fi; 235 | if [ "$packfail" ]; then 236 | abort "Repacking ramdisk failed. Aborting..."; 237 | fi; 238 | 239 | if [ -f "$bin/mkmtkhdr" -a -f "$split_img/boot.img-base" ]; then 240 | mtktype=$(od -ta -An -N8 -j8 $split_img/ramdisk.cpio.gz-mtk | sed -e 's/ nul//g' -e 's/ //g' | tr '[:upper:]' '[:lower:]'); 241 | case $mtktype in 242 | rootfs|recovery) $bin/mkmtkhdr --$mtktype ramdisk-new.cpio*;; 243 | esac; 244 | fi; 245 | } 246 | 247 | # flash_boot (build, sign and write image only) 248 | flash_boot() { 249 | local varlist i kernel ramdisk fdt cmdline comp part0 part1 nocompflag signfail pk8 cert avbtype; 250 | 251 | cd $split_img; 252 | if [ -f "$bin/mkimage" ]; then 253 | varlist="name arch os type comp addr ep"; 254 | elif [ -f "$bin/mkbootimg" -a -f "$bin/unpackelf" -a -f boot.img-base ]; then 255 | mv -f cmdline.txt boot.img-cmdline 2>/dev/null; 256 | varlist="cmdline base pagesize kernel_offset ramdisk_offset tags_offset"; 257 | fi; 258 | for i in $varlist; do 259 | if [ -f boot.img-$i ]; then 260 | eval local $i=\"$(cat boot.img-$i)\"; 261 | fi; 262 | done; 263 | 264 | cd $home; 265 | 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 266 | if [ -f $i ]; then 267 | kernel=$home/$i; 268 | break; 269 | fi; 270 | done; 271 | if [ "$kernel" ]; then 272 | if [ -f "$bin/mkmtkhdr" -a -f "$split_img/boot.img-base" ]; then 273 | $bin/mkmtkhdr --kernel $kernel; 274 | kernel=$kernel-mtk; 275 | fi; 276 | elif [ "$(ls $split_img/kernel* 2>/dev/null)" ]; then 277 | kernel=$(ls $split_img/kernel* | grep -v 'kernel_dtb' | tail -n1); 278 | fi; 279 | if [ "$(ls ramdisk-new.cpio* 2>/dev/null)" ]; then 280 | ramdisk=$home/$(ls ramdisk-new.cpio* | tail -n1); 281 | elif [ -f "$bin/mkmtkhdr" -a -f "$split_img/boot.img-base" ]; then 282 | ramdisk=$split_img/ramdisk.cpio.gz-mtk; 283 | else 284 | ramdisk=$(ls $split_img/ramdisk.cpio* 2>/dev/null | tail -n1); 285 | fi; 286 | for fdt in dt recovery_dtbo dtb; do 287 | for i in $home/$fdt $home/$fdt.img $split_img/$fdt; do 288 | if [ -f $i ]; then 289 | eval local $fdt=$i; 290 | break; 291 | fi; 292 | done; 293 | done; 294 | 295 | cd $split_img; 296 | if [ -f "$bin/mkimage" ]; then 297 | [ "$comp" == "uncompressed" ] && comp=none; 298 | part0=$kernel; 299 | case $type in 300 | Multi) part1=":$ramdisk";; 301 | RAMDisk) part0=$ramdisk;; 302 | esac; 303 | $bin/mkimage -A $arch -O $os -T $type -C $comp -a $addr -e $ep -n "$name" -d $part0$part1 $home/boot-new.img; 304 | elif [ -f "$bin/elftool" ]; then 305 | [ "$dt" ] && dt="$dt,rpm"; 306 | [ -f cmdline.txt ] && cmdline="cmdline.txt@cmdline"; 307 | $bin/elftool pack -o $home/boot-new.img header=elftool_out/header $kernel $ramdisk,ramdisk $dt $cmdline; 308 | elif [ -f "$bin/mboot" ]; then 309 | cp -f $kernel kernel; 310 | cp -f $ramdisk ramdisk.cpio.gz; 311 | $bin/mboot -d $split_img -f $home/boot-new.img; 312 | elif [ -f "$bin/rkcrc" ]; then 313 | $bin/rkcrc -k $ramdisk $home/boot-new.img; 314 | elif [ -f "$bin/mkbootimg" -a -f "$bin/unpackelf" -a -f boot.img-base ]; then 315 | [ "$dt" ] && dt="--dt $dt"; 316 | $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; 317 | else 318 | [ "$kernel" ] && cp -f $kernel kernel; 319 | [ "$ramdisk" ] && cp -f $ramdisk ramdisk.cpio; 320 | [ "$dt" -a -f extra ] && cp -f $dt extra; 321 | for i in dtb recovery_dtbo; do 322 | [ "$(eval echo \$$i)" -a -f $i ] && cp -f $(eval echo \$$i) $i; 323 | done; 324 | case $kernel in 325 | *Image*) 326 | if [ ! "$magisk_patched" -a ! "$no_magisk_check" ]; then 327 | $bin/magiskboot cpio ramdisk.cpio test; 328 | magisk_patched=$?; 329 | fi; 330 | if [ $((magisk_patched & 3)) -eq 1 ]; then 331 | ui_print " " "Magisk detected! Patching kernel so reflashing Magisk is not necessary..."; 332 | comp=$($bin/magiskboot decompress kernel 2>&1 | grep -vE 'raw|zimage' | sed -n 's;.*\[\(.*\)\];\1;p'); 333 | ($bin/magiskboot split $kernel || $bin/magiskboot decompress $kernel kernel) 2>/dev/null; 334 | if [ $? != 0 -a "$comp" ] && $comp --help 2>/dev/null; then 335 | echo "Attempting kernel unpack with busybox $comp..." >&2; 336 | $comp -dc $kernel > kernel; 337 | fi; 338 | # legacy SAR kernel string skip_initramfs -> want_initramfs 339 | $bin/magiskboot hexpatch kernel 736B69705F696E697472616D6673 77616E745F696E697472616D6673; 340 | if [ "$(file_getprop $home/anykernel.sh do.modules)" == 1 ] && [ "$(file_getprop $home/anykernel.sh do.systemless)" == 1 ]; then 341 | strings kernel 2>/dev/null | grep -E -m1 'Linux version.*#' > $home/vertmp; 342 | fi; 343 | if [ "$comp" ]; then 344 | $bin/magiskboot compress=$comp kernel kernel.$comp; 345 | if [ $? != 0 ] && $comp --help 2>/dev/null; then 346 | echo "Attempting kernel repack with busybox $comp..." >&2; 347 | $comp -9c kernel > kernel.$comp; 348 | fi; 349 | mv -f kernel.$comp kernel; 350 | fi; 351 | [ ! -f .magisk ] && $bin/magiskboot cpio ramdisk.cpio "extract .backup/.magisk .magisk"; 352 | export $(cat .magisk); 353 | for fdt in dtb extra kernel_dtb recovery_dtbo; do 354 | [ -f $fdt ] && $bin/magiskboot dtb $fdt patch; # remove dtb verity/avb 355 | done; 356 | elif [ -d /data/data/me.weishu.kernelsu ] && [ "$(file_getprop $home/anykernel.sh do.modules)" == 1 ] && [ "$(file_getprop $home/anykernel.sh do.systemless)" == 1 ]; then 357 | ui_print " " "KernelSU detected! Setting up for kernel helper module..."; 358 | comp=$($bin/magiskboot decompress kernel 2>&1 | grep -vE 'raw|zimage' | sed -n 's;.*\[\(.*\)\];\1;p'); 359 | ($bin/magiskboot split $kernel || $bin/magiskboot decompress $kernel kernel) 2>/dev/null; 360 | if [ $? != 0 -a "$comp" ] && $comp --help 2>/dev/null; then 361 | echo "Attempting kernel unpack with busybox $comp..." >&2; 362 | $comp -dc $kernel > kernel; 363 | fi; 364 | strings kernel > stringstmp 2>/dev/null; 365 | if grep -q -E '^/data/adb/ksud$' stringstmp; then 366 | touch $home/kernelsu_patched; 367 | grep -E -m1 'Linux version.*#' stringstmp > $home/vertmp; 368 | [ -d $ramdisk/overlay.d ] && ui_print " " "Warning: overlay.d detected in ramdisk but not currently supported by KernelSU!"; 369 | else 370 | ui_print " " "Warning: No KernelSU support detected in kernel!"; 371 | fi; 372 | rm -f stringstmp; 373 | if [ "$comp" ]; then 374 | $bin/magiskboot compress=$comp kernel kernel.$comp; 375 | if [ $? != 0 ] && $comp --help 2>/dev/null; then 376 | echo "Attempting kernel repack with busybox $comp..." >&2; 377 | $comp -9c kernel > kernel.$comp; 378 | fi; 379 | mv -f kernel.$comp kernel; 380 | fi; 381 | else 382 | case $kernel in 383 | *-dtb) rm -f kernel_dtb;; 384 | esac; 385 | fi; 386 | unset magisk_patched KEEPVERITY KEEPFORCEENCRYPT RECOVERYMODE PREINITDEVICE SHA1 RANDOMSEED; # leave PATCHVBMETAFLAG set for repack 387 | ;; 388 | esac; 389 | case $ramdisk_compression in 390 | none|cpio) nocompflag="-n";; 391 | esac; 392 | case $patch_vbmeta_flag in 393 | auto|"") [ "$PATCHVBMETAFLAG" ] || export PATCHVBMETAFLAG=false;; 394 | 1) export PATCHVBMETAFLAG=true;; 395 | *) export PATCHVBMETAFLAG=false;; 396 | esac; 397 | $bin/magiskboot repack $nocompflag $bootimg $home/boot-new.img; 398 | fi; 399 | if [ $? != 0 ]; then 400 | abort "Repacking image failed. Aborting..."; 401 | fi; 402 | [ "$PATCHVBMETAFLAG" ] && unset PATCHVBMETAFLAG; 403 | [ -f .magisk ] && touch $home/magisk_patched; 404 | 405 | cd $home; 406 | if [ -f "$bin/futility" -a -d "$bin/chromeos" ]; then 407 | if [ -f "$split_img/chromeos" ]; then 408 | echo "Signing with CHROMEOS..." >&2; 409 | $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; 410 | fi; 411 | [ $? != 0 ] && signfail=1; 412 | fi; 413 | if [ -d "$bin/avb" ]; then 414 | pk8=$(ls $bin/avb/*.pk8); 415 | cert=$(ls $bin/avb/*.x509.*); 416 | case $block in 417 | *recovery*|*RECOVERY*|*SOS*) avbtype=recovery;; 418 | *) avbtype=boot;; 419 | esac; 420 | if [ -f "$bin/boot_signer-dexed.jar" ]; then 421 | if [ -f /system/bin/dalvikvm ] && [ "$(/system/bin/dalvikvm -Xnoimage-dex2oat -cp $bin/boot_signer-dexed.jar com.android.verity.BootSignature -verify boot.img 2>&1 | grep VALID)" ]; then 422 | echo "Signing with AVBv1 /$avbtype..." >&2; 423 | /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; 424 | fi; 425 | else 426 | if $bin/magiskboot verify boot.img; then 427 | echo "Signing with AVBv1 /$avbtype..." >&2; 428 | $bin/magiskboot sign /$avbtype boot-new.img $cert $pk8; 429 | fi; 430 | fi; 431 | fi; 432 | if [ $? != 0 -o "$signfail" ]; then 433 | abort "Signing image failed. Aborting..."; 434 | fi; 435 | mv -f boot-new-signed.img boot-new.img 2>/dev/null; 436 | 437 | if [ ! -f boot-new.img ]; then 438 | abort "No repacked image found to flash. Aborting..."; 439 | elif [ "$(wc -c < boot-new.img)" -gt "$(wc -c < boot.img)" ]; then 440 | abort "New image larger than target partition. Aborting..."; 441 | fi; 442 | blockdev --setrw $block 2>/dev/null; 443 | if [ -f "$bin/flash_erase" -a -f "$bin/nandwrite" ]; then 444 | $bin/flash_erase $block 0 0; 445 | $bin/nandwrite -p $block boot-new.img; 446 | elif [ "$customdd" ]; then 447 | dd if=/dev/zero of=$block $customdd 2>/dev/null; 448 | dd if=boot-new.img of=$block $customdd; 449 | else 450 | cat boot-new.img /dev/zero > $block 2>/dev/null || true; 451 | fi; 452 | if [ $? != 0 ]; then 453 | abort "Flashing image failed. Aborting..."; 454 | fi; 455 | } 456 | 457 | # flash_generic 458 | flash_generic() { 459 | local avb avbblock avbpath file flags img imgblock imgsz isro isunmounted path; 460 | 461 | cd $home; 462 | for file in $1 $1.img; do 463 | if [ -f $file ]; then 464 | img=$file; 465 | break; 466 | fi; 467 | done; 468 | 469 | if [ "$img" -a ! -f ${1}_flashed ]; then 470 | for path in /dev/block/mapper /dev/block/by-name /dev/block/bootdevice/by-name; do 471 | for file in $1 $1$slot; do 472 | if [ -e $path/$file ]; then 473 | imgblock=$path/$file; 474 | break 2; 475 | fi; 476 | done; 477 | done; 478 | if [ ! "$imgblock" ]; then 479 | abort "$1 partition could not be found. Aborting..."; 480 | fi; 481 | if [ ! "$no_block_display" ]; then 482 | ui_print " " "$imgblock"; 483 | fi; 484 | if [ "$path" == "/dev/block/mapper" ]; then 485 | avb=$($bin/httools_static avb $1); 486 | [ $? == 0 ] || abort "Failed to parse fstab entry for $1. Aborting..."; 487 | if [ "$avb" ]; then 488 | flags=$($bin/httools_static disable-flags); 489 | [ $? == 0 ] || abort "Failed to parse top-level vbmeta. Aborting..."; 490 | if [ "$flags" == "enabled" ]; then 491 | ui_print " " "dm-verity detected! Patching $avb..."; 492 | for avbpath in /dev/block/mapper /dev/block/by-name /dev/block/bootdevice/by-name; do 493 | for file in $avb $avb$slot; do 494 | if [ -e $avbpath/$file ]; then 495 | avbblock=$avbpath/$file; 496 | break 2; 497 | fi; 498 | done; 499 | done; 500 | cd $bin; 501 | $bin/httools_static patch $1 $home/$img $avbblock || abort "Failed to patch $1 on $avb. Aborting..."; 502 | cd $home; 503 | fi 504 | fi 505 | imgsz=$(wc -c < $img); 506 | if [ "$imgsz" != "$(wc -c < $imgblock)" ]; then 507 | if [ -d /postinstall/tmp -a "$slot_select" == "inactive" ]; then 508 | echo "Resizing $1$slot snapshot..." >&2; 509 | $bin/snapshotupdater_static update $1 $imgsz || abort "Resizing $1$slot snapshot failed. Aborting..."; 510 | else 511 | echo "Removing any existing $1_ak3..." >&2; 512 | $bin/lptools_static remove $1_ak3; 513 | echo "Clearing any merged cow partitions..." >&2; 514 | $bin/lptools_static clear-cow; 515 | echo "Attempting to create $1_ak3..." >&2; 516 | if $bin/lptools_static create $1_ak3 $imgsz; then 517 | echo "Replacing $1$slot with $1_ak3..." >&2; 518 | $bin/lptools_static unmap $1_ak3 || abort "Unmapping $1_ak3 failed. Aborting..."; 519 | $bin/lptools_static map $1_ak3 || abort "Mapping $1_ak3 failed. Aborting..."; 520 | $bin/lptools_static replace $1_ak3 $1$slot || abort "Replacing $1$slot failed. Aborting..."; 521 | imgblock=/dev/block/mapper/$1_ak3; 522 | ui_print " " "Warning: $1$slot replaced in super. Reboot before further logical partition operations."; 523 | else 524 | echo "Creating $1_ak3 failed. Attempting to resize $1$slot..." >&2; 525 | $bin/httools_static umount $1 || abort "Unmounting $1 failed. Aborting..."; 526 | if [ -e $path/$1-verity ]; then 527 | $bin/lptools_static unmap $1-verity || abort "Unmapping $1-verity failed. Aborting..."; 528 | fi 529 | $bin/lptools_static unmap $1$slot || abort "Unmapping $1$slot failed. Aborting..."; 530 | $bin/lptools_static resize $1$slot $imgsz || abort "Resizing $1$slot failed. Aborting..."; 531 | $bin/lptools_static map $1$slot || abort "Mapping $1$slot failed. Aborting..."; 532 | isunmounted=1; 533 | fi 534 | fi 535 | fi 536 | elif [ "$(wc -c < $img)" -gt "$(wc -c < $imgblock)" ]; then 537 | abort "New $1 image larger than $1 partition. Aborting..."; 538 | fi; 539 | isro=$(blockdev --getro $imgblock 2>/dev/null); 540 | blockdev --setrw $imgblock 2>/dev/null; 541 | if [ -f "$bin/flash_erase" -a -f "$bin/nandwrite" ]; then 542 | $bin/flash_erase $imgblock 0 0; 543 | $bin/nandwrite -p $imgblock $img; 544 | elif [ "$customdd" ]; then 545 | dd if=/dev/zero of=$imgblock 2>/dev/null; 546 | dd if=$img of=$imgblock; 547 | else 548 | cat $img /dev/zero > $imgblock 2>/dev/null || true; 549 | fi; 550 | if [ $? != 0 ]; then 551 | abort "Flashing $1 failed. Aborting..."; 552 | fi; 553 | if [ "$isro" != 0 ]; then 554 | blockdev --setro $imgblock 2>/dev/null; 555 | fi; 556 | if [ "$isunmounted" -a "$path" == "/dev/block/mapper" ]; then 557 | $bin/httools_static mount $1 || abort "Mounting $1 failed. Aborting..."; 558 | fi 559 | touch ${1}_flashed; 560 | fi; 561 | } 562 | 563 | # flash_dtbo (backwards compatibility for flash_generic) 564 | flash_dtbo() { flash_generic dtbo; } 565 | 566 | ### write_boot (repack ramdisk then build, sign and write image, vendor_dlkm and dtbo) 567 | write_boot() { 568 | repack_ramdisk; 569 | flash_boot; 570 | flash_generic vendor_boot; # temporary until hdr v4 can be unpacked/repacked fully by magiskboot 571 | flash_generic vendor_kernel_boot; # temporary until hdr v4 can be unpacked/repacked fully by magiskboot 572 | flash_generic vendor_dlkm; 573 | flash_generic system_dlkm; 574 | flash_generic dtbo; 575 | } 576 | ### 577 | 578 | ### file editing functions: 579 | # backup_file 580 | backup_file() { [ ! -f $1~ ] && cp -fp $1 $1~; } 581 | 582 | # restore_file 583 | restore_file() { [ -f $1~ ] && cp -fp $1~ $1; rm -f $1~; } 584 | 585 | # replace_string 586 | replace_string() { 587 | [ "$5" == "global" ] && local scope=g; 588 | if ! grep -q "$2" $1; then 589 | sed -i "s;${3};${4};${scope}" $1; 590 | fi; 591 | } 592 | 593 | # replace_section 594 | replace_section() { 595 | local begin endstr last end; 596 | begin=$(grep -n -m1 "$2" $1 | cut -d: -f1); 597 | if [ "$begin" ]; then 598 | if [ "$3" == " " -o ! "$3" ]; then 599 | endstr='^[[:space:]]*$'; 600 | last=$(wc -l $1 | cut -d\ -f1); 601 | else 602 | endstr="$3"; 603 | fi; 604 | for end in $(grep -n "$endstr" $1 | cut -d: -f1) $last; do 605 | if [ "$end" ] && [ "$begin" -lt "$end" ]; then 606 | sed -i "${begin},${end}d" $1; 607 | [ "$end" == "$last" ] && echo >> $1; 608 | sed -i "${begin}s;^;${4}\n;" $1; 609 | break; 610 | fi; 611 | done; 612 | fi; 613 | } 614 | 615 | # remove_section 616 | remove_section() { 617 | local begin endstr last end; 618 | begin=$(grep -n -m1 "$2" $1 | cut -d: -f1); 619 | if [ "$begin" ]; then 620 | if [ "$3" == " " -o ! "$3" ]; then 621 | endstr='^[[:space:]]*$'; 622 | last=$(wc -l $1 | cut -d\ -f1); 623 | else 624 | endstr="$3"; 625 | fi; 626 | for end in $(grep -n "$endstr" $1 | cut -d: -f1) $last; do 627 | if [ "$end" ] && [ "$begin" -lt "$end" ]; then 628 | sed -i "${begin},${end}d" $1; 629 | break; 630 | fi; 631 | done; 632 | fi; 633 | } 634 | 635 | # insert_line 636 | insert_line() { 637 | local offset line; 638 | if ! grep -q "$2" $1; then 639 | case $3 in 640 | before) offset=0;; 641 | after) offset=1;; 642 | esac; 643 | line=$((`grep -n -m1 "$4" $1 | cut -d: -f1` + offset)); 644 | if [ -f $1 -a "$line" ] && [ "$(wc -l $1 | cut -d\ -f1)" -lt "$line" ]; then 645 | echo "$5" >> $1; 646 | else 647 | sed -i "${line}s;^;${5}\n;" $1; 648 | fi; 649 | fi; 650 | } 651 | 652 | # replace_line 653 | replace_line() { 654 | local lines line; 655 | if grep -q "$2" $1; then 656 | lines=$(grep -n "$2" $1 | cut -d: -f1 | sort -nr); 657 | [ "$4" == "global" ] || lines=$(echo "$lines" | tail -n1); 658 | for line in $lines; do 659 | sed -i "${line}s;.*;${3};" $1; 660 | done; 661 | fi; 662 | } 663 | 664 | # remove_line 665 | remove_line() { 666 | local lines line; 667 | if grep -q "$2" $1; then 668 | lines=$(grep -n "$2" $1 | cut -d: -f1 | sort -nr); 669 | [ "$3" == "global" ] || lines=$(echo "$lines" | tail -n1); 670 | for line in $lines; do 671 | sed -i "${line}d" $1; 672 | done; 673 | fi; 674 | } 675 | 676 | # prepend_file 677 | prepend_file() { 678 | if ! grep -q "$2" $1; then 679 | echo "$(cat $patch/$3 $1)" > $1; 680 | fi; 681 | } 682 | 683 | # insert_file 684 | insert_file() { 685 | local offset line; 686 | if ! grep -q "$2" $1; then 687 | case $3 in 688 | before) offset=0;; 689 | after) offset=1;; 690 | esac; 691 | line=$((`grep -n -m1 "$4" $1 | cut -d: -f1` + offset)); 692 | sed -i "${line}s;^;\n;" $1; 693 | sed -i "$((line - 1))r $patch/$5" $1; 694 | fi; 695 | } 696 | 697 | # append_file 698 | append_file() { 699 | if ! grep -q "$2" $1; then 700 | echo -ne "\n" >> $1; 701 | cat $patch/$3 >> $1; 702 | echo -ne "\n" >> $1; 703 | fi; 704 | } 705 | 706 | # replace_file 707 | replace_file() { 708 | cp -pf $patch/$3 $1; 709 | chmod $2 $1; 710 | } 711 | 712 | # patch_fstab block|mount|fstype|options|flags 713 | patch_fstab() { 714 | local entry part newpart newentry; 715 | entry=$(grep "$2[[:space:]]" $1 | grep "$3"); 716 | if [ ! "$(echo "$entry" | grep "$6")" -o "$6" == " " -o ! "$6" ]; then 717 | case $4 in 718 | block) part=$(echo "$entry" | awk '{ print $1 }');; 719 | mount) part=$(echo "$entry" | awk '{ print $2 }');; 720 | fstype) part=$(echo "$entry" | awk '{ print $3 }');; 721 | options) part=$(echo "$entry" | awk '{ print $4 }');; 722 | flags) part=$(echo "$entry" | awk '{ print $5 }');; 723 | esac; 724 | newpart=$(echo "$part" | sed -e "s;${5};${6};" -e "s; ;;g" -e 's;,\{2,\};,;g' -e 's;,*$;;g' -e 's;^,;;g'); 725 | newentry=$(echo "$entry" | sed "s;${part};${newpart};"); 726 | sed -i "s;${entry};${newentry};" $1; 727 | fi; 728 | } 729 | 730 | # patch_cmdline 731 | patch_cmdline() { 732 | local cmdfile cmdtmp match; 733 | if [ -f "$split_img/cmdline.txt" ]; then 734 | cmdfile=$split_img/cmdline.txt; 735 | else 736 | cmdfile=$home/cmdtmp; 737 | grep "^cmdline=" $split_img/header | cut -d= -f2- > $cmdfile; 738 | fi; 739 | if ! grep -q "$1" $cmdfile; then 740 | cmdtmp=$(cat $cmdfile); 741 | echo "$cmdtmp $2" > $cmdfile; 742 | sed -i -e 's;^[ \t]*;;' -e 's; *; ;g' -e 's;[ \t]*$;;' $cmdfile; 743 | else 744 | match=$(grep -o "$1.*$" $cmdfile | cut -d\ -f1); 745 | sed -i -e "s;${match};${2};" -e 's;^[ \t]*;;' -e 's; *; ;g' -e 's;[ \t]*$;;' $cmdfile; 746 | fi; 747 | if [ -f "$home/cmdtmp" ]; then 748 | sed -i "s|^cmdline=.*|cmdline=$(cat $cmdfile)|" $split_img/header; 749 | rm -f $cmdfile; 750 | fi; 751 | } 752 | 753 | # patch_prop 754 | patch_prop() { 755 | if ! grep -q "^$2=" $1; then 756 | echo -ne "\n$2=$3\n" >> $1; 757 | else 758 | local line=$(grep -n -m1 "^$2=" $1 | cut -d: -f1); 759 | sed -i "${line}s;.*;${2}=${3};" $1; 760 | fi; 761 | } 762 | 763 | # patch_ueventd 764 | patch_ueventd() { 765 | local file dev perm user group newentry line; 766 | file=$1; dev=$2; perm=$3; user=$4; 767 | shift 4; 768 | group="$@"; 769 | newentry=$(printf "%-23s %-4s %-8s %s\n" "$dev" "$perm" "$user" "$group"); 770 | line=$(grep -n -m1 "$dev" $file | cut -d: -f1); 771 | if [ "$line" ]; then 772 | sed -i "${line}s;.*;${newentry};" $file; 773 | else 774 | echo -ne "\n$newentry\n" >> $file; 775 | fi; 776 | } 777 | ### 778 | 779 | ### configuration/setup functions: 780 | # reset_ak [keep] 781 | reset_ak() { 782 | local current i; 783 | 784 | current=$(dirname $home/*-files/current); 785 | if [ -d "$current" ]; then 786 | for i in $bootimg $home/boot-new.img; do 787 | [ -e $i ] && cp -af $i $current; 788 | done; 789 | for i in $current/*; do 790 | [ -f $i ] && rm -f $home/$(basename $i); 791 | done; 792 | fi; 793 | [ -d $split_img ] && rm -rf $ramdisk; 794 | rm -rf $bootimg $split_img $home/*-new* $home/*-files/current; 795 | 796 | if [ "$1" == "keep" ]; then 797 | [ -d $home/rdtmp ] && mv -f $home/rdtmp $ramdisk; 798 | else 799 | rm -rf $patch $home/rdtmp; 800 | fi; 801 | if [ ! "$no_block_display" ]; then 802 | ui_print " "; 803 | fi; 804 | setup_ak; 805 | } 806 | 807 | # setup_ak 808 | setup_ak() { 809 | local blockfiles plistboot plistinit plistreco parttype name part mtdmount mtdpart mtdname target; 810 | 811 | # slot detection enabled by is_slot_device=1 or auto (from anykernel.sh) 812 | case $is_slot_device in 813 | 1|auto) 814 | slot=$(getprop ro.boot.slot_suffix 2>/dev/null); 815 | [ "$slot" ] || slot=$(grep -o 'androidboot.slot_suffix=.*$' /proc/cmdline | cut -d\ -f1 | cut -d= -f2); 816 | if [ ! "$slot" ]; then 817 | slot=$(getprop ro.boot.slot 2>/dev/null); 818 | [ "$slot" ] || slot=$(grep -o 'androidboot.slot=.*$' /proc/cmdline | cut -d\ -f1 | cut -d= -f2); 819 | [ "$slot" ] && slot=_$slot; 820 | fi; 821 | [ "$slot" == "normal" ] && unset slot; 822 | if [ "$slot" ]; then 823 | if [ -d /postinstall/tmp -a ! "$slot_select" ]; then 824 | slot_select=inactive; 825 | fi; 826 | case $slot_select in 827 | inactive) 828 | case $slot in 829 | _a) slot=_b;; 830 | _b) slot=_a;; 831 | esac; 832 | ;; 833 | esac; 834 | fi; 835 | if [ ! "$slot" -a "$is_slot_device" == 1 ]; then 836 | abort "Unable to determine active slot. Aborting..."; 837 | fi; 838 | ;; 839 | esac; 840 | 841 | # clean up any template placeholder files 842 | cd $home; 843 | rm -f modules/system/lib/modules/placeholder patch/placeholder ramdisk/placeholder; 844 | rmdir -p modules patch ramdisk 2>/dev/null; 845 | 846 | # automate simple multi-partition setup for hdr_v4 boot + init_boot + vendor_kernel_boot (for dtb only until magiskboot supports hdr v4 vendor_ramdisk unpack/repack) 847 | if [ -e "/dev/block/bootdevice/by-name/init_boot$slot" -a ! -f init_v4_setup ] && [ -f dtb -o -d vendor_ramdisk -o -d vendor_patch ]; then 848 | echo "Setting up for simple automatic init_boot flashing..." >&2; 849 | (mkdir boot-files; 850 | mv -f Image* boot-files; 851 | mkdir init_boot-files; 852 | mv -f ramdisk patch init_boot-files; 853 | mkdir vendor_kernel_boot-files; 854 | mv -f dtb vendor_kernel_boot-files; 855 | mv -f vendor_ramdisk vendor_kernel_boot-files/ramdisk; 856 | mv -f vendor_patch vendor_kernel_boot-files/patch) 2>/dev/null; 857 | touch init_v4_setup; 858 | # automate simple multi-partition setup for hdr_v3+ boot + vendor_boot with dtb/dlkm (for v3 only until magiskboot supports hdr v4 vendor_ramdisk unpack/repack) 859 | elif [ -e "/dev/block/bootdevice/by-name/vendor_boot$slot" -a ! -f vendor_v3_setup ] && [ -f dtb -o -d vendor_ramdisk -o -d vendor_patch ]; then 860 | echo "Setting up for simple automatic vendor_boot flashing..." >&2; 861 | (mkdir boot-files; 862 | mv -f Image* ramdisk patch boot-files; 863 | mkdir vendor_boot-files; 864 | mv -f dtb vendor_boot-files; 865 | mv -f vendor_ramdisk vendor_boot-files/ramdisk; 866 | mv -f vendor_patch vendor_boot-files/patch) 2>/dev/null; 867 | touch vendor_v3_setup; 868 | fi; 869 | 870 | # target block partition detection enabled by block= or auto (from anykernel.sh) 871 | case $block in 872 | /dev/*) 873 | if [ "$slot" ] && [ -e "$block$slot" ]; then 874 | target=$block$slot; 875 | elif [ -e "$block" ]; then 876 | target=$block; 877 | fi; 878 | ;; 879 | *) 880 | # maintain brief lists of historic matching partition type names for boot, recovery and init_boot/ramdisk 881 | plistboot="boot BOOT LNX android_boot bootimg KERN-A kernel KERNEL"; 882 | plistreco="recovery RECOVERY SOS android_recovery recovery_ramdisk"; 883 | plistinit="init_boot ramdisk"; 884 | case $block in 885 | auto) parttype="$plistinit $plistboot";; 886 | boot|kernel) parttype=$plistboot;; 887 | recovery|recovery_ramdisk) parttype=$plistreco;; 888 | init_boot|ramdisk) parttype=$plistinit;; 889 | *) parttype=$block;; 890 | esac; 891 | for name in $parttype; do 892 | for part in $name$slot $name; do 893 | if [ "$(grep -w "$part" /proc/mtd 2>/dev/null)" ]; then 894 | mtdmount=$(grep -w "$part" /proc/mtd); 895 | mtdpart=$(echo $mtdmount | cut -d\" -f2); 896 | if [ "$mtdpart" == "$part" ]; then 897 | mtdname=$(echo $mtdmount | cut -d: -f1); 898 | else 899 | abort "Unable to determine mtd $block partition. Aborting..."; 900 | fi; 901 | [ -e /dev/mtd/$mtdname ] && target=/dev/mtd/$mtdname; 902 | elif [ -e /dev/block/by-name/$part ]; then 903 | target=/dev/block/by-name/$part; 904 | elif [ -e /dev/block/bootdevice/by-name/$part ]; then 905 | target=/dev/block/bootdevice/by-name/$part; 906 | elif [ -e /dev/block/platform/*/by-name/$part ]; then 907 | target=/dev/block/platform/*/by-name/$part; 908 | elif [ -e /dev/block/platform/*/*/by-name/$part ]; then 909 | target=/dev/block/platform/*/*/by-name/$part; 910 | elif [ -e /dev/$part ]; then 911 | target=/dev/$part; 912 | fi; 913 | [ "$target" ] && break 2; 914 | done; 915 | done; 916 | ;; 917 | esac; 918 | if [ "$target" ]; then 919 | block=$(ls $target 2>/dev/null); 920 | else 921 | abort "Unable to determine $block partition. Aborting..."; 922 | fi; 923 | if [ ! "$no_block_display" ]; then 924 | ui_print "$block"; 925 | fi; 926 | 927 | # allow multi-partition ramdisk modifying configurations (using reset_ak) 928 | name=$(basename $block | sed -e 's/_a$//' -e 's/_b$//'); 929 | if [ "$block" ] && [ ! -d "$ramdisk" -a ! -d "$patch" ]; then 930 | blockfiles=$home/$name-files; 931 | if [ "$(ls $blockfiles 2>/dev/null)" ]; then 932 | cp -af $blockfiles/* $home; 933 | else 934 | mkdir $blockfiles; 935 | fi; 936 | touch $blockfiles/current; 937 | fi; 938 | 939 | # run attributes function for current block if it exists 940 | type attributes >/dev/null 2>&1 && attributes; # backwards compatibility 941 | type ${name}_attributes >/dev/null 2>&1 && ${name}_attributes; 942 | } 943 | ### 944 | 945 | ### end methods 946 | 947 | setup_ak; 948 | -------------------------------------------------------------------------------- /tools/busybox: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kernel-SU/AnyKernel3/ac6360f6fc1895cbd218246f12a771f63d0e5e4f/tools/busybox -------------------------------------------------------------------------------- /tools/fec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kernel-SU/AnyKernel3/ac6360f6fc1895cbd218246f12a771f63d0e5e4f/tools/fec -------------------------------------------------------------------------------- /tools/httools_static: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kernel-SU/AnyKernel3/ac6360f6fc1895cbd218246f12a771f63d0e5e4f/tools/httools_static -------------------------------------------------------------------------------- /tools/lptools_static: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kernel-SU/AnyKernel3/ac6360f6fc1895cbd218246f12a771f63d0e5e4f/tools/lptools_static -------------------------------------------------------------------------------- /tools/magiskboot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kernel-SU/AnyKernel3/ac6360f6fc1895cbd218246f12a771f63d0e5e4f/tools/magiskboot -------------------------------------------------------------------------------- /tools/magiskpolicy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kernel-SU/AnyKernel3/ac6360f6fc1895cbd218246f12a771f63d0e5e4f/tools/magiskpolicy -------------------------------------------------------------------------------- /tools/snapshotupdater_static: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kernel-SU/AnyKernel3/ac6360f6fc1895cbd218246f12a771f63d0e5e4f/tools/snapshotupdater_static --------------------------------------------------------------------------------