buildfloppies.sh revision 1.4 1 #!/bin/sh
2 #
3 # $NetBSD: buildfloppies.sh,v 1.4 2002/12/21 15:54:49 lukem Exp $
4 #
5 # Copyright (c) 2002 The NetBSD Foundation, Inc.
6 # All rights reserved.
7 #
8 # This code is derived from software contributed to The NetBSD Foundation
9 # by Luke Mewburn of Wasabi Systems.
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
13 # are met:
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 # 3. All advertising materials mentioning features or use of this software
20 # must display the following acknowledgement:
21 # This product includes software developed by the NetBSD
22 # Foundation, Inc. and its contributors.
23 # 4. Neither the name of The NetBSD Foundation nor the names of its
24 # contributors may be used to endorse or promote products derived
25 # from this software without specific prior written permission.
26 #
27 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 # POSSIBILITY OF SUCH DAMAGE.
38 #
39
40 # set defaults
41 #
42 : ${PAX=pax}
43 prog=${0##*/}
44
45
46 usage()
47 {
48 cat 1>&2 << _USAGE_
49 Usage: ${prog} [-i instboot] [-m max] [-p] [-s suffix] base size file [...]
50 -i instboot run instboot to install a bootstrap on @IMAGE@
51 -m max maximum number of floppies to build
52 -p pad last floppy to floppy size
53 -s suffix suffix for floppies
54 base basename of generated floppies
55 size size of a floppy in 512 byte blocks
56 file [...] file(s) to build
57 _USAGE_
58 exit 1
59 }
60
61 plural()
62 {
63 [ $1 -ne 1 ] && echo "s"
64 }
65
66
67 # parse and check arguments
68 #
69
70 while getopts i:m:ps: opt; do
71 case ${opt} in
72 i)
73 instboot=${OPTARG} ;;
74 m)
75 maxdisks=${OPTARG} ;;
76 p)
77 pad=1 ;;
78 s)
79 suffix=${OPTARG} ;;
80 \?|*)
81 usage
82 ;;
83 esac
84 done
85
86 shift $(( ${OPTIND} - 1 ))
87 [ $# -lt 3 ] && usage
88 floppybase=$1
89 floppysize=$2
90 shift 2
91 files=$*
92
93 # setup temp file, remove existing images
94 #
95 floppy=floppy.$$.tar
96 trap "rm -f ${floppy}" 0 1 2 3 # EXIT HUP INT QUIT
97 rm -f ${floppybase}?${suffix}
98
99
100 # create tar file
101 #
102 dd if=/dev/zero of=${floppy} bs=8k count=1 2>/dev/null
103 ${PAX} -O -w ${files} >> ${floppy} || exit 1
104 # XXX: use pax metafile and set perms?
105 if [ -n "$instboot" ]; then
106 instboot=$( echo $instboot | sed -e s/@IMAGE@/${floppy}/ )
107 echo "Running instboot: ${instboot}"
108 eval ${instboot} || exit 1
109 fi
110
111 # check size against available number of disks
112 #
113 set -- `ls -l ${floppy}`
114 bytes=$5
115 blocks=$(( ${bytes} / 512 ))
116 numdisks=$(( (${blocks} - 1) / ${floppysize} + 1 ))
117 if [ -z "${maxdisks}" ]; then
118 maxdisks=${numdisks}
119 fi
120
121 if [ ${numdisks} -gt ${maxdisks} ]; then
122 excess=$(( ( ${blocks} - ${floppysize} * ${maxdisks} ) * 512 ))
123 echo 1>&2 \
124 "$prog: Image is ${excess} bytes ($(( ${excess} / 1024 )) KB)"\
125 "too big to fit on ${maxdisks} disk"$(plural ${maxdisks})
126 exit 1
127 fi
128
129 if [ -n "${pad}" ]; then
130 padsize=$(( ${floppysize} * ${maxdisks} ))
131 padcount=$(( ${padsize} - ${blocks} ))
132 echo \
133 "Writing $(( ${padsize} * 512 )) bytes ($(( ${padsize} / 2 )) KB)" \
134 "on ${numdisks} disk"$(plural ${numdisks})"," \
135 "padded by $(( ${padcount} * 512 )) bytes" \
136 "($(( ${padcount} / 2 )) KB)"
137 else
138 echo "Writing ${bytes} bytes ($(( ${blocks} / 2 )) KB)"\
139 "on ${numdisks} disk"$(plural ${numdisks})
140 fi
141
142 # write disks
143 #
144 curdisk=1
145 image=
146 floppysize8k=$(( ${floppysize} / 16 ))
147 while [ ${curdisk} -le ${numdisks} ]; do
148 image="${floppybase}${curdisk}${suffix}"
149 echo "Creating disk ${curdisk} to ${image}"
150 if [ ${curdisk} -eq 1 ]; then
151 seek=0
152 skip=0
153 : > ${image}
154 else
155 seek=1
156 skip=$(( (${curdisk} - 1) * (${floppysize8k} - 1) + 1 ))
157 echo USTARFS ${curdisk} > ${image}
158 fi
159 count=$(( ${floppysize8k} - ${seek} ))
160 # echo 1>&2 " DEBUG: disk ${curdisk} seek=${seek} skip=${skip} count=${count}"
161 dd bs=8k conv=sync seek=${seek} skip=${skip} count=${count} \
162 if=${floppy} of=${image} 2>/dev/null
163
164 curdisk=$(( ${curdisk} + 1 ))
165 done
166
167 #
168 # XXX: the old bootfloppy generation code used to zero the last 0.5k of the
169 # end of the image in single disk configs; that possibly trashed real
170 # data???
171 # is that functionality still required?
172 #
173
174 # pad last disk if necessary
175 #
176 if [ -n "${pad}" ]; then
177 padseek=$(( ${floppysize} - ${padcount} ))
178 # echo 1>&2 " DEBUG: padding ${image} with $(( ${padcount} * 512 )) at offset $(( ${padseek} * 512 ))"
179 dd seek=${padseek} count=${padcount} \
180 if=/dev/zero of=${image} 2>/dev/null
181 fi
182
183
184 # final status
185 #
186 echo "Final result:"
187 ls -l ${floppybase}?${suffix}
188
189 exit 0
190