xref: /netbsd-src/distrib/utils/embedded/mkimage (revision 16dce51364ebe8aeafbae46bc5aa167b8115bc45)
1#!/bin/sh
2# $NetBSD: mkimage,v 1.69 2017/11/28 02:56:44 kre Exp $
3#
4# Copyright (c) 2013, 2014 The NetBSD Foundation, Inc.
5# All rights reserved.
6#
7# This code is derived from software contributed to The NetBSD Foundation
8# by Christos Zoulas.
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions
12# are met:
13# 1. Redistributions of source code must retain the above copyright
14#    notice, this list of conditions and the following disclaimer.
15# 2. Redistributions in binary form must reproduce the above copyright
16#    notice, this list of conditions and the following disclaimer in the
17#    documentation and/or other materials provided with the distribution.
18# 3. Neither the name of The NetBSD Foundation nor the names of its
19#    contributors may be used to endorse or promote products derived
20#    from this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32# POSSIBILITY OF SUCH DAMAGE.
33#
34
35#
36# Makes a bootable image for the host architecture given.
37# The host specific functions are pulled in from a /bin/sh script in the
38# "conf" directory, and is expected to provide the following shell
39# functions, which are called in the following order:
40#
41#  - make_fstab: Creates the host's /etc/fstab with / on ${rootdev}.
42#    If -m is given, a number of directories are put on a tmpfs RAM disk
43#  - customize: After unpacking the sets, this gets the system to
44#    a working state, e. g. by setting up /etc/rc.conf and /dev
45#  - populate: Add common goods like kernel and bootloader
46#  - make_label: Prints disklabel to stdout
47#
48
49set -e
50
51DIR="$(cd "$(dirname "$0")" && pwd)"
52PROG="$(basename "$0")"
53
54MAKE=${TOOL_MAKE:-make}
55DISKLABEL=${TOOL_DISKLABEL:-disklabel}
56FDISK=${TOOL_FDISK:-fdisk}
57MAKEFS=${TOOL_MAKEFS:-makefs}
58MTREE=${TOOL_MTREE:-mtree}
59INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
60MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
61GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
62
63src="/usr/src"
64sets="base comp etc games man misc modules tests text"
65xsets="xbase xcomp xetc xfont xserver"
66minfree="10%"
67bar="==="
68
69tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")"
70mnt="${tmp}/mnt"
71mkdir -p "${mnt}/etc" "${mnt}/dev"
72
73trap "cleanup" 0 1 2 3 15
74
75cleanup() {
76	case "$tmp" in
77	"${TMPDIR:-/tmp}/$PROG."*)	rm -fr "$tmp";;
78	esac
79}
80
81fail() {
82	IFS=' '
83	echo >&2 "${PROG}: $*"
84	exit 1
85}
86
87getsize() {
88	set -- $(ls -l $1)
89	echo $5
90}
91
92getsectors() {
93	case "$1" in
94	*g)
95		m=1073741824
96		v=${1%g}
97		;;
98	*m)
99		m=1048576
100		v=${1%m}
101		;;
102	*k)
103		m=1024
104		v=${1%k}
105		;;
106	*[0-9b])
107		m=1
108		v=${1%b}
109		;;
110	esac
111	echo $((m * v / 512))
112}
113
114usage() {
115	cat << EOF 1>&2
116Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
117
118-b	Boot only, no sets loaded
119-r	root device kind (sd, wd, ld)
120-d	Add the debug sets
121-m	Optimize the OS installation to mimimize disk writes for SSDs
122-x	Load the X sets too, not just the base ones
123EOF
124	exit 1
125}
126
127# First pass for options to get the host and src directories
128OPTS="B:D:K:S:bc:dh:mr:s:x"
129while getopts "$OPTS" f
130do
131	case $f in
132	h)	h="$OPTARG";;
133	S)	src="$OPTARG";;
134	*)	;;
135	esac
136done
137
138if [ -z "$h" ]
139then
140	usage
141fi
142
143if [ ! -f "${DIR}/conf/${h}.conf" ]
144then
145	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
146	exit 1
147fi
148
149resize=false
150
151. "${DIR}/conf/${h}.conf"
152release="/usr/obj/${MACHINE}/release"
153
154selected_sets="$sets"
155dsets_p=false
156xsets_p=false
157minwrites=false
158rootdev=ld
159endian=
160
161OPTIND=1
162while getopts "$OPTS" f
163do
164	case $f in
165	B)	endian="-B $OPTARG";;
166	D)	release="$OPTARG";;
167	K)	kernel="$OPTARG";;
168	S)	;;
169	b)	bootonly=true;;
170	d)	dsets_p=true
171		selected_sets="$selected_sets debug"
172		if $xsets_p; then
173			selected_sets="$selected_sets xdebug"
174		fi
175		;;
176	c)	custom="$OPTARG";;
177	h)	;;
178	m)	minwrites=true;;
179	r)	rootdev="$OPTARG";;
180	s)	size="$OPTARG";;
181	x)	xsets_p=true
182		selected_sets="$selected_sets $xsets"
183		if $dsets_p; then
184		    selected_sets="$selected_sets xdebug"
185		fi
186		;;
187	*)	usage;;
188	esac
189done
190
191shift $(( $OPTIND - 1 ))
192if [ -n "$1" ]; then
193	# take the next argument as being the image name
194	image="$1"
195	shift
196fi
197
198case "$image" in
199*.gz)	compress=true; image="${image%.gz}";;
200*)	compress=false;;
201esac
202
203if [ -z "${bootonly}" ]; then
204	echo ${bar} configuring sets ${bar}
205	(cat "${release}/etc/mtree/NetBSD.dist"
206	for i in $selected_sets; do
207		s="${release}/etc/mtree/set.$i"
208		if [ -f "$s" ]; then
209			cat "$s"
210		fi
211	done) > "$tmp/selected_sets"
212fi
213
214make_fstab
215customize
216populate
217
218if [ -n "${msdosid}" ]; then
219	echo ${bar} Populating msdos filesystem ${bar}
220	${MAKEFS} -N ${release}/etc -t msdos -o volume_label=NETBSD \
221	    -O $((${init} / 2))m -s $((${boot} / 2))m \
222	    ${image} ${mnt}/boot
223fi
224
225if [ -z "${bootonly}" ]; then
226	echo ${bar} Populating ffs filesystem ${bar}
227	${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
228	    -O ${ffsoffset} \
229	    -o d=4096,f=8192,b=65536 -b $((${extra}))m \
230	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
231fi
232
233if [ "${size}" = 0 ]; then
234	size="$(getsize "${image}")"
235fi
236newsize=$((${size} / 2 / 1024))
237compare=$((${newsize} * 2 * 1024))
238while [ "${compare}" != "${size}" ]
239do
240	size="$((size + size - compare))"
241	newsize="$((${size} / 2 / 1024))"
242	compare="$((${newsize} * 2 * 1024))"
243done
244
245if [ -n "${msdosid}" ]; then
246	echo ${bar} Running fdisk ${bar}
247	initsecs=$((${init} * 1024))
248	bootsecs=$((${boot} * 1024))
249	${FDISK} -f -i ${image}
250	${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
251	if [ -z "${bootonly}" ]; then
252		ffsstart="$(getsectors ${ffsoffset})"
253		imagesize="$(getsize "${image}")"
254		imagesecs="$(getsectors ${imagesize})"
255		ffssize="$(expr ${imagesecs} - ${ffsstart})"
256		${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
257	fi
258
259	echo ${bar} Adding label ${bar}
260	make_label > ${tmp}/label
261	${DISKLABEL} -R -F ${image} ${tmp}/label
262elif [ -n "${netbsdid}" ]; then
263	echo ${bar} Adding label ${bar}
264	make_label > ${tmp}/label
265	${DISKLABEL} -R -F ${image} ${tmp}/label
266
267	echo ${bar} Running fdisk ${bar}
268	${FDISK} -f -i ${image}
269	${FDISK} -f -a -u -0 -s 169/${init} ${image}
270	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
271fi
272
273if $compress; then
274	echo ${bar} Compressing image ${bar}
275	rm -f "${image}.gz"
276	${GZIP_CMD} -9 ${image}
277	image="${image}.gz"
278fi
279
280echo ${bar} Image is ${image} ${bar}
281