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