#!/bin/bash
#
# $Id: elf2hex,v 1.1 2006/02/01 21:31:47 cnguyen Exp $
#
# This script takes an elf file and from it generates a loadable
# .hex file.
#
# Usage:
#
# % elf2hex file.arm [-d]
#
#           file.arm        Input arm elf file
#           -d              Just dump, no output file
#           -max NN         Zero pad output hex file to this length
#

# dump="/home/tako/hdk/linux/arm-elf/gcc-3.4.3/arm-elf/bin/objdump"
# list2hex="${PROJECT_ROOT}/main/test/sys/arm/tools/list2hex.pl"
dump="/opt/crosstool/arm-elf/bin/objdump"
list2hex="${ROOT}/../sw/tools/conv/list2hex.pl"
object=""
justdump=0
makehex=1
njl_verbose=1
list2hex_flags=""

if [ $# -le 0 ]; then
    echo usage\: $0 elf_file 
    exit 1
else
    object=$1
    if [ $njl_verbose -gt 0 ]; then
	echo new object $1
    fi
    shift
fi
while [ $# -gt 0 ]; do
    if [ "X$1" == "X-d" ]; then
	justdump=1
	makehex=0
    fi
    if [ "X$1" == "X-max" ]; then
	shift
	list2hex_flags="${list2hex_flags} -max $1"
    fi
    shift
done
if [ $njl_verbose -gt 0 ]; then
        echo dumping $object
fi
outfile=${object%%.*}.hex
dumpfile=${object%%.*}.list
if [ $njl_verbose -gt 0 ]; then
        echo outfile $outfile
fi

if [ $justdump -eq 1 ]; then
        if [ $njl_verbose -gt 0 ]; then
                echo just dump
        fi
        ${dump} -D $object
else
        if [ $makehex -eq 1 ]; then
                if [ $njl_verbose -gt 0 ]; then
                        echo process
                fi
                ${dump} -D $object > $dumpfile
		${list2hex} $dumpfile $outfile ${list2hex_flags}
        fi
fi

# eof
