Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / utility / bin2hex.pl
1 #!/usr/bin/perl -w
2 ###############################################################
3 #binary-to-hex perl tool: obtained & modified on 10/05/08
4 ###############################################################
5 use strict;
6
7 my ($format, $binfile, $outfile, $arr_form, $ColNum, $show_hex, $filesize);
8 $format = $ARGV[0] || usage();
9 $binfile = $ARGV[1] || usage();
10 $outfile = $ARGV[2] || $binfile.".$format";
11 $arr_form = $ARGV[3];
12 $ColNum = $ARGV[4] || usage();
13 $show_hex = $ARGV[5];
14
15 my ($orig, @converted);
16
17     $orig = readdata ($binfile);
18
19     $orig = hexdump($orig);
20     if ($show_hex) {print "Original Binary text:\n\t $orig","\n\n";}
21
22     if ($format eq 'a' || $format eq 'asm') {
23         @converted = convert_to_asm ($orig);
24
25     } elsif ($format eq 'c') {
26         @converted = convert_to_c ($orig);
27
28     } else {
29         print "Unknown format to convert!\n";
30         exit (-1);
31     }
32     if ($show_hex) {print "Converted hex text:\n",join ('', @converted), "\n";}
33
34     writedata ($outfile, @converted);
35
36 sub convert_to_asm {
37     my @data = split(' ', join (' ', @_)); #nop here only one list passed to join
38
39     my $i = 0;
40     if ($arr_form){
41       foreach (@data) {
42           if ($i++ < 8) {
43             $_=$_."h, 0";
44           } else {
45             $_=$_."h\nbyte 0";
46             $i = 0;
47           }
48       }
49
50       unshift (@data, "byte 0");
51       $data[-1] =~s/[,|\nbyte] 0$//g;
52     }else{
53       foreach (@data){
54         if ($i++ < $ColNum-1) {
55           #$_.=",";
56         }else {
57           $_.="\n";
58           $i = 0;
59         }
60       }      
61     }  
62     return @data;
63 }
64
65 sub convert_to_c {
66     my @data = split(' ', join (' ', @_)); #nop here only one list passed to join
67
68     my $i = 0;
69     if ($arr_form){
70       foreach (@data) {
71         if ($i++ < $ColNum-1) {
72             $_.=", 0x";
73         } else {
74             $_.=",\n\t0x";
75             $i = 0;
76         }
77       }
78       unshift (@data, "unsigned char data[$filesize] = {\n\t0x");  #add some pattern at the front of @data
79       $data[-1] =~s/0x$//g;
80       $data[-1] =~s/[ |\n\t]//g;
81       $data[-1] =~s/\,//g;
82       push (@data, "\n};");
83     }else{
84       foreach (@data){
85         if ($i++ < $ColNum-1) {
86         }else {
87           $_.="\n";
88           $i = 0;
89         }
90       }      
91     }
92     return @data;
93 }
94
95 sub readdata {
96     my ($line);
97     my ($file) = @_;
98     #printf "dbg:file = $file\n";
99     open (BF, "$file") || die "Cannot open $file: $!";  #$! contains current value of errno
100     binmode (BF);
101     $filesize = (stat($file))[7];
102     my ($DATA) = ""; #<BF>;
103     my (@Data_check) = <BF>;
104     foreach $line (@Data_check){
105       #printf "dbg:line = $line\n";
106       $DATA.=$line;
107     }  
108     #printf "dbg:DATA string = $DATA\n";
109     close (BF);
110     return ($DATA);
111 }
112
113 sub writedata {
114     my ($file, @FomatData) = @_;
115     open (AF, ">$file") || die "Cannot open $file: $!";
116     my $i = 0;
117     my $b0 = 0;    
118     my $b1 = 0; 
119     my $b2 = 0; 
120     my $b3 = 0;        
121     foreach (@FomatData) {
122       if($ColNum eq '1') {
123         if($i == 0) {
124           $b0 = $_;
125           $i++;
126         } else {
127         if($i == 1) {
128           $b1 = $_;
129           $i++;
130         } else {
131         if($i == 2) {
132           $b2 = $_;
133           $i++;
134         } else {
135           print AF "$_";
136           print AF "$b2";
137           print AF "$b1";
138           print AF "$b0";
139           $i = 0;
140         }}}
141        }else{
142           print AF "$_";
143        }
144     }
145     close (AF);
146 }
147
148 sub hexdump
149 {
150   join ' ', map { sprintf "%02X", $_ } unpack "C*", $_[0];
151 }
152
153 sub usage {
154     print STDERR <<EOF;
155     
156 Usage: bin2hex format binfile outfile
157      format      format to convert to, 
158                  asm ==> 'assembly', 
159                  c ==> 'C'
160      binfile     binary file you want to convert.
161      outfile     output file to store the result of output.
162      arr_form    displayed in array-form
163      ColNum      num of columns of the shown array
164
165 EOF
166    exit(-1);
167 }
168