#!/bin/bash

# Handle running Poky images under qemu
#
# Copyright (C) 2006-2008 OpenedHand Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


# Call setting:
#   QEMU_MEMORY (optional) - set the amount of memory in the emualted system.
#   SERIAL_LOGFILE (optional) - log the serial port output to a file
#   CROSSPATH - the path to any cross toolchain to use with distcc
#
# Image options:
#   MACHINE - the machine to run
#   TYPE - the image type to run
#   ZIMAGE - the kernel image file to use
#   HDIMAGE - the disk image file to use
#

if [ -z "$QEMU_MEMORY" ]; then
	case "$MACHINE" in
		"qemux86") 
			QEMU_MEMORY="128M"
			;;
		*)
			QEMU_MEMORY="64M"
			;;
	esac

fi

QEMUIFUP=`which poky-qemu-ifup`
QEMUIFDOWN=`which poky-qemu-ifdown`
KERNEL_NETWORK_CMD="ip=192.168.7.2::192.168.7.1:255.255.255.0"
QEMU_TAP_CMD="-net tap,vlan=0,ifname=tap0,script=$QEMUIFUP,downscript=$QEMUIFDOWN"
QEMU_NETWORK_CMD="-net nic,vlan=0 $QEMU_TAP_CMD"
KERNCMDLINE="mem=$QEMU_MEMORY"

SERIALOPTS=""
if [ "x$SERIAL_LOGFILE" != "x" ]; then
    SERIALOPTS="-serial file:$SERIAL_LOGFILE"
fi

case "$MACHINE" in
	"qemuarm") ;;
	"qemux86") ;;
	"akita") ;;
	"spitz") ;;
	"nokia800") ;;
	"nokia800-maemo") ;;
	*)
		echo "Error: Unsupported machine type $MACHINE"
		return
		;;
esac

if [ "$TYPE" != "nfs" -a ! -f "$HDIMAGE" ]; then
    echo "Error: Image file $HDIMAGE doesn't exist"
    return
fi

if [ ! -f "$ZIMAGE" ]; then
    echo "Error: Kernel image file $ZIMAGE doesn't exist"
    return
fi

if [ -e /proc/sys/vm/mmap_min_addr ]; then
    if [ `cat /proc/sys/vm/mmap_min_addr` != "0" ]; then
        echo "Error, please set /proc/sys/vm/mmap_min_addr to 0 since otherwise it can cause problems with QEMU"
	return
    fi
fi

if [ "$MACHINE" = "qemuarm" ]; then
    QEMU=qemu-system-arm
    if [ "$TYPE" = "ext2" ]; then
        KERNCMDLINE="root=/dev/sda console=ttyAMA0 console=tty0 mem=$QEMU_MEMORY"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M versatilepb -hda $HDIMAGE -usb -usbdevice wacom-tablet -no-reboot"
    fi
    if [ "$TYPE" = "nfs" ]; then
        if [ "x$HDIMAGE" = "x" ]; then
            HDIMAGE=/srv/nfs/qemuarm
        fi
        if [ ! -d "$HDIMAGE" ]; then
            echo "Error: NFS mount point $HDIMAGE doesn't exist"
            return
        fi
        KERNCMDLINE="root=/dev/nfs nfsroot=192.168.7.1:$HDIMAGE rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M versatilepb -no-reboot"
    fi
fi

if [ "$MACHINE" = "qemux86" ]; then
    QEMU=qemu
    if [ "$TYPE" = "ext2" ]; then
        KERNCMDLINE="root=/dev/hda mem=$QEMU_MEMORY $KERNEL_NETWORK_CMD"
        QEMUOPTIONS="-std-vga $QEMU_NETWORK_CMD -hda $HDIMAGE -usb -usbdevice wacom-tablet"
    fi
    if [ "$TYPE" = "nfs" ]; then
        if [ "x$HDIMAGE" = "x" ]; then
            HDIMAGE=/srv/nfs/qemux86
        fi
        if [ ! -d "$HDIMAGE" ]; then
            echo "Error: NFS mount point $HDIMAGE doesn't exist."
            return
        fi
        KERNCMDLINE="root=/dev/nfs nfsroot=192.168.7.1:$HDIMAGE rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY"
        QEMUOPTIONS="-std-vga $QEMU_NETWORK_CMD"
    fi
fi

if [ "$MACHINE" = "spitz" ]; then
    QEMU=qemu-system-arm
    if [ "$TYPE" = "ext3" ]; then
        echo $HDIMAGE
        HDIMAGE=`readlink -f $HDIMAGE`
        echo $HDIMAGE
        if [ ! -e "$HDIMAGE.qemudisk" ]; then
            echo "Adding a partition table to the ext3 image for use by QEMU, please wait..."
            poky-addptable2image $HDIMAGE $HDIMAGE.qemudisk
        fi
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M spitz -hda $HDIMAGE.qemudisk -portrait"
    fi
