#!/usr/bin/expect -f
#
# Load kernel and filesystem onto an Auger v2 board attached
# to the serial port.  Some serial program (like "minicom") must already be
# set up to connect.
#
# Usage is: ./loadfs <SERIAL>
#
#--------------------------------------

set INITRAMFS_KERNEL "zImage-2.6.27-vpac2-mkubifs.dat"
set KERNEL "zImage-2.6.27.48-vpac2m.dat"
set FILESYS "rootfs-vpac1-ubifs-256-2.6.27.48.tar.gz"

set GATEWAY   "192.168.61.161"
set SUBNET    "192.168.61"

set SERIAL "minicom"

#--------------------------------------

log_user 1
exp_internal 0

set sNum [lindex $argv 0]

if { $sNum == "" } { 
    puts "Usage: ./loadfs <SERIAL>"
    exit
}

fconfigure stdin -blocking 1
puts "Loading kernel and filesystem onto Auger board $sNum on serial port."
puts "This is only for use on a NAND-flash-based Voipac PC card."
puts ""
puts "This will erase everything except the bootloader!!!  This procedure"
puts "is OBSOLETE and can result in an unbootable filesystem."
puts ""
puts "ARE YOU SURE YOU WANT TO CONTINUE (y/n)? "
flush stdout
gets stdin ans
if { ($ans != "Y") && ($ans != "y")} {
    puts "OK, aborting."
    exit    
}

spawn $SERIAL
send "\r"

set timeout 20
puts "Press the reset button now (within 20 seconds)..."

# Check that we're really booting a NAND card!
expect "NAND X-Loader" { } \
timeout { 
        puts "Didn't see expected reboot message... are you sure this is a NAND-based card?"
        exit
}

set timeout -1
expect "Hit any key to stop autoboot"

send "s"
expect "u-boot> "

#--------------------------------------
# Erase the flash

send "erase 1:2-\r"
expect "Done"
expect "u-boot> "

#--------------------------------------
# Set up u-boot variables
puts "Setting up IP addresses..."

send "setenv serverip $GATEWAY\r"
expect "u-boot> "

send "setenv ipaddr $SUBNET.$sNum\r"
expect "u-boot> "

send "save\r"
expect "done"
expect "u-boot> "

#--------------------------------------
# Boot into initramfs kernel

puts "Booting into initramfs kernel..."

send "set bootargs console=ttyS0,38400\r"
expect "u-boot> "

send "tftp $INITRAMFS_KERNEL\r"
expect "done"
expect "u-boot> "

send "wtags; cp.b a0000124 a0000114 80; go\r"

#--------------------------------------
# Now create the filesystem

puts "Creating the filesystem..."

expect "login: "
send "root\r"

expect "Password: "
send "root\r"
expect "# "

send "ubidetach /dev/ubi_ctrl -m 2\r"
expect "# "

send "flash_eraseall /dev/mtd2\r"
expect "# "

send "ubiattach /dev/ubi_ctrl -m 2\r"
expect "UBI device number"
expect "# "

send "ubimkvol /dev/ubi0 -N rootfs -m\r"
expect "Volume ID"
expect "# "

send "mount -t ubifs ubi0:rootfs /mnt\r"
expect "media format"
expect "# "

send "tftp -r $FILESYS -l /tmp/rootfs.tar.gz -g $GATEWAY\r"
expect "# "

send "tar xzf /tmp/rootfs.tar.gz -C /mnt\r"
expect "# "

send "sync\r"
expect "# "

send "umount /mnt\r"
expect "UBIFS:"
expect "# "

send "ubidetach /dev/ubi_ctrl -m 2\r"
expect "UBI"
expect "# "

send "reboot\r"

expect "Hit any key to stop autoboot"

send "s"
expect "u-boot> "

#--------------------------------------
# Install the real kernel

send "set bootargs root=ubi0:rootfs rootfstype=ubifs ubi.mtd=Filesystem console=ttyS0,38400\r"
expect "u-boot> "

send "set bootcmd one read a1000000 40000 160000\x5c\x3b wtags\x5c\x3b go a1000000\r"
expect "u-boot> "

send "save\r"
expect "done"
expect "u-boot> "

send "tftp $KERNEL\r"
expect "done"
expect "u-boot> "

send "erase 1:2-12\r"
expect "Done"
expect "u-boot> "

send "cp.b a1000000 40000 \x24(filesize)\r"
expect "done"
expect "u-boot> "

send "wtags; go\r"
expect "login: "

puts "Reboot into Linux succesful!"
puts "Done."

exit
