Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / utility / bin2hex.pl
1 #!/usr/bin/perl -w
2
3 # Copyright (c) 2013 Qualcomm Atheros, Inc.
4 # All rights reserved.
5 #
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:
9 #
10 #  * Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 #
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
16 #    distribution.
17 #
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.
21 #
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.
35
36 ###############################################################
37 #binary-to-hex perl tool: obtained & modified on 10/05/08
38 ###############################################################
39 use strict;
40
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";
45 $arr_form = $ARGV[3];
46 $ColNum = $ARGV[4] || usage();
47 $show_hex = $ARGV[5];
48
49 my ($orig, @converted);
50
51     $orig = readdata ($binfile);
52
53     $orig = hexdump($orig);
54     if ($show_hex) {print "Original Binary text:\n\t $orig","\n\n";}
55
56     if ($format eq 'a' || $format eq 'asm') {
57         @converted = convert_to_asm ($orig);
58
59     } elsif ($format eq 'c') {
60         @converted = convert_to_c ($orig);
61
62     } else {
63         print "Unknown format to convert!\n";
64         exit (-1);
65     }
66     if ($show_hex) {print "Converted hex text:\n",join ('', @converted), "\n";}
67
68     writedata ($outfile, @converted);
69
70 sub convert_to_asm {
71     my @data = split(' ', join (' ', @_)); #nop here only one list passed to join
72
73     my $i = 0;
74     if ($arr_form){
75       foreach (@data) {
76           if ($i++ < 8) {
77             $_=$_."h, 0";
78           } else {
79             $_=$_."h\nbyte 0";
80             $i = 0;
81           }
82       }
83
84       unshift (@data, "byte 0");
85       $data[-1] =~s/[,|\nbyte] 0$//g;
86     }else{
87       foreach (@data){
88         if ($i++ < $ColNum-1) {
89           #$_.=",";
90         }else {
91           $_.="\n";
92           $i = 0;
93         }
94       }      
95     }  
96     return @data;
97 }
98
99 sub convert_to_c {
100     my @data = split(' ', join (' ', @_)); #nop here only one list passed to join
101
102     my $i = 0;
103     if ($arr_form){
104       foreach (@data) {
105         if ($i++ < $ColNum-1) {
106             $_.=", 0x";
107         } else {
108             $_.=",\n\t0x";
109             $i = 0;
110         }
111       }
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;
115       $data[-1] =~s/\,//g;
116       push (@data, "\n};");
117     }else{
118       foreach (@data){
119         if ($i++ < $ColNum-1) {
120         }else {
121           $_.="\n";
122           $i = 0;
123         }
124       }      
125     }
126     return @data;
127 }
128
129 sub readdata {
130     my ($line);
131     my ($file) = @_;
132     #printf "dbg:file = $file\n";
133     open (BF, "$file") || die "Cannot open $file: $!";  #$! contains current value of errno
134     binmode (BF);
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";
140       $DATA.=$line;
141     }  
142     #printf "dbg:DATA string = $DATA\n";
143     close (BF);
144     return ($DATA);
145 }
146
147 sub writedata {
148     my ($file, @FomatData) = @_;
149     open (AF, ">$file") || die "Cannot open $file: $!";
150     my $i = 0;
151     my $b0 = 0;    
152     my $b1 = 0; 
153     my $b2 = 0; 
154     my $b3 = 0;        
155     foreach (@FomatData) {
156       if($ColNum eq '1') {
157         if($i == 0) {
158           $b0 = $_;
159           $i++;
160         } else {
161         if($i == 1) {
162           $b1 = $_;
163           $i++;
164         } else {
165         if($i == 2) {
166           $b2 = $_;
167           $i++;
168         } else {
169           print AF "$_";
170           print AF "$b2";
171           print AF "$b1";
172           print AF "$b0";
173           $i = 0;
174         }}}
175        }else{
176           print AF "$_";
177        }
178     }
179     close (AF);
180 }
181
182 sub hexdump
183 {
184   join ' ', map { sprintf "%02X", $_ } unpack "C*", $_[0];
185 }
186
187 sub usage {
188     print STDERR <<EOF;
189     
190 Usage: bin2hex format binfile outfile
191      format      format to convert to, 
192                  asm ==> 'assembly', 
193                  c ==> 'C'
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
198
199 EOF
200    exit(-1);
201 }
202