xref: /netbsd-src/distrib/utils/embedded/mkimage (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1#!/bin/sh
2# $NetBSD: mkimage,v 1.75 2020/07/17 15:16:34 jmcneill 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}
57GPT=${TOOL_GPT:-gpt}
58MAKEFS=${TOOL_MAKEFS:-makefs}
59MTREE=${TOOL_MTREE:-mtree}
60INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
61MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
62GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
63
64src="/usr/src"
65sets="base comp etc games man misc modules rescue tests text"
66xsets="xbase xcomp xetc xfont xserver"
67minfree="10%"
68bar="==="
69
70tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")"
71mnt="${tmp}/mnt"
72mkdir -p "${mnt}/etc" "${mnt}/dev"
73
74trap "cleanup" 0 1 2 3 15
75
76cleanup() {
77	case "$tmp" in
78	"${TMPDIR:-/tmp}/$PROG."*)	rm -fr "$tmp";;
79	esac
80}
81
82fail() {
83	IFS=' '
84	echo >&2 "${PROG}: $*"
85	exit 1
86}
87
88getsize() {
89	set -- $(ls -l $1)
90	echo $5
91}
92
93getsectors() {
94	case "$1" in
95	*g)
96		m=1073741824
97		v=${1%g}
98		;;
99	*m)
100		m=1048576
101		v=${1%m}
102		;;
103	*k)
104		m=1024
105		v=${1%k}
106		;;
107	*[0-9b])
108		m=1
109		v=${1%b}
110		;;
111	esac
112	echo $((m * v / 512))
113}
114
115usage() {
116	cat << EOF 1>&2
117Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
118
119-b	Boot only, no sets loaded
120-r	root device kind (sd, wd, ld)
121-d	Add the debug sets
122-m	Optimize the OS installation to mimimize disk writes for SSDs
123-x	Load the X sets too, not just the base ones
124EOF
125	exit 1
126}
127
128# First pass for options to get the host and src directories
129OPTS="B:D:K:S:bc:dh:mr:s:x"
130while getopts "$OPTS" f
131do
132	case $f in
133	h)	h="$OPTARG";;
134	S)	src="$OPTARG";;
135	*)	;;
136	esac
137done
138
139if [ -z "$h" ]
140then
141	usage
142fi
143
144if [ ! -f "${DIR}/conf/${h}.conf" ]
145then
146	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
147	exit 1
148fi
149
150resize=false
151gpt=false
152gpt_hybrid=false
153
154. "${DIR}/conf/${h}.conf"
155release="/usr/obj/${MACHINE}/release"
156
157selected_sets="$sets"
158dsets_p=false
159xsets_p=false
160minwrites=false
161rootdev=ld
162endian=
163
164OPTIND=1
165while getopts "$OPTS" f
166do
167	case $f in
168	B)	endian="-B $OPTARG";;
169	D)	release="$OPTARG";;
170	K)	kernel="$OPTARG";;
171	S)	;;
172	b)	bootonly=true;;
173	d)	dsets_p=true
174		selected_sets="$selected_sets debug"
175		if $xsets_p; then
176			selected_sets="$selected_sets xdebug"
177		fi
178		;;
179	c)	custom="$OPTARG";;
180	h)	;;
181	m)	minwrites=true;;
182	r)	rootdev="$OPTARG";;
183	s)	size="$OPTARG";;
184	x)	xsets_p=true
185		selected_sets="$selected_sets $xsets"
186		if $dsets_p; then
187		    selected_sets="$selected_sets xdebug"
188		fi
189		;;
190	*)	usage;;
191	esac
192done
193
194shift $(( $OPTIND - 1 ))
195if [ -n "$1" ]; then
196	# take the next argument as being the image name
197	image="$1"
198	shift
199fi
200
201case "$image" in
202*.gz)	compress=true; image="${image%.gz}";;
203*)	compress=false;;
204esac
205
206if [ -z "${bootonly}" ]; then
207	echo ${bar} configuring sets ${bar}
208	(cat "${release}/etc/mtree/NetBSD.dist"
209	for i in $selected_sets; do
210		s="${release}/etc/mtree/set.$i"
211		if [ -f "$s" ]; then
212			cat "$s"
213		fi
214	done) > "$tmp/selected_sets"
215fi
216
217make_fstab
218customize
219populate
220
221if [ ! "${MKDTB}" = "no" ]; then
222	#
223	# Part of the dtb set resides on the FAT partition (/boot/dtb/*), and
224	# the rest on FFS. Split it up here.
225	#
226	echo ${bar} Installing devicetree blobs ${bar}
227	mkdir -p "${mnt}/boot"
228	cp -r "${release}/boot/dtb" "${mnt}/boot/dtb"
229
230	mkdir -p "${mnt}/etc/mtree"
231	cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb"
232	echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets"
233
234	mkdir -p "${mnt}/var/db/obsolete"
235	cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb"
236	echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets"
237fi
238
239if [ -n "${msdosid}" ]; then
240	echo ${bar} Populating msdos filesystem ${bar}
241
242	case $(( ${msdosid} )) in
243	1)	fat_opt=",fat_type=12";;
244	4|6|14)	fat_opt=",fat_type=16";;
245	11|12)	fat_opt=",fat_type=32";;
246	*)	fat_opt=;;
247	esac
248	${MAKEFS} -N ${release}/etc -t msdos \
249	    -o "volume_label=NETBSD${fat_opt}" \
250	    -O $((${init} / 2))m -s $((${boot} / 2))m \
251	    ${image} ${mnt}/boot
252fi
253
254if [ -z "${bootonly}" ]; then
255	echo ${bar} Populating ffs filesystem ${bar}
256	${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
257	    -O ${ffsoffset} \
258	    -o d=4096,f=8192,b=65536 -b $((${extra}))m \
259	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
260fi
261
262if [ "${size}" = 0 ]; then
263	size="$(getsize "${image}")"
264fi
265newsize=$((${size} / 2 / 1024))
266compare=$((${newsize} * 2 * 1024))
267while [ "${compare}" != "${size}" ]
268do
269	size="$((size + size - compare))"
270	newsize="$((${size} / 2 / 1024))"
271	compare="$((${newsize} * 2 * 1024))"
272done
273
274if $gpt; then
275	if $gpt_hybrid; then
276		gpt_flags="-H"
277	fi
278	initsecs=$((${init} * 1024))
279	bootsecs=$((${boot} * 1024))
280	ffsstart="$(getsectors ${ffsoffset})"
281
282	echo ${bar} Clearing existing partitions ${bar}
283	${GPT} ${gpt_flags} ${image} destroy || true
284
285	echo ${bar} Creating partitions ${bar}
286	${GPT} ${gpt_flags} ${image} create ${gpt_create_flags}
287	${GPT} ${gpt_flags} ${image} add -b ${initsecs} -s ${bootsecs} -l ${gpt_label_boot:-EFI} -t ${gpt_boot_type:-efi}
288	${GPT} ${gpt_flags} ${image} set -a required -i 1
289	${GPT} ${gpt_flags} ${image} add -a 4m -b ${ffsstart} -l ${gpt_label_ffs:-netbsd-root} -t ffs
290	${GPT} ${gpt_flags} ${image} show
291	if $gpt_hybrid; then
292		echo ${bar} Creating hybrid MBR ${bar}
293		${FDISK} -f -g -u -0 -a -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
294		${FDISK} -f -g -u -3 -s 238/1/$((${initsecs} - 1)) -F ${image}
295		${FDISK} -F ${image}
296	fi
297else
298	if [ -n "${msdosid}" ]; then
299		echo ${bar} Running fdisk ${bar}
300		initsecs=$((${init} * 1024))
301		bootsecs=$((${boot} * 1024))
302		${FDISK} -f -i ${image}
303		${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
304		if [ -z "${bootonly}" ]; then
305			ffsstart="$(getsectors ${ffsoffset})"
306			imagesize="$(getsize "${image}")"
307			imagesecs="$(getsectors ${imagesize})"
308			ffssize="$(expr ${imagesecs} - ${ffsstart})"
309			${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
310		fi
311
312		echo ${bar} Adding label ${bar}
313		make_label > ${tmp}/label
314		${DISKLABEL} -R -F ${image} ${tmp}/label
315	elif [ -n "${netbsdid}" ]; then
316		echo ${bar} Adding label ${bar}
317		make_label > ${tmp}/label
318		${DISKLABEL} -R -F ${image} ${tmp}/label
319
320		echo ${bar} Running fdisk ${bar}
321		${FDISK} -f -i ${image}
322		${FDISK} -f -a -u -0 -s 169/${init} ${image}
323		${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
324	fi
325fi
326
327if $compress; then
328	echo ${bar} Compressing image ${bar}
329	rm -f "${image}.gz"
330	${GZIP_CMD} -9 ${image}
331	image="${image}.gz"
332fi
333
334echo ${bar} Image is ${image} ${bar}
335