001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045 print_usage () {
046 echo "Usage: $0 [X:|image] path/to/loader option path/to/modules..."
047 echo " where X: is a valid floppy drive on your computer"
048 echo " where image is any file name"
049 exit 1
050 }
051
052 grub_dirs_common="/usr/local/share/grub/i386-freebsd /usr/local/share/grub/i386-pc /usr/share/grub/i386-pc /usr/lib/grub/i386-pc /usr/local/grub /usr/share/grub/i386-redhat /usr/local/src/grub-0.5.94 $HOME/share/grub/i386-pc/"
053 sbin_grub_path="/usr/local/sbin /usr/sbin /sbin $HOME/sbin"
054
055 PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
056 export PATH
057
058 MTOOLSRC=mtoolsrc
059 export MTOOLSRC
060
061
062 FLOPPY_DRIVE=A:
063 IMG_FNAME=fd.img
064
065
066
067
068 init_image () {
069 echo "Initialize disk image $IMG_FILE..."
070 if [ ! -f $IMG_FNAME ] ; then
071 dd if=/dev/zero of=$IMG_FNAME bs=18k count=80 1>/dev/null 2>&1
072 fi
073
074 rm -f $MTOOLSRC
075 echo "drive u: file=\"$IMG_FNAME\" 1.44M filter" > $MTOOLSRC
076
077 if mformat U: ; then : ; else
078 rm -f $MTOOLSRC
079 echo "drive u: file=\"$IMG_FNAME\" 1.44M" > $MTOOLSRC
080 if mformat U: ; then : ; else
081 rm -f $MTOOLSRC
082 echo "drive u: file=\"$IMG_FNAME\"" > $MTOOLSRC
083 mformat U:
084 fi
085 fi
086 }
087
088
089
090
091
092 init_floppy () {
093 echo "Formatting floppy..."
094 mformat $FLOPPY_DRIVE || exit 1
095 }
096
097
098 lookup_grub () {
099
100 for d in $grub_dirs_common ; do
101 if [ -d $d ] ; then
102 GRUBDIR=$d
103 break
104 fi
105 done
106
107
108 if [ ! -d "$GRUBDIR" ] ; then
109 GRUBDIR=`whereis grub | awk '{ print "find "$3" -name stage2" }' | sh | xargs dirname 2>/dev/null`
110 fi
111
112
113 if [ ! -d "$GRUBDIR" ] ; then
114 GRUBDIR=`locate stage2 | head -1 | xargs dirname 2>/dev/null`
115 fi
116
117
118 for d in $sbin_grub_path ; do
119 if [ -x $d/grub ] ; then
120 SBIN_GRUB=$d/grub
121 break
122 fi
123 done
124
125 if [ -d "$GRUBDIR" -a -x "$SBIN_GRUB" ] ; then
126 echo "Found correct grub installation in $GRUBDIR"
127 echo "Found correct /sbin/grub at $SBIN_GRUB"
128 else
129 echo "Couldn't find a correct grub installation."
130 exit 1
131 fi
132 }
133
134
135
136
137
138 setup_disk () {
139 echo "Setup destination disk..."
140
141 mmd $1/boot
142 mmd $1/boot/grub
143
144 if [ -d $GRUBDIR/stage1 ] ; then
145 mcopy $GRUBDIR/stage1/stage1 $1/boot/grub/
146 mcopy $GRUBDIR/stage2/stage2 $1/boot/grub/
147 else
148 mcopy $GRUBDIR/stage1 $1/boot/grub/
149 mcopy $GRUBDIR/stage2 $1/boot/grub/
150 fi
151 mmd $1/system
152 mmd $1/modules
153
154 $SBIN_GRUB --batch --no-floppy <<EOT 1>/dev/null 2>/dev/null || exit 1
155 device (fd0) $IMG_FNAME
156 install (fd0)/boot/grub/stage1 (fd0) (fd0)/boot/grub/stage2 p (fd0)/boot/grub/menu.txt
157 quit
158 EOT
159 }
160
161
162
163
164
165
166
167
168 lookup_grub
169
170 dest="$1" ; shift
171 loader_fname="$1" ; shift
172 options="$1" ; shift
173 modules="$*"
174
175
176 case x$dest in
177 x*:)
178 drive=$dest
179 IMG_FNAME=$dest
180 FLOPPY_DRIVE=$dest
181 init_floppy
182 ;;
183 x*)
184 drive=U:
185 IMG_FNAME=$dest
186 init_image
187 ;;
188 esac
189
190
191 setup_disk $drive
192
193
194 mcopy -bo $loader_fname $drive/system/`basename $loader_fname`
195
196
197 rm -f menu.txt
198 cat <<EOF > menu.txt
199 timeout 0
200 default 0
201 title Simple OS
202 root (fd0)
203 kernel /system/`basename $loader_fname` $options
204 EOF
205
206
207 for f in $modules ; do
208 if [ ! -f $f ] ; then
209 echo "ERROR: module $f not correctly compiled in."
210 exit 1
211 fi
212 if ! mcopy -bo $f $drive/modules/`basename $f` ; then
213 echo "ERROR: module $f could not be transferred to floppy."
214 exit 1
215 fi
216 echo module /modules/`basename $f` >> menu.txt
217 done
218
219
220 mcopy -bo menu.txt $drive/boot/grub/