1#! /bin/sh
2#
3# $NetBSD: sunbootcd.sh,v 1.7 2018/09/16 21:41:34 kre Exp $
4#
5# Copyright (c) 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.
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#
20# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30# POSSIBILITY OF SUCH DAMAGE.
31#
32
33: ${SUNLABEL:=sunlabel}		# sunlabel(8)
34: ${CYLSIZE:=640}		# Cylinder size, in 512byte blocks
35
36PROGNAME=${0##*/}
37FORMAT="%-8s offset %4d, size %4d, file %s\n"
38
39
40usage()
41{
42	cat 1>&2 << _USAGE_
43Usage: ${PROGNAME} fsimage sun4 [sun4c [sun4m [sun3|sun4d [sun3x|sun4u]]]]
44	Combine file system partitions for Sun Microsystems, Inc. computers
45	into a CD-ROM file system image suitable for booting on the
46	following platforms:
47		NetBSD/sun3:	sun3, sun3x
48		NetBSD/sparc:	sun4, sun4c, sun4d, sun4m
49		NetBSD/sparc64:	sun4u
50	The architecture arguments must be bootable file system image
51	for that architecture, or \`-' if no entry is desired.
52	\`fsimage' is typically an iso9660 file system image, although
53	any type of file system can be used as long as the first 512
54	bytes of the image are not used.  \`fsimage' is modified, and
55	the additional partitions are added in order.  If the same
56	filename is used more than once for different architectures,
57	it will only be copied once.
58_USAGE_
59	exit 1
60}
61
62case "$#" in
63([23456])	;;
64(*)		usage;;
65esac
66
67for curfile
68do
69	[ "$curfile" = "-" ] && continue
70	if [ ! -f "$curfile" ]; then
71		echo 1>&2 "${PROGNAME}: ${curfile}: No such file."
72		exit 1
73	fi
74done
75
76ISOIMAGE="$1";	shift
77
78ISOSIZE=$( ls -l "${ISOIMAGE}" | awk '{print $5}' )
79ISOBLKS=$(( (${ISOSIZE} + 511) / 512 ))
80ISOCYLS=$(( (${ISOBLKS} + (${CYLSIZE} - 1)) / ${CYLSIZE} ))
81
82printf "${FORMAT}" "fsimage:" 0 ${ISOCYLS} "${ISOIMAGE}"
83
84ENDCYL=${ISOCYLS}
85curpart=0
86for curfile
87do
88	curpart=$(( ${curpart} + 1 ))
89	[ "$curfile" = "-" ] && continue
90
91	tpart=1
92	curoff=${ENDCYL}
93	while [ ${tpart} -lt ${curpart} ]; do
94		tfile=$(eval echo \$PART${tpart}FILE)
95		if [ "${curfile}" = "${tfile}" ]; then
96			curoff=$(eval echo \$PART${tpart}OFF)
97			break
98		fi
99		tpart=$(( ${tpart} + 1 ))
100	done
101
102	cursize=$( ls -l "${curfile}" | awk '{print $5}' )
103	curblks=$(( (${cursize} + 511) / 512 ))
104	curcyls=$(( (${curblks} + (${CYLSIZE} - 1)) / ${CYLSIZE} ))
105	printf "${FORMAT}" "Image ${curpart}:" ${curoff} ${curcyls} "${curfile}"
106
107	eval	PART${curpart}SIZE=${cursize} \
108		PART${curpart}BLKS=${curblks} \
109		PART${curpart}CYLS=${curcyls} \
110		PART${curpart}OFF=${curoff} \
111		PART${curpart}FILE="${curfile}"
112
113	if [ $curoff -eq $ENDCYL ]; then		# append ${curfile}
114		echo "    (appending ${curfile} to ${ISOIMAGE})"
115		dd if="${curfile}" of="${ISOIMAGE}" bs=${CYLSIZE}b \
116		    seek=${ENDCYL} conv=notrunc,sync 2>/dev/null
117		ENDCYL=$(( $ENDCYL + $curcyls ))
118	fi
119
120done
121
122printf "${FORMAT}" "Final:" 0 ${ENDCYL} "${ISOIMAGE}"
123
124${SUNLABEL} -nq "${ISOIMAGE}" << _partinfo_
125V nsect ${CYLSIZE}
126V nhead 1
127V rpm 300
128V pcyl ${ENDCYL}
129V ncyl ${ENDCYL}
130a 0 $(( ${ISOCYLS} * ${CYLSIZE} ))
131b ${PART1OFF:-0} $(( ${PART1CYLS:-0} * ${CYLSIZE} ))
132c ${PART2OFF:-0} $(( ${PART2CYLS:-0} * ${CYLSIZE} ))
133d ${PART3OFF:-0} $(( ${PART3CYLS:-0} * ${CYLSIZE} ))
134e ${PART4OFF:-0} $(( ${PART4CYLS:-0} * ${CYLSIZE} ))
135f ${PART5OFF:-0} $(( ${PART5CYLS:-0} * ${CYLSIZE} ))
136W
137_partinfo_
138