Setting up repository
[linux-libre-firmware.git] / ath9k_htc / sboot / utility / imghdr / imghdr.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
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:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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
15  *    distribution.
16  *
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.
20  *
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.
34  */
35 #include <stdio.h>
36 #include <string.h>
37
38
39 #define MAX_READ_SIZE   80
40
41 static void crc16ccitt_init(unsigned short *uCcitt16)
42 {
43         *uCcitt16 = 0xFFFF;
44 }
45
46 static void crc16ccitt_update(unsigned short *uCcitt16, unsigned char *pBuffer, unsigned long uBufSize)
47 {
48         unsigned long i = 0;
49         unsigned long j = 0;
50
51         for(i = 0; i < uBufSize; i++)
52         {
53                 for(j=0; j<3; j++)
54                 {
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;
60                 }
61         }
62 }
63
64 static void crc16ccitt_final(unsigned short *uCcitt16)
65 {
66         *uCcitt16 = ~(*uCcitt16);
67 }
68
69 void write_file(unsigned short crc, unsigned short file_size, FILE *out, FILE *in)
70 {
71         unsigned char buffer[MAX_READ_SIZE];
72         unsigned short size;
73
74         //set file pointer to start of the file
75         if( feof(in) )
76                 fseek(in,0,SEEK_SET);
77
78         while(1)
79         {
80                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
81         
82                 if (size<MAX_READ_SIZE)
83                 {
84                         if( size%4==1 )
85                         {
86                                 buffer[size] = 0x0;
87                                 buffer[size+1] = 0x0;
88                                 buffer[size+2] = 0x0;
89                                 size+=3;
90                         }
91                         else if ( size%4==2 )
92                         {
93                                 buffer[size] = 0x0;
94                                 buffer[size+1] = 0x0;
95                                 size+=2;
96                         }
97                         else if ( size%4==3 )
98                         {
99                                 buffer[size] = 0x0;
100                                 size+=1;
101                         }
102                         fwrite(buffer, sizeof(unsigned char), size, out);
103                         goto ERR_DONE;
104                 }
105                 fwrite(buffer, sizeof(unsigned char), MAX_READ_SIZE, out);
106         }
107 ERR_DONE:
108
109         fwrite(&crc, sizeof(unsigned char), sizeof(short), out);
110         fwrite(&file_size, sizeof(unsigned char), sizeof(short), out);
111
112         return;
113 }
114
115
116 unsigned short cal_crc(FILE *in)
117 {
118         int size;
119         long file_size;
120         unsigned short crc = 0;
121         unsigned char buffer[MAX_READ_SIZE];
122
123         file_size = size = 0;
124
125     crc16ccitt_init(&crc);
126
127         while(1)
128         {
129                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
130                 file_size += size;
131
132                 if( size%4==1 )
133                 {
134                         buffer[size] = 0x0;
135                         buffer[size+1] = 0x0;
136                         buffer[size+2] = 0x0;
137                         size+=3;
138                 }
139                 else if ( size%4==2 )
140                 {
141                         buffer[size] = 0x0;
142                         buffer[size+1] = 0x0;
143                         size+=2;
144                 }
145                 else if ( size%4==3 )
146                 {
147                         buffer[size] = 0x0;
148                         size+=1;
149                 }
150
151                 crc16ccitt_update(&crc, (unsigned char*)buffer, size);
152
153                 if (size<MAX_READ_SIZE)
154                         goto ERR_DONE;
155         }
156
157 ERR_DONE:
158         
159     crc16ccitt_final(&crc);
160     printf(" ==> output crc 0x%04x with file size 0x%04x\n\r", crc, file_size);
161
162         return crc;
163 }
164
165
166 int main(int argc, char* argv[])
167 {
168         FILE *in, *out;
169         int retVal;
170         int i=0;
171         unsigned short crc = 0;
172         unsigned short size = 0;
173         char input_file_name[80];
174         char output_file_name[80];
175         
176         in = out = 0x0;
177
178         if( argc < 3 )
179         {
180                 printf("\"imghdr [input_file] [output_file] - calculate the image and prefix to the binary \"!\n\r");
181                 goto ERR_DONE;
182         }
183         strcpy(input_file_name, argv[1]);
184         strcpy(output_file_name, argv[2]);
185
186         printf("imghdr %s %s!\n\r", input_file_name, output_file_name);
187
188         if((in = fopen(input_file_name,"rb")) == NULL)
189                 goto ERR_DONE;
190
191         if((out = fopen(output_file_name,"wt")) == NULL)
192                 goto ERR_DONE;
193         
194         crc = cal_crc(in);
195         
196         fseek( in, 0, SEEK_END );
197         size = ftell(in);    // get file length
198         fseek( in, 0, SEEK_SET );
199         
200         printf(" ==> (2) output crc 0x%04x with file size 0x%04x\n\r", crc, size);
201         write_file(crc, size, out, in);
202
203
204 ERR_DONE:
205
206         if(in) fclose(in);
207         if(out) fclose(out);
208
209         return 0;
210
211 }