#!/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin

# Check to see if we need to do that (from kernel command line)?
AUTOROOTFS_SET=`dmesg | grep "Kernel command line" | grep autorootfs`
if [ "$AUTOROOTFS_SET" = "" ]; then
	if [ -x /bin/echo ]; then
		echo "Using rootfs from initramfs/initrd ..."
	fi
	if [ -x /sbin/init ]; then
		exec /sbin/init
	fi
	if [ -x /bin/init ]; then
		exec /bin/init
	fi
	exit  0
fi

#
# *****************************************************************
# BE WARNED that following may have some security implications ... 
# *****************************************************************
#
# If "autorootfs" is found in kernel command line, the following script 
# will try to 
#
# 1. load the kernel module for various external storage 
#    device.
# 2. try to mount the external storage device, and find "init"
#    program.
# 3. If "init" can be found, do a switch_root to make use of 
#    newly found rootfs.
# 4. If "init" not found, unload the kernel module, and try
#    the next device.
# 5. If no rootfs is found, use the original one (e.g. initramfs).
#
# Note that NO authentication/verification of the external rootfs
# is performed here in the example. Further enhancements will be 
# required and is recommended.
#

KERNEL_VER=`uname -r`
MOD_PATH=/lib/modules/$KERNEL_VER

#
# Look for rootfs in the NAND flash
#
RFS="/dev/nblock7 /dev/nblock8"
MOD_ERR=
if [ -f $MOD_PATH/extra/sigmablock.ko ]; then
	modprobe sigmablock > /dev/null 2>&1 || MOD_ERR="y"
fi
if [ "$MOD_ERR" = "" ]; then
	# Figure out major# for NAND block device(s)
	mount -t proc proc /proc
	SIGMNUM=`cat /proc/partitions | grep sigmblock | wc -l`
	SIGMMAJ=`cat /proc/devices | grep sigmblock | head -c 4`
	umount -f /proc
	# Creating device node(s) for NAND block device(s)
	ITER=0
	while [ "$ITER" != "$SIGMNUM" ];
	do
		mknod -m 664 /dev/nblock$ITER b $SIGMMAJ $ITER
		ITER=`expr $ITER + 1`
	done
	# Assume NAND flash uses ext3
	for rfs in `echo $RFS`
	do
		MOUNT_ERR=
	 	mount -t ext3 -o noatime,data=journal $rfs /mnt > /dev/null 2>&1 || MOUNT_ERR="y"
		if [ "$MOUNT_ERR" = "" ]; then
			if [ -x /mnt/init ]; then
				echo "/mnt/init found on $rfs, switching rootfs ..."
				exec switch_root /mnt /init
			fi
			umount -f /mnt
		fi
	done
	rmmod sigmablock > /dev/null 2>&1
fi

#
# Look for rootfs from SATA devices
#
RFS="/dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1"
MOD_ERR=
modprobe sd_mod > /dev/null 2>&1
if [ -f $MOD_PATH/kernel/drivers/ata/sata_tango3.ko ]; then
	modprobe sata_tango3 > /dev/null 2>&1 || MOD_ERR="y"
fi
if [ ! "$MOD_ERR" = "" ]; then
	if [ -f $MOD_PATH/kernel/drivers/ata/sata_tangox.ko ]; then
		MOD_ERR=
		modprobe sata_tangox > /dev/null 2>&1 || MOD_ERR="y"
	fi
fi
if [ "$MOD_ERR" = "" ]; then
	sleep 3
	for rfs in `echo $RFS`
	do
		MOUNT_ERR=
 		mount $rfs /mnt > /dev/null 2>&1 || MOUNT_ERR="y"
		if [ "$MOUNT_ERR" = "" ]; then
			if [ -x /mnt/init ]; then
				echo "/mnt/init found on $rfs, switching rootfs ..."
				exec switch_root /mnt /init
			fi
			umount -f /mnt
		fi
	done
	rmmod sata_tangox > /dev/null 2>&1
	rmmod sata_tango3 > /dev/null 2>&1
fi
rmmod sd_mod > /dev/null 2>&1

#
# Look for rootfs from USB storage devices
#
RFS="/dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1"
MOD_ERR=
modprobe sd_mod > /dev/null 2>&1
modprobe usb-storage > /dev/null 2>&1
if [ -f $MOD_PATH/kernel/drivers/usb/host/tangox-ehci-hcd.ko ]; then
	modprobe tangox-ehci-hcd > /dev/null 2>&1 || MOD_ERR="y"
fi
if [ -f $MOD_PATH/kernel/drivers/usb/host/tangox-ohci-hcd.ko ]; then
	modprobe tangox-ohci-hcd > /dev/null 2>&1
fi
if [ "$MOD_ERR" = "" ]; then
	sleep 5
	for rfs in `echo $RFS`
	do
		MOUNT_ERR=
	 	mount $rfs /mnt > /dev/null 2>&1 || MOUNT_ERR="y"
		if [ "$MOUNT_ERR" = "" ]; then
			if [ -x /mnt/init ]; then
				echo "/mnt/init found on $rfs, switching rootfs ..."
				exec switch_root /mnt /init
			fi
			umount -f /mnt
		fi
	done
	rmmod tangox-ohci-hcd > /dev/null 2>&1
	rmmod tangox-ehci-hcd > /dev/null 2>&1
fi
rmmod usb-storage > /dev/null 2>&1
rmmod sd_mod > /dev/null 2>&1

#
# Look for rootfs from MMC storage devices
#
RFS="/dev/mmcblk0p1 /dev/mmcblk1p1"
MOD_ERR=
modprobe mmc_block > /dev/null 2>&1
modprobe sdhci > /dev/null 2>&1
if [ -f $MOD_PATH/kernel/drivers/mmc/host/sdhci-tangox.ko ]; then
	modprobe sdhci-tangox > /dev/null 2>&1 || MOD_ERR="y"
fi
if [ "$MOD_ERR" = "" ]; then
	# Figure out major# for MMC block device(s)
	mount -t proc proc /proc
	MMCMAJ=`cat /proc/devices | grep mmc | head -c 4`
	umount -f /proc
	mknod -m 664 /dev/mmcblk0 b $SIGMMAJ 0
	mknod -m 664 /dev/mmcblk0p1 b $SIGMMAJ 1
	mknod -m 664 /dev/mmcblk1 b $SIGMMAJ 8
	mknod -m 664 /dev/mmcblk1p1 b $SIGMMAJ 9
	for rfs in `echo $RFS`
	do
		MOUNT_ERR=
	 	mount $rfs /mnt > /dev/null 2>&1 || MOUNT_ERR="y"
		if [ "$MOUNT_ERR" = "" ]; then
			if [ -x /mnt/init ]; then
				echo "/mnt/init found on $rfs, switching rootfs ..."
				exec switch_root /mnt /init
			fi
			umount -f /mnt
		fi
	done
	rmmod sdhci-tangox > /dev/null 2>&1
fi
rmmod sdhci > /dev/null 2>&1
rmmod mmc_block > /dev/null 2>&1

#
# Cannot find any suitable rootfs, use the current rootfs ...
#
if [ -x /bin/echo ]; then
	echo "Using rootfs from initramfs/initrd ..."
fi
if [ -x /sbin/init ]; then
	exec /sbin/init
fi
if [ -x /bin/init ]; then
	exec /bin/init
fi

