Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.39
      1 #!/bin/sh
      2 # $NetBSD: mkimage,v 1.39 2013/05/20 19:27:17 christos Exp $
      3 #
      4 # Copyright (c) 2013 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 
     45 src="/usr/src"
     46 release="/usr/obj/evbarm/release"
     47 sets="base comp etc games man misc modules text"
     48 xsets="xbase xcomp xetc xfont xserver" 
     49 minfree="10%"
     50 bar="==="
     51 
     52 tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
     53 mnt="${tmp}/mnt"
     54 mkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot"
     55 
     56 trap "cleanup" 0 1 2 3 15
     57 
     58 cleanup() {
     59 	case "$tmp" in
     60 	/tmp/$PROG.*)	rm -fr "$tmp";;
     61 	esac
     62 }
     63 
     64 getsize() {
     65 	set -- $(ls -l $1)
     66 	echo $5
     67 }
     68 
     69 usage() {
     70 	cat << EOF 1>&2
     71 Usage: $PROG -h <host-arch> [-bmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
     72 
     73 -b	Boot only, no sets loaded
     74 -d	Add the debug sets
     75 -m	Mimimize disk writes for sd cards
     76 -x	Load the x sets too, not just the base ones
     77 EOF
     78 	exit 1
     79 }
     80 
     81 # First pass for options to get the host and src directories
     82 OPTS="K:D:S:bc:dh:ms:x"
     83 while getopts "$OPTS" f
     84 do
     85 	case $f in
     86 	h)	h="$OPTARG";;
     87 	S)	src="$OPTARG";;
     88 	*)	;;
     89 	esac
     90 done
     91 
     92 if [ -z "$h" ]
     93 then
     94 	usage
     95 fi
     96 
     97 if [ ! -f "${DIR}/conf/${h}.conf" ]
     98 then
     99 	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
    100 	exit 1
    101 fi
    102 
    103 . "${DIR}/conf/${h}.conf"
    104 
    105 selected_sets="$sets"
    106 dsets=false
    107 xsets=false
    108 minwrites=false
    109 
    110 OPTIND=1
    111 while getopts "$OPTS" f
    112 do
    113 	case $f in
    114 	D)	release="$OPTARG";;
    115 	K)	kernel="$OPTARG";;
    116 	S)	;;
    117 	b)	bootonly="true";;
    118 	d)	dsets=true
    119 		selected_sets="$selected_sets debug"
    120 		if $xsets; then
    121 			selected_sets="$selected_sets xdebug"
    122 		fi
    123 		;;
    124 	c)	custom="$OPTARG";;
    125 	h)	;;
    126 	m)	minwrites=true;;
    127 	s)	size="$OPTARG";;
    128 	x)	xsets=true
    129 		selected_sets="$selected_sets $xsets"
    130 		if $dsets; then
    131 		    selected_sets="$selected_sets xdebug"
    132 		fi
    133 		;;
    134 	*)	usage;;
    135 	esac
    136 done
    137 
    138 shift $(( $OPTIND - 1 ))
    139 if [ -n "$1" ]; then
    140 	# take the next argument as being the image name
    141 	image="$1"
    142 	shift
    143 fi
    144 
    145 case "$image" in
    146 *.gz)	compress=true; image="${image%.gz}";;
    147 *)	compress=false;;
    148 esac
    149 
    150 if [ -z "$bootonly" ]; then
    151 	echo ${bar} configuring sets ${bar}
    152 	(echo '/set type=dir uname=root gname=wheel mode=0755'
    153 	for i in $selected_sets; do
    154 		s="${release}/etc/mtree/set.$i"
    155 		if [ -f "$s" ]; then
    156 			cat "$s"
    157 		fi
    158 	done) > "$tmp/selected_sets"
    159 fi
    160 
    161 make_fstab
    162 customize
    163 populate
    164 
    165 if [ -z "$bootonly" ]; then
    166 	(cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 
    167 	    ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets"
    168 fi
    169 if [ -n ${msdosid} ]; then
    170 	echo ${bar} Populating msdos filesystem ${bar}
    171 	${MAKEFS} -N ${release}/etc -t msdos \
    172 	    -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m ${image} ${mnt}/boot
    173 fi
    174 
    175 if [ -z "$bootonly" ]; then
    176 	echo ${bar} Populating ffs filesystem ${bar}
    177 	${MAKEFS} -N ${release}/etc -t ffs -rx \
    178 	    -O $(((${init} + ${boot} + ${swap}) / 2))m \
    179 		-o d=4096 \
    180 	    -b $((${extra}))m \
    181 	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
    182 fi
    183 
    184 if [ "${size}" = 0 ]; then
    185 	size="$(getsize "${image}")"
    186 fi
    187 newsize=$((${size} / 2 / 1024))
    188 
    189 echo ${bar} Adding label ${bar}
    190 make_label > ${tmp}/label
    191 ${DISKLABEL} -R -F ${image} ${tmp}/label
    192 if [ -n ${msdosid} ]; then
    193 	echo ${bar} Running fdisk ${bar}
    194 	initsecs=$((${init} * 1024))
    195 	bootsecs=$((${boot} * 1024))
    196 	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
    197 fi
    198 
    199 if $compress; then
    200 	echo ${bar} Compressing image ${bar}
    201 	rm -f "${image}.gz"
    202 	gzip -9 ${image}
    203 	image="${image}.gz"
    204 fi
    205 
    206 echo ${bar} Image is ${image} ${bar}
    207