#!/bin/bash

SYSCON=ttyS2,115200

echo "= Copying loader scripts to boot partition"

mkdir -p /tmp/boot/boot
cat > /tmp/boot/boot/olpc.fth <<'EOF'
\ OLPC XO boot script

: enable-output ( -- )
  " /" find-device " model" get-property if
    false  else   " olpc,XO" 2swap sindex
  then if  " visible unfreeze" eval  then
;

: check-ofw-version ( -- )
   " /" find-device " compatible" get-property
   abort" No compatible property on /" ( -- compatible$ )

   \ Good compatible strings
   " mrvl,mmp2"    2over sindex  if  2drop exit  then
   " marvell,mmp3" 2over sindex  if  2drop exit  then

   \ Try to be helpful
   cr
   " olpc,xo-1.75" 2swap sindex if
     ." Firmware Q4E00 or newer is needed to boot a Devicetree enabled kernel." cr
     cr
     ." One way to update is to copy http://dev.laptop.org/~quozl/q4e00ja.rom" cr
     ." to a FAT partition on a USB flash stick and run ""flash u:\q4e00ja.rom""" cr
     " show-sad" eval
   else
     ." This hardware or firmware revision is not supported. Sorry." cr
   then
   cr
   ." Aborting boot." cr
   abort
;

: unix-to-ofw-path ( $ -- $ )
   " last:" 2swap $cat2
   2dup
   begin dup 0<> while
      1 -
      2dup + dup c@
      ascii / = if ascii \ swap c! else drop then
   repeat
   2drop
;

: load-extlinux-conf ( $ -- )
   $read-file if exit then
   2dup

   0 0 to boot-device
   0 0 to ramdisk
   0 0 to boot-file

   begin
      linefeed left-parse-string

      -leading bl left-parse-string
      2dup " label" $=  if
         4drop
         \ If we see a label, but already have boot-device set, then
         \ we drop the rest instead of proceeding to read another entry
         boot-device swap drop 0<>  if  drop 0  then
      else
         2dup " kernel" $=  if  2over unix-to-ofw-path to boot-device  else
         2dup " linux"  $=  if  2over unix-to-ofw-path to boot-device  else
         2dup " initrd" $=  if  2over unix-to-ofw-path to ramdisk      else
         2dup " append" $=  if  2over                  to boot-file
         then then then then
         4drop
      then

      dup 0 =
   until
   2drop
   free-mem
;

enable-output
check-ofw-version
" last:\extlinux\extlinux.conf" load-extlinux-conf
boot
EOF
