#!/bin/busybox sh
#
# By Chih-Wei Huang <cwhuang@linux.org.tw>
# and Thorsten Glaser <tg@mirbsd.org>
#
# Modified by BlissLabs & Contributors
#
# License: GNU Public License
# We explicitely grant the right to use the scripts
# with Android-x86 project.
#

PATH=/sbin:/bin:/system/bin:/system/xbin:/vendor/bin:/system_ext/bin:/system/vendor/bin:/system/system_ext/bin; export PATH

mem_sleep_default=shallow
acpi_sleep=s3_mode
pcie_aspm=off
syscall_hardening=off
SDCARDFS_DISABLE=1
FFMPEG_OMX_CODEC=1
OMX_NO_YUV420=1 
CODEC2_LEVEL=0
MULTI_REFRESH_RATE=1
INTEL_PSTATE_CPU_MIN_PERF_PCT=5 INTEL_PSTATE_CPU_MAX_PERF_PCT=100
INTEL_PSTATE_STATUS=passive
CPU_ENERGY_PERFORMANCE_PREF=performance 
FORCE_CPU_GOV=performance 
FORCE_CPU_SCALING_GOV=performance 
FORCE_GPU_SCALING_GOV=performance 
FORCE_POWER_PROFILE=performance
FORCE_MEMORY_TRIM_ENABLE=true 
FORCE_MIN_FREE_MEMORY=512 
FORCE_MAX_FREE_MEMORY=1024
FORCE_ENFORCE_MIN_FREE_MB=1024 
FORCE_SCHED_MIN_FREE_PAGES=1024 
FORCE_SCHED_MIN_DIRTY_PAGES=128 
FORCE_SWAP_FREE_LOW_PERCENTAGE=10 
FORCE_THRASHING_LIMIT=30 
FORCE_PSI_PARTIAL_STALL_THRESHOLD=200 
FORCE_PSI_COMPLETE_STALL_THRESHOLD=700  
FORCE_THRASHING_LIMIT_DECAY=50 
FORCE_SWAP_UTIL_MAX=100
FORCE_BLUETOOTH_SERVICE=enable
BTLINUX_HAL=1
VIRT_WIFI=1
SET_FAKE_CHARGING_STATUS=1
SET_FAKE_BATTERY_LEVEL=100

# ASCII art
text_art()
{
cat << "EOF"
{====================================================}
{          Android TV 13 x86_64 BRTDTV               }
{    Bruno Telegram Channel:@Android_roms_x86        }
{    Tulio Domingos Youtube Channel:@TDAndroidTVnoPC }
{    Website:android-tvno-pc.my.canva.site           }
{====================================================}
EOF
}

bliss_feature()
{
	if [ "$INTERNAL_MOUNT" -gt "0" ]; then
cat << "EOF"
# WARNING: INTERNAL_MOUNT option is enabled, all the internal partitions
# will now be recognized as USBs if the supported filesystem is available
# and will create empty directories like Android, DCIM, Documents, etc.

## DO NOT PRESS "TAP TO FIX", IT WILL FORMAT THE DRIVE!
EOF

	mount --bind system/vendor/etc/fstab.internal.x86 $(ls | grep fstab.)
	fi

	# Enabling AOSP features for PC
	if [ "$PC_MODE" -gt "0" ]; then
cat << "EOF"
#Based on LMODroid 4.2
#Thanks hmtheboy154
EOF
		mount --bind system/etc/hidden_xml/pc.xml system/etc/permissions/pc.xml
	fi

	if [ "$HPE" -gt "0" ]; then
cat << "EOF"
#Website: android-tvno-pc.my.canva.site
#Android TV 13 x86_64 BRTDTV
EOF
		mount --bind system/etc/hidden_xml/hpe.xml system/etc/sysconfig/hpe.xml
	fi

	# Hide bootctrl from VINTF manifest if we don't set up virtual A/B
	if [ ! "$(cat /proc/cmdline | grep androidboot.slot_suffix)" ]; then
cat << "EOF"
{====================================================}
{               Based on LMODroid 4.2                }
{                      By                            }
{                  HmTheBoy154                       }
{====================================================}
EOF
	echo '<?xml version="1.0" encoding="utf-8"?>' > /tmp/fakeboot.xml
	echo '<manifest version="1.0" type="device">' >> /tmp/fakeboot.xml
	echo '</manifest>' >> /tmp/fakeboot.xml
	mount --bind /tmp/fakeboot.xml system/vendor/etc/vintf/manifest/android.hardware.boot@1.2.xml
	fi

	# Hide BLE feature from the system
	if [ "$BT_BLE_DISABLE" -ge "1" ]; then
cat << "EOF"
# NOTICE: BLE is set to be disabled
EOF
	echo '<?xml version="1.0" encoding="utf-8"?>' > /tmp/noble.xml
	echo '<permissions>' >> /tmp/noble.xml
	echo '</permissions>' >> /tmp/noble.xml
	mount --bind /tmp/noble.xml system/vendor/etc/permissions/android.hardware.bluetooth_le.xml
	fi
}

