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