Home | History | Annotate | Line # | Download | only in compile
      1 #!/bin/sh
      2 # $NetBSD: walnut-mkimg.sh,v 1.6 2024/12/11 00:06:58 maya 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 : ${STAT=stat}
     22 : ${AWK=awk}
     23 : ${FILE=file}
     24 
     25 file=$( ${FILE} $input )
     26 case $file in
     27 *:\ ELF\ *)
     28 	start=`${OBJDUMP} -f ${input} | ${AWK} '/start address/ { print $NF }'`
     29 	start=`printf "%d" $start`
     30 	${OBJCOPY} -O binary ${input} ${input}.bin.$$
     31 	;;
     32 *)
     33 	case $file in
     34 	*\ [Ff]ile\ [Ss]ystem*|*\ [Ff]ilesystem*)
     35 		overwrite=1
     36 		;;
     37 	esac
     38 	cp ${input} ${input}.bin.$$
     39 	;;
     40 esac
     41 
     42 size=$(${STAT} -f '%z' ${input}.bin.$$)
     43 size=$(( ( $size + 511 ) / 512 ))
     44 
     45 enc()
     46 {
     47 	local _x=$1; shift
     48 	printf $( printf '\\x%x' $_x )
     49 }
     50 
     51 be32enc()
     52 {
     53 	local _x=$1; shift
     54 	enc $(( ( $_x >> 24 ) & 0xff ))
     55 	enc $(( ( $_x >> 16 ) & 0xff ))
     56 	enc $(( ( $_x >>  8 ) & 0xff ))
     57 	enc $(( ( $_x >>  0 ) & 0xff ))
     58 }
     59 
     60 {
     61 	be32enc $magic
     62 	be32enc $start
     63 	be32enc $size
     64 	be32enc 0
     65 	be32enc $start
     66 	be32enc 0
     67 	be32enc 0
     68 	be32enc 0
     69 } > ${input}.hdr.$$
     70 
     71 if [ $overwrite = 0 ]; then
     72 	cat ${input}.hdr.$$ ${input}.bin.$$ > ${output}
     73 else
     74 	cp ${input}.bin.$$ ${output}
     75 	dd if=${input}.hdr.$$ of=${output} conv=notrunc
     76 fi
     77 
     78 rm -f ${input}.hdr.$$ ${input}.bin.$$
     79 exit
     80