• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3if [ $# != 2 ]
4then
5echo "elftobin.sh <input_elf_file> <output_img_file>"
6exit
7fi
8
9if [ $XTENSA_CORE == "" ]
10then
11echo "XTENSA_CORE is not set"
12exit
13fi
14
15if [ $XTENSA_SYSTEM == "" ]
16then
17echo "XTENSA_SYSTEM is not set"
18exit
19fi
20
21export INPUT_ELF_FILE=$1
22export OUTPUT_BIN_FILE=__hikey_temp.bin
23export OUTPUT_IMG_FILE=$2
24export OP_DIR=./
25
26xt-objdump -h $INPUT_ELF_FILE > __elfdump
27less __elfdump | sed -n '/ALLOC/{g;1!p;};h' > __header
28less __header | awk '{print $2 " " $3 " " $4}' > __proc
29
30sections=`less __proc | cut -f1 -d' ' | tr '\n' ',' | sed 's/\,$//g'`
31SECTION_NUM=`awk 'END {print NR}' __proc`
32echo "Total number of sections are $SECTION_NUM"
33#sections=(".dram0.data" ".ResetVector.text" ".Reset.literal" ".WindowVectors.text" ".Level2InterruptVector.literal" ".Level2InterruptVector.text" ".Level3InterruptVector.literal" ".Level3InterruptVector.text" ".DebugExceptionVector.literal" ".DebugExceptionVector.text" ".NMIExceptionVector.literal" ".NMIExceptionVector.text" ".KernelExceptionVector.literal" ".KernelExceptionVector.text" ".UserExceptionVector.literal" ".UserExceptionVector.text" ".DoubleExceptionVector.literal" ".DoubleExceptionVector.text" ".Reset.text" ".iram0.text" ".rodata" ".text" ".data" ".bss")
34#dst_addr=("0xe8058000" "0xe8080000" "0xe8080300" "0xe8080400" "0xe8080578" "0xe8080580" "0xe80805b8" "0xe80805c0" "0xe8080638" "0xe8080640" "0xe8080678" "0xe80806c0" "0xe80806f8" "0xe8080700" "0xe8080738" "0xe8080740" "0xe8080778" "0xe80807c0" "0xe8080800" "0xe8080a44" "0xc0000000" "0xc0000cc8" "0xc00025c0" "0xc0002a20")
35
36ID_Sec=("0x01010000" "0x01000001" "0x01000002" "0x01000003" "0x01000004" "0x01000005" "0x01000006" "0x01000007" "0x01000008" "0x01000009" "0x0100000A" "0x0100000B" "0x0100000C" "0x0100000D" "0x0100000E" "0x0100000F" "0x01000010" "0x01000011" "0x01000012" "0x01000013" "0x00000014" "0x00000015" "0x00010016" "0x00020017")
37
38head -c "4" /dev/zero > __zero.bin
39
40i=0
41for j in `seq 1 $SECTION_NUM`
42do
43	VAR=`less __proc | awk 'FNR == "'"$j"'" {print $1}'`
44	VARBIN=`echo "$VAR" | sed 's/^./__/' | sed "s/\..*//" | sed 's/$/.bin/' `
45	#echo "section: $VAR  $VARBIN"
46	xt-objcopy $INPUT_ELF_FILE -O binary --only-section $VAR $VARBIN
47
48	size_sec[${i}]=$(cat $VARBIN | wc -c)
49	#echo "size : ${size_sec[${i}]}"
50    mod0=`expr ${size_sec[${i}]} % 4`
51    nbytes=`expr 4 - $mod0`
52	if [ "$nbytes" -ne "4" ] ; then
53		tail -c $nbytes __zero.bin >> $VARBIN
54		size_sec[$i]=`expr ${size_sec[${i}]} + $nbytes`
55	fi
56	cat $VARBIN >> $OUTPUT_BIN_FILE
57	i=`expr $i + 1`;
58done
59cd $OP_DIR
60
61tmp1=$(cat $OUTPUT_BIN_FILE | wc -c)
62total_sections=$SECTION_NUM
63main_header_size=32
64section_header_size=16
65size_total_img=`expr $main_header_size + $tmp1 + $total_sections \* $section_header_size`
66current_date_time=`date "+%Y/%m/%d %H:%M:%S"`
67echo "HIF:$current_date_time" > $OUTPUT_IMG_FILE
68
69printf "0: %.8x" $size_total_img | sed -e 's/0\: \(..\)\(..\)\(..\)\(..\)/0\: \4\3\2\1/' | xxd -r -g0 >> $OUTPUT_IMG_FILE
70printf "0: %.8x" $total_sections | sed -e 's/0\: \(..\)\(..\)\(..\)\(..\)/0\: \4\3\2\1/' | xxd -r -g0 >> $OUTPUT_IMG_FILE
71
72cnt1=`expr $main_header_size + $total_sections \* $section_header_size`
73i=0
74for j in `seq 1 $SECTION_NUM`
75do
76	DST_ADDR=`less __proc | awk 'FNR == "'"$j"'" {print $3}'`
77	printf "0: %.8x" ${ID_Sec[${i}]} | sed -e 's/0\: \(..\)\(..\)\(..\)\(..\)/0\: \4\3\2\1/' | xxd -r -g0 >> $OUTPUT_IMG_FILE
78	printf "0: %.8x" $cnt1 | sed -e 's/0\: \(..\)\(..\)\(..\)\(..\)/0\: \4\3\2\1/' | xxd -r -g0 >> $OUTPUT_IMG_FILE
79	printf "0: %.8x" 0x$DST_ADDR | sed -e 's/0\: \(..\)\(..\)\(..\)\(..\)/0\: \4\3\2\1/' | xxd -r -g0 >> $OUTPUT_IMG_FILE
80	printf "0: %.8x" ${size_sec[${i}]} | sed -e 's/0\: \(..\)\(..\)\(..\)\(..\)/0\: \4\3\2\1/' | xxd -r -g0 >> $OUTPUT_IMG_FILE
81	cnt_prev=$cnt1
82	cnt1=`expr ${size_sec[${i}]} + $cnt_prev`
83	i=`expr $i + 1`
84done
85
86cat $OUTPUT_BIN_FILE >> $OUTPUT_IMG_FILE
87rm $OUTPUT_BIN_FILE
88rm __*.bin
89rm __proc
90rm __elfdump
91rm __header
92
93echo "elftobin successful!"
94
95