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.
39 #define MAX_READ_SIZE 80
41 static void crc16ccitt_init(unsigned short *uCcitt16)
46 static void crc16ccitt_update(unsigned short *uCcitt16, unsigned char *pBuffer, unsigned long uBufSize)
51 for(i = 0; i < uBufSize; i++)
55 *uCcitt16 = (*uCcitt16 >> 8) | (*uCcitt16 << 8);
56 *uCcitt16 ^= pBuffer[3-j];
57 *uCcitt16 ^= (*uCcitt16 & 0xFF) >> 4;
58 *uCcitt16 ^= (*uCcitt16 << 8) << 4;
59 *uCcitt16 ^= ((*uCcitt16 & 0xFF) << 4) << 1;
64 static void crc16ccitt_final(unsigned short *uCcitt16)
66 *uCcitt16 = ~(*uCcitt16);
69 void write_file(unsigned short crc, unsigned short file_size, FILE *out, FILE *in)
71 unsigned char buffer[MAX_READ_SIZE];
74 //set file pointer to start of the file
80 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
82 if (size<MAX_READ_SIZE)
102 fwrite(buffer, sizeof(unsigned char), size, out);
105 fwrite(buffer, sizeof(unsigned char), MAX_READ_SIZE, out);
109 fwrite(&crc, sizeof(unsigned char), sizeof(short), out);
110 fwrite(&file_size, sizeof(unsigned char), sizeof(short), out);
116 unsigned short cal_crc(FILE *in)
120 unsigned short crc = 0;
121 unsigned char buffer[MAX_READ_SIZE];
123 file_size = size = 0;
125 crc16ccitt_init(&crc);
129 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
135 buffer[size+1] = 0x0;
136 buffer[size+2] = 0x0;
139 else if ( size%4==2 )
142 buffer[size+1] = 0x0;
145 else if ( size%4==3 )
151 crc16ccitt_update(&crc, (unsigned char*)buffer, size);
153 if (size<MAX_READ_SIZE)
159 crc16ccitt_final(&crc);
160 printf(" ==> output crc 0x%04x with file size 0x%04x\n\r", crc, file_size);
166 int main(int argc, char* argv[])
171 unsigned short crc = 0;
172 unsigned short size = 0;
173 char input_file_name[80];
174 char output_file_name[80];
180 printf("\"imghdr [input_file] [output_file] - calculate the image and prefix to the binary \"!\n\r");
183 strcpy(input_file_name, argv[1]);
184 strcpy(output_file_name, argv[2]);
186 printf("imghdr %s %s!\n\r", input_file_name, output_file_name);
188 if((in = fopen(input_file_name,"rb")) == NULL)
191 if((out = fopen(output_file_name,"wt")) == NULL)
196 fseek( in, 0, SEEK_END );
197 size = ftell(in); // get file length
198 fseek( in, 0, SEEK_SET );
200 printf(" ==> (2) output crc 0x%04x with file size 0x%04x\n\r", crc, size);
201 write_file(crc, size, out, in);