usb: gadget: Allow to build multiple legacy gadgets
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / scripts / sprd_mkdzimage.sh
1 #!/bin/bash
2
3
4 ## Functions
5 function write_to_4bytes_binary()
6 {
7         HEX=`echo "obase=16; $1" | bc`
8
9         NUM=$((8-${#HEX}))
10
11         ZERO="00000000"
12         SUB=${ZERO:0:$NUM}
13
14         HEX=$SUB$HEX
15
16         for str in $(echo $HEX | sed 's/../& /g' | rev); do
17                 str=$(echo -en $str | rev)
18                 echo -en "\x$str"
19         done > $2
20 }
21
22 function write_to_padding_binary()
23 {
24         rm -f padding
25
26         PAD_SIZE=$(($(($PAD - $(($1 % $PAD)))) % $PAD))
27         if [ $PAD_SIZE -gt 0 ]; then
28                 dd if=/dev/zero of=./padding bs=1 count=$PAD_SIZE 2>/dev/zero
29         else
30                 touch padding
31         fi
32
33         echo -en " | PAD: $PAD_SIZE[B]\n"
34 }
35
36 function help() {
37         echo "sprd_mkdzimage.sh [OPTIONS] -o <output file> -k <kernel file>"
38         echo "  options:"
39         echo "  --output-file/-o    output file"
40         echo "  --kernel/-k         kernel file"
41         echo "  --devicetree/-d     devicetree file"
42         echo "  --help/-h           this help screen"
43         exit 0
44 }
45
46
47 ## Defines
48 OUT="dzImage"
49 OUT_TMP="dzImage.tmp"
50 OUT_DIR="./arch/arm/boot"
51
52 MAGIC="NZIT"            # 0x54495A4E
53 KERNEL_ADDR=32768       # 0x00008000
54 ATAGS_ADDR=31457280     # 0x01e00000
55
56 PAD=2048
57
58
59 if [ "$#" -eq "0" ]; then
60         help
61 fi
62
63
64 ## Input parameters
65 params="$(getopt -o o:k:d:h -l output-file:,:kernel:,devicetree:,help --name "$0" -- "$@")"
66 eval set -- "$params"
67 while true
68 do
69         case "$1" in
70                 -o|--output-file)
71                         OUTPUT_FILE=$2
72                         shift 2
73                         OUT_DIR=`dirname $OUTPUT_FILE`
74                         if [ ! -d "$OUT_DIR" ]; then
75                                 echo "invalid output path '$OUT_DIR'."
76                                 exit 1
77                         fi
78                         OUT=${OUTPUT_FILE#${OUT_DIR}}
79                         OUT=${OUT//\//}
80                         OUT_TMP=${OUT}.tmp
81                         ;;
82                 -k|--kernel)
83                         KERNEL=$2
84                         shift 2
85                         ;;
86                 -d|--devicetree)
87                         DEVICETREE=$2
88                         shift 2
89                         ;;
90                 -h|--help)
91                         help
92                         ;;
93                 --)
94                         shift
95                         break
96                         ;;
97                 *)
98                         help
99                         ;;
100         esac
101 done
102
103 if [ "z${KERNEL}" == "z" ] || [ "z${DEVICETREE}" == "z" ]; then
104         help
105 fi
106
107 if [ ! -e "${KERNEL}" ]; then
108         echo -en "kernel '${KERNEL}' not found.\nexit\n"
109         exit -1
110 fi
111
112 if [ ! -e "${DEVICETREE}" ]; then
113         echo -en "devicetree '${DEVICETREE}' not found.\nexit\n"
114         exit -1
115 fi
116
117
118 ## Header
119 rm -f $OUT
120 rm -f $OUT_TMP
121 touch $OUT_TMP
122
123 HEADER_SIZE=28
124
125 echo -en " *HEADER "
126 echo -en "$HEADER_SIZE[B]\n"
127
128 echo -en $MAGIC > $OUT
129 cat $OUT >> $OUT_TMP
130 write_to_4bytes_binary $KERNEL_ADDR $OUT
131 cat $OUT >> $OUT_TMP
132
133 FILE="${KERNEL}"
134 SIZE=`du -b $FILE | awk '{print $1}'`
135 write_to_4bytes_binary $SIZE $OUT
136 cat $OUT >> $OUT_TMP
137
138 DTB_ADDR=$(($KERNEL_ADDR + $SIZE))
139 write_to_4bytes_binary $DTB_ADDR $OUT
140 cat $OUT >> $OUT_TMP
141
142 FILE="${DEVICETREE}"
143 SIZE=`du -b $FILE | awk '{print $1}'`
144 write_to_4bytes_binary $SIZE $OUT
145 cat $OUT >> $OUT_TMP
146
147 write_to_4bytes_binary $ATAGS_ADDR $OUT
148 cat $OUT >> $OUT_TMP
149 write_to_4bytes_binary $PAD $OUT
150 cat $OUT >> $OUT_TMP
151
152 write_to_padding_binary $HEADER_SIZE
153 cat $OUT_TMP padding > $OUT
154
155
156 ## Kernel Binary
157 FILE="${KERNEL}"
158 FILE_PATH=`dirname ${KERNEL}`
159 FILE_NAME=${FILE#${FILE_PATH}}
160 FILE_NAME=${FILE_NAME//\//}
161
162 echo -en " *${FILE_NAME} "
163 cat $OUT $FILE > $OUT_TMP
164
165 SIZE=`du -b $FILE | awk '{print $1}'`
166 echo -en "$SIZE[B]\n"
167
168 write_to_padding_binary $SIZE
169 cat $OUT_TMP padding > $OUT
170
171
172 ## Devicetree Binary
173 FILE="${DEVICETREE}"
174 FILE_PATH=`dirname ${DEVICETREE}`
175 FILE_NAME=${FILE#${FILE_PATH}}
176 FILE_NAME=${FILE_NAME//\//}
177
178 echo -en " *${FILE_NAME} "
179 cat $OUT $FILE > $OUT_TMP
180
181 SIZE=`du -b $FILE | awk '{print $1}'`
182 echo -en "$SIZE[B]\n"
183
184 write_to_padding_binary $SIZE
185 cat $OUT_TMP padding > $OUT
186
187
188 ## END
189 rm -f $OUT_TMP
190 rm -f padding
191 rm -f $OUT_DIR/$OUT
192 mv -f $OUT $OUT_DIR/
193
194 S=`du -b $OUT_DIR/$OUT | awk '{print $1}'`
195 S_K=$(($S/1024))
196 echo -en "## OUT: $OUT size: $S[B]; $S_K[KB]\n"