Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.67
      1 #!/bin/sh
      2 # $NetBSD: mkimage,v 1.67 2017/11/28 00:14:30 kre 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 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 	/tmp/$PROG.*)	rm -fr "$tmp";;
     78 	esac
     79 }
     80 
     81 getsize() {
     82 	set -- $(ls -l $1)
     83 	echo $5
     84 }
     85 
     86 getsectors() {
     87 	case "$1" in
     88 	*g)
     89 		m=1073741824
     90 		v=${1%g}
     91 		;;
     92 	*m)
     93 		m=1048576
     94 		v=${1%m}
     95 		;;
     96 	*k)
     97 		m=1024
     98 		v=${1%k}
     99 		;;
    100 	*[0-9b])
    101 		m=1
    102 		v=${1%b}
    103 		;;
    104 	esac
    105 	echo $((m * v / 512))
    106 }
    107 
    108 usage() {
    109 	cat << EOF 1>&2
    110 Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
    111 
    112 -b	Boot only, no sets loaded
    113 -r	root device kind (sd, wd, ld)
    114 -d	Add the debug sets
    115 -m	Optimize the OS installation to mimimize disk writes for SSDs
    116 -x	Load the X sets too, not just the base ones
    117 EOF
    118 	exit 1
    119 }
    120 
    121 # First pass for options to get the host and src directories
    122 OPTS="B:D:K:S:bc:dh:mr:s:x"
    123 while getopts "$OPTS" f
    124 do
    125 	case $f in
    126 	h)	h="$OPTARG";;
    127 	S)	src="$OPTARG";;
    128 	*)	;;
    129 	esac
    130 done
    131 
    132 if [ -z "$h" ]
    133 then
    134 	usage
    135 fi
    136 
    137 if [ ! -f "${DIR}/conf/${h}.conf" ]
    138 then
    139 	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
    140 	exit 1
    141 fi
    142 
    143 resize=false
    144 
    145 . "${DIR}/conf/${h}.conf"
    146 release="/usr/obj/${MACHINE}/release"
    147 
    148 selected_sets="$sets"
    149 dsets_p=false
    150 xsets_p=false
    151 minwrites=false
    152 rootdev=ld
    153 endian=
    154 
    155 OPTIND=1
    156 while getopts "$OPTS" f
    157 do
    158 	case $f in
    159 	B)	endian="-B $OPTARG";;
    160 	D)	release="$OPTARG";;
    161 	K)	kernel="$OPTARG";;
    162 	S)	;;
    163 	b)	bootonly=true;;
    164 	d)	dsets_p=true
    165 		selected_sets="$selected_sets debug"
    166 		if $xsets_p; then
    167 			selected_sets="$selected_sets xdebug"
    168 		fi
    169 		;;
    170 	c)	custom="$OPTARG";;
    171 	h)	;;
    172 	m)	minwrites=true;;
    173 	r)	rootdev="$OPTARG";;
    174 	s)	size="$OPTARG";;
    175 	x)	xsets_p=true
    176 		selected_sets="$selected_sets $xsets"
    177 		if $dsets_p; then
    178 		    selected_sets="$selected_sets xdebug"
    179 		fi
    180 		;;
    181 	*)	usage;;
    182 	esac
    183 done
    184 
    185 shift $(( $OPTIND - 1 ))
    186 if [ -n "$1" ]; then
    187 	# take the next argument as being the image name
    188 	image="$1"
    189 	shift
    190 fi
    191 
    192 case "$image" in
    193 *.gz)	compress=true; image="${image%.gz}";;
    194 *)	compress=false;;
    195 esac
    196 
    197 if [ -z "${bootonly}" ]; then
    198 	echo ${bar} configuring sets ${bar}
    199 	(cat "${release}/etc/mtree/NetBSD.dist"
    200 	for i in $selected_sets; do
    201 		s="${release}/etc/mtree/set.$i"
    202 		if [ -f "$s" ]; then
    203 			cat "$s"
    204 		fi
    205 	done) > "$tmp/selected_sets"
    206 fi
    207 
    208 make_fstab
    209 customize
    210 populate
    211 
    212 if [ -n "${msdosid}" ]; then
    213 	echo ${bar} Populating msdos filesystem ${bar}
    214 	${MAKEFS} -N ${release}/etc -t msdos -o volume_label=NETBSD \
    215 	    -O $((${init} / 2))m -s $((${boot} / 2))m \
    216 	    ${image} ${mnt}/boot
    217 fi
    218 
    219 if [ -z "${bootonly}" ]; then
    220 	echo ${bar} Populating ffs filesystem ${bar}
    221 	${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
    222 	    -O ${ffsoffset} \
    223 	    -o d=4096,f=8192,b=65536 -b $((${extra}))m \
    224 	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
    225 fi
    226 
    227 if [ "${size}" = 0 ]; then
    228 	size="$(getsize "${image}")"
    229 fi
    230 newsize=$((${size} / 2 / 1024))
    231 compare=$((${newsize} * 2 * 1024))
    232 while [ "${compare}" != "${size}" ]
    233 do    
    234 	size="$((size + size - compare))"  
    235 	newsize="$((${size} / 2 / 1024))"
    236 	compare="$((${newsize} * 2 * 1024))"
    237 done                      
    238 
    239 if [ -n "${msdosid}" ]; then
    240 	echo ${bar} Running fdisk ${bar}
    241 	initsecs=$((${init} * 1024))
    242 	bootsecs=$((${boot} * 1024))
    243 	${FDISK} -f -i ${image}
    244 	${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
    245 	if [ -z "${bootonly}" ]; then
    246 		ffsstart="$(getsectors ${ffsoffset})"
    247 		imagesize="$(getsize "${image}")"
    248 		imagesecs="$(getsectors ${imagesize})"
    249 		ffssize="$(expr ${imagesecs} - ${ffsstart})"
    250 		${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
    251 	fi
    252 
    253 	echo ${bar} Adding label ${bar}
    254 	make_label > ${tmp}/label
    255 	${DISKLABEL} -R -F ${image} ${tmp}/label
    256 elif [ -n "${netbsdid}" ]; then
    257 	echo ${bar} Adding label ${bar}
    258 	make_label > ${tmp}/label
    259 	${DISKLABEL} -R -F ${image} ${tmp}/label
    260 
    261 	echo ${bar} Running fdisk ${bar}
    262 	${FDISK} -f -i ${image}
    263 	${FDISK} -f -a -u -0 -s 169/${init} ${image}
    264 	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
    265 fi
    266 
    267 if $compress; then
    268 	echo ${bar} Compressing image ${bar}
    269 	rm -f "${image}.gz"
    270 	${GZIP_CMD} -9 ${image}
    271 	image="${image}.gz"
    272 fi
    273 
    274 echo ${bar} Image is ${image} ${bar}
    275