Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.57
      1 #!/bin/sh
      2 # $NetBSD: mkimage,v 1.57 2015/04/18 22:06:48 hubertf 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 set -e
     36 
     37 DIR="$(cd "$(dirname "$0")" && pwd)"
     38 PROG="$(basename "$0")"
     39 
     40 DISKLABEL=${TOOL_DISKLABEL:-disklabel}
     41 FDISK=${TOOL_FDISK:-fdisk}
     42 MAKEFS=${TOOL_MAKEFS:-makefs}
     43 MTREE=${TOOL_MTREE:-mtree}
     44 INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot}
     45 GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1)
     46 
     47 src="/usr/src"
     48 sets="base comp etc games man misc modules text"
     49 xsets="xbase xcomp xetc xfont xserver" 
     50 minfree="10%"
     51 bar="==="
     52 
     53 tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
     54 mnt="${tmp}/mnt"
     55 mkdir -p "${mnt}/etc" "${mnt}/dev"
     56 
     57 trap "cleanup" 0 1 2 3 15
     58 
     59 cleanup() {
     60 	case "$tmp" in
     61 	/tmp/$PROG.*)	rm -fr "$tmp";;
     62 	esac
     63 }
     64 
     65 getsize() {
     66 	set -- $(ls -l $1)
     67 	echo $5
     68 }
     69 
     70 usage() {
     71 	cat << EOF 1>&2
     72 Usage: $PROG -h <host-arch> [-bdmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
     73 
     74 -b	Boot only, no sets loaded
     75 -r	root device kind (sd, wd, ld)
     76 -d	Add the debug sets
     77 -m	Optimize the OS installation to mimimize disk writes for SSDs
     78 -x	Load the X sets too, not just the base ones
     79 EOF
     80 	exit 1
     81 }
     82 
     83 # First pass for options to get the host and src directories
     84 OPTS="K:D:S:bc:dh:mr:s:x"
     85 while getopts "$OPTS" f
     86 do
     87 	case $f in
     88 	h)	h="$OPTARG";;
     89 	S)	src="$OPTARG";;
     90 	*)	;;
     91 	esac
     92 done
     93 
     94 if [ -z "$h" ]
     95 then
     96 	usage
     97 fi
     98 
     99 if [ ! -f "${DIR}/conf/${h}.conf" ]
    100 then
    101 	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
    102 	exit 1
    103 fi
    104 
    105 resize=false
    106 
    107 . "${DIR}/conf/${h}.conf"
    108 release="/usr/obj/${MACHINE}/release"
    109 
    110 selected_sets="$sets"
    111 dsets_p=false
    112 xsets_p=false
    113 minwrites=false
    114 rootdev=ld
    115 
    116 OPTIND=1
    117 while getopts "$OPTS" f
    118 do
    119 	case $f in
    120 	D)	release="$OPTARG";;
    121 	K)	kernel="$OPTARG";;
    122 	S)	;;
    123 	b)	bootonly=true;;
    124 	d)	dsets_p=true
    125 		selected_sets="$selected_sets debug"
    126 		if $xsets_p; then
    127 			selected_sets="$selected_sets xdebug"
    128 		fi
    129 		;;
    130 	c)	custom="$OPTARG";;
    131 	h)	;;
    132 	m)	minwrites=true;;
    133 	r)	rootdev="$OPTARG";;
    134 	s)	size="$OPTARG";;
    135 	x)	xsets_p=true
    136 		selected_sets="$selected_sets $xsets"
    137 		if $dsets_p; then
    138 		    selected_sets="$selected_sets xdebug"
    139 		fi
    140 		;;
    141 	*)	usage;;
    142 	esac
    143 done
    144 
    145 shift $(( $OPTIND - 1 ))
    146 if [ -n "$1" ]; then
    147 	# take the next argument as being the image name
    148 	image="$1"
    149 	shift
    150 fi
    151 
    152 case "$image" in
    153 *.gz)	compress=true; image="${image%.gz}";;
    154 *)	compress=false;;
    155 esac
    156 
    157 if [ -z "${bootonly}" ]; then
    158 	echo ${bar} configuring sets ${bar}
    159 	(cat "${release}/etc/mtree/NetBSD.dist"
    160 	for i in $selected_sets; do
    161 		s="${release}/etc/mtree/set.$i"
    162 		if [ -f "$s" ]; then
    163 			cat "$s"
    164 		fi
    165 	done) > "$tmp/selected_sets"
    166 fi
    167 
    168 make_fstab
    169 customize
    170 populate
    171 
    172 if [ -n "${msdosid}" ]; then
    173 	echo ${bar} Populating msdos filesystem ${bar}
    174 	${MAKEFS} -N ${release}/etc -t msdos \
    175 	    -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m \
    176 	    ${image} ${mnt}/boot
    177 fi
    178 
    179 if [ -z "${bootonly}" ]; then
    180 	echo ${bar} Populating ffs filesystem ${bar}
    181 	${MAKEFS} -r -N ${release}/etc -t ffs -rx \
    182 	    -O ${ffsoffset} \
    183 	    -o d=4096,f=2048,b=16384 -b $((${extra}))m \
    184 	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
    185 fi
    186 
    187 if [ "${size}" = 0 ]; then
    188 	size="$(getsize "${image}")"
    189 fi
    190 newsize=$((${size} / 2 / 1024))
    191 compare=$((${newsize} * 2 * 1024))
    192 while [ "${compare}" != "${size}" ]
    193 do    
    194 	size="$((size + size - compare))"  
    195 	newsize="$((${size} / 2 / 1024))"
    196 	compare="$((${newsize} * 2 * 1024))"
    197 done                      
    198 
    199 echo ${bar} Adding label ${bar}
    200 make_label > ${tmp}/label
    201 ${DISKLABEL} -R -F ${image} ${tmp}/label
    202 if [ -n "${msdosid}" ]; then
    203 	echo ${bar} Running fdisk ${bar}
    204 	initsecs=$((${init} * 1024))
    205 	bootsecs=$((${boot} * 1024))
    206 	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
    207 elif [ -n "${netbsdid}" ]; then
    208 	echo ${bar} Running fdisk ${bar}
    209 	${FDISK} -f -i ${image}
    210 	${FDISK} -f -a -u -0 -s 169/${init} ${image}
    211 	${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1
    212 fi
    213 
    214 if $compress; then
    215 	echo ${bar} Compressing image ${bar}
    216 	rm -f "${image}.gz"
    217 	${GZIP_CMD} -9 ${image}
    218 	image="${image}.gz"
    219 fi
    220 
    221 echo ${bar} Image is ${image} ${bar}
    222