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