Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.3.4.2
      1  1.3.4.1  tls #!/bin/sh
      2  1.3.4.1  tls # $NetBSD: mkimage,v 1.3.4.2 2013/06/23 06:26:19 tls Exp $
      3  1.3.4.1  tls #
      4  1.3.4.1  tls # Copyright (c) 2013 The NetBSD Foundation, Inc.
      5      1.1  agc # All rights reserved.
      6      1.1  agc #
      7  1.3.4.1  tls # This code is derived from software contributed to The NetBSD Foundation
      8  1.3.4.1  tls # by Christos Zoulas.
      9  1.3.4.1  tls #
     10      1.1  agc # Redistribution and use in source and binary forms, with or without
     11      1.1  agc # modification, are permitted provided that the following conditions
     12      1.1  agc # are met:
     13      1.1  agc # 1. Redistributions of source code must retain the above copyright
     14      1.1  agc #    notice, this list of conditions and the following disclaimer.
     15      1.1  agc # 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  agc #    notice, this list of conditions and the following disclaimer in the
     17      1.1  agc #    documentation and/or other materials provided with the distribution.
     18  1.3.4.1  tls # 3. Neither the name of The NetBSD Foundation nor the names of its
     19  1.3.4.1  tls #    contributors may be used to endorse or promote products derived
     20  1.3.4.1  tls #    from this software without specific prior written permission.
     21  1.3.4.1  tls #
     22  1.3.4.1  tls # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  1.3.4.1  tls # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  1.3.4.1  tls # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  1.3.4.1  tls # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  1.3.4.1  tls # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.3.4.1  tls # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.3.4.1  tls # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.3.4.1  tls # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.3.4.1  tls # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.3.4.1  tls # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  1.3.4.1  tls # POSSIBILITY OF SUCH DAMAGE.
     33  1.3.4.1  tls #
     34  1.3.4.1  tls 
     35  1.3.4.1  tls set -e
     36  1.3.4.1  tls 
     37  1.3.4.1  tls DIR="$(cd "$(dirname "$0")" && pwd)"
     38  1.3.4.1  tls PROG="$(basename "$0")"
     39  1.3.4.1  tls 
     40  1.3.4.1  tls DISKLABEL=${TOOL_DISKLABEL:-disklabel}
     41  1.3.4.1  tls FDISK=${TOOL_FDISK:-fdisk}
     42  1.3.4.2  tls GZIP=${TOOL_GZIP:-gzip}
     43  1.3.4.1  tls MAKEFS=${TOOL_MAKEFS:-makefs}
     44  1.3.4.1  tls MTREE=${TOOL_MTREE:-mtree}
     45  1.3.4.1  tls 
     46  1.3.4.1  tls src="/usr/src"
     47  1.3.4.1  tls release="/usr/obj/evbarm/release"
     48  1.3.4.1  tls sets="base comp etc games man misc modules text"
     49  1.3.4.1  tls xsets="xbase xcomp xetc xfont xserver" 
     50  1.3.4.1  tls minfree="10%"
     51  1.3.4.1  tls bar="==="
     52      1.1  agc 
     53  1.3.4.1  tls tmp="$(mktemp -d "/tmp/$PROG.XXXXXX")"
     54  1.3.4.1  tls mnt="${tmp}/mnt"
     55  1.3.4.1  tls mkdir -p "${mnt}/etc" "${mnt}/dev" "${mnt}/boot"
     56  1.3.4.1  tls 
     57  1.3.4.1  tls trap "cleanup" 0 1 2 3 15
     58  1.3.4.1  tls 
     59  1.3.4.1  tls cleanup() {
     60  1.3.4.1  tls 	case "$tmp" in
     61  1.3.4.1  tls 	/tmp/$PROG.*)	rm -fr "$tmp";;
     62  1.3.4.1  tls 	esac
     63  1.3.4.1  tls }
     64      1.1  agc 
     65  1.3.4.1  tls getsize() {
     66  1.3.4.1  tls 	set -- $(ls -l $1)
     67  1.3.4.1  tls 	echo $5
     68      1.1  agc }
     69      1.1  agc 
     70  1.3.4.1  tls usage() {
     71  1.3.4.1  tls 	cat << EOF 1>&2
     72  1.3.4.2  tls Usage: $PROG -h <host-arch> [-bmx] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>]
     73  1.3.4.2  tls 
     74  1.3.4.2  tls -b	Boot only, no sets loaded
     75  1.3.4.2  tls -d	Add the debug sets
     76  1.3.4.2  tls -m	Mimimize disk writes for sd cards
     77  1.3.4.2  tls -x	Load the x sets too, not just the base ones
     78  1.3.4.1  tls EOF
     79  1.3.4.1  tls 	exit 1
     80      1.1  agc }
     81      1.1  agc 
     82  1.3.4.1  tls # First pass for options to get the host and src directories
     83  1.3.4.2  tls OPTS="K:D:S:bc:dh:ms:x"
     84  1.3.4.1  tls while getopts "$OPTS" f
     85  1.3.4.1  tls do
     86  1.3.4.1  tls 	case $f in
     87  1.3.4.1  tls 	h)	h="$OPTARG";;
     88  1.3.4.1  tls 	S)	src="$OPTARG";;
     89  1.3.4.1  tls 	*)	;;
     90      1.1  agc 	esac
     91      1.1  agc done
     92      1.1  agc 
     93  1.3.4.1  tls if [ -z "$h" ]
     94  1.3.4.1  tls then
     95  1.3.4.1  tls 	usage
     96      1.1  agc fi
     97      1.1  agc 
     98  1.3.4.1  tls if [ ! -f "${DIR}/conf/${h}.conf" ]
     99  1.3.4.1  tls then
    100  1.3.4.1  tls 	echo $PROG: ${DIR}/conf/${h}.conf is not present 1>&2
    101  1.3.4.1  tls 	exit 1
    102      1.1  agc fi
    103      1.1  agc 
    104  1.3.4.1  tls . "${DIR}/conf/${h}.conf"
    105      1.1  agc 
    106  1.3.4.1  tls selected_sets="$sets"
    107  1.3.4.2  tls dsets=false
    108  1.3.4.2  tls xsets=false
    109  1.3.4.2  tls minwrites=false
    110      1.1  agc 
    111  1.3.4.1  tls OPTIND=1
    112  1.3.4.1  tls while getopts "$OPTS" f
    113  1.3.4.1  tls do
    114  1.3.4.1  tls 	case $f in
    115  1.3.4.1  tls 	D)	release="$OPTARG";;
    116  1.3.4.1  tls 	K)	kernel="$OPTARG";;
    117  1.3.4.1  tls 	S)	;;
    118  1.3.4.2  tls 	b)	bootonly="true";;
    119  1.3.4.2  tls 	d)	dsets=true
    120  1.3.4.2  tls 		selected_sets="$selected_sets debug"
    121  1.3.4.2  tls 		if $xsets; then
    122  1.3.4.2  tls 			selected_sets="$selected_sets xdebug"
    123  1.3.4.2  tls 		fi
    124  1.3.4.2  tls 		;;
    125  1.3.4.1  tls 	c)	custom="$OPTARG";;
    126  1.3.4.1  tls 	h)	;;
    127  1.3.4.2  tls 	m)	minwrites=true;;
    128  1.3.4.1  tls 	s)	size="$OPTARG";;
    129  1.3.4.2  tls 	x)	xsets=true
    130  1.3.4.2  tls 		selected_sets="$selected_sets $xsets"
    131  1.3.4.2  tls 		if $dsets; then
    132  1.3.4.2  tls 		    selected_sets="$selected_sets xdebug"
    133  1.3.4.2  tls 		fi
    134  1.3.4.2  tls 		;;
    135  1.3.4.1  tls 	*)	usage;;
    136  1.3.4.1  tls 	esac
    137  1.3.4.1  tls done
    138      1.1  agc 
    139  1.3.4.1  tls shift $(( $OPTIND - 1 ))
    140  1.3.4.1  tls if [ -n "$1" ]; then
    141  1.3.4.1  tls 	# take the next argument as being the image name
    142  1.3.4.1  tls 	image="$1"
    143  1.3.4.1  tls 	shift
    144  1.3.4.1  tls fi
    145      1.1  agc 
    146  1.3.4.1  tls case "$image" in
    147  1.3.4.1  tls *.gz)	compress=true; image="${image%.gz}";;
    148  1.3.4.1  tls *)	compress=false;;
    149  1.3.4.1  tls esac
    150  1.3.4.1  tls 
    151  1.3.4.2  tls if [ -z "$bootonly" ]; then
    152  1.3.4.2  tls 	echo ${bar} configuring sets ${bar}
    153  1.3.4.2  tls 	(echo '/set type=dir uname=root gname=wheel mode=0755'
    154  1.3.4.2  tls 	for i in $selected_sets; do
    155  1.3.4.2  tls 		s="${release}/etc/mtree/set.$i"
    156  1.3.4.2  tls 		if [ -f "$s" ]; then
    157  1.3.4.2  tls 			cat "$s"
    158  1.3.4.2  tls 		fi
    159  1.3.4.2  tls 	done) > "$tmp/selected_sets"
    160  1.3.4.2  tls fi
    161  1.3.4.1  tls 
    162  1.3.4.1  tls make_fstab
    163  1.3.4.1  tls customize
    164  1.3.4.1  tls populate
    165  1.3.4.1  tls 
    166  1.3.4.2  tls if [ -z "$bootonly" ]; then
    167  1.3.4.2  tls 	(cd ${mnt}; ${MTREE} -N ${release}/etc -c -k all | 
    168  1.3.4.2  tls 	    ${MTREE} -N ${release}/etc -C -k all) >> "$tmp/selected_sets"
    169  1.3.4.2  tls fi
    170  1.3.4.1  tls if [ -n ${msdosid} ]; then
    171  1.3.4.1  tls 	echo ${bar} Populating msdos filesystem ${bar}
    172  1.3.4.1  tls 	${MAKEFS} -N ${release}/etc -t msdos \
    173  1.3.4.2  tls 	    -O $((${init} / 2))m -s $((${boot} / 2 + ${init} / 2))m ${image} ${mnt}/boot
    174  1.3.4.1  tls fi
    175  1.3.4.1  tls 
    176  1.3.4.2  tls if [ -z "$bootonly" ]; then
    177  1.3.4.2  tls 	echo ${bar} Populating ffs filesystem ${bar}
    178  1.3.4.2  tls 	${MAKEFS} -N ${release}/etc -t ffs -rx \
    179  1.3.4.2  tls 	    -O $(((${init} + ${boot} + ${swap}) / 2))m \
    180  1.3.4.2  tls 		-o d=4096 \
    181  1.3.4.2  tls 	    -b $((${extra}))m \
    182  1.3.4.2  tls 	    -F "$tmp/selected_sets" ${image} "${release}" "${mnt}"
    183  1.3.4.2  tls fi
    184      1.1  agc 
    185  1.3.4.1  tls if [ "${size}" = 0 ]; then
    186  1.3.4.1  tls 	size="$(getsize "${image}")"
    187      1.2  agc fi
    188  1.3.4.1  tls newsize=$((${size} / 2 / 1024))
    189      1.1  agc 
    190  1.3.4.1  tls echo ${bar} Adding label ${bar}
    191  1.3.4.1  tls make_label > ${tmp}/label
    192  1.3.4.1  tls ${DISKLABEL} -R -F ${image} ${tmp}/label
    193  1.3.4.1  tls if [ -n ${msdosid} ]; then
    194  1.3.4.1  tls 	echo ${bar} Running fdisk ${bar}
    195  1.3.4.1  tls 	initsecs=$((${init} * 1024))
    196  1.3.4.1  tls 	bootsecs=$((${boot} * 1024))
    197  1.3.4.1  tls 	${FDISK} -f -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image}
    198  1.3.4.1  tls fi
    199      1.1  agc 
    200  1.3.4.1  tls if $compress; then
    201  1.3.4.1  tls 	echo ${bar} Compressing image ${bar}
    202  1.3.4.1  tls 	rm -f "${image}.gz"
    203  1.3.4.2  tls 	${GZIP} -9 ${image}
    204  1.3.4.1  tls 	image="${image}.gz"
    205  1.3.4.1  tls fi
    206      1.1  agc 
    207  1.3.4.1  tls echo ${bar} Image is ${image} ${bar}
    208