Home | History | Annotate | Line # | Download | only in embedded
      1 #!/bin/sh
      2 # $NetBSD: mkimage,v 1.97 2026/05/22 14:44:16 riastradh 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, which 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 
     49 set -e
     50 
     51 DIR="$(cd "$(dirname "$0")" && pwd)"
     52 PROG="$(basename "$0")"
     53 
     54 MAKE=${TOOL_MAKE:-make}
     55 DISKLABEL=${TOOL_DISKLABEL:-disklabel}
     56 FDISK=${TOOL_FDISK:-fdisk}
     57 GPT=${TOOL_GPT:-gpt}
     58 MAKEFS=${TOOL_MAKEFS:-makefs}
     59 MTREE=${TOOL_MTREE:-mtree}
     60 INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
     61 MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
     62 GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
     63 SED=${TOOL_SED:-sed}
     64 PWD_MKDB=${TOOL_PWD_MKDB:-pwd_mkdb}
     65 
     66 postfix=false
     67 [ "${MKPOSTFIX:-yes}" = no ] || postfix=true
     68 
     69 src="/usr/src"
     70 sets="base comp etc games gpufw man manhtml misc modules rescue tests text"
     71 xsets="xbase xcomp xetc xfont xserver"
     72 minfree="10%"
     73 bar="==="
     74 
     75 tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")"
     76 mnt="${tmp}/mnt"
     77 mkdir -p "${mnt}/etc" "${mnt}/dev"
     78 
     79 trap "cleanup" 0 1 2 3 15
     80 
     81 cleanup() {
     82 	case "$tmp" in
     83 	"${TMPDIR:-/tmp}/$PROG."*)	rm -fr "$tmp";;
     84 	esac
     85 }
     86 
     87 fail() {
     88 	IFS=' '
     89 	echo >&2 "${PROG}: $*"
     90 	exit 1
     91 }
     92 
     93 getsize() {
     94 	set -- $(ls -l $1)
     95 	echo $5
     96 }
     97 
     98 getsectors() {
     99 	case "$1" in
    100 	*g)
    101 		m=1073741824
    102 		v=${1%g}
    103 		;;
    104 	*m)
    105 		m=1048576
    106 		v=${1%m}
    107 		;;
    108 	*k)
    109 		m=1024
    110 		v=${1%k}
    111 		;;
    112 	*[0-9b])
    113 		m=1
    114 		v=${1%b}
    115 		;;
    116 	esac
    117 	echo $((m * v / 512))
    118 }
    119 
    120 minwrites_fstab_entries() {
    121 	$minwrites || return 0
    122 	cat << EOF
    123 tmpfs		/var/log		tmpfs	rw,union,-s32M
    124 tmpfs		/var/run		tmpfs	rw,union,-s1M
    125 tmpfs		/var/mail		tmpfs	rw,union,-s10M
    126 tmpfs		/var/chroot		tmpfs	rw,union,-s10M
    127 EOF
    128 	if $postfix; then
    129 	cat << EOF
    130 tmpfs		/var/spool/postfix	tmpfs	rw,union,-s20M
    131 tmpfs		/var/db/postfix		tmpfs	rw,union,-s1M
    132 EOF
    133 	fi
    134 }
    135 
    136 make_fstab_gpt() {
    137 	local boot=$1
    138 	local rootopts=
    139 	if $minwrites; then
    140 		rootopts=,log,nodevmtime
    141 	fi
    142 
    143 	cat > ${mnt}/etc/fstab << EOF
    144 # NetBSD /etc/fstab
    145 # See /usr/share/examples/fstab/ for more examples.
    146 NAME=${gpt_label_ffs:-netbsd-root}	/		ffs	rw,noatime${rootopts}	1 1
    147 NAME=${gpt_label_boot:-$boot}		/boot		msdos	rw	1 1
    148 ptyfs		/dev/pts	ptyfs	rw
    149 procfs		/proc		procfs	rw
    150 tmpfs		/var/shm	tmpfs	rw,-m1777,-sram%25
    151 EOF
    152 	minwrites_fstab_entries >> ${mnt}/etc/fstab
    153 }
    154 
    155 # From Richard Neswold's:
    156 # http://rich-tbp.blogspot.com/2013/03/netbsd-on-rpi-minimizing-disk-writes.html
    157 # Also for the postfix stuff below
    158 make_fstab_normal() {
    159 	local rootopts=
    160 	if $minwrites; then
    161 		rootopts=,nodevmtime
    162 	fi
    163 	cat > ${mnt}/etc/fstab << EOF
    164 # NetBSD /etc/fstab
    165 # See /usr/share/examples/fstab/ for more examples.
    166 ROOT.a		/			ffs	rw,noatime${rootopts}	1 1
    167 ROOT.e		/boot			msdos	rw				1 1
    168 ptyfs		/dev/pts		ptyfs	rw
    169 procfs		/proc			procfs	rw
    170 tmpfs		/tmp			tmpfs	rw,-s32M
    171 tmpfs		/var/shm		tmpfs	rw,-m1777,-sram%25
    172 EOF
    173 	minwrites_fstab_entries >> ${mnt}/etc/fstab
    174 }
    175 
    176 make_fstab_default() {
    177 	if $gpt; then
    178 		make_fstab_gpt "$@"
    179 	else
    180 		make_fstab_normal
    181 	fi
    182 	echo "./etc/fstab type=file uname=root gname=wheel mode=0644" \
    183 	    >> "$tmp/selected_sets"
    184 
    185 	# Missing mount points from fstab
    186 	echo "./proc type=dir uname=root gname=wheel mode=0755" \
    187 	    >> "$tmp/selected_sets"
    188 }
    189 
    190 usage() {
    191 	cat << EOF 1>&2
    192 Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
    193 
    194 -b	Boot only, no sets loaded
    195 -r	root device kind (sd, wd, ld)
    196 -d	Add the debug sets
    197 -m	Optimize the OS installation to minimize disk writes for SSDs
    198 -x	Load the X sets too, not just the base ones
    199 EOF
    200 	exit 1
    201 }
    202 
    203 # First pass for options to get the host and src directories
    204 OPTS="B:C:D:K:S:bc:dh:mr:s:x"
    205 while getopts "$OPTS" f
    206 do
    207 	case $f in
    208 	C)	conf="$OPTARG"
    209 		# Take -C relative to working directory; don't search
    210 		# in PATH.
    211 		case $conf in
    212 		/*)	;;
    213 		*)	conf=./"$conf";;
    214 		esac
    215 		;;
    216 	h)	h="$OPTARG";;
    217 	S)	src="$OPTARG";;
    218 	*)	;;
    219 	esac
    220 done
    221 
    222 if [ -z "$h" ]
    223 then
    224 	usage
    225 fi
    226 
    227 : ${conf:=${DIR}/conf/${h}.conf}
    228 if [ ! -f "$conf" ]
    229 then
    230 	echo $PROG: $conf is not present 1>&2
    231 	exit 1
    232 fi
    233 
    234 resize=false
    235 gpt=false
    236 gpt_hybrid=false
    237 fsize=8192
    238 bsize=65536
    239 ffsversion=1
    240 
    241 . "$conf"
    242 release="/usr/obj/${MACHINE}/release"
    243 
    244 selected_sets="$sets"
    245 dsets_p=false
    246 xsets_p=false
    247 minwrites=false
    248 rootdev=ld
    249 endian=
    250 
    251 OPTIND=1
    252 while getopts "$OPTS" f
    253 do
    254 	case $f in
    255 	B)	endian="-B $OPTARG";;
    256 	C)	;;
    257 	D)	release="$OPTARG";;
    258 	K)	kernel="$OPTARG";;
    259 	S)	;;
    260 	b)	bootonly=true;;
    261 	d)	dsets_p=true
    262 		selected_sets="$selected_sets debug"
    263 		if $xsets_p; then
    264 			selected_sets="$selected_sets xdebug"
    265 		fi
    266 		;;
    267 	c)	custom="$OPTARG";;
    268 	h)	;;
    269 	m)	minwrites=true;;
    270 	r)	rootdev="$OPTARG";;
    271 	s)	size="$OPTARG";;
    272 	x)	xsets_p=true
    273 		selected_sets="$selected_sets $xsets"
    274 		if $dsets_p; then
    275 		    selected_sets="$selected_sets xdebug"
    276 		fi
    277 		;;
    278 	*)	usage;;
    279 	esac
    280 done
    281 if [ -n "${MKREPRO_TIMESTAMP}" ]; then
    282 	timestamp_opt="-T ${MKREPRO_TIMESTAMP}"
    283 	volume_opt=",volume_id=$((${MKREPRO_TIMESTAMP} & 0xffff))"
    284 fi
    285 
    286 shift $(( $OPTIND - 1 ))
    287 if [ -n "$1" ]; then
    288 	# take the next argument as being the image name
    289 	image="$1"
    290 	shift
    291 fi
    292 
    293 case "$image" in
    294 *.gz)	compress=true; image="${image%.gz}";;
    295 *)	compress=false;;
    296 esac
    297 
    298 if [ -z "${bootonly}" ]; then
    299 	echo ${bar} configuring sets ${bar}
    300 	(cat "${release}/etc/mtree/NetBSD.dist"
    301 	for i in $selected_sets; do
    302 		s="${release}/etc/mtree/set.$i"
    303 		if [ -f "$s" ]; then
    304 			cat "$s"
    305 		fi
    306 	done) > "$tmp/selected_sets"
    307 fi
    308 
    309 make_fstab
    310 customize
    311 populate
    312 
    313 if [ ! "${MKDTB}" = "no" ]; then
    314 	#
    315 	# Part of the dtb set resides on the FAT partition (/boot/dtb/*), and
    316 	# the rest on FFS. Split it up here.
    317 	#
    318 	echo ${bar} Installing devicetree blobs ${bar}
    319 	mkdir -p "${mnt}/boot"
    320 	cp -r "${release}/boot/dtb" "${mnt}/boot/dtb"
    321 
    322 	mkdir -p "${mnt}/etc/mtree"
    323 	cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb"
    324 	echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets"
    325 
    326 	mkdir -p "${mnt}/var/db/obsolete"
    327 	cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb"
    328 	echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets"
    329 fi
    330 
    331 if [ -n "${msdosid}" ]; then
    332 	echo ${bar} Populating msdos filesystem ${bar}
    333 
    334 	case $(( ${msdosid} )) in
    335 	1)	fat_opt=",fat_type=12";;
    336 	4|6|14)	fat_opt=",fat_type=16";;
    337 	11|12)	fat_opt=",fat_type=32";;
    338 	*)	fat_opt=;;
    339 	esac
    340 	${MAKEFS} -N ${release}/etc -t msdos \
    341 	    -o "volume_label=NETBSD${fat_opt}${volume_opt}" ${timestamp_opt} \
    342 	    -O $((${init} / 2))m -s $((${boot} / 2))m \
    343 	    ${image} ${mnt}/boot
    344 fi
    345 
    346 if [ -z "${bootonly}" ]; then
    347 	echo ${bar} Populating ffs filesystem ${bar}
    348 	${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
    349 	    -O ${ffsoffset} ${timestamp_opt} \
    350 	    -o d=4096,f=${fsize},b=${bsize},v=${ffsversion} -b $((${extra}))m \
    351 	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
    352 fi
    353 
    354 if [ "${size}" = 0 ]; then
    355 	size="$(getsize "${image}")"
    356 	# Round up to a multiple of 4m and add 1m of slop.
    357 	alignunit=$((4*1024*1024))
    358 	alignsize=$((alignunit*((size + alignunit - 1)/alignunit)))
    359 	alignsize=$((alignsize + 1024*1024))
    360 	if [ "${size}" -lt "${alignsize}" ]; then
    361 		dd bs="$((alignsize - size))" count=1 if=/dev/zero conv=sync \
    362 			>> "${image}" 2> /dev/null
    363 		size="${alignsize}"
    364 	fi
    365 fi
    366 
    367 if $gpt; then
    368 	if $gpt_hybrid; then
    369 		gpt_flags="-H"
    370 	fi
    371 	gpt_flags="${gpt_flags} ${timestamp_opt}"
    372 	initsecs=$((${init} * 1024))
    373 	bootsecs=$((${boot} * 1024))
    374 	ffsstart="$(getsectors ${ffsoffset})"
    375 
    376 	echo ${bar} Clearing existing partitions ${bar}
    377 	${GPT} ${gpt_flags} ${image} destroy || true
    378 
    379 	echo ${bar} Creating partitions ${bar}
    380 	${GPT} ${gpt_flags} ${image} create ${gpt_create_flags}
    381 	${GPT} ${gpt_flags} ${image} add -b ${initsecs} -s ${bootsecs} -l ${gpt_label_boot:-EFI} -t ${gpt_boot_type:-efi}
    382 	${GPT} ${gpt_flags} ${image} set -a required -i 1
    383 	${GPT} ${gpt_flags} ${image} add -a 4m -b ${ffsstart} -l ${gpt_label_ffs:-netbsd-root} -t ffs
    384 	${GPT} ${gpt_flags} ${image} show
    385 	if $gpt_hybrid; then
    386 		echo ${bar} Creating hybrid MBR ${bar}
    387 		${FDISK} -f -g -u -0 -a -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
    388 		${FDISK} -f -g -u -3 -s 238/1/$((${initsecs} - 1)) -F ${image}
    389 		${FDISK} -F ${image}
    390 	fi
    391 else
    392 	if [ -n "${msdosid}" ]; then
    393 		echo ${bar} Running fdisk ${bar}
    394 		initsecs=$((${init} * 1024))
    395 		bootsecs=$((${boot} * 1024))
    396 		${FDISK} -f -i ${image}
    397 		${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
    398 		if [ -z "${bootonly}" ]; then
    399 			ffsstart="$(getsectors ${ffsoffset})"
    400 			imagesize="$(getsize "${image}")"
    401 			imagesecs="$(getsectors ${imagesize})"
    402 			ffssize="$(expr ${imagesecs} - ${ffsstart})"
    403 			${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
    404 		fi
    405 
    406 		echo ${bar} Adding label ${bar}
    407 		make_label > ${tmp}/label
    408 		${DISKLABEL} -m -R -F ${image} ${tmp}/label
    409 	elif [ -n "${netbsdid}" ]; then
    410 		echo ${bar} Adding label ${bar}
    411 		make_label > ${tmp}/label
    412 		${DISKLABEL} -m -R -F ${image} ${tmp}/label
    413 
    414 		echo ${bar} Running fdisk ${bar}
    415 		${FDISK} -f -i ${image}
    416 		${FDISK} -f -a -u -0 -s 169/${init} ${image}
    417 		${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
    418 	fi
    419 fi
    420 
    421 if $compress; then
    422 	echo ${bar} Compressing image ${bar}
    423 	rm -f "${image}.gz"
    424 	${GZIP_CMD} -n -9 ${image}
    425 	image="${image}.gz"
    426 fi
    427 
    428 cd "${IMAGEDIR}"
    429 ${CKSUM} -a MD5 "$(basename "${image}")" > MD5
    430 ${CKSUM} -a SHA512 "$(basename "${image}")" > SHA512
    431 
    432 echo ${bar} Image is ${image} ${bar}
    433