From d5a3525a3e2a8cf2b61856bff9a392821e3e0371 Mon Sep 17 00:00:00 2001
From: James Cameron <quozl@laptop.org>
Date: Wed, 29 Oct 2008 09:32:31 +1100
Subject: [PATCH] bootable attempt

---
 README        |  29 ++++++++++--
 README.approx |   5 +-
 mkbootable.sh | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 153 insertions(+), 8 deletions(-)
 create mode 100755 mkbootable.sh

diff --git a/README b/README
index 317f4fd..843d3c1 100644
--- a/README
+++ b/README
@@ -1,10 +1,15 @@
 xodist - Debian image builder for OLPC XO
 
+Why is this needed?  Because the OLPC XO has exceptional and flexible
+firmware support unlike an ordinary PC.  The usual Debian boot CDs do
+not work, yet.  xodist builds a Debian system image in the format
+required by the firmware.
+
 See *.packages for other image flavours, and change "kde" below to the
 flavour.  See README.approx before starting if you would like to cache
 the packages downloaded.
 
-How to use xodist to generate a Debian KDE image:
+How to use xodist to generate a Debian KDE image for an OLPC XO:
 
 1.  build the root filesystem,
 
@@ -16,10 +21,24 @@ How to use xodist to generate a Debian KDE image:
 
 3.  copy the files to a thumb drive,
 
-	cp kde.{dat,crc,img} /mnt/
+	sudo ./mkbootable.sh --output /mnt kde
+
+4.  ensure the XO is unlocked, by obtaining a Developer Key, and using
+the OpenFirmware command,
+
+	disable-security
+
+5.  shut down the XO,
+
+6.  insert the thumb drive,
+
+7.  power up, when prompted press Enter, and the image will be copied
+to the NAND flash, and then booted,
 
-4.  boot an XO into OFW (note: OFW q2e14 or newer is required), then type:
+Warning: this erases everything on the XO, including any Developer
+Key, but if you have followed step 4 above that should not matter so
+much.
 
-        update-nand u:\kde.img
+8.  remove the thumb drive before the next reboot or power up.
 
-Warning: this erases everything on the XO.
+Note: Firmware Q2E14 or newer is required.
diff --git a/README.approx b/README.approx
index f93bb4a..7b67cf4 100644
--- a/README.approx
+++ b/README.approx
@@ -16,10 +16,9 @@ security        http://security.debian.org/
 
 	% sudo /etc/init.d/approx restart
 
-4.  during the creation of the root filesystem described in README,
-add the --mirror option, like this:
+4.  follow README, but during the creation of the root filesystem, add
+the --mirror option, like this:
 
         sudo ./initchroot.sh \
             --package-list kde.packages \
             --mirror http://localhost:9999/debian kde.root
-
diff --git a/mkbootable.sh b/mkbootable.sh
new file mode 100755
index 0000000..ec6b076
--- /dev/null
+++ b/mkbootable.sh
@@ -0,0 +1,127 @@
+#!/bin/sh
+set -e
+
+IMAGE_DEFAULT=
+INTERACTIVE=yes
+OUTPUT=${IMAGE_DEFAULT}.bootable
+
+. functions.sh
+
+images()
+{
+	echo "Available images:" 1>&2
+	echo -n "  " 1>&2
+	ls *.img | cut -f1 -d. | tr '\n' ' ' 1>&2
+}
+
+usage()
+{
+	echo "" 1>&2
+	echo "Usage: $0 [<options>] <image-name>" 1>&2
+	echo "" 1>&2
+	echo "Options:" 1>&2
+	echo "  --no-interactive    in the generated media, during boot, do not prompt" 1>&2
+	echo "  --output dir        directory in which to create the structure" 1>&2
+	echo "" 1>&2
+	echo "Argument:" 1>&2
+	echo "  <image-name>       an image name, default $IMAGE_DEFAULT" 1>&2
+	echo "" 1>&2
+	images
+	echo "" 1>&2
+	exit 1
+}
+
+if test $# == 0; then
+	usage
+fi
+
+IMAGE="$IMAGE_DEFAULT"
+while test $# != 0
+do
+	case $1 in
+	--no-interactive)
+		INTERACTIVE=no
+		;;
+	--interactive)
+		INTERACTIVE=yes
+		;;
+	--output)
+		OUTPUT=$2
+		shift
+		;;
+	*)
+		if [ "$#" != "1" ]; then
+			echo "Unknown option $1" 1>&2
+			usage
+		else
+			IMAGE="$1"
+			if [ "${OUTPUT}" == "" ]; then
+				OUTPUT=${IMAGE}.bootable
+			fi
+		fi
+		;;
+	esac
+	shift
+done
+
+if ! test -f ${IMAGE}.img; then
+	echo "*** No such image ${IMAGE}"
+	exit 1
+fi
+
+if ! test -d ${OUTPUT}; then
+	mkdir ${OUTPUT}
+fi
+
+cp ${IMAGE}.{dat,crc,img} ${OUTPUT}
+
+mkdir -p ${OUTPUT}/boot
+
+# create essential header required by OpenFirmware
+cat <<EOF >${OUTPUT}/boot/olpc.fth
+\ OLPC boot script
+
+cr
+." This is a debxo bootable install script." cr
+cr
+." `md5sum ${IMAGE}.img`" cr
+." `md5sum ${IMAGE}.dat`" cr
+." `md5sum ${IMAGE}.crc`" cr
+cr
+EOF
+
+# create either an interactive or non-interactive section
+case $INTERACTIVE in
+	no)
+		cat <<EOF >>${OUTPUT}/boot/olpc.fth
+
+\ --no-interactive was used, so we do not prompt
+." Erasing everything here and installing debxo ${IMAGE}" cr
+EOF
+		;;
+	yes|*)
+		cat <<EOF >>${OUTPUT}/boot/olpc.fth
+
+\ --interactive was used, so we prompt before erasing
+." Power off to abort, or" cr
+." press Enter to erase everything here and install debxo ${IMAGE} ?"
+begin
+    key 13 =
+until
+EOF
+		;;
+esac
+
+cat <<EOF >>${OUTPUT}/boot/olpc.fth
+
+cr
+." Starting"
+
+\ erase the NAND flash and fill it with the image
+update-nand u:\\${IMAGE}.img
+
+\ boot from the NAND flash
+boot n:\boot\olpc.fth
+EOF
+
+# TODO: avoid reflashing if already flashed
-- 
2.39.5