1#!/bin/sh
2
3usage() {
4	echo "Usage: makebootfloppy [-cd] [-base <directory>] [-preserve] [-image <target>]"
5	echo "-cd       : Creates a boot floppy capable of booting to a CD."
6	echo "            If not specified, the boot floppy will only be able to boot"
7	echo "            systems from hard drives."
8	echo "-base     : Specifies the base directory of the system you wish to make"
9	echo "            a boot floppy from. Defaults to /boot"
10	echo "-preserve : Leaves a copy of the floppy image in /tmp (only valid when"
11	echo "            used with the -cd option)."
12	echo "-image    : writes the floppy image to the specified file - this implies"
13	echo "            the -cd option."
14	exit $1
15}
16
17BASE=/boot
18CD=0
19PRESERVE=0
20IMAGE=/dev/disk/floppy/raw
21
22while [ "x$1" != "x" ] ; do
23	if [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then
24		usage 0
25	elif [ "$1" = "-cd" ] ; then
26		CD=1
27	elif [ "$1" = "-preserve" ] ; then
28		PRESERVE=1
29	elif [ "$1" = "-image" ] ; then
30		shift
31		IMAGE=$1
32		CD=1
33		if [ "x$1" = "x" ] ; then
34			echo "-image requires an argument."
35			usage 1
36		fi
37	elif [ "$1" = "-base" ] ; then
38		shift
39		BASE=$1
40		if [ "x$1" = "x" ] || [ ! -d $BASE ] ; then
41			echo "-base requires a directory argument."
42			usage 1
43		fi
44	else
45		echo "Invalid option: $1"
46		usage 1
47	fi
48	shift
49done
50
51if [ $CD = 1 ] ; then
52	rm -f /tmp/boot.tgz /tmp/boot.img
53
54	echo "Creating boot image..."
55
56	mkdir -p /tmp/system
57	cp $BASE/system/kernel_x86 /tmp/system/
58	oldCWD=$(pwd)
59
60	cd /tmp
61	tar chf /tmp/boot.tar system/kernel_x86
62	rm -r /tmp/system
63
64	cd $oldCWD
65	cd $BASE
66	pwd
67	tar rvhf /tmp/boot.tar \
68		system/add-ons/kernel/busses/ide \
69		system/add-ons/kernel/bus_managers \
70		system/add-ons/kernel/file_systems/bfs \
71		system/add-ons/kernel/generic \
72		system/add-ons/kernel/partitioning_systems \
73		system/add-ons/kernel/drivers/disk/scsi/scsi* \
74		system/add-ons/kernel/file_systems/bfs \
75		> /dev/null
76#		system/add-ons/kernel/boot \
77	gzip -c /tmp/boot.tar > /tmp/boot.tgz
78	rm /tmp/boot.tar
79	cd $oldCWD
80	if [ $? != 0 ] ; then
81		echo "Error creating boot floppy"
82		exit 1
83	fi
84
85	dd if=/dev/zero of=/tmp/boot.img bs=1k count=1440
86	if [ $? != 0 ] ; then
87		echo "Error creating temporary boot image"
88		exit 1
89	fi
90	dd if=$BASE/system/haiku_loader of=/tmp/boot.img conv=notrunc
91	dd if=/tmp/boot.tgz of=/tmp/boot.img bs=192k seek=1 conv=notrunc
92
93	echo "Writing boot image to "$IMAGE
94	dd if=/tmp/boot.img of=$IMAGE bs=72k
95	_retval=$?
96
97	if [ $PRESERVE = 0 ] ; then
98		rm -f /tmp/boot.tgz /tmp/boot.img
99	fi
100
101	if [ $_retval != 0 ] ; then
102		echo "Error creating boot floppy"
103		exit 1
104	fi
105else
106	# non-CD mode (only writes the bare boot loader)
107
108	echo "Writing boot loader..."
109	dd if=$BASE/system/haiku_loader of=$IMAGE bs=18k
110	if [ $? != 0 ] ; then
111		echo "Error creating boot floppy"
112		exit 1
113	fi
114
115	echo "Erasing old boot drivers from the floppy..."
116	dd if=/dev/zero of=$IMAGE bs=512 conv=notrunc seek=384 count=1
117	if [ $? != 0 ] ; then
118		echo "Error creating boot floppy"
119		exit 1
120	fi
121fi
122
123echo "Done!"
124exit 0
125