LeOS-GSI/external/ih8sn/META-INF/com/google/android/update-binary

145 lines
3.6 KiB
Bash

#!/sbin/sh
OUTFD="/proc/self/fd/$2"
ZIP=$3
ui_print() {
echo "ui_print $1" > "$OUTFD";
echo "ui_print" > "$OUTFD";
}
unmount_partition() {
ui_print "Unmounting partitions"
umount -l "$SYSTEM_MNT"
}
error() {
ui_print "$1"
unmount_partition
exit 1
}
error_no_space() {
error "Not enough space for ih8sn! Aborting"
}
error_mounting() {
error "Could not mount $1! Aborting"
}
get_block_for_mount_point() {
grep -v "^#" /etc/recovery.fstab | grep "[[:blank:]]$1[[:blank:]]" | tail -n1 | tr -s [:blank:] ' ' | cut -d' ' -f1
}
find_block() {
local name="$1"
local fstab_entry=$(get_block_for_mount_point "/$name")
# P-SAR hacks
[ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/")
[ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/system_root")
local dev
if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
if [ -n "$fstab_entry" ]; then
dev="${BLK_PATH}/${fstab_entry}${SLOT_SUFFIX}"
else
dev="${BLK_PATH}/${name}${SLOT_SUFFIX}"
fi
else
if [ -n "$fstab_entry" ]; then
dev="${fstab_entry}${SLOT_SUFFIX}"
else
dev="${BLK_PATH}/${name}${SLOT_SUFFIX}"
fi
fi
if [ -b "$dev" ]; then
echo "$dev"
fi
}
compute_ih8sn_space() {
# Set minimum space required for ih8sn 10mb + STORAGE_BUFFER
NEEDED_STORAGE_SYSTEM=$(expr $STORAGE_BUFFER + $STORAGE_BUFFER)
RECLAIMABLE_STORAGE_SYSTEM=$(find . -type f | sed "s|^./|${SYSTEM_MNT}/system/|" | xargs ls -d 2>/dev/null | xargs du -cs PLACEHOLDER 2>/dev/null | tail -n1 | cut -f1)
NEEDED_STORAGE_SYSTEM=$(expr $NEEDED_STORAGE_SYSTEM - $RECLAIMABLE_STORAGE_SYSTEM)
}
ui_print "**********************"
ui_print "ih8sn installer"
ui_print "**********************"
ui_print "Mounting system partition"
# Ensure system is unmounted so mounting succeeds
umount /system || umount /mnt/system || true
# Find partitions
DYNAMIC_PARTITIONS=`getprop ro.boot.dynamic_partitions`
if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
BLK_PATH="/dev/block/mapper"
else
BLK_PATH=/dev/block/bootdevice/by-name
fi
CURRENTSLOT=`getprop ro.boot.slot_suffix`
if [ ! -z "$CURRENTSLOT" ]; then
if [ "$CURRENTSLOT" == "_a" ]; then
SLOT_SUFFIX="_a"
else
SLOT_SUFFIX="_b"
fi
fi
SYSTEM_BLOCK=$(find_block "system")
# Disable rw protection on dynamic partitions
if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
blockdev --setrw "$SYSTEM_BLOCK"
fi
# Mount and define SYSTEM_MNT
SYSTEM_MNT=/mnt/system
mkdir -p "$SYSTEM_MNT" || true
if mount -o rw "$SYSTEM_BLOCK" "$SYSTEM_MNT"; then
ui_print "$SYSTEM_MNT mounted"
else
error_mounting "$SYSTEM_MNT"
fi
# Compute storage requirements
SYSTEM_STORAGE=`df $SYSTEM_MNT | tail -1 | tr -s ' ' | cut -d ' ' -f4`
STORAGE_BUFFER=10240
compute_ih8sn_space
if [ "$SYSTEM_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM" ]; then
compute_ih8sn_space
if [ "$SYSTEM_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM" ]; then
error_no_space
fi
fi
# Set ih8sn config
SERIALNO=$(getprop ro.boot.serialno)
PRODUCT=$(getprop ro.build.product)
DEFAULT_CONFIG=system/etc/ih8sn.conf
if [[ `unzip -l $ZIP | grep ih8sn.conf.$SERIALNO$` ]] && [[ ! -z "$SERIALNO" ]]; then
CONFIG=$DEFAULT_CONFIG.$SERIALNO
elif [[ `unzip -l $ZIP | grep ih8sn.conf.$PRODUCT$` ]] && [[ ! -z "$PRODUCT" ]]; then
CONFIG=$DEFAULT_CONFIG.$PRODUCT
else
CONFIG=$DEFAULT_CONFIG
fi
ui_print "Copying files"
unzip -qq "$ZIP" system/etc/init/ih8sn.rc system/addon.d/60-ih8sn.sh system/bin/ih8sn -d "${SYSTEM_MNT}/"
ui_print "Using ih8sn config: $CONFIG"
unzip -qqp "$ZIP" $CONFIG > "${SYSTEM_MNT}/$DEFAULT_CONFIG"
unmount_partition
ui_print "Done!"
exit 0