Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / utility / bin2hex / bin2hex_swp.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                 if( nl==1 )
50                 {
51                         if(i%16 == 0){
52                                 fprintf(out, "\n");
53                         }
54
55
56                     tmp_buf[0] = buf[i];
57                     tmp_buf[1] = buf[i+1];
58                     tmp_buf[2] = buf[i+2];
59                     tmp_buf[3] = buf[i+3];
60 #if 0
61             if( i+4>=size)
62             {
63                 if(i%4==3)
64                 {
65                                 tmp_buf[3] = 0x0;   // padding
66                                 printf("3: i:%d size:%d\n\r", i, size);
67                 }
68                 else if (i%4==2)
69                 {
70                                 tmp_buf[2] = 0x0;   // padding
71                                 tmp_buf[3] = 0x0;   // padding
72                                 printf("2: i:%d size:%d\n\r", i, size);
73                 }
74                 else if (i%4==1)
75                 {
76                                 tmp_buf[1] = 0x0;   // padding
77                                 tmp_buf[2] = 0x0;   // padding
78                                 tmp_buf[3] = 0x0;   // padding
79                                 printf("1: i:%d size:%d\n\r", i, size);
80                 }
81             }
82 #endif
83                         fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
84                 }
85                 else
86                 {
87
88                         if(i%16 == 0){
89                                 fprintf(out, "\n");
90                         }
91
92                         tmp_buf[0] = buf[i+3];
93                         tmp_buf[1] = buf[i+2];
94                         tmp_buf[2] = buf[i+1];
95                         tmp_buf[3] = buf[i+0];
96 #if 0
97             if( i+4>=size)
98             {
99                 if(i%4==3)
100                 {
101                                 tmp_buf[0] = 0x0;   // padding
102                 }
103                 else if (i%4==2)
104                 {
105                                 tmp_buf[0] = 0x0;   // padding
106                                 tmp_buf[1] = 0x0;   // padding
107                 }
108                 else if (i%4==1)
109                 {
110                     tmp_buf[0] = 0x0;   // padding
111                                 tmp_buf[1] = 0x0;   // padding
112                                 tmp_buf[2] = 0x0;   // padding
113                 }
114             }
115             else
116             {
117
118             }
119 #endif
120                         fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
121                 }
122         checksum = checksum ^ *((unsigned long *)(&tmp_buf[0]));
123         }
124 }
125
126 void write_rom(FILE *out, FILE *in)
127 {
128         int size;
129         long file_size;
130         unsigned char buffer[MAX_READ_SIZE];
131         int multiple = 0;
132
133         file_size = size = 0;
134
135         while(1)
136         {
137                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
138                 file_size += size;
139
140                 //write_file(out, buffer, size, NULL, 0);
141                 if( size == 0 )
142                 {
143                         if (multiple)
144                                 fprintf(out, "%08X\n", checksum);
145
146                         goto ERR_DONE;
147                 }
148                 else if (size<MAX_READ_SIZE)
149                 {
150                     multiple = 0;
151                         write_file(out, buffer, size, NULL, 0);
152                         fprintf(out, "%08X\n", checksum);
153                         goto ERR_DONE;
154                 }
155                 else if (size==MAX_READ_SIZE)
156                 {
157                         multiple = 1;
158                         write_file(out, buffer, MAX_READ_SIZE, NULL, 0);
159             }
160             else
161                 goto ERR_DONE;
162         }
163
164 ERR_DONE:
165
166         return;
167 }
168
169
170 void write_array(FILE *out, FILE *in)
171 {
172         int size;
173         long file_size;
174         unsigned char buffer[MAX_READ_SIZE];
175         int multiple = 0;
176
177         file_size = size = 0;
178
179 //      fprintf(out, "#include \"80211core_sh.h\"\n");
180         fprintf(out, "const unsigned long zcFwImage[] = {\n");
181         while(1)
182         {
183                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
184                 file_size += size;
185                 if( size == 0 )
186                 {
187                         if (multiple)
188                         {
189                                 fprintf(out, "0x%08X\n", checksum);
190                                 file_size += 4;
191                         }
192
193                         fprintf(out, "};\n");
194                         fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
195
196                         goto ERR_DONE;
197                 }
198                 else if (size<MAX_READ_SIZE)
199                 {
200                         multiple = 0;
201
202                         write_file(out, buffer, size, NULL, 0);
203                         fprintf(out, "0x%08X\n", checksum);
204
205                         if( (size%4)!=0 )
206                             file_size += (4-(size%4));
207
208                         file_size += 4;
209                         fprintf(out, "};\n");
210                         fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
211
212                         goto ERR_DONE;
213                 }
214                 else if (size==MAX_READ_SIZE)
215                 {
216                         multiple = 1;
217                         write_file(out, buffer, MAX_READ_SIZE, NULL, 0);
218                 }
219                 else
220                         goto ERR_DONE;
221         }
222
223 ERR_DONE:
224         return;
225 }
226
227
228 int main(int argc, char* argv[])
229 {
230         FILE *in, *out;
231         int retVal;
232         int i=0;
233         char input_file_name[80];
234         char output_file_name[80];
235
236         in = out = 0x0;
237
238         if( argc < 3 )
239         {
240                 printf("\"bin2hex [input_file] [output_file] - gen array data\"!\n\r");
241                 printf("\"bin2hex [input_file] [output_file] [rom]- gen rom code\"!\n\r");
242                 goto ERR_DONE;
243         }
244         strcpy(input_file_name, argv[1]);
245         strcpy(output_file_name, argv[2]);
246
247         printf("bin2h %s %s!\n\r", input_file_name, output_file_name);
248         //goto ERR_DONE;
249
250         if((in = fopen(input_file_name,"rb")) == NULL)
251                 goto ERR_DONE;
252
253         if((out = fopen(output_file_name,"wt")) == NULL)
254                 goto ERR_DONE;
255
256         // actually we don't really see what's the third param now,
257         if( argv[3] )
258                 write_rom(out, in);      // for loading into RAM directly, e.g ROM code or patch code
259         else
260                 write_array(out, in);   // for generating firmware
261
262 ERR_DONE:
263
264         if(in) fclose(in);
265         if(out) fclose(out);
266
267         return 0;
268
269 }