Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / build / utility / imghdr / imghdr.c
1 #include <stdio.h>
2 #include <string.h>
3
4
5 #define MAX_READ_SIZE   80
6
7 static void crc16ccitt_init(unsigned short *uCcitt16)
8 {
9         *uCcitt16 = 0xFFFF;
10 }
11
12 static void crc16ccitt_update(unsigned short *uCcitt16, unsigned char *pBuffer, unsigned long uBufSize)
13 {
14         unsigned long i = 0;
15         unsigned long j = 0;
16
17         for(i = 0; i < uBufSize; i++)
18         {
19                 for(j=0; j<3; j++)
20                 {
21                         *uCcitt16 = (*uCcitt16 >> 8) | (*uCcitt16 << 8);
22                         *uCcitt16 ^= pBuffer[3-j];
23                         *uCcitt16 ^= (*uCcitt16 & 0xFF) >> 4;
24                         *uCcitt16 ^= (*uCcitt16 << 8) << 4;
25                         *uCcitt16 ^= ((*uCcitt16 & 0xFF) << 4) << 1;
26                 }
27         }
28 }
29
30 static void crc16ccitt_final(unsigned short *uCcitt16)
31 {
32         *uCcitt16 = ~(*uCcitt16);
33 }
34
35 void write_file(unsigned short crc, unsigned short file_size, FILE *out, FILE *in)
36 {
37         unsigned char buffer[MAX_READ_SIZE];
38         unsigned short size;
39
40         //set file pointer to start of the file
41         if( feof(in) )
42                 fseek(in,0,SEEK_SET);
43
44         while(1)
45         {
46                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
47         
48                 if (size<MAX_READ_SIZE)
49                 {
50                         if( size%4==1 )
51                         {
52                                 buffer[size] = 0x0;
53                                 buffer[size+1] = 0x0;
54                                 buffer[size+2] = 0x0;
55                                 size+=3;
56                         }
57                         else if ( size%4==2 )
58                         {
59                                 buffer[size] = 0x0;
60                                 buffer[size+1] = 0x0;
61                                 size+=2;
62                         }
63                         else if ( size%4==3 )
64                         {
65                                 buffer[size] = 0x0;
66                                 size+=1;
67                         }
68                         fwrite(buffer, sizeof(unsigned char), size, out);
69                         goto ERR_DONE;
70                 }
71                 fwrite(buffer, sizeof(unsigned char), MAX_READ_SIZE, out);
72         }
73 ERR_DONE:
74
75         fwrite(&crc, sizeof(unsigned char), sizeof(short), out);
76         fwrite(&file_size, sizeof(unsigned char), sizeof(short), out);
77
78         return;
79 }
80
81
82 unsigned short cal_crc(FILE *in)
83 {
84         int size;
85         long file_size;
86         unsigned short crc = 0;
87         unsigned char buffer[MAX_READ_SIZE];
88
89         file_size = size = 0;
90
91     crc16ccitt_init(&crc);
92
93         while(1)
94         {
95                 size = fread(buffer, sizeof(unsigned char), sizeof(buffer), in);
96                 file_size += size;
97
98                 if( size%4==1 )
99                 {
100                         buffer[size] = 0x0;
101                         buffer[size+1] = 0x0;
102                         buffer[size+2] = 0x0;
103                         size+=3;
104                 }
105                 else if ( size%4==2 )
106                 {
107                         buffer[size] = 0x0;
108                         buffer[size+1] = 0x0;
109                         size+=2;
110                 }
111                 else if ( size%4==3 )
112                 {
113                         buffer[size] = 0x0;
114                         size+=1;
115                 }
116
117                 crc16ccitt_update(&crc, (unsigned char*)buffer, size);
118
119                 if (size<MAX_READ_SIZE)
120                         goto ERR_DONE;
121         }
122
123 ERR_DONE:
124         
125     crc16ccitt_final(&crc);
126     printf(" ==> output crc 0x%04x with file size 0x%04x\n\r", crc, file_size);
127
128         return crc;
129 }
130
131
132 int main(int argc, char* argv[])
133 {
134         FILE *in, *out;
135         int retVal;
136         int i=0;
137         unsigned short crc = 0;
138         unsigned short size = 0;
139         char input_file_name[80];
140         char output_file_name[80];
141         
142         in = out = 0x0;
143
144         if( argc < 3 )
145         {
146                 printf("\"imghdr [input_file] [output_file] - calculate the image and prefix to the binary \"!\n\r");
147                 goto ERR_DONE;
148         }
149         strcpy(input_file_name, argv[1]);
150         strcpy(output_file_name, argv[2]);
151
152         printf("imghdr %s %s!\n\r", input_file_name, output_file_name);
153
154         if((in = fopen(input_file_name,"rb")) == NULL)
155                 goto ERR_DONE;
156
157         if((out = fopen(output_file_name,"wt")) == NULL)
158                 goto ERR_DONE;
159         
160         crc = cal_crc(in);
161         
162         fseek( in, 0, SEEK_END );
163         size = ftell(in);    // get file length
164         fseek( in, 0, SEEK_SET );
165         
166         printf(" ==> (2) output crc 0x%04x with file size 0x%04x\n\r", crc, size);
167         write_file(crc, size, out, in);
168
169
170 ERR_DONE:
171
172         if(in) fclose(in);
173         if(out) fclose(out);
174
175         return 0;
176
177 }