Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.72
      1 #!/bin/sh
      2 # $NetBSD: mkimage,v 1.72 2020/05/18 21:19:34 jmcneill 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 [ ! "${MKDTB}" = "no" ]; then
    219 	#
    220 	# Part of the dtb set resides on the FAT partition (/boot/dtb/*), and
    221 	# the rest on FFS. Split it up here.
    222 	#
    223 	echo ${bar} Installing devicetree blobs ${bar}
    224 	mkdir -p "${mnt}/boot"
    225 	cp -r "${release}/boot/dtb" "${mnt}/boot/dtb"
    226 
    227 	mkdir -p "${mnt}/etc/mtree"
    228 	cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb"
    229 	echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets"
    230 
    231 	mkdir -p "${mnt}/var/db/obsolete"
    232 	cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb"
    233 	echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets"
    234 fi
    235 
    236 if [ -n "${msdosid}" ]; then
    237 	echo ${bar} Populating msdos filesystem ${bar}
    238 
    239 	case $(( ${msdosid} )) in
    240 	1)	fat_opt=",fat_type=12";;
    241 	4|6|14)	fat_opt=",fat_type=16";;
    242 	11|12)	fat_opt=",fat_type=32";;
    243 	*)	fat_opt=;;
    244 	esac
    245 	${MAKEFS} -N ${release}/etc -t msdos \
    246 	    -o "volume_label=NETBSD${fat_opt}" \
    247 	    -O $((${init} / 2))m -s $((${boot} / 2))m \
    248 	    ${image} ${mnt}/boot
    249 fi
    250 
    251 if [ -z "${bootonly}" ]; then
    252 	echo ${bar} Populating ffs filesystem ${bar}
    253 	${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \
    254 	    -O ${ffsoffset} \
    255 	    -o d=4096,f=8192,b=65536 -b $((${extra}))m \
    256 	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
    257 fi
    258 
    259 if [ "${size}" = 0 ]; then
    260 	size="$(getsize "${image}")"
    261 fi
    262 newsize=$((${size} / 2 / 1024))
    263 compare=$((${newsize} * 2 * 1024))
    264 while [ "${compare}" != "${size}" ]
    265 do    
    266 	size="$((size + size - compare))"  
    267 	newsize="$((${size} / 2 / 1024))"
    268 	compare="$((${newsize} * 2 * 1024))"
    269 done                      
    270 
    271 if [ -n "${msdosid}" ]; then
    272 	echo ${bar} Running fdisk ${bar}
    273 	initsecs=$((${init} * 1024))
    274 	bootsecs=$((${boot} * 1024))
    275 	${FDISK} -f -i ${image}
    276 	${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
    277 	if [ -z "${bootonly}" ]; then
    278 		ffsstart="$(getsectors ${ffsoffset})"
    279 		imagesize="$(getsize "${image}")"
    280 		imagesecs="$(getsectors ${imagesize})"
    281 		ffssize="$(expr ${imagesecs} - ${ffsstart})"
    282 		${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image}
    283 	fi
    284 
    285 	echo ${bar} Adding label ${bar}
    286 	make_label > ${tmp}/label
    287 	${DISKLABEL} -R -F ${image} ${tmp}/label
    288 elif [ -n "${netbsdid}" ]; then
    289 	echo ${bar} Adding label ${bar}
    290 	make_label > ${tmp}/label
    291 	${DISKLABEL} -R -F ${image} ${tmp}/label
    292 
    293 	echo ${bar} Running fdisk ${bar}
    294 	${FDISK} -f -i ${image}
    295 	${FDISK} -f -a -u -0 -s 169/${init} ${image}
    296 	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
    297 fi
    298 
    299 if $compress; then
    300 	echo ${bar} Compressing image ${bar}
    301 	rm -f "${image}.gz"
    302 	${GZIP_CMD} -9 ${image}
    303 	image="${image}.gz"
    304 fi
    305 
    306 echo ${bar} Image is ${image} ${bar}
    307