6fd25410914b58ad4168a2819176484af4d69961
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / utility / bin2hex / bin2hex.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
16                 if( nl==1 )
17                 {
18                         if(i%16 == 0) {
19                                 fprintf(out, "\n");
20                         }
21                     tmp_buf[0] = buf[i];
22                     tmp_buf[1] = buf[i+1];
23                     tmp_buf[2] = buf[i+2];
24                     tmp_buf[3] = buf[i+3];
25
26                         fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
27
28         } else {
29             
30             if(i%16 == 0) {
31                 fprintf(out, "\n");
32                         }
33                         tmp_buf[0] = buf[i+3];
34                         tmp_buf[1] = buf[i+2];
35                         tmp_buf[2] = buf[i+1];
36                         tmp_buf[3] = buf[i+0];
37                         fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
38                 }
39         checksum = checksum ^ *((unsigned long *)(&tmp_buf[0]));
40         }
41 }
42
43 void write_rom(FILE *out, FILE *in)
44 {
45         int size;
46         long file_size;
47         unsigned char buffer[MAX_READ_SIZE];
48         int multiple = 0;
49
50         file_size = size = 0;
51
52         while(1)
53         {
54                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
55                 file_size += size;
56
57                 //write_file(out, buffer, size, NULL, 0);
58                 if( size == 0 )
59                 {
60                         if (multiple)
61                                 fprintf(out, "%08X\n", checksum);
62
63                         goto ERR_DONE;
64                 }
65                 else if (size<MAX_READ_SIZE)
66                 {
67                     multiple = 0;
68                         write_file(out, buffer, size, NULL, 1);
69                         fprintf(out, "%08X\n", checksum);
70                         goto ERR_DONE;
71                 }
72                 else if (size==MAX_READ_SIZE)
73                 {
74                         multiple = 1;
75                         write_file(out, buffer, MAX_READ_SIZE, NULL, 1);
76             }
77             else
78                 goto ERR_DONE;
79         }
80
81 ERR_DONE:
82
83         return;
84 }
85
86
87 void write_array(FILE *out, FILE *in, unsigned char hif)
88 {
89         int size;
90         long file_size;
91         unsigned char buffer[MAX_READ_SIZE];
92         int multiple = 0;
93
94         file_size = size = 0;
95
96 //      fprintf(out, "#include \"80211core_sh.h\"\n");
97         fprintf(out, "#include <stdint.h>\n");
98         fprintf(out, "const uint32_t zcFwImage[] = {\n");
99         while(1)
100         {
101                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
102                 file_size += size;
103                 if( size == 0 )
104                 {
105                         if (multiple)
106                         {
107                                 fprintf(out, "0x%08X\n", checksum);
108                                 file_size += 4;
109                         }
110
111                         fprintf(out, "};\n");
112                         fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
113
114                         goto ERR_DONE;
115                 }
116                 else if (size<MAX_READ_SIZE)
117                 {
118                         multiple = 0;
119
120                         write_file(out, buffer, size, NULL, hif);
121                         fprintf(out, "0x%08X\n", checksum);
122
123                         if( (size%4)!=0 )
124                             file_size += (4-(size%4));
125
126                         file_size += 4;
127                         fprintf(out, "};\n");
128                         fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
129
130                         goto ERR_DONE;
131                 }
132                 else if (size==MAX_READ_SIZE)
133                 {
134                         multiple = 1;
135                         write_file(out, buffer, MAX_READ_SIZE, NULL, hif);
136                 }
137                 else
138                         goto ERR_DONE;
139         }
140
141 ERR_DONE:
142         return;
143 }
144
145
146 int main(int argc, char* argv[])
147 {
148         FILE *in, *out;
149         int retVal;
150         int i=0;
151         char input_file_name[80];
152         char output_file_name[80];
153
154         in = out = 0x0;
155
156         if( argc < 3 )
157         {
158                 printf("\"bin2hex [input_file] [output_file] - gen array data\"!\n\r");
159                 printf("\"bin2hex [input_file] [output_file] [rom]- gen rom code\"!\n\r");
160                 goto ERR_DONE;
161         }
162         strcpy(input_file_name, argv[1]);
163         strcpy(output_file_name, argv[2]);
164
165         printf("bin2h %s %s!\n\r", input_file_name, output_file_name);
166
167         if((in = fopen(input_file_name,"rb")) == NULL)
168                 goto ERR_DONE;
169
170         if((out = fopen(output_file_name,"wt")) == NULL)
171                 goto ERR_DONE;
172
173         if( !strcmp(argv[3],"rom"))
174         /* for loading into RAM directly, e.g ROM code or patch code */
175         write_rom(out, in);     
176     else { 
177         if(!strcmp(argv[4],"usb")) 
178             write_array(out, in, 1);    /* for generating firmware (usb) */
179         else if (!strcmp(argv[4],"pci")) 
180             write_array(out, in, 0);    /* for generating firmware (pci) */
181         else
182             write_array(out, in, 1);    /* Default case firmware (usb) */
183     }
184
185 ERR_DONE:
186
187         if(in) fclose(in);
188         if(out) fclose(out);
189
190         return 0;
191
192 }