Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.70
      1 #!/bin/sh
      2 # $NetBSD: mkimage,v 1.70 2019/10/02 11:16:00 maya 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 
     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 MAKEFS=${TOOL_MAKEFS:-makefs}
     58 MTREE=${TOOL_MTREE:-mtree}
     59 INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
     60 MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage}
     61 GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
     62 
     63 src="/usr/src"
     64 sets="base comp etc games man misc modules rescue tests text"
     65 xsets="xbase xcomp xetc xfont xserver" 
     66 minfree="10%"
     67 bar="==="
     68 
     69 tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")"
     70 mnt="${tmp}/mnt"
     71 mkdir -p "${mnt}/etc" "${mnt}/dev"
     72 
     73 trap "cleanup" 0 1 2 3 15
     74 
     75 cleanup() {
     76 	case "$tmp" in
     77 	"${TMPDIR:-/tmp}/$PROG."*)	rm -fr "$tmp";;
     78 	esac
     79 }
     80 
     81 fail() {
     82 	IFS=' '
     83 	echo >&2 "${PROG}: $*"
     84 	exit 1
     85 }
     86 
     87 getsize() {
     88 	set -- $(ls -l $1)
     89 	echo $5
     90 }
     91 
     92 getsectors() {
     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 
    114 usage() {
    115 	cat << EOF 1>&2
    116 Usage: $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
    123 EOF
    124 	exit 1
    125 }
    126 
    127 # First pass for options to get the host and src directories
    128 OPTS="B:D:K:S:bc:dh:mr:s:x"
    129 while getopts "$OPTS" f
    130 do
    131 	case $f in
    132 	h)	h="$OPTARG";;
    133 	S)	src="$OPTARG";;
    134 	*)	;;
    135 	esac
    136 done
    137 
    138 if [ -z "$h" ]
    139 then
    140 	usage
    141 fi
    142 
    143 if [ ! -f "${DIR}/conf/${h}.conf" ]
    144 then
    145 	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
    146 	exit 1
    147 fi
    148 
    149 resize=false
    150 
    151 . "${DIR}/conf/${h}.conf"
    152 release="/usr/obj/${MACHINE}/release"
    153 
    154 selected_sets="$sets"
    155 dsets_p=false
    156 xsets_p=false
    157 minwrites=false
    158 rootdev=ld
    159 endian=
    160 
    161 OPTIND=1
    162 while getopts "$OPTS" f
    163 do
    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
    189 done
    190 
    191 shift $(( $OPTIND - 1 ))
    192 if [ -n "$1" ]; then
    193 	# take the next argument as being the image name
    194 	image="$1"
    195 	shift
    196 fi
    197 
    198 case "$image" in
    199 *.gz)	compress=true; image="${image%.gz}";;
    200 *)	compress=false;;
    201 esac
    202 
    203 if [ -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"
    212 fi
    213 
    214 make_fstab
    215 customize
    216 populate
    217 
    218 if [ -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
    223 fi
    224 
    225 if [ -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}"
    231 fi
    232 
    233 if [ "${size}" = 0 ]; then
    234 	size="$(getsize "${image}")"
    235 fi
    236 newsize=$((${size} / 2 / 1024))
    237 compare=$((${newsize} * 2 * 1024))
    238 while [ "${compare}" != "${size}" ]
    239 do    
    240 	size="$((size + size - compare))"  
    241 	newsize="$((${size} / 2 / 1024))"
    242 	compare="$((${newsize} * 2 * 1024))"
    243 done                      
    244 
    245 if [ -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
    262 elif [ -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
    271 fi
    272 
    273 if $compress; then
    274 	echo ${bar} Compressing image ${bar}
    275 	rm -f "${image}.gz"
    276 	${GZIP_CMD} -9 ${image}
    277 	image="${image}.gz"
    278 fi
    279 
    280 echo ${bar} Image is ${image} ${bar}
    281