#!/bin/sh
#
#
# LK8000 Tactical Flight Computer -  WWW.LK8000.IT
# Released under GNU/GPL License v.2
# See CREDITS.TXT file for authors and copyrights
# 
# Author: Bruno de Lacheisserie
# 

# This Script is Adapted from XCSoar

# This script is launched by /etc/inittab on boot.  It initialises the
# Kobo device and launches LK8000.

/bin/mount -o remount,noatime,nodiratime /dev/mmcblk0p1 /

export PATH=/sbin:/bin:/usr/sbin:/usr/bin
umask 022

# mount the data partition
/bin/dosfsck -a -w /dev/mmcblk0p3
/bin/mkdir -p /mnt/onboard
/bin/mount -t vfat -o iocharset=utf8 /dev/mmcblk0p3 /mnt/onboard
/bin/rm -rf /mnt/onboard/fsck* /mnt/onboard/FSCK*


# check the kernel
#TODO : check CONFIG_USB_EHCI_ARC_OTG is valid for GloHD ???
/bin/mkdir -p /proc /dev
/bin/mount -t proc none /proc
if zcat /proc/config.gz |grep -q ^CONFIG_USB_EHCI_ARC_OTG=y; then
	# this is the USB-OTG kernel
	OTG_kernel=Y
else
	# this is the KoboLabs kernel
	OTG_kernel=N
fi
umount /proc

# launch the original Kobo init script if:
# - there's a pending update
# - Nickel start was scheduled
# - the LK8000 installation is broken
if [ -f /mnt/onboard/.kobo/Kobo.tgz \
    -o -f /mnt/onboard/.kobo/KoboRoot.tgz \
    -o -f /mnt/onboard/LK8000/kobo/start_nickel \
    -o ! -x /opt/LK8000/bin/LK8000-KOBO ]; then
	
	if [ "${OTG_kernel}" == "Y" -a -f /opt/LK8000/lib/kernel/uImage.kobo ]; then	
            # this is the USB-OTG kernel, but Nickel should better have
            # the original Kobo kernel; install it and reboot
						
            rm -f /mnt/onboard/LK8000/kobo/reboot

            /bin/mount -t tmpfs none /dev
            /bin/mknod /dev/mmcblk0 b 179 0
            dd if=/opt/LK8000/lib/kernel/uImage.kobo of=/dev/mmcblk0 bs=512 seek=2048 
            sync 
            exec reboot
    fi	
	
    rm -f /mnt/onboard/LK8000/kobo/start_nickel
    # launch user script before nickel start
    if [ -f /mnt/onboard/LK8000/kobo/init_nickel.sh ]; then
        source /mnt/onboard/LK8000/kobo/init_nickel.sh
    fi
    umount -l /mnt/onboard
    exec /etc/init.d/rcS
fi

# PLATFORM is used for the path to the WiFi & Usb drivers
if [ ! -n "${PLATFORM}" ] ; then
	PLATFORM="freescale"
	if dd if="/dev/mmcblk0" bs=512 skip=1024 count=1 | grep -q "HW CONFIG" ; then
		CPU="$(ntx_hwconfig -s -p /dev/mmcblk0 CPU)"
		PLATFORM="${CPU}-ntx"
	fi

	if [ "${PLATFORM}" == "freescale" ] ; then
		if [ ! -s "/lib/firmware/imx/epdc_E60_V220.fw" ] ; then
			mkdir -p "/lib/firmware/imx"
			dd if="/dev/mmcblk0" bs=512K skip=10 count=1 | zcat > "/lib/firmware/imx/epdc_E60_V220.fw"
			sync
		fi
	elif [ ! -e "/etc/u-boot/${PLATFORM}/u-boot.mmc" ] ; then
		PLATFORM="ntx508"
	fi

    if [ $CPU ]; then
        PCB="$(ntx_hwconfig -s -p /dev/mmcblk0 PCB)"
        RAM="$(ntx_hwconfig -s -p /dev/mmcblk0 RAMType)"
        RAM_SIZE="$(ntx_hwconfig -s -p /dev/mmcblk0 RamSize)"
        RAM_SIZE="$(echo $RAM_SIZE | awk '{print $RAM_SIZE-MB}')"
    
        echo "${CPU}" > /mnt/onboard/LK8000/kobo/CPU
        echo "${PCB}" > /mnt/onboard/LK8000/kobo/PCB
        echo "${RAM}" > /mnt/onboard/LK8000/kobo/RAM
        echo "${RAM_SIZE}" > /mnt/onboard/LK8000/kobo/RAM_SIZE
    fi
