Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / 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 #include <stdint.h>
38
39 #define MAX_READ_SIZE   80
40
41 uint32_t checksum = 0;
42
43 void write_file(FILE *out, unsigned char *buf, uint32_t size, unsigned char *endian, unsigned char nl)
44 {
45         int i=0;
46         unsigned char tmp_buf[4];
47
48         for(i=0; i<size; i+=4)
49         {
50
51                 if( nl==1 )
52                 {
53                         if(i%16 == 0) {
54                                 fprintf(out, "\n");
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
61                         fprintf(out, "0x%08X, ", *((uint32_t *)(&tmp_buf[0])));
62
63         } else {
64             
65             if(i%16 == 0) {
66                 fprintf(out, "\n");
67                         }
68                         tmp_buf[0] = buf[i+3];
69                         tmp_buf[1] = buf[i+2];
70                         tmp_buf[2] = buf[i+1];
71                         tmp_buf[3] = buf[i+0];
72                         fprintf(out, "0x%08X, ", *((uint32_t *)(&tmp_buf[0])));
73                 }
74         checksum = checksum ^ *((uint32_t *)(&tmp_buf[0]));
75         }
76 }
77
78 void write_rom(FILE *out, FILE *in)
79 {
80         int size;
81         long file_size;
82         unsigned char buffer[MAX_READ_SIZE];
83         int multiple = 0;
84
85         file_size = size = 0;
86
87         while(1)
88         {
89                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
90                 file_size += size;
91
92                 //write_file(out, buffer, size, NULL, 0);
93                 if( size == 0 )
94                 {
95                         if (multiple)
96                                 fprintf(out, "%08X\n", checksum);
97
98                         goto ERR_DONE;
99                 }
100                 else if (size<MAX_READ_SIZE)
101                 {
102                     multiple = 0;
103                         write_file(out, buffer, size, NULL, 1);
104                         fprintf(out, "%08X\n", checksum);
105                         goto ERR_DONE;
106                 }
107                 else if (size==MAX_READ_SIZE)
108                 {
109                         multiple = 1;
110                         write_file(out, buffer, MAX_READ_SIZE, NULL, 1);
111             }
112             else
113                 goto ERR_DONE;
114         }
115
116 ERR_DONE:
117
118         return;
119 }
120
121
122 void write_array(FILE *out, FILE *in, unsigned char hif)
123 {
124         int size;
125         long file_size;
126         unsigned char buffer[MAX_READ_SIZE];
127         int multiple = 0;
128
129         file_size = size = 0;
130
131 //      fprintf(out, "#include \"80211core_sh.h\"\n");
132         fprintf(out, "#include <stdint.h>\n");
133         fprintf(out, "const uint32_t zcFwImage[] = {\n");
134         while(1)
135         {
136                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
137                 file_size += size;
138                 if( size == 0 )
139                 {
140                         if (multiple)
141                         {
142                                 fprintf(out, "0x%08X\n", checksum);
143                                 file_size += 4;
144                         }
145
146                         fprintf(out, "};\n");
147                         fprintf(out, "\nconst uint32_t zcFwImageSize=%ld;\n", file_size);
148
149                         goto ERR_DONE;
150                 }
151                 else if (size<MAX_READ_SIZE)
152                 {
153                         multiple = 0;
154
155                         write_file(out, buffer, size, NULL, hif);
156                         fprintf(out, "0x%08X\n", checksum);
157
158                         if( (size%4)!=0 )
159                             file_size += (4-(size%4));
160
161                         file_size += 4;
162                         fprintf(out, "};\n");
163                         fprintf(out, "\nconst uint32_t zcFwImageSize=%ld;\n", file_size);
164
165                         goto ERR_DONE;
166                 }
167                 else if (size==MAX_READ_SIZE)
168                 {
169                         multiple = 1;
170                         write_file(out, buffer, MAX_READ_SIZE, NULL, hif);
171                 }
172                 else
173                         goto ERR_DONE;
174         }
175
176 ERR_DONE:
177         return;
178 }
179
180
181 int main(int argc, char* argv[])
182 {
183         FILE *in, *out;
184         int retVal;
185         int i=0;
186         char input_file_name[80];
187         char output_file_name[80];
188
189         in = out = 0x0;
190
191         if( argc < 3 )
192         {
193                 printf("\"bin2hex [input_file] [output_file] - gen array data\"!\n\r");
194                 printf("\"bin2hex [input_file] [output_file] [rom]- gen rom code\"!\n\r");
195                 goto ERR_DONE;
196         }
197         strcpy(input_file_name, argv[1]);
198         strcpy(output_file_name, argv[2]);
199
200         printf("bin2h %s %s!\n\r", input_file_name, output_file_name);
201
202         if((in = fopen(input_file_name,"rb")) == NULL)
203                 goto ERR_DONE;
204
205         if((out = fopen(output_file_name,"wt")) == NULL)
206                 goto ERR_DONE;
207
208         if( !strcmp(argv[3],"rom"))
209         /* for loading into RAM directly, e.g ROM code or patch code */
210         write_rom(out, in);     
211     else { 
212         if(!strcmp(argv[4],"usb")) 
213             write_array(out, in, 1);    /* for generating firmware (usb) */
214         else if (!strcmp(argv[4],"pci")) 
215             write_array(out, in, 0);    /* for generating firmware (pci) */
216         else
217             write_array(out, in, 1);    /* Default case firmware (usb) */
218     }
219
220 ERR_DONE:
221
222         if(in) fclose(in);
223         if(out) fclose(out);
224
225         return 0;
226
227 }