Home | History | Annotate | Line # | Download | only in common
buildfloppies.sh revision 1.6
      1 #!/bin/sh
      2 #
      3 # $NetBSD: buildfloppies.sh,v 1.6 2003/03/07 09:33:53 lukem Exp $
      4 #
      5 # Copyright (c) 2002-2003 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 bytes=$( ls -l "${floppy}" | awk '{print $5}' )
    114 blocks=$(( ${bytes} / 512 ))
    115 numdisks=$(( ( ${blocks} + ${floppysize} - 1 ) / ${floppysize} ))
    116 if [ -z "${maxdisks}" ]; then
    117 	maxdisks=${numdisks}
    118 fi
    119 
    120 if [ ${numdisks} -gt ${maxdisks} ]; then
    121 	excess=$(( ( ${blocks} - ${floppysize} * ${maxdisks} ) * 512 ))
    122 	echo 1>&2 \
    123 	    "$prog: Image is ${excess} bytes ($(( ${excess} / 1024 )) KB)"\
    124 	    "too big to fit on ${maxdisks} disk"$(plural ${maxdisks})
    125 	exit 1
    126 fi
    127 
    128 padsize=$(( ${floppysize} * ${maxdisks} ))
    129 padcount=$(( ${padsize} - ${blocks} ))
    130 if [ -n "${pad}" ]; then
    131 	echo \
    132 	    "Writing $(( ${padsize} * 512 )) bytes ($(( ${padsize} / 2 )) KB)" \
    133 	    "on ${numdisks} disk"$(plural ${numdisks})"," \
    134 	    "padded by $(( ${padcount} * 512 )) bytes" \
    135 	    "($(( ${padcount} / 2 )) KB)"
    136 else
    137 	echo "Writing ${bytes} bytes ($(( ${blocks} / 2 )) KB)"\
    138 	    "on ${numdisks} disk"$(plural ${numdisks})"," \
    139 	    "free space $(( ${padcount} * 512 )) bytes" \
    140 	    "($(( ${padcount} / 2 )) KB)"
    141 fi
    142 
    143 #	write disks
    144 #
    145 curdisk=1
    146 image=
    147 floppysize8k=$(( ${floppysize} / 16 ))
    148 while [ ${curdisk} -le ${numdisks} ]; do
    149 	image="${floppybase}${curdisk}${suffix}"
    150 	echo "Creating disk ${curdisk} to ${image}"
    151 	if [ ${curdisk} -eq 1 ]; then
    152 		seek=0
    153 		skip=0
    154 		: > ${image}
    155 	else
    156 		seek=1
    157 		skip=$(( (${curdisk} - 1) * (${floppysize8k} - 1) + 1 ))
    158 		echo USTARFS ${curdisk} > ${image}
    159 	fi
    160 	count=$(( ${floppysize8k} - ${seek} ))
    161 # echo 1>&2 " DEBUG: disk ${curdisk} seek=${seek} skip=${skip} count=${count}"
    162 	dd bs=8k conv=sync seek=${seek} skip=${skip} count=${count} \
    163 	    if=${floppy} of=${image} 2>/dev/null
    164 
    165 	curdisk=$(( ${curdisk} + 1 ))
    166 done
    167 
    168 #
    169 # XXX:	the old bootfloppy generation code used to zero the last 0.5k of the
    170 #	end of the image in single disk configs; that possibly trashed real
    171 #	data???
    172 #	is that functionality still required?
    173 #
    174 
    175 #	pad last disk if necessary
    176 #
    177 if [ -n "${pad}" ]; then
    178 	padseek=$(( ${floppysize} - ${padcount} ))
    179 # echo 1>&2 " DEBUG: padding ${image} with $(( ${padcount} * 512 )) at offset $(( ${padseek} * 512 ))"
    180 	dd seek=${padseek} count=${padcount} \
    181 	    if=/dev/zero of=${image} 2>/dev/null
    182 fi
    183 
    184 
    185 #	final status
    186 #
    187 echo "Final result:"
    188 ls -l ${floppybase}?${suffix}
    189 
    190 exit 0
    191