xref: /netbsd/distrib/common/buildfloppies.sh (revision bf9ec67e)
1#!/bin/sh
2#
3# $NetBSD: buildfloppies.sh,v 1.3 2002/05/04 01:34:37 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}
43prog=${0##*/}
44
45
46usage()
47{
48	cat 1>&2 << _USAGE_
49Usage: ${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
61plural()
62{
63	[ $1 -ne 1 ] && echo "s"
64}
65
66
67#	parse and check arguments
68#
69
70while 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
84done
85
86shift $(( ${OPTIND} - 1 ))
87[ $# -lt 3 ] && usage
88floppybase=$1
89floppysize=$2
90shift 2
91files=$*
92
93#	setup temp file, remove existing images
94#
95floppy=floppy.$$.tar
96trap "rm -f ${floppy}" 0 1 2 3			# EXIT HUP INT QUIT
97rm -f ${floppybase}?${suffix}
98
99
100#	create tar file
101#
102dd if=/dev/zero of=${floppy} bs=8k count=1 2>/dev/null
103${PAX} -w ${files} >> ${floppy} || exit 1
104	# XXX: use pax metafile and set perms?
105if [ -n "$instboot" ]; then
106	instboot=$( echo $instboot | sed -e s/@IMAGE@/${floppy}/ )
107	echo "Running instboot: ${instboot}"
108	eval ${instboot} || exit 1
109fi
110
111#	check size against available number of disks
112#
113set -- `ls -l ${floppy}`
114bytes=$5
115blocks=$(( ${bytes} / 512 ))
116numdisks=$(( (${blocks} - 1) / ${floppysize} + 1 ))
117if [ -z "${maxdisks}" ]; then
118	maxdisks=${numdisks}
119fi
120
121if [ ${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
127fi
128
129if [ -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)"
137else
138	echo "Writing ${bytes} bytes ($(( ${blocks} / 2 )) KB)"\
139	    "on ${numdisks} disk"$(plural ${numdisks})
140fi
141
142#	write disks
143#
144curdisk=1
145image=
146floppysize8k=$(( ${floppysize} / 16 ))
147while [ ${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 ))
165done
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#
176if [ -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
181fi
182
183
184#	final status
185#
186echo "Final result:"
187ls -l ${floppybase}?${suffix}
188
189exit 0
190