#!/bin/busybox sh

export CONSOLE=/dev/ttymxc0
export PATH=/bin:/sbin
/bin/busybox --install -s
ln -s /slib/libc.so.0 /slib/libgcc_s.so.1
mount -t devtmpfs devtmpfs /dev
mkdir /tmp/

mount -t proc proc /proc
mount -t sysfs sysfs /sys
mkdir /etc/
ln -s /dev/resolv.conf /etc/resolv.conf
mkdir /dev/pts
mount -t devpts devpts /dev/pts
ifconfig lo 127.0.0.1 up
route add -net 127.0.0.0 netmask 255.0.0.0 lo

stty -F $CONSOLE ospeed 115200 raw -echo sane >/dev/null 2>&1
setconsole -r $CONSOLE
stty -F $CONSOLE rows 25 cols 80 isig
(
	x=0
	tmout=1000
	echo "TS IMX6 USB Production"

	# If SATA is used, it claims sda and usb blaster is sdb,
	# Otherwise use sdb
	dmesg | grep "ata1: SATA link up" 2>&1 > /dev/null
	if [ "$?" -eq "0" ]; then
		DEV="/dev/sdb1"
	else
		DEV="/dev/sda1"
	fi

	while [ "$x" -lt $tmout -a ! -b "$DEV" ]; do
		x=$((x+1))
		sleep .1
	done

	mkdir /mnt/usb/
	mount -o ro $DEV /mnt/usb/
	cp /mnt/usb/blast.sh /tmp/
	chmod a+x /tmp/blast.sh
	/tmp/blast.sh
) <$CONSOLE >$CONSOLE 2>&1

(
	export PATH=$PATH:/mnt/root/bin:/mnt/root/sbin:/usr/bin:/usr/sbin
	setsid sh -c 'exec sh <$CONSOLE >$CONSOLE 2>&1'
) <$CONSOLE >$CONSOLE 2>&1 &

wait
