1#!/bin/sh 2# $NetBSD: walnut-mkimg.sh,v 1.4 2010/11/06 16:23:35 uebayasi Exp $ 3 4# Convert an input to a TFTP image loadable by the IBM PowerPC OpenBIOS. 5 6magic=5394511 # IBM OpenBIOS magic number 0x0052504f 7start=0 8size=0 9overwrite=0 10 11if [ $# -ne 2 ] ; then 12 echo usage: $0 input image 1>&2 13 exit 1 14fi 15 16input=$1; shift 17output=$1; shift 18 19: ${OBJDUMP=objdump} 20: ${OBJCOPY=objcopy} 21 22file=$( file $input ) 23case $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 ;; 37esac 38 39size=`stat -f '%z' ${input}.bin.$$` 40size=$(( ( $size + 511 ) / 512 )) 41 42enc() 43{ 44 local _x=$1; shift 45 printf $( printf '\\x%x' $_x ) 46} 47 48be32enc() 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 68if [ $overwrite = 0 ]; then 69 cat ${input}.hdr.$$ ${input}.bin.$$ > ${output} 70else 71 cp ${input}.bin.$$ ${output} 72 dd if=${input}.hdr.$$ of=${output} conv=notrunc 73fi 74 75rm -f ${input}.hdr.$$ ${input}.bin.$$ 76exit 77