Bump master branch firmware version to 1.4.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / utility / bin2hex / bin2hex.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include <stdio.h>
36 #include <string.h>
37
38 #define MAX_READ_SIZE   80
39
40 unsigned long checksum = 0;
41
42 void write_file(FILE *out, unsigned char *buf, unsigned long size, unsigned char *endian, unsigned char nl)
43 {
44         int i=0;
45         unsigned char tmp_buf[4];
46
47         for(i=0; i<size; i+=4)
48         {
49
50                 if( nl==1 )
51                 {
52                         if(i%16 == 0) {
53                                 fprintf(out, "\n");
54                         }
55                     tmp_buf[0] = buf[i];
56                     tmp_buf[1] = buf[i+1];
57                     tmp_buf[2] = buf[i+2];
58                     tmp_buf[3] = buf[i+3];
59
60                         fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
61
62         } else {
63             
64             if(i%16 == 0) {
65                 fprintf(out, "\n");
66                         }
67                         tmp_buf[0] = buf[i+3];
68                         tmp_buf[1] = buf[i+2];
69                         tmp_buf[2] = buf[i+1];
70                         tmp_buf[3] = buf[i+0];
71                         fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
72                 }
73         checksum = checksum ^ *((unsigned long *)(&tmp_buf[0]));
74         }
75 }
76
77 void write_rom(FILE *out, FILE *in)
78 {
79         int size;
80         long file_size;
81         unsigned char buffer[MAX_READ_SIZE];
82         int multiple = 0;
83
84         file_size = size = 0;
85
86         while(1)
87         {
88                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
89                 file_size += size;
90
91                 //write_file(out, buffer, size, NULL, 0);
92                 if( size == 0 )
93                 {
94                         if (multiple)
95                                 fprintf(out, "%08X\n", checksum);
96
97                         goto ERR_DONE;
98                 }
99                 else if (size<MAX_READ_SIZE)
100                 {
101                     multiple = 0;
102                         write_file(out, buffer, size, NULL, 1);
103                         fprintf(out, "%08X\n", checksum);
104                         goto ERR_DONE;
105                 }
106                 else if (size==MAX_READ_SIZE)
107                 {
108                         multiple = 1;
109                         write_file(out, buffer, MAX_READ_SIZE, NULL, 1);
110             }
111             else
112                 goto ERR_DONE;
113         }
114
115 ERR_DONE:
116
117         return;
118 }
119
120
121 void write_array(FILE *out, FILE *in, unsigned char hif)
122 {
123         int size;
124         long file_size;
125         unsigned char buffer[MAX_READ_SIZE];
126         int multiple = 0;
127
128         file_size = size = 0;
129
130 //      fprintf(out, "#include \"80211core_sh.h\"\n");
131         fprintf(out, "#include <stdint.h>\n");
132         fprintf(out, "const uint32_t zcFwImage[] = {\n");
133         while(1)
134         {
135                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
136                 file_size += size;
137                 if( size == 0 )
138                 {
139                         if (multiple)
140                         {
141                                 fprintf(out, "0x%08X\n", checksum);
142                                 file_size += 4;
143                         }
144
145                         fprintf(out, "};\n");
146                         fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
147
148                         goto ERR_DONE;
149                 }
150                 else if (size<MAX_READ_SIZE)
151                 {
152                         multiple = 0;
153
154                         write_file(out, buffer, size, NULL, hif);
155                         fprintf(out, "0x%08X\n", checksum);
156
157                         if( (size%4)!=0 )
158                             file_size += (4-(size%4));
159
160                         file_size += 4;
161                         fprintf(out, "};\n");
162                         fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
163
164                         goto ERR_DONE;
165                 }
166                 else if (size==MAX_READ_SIZE)
167                 {
168                         multiple = 1;
169                         write_file(out, buffer, MAX_READ_SIZE, NULL, hif);
170                 }
171                 else
172                         goto ERR_DONE;
173         }
174
175 ERR_DONE:
176         return;
177 }
178
179
180 int main(int argc, char* argv[])
181 {
182         FILE *in, *out;
183         int retVal;
184         int i=0;
185         char input_file_name[80];
186         char output_file_name[80];
187
188         in = out = 0x0;
189
190         if( argc < 3 )
191         {
192                 printf("\"bin2hex [input_file] [output_file] - gen array data\"!\n\r");
193                 printf("\"bin2hex [input_file] [output_file] [rom]- gen rom code\"!\n\r");
194                 goto ERR_DONE;
195         }
196         strcpy(input_file_name, argv[1]);
197         strcpy(output_file_name, argv[2]);
198
199         printf("bin2h %s %s!\n\r", input_file_name, output_file_name);
200
201         if((in = fopen(input_file_name,"rb")) == NULL)
202                 goto ERR_DONE;
203
204         if((out = fopen(output_file_name,"wt")) == NULL)
205                 goto ERR_DONE;
206
207         if( !strcmp(argv[3],"rom"))
208         /* for loading into RAM directly, e.g ROM code or patch code */
209         write_rom(out, in);     
210     else { 
211         if(!strcmp(argv[4],"usb")) 
212             write_array(out, in, 1);    /* for generating firmware (usb) */
213         else if (!strcmp(argv[4],"pci")) 
214             write_array(out, in, 0);    /* for generating firmware (pci) */
215         else
216             write_array(out, in, 1);    /* Default case firmware (usb) */
217     }
218
219 ERR_DONE:
220
221         if(in) fclose(in);
222         if(out) fclose(out);
223
224         return 0;
225
226 }