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