Home | History | Annotate | Line # | Download | only in compile
walnut-mkimg.sh revision 1.4
      1  1.1       scw #!/bin/sh
      2  1.4  uebayasi # $NetBSD: walnut-mkimg.sh,v 1.4 2010/11/06 16:23:35 uebayasi Exp $
      3  1.1       scw 
      4  1.4  uebayasi # Convert an input to a TFTP image loadable by the IBM PowerPC OpenBIOS.
      5  1.2    simonb 
      6  1.2    simonb magic=5394511	# IBM OpenBIOS magic number 0x0052504f
      7  1.4  uebayasi start=0
      8  1.4  uebayasi size=0
      9  1.4  uebayasi overwrite=0
     10  1.1       scw 
     11  1.1       scw if [ $# -ne 2 ] ; then
     12  1.4  uebayasi 	echo usage: $0 input image 1>&2
     13  1.1       scw 	exit 1
     14  1.1       scw fi
     15  1.1       scw 
     16  1.4  uebayasi input=$1; shift
     17  1.1       scw output=$1; shift
     18  1.1       scw 
     19  1.1       scw : ${OBJDUMP=objdump}
     20  1.1       scw : ${OBJCOPY=objcopy}
     21  1.1       scw 
     22  1.4  uebayasi file=$( file $input )
     23  1.4  uebayasi case $file in
     24  1.4  uebayasi *:\ ELF\ *)
     25  1.4  uebayasi 	start=`${OBJDUMP} -f ${input} | awk '/start address/ { print $NF }'`
     26  1.4  uebayasi 	start=`printf "%d" $start`
     27  1.4  uebayasi 	${OBJCOPY} -O binary ${input} ${input}.bin.$$
     28  1.4  uebayasi 	;;
     29  1.4  uebayasi *)
     30  1.4  uebayasi 	case $file in
     31  1.4  uebayasi 	*\ [Ff]ile\ [Ss]ystem*|*\ [Ff]ilesystem*)
     32  1.4  uebayasi 		overwrite=1
     33  1.4  uebayasi 		;;
     34  1.4  uebayasi 	esac
     35  1.4  uebayasi 	cp ${input} ${input}.bin.$$
     36  1.4  uebayasi 	;;
     37  1.4  uebayasi esac
     38  1.4  uebayasi 
     39  1.4  uebayasi size=`stat -f '%z' ${input}.bin.$$`
     40  1.4  uebayasi size=$(( ( $size + 511 ) / 512 ))
     41  1.4  uebayasi 
     42  1.4  uebayasi enc()
     43  1.4  uebayasi {
     44  1.4  uebayasi 	local _x=$1; shift
     45  1.4  uebayasi 	printf $( printf '\\x%x' $_x )
     46  1.4  uebayasi }
     47  1.4  uebayasi 
     48  1.4  uebayasi be32enc()
     49  1.4  uebayasi {
     50  1.4  uebayasi 	local _x=$1; shift
     51  1.4  uebayasi 	enc $(( ( $_x >> 24 ) & 0xff ))
     52  1.4  uebayasi 	enc $(( ( $_x >> 16 ) & 0xff ))
     53  1.4  uebayasi 	enc $(( ( $_x >>  8 ) & 0xff ))
     54  1.4  uebayasi 	enc $(( ( $_x >>  0 ) & 0xff ))
     55  1.4  uebayasi }
     56  1.4  uebayasi 
     57  1.4  uebayasi {
     58  1.4  uebayasi 	be32enc $magic
     59  1.4  uebayasi 	be32enc $start
     60  1.4  uebayasi 	be32enc $size
     61  1.4  uebayasi 	be32enc 0
     62  1.4  uebayasi 	be32enc $start
     63  1.4  uebayasi 	be32enc 0
     64  1.4  uebayasi 	be32enc 0
     65  1.4  uebayasi 	be32enc 0
     66  1.4  uebayasi } > ${input}.hdr.$$
     67  1.4  uebayasi 
     68  1.4  uebayasi if [ $overwrite = 0 ]; then
     69  1.4  uebayasi 	cat ${input}.hdr.$$ ${input}.bin.$$ > ${output}
     70  1.4  uebayasi else
     71  1.4  uebayasi 	cp ${input}.bin.$$ ${output}
     72  1.4  uebayasi 	dd if=${input}.hdr.$$ of=${output} conv=notrunc
     73  1.4  uebayasi fi
     74  1.1       scw 
     75  1.4  uebayasi rm -f ${input}.hdr.$$ ${input}.bin.$$
     76  1.1       scw exit
     77