3 # Copyright (c) 2013 Qualcomm Atheros, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted (subject to the limitations in the
8 # disclaimer below) provided that the following conditions are met:
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the
18 # * Neither the name of Qualcomm Atheros nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
22 # NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
23 # GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
24 # HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
25 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34 # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 ###############################################################
37 #binary-to-hex perl tool: obtained & modified on 10/05/08
38 ###############################################################
41 my ($format, $binfile, $outfile, $arr_form, $ColNum, $show_hex, $filesize);
42 $format = $ARGV[0] || usage();
43 $binfile = $ARGV[1] || usage();
44 $outfile = $ARGV[2] || $binfile.".$format";
46 $ColNum = $ARGV[4] || usage();
49 my ($orig, @converted);
51 $orig = readdata ($binfile);
53 $orig = hexdump($orig);
54 if ($show_hex) {print "Original Binary text:\n\t $orig","\n\n";}
56 if ($format eq 'a' || $format eq 'asm') {
57 @converted = convert_to_asm ($orig);
59 } elsif ($format eq 'c') {
60 @converted = convert_to_c ($orig);
63 print "Unknown format to convert!\n";
66 if ($show_hex) {print "Converted hex text:\n",join ('', @converted), "\n";}
68 writedata ($outfile, @converted);
71 my @data = split(' ', join (' ', @_)); #nop here only one list passed to join
84 unshift (@data, "byte 0");
85 $data[-1] =~s/[,|\nbyte] 0$//g;
88 if ($i++ < $ColNum-1) {
100 my @data = split(' ', join (' ', @_)); #nop here only one list passed to join
105 if ($i++ < $ColNum-1) {
112 unshift (@data, "unsigned char data[$filesize] = {\n\t0x"); #add some pattern at the front of @data
113 $data[-1] =~s/0x$//g;
114 $data[-1] =~s/[ |\n\t]//g;
116 push (@data, "\n};");
119 if ($i++ < $ColNum-1) {
132 #printf "dbg:file = $file\n";
133 open (BF, "$file") || die "Cannot open $file: $!"; #$! contains current value of errno
135 $filesize = (stat($file))[7];
136 my ($DATA) = ""; #<BF>;
137 my (@Data_check) = <BF>;
138 foreach $line (@Data_check){
139 #printf "dbg:line = $line\n";
142 #printf "dbg:DATA string = $DATA\n";
148 my ($file, @FomatData) = @_;
149 open (AF, ">$file") || die "Cannot open $file: $!";
155 foreach (@FomatData) {
184 join ' ', map { sprintf "%02X", $_ } unpack "C*", $_[0];
190 Usage: bin2hex format binfile outfile
191 format format to convert to,
194 binfile binary file you want to convert.
195 outfile output file to store the result of output.
196 arr_form displayed in array-form
197 ColNum num of columns of the shown array