#!/bin/bash

#
# uBoot configuration for Debian on the Openmoko FreeRunner (GTA02)
#
# Copyright 2008 Joachim Breitner <nomeata@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# 18.08.08: * default boot in quiet-mode for speed
#           * added option to boot without quiet-mode for debugging
# (Rorschach)
# 
# 20.08.08: * changing the script so it can be run from inside debian-booted-freerunner
#	    * Probably that can be put as an option
#	    *     -whether to use dfu_util for reading/writing uBoot
#	    *	  -or to use the utilities on the phone itself and read/write uboot in booted phone
# (Vikas)

set -e

echo "This will configure your uboot environment to boot a Debian installation"
echo "It also adds a menu entry to boot the image installed in flash"
echo "Make sure this script runs as root, that uBoot is running on your Freerunner"
echo

tmpdir=$(mktemp -d -t uboot-env.XXXXXXXXXX)
trap "rm -f $tmpdir/env.orig $tmpdir/env.new $tmpdir/uboot_menu.in ; rmdir $tmpdir" EXIT

cd $tmpdir
cat > uboot_menu.in <<'__END__'
bootcmd=
  setenv bootargs
    ${bootargs_base} ${mtdparts}
    rootfstype=ext2 root=/dev/mmcblk0p2 quiet rootdelay=5;
  mmcinit;
  ext2load mmc 1 0x32000000 ${sd_image_name};
  bootm 0x32000000

menu_1=
  Boot without quiet-mode:
  setenv bootargs
    ${bootargs_base} ${mtdparts}
    rootfstype=ext2 root=/dev/mmcblk0p2 rootdelay=5;
  mmcinit;
  ext2load mmc 1 0x32000000 ${sd_image_name};
  bootm 0x32000000

menu_2=
  Boot from Flash:
  setenv bootargs
    ${bootargs_base} ${mtdparts};
  nand read.e 0x32000000 kernel 0x200000;
  bootm 0x32000000

menu_3=
  Reboot:
  reset

menu_4=
  Power off:
  neo1973 power-off

menu_5=
  Set console to USB:
  setenv stdin usbtty;
  setenv stdout usbtty;
  setenv stderr usbtty

menu_6=
  Set console to serial:
  setenv stdin serial;
  setenv stdout serial;
  setenv stderr serial

menu_7=

menu_8=
__END__

# device-specific values
UBOOT_ENV_SIZE=262144

echo " * Checking for a sane MTD configuration"
if ! fgrep mtd2 < /proc/mtd |fgrep -q "u-boot_env"; then
	echo "E: U-Boot environment not found at expected location in mtd2"
	exit 1
fi

echo " * Dumping current U-Boot environment"
#dd if=/dev/mtdblock2 of=\$INST_DIR/root/env.orig
nanddump /dev/mtd2 -o -b -f /root/env.orig
if [ ! \$(wc -c /root/env.orig | awk '{print \$1}') -eq $UBOOT_ENV_SIZE ]; then
        echo "Environment did not have the expected size of $UBOOT_ENV_SIZE"
        exit 1
fi

echo " * Merging Debian menu entries into U-Boot environment (can take up to 70s)"
uboot-envedit -i /root/env.orig -f /root/uboot_menu.in -o /root/env.new
if [ ! \$(wc -c /root/env.new | awk '{print \$1}') -eq $UBOOT_ENV_SIZE ]; then
        echo "Newly created environment did not have the expected size of $UBOOT_ENV_SIZE"
        exit 1
fi

echo " * Writing back U-Boot environment"
flash_eraseall /dev/mtd2
nandwrite /dev/mtd2 /root/env.new

echo "I: U-Boot environment modified"