# auto installation
if [ -n "$AUTO_INSTALL" ]; then
	echo -n "This will replace all operating-systems on this device with a read-only version of this OS. Are you sure you want to continue (y/n)? "
	read answer
	if [ "$answer" != "${answer#[Yy]}" ] ;then
		echo "Booting in Live Mode instead"
	else
		echo "Starting Installer"
		INSTALL=1
	fi
fi

# configure debugging output
if [ -n "$DEBUG" -o -n "$INSTALL" ]; then
	LOG=/tmp/log
	set -x
else
	LOG=/dev/null
	test -e "$LOG" || busybox mknod $LOG c 1 3
fi
exec 2>> $LOG

# early boot
if test x"$HAS_CTTY" != x"Yes"; then
	# initialise /proc & /sys and /tmp
	busybox mount -t proc proc /proc
	busybox mount -t sysfs sys /sys
	busybox mount -t tmpfs tmpfs /tmp
	# let busybox install all applets as symlinks
	# First let Docker busybox install applet
	busybox.docker --install -s /bin
	# Then we get Debian busybox applets in /tmp
	# and move it back to /bin
	mkdir /tmp/busybox_bin && busybox --install -s /tmp/busybox_bin && mv /tmp/busybox_bin/* /bin
	# remove busybox fdisk & mke2fs
	rm /bin/fdisk
	rm /bin/mke2fs
	# spawn shells on tty 2 and 3 if debug or installer
	if test -n "$DEBUG" || test -n "$INSTALL"; then
		# ensure they can open a controlling tty
		mknod /dev/tty c 5 0
		# create device nodes then spawn on them
		mknod /dev/tty2 c 4 2 && openvt
		mknod /dev/tty3 c 4 3 && openvt
	fi
	if test -z "$DEBUG" || test -n "$INSTALL"; then
		echo 0 0 0 0 > /proc/sys/kernel/printk
	fi
	# initialise /dev (first time)
	busybox mount -t devtmpfs devtmpfs /dev
	mkdir -p /dev/block
	echo /bin/mdev > /proc/sys/kernel/hotplug
	mdev -s
	# re-run this script with a controlling tty
	exec env HAS_CTTY=Yes setsid cttyhack /bin/sh "$0" "$@"
fi

# now running under a controlling tty; debug output from stderr into log file
# boot up Android

# First, chmod /sbin because somehow the build process messed up the permission
chmod 755 /sbin/*

error()
{
	echo $*
	return 1
}

try_mount()
{
	RW=$1; shift
	if [ "${ROOT#*:/}" != "$ROOT" ]; then
		# for NFS roots, use nolock to avoid dependency to portmapper
		mount -o $RW,noatime,nolock $@
		return $?
	fi
	case $(blkid $1) in
		*TYPE=*ntfs*)
			if [ "$USE_NTFS3" -ge 1 ] || [ "$BOOT_USE_NTFS3" -ge 1 ]; then
				mount -t ntfs3 -o rw,noatime,windows_names $@
			else
				mount.ntfs-3g -o rw,force $@
			fi
			;;
		*TYPE=*)
			mount -o $RW,noatime $@
			;;
		*)
			return 1
			;;
	esac
}

check_root()
{
	if [ "`dirname $1`" = "/dev" ]; then
		[ -e $1 ] || return 1
		blk=`basename $1`
		[ ! -e /dev/block/$blk ] && ln $1 /dev/block
		dev=/dev/block/$blk
	else
		dev=$1
	fi
	try_mount ro $dev /mnt || return 1

	# If $INSTALL is being set, assuming that we are in a removable device or
	# in a iso or any type of device that copied all the content of an iso in
	if [ -n "$INSTALL" ]; then
		if [ ! -d "/mnt/isolinux" ] || [ ! -f "/mnt/install.img" ]; then
			return 1
		fi
	fi

	if [ -n "$iso" -a -e /mnt/$iso ]; then
		mount --move /mnt /iso
		mkdir /mnt/iso
		mount -o loop /iso/$iso /mnt/iso
	fi
	if [ -e /mnt/$SRC/system.sfs ]; then
		mount -o loop,noatime /mnt/$SRC/system.sfs android
		if [ -e android/system.img ]; then
			mount --move android /sfs
			mount -o loop,noatime /sfs/system.img android
		fi
	elif [ -e /mnt/$SRC/system.efs ]; then
		mount -o loop,noatime /mnt/$SRC/system.efs android
		if [ -e android/system.img ]; then
			mount --move android /sfs
			mount -o loop,noatime /sfs/system.img android
		fi
	elif [ -e /mnt/$SRC/system$SLOT.img ]; then
		remount_rw
		mount -o loop,noatime /mnt/$SRC/system$SLOT.img android
	elif [ -s /mnt/$SRC/system/system/build.prop ]; then
		remount_rw
		mount --bind /mnt/$SRC/system android
	elif [ -z "$SRC" -a -s /mnt/system/build.prop ]; then
		mount --bind /mnt android
	else
		return 1
	fi
	echo " Found at $1"
	hash -r
	text_art
}

remount_rw()
{
	# "foo" as mount source is given to workaround a Busybox bug with NFS
	# - as it's ignored anyways it shouldn't harm for other filesystems.
	mount -o remount,rw foo /mnt
}

debug_shell()
{
	if [ -x system/bin/sh ]; then
		echo Running MirBSD Korn Shell...
		USER="($1)" system/bin/sh -l 2>&1
		[ $? -ne 0 ] && /bin/sh 2>&1
	else
		echo Running busybox ash...
		busybox sh 2>&1
	fi
}

setup_loop()
{
	for i in $(seq 0 9); do
		if ! losetup /dev/loop$i >/dev/null 2>&1; then
			if losetup /dev/loop$i $1 >/dev/null 2>&1; then
				local dev="/dev/block/loop$i"

				ln -s "/dev/loop$i" "$dev"
				echo "$dev"

				return 0
			fi
		fi
	done
}

setup_ab_loops()
{
	if [ ! -z "$SLOT" ]; then
		if [ "$SLOT" = "_a" ]; then
			ln -s $(setup_loop /mnt/$SRC/system_b.img) /dev/block/by-name/system_b
		else
			ln -s $(setup_loop /mnt/$SRC/system_a.img) /dev/block/by-name/system_a
		fi

		ln -s $(setup_loop /mnt/$SRC/initrd_a.img) /dev/block/by-name/initrd_a
		ln -s $(setup_loop /mnt/$SRC/initrd_b.img) /dev/block/by-name/initrd_b
		ln -s $(setup_loop /mnt/$SRC/kernel_a) /dev/block/by-name/kernel_a
		ln -s $(setup_loop /mnt/$SRC/kernel_b) /dev/block/by-name/kernel_b
		ln -s $(setup_loop /mnt/$SRC/misc.img) /dev/block/by-name/misc
	fi
}

echo "Detecting ATV13-BRTDTV..."
echo ""
echo "If it took too long to detect, reboot and check your installation folder"
echo ""

[ -z "$SRC" -a -n "$BOOT_IMAGE" ] && SRC=`dirname $BOOT_IMAGE`

for c in `cat /proc/cmdline`; do
	case $c in
		iso-scan/filename=*)
			SRC=iso
			eval `echo $c | cut -b1-3,18-`
			;;
		androidboot.slot_suffix=*)
			eval SLOT=`echo $c | cut -f 2 -d "="`
			;;
		androidboot.efidisk=*)
			eval EFIDISK=`echo $c | cut -f 2 -d "="`
			;;
		androidboot.efidir=*)
			eval EFIDIR=`echo $c | cut -f 2 -d "="`
			;;
		*)
			;;
	esac
done

cd /
while :; do
	for device in ${ROOT:-/dev/[hmnsvx][dmrv][0-9a-z]*}; do
		check_root $device && break 2

		# Also umount /android and /sfs if already mounted
		mountpoint -q /android && umount /android
		mountpoint -q /sfs && umount /sfs
		mountpoint -q /mnt && umount /mnt
	done
	sleep 1
	echo -n .
done

if ! grep 'sse4_2' /proc/cpuinfo 1>/dev/null; then {
  printf 'WARNING: Your CPU does not support SSE4.2 instructions, the OS will not boot\n';
} fi
cd /android

ln -s mnt/$SRC /src
ln -s android/system /
ln -s ../system/lib/firmware ../system/lib/modules /lib
mkdir -p /lib/modules
ln -s ../system/lib/modules /lib/modules/`uname -r`

if [ -d system/apex/com.android.runtime.debug ]; then
	ln -s ../android/system/apex/com.android.runtime.debug /apex/com.android.runtime
elif [ -d system/apex/com.android.runtime.release ]; then
	ln -s ../android/system/apex/com.android.runtime.release /apex/com.android.runtime
elif [ -d system/apex/com.android.runtime ]; then
	ln -s ../android/system/apex/com.android.runtime /apex/com.android.runtime
fi

bliss_feature
device_information

if [ -n "$INSTALL" ]; then
	zcat /src/install.img | ( cd /; cpio -iud > /dev/null )
fi

# load scripts
for s in `ls /scripts/* /src/scripts/*`; do
	test -e "$s" && source $s
done

# ensure keyboard driver is loaded
if [ -n "$INSTALL" -o -n "$DEBUG" ]; then
	echo "Detecting hardware..."
	busybox modprobe -a hid-apple
	if [ "$AUTO_LOAD" = "alpine" ]; then
		auto_detect_alpine &
	else
		auto_detect &
	fi
	sleep 2
fi

if [ 0$DEBUG -gt 0 ]; then
	echo -e "\nType 'exit' to continue booting...\n"
	debug_shell debug-found
fi

# A target should provide its detect_hardware function.
# On success, return 0 with the following values set.
# return 1 if it wants to use auto_detect
[ "$AUTO" != "1" ] && detect_hardware && FOUND=1

[ -n "$INSTALL" ] && do_install

load_modules
mount_data
mount_sdcard
mount_grub
setup_ab_loops
setup_tslib
setup_dpi
post_detect

if [ 0$DEBUG -gt 1 ]; then
	echo -e "\nUse Alt-F1/F2/F3 to switch between virtual consoles"
	echo -e "Type 'exit' to enter Android...\n"

	debug_shell debug-late
fi

[ -n "$DEBUG" ] && SWITCH=${SWITCH:-chroot}

# We must disable mdev before switching to Android
# since it conflicts with Android's init
echo > /proc/sys/kernel/hotplug

# Use correct modprobe location
echo /system/bin/modprobe > /proc/sys/kernel/modprobe

export ANDROID_ROOT=/system

exec ${SWITCH:-switch_root} /android /init

# avoid kernel panic
while :; do
	echo
	echo '	Android-x86 console shell. Use only in emergencies.'
	echo
	debug_shell fatal-err
done
