Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.64.4.1
      1      1.31  jmcneill #!/bin/sh
      2  1.64.4.1       snj # $NetBSD: mkimage,v 1.64.4.1 2017/07/18 19:13:07 snj 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.64.4.1       snj MAKE=${TOOL_MAKE:-make}
     55      1.21  christos DISKLABEL=${TOOL_DISKLABEL:-disklabel}
     56      1.24  christos FDISK=${TOOL_FDISK:-fdisk}
     57      1.21  christos MAKEFS=${TOOL_MAKEFS:-makefs}
     58      1.25  jmcneill MTREE=${TOOL_MTREE:-mtree}
     59      1.45  christos INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
     60      1.59  christos MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
     61      1.44       ast GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
     62      1.21  christos 
     63      1.17  christos src="/usr/src"
     64      1.17  christos sets="base comp etc games man misc modules text"
     65      1.17  christos xsets="xbase xcomp xetc xfont xserver" 
     66      1.17  christos minfree="10%"
     67      1.17  christos bar="==="
     68      1.17  christos 
     69      1.17  christos tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
     70      1.17  christos mnt="${tmp}/mnt"
     71      1.46  christos mkdir -p "${mnt}/etc" "${mnt}/dev"
     72      1.23  christos 
     73      1.17  christos trap "cleanup" 0 1 2 3 15
     74      1.17  christos 
     75      1.17  christos cleanup() {
     76      1.17  christos 	case "$tmp" in
     77      1.17  christos 	/tmp/$PROG.*)	rm -fr "$tmp";;
     78      1.17  christos 	esac
     79      1.17  christos }
     80       1.1       agc 
     81      1.17  christos getsize() {
     82      1.17  christos 	set -- $(ls -l $1)
     83      1.17  christos 	echo $5
     84       1.1       agc }
     85       1.1       agc 
     86      1.63  jmcneill getsectors() {
     87      1.63  jmcneill 	case "$1" in
     88      1.63  jmcneill 	*g)
     89      1.63  jmcneill 		m=1073741824
     90      1.63  jmcneill 		v=${1%g}
     91      1.63  jmcneill 		;;
     92      1.63  jmcneill 	*m)
     93      1.63  jmcneill 		m=1048576
     94      1.63  jmcneill 		v=${1%m}
     95      1.63  jmcneill 		;;
     96      1.63  jmcneill 	*k)
     97      1.63  jmcneill 		m=1024
     98      1.63  jmcneill 		v=${1%k}
     99      1.63  jmcneill 		;;
    100      1.63  jmcneill 	*[0-9b])
    101      1.63  jmcneill 		m=1
    102      1.63  jmcneill 		v=${1%b}
    103      1.63  jmcneill 		;;
    104      1.63  jmcneill 	esac
    105      1.63  jmcneill 	echo $((m * v / 512))
    106      1.63  jmcneill }
    107      1.63  jmcneill 
    108      1.12  christos usage() {
    109      1.12  christos 	cat << EOF 1>&2
    110      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>]
    111      1.39  christos 
    112      1.39  christos -b	Boot only, no sets loaded
    113      1.47  christos -r	root device kind (sd, wd, ld)
    114      1.39  christos -d	Add the debug sets
    115      1.43  christos -m	Optimize the OS installation to mimimize disk writes for SSDs
    116      1.57   hubertf -x	Load the X sets too, not just the base ones
    117      1.12  christos EOF
    118      1.13  christos 	exit 1
    119      1.12  christos }
    120      1.12  christos 
    121      1.32  jmcneill # First pass for options to get the host and src directories
    122      1.60    martin OPTS="B:D:K:S:bc:dh:mr:s:x"
    123       1.4  christos while getopts "$OPTS" f
    124       1.4  christos do
    125       1.4  christos 	case $f in
    126       1.4  christos 	h)	h="$OPTARG";;
    127      1.32  jmcneill 	S)	src="$OPTARG";;
    128       1.4  christos 	*)	;;
    129       1.4  christos 	esac
    130       1.4  christos done
    131       1.4  christos 
    132       1.4  christos if [ -z "$h" ]
    133       1.4  christos then
    134       1.4  christos 	usage
    135       1.4  christos fi
    136       1.4  christos 
    137       1.5  christos if [ ! -f "${DIR}/conf/${h}.conf" ]
    138       1.4  christos then
    139       1.5  christos 	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
    140       1.4  christos 	exit 1
    141       1.4  christos fi
    142       1.4  christos 
    143      1.56  jmcneill resize=false
    144      1.56  jmcneill 
    145       1.5  christos . "${DIR}/conf/${h}.conf"
    146      1.45  christos release="/usr/obj/${MACHINE}/release"
    147       1.4  christos 
    148      1.17  christos selected_sets="$sets"
    149      1.50     skrll dsets_p=false
    150      1.50     skrll xsets_p=false
    151      1.39  christos minwrites=false
    152      1.47  christos rootdev=ld
    153      1.60    martin endian=
    154      1.17  christos 
    155       1.4  christos OPTIND=1
    156       1.4  christos while getopts "$OPTS" f
    157       1.4  christos do
    158       1.4  christos 	case $f in
    159      1.60    martin 	B)	endian="-B $OPTARG";;
    160      1.18  christos 	D)	release="$OPTARG";;
    161      1.18  christos 	K)	kernel="$OPTARG";;
    162      1.32  jmcneill 	S)	;;
    163      1.41  christos 	b)	bootonly=true;;
    164      1.50     skrll 	d)	dsets_p=true
    165      1.39  christos 		selected_sets="$selected_sets debug"
    166      1.50     skrll 		if $xsets_p; then
    167      1.39  christos 			selected_sets="$selected_sets xdebug"
    168      1.39  christos 		fi
    169      1.39  christos 		;;
    170       1.4  christos 	c)	custom="$OPTARG";;
    171       1.4  christos 	h)	;;
    172      1.39  christos 	m)	minwrites=true;;
    173      1.47  christos 	r)	rootdev="$OPTARG";;
    174       1.4  christos 	s)	size="$OPTARG";;
    175      1.50     skrll 	x)	xsets_p=true
    176      1.39  christos 		selected_sets="$selected_sets $xsets"
    177      1.50     skrll 		if $dsets_p; then
    178      1.39  christos 		    selected_sets="$selected_sets xdebug"
    179      1.39  christos 		fi
    180      1.39  christos 		;;
    181       1.6  christos 	*)	usage;;
    182       1.1       agc 	esac
    183       1.1       agc done
    184       1.1       agc 
    185      1.20  jmcneill shift $(( $OPTIND - 1 ))
    186       1.4  christos if [ -n "$1" ]; then
    187       1.1       agc 	# take the next argument as being the image name
    188       1.1       agc 	image="$1"
    189       1.1       agc 	shift
    190       1.1       agc fi
    191       1.1       agc 
    192      1.22  christos case "$image" in
    193      1.22  christos *.gz)	compress=true; image="${image%.gz}";;
    194      1.22  christos *)	compress=false;;
    195      1.22  christos esac
    196      1.22  christos 
    197      1.45  christos if [ -z "${bootonly}" ]; then
    198      1.36   garbled 	echo ${bar} configuring sets ${bar}
    199      1.51     skrll 	(cat "${release}/etc/mtree/NetBSD.dist"
    200      1.36   garbled 	for i in $selected_sets; do
    201      1.36   garbled 		s="${release}/etc/mtree/set.$i"
    202      1.36   garbled 		if [ -f "$s" ]; then
    203      1.36   garbled 			cat "$s"
    204      1.36   garbled 		fi
    205      1.36   garbled 	done) > "$tmp/selected_sets"
    206      1.36   garbled fi
    207       1.1       agc 
    208       1.4  christos make_fstab
    209      1.17  christos customize
    210      1.17  christos populate
    211       1.1       agc 
    212      1.45  christos if [ -n "${msdosid}" ]; then
    213      1.17  christos 	echo ${bar} Populating msdos filesystem ${bar}
    214      1.63  jmcneill 	${MAKEFS} -N ${release}/etc -t msdos -o volume_label=NETBSD \
    215      1.63  jmcneill 	    -O $((${init} / 2))m -s $((${boot} / 2))m \
    216      1.41  christos 	    ${image} ${mnt}/boot
    217       1.1       agc fi
    218       1.1       agc 
    219      1.45  christos if [ -z "${bootonly}" ]; then
    220      1.36   garbled 	echo ${bar} Populating ffs filesystem ${bar}
    221      1.60    martin 	${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
    222      1.46  christos 	    -O ${ffsoffset} \
    223      1.61  jmcneill 	    -o d=4096,f=8192,b=65536 -b $((${extra}))m \
    224      1.36   garbled 	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
    225      1.36   garbled fi
    226       1.1       agc 
    227      1.23  christos if [ "${size}" = 0 ]; then
    228      1.23  christos 	size="$(getsize "${image}")"
    229      1.17  christos fi
    230      1.23  christos newsize=$((${size} / 2 / 1024))
    231      1.49  christos compare=$((${newsize} * 2 * 1024))
    232      1.49  christos while [ "${compare}" != "${size}" ]
    233      1.49  christos do    
    234      1.49  christos 	size="$((size + size - compare))"  
    235      1.49  christos 	newsize="$((${size} / 2 / 1024))"
    236      1.49  christos 	compare="$((${newsize} * 2 * 1024))"
    237      1.49  christos done                      
    238       1.1       agc 
    239      1.45  christos if [ -n "${msdosid}" ]; then
    240      1.17  christos 	echo ${bar} Running fdisk ${bar}
    241      1.30  jmcneill 	initsecs=$((${init} * 1024))
    242      1.30  jmcneill 	bootsecs=$((${boot} * 1024))
    243      1.63  jmcneill 	${FDISK} -f -i ${image}
    244      1.62     skrll 	${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
    245      1.63  jmcneill 	if [ -z "${bootonly}" ]; then
    246      1.63  jmcneill 		ffsstart="$(getsectors ${ffsoffset})"
    247      1.63  jmcneill 		imagesize="$(getsize "${image}")"
    248      1.63  jmcneill 		imagesecs="$(getsectors ${imagesize})"
    249      1.63  jmcneill 		ffssize="$(expr ${imagesecs} - ${ffsstart})"
    250      1.63  jmcneill 		${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
    251      1.63  jmcneill 	fi
    252      1.64  jmcneill 
    253      1.64  jmcneill 	echo ${bar} Adding label ${bar}
    254      1.64  jmcneill 	make_label > ${tmp}/label
    255      1.64  jmcneill 	${DISKLABEL} -R -F ${image} ${tmp}/label
    256      1.45  christos elif [ -n "${netbsdid}" ]; then
    257      1.64  jmcneill 	echo ${bar} Adding label ${bar}
    258      1.64  jmcneill 	make_label > ${tmp}/label
    259      1.64  jmcneill 	${DISKLABEL} -R -F ${image} ${tmp}/label
    260      1.64  jmcneill 
    261      1.45  christos 	echo ${bar} Running fdisk ${bar}
    262      1.45  christos 	${FDISK} -f -i ${image}
    263      1.49  christos 	${FDISK} -f -a -u -0 -s 169/${init} ${image}
    264      1.45  christos 	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
    265       1.2       agc fi
    266      1.22  christos 
    267      1.22  christos if $compress; then
    268      1.22  christos 	echo ${bar} Compressing image ${bar}
    269      1.23  christos 	rm -f "${image}.gz"
    270      1.44       ast 	${GZIP_CMD} -9 ${image}
    271      1.22  christos 	image="${image}.gz"
    272      1.22  christos fi
    273      1.22  christos 
    274      1.17  christos echo ${bar} Image is ${image} ${bar}
    275