Skip to content

Commit

Permalink
Magisk Template v3
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Mar 20, 2017
1 parent 6fd89d8 commit f9e2e9b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
92 changes: 47 additions & 45 deletions META-INF/com/google/android/update-binary
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/sbin/sh

# Detect whether in boot mode
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false

# Default permissions
umask 022

##########################################################################################
# Functions
##########################################################################################

ui_print() {
if ($BOOTMODE); then
if $BOOTMODE; then
echo "$1"
else
echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD
Expand All @@ -20,7 +26,7 @@ grep_prop() {
if [ -z "$FILES" ]; then
FILES='/system/build.prop'
fi
cat $FILES 2>/dev/null | sed -n $REGEX | head -n 1
cat $FILES 2>/dev/null | sed -n "$REGEX" | head -n 1
}

is_mounted() {
Expand All @@ -34,8 +40,9 @@ is_mounted() {

mount_image() {
if [ ! -d "$2" ]; then
mount -o rw,remount rootfs /
mkdir -p $2 2>/dev/null
chmod 755 $2
($BOOTMODE) && mount -o ro,remount rootfs /
[ ! -d "$2" ] && return 1
fi
if (! is_mounted $2); then
Expand All @@ -44,7 +51,7 @@ mount_image() {
if (! is_mounted $2); then
LOOPDEVICE=/dev/block/loop$LOOP
if [ ! -f "$LOOPDEVICE" ]; then
mknod $LOOPDEVICE b 7 $LOOP
mknod $LOOPDEVICE b 7 $LOOP 2>/dev/null
fi
losetup $LOOPDEVICE $1
if [ "$?" -eq "0" ]; then
Expand All @@ -68,7 +75,7 @@ mount_image() {
set_perm() {
chown $2:$3 $1 || exit 1
chmod $4 $1 || exit 1
if [ "$5" ]; then
if [ ! -z "$5" ]; then
chcon $5 $1 2>/dev/null
else
chcon 'u:object_r:system_file:s0' $1 2>/dev/null
Expand All @@ -94,20 +101,19 @@ mktouch() {
chmod 644 $1
}

payload_size_check() {
reqSizeM=0;
for entry in $(unzip -l "$@" 2>/dev/null | tail -n +4 | awk '{ print $1 }'); do
test $entry != "--------" && reqSizeM=$((reqSizeM + entry)) || break;
done;
test $reqSizeM -lt 1048576 && reqSizeM=1 || reqSizeM=$((reqSizeM / 1048576));
request_size_check() {
reqSizeM=`unzip -l "$1" 2>/dev/null | tail -n 1 | awk '{ print $1 }'`
reqSizeM=$((reqSizeM / 1048576 + 1))
}

target_size_check() {
e2fsck -p -f $1
curBlocks=`e2fsck -n $1 2>/dev/null | cut -d, -f3 | cut -d\ -f2`;
curUsedM=$((`echo "$curBlocks" | cut -d/ -f1` * 4 / 1024));
curSizeM=$((`echo "$curBlocks" | cut -d/ -f2` * 4 / 1024));
curFreeM=$((curSizeM - curUsedM));
image_size_check() {
e2fsck -yf $1
curBlocks=`e2fsck -n $1 2>/dev/null | grep $1 | cut -d, -f3 | cut -d\ -f2`;
curUsedM=`echo "$curBlocks" | cut -d/ -f1`
curSizeM=`echo "$curBlocks" | cut -d/ -f1`
curFreeM=$(((curSizeM - curUsedM) * 4 / 1024))
curUsedM=$((curUsedM * 4 / 1024 + 1))
curSizeM=$((curSizeM * 4 / 1024))
}

##########################################################################################
Expand All @@ -133,24 +139,21 @@ if [ "$?" -eq "0" ]; then
done
fi

if [ -z "$BOOTMODE" ]; then
BOOTMODE=false
fi

if ($BOOTMODE) && (! is_mounted /magisk); then
if $BOOTMODE && ! is_mounted /magisk; then
ui_print "! Magisk is not activated!... abort"
exit 1
fi

# Fix SuperSU.....
($BOOTMODE) && /data/magisk/sepolicy-inject -s fsck --live
$BOOTMODE && $BINDIR/sepolicy-inject --live "allow fsck * * *"

# This path should work in any cases
TMPDIR=/dev/tmp

TMPDIR=/tmp
MOUNTPATH=/magisk
IMGNAME=magisk.img

if ($BOOTMODE); then
TMPDIR=/dev/tmp
if $BOOTMODE; then
MOUNTPATH=/dev/magisk_merge
IMGNAME=magisk_merge.img
fi
Expand Down Expand Up @@ -196,10 +199,10 @@ if [ ! -f '/system/build.prop' ]; then
exit 1
fi

API=$(grep_prop ro.build.version.sdk)
ABI=$(grep_prop ro.product.cpu.abi | cut -c-3)
ABI2=$(grep_prop ro.product.cpu.abi2 | cut -c-3)
ABILONG=$(grep_prop ro.product.cpu.abi)
API=`grep_prop ro.build.version.sdk`
ABI=`grep_prop ro.product.cpu.abi | cut -c-3`
ABI2=`grep_prop ro.product.cpu.abi2 | cut -c-3`
ABILONG=`grep_prop ro.product.cpu.abi`

ARCH=arm
IS64BIT=false
Expand All @@ -211,8 +214,7 @@ if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; IS64BIT=true; fi;
# You can get the Android API version from $API, the CPU architecture from $ARCH
# Useful if you are creating Android version / platform dependent mods

IMG=
if (is_mounted /data); then
if is_mounted /data; then
IMG=/data/$IMGNAME
if [ ! -f "/data/magisk.img" ]; then
ui_print "! Magisk is not installed!"
Expand All @@ -232,24 +234,24 @@ else
ui_print " "
fi

payload_size_check "$ZIP" "*"
request_size_check "$ZIP"

if [ -f "$IMG" ]; then
ui_print "- $IMG detected!"
target_size_check $IMG
image_size_check $IMG
if [ "$reqSizeM" -gt "$curFreeM" ]; then
SIZE=$((((reqSizeM + curUsedM) / 32 + 2) * 32))
SIZE=$(((reqSizeM + curUsedM) / 32 * 32 + 64))
ui_print "- Resizing $IMG to ${SIZE}M..."
resize2fs $IMG ${SIZE}M
fi
else
SIZE=$(((reqSizeM / 32 + 2) * 32));
SIZE=$((reqSizeM / 32 * 32 + 64));
ui_print "- Creating $IMG with size ${SIZE}M"
make_ext4fs -l ${SIZE}M -a /magisk -S $INSTALLER/common/file_contexts_image $IMG
fi

mount_image $IMG $MOUNTPATH
if (! is_mounted $MOUNTPATH); then
if ! is_mounted $MOUNTPATH; then
ui_print "! $IMG mount failed... abort"
exit 1
fi
Expand All @@ -268,30 +270,30 @@ for TARGET in $REPLACE; do
done

# Auto Mount
if ($AUTOMOUNT); then
if $AUTOMOUNT; then
mktouch $MODPATH/auto_mount
fi

# prop files
if ($PROPFILE); then
if $PROPFILE; then
cp -af $INSTALLER/common/system.prop $MODPATH/system.prop
fi

# Module info
cp -af $INSTALLER/module.prop $MODPATH/module.prop
if ($BOOTMODE); then
if $BOOTMODE; then
# Update info for Magisk Manager
mktouch /magisk/$MODID/update
cp -af $INSTALLER/module.prop /magisk/$MODID/module.prop
fi

# post-fs-data mode scripts
if ($POSTFSDATA); then
if $POSTFSDATA; then
cp -af $INSTALLER/common/post-fs-data.sh $MODPATH/post-fs-data.sh
fi

# service mode scripts
if ($LATESTARTSERVICE); then
if $LATESTARTSERVICE; then
cp -af $INSTALLER/common/service.sh $MODPATH/service.sh
fi

Expand All @@ -305,14 +307,14 @@ losetup -d $LOOPDEVICE
rmdir $MOUNTPATH

# Shrink the image if possible
target_size_check $IMG
NEWDATASIZE=$(((curUsedM / 32 + 2) * 32))
image_size_check $IMG
NEWDATASIZE=$((curUsedM / 32 * 32 + 32))
if [ "$curSizeM" -gt "$NEWDATASIZE" ]; then
ui_print "- Shrinking $IMG to ${NEWDATASIZE}M..."
resize2fs $IMG ${NEWDATASIZE}M
fi

if (! $BOOTMODE); then
if ! $BOOTMODE; then
umount /system
umount /vendor 2>/dev/null
fi
Expand Down
1 change: 1 addition & 0 deletions module.prop
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version=v1
versionCode=1
author=topjohnwu
description=A short description
template=3

0 comments on commit f9e2e9b

Please sign in to comment.