fi
# end of value check of PLATFORM

echo "${PLATFORM}" > /mnt/onboard/LK8000/kobo/PLATFORM

# create symbolic link for usb & wifi module
if [ -d "/drivers/${PLATFORM}" ] ; then
    if [ -L "/drivers/current" ]; then
        rm "/drivers/current"
    fi
    ln -s "/drivers/${PLATFORM}" "/drivers/current"
fi


# extract Model for debug facility
MODEL=`dd if=/dev/mmcblk0 bs=8 count=1 skip=64`
echo "${MODEL}" > /mnt/onboard/LK8000/kobo/MODEL

# install OTG_kernel if available and not already installed.
if [ "${OTG_kernel}" == "N" -a -f /mnt/onboard/LK8000/kobo/uImage-${PLATFORM} ]; then
    /bin/mount -t tmpfs none /dev
    /bin/mknod /dev/mmcblk0 b 179 0

    # save original kernel as uImage.kobo
    rm -f /opt/LK8000/lib/kernel/uImage.kobo
    dd if=/dev/mmcblk0 bs=512 skip=2048 count=6000 of=/opt/LK8000/lib/kernel/uImage.kobo
	
    # "Switch to OTG_kernel and reboot"
    dd if=/mnt/onboard/LK8000/kobo/uImage-${PLATFORM} of=/dev/mmcblk0 bs=512 seek=2048 
    sync 
    exec reboot
fi

# prepare file system

/bin/mkdir -p /proc /dev /root /tmp /sys /var/lib /var/log /var/run

/bin/mount -t proc none /proc
/bin/mount -t tmpfs none -o size=16m /tmp
/bin/mount -t tmpfs none /dev
/bin/mount -t tmpfs none -o size=16k /var/lib
/bin/mount -t tmpfs none -o size=16k /var/log
/bin/mount -t tmpfs none -o size=128k /var/run
/bin/mount -t sysfs none -o size=500k /sys

/bin/mknod /dev/null c 1 3
/bin/mknod /dev/zero c 1 5
/bin/mknod /dev/random c 1 8
/bin/mknod /dev/urandom c 1 9
/bin/mknod /dev/ptmx c 5 2
/bin/mkdir /dev/input
/bin/mknod /dev/input/event0 c 13 64
/bin/mknod /dev/input/event1 c 13 65
/bin/mknod /dev/fb0 c 29 0
/bin/mknod /dev/rtc0 c 254 0
/bin/mknod /dev/mmcblk0 b 179 0
/bin/mknod /dev/mmcblk0p1 b 179 1
/bin/mknod /dev/mmcblk0p2 b 179 2
/bin/mknod /dev/mmcblk0p3 b 179 3

/sbin/hwclock -s -u

# workaround for kernel crash "scheduling while atomic"
echo -e '\000\000\000\000' > /proc/sys/kernel/hotplug

# disable the flashing led
echo "ch 3" > /sys/devices/platform/pmic_light.1/lit
echo "cur 0" > /sys/devices/platform/pmic_light.1/lit
echo "dc 0" > /sys/devices/platform/pmic_light.1/lit

#start udev deamon & trigger
/sbin/udevd -d
/sbin/udevadm trigger 

# launch user script
if [ -f /mnt/onboard/LK8000/kobo/init.sh ]; then
    source /mnt/onboard/LK8000/kobo/init.sh
fi

# workaround for Kobo Touch N905B kernel bug: the mxc_fb driver
# crashes when KoboMenu tries to rotate the display too early after
# boot
if [ `dd if=/dev/mmcblk0 bs=8 count=1 skip=64` = "SN-N905B" ]; then
    sleep 1
fi

# that allow to restart on Nickel in case of crash.
echo " " > /mnt/onboard/LK8000/kobo/start_nickel

# finally, launch LK8000-KOBO
exec /opt/LK8000/bin/LK8000-KOBO >> /mnt/onboard/LK8000/error.log 2>&1
