Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / utility / bin2hex / bin2hex_swp.c
1 #include <stdio.h>
2 #include <string.h>
3
4 #define MAX_READ_SIZE   80
5
6 unsigned long checksum = 0;
7
8 void write_file(FILE *out, unsigned char *buf, unsigned long size, unsigned char *endian, unsigned char nl)
9 {
10         int i=0;
11         unsigned char tmp_buf[4];
12
13         for(i=0; i<size; i+=4)
14         {
15                 if( nl==1 )
16                 {
17                         if(i%16 == 0){
18                                 fprintf(out, "\n");
19                         }
20
21
22                     tmp_buf[0] = buf[i];
23                     tmp_buf[1] = buf[i+1];
24                     tmp_buf[2] = buf[i+2];
25                     tmp_buf[3] = buf[i+3];
26 #if 0
27             if( i+4>=size)
28             {
29                 if(i%4==3)
30                 {
31                                 tmp_buf[3] = 0x0;   // padding
32                                 printf("3: i:%d size:%d\n\r", i, size);
33                 }
34                 else if (i%4==2)
35                 {
36                                 tmp_buf[2] = 0x0;   // padding
37                                 tmp_buf[3] = 0x0;   // padding
38                                 printf("2: i:%d size:%d\n\r", i, size);
39                 }
40                 else if (i%4==1)
41                 {
42                                 tmp_buf[1] = 0x0;   // padding
43                                 tmp_buf[2] = 0x0;   // padding
44                                 tmp_buf[3] = 0x0;   // padding
45                                 printf("1: i:%d size:%d\n\r", i, size);
46                 }
47             }
48 #endif
49                         fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
50                 }
51                 else
52                 {
53
54                         if(i%16 == 0){
55                                 fprintf(out, "\n");
56                         }
57
58                         tmp_buf[0] = buf[i+3];
59                         tmp_buf[1] = buf[i+2];
60                         tmp_buf[2] = buf[i+1];
61                         tmp_buf[3] = buf[i+0];
62 #if 0
63             if( i+4>=size)
64             {
65                 if(i%4==3)
66                 {
67                                 tmp_buf[0] = 0x0;   // padding
68                 }
69                 else if (i%4==2)
70                 {
71                                 tmp_buf[0] = 0x0;   // padding
72                                 tmp_buf[1] = 0x0;   // padding
73                 }
74                 else if (i%4==1)
75                 {
76                     tmp_buf[0] = 0x0;   // padding
77                                 tmp_buf[1] = 0x0;   // padding
78                                 tmp_buf[2] = 0x0;   // padding
79                 }
80             }
81             else
82             {
83
84             }
85 #endif
86                         fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
87                 }
88         checksum = checksum ^ *((unsigned long *)(&tmp_buf[0]));
89         }
90 }
91
92 void write_rom(FILE *out, FILE *in)
93 {
94         int size;
95         long file_size;
96         unsigned char buffer[MAX_READ_SIZE];
97         int multiple = 0;
98
99         file_size = size = 0;
100
101         while(1)
102         {
103                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
104                 file_size += size;
105
106                 //write_file(out, buffer, size, NULL, 0);
107                 if( size == 0 )
108                 {
109                         if (multiple)
110                                 fprintf(out, "%08X\n", checksum);
111
112                         goto ERR_DONE;
113                 }
114                 else if (size<MAX_READ_SIZE)
115                 {
116                     multiple = 0;
117                         write_file(out, buffer, size, NULL, 0);
118                         fprintf(out, "%08X\n", checksum);
119                         goto ERR_DONE;
120                 }
121                 else if (size==MAX_READ_SIZE)
122                 {
123                         multiple = 1;
124                         write_file(out, buffer, MAX_READ_SIZE, NULL, 0);
125             }
126             else
127                 goto ERR_DONE;
128         }
129
130 ERR_DONE:
131
132         return;
133 }
134
135
136 void write_array(FILE *out, FILE *in)
137 {
138         int size;
139         long file_size;
140         unsigned char buffer[MAX_READ_SIZE];
141         int multiple = 0;
142
143         file_size = size = 0;
144
145 //      fprintf(out, "#include \"80211core_sh.h\"\n");
146         fprintf(out, "const unsigned long zcFwImage[] = {\n");
147         while(1)
148         {
149                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
150                 file_size += size;
151                 if( size == 0 )
152                 {
153                         if (multiple)
154                         {
155                                 fprintf(out, "0x%08X\n", checksum);
156                                 file_size += 4;
157                         }
158
159                         fprintf(out, "};\n");
160                         fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
161
162                         goto ERR_DONE;
163                 }
164                 else if (size<MAX_READ_SIZE)
165                 {
166                         multiple = 0;
167
168                         write_file(out, buffer, size, NULL, 0);
169                         fprintf(out, "0x%08X\n", checksum);
170
171                         if( (size%4)!=0 )
172                             file_size += (4-(size%4));
173
174                         file_size += 4;
175                         fprintf(out, "};\n");
176                         fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
177
178                         goto ERR_DONE;
179                 }
180                 else if (size==MAX_READ_SIZE)
181                 {
182                         multiple = 1;
183                         write_file(out, buffer, MAX_READ_SIZE, NULL, 0);
184                 }
185                 else
186                         goto ERR_DONE;
187         }
188
189 ERR_DONE:
190         return;
191 }
192
193
194 int main(int argc, char* argv[])
195 {
196         FILE *in, *out;
197         int retVal;
198         int i=0;
199         char input_file_name[80];
200         char output_file_name[80];
201
202         in = out = 0x0;
203
204         if( argc < 3 )
205         {
206                 printf("\"bin2hex [input_file] [output_file] - gen array data\"!\n\r");
207                 printf("\"bin2hex [input_file] [output_file] [rom]- gen rom code\"!\n\r");
208                 goto ERR_DONE;
209         }
210         strcpy(input_file_name, argv[1]);
211         strcpy(output_file_name, argv[2]);
212
213         printf("bin2h %s %s!\n\r", input_file_name, output_file_name);
214         //goto ERR_DONE;
215
216         if((in = fopen(input_file_name,"rb")) == NULL)
217                 goto ERR_DONE;
218
219         if((out = fopen(output_file_name,"wt")) == NULL)
220                 goto ERR_DONE;
221
222         // actually we don't really see what's the third param now,
223         if( argv[3] )
224                 write_rom(out, in);      // for loading into RAM directly, e.g ROM code or patch code
225         else
226                 write_array(out, in);   // for generating firmware
227
228 ERR_DONE:
229
230         if(in) fclose(in);
231         if(out) fclose(out);
232
233         return 0;
234
235 }