#!/bin/sh

# If we are in the flashing procedure, start with the kernel and file system
# loaded into the DDR memory
memcmp -s /dev/mem -d /env/magicstr 0x71FFFFF8 0x0 8
if [ $? -eq 0 ]; then
	echo "Booting kernel for NAND flashing procedure"
	bootargs="console=ttyS0,115200 panic=10 mem=64M"
	bootargs="$bootargs initrd=0x72000000,25M"
	bootargs="$bootargs root=/dev/ram0"
	addpart /dev/ram0 0x1000000@0x1000000(kernel)
	addpart /dev/ram0 0x1000000@0x2000000(initrd)
	bootm /dev/ram0.kernel
fi

#default value (can be overridden in /env/config)
preboot_timeout="3"
autoboot_timeout="5"
autoboot_select="1"

### Override default value using /env/config
. /env/config
echo "Starting up barebox [wrs3] (MAC=$eth0.ethaddr)"


### Generic setup
PATH=/env/bin
export PATH

if [ x$autoboot_timeout != x ]; then
    menu_timeout="-A $autoboot_timeout"
fi

#Set 2nd LED ON (PA2)
gpio_set_value 33 0

PS1="wrs-$eth0.ethaddr# "

### Creating the partitions:
if [ -e /dev/mem.kernel ]; then
else
    addpart /dev/mem  8M@0x71000000(kernel)
fi
if [ -e /dev/nand0.kernel ]; then
else
    addpart /dev/nand0 256k@0x4000(bareboxenv),8M@0x100000(kernel),-@0x4000000(rootfs)
fi
if [ -e /dev/nand0.kernel.bb ]; then
else
    nand -a /dev/nand0.*
fi

# Create the menu
#menu -r -m boot
menu -a -m boot -d "Welcome on WRSv3 Boot Sequence"
menu -e -a -m boot -c 'boot-nand' -d "boot from nand (default)"
menu -e -a -m boot -c 'boot-script' -d "boot from TFTP script"
menu -e -a -m boot -c 'edit-config' -d "edit config"
menu -e -a -m boot -c 'exit 0' -d "exit to shell"
menu -e -a -m boot -c reset -d "reboot"

# allow the user to see previous messages
echo "starting menu in ${preboot_timeout} seconds"
sleep ${preboot_timeout}
# Select by default a specific menu
menu -m boot -S -n $autoboot_select
#Use a timeout to let the user change the entry in menu
menu -s boot $menu_timeout