fi

if [ "$MACHINE" = "akita" ]; then
    QEMU=qemu-system-arm
    if [ "$TYPE" = "jffs2" ]; then
        HDIMAGE=`readlink -f $HDIMAGE`
        if [ ! -e "$HDIMAGE.qemuflash" ]; then
            echo "Converting raw image into flash image format for use by QEMU, please wait..."
	    raw2flash.akita < $HDIMAGE > $HDIMAGE.qemuflash
        fi
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M akita -mtdblock $HDIMAGE.qemuflash -portrait"
    fi
fi

if [ "$MACHINE" = "nokia800" ]; then
    QEMU=qemu-system-arm
    if [ "$TYPE" = "jffs2" ]; then
        HDIMAGE=`readlink -f $HDIMAGE`
        if [ ! -e "$HDIMAGE.qemuflash" ]; then
            echo "'Flashing' rootfs, please wait..."
            poky-nokia800-flashutil $HDIMAGE $HDIMAGE.qemuflash 
        fi
	KERNCMDLINE="root=/dev/mtdblock4 rootfstype=jffs2"
        QEMU_NETWORK_CMD="-net nic,model=usb,vlan=0 $QEMU_TAP_CMD"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M n800 -mtdblock $HDIMAGE.qemuflash -serial vc -m 130 -serial vc -serial vc -serial vc -usb -usbdevice net:0"
    fi
fi

if [ "$MACHINE" = "nokia800-maemo" ]; then
    QEMU=qemu-system-arm
    if [ "$TYPE" = "jffs2" ]; then
        HDIMAGE=`readlink -f $HDIMAGE`
        if [ ! -e "$HDIMAGE.qemuflash" ]; then
            if [ ! -e "$HDIMAGE.initfs" ]; then
                echo "Error, $HDIMAGE.initfs must exist!"
                return
            fi
            if [ ! -e "$HDIMAGE.config" ]; then
                echo "Error, $HDIMAGE.config must exist!"
                echo "To generate it, take an n800 and cat /dev/mtdblock1 > $HDIMAGE.config"
                return
            fi
            echo "'Flashing' config partition, please wait..."
            poky-nokia800-flashutil $HDIMAGE.config $HDIMAGE.qemuflash config
            echo "'Flashing' initfs, please wait..."
            poky-nokia800-flashutil $HDIMAGE.initfs $HDIMAGE.qemuflash initfs
            echo "'Flashing' rootfs, please wait..."
            poky-nokia800-flashutil $HDIMAGE $HDIMAGE.qemuflash 
        fi
	KERNCMDLINE=""
        QEMU_NETWORK_CMD="-net nic,model=usb,vlan=0 $QEMU_TAP_CMD"
        QEMUOPTIONS="$QEMU_NETWORK_CMD -M n800 -mtdblock $HDIMAGE.qemuflash -serial vc -m 130 -serial vc -serial vc -serial vc -usb -usbdevice net:0 -show-cursor"
    fi
fi

if [ "x$QEMUOPTIONS" = "x" ]; then
    echo "Error: Unable to support this combination of options"
    return
fi

SDKDIR="/usr/local/poky/eabi-glibc"
if [ "$MACHINE" = "qemuarm" -o "$MACHINE" = "spitz" -o "$MACHINE" = "borzoi" -o "$MACHINE" = "akita" -o "$MACHINE" = "nokia800" ]; then
    SDKPATH="$SDKDIR/arm/arm-poky-linux-gnueabi/bin:$SDKDIR/arm/bin"
fi

if [ "$MACHINE" = "qemux86" ]; then
    SDKPATH="$SDKDIR/i586/i586-poky-linux/bin:$SDKDIR/i586/bin"
fi
PATH=$CROSSPATH:$SDKPATH:$PATH

QEMUBIN=`which $QEMU`

if [ ! -x "$QEMUBIN" ]; then
    echo "Error: No QEMU binary '$QEMU' could be found."
    return
fi

function _quit() {
    if [ -n "$PIDFILE" ]; then
        #echo kill `cat $PIDFILE`
        kill `cat $PIDFILE`
    fi
    return
}

DISTCCD=`which distccd`
PIDFILE=""

trap _quit INT TERM QUIT

if [ -x "$DISTCCD" ]; then
    echo "Starting distccd..."
    PIDFILE=`mktemp`
    $DISTCCD --allow 192.168.7.2 --daemon --pid-file $PIDFILE &
else
    echo "Warning: distccd not present, no distcc support loaded."
fi

echo "Running $QEMU using sudo..."
echo $QEMUBIN -kernel $ZIMAGE $QEMUOPTIONS $SERIALOPTS $* --append "$KERNCMDLINE"
sudo $QEMUBIN -kernel $ZIMAGE $QEMUOPTIONS $SERIALOPTS $* --append "$KERNCMDLINE" || /bin/true

trap - INT TERM QUIT
return
