#!/bin/sh
#
# Copyright (c) 1994-1998 The University of Utah and the Flux Group.
# 
# This file is part of the OSKit Linux Boot Loader, which is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License (GPL), version 2, as published by the Free Software
# Foundation (FSF).
# 
# The OSKit 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 GPL for more details.  You should have
# received a copy of the GPL along with the OSKit; see the file COPYING.  If
# not, write to the FSF, 59 Temple Place #330, Boston, MA 02111, USA.
#

# This little script uses GNU ld to build a Linux 16-bit compressed boot image
# from a set of boot modules.

bootdir=${BOOTDIR-/usr/flux/lib/boot}
as=${AS-i486-linux-as}
ld=${LD-i486-linux-ld}

files=
outfile=zImage
savetemps=

# Parse the command-line options.
until [ $# -eq 0 ]
do
	case "$1" in
		-o ) outfile="$2"; shift; shift;;
		-save-temps) savetemps="$1"; shift;;
		* ) files="$files $1"; shift;;
	esac
done

# Wrap each of the input files in a .o file.
# At the same time, build an assembly language module
# containing a table describing the boot modules.
echo >$outfile.mods.S ".text; .long 0xf00baabb"
for file in $files; do
	# Convert all non-alphanumeric chars to underscores.
	# The BFD binary input format will do the same thing
	# to produce the symbol names that it "wraps around" the input files.
	sym_name=`echo $file | sed -e 's,[^a-zA-Z0-9],_,g'`
	echo >>$outfile.mods.S ".long _binary_$sym_name""_start"
	echo >>$outfile.mods.S ".long _binary_$sym_name""_end"
	echo >>$outfile.mods.S ".long cmdline_$sym_name"
	echo >>$outfile.mods.S ".data"
	echo >>$outfile.mods.S "cmdline_$sym_name: .ascii \"$file\\0\""
	echo >>$outfile.mods.S ".text"
done
echo >>$outfile.mods.S ".long 0; .data; .align 4"

# Assemble the module vector file.
$as -c -o $outfile.mods.o $outfile.mods.S || exit 1

# Link the module vector file with the boot module files.
# Use the binary bfd backend for both the input bmod files and the output file.
# There is no meaning to "entry point address" in the binary backend,
# but the linker doesn't know that, so it looks for _start and warns
# when it doesn't find it (and falls back to zero).  To stifle the warning
# we can define the `_start' symbol on the command line; I think this
# works with all GNU ld versions.
$ld -Ttext 0 -defsym _start=0 -oformat binary -o $outfile.tmp \
	$outfile.mods.o -format binary $files -format default \
	|| exit 1

# Compress the whole output file as one big glob.
gzip -9 <$outfile.tmp >$outfile.tmp.gz

# Create the final boot image by tacking that onto the end of 'linuxboot.bin'.
cat $bootdir/linuxboot.bin $outfile.tmp.gz >$outfile

if test -z "$savetemps"; then
	rm -f $outfile.mods.S $outfile.mods.o $outfile.tmp $outfile.tmp.gz
fi

exit 0
