2 * Copyright (c) 2013 Qualcomm Atheros, Inc.
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:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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.
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.
38 #define MAX_READ_SIZE 80
40 unsigned long checksum = 0;
42 void write_file(FILE *out, unsigned char *buf, unsigned long size, unsigned char *endian, unsigned char nl)
45 unsigned char tmp_buf[4];
47 for(i=0; i<size; i+=4)
57 tmp_buf[1] = buf[i+1];
58 tmp_buf[2] = buf[i+2];
59 tmp_buf[3] = buf[i+3];
65 tmp_buf[3] = 0x0; // padding
66 printf("3: i:%d size:%d\n\r", i, size);
70 tmp_buf[2] = 0x0; // padding
71 tmp_buf[3] = 0x0; // padding
72 printf("2: i:%d size:%d\n\r", i, size);
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);
83 fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
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];
101 tmp_buf[0] = 0x0; // padding
105 tmp_buf[0] = 0x0; // padding
106 tmp_buf[1] = 0x0; // padding
110 tmp_buf[0] = 0x0; // padding
111 tmp_buf[1] = 0x0; // padding
112 tmp_buf[2] = 0x0; // padding
120 fprintf(out, "0x%08X, ", *((unsigned long *)(&tmp_buf[0])));
122 checksum = checksum ^ *((unsigned long *)(&tmp_buf[0]));
126 void write_rom(FILE *out, FILE *in)
130 unsigned char buffer[MAX_READ_SIZE];
133 file_size = size = 0;
137 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
140 //write_file(out, buffer, size, NULL, 0);
144 fprintf(out, "%08X\n", checksum);
148 else if (size<MAX_READ_SIZE)
151 write_file(out, buffer, size, NULL, 0);
152 fprintf(out, "%08X\n", checksum);
155 else if (size==MAX_READ_SIZE)
158 write_file(out, buffer, MAX_READ_SIZE, NULL, 0);
170 void write_array(FILE *out, FILE *in)
174 unsigned char buffer[MAX_READ_SIZE];
177 file_size = size = 0;
179 // fprintf(out, "#include \"80211core_sh.h\"\n");
180 fprintf(out, "const unsigned long zcFwImage[] = {\n");
183 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
189 fprintf(out, "0x%08X\n", checksum);
193 fprintf(out, "};\n");
194 fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
198 else if (size<MAX_READ_SIZE)
202 write_file(out, buffer, size, NULL, 0);
203 fprintf(out, "0x%08X\n", checksum);
206 file_size += (4-(size%4));
209 fprintf(out, "};\n");
210 fprintf(out, "\nconst unsigned long zcFwImageSize=%ld;\n", file_size);
214 else if (size==MAX_READ_SIZE)
217 write_file(out, buffer, MAX_READ_SIZE, NULL, 0);
228 int main(int argc, char* argv[])
233 char input_file_name[80];
234 char output_file_name[80];
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");
244 strcpy(input_file_name, argv[1]);
245 strcpy(output_file_name, argv[2]);
247 printf("bin2h %s %s!\n\r", input_file_name, output_file_name);
250 if((in = fopen(input_file_name,"rb")) == NULL)
253 if((out = fopen(output_file_name,"wt")) == NULL)
256 // actually we don't really see what's the third param now,
258 write_rom(out, in); // for loading into RAM directly, e.g ROM code or patch code
260 write_array(out, in); // for generating firmware