This source file includes following definitions.
- crypt_fwrite
- crypt_fread
- pak_create
- checksumm_init
- checksumm_update
- pak_add_file
- pak_add_4byteid
- pak_close
- usage
- main
1 #include <stdio.h>
2 #include <stddef.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <stdlib.h>
6
7 typedef struct {
8 char type[32];
9 char description[32];
10 char name[32];
11 uint32_t length;
12 uint32_t checksumm;
13 } __attribute__((packed)) header_t;
14
15 typedef unsigned short uint16;
16
17 unsigned char crypt1[512] = {
18 0x06, 0x5f, 0xd3, 0x1c, 0xb6, 0x2f, 0x10, 0x77,
19 0xce, 0x3f, 0x5d, 0x7b, 0xa1, 0x92, 0x2f, 0x8a,
20 0xae, 0xff, 0x56, 0x10, 0x40, 0xc4, 0xd6, 0x5d,
21 0x7b, 0x64, 0xc0, 0xaa, 0x41, 0xa6, 0x69, 0x96,
22 0x1f, 0xad, 0xac, 0x42, 0x5d, 0x24, 0x4a, 0xad,
23 0xdf, 0x3d, 0x05, 0x81, 0x67, 0x18, 0xed, 0x7a,
24 0x9c, 0x43, 0xe6, 0x4b, 0xe3, 0x1e, 0xc9, 0x50,
25 0xa0, 0x32, 0xd3, 0x2d, 0x90, 0xc8, 0xc3, 0xa9,
26 0xb2, 0x54, 0x70, 0x17, 0xee, 0x17, 0x1b, 0xbd,
27 0x66, 0x56, 0x74, 0x34, 0xef, 0x88, 0x3b, 0x95,
28 0xd6, 0xb0, 0xc3, 0x87, 0xe9, 0x4f, 0x0d, 0xe8,
29 0x43, 0xca, 0x26, 0x14, 0xbe, 0xe3, 0xaf, 0x68,
30 0xd4, 0x2f, 0xf2, 0x1e, 0x1b, 0x45, 0xda, 0x3a,
31 0x1b, 0xd2, 0x9d, 0x9c, 0x02, 0xb0, 0x43, 0x9f,
32 0xd3, 0x44, 0x0e, 0xa4, 0x27, 0x4b, 0xf0, 0x11,
33 0x4e, 0x14, 0xc7, 0x8f, 0xac, 0x71, 0xd4, 0x1a,
34 0xbe, 0xc4, 0x72, 0x60, 0x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, 0xb1, 0xaa, 0x72,
57 0x52, 0xeb, 0x5a, 0x83, 0x95, 0x44, 0xee, 0x3b,
58 0xe6, 0x7a, 0xfa, 0xa1, 0xa0, 0x48, 0xcc, 0x66,
59 0xed, 0xc1, 0x4f, 0xa3, 0x3f, 0xa4, 0xf7, 0xda,
60 0x34, 0x92, 0x27, 0xc0, 0xe1, 0x73, 0x6e, 0x92,
61 0x7b, 0x0c, 0x57, 0xec, 0x65, 0xb0, 0xc0, 0x75,
62 0x22, 0x55, 0x03, 0x10, 0x56, 0xf0, 0xb9, 0x21,
63 0x49, 0xf1, 0xeb, 0x50, 0xa0, 0x96, 0xbd, 0x17,
64 0x25, 0x22, 0xea, 0x30, 0x42, 0x34, 0xc8, 0x7f,
65 0x19, 0x90, 0xc7, 0x70, 0x48, 0x8a, 0x17, 0x8b,
66 0x4c, 0x24, 0x12, 0x53, 0x6a, 0xf4, 0xf6, 0x7d,
67 0xb4, 0x21, 0xc1, 0xfa, 0xef, 0x87, 0xca, 0x0a,
68 0x07, 0x7f, 0xbd, 0x52, 0x18, 0x21, 0x0f, 0x40,
69 0x7b, 0xb1, 0x1a, 0xc2, 0xc2, 0xfb, 0xef, 0x18,
70 0x7c, 0x64, 0x74, 0x6c, 0x94, 0xbe, 0xc5, 0x78,
71 0x63, 0x97, 0x4b, 0xa5, 0xe5, 0x4a, 0x70, 0x4a,
72 0x8c, 0x2a, 0x86, 0xfd, 0x17, 0xd8, 0x20, 0xac,
73 0xfc, 0x50, 0xd9, 0x81, 0x84, 0x78, 0x3f, 0x7e,
74 0xeb, 0xed, 0xa6, 0x34, 0xc1, 0x9d, 0x75, 0x87,
75 0xf6, 0x8e, 0x49, 0x42, 0xef, 0x62, 0x68, 0xfd,
76 0x13, 0xf2, 0xb6, 0xbe, 0x90, 0x60, 0x79, 0x60,
77 0xc8, 0xbb, 0x83, 0xcd, 0xa6, 0x5e, 0x44, 0x02,
78 0xb7, 0x3e, 0xcf, 0x82, 0xd2, 0x0e, 0x51, 0x44,
79 0x9b, 0x5c, 0xc3, 0x33, 0x76, 0xe1, 0xc0, 0x5e,
80 0x25, 0xc0, 0x16, 0xbe, 0x7f, 0xeb, 0x1a, 0x31,
81 0x69, 0x15, 0x76, 0x61, 0xaa, 0xe9, 0x30, 0xdc,
82 };
83
84 unsigned char crypt2[513] = {
85 0x11, 0xf5, 0x6e, 0x60, 0xde, 0x9c, 0x6a, 0xfc,
86 0x77, 0xc1, 0xd1, 0xf7, 0xf9, 0x5e, 0x28, 0x9b,
87 0xf5, 0xc6, 0x6b, 0x1d, 0x6a, 0x4d, 0x74, 0x7c,
88 0x29, 0x55, 0x05, 0x86, 0xa7, 0xcf, 0x80, 0x6e,
89 0x52, 0x20, 0x8a, 0xe5, 0x37, 0xd8, 0x75, 0xa2,
90 0x52, 0x54, 0xde, 0x72, 0x80, 0xf4, 0x37, 0x89,
91 0xcf, 0x02, 0x18, 0x27, 0xd5, 0x6b, 0x1b, 0xfc,
92 0x86, 0x44, 0x02, 0x7a, 0x93, 0x67, 0x8b, 0xaf,
93 0x8e, 0xb8, 0x06, 0xf3, 0x89, 0x71, 0x59, 0xcf,
94 0x24, 0x6c, 0x35, 0x9b, 0xfe, 0x61, 0x38, 0xe8,
95 0x6d, 0x55, 0xa8, 0xe9, 0xf9, 0xec, 0x97, 0x6a,
96 0x14, 0xd6, 0xf4, 0x34, 0x19, 0x7b, 0xa4, 0x59,
97 0x94, 0xbd, 0x67, 0xb2, 0xc2, 0x72, 0x79, 0x10,
98 0x85, 0x8c, 0xc5, 0xeb, 0x2b, 0x7f, 0x82, 0x86,
99 0xc0, 0x0a, 0xd0, 0x18, 0x29, 0x6c, 0xfa, 0x9a,
100 0x33, 0x16, 0xa0, 0x09, 0x20, 0xbc, 0x8e, 0xef,
101 0x3d, 0x0a, 0x8b, 0x24, 0x25, 0xaf, 0x82, 0x26,
102 0x3a, 0xed, 0x05, 0xb7, 0x7a, 0xd3, 0xae, 0x7a,
103 0x74, 0xb0, 0xef, 0xea, 0x36, 0x7a, 0xb8, 0x0c,
104 0x97, 0x5a, 0x5c, 0x23, 0x60, 0x06, 0x9f, 0x8b,
105 0x18, 0x1d, 0x15, 0xa5, 0x52, 0x2e, 0x7e, 0x7a,
106 0x13, 0x39, 0xdc, 0x31, 0x9d, 0x62, 0xbe, 0xfd,
107 0xb3, 0x29, 0x34, 0xff, 0xcb, 0x07, 0xe4, 0xcb,
108 0xa1, 0x77, 0xb2, 0x54, 0xd3, 0xa5, 0xc8, 0x4a,
109 0xe5, 0x38, 0xee, 0x33, 0x61, 0x4b, 0x38, 0x5a,
110 0xfc, 0x6b, 0xc6, 0x25, 0x99, 0x3c, 0xcd, 0x4b,
111 0x10, 0xbd, 0x26, 0x1c, 0xb1, 0x0a, 0x4e, 0x0d,
112 0xc9, 0x97, 0x55, 0x70, 0x51, 0x48, 0x25, 0xb0,
113 0x42, 0x24, 0x58, 0x05, 0x59, 0x9e, 0x59, 0xd4,
114 0x4d, 0xde, 0x35, 0x1d, 0xcb, 0x78, 0xb3, 0x9c,
115 0xa9, 0x15, 0xee, 0xbd, 0x72, 0xca, 0x6c, 0x5b,
116 0xa5, 0x11, 0x3c, 0xb4, 0xf1, 0x8a, 0x0d, 0x52,
117 0xd7, 0x49, 0x77, 0x91, 0xf2, 0x38, 0x93, 0xc3,
118 0x76, 0xd7, 0xb6, 0xdc, 0x93, 0x2e, 0x15, 0xb5,
119 0xfd, 0x9f, 0xdb, 0xcd, 0x61, 0x05, 0x0f, 0x72,
120 0x65, 0xb4, 0x6f, 0xee, 0x44, 0xb5, 0xbb, 0x01,
121 0x10, 0x7c, 0xde, 0xcd, 0xb3, 0xce, 0xfb, 0x33,
122 0x08, 0x93, 0x2c, 0x84, 0x11, 0x61, 0xa4, 0x53,
123 0x00, 0xb8, 0x36, 0xdb, 0x52, 0x29, 0x6f, 0x67,
124 0xaf, 0xf1, 0x42, 0x9f, 0xac, 0x46, 0xec, 0x36,
125 0xff, 0xbc, 0x17, 0xd9, 0xc6, 0xf4, 0x67, 0xde,
126 0x93, 0xcc, 0x0a, 0xa5, 0xb6, 0x4c, 0x26, 0x28,
127 0x4f, 0x68, 0x82, 0xa1, 0x2b, 0x29, 0x2c, 0x97,
128 0xf9, 0xd4, 0xc1, 0x07, 0xc3, 0xfe, 0x1c, 0x22,
129 0xf5, 0xf5, 0x2a, 0xd8, 0xbd, 0x4b, 0xea, 0x5b,
130 0x97, 0xbc, 0x3e, 0x24, 0xc1, 0xa0, 0x25, 0xa9,
131 0x44, 0x3b, 0x6f, 0x68, 0xef, 0x1d, 0x05, 0x56,
132 0xda, 0x53, 0xb0, 0xb7, 0xff, 0x4c, 0xca, 0x55,
133 0x81, 0xf7, 0xa7, 0x25, 0x4e, 0x8c, 0x12, 0x89,
134 0x10, 0x44, 0x91, 0xc7, 0xbb, 0x3c, 0xcf, 0x24,
135 0x40, 0xc1, 0x66, 0x8e, 0xbb, 0xf4, 0x11, 0x0b,
136 0xf7, 0x10, 0x3f, 0x9d, 0x81, 0x48, 0xe8, 0x1b,
137 0xc8, 0xf3, 0x08, 0xbc, 0xbb, 0x56, 0x01, 0x86,
138 0x05, 0x75, 0x76, 0x25, 0xba, 0xd2, 0x41, 0x5f,
139 0xed, 0x6a, 0xbb, 0xf7, 0xb5, 0x32, 0x90, 0x36,
140 0x0f, 0x88, 0x8d, 0x86, 0x1a, 0x8e, 0xdb, 0xee,
141 0x2b, 0xbc, 0xd5, 0x58, 0x6b, 0xaa, 0x70, 0xa0,
142 0x05, 0xe4, 0x41, 0xca, 0x0c, 0x1d, 0x08, 0x57,
143 0x19, 0x73, 0x5f, 0x4b, 0xec, 0xd6, 0xfd, 0xc0,
144 0xc2, 0x62, 0x17, 0xab, 0x84, 0x4d, 0x98, 0x7f,
145 0x19, 0xec, 0x49, 0x9e, 0xd4, 0x71, 0xf2, 0xc5,
146 0x3e, 0x65, 0x9e, 0x67, 0x7e, 0xc8, 0xa1, 0xc2,
147 0xed, 0xb6, 0xdd, 0x6a, 0x64, 0x00, 0x14, 0xbb,
148 0xfe, 0x6d, 0x11, 0xd5, 0x60, 0xd8, 0x71, 0xff,
149 0x36};
150
151 #define CRYPT1_SIZE 512
152 #define CRYPT2_SIZE 513
153
154 #define HEADER_SIZE (sizeof(header_t)/sizeof(char))
155
156 int crypt_fwrite(void *buf, int len, FILE *f)
157 {
158 unsigned char cbuf[1024];
159 int r, rtot, start, towr, i, c1, c2;
160
161 start = ftell(f);
162 rtot = 0;
163
164 c1 = start % CRYPT1_SIZE;
165 c2 = start % CRYPT2_SIZE;
166
167 while (len>0){
168 if (len>1024)
169 towr = 1024;
170 else
171 towr = len;
172
173 for (i=0;i<towr;i++){
174 cbuf[i] = ((unsigned char*)buf)[i+rtot] ^ crypt1[c1++] ^ crypt2[c2++];
175
176 if (c1 >= CRYPT1_SIZE)
177 c1 = 0;
178 if (c2 >= CRYPT2_SIZE)
179 c2 = 0;
180 }
181
182 r = fwrite(cbuf,1,towr,f);
183 if (r<0)
184 return r;
185 rtot += r;
186 len -= r;
187 }
188
189 return rtot;
190 }
191
192 int crypt_fread(void *buf, int len, FILE *f)
193 {
194 unsigned char cbuf[1024];
195 int r, rtot, start, towr, i, c1, c2;
196
197 start = ftell(f);
198 rtot = 0;
199
200 c1 = start % CRYPT1_SIZE;
201 c2 = start % CRYPT2_SIZE;
202
203 if (len>1024)
204 towr = 1024;
205 else
206 towr = len;
207 r = fread(cbuf,1,towr,f);
208
209 while (len>0 && r>0){
210 for (i=0;i<r;i++){
211 ((unsigned char*)buf)[i+rtot] = ((unsigned char*)cbuf)[i] ^ crypt1[c1++] ^ crypt2[c2++];
212
213 if (c1 >= CRYPT1_SIZE)
214 c1 = 0;
215 if (c2 >= CRYPT2_SIZE)
216 c2 = 0;
217 }
218
219 rtot += r;
220 len -= r;
221
222 if (len>1024)
223 towr = 1024;
224 else
225 towr = len;
226 r = fread(cbuf,1,towr,f);
227 if (r<0)
228 return r;
229 }
230
231 return rtot;
232 }
233
234
235
236 typedef struct {
237 FILE *f;
238 } pak_t;
239
240 pak_t *pak_create(char *filename)
241 {
242 char fnbuff[100];
243 char *fnp;
244 FILE *f;
245 pak_t *p;
246 header_t h;
247
248 f = fopen(filename, "w+b");
249 if(f == NULL){
250 return NULL;
251 }
252
253 p = malloc(sizeof(pak_t));
254 p->f = f;
255
256 fnp = strrchr(filename, '/');
257 if (fnp != NULL){
258 strncpy(fnbuff, fnp+1, 31);
259 } else
260 strncpy(fnbuff, filename, 31);
261 fnbuff[31] = 0;
262
263
264 memset(&h, 0, HEADER_SIZE);
265
266 strcpy(h.type, "HEADER");
267 strcpy(h.description, "UpgradeFirmSignature");
268 strcpy(h.name, fnbuff);
269
270 h.length = -1;
271 h.checksumm = -1;
272
273 crypt_fwrite(&h,HEADER_SIZE,f);
274
275 return p;
276 }
277
278 uint32_t checksumm_init()
279 {
280 return 0;
281 }
282
283
284 uint32_t checksumm_update(uint32_t cs, char *buf, int len)
285 {
286 int i;
287
288 for (i=0;i<len/2;i++)
289 cs+=((unsigned short*)buf)[i];
290 if (len&1)
291 cs+=buf[len-1];
292
293 return cs;
294 }
295
296 #define BLKSIZE 0x1000
297 int pak_add_file(pak_t *p,
298 char *type, char *desc, char *infirname, char *filename, uint32_t baseaddr)
299 {
300 char fnbuff[100];
301 char buff[BLKSIZE];
302 header_t h;
303 FILE *inf;
304 char *fnp;
305 long filelen;
306 int bcnt;
307
308 inf = fopen(filename, "r+b");
309 if (inf == NULL){
310 return -1;
311 }
312
313 memset(&h, 0, HEADER_SIZE);
314
315 fseek(inf, 0, SEEK_END);
316 filelen = ftell(inf);
317 fseek(inf, 0, SEEK_SET);
318
319 fnp = strrchr(infirname, '/');
320 if (fnp != NULL){
321 strncpy(fnbuff, fnp+1, 31);
322 } else
323 strncpy(fnbuff, infirname, 31);
324 fnbuff[31] = 0;
325
326 fseek(p->f, HEADER_SIZE, SEEK_CUR);
327 h.checksumm = checksumm_init();
328
329
330 bcnt = fread(buff, 1, BLKSIZE, inf);
331 while (bcnt>0){
332 h.checksumm = checksumm_update(h.checksumm, buff, bcnt);
333 crypt_fwrite(buff, bcnt, p->f);
334 bcnt = fread(buff, 1, BLKSIZE, inf);
335 }
336
337
338 fseek(p->f, -filelen-HEADER_SIZE, SEEK_CUR);
339
340 strcpy(h.type, type);
341 strcpy(h.description, desc);
342 strcpy(h.name, fnbuff);
343
344 h.length = filelen;
345 h.checksumm = (baseaddr==0xffffffff)?h.checksumm:baseaddr;
346 crypt_fwrite(&h,HEADER_SIZE,p->f);
347 fseek(p->f, filelen, SEEK_CUR);
348
349
350 fclose(inf);
351 return 0;
352 }
353
354 int pak_add_4byteid(pak_t *p,
355 char *desc, char *name, uint32_t id)
356 {
357 header_t h;
358
359 memset(&h, 0, HEADER_SIZE);
360 strcpy(h.type, "4BYTEID");
361 strcpy(h.description, desc);
362 strcpy(h.name, name);
363 h.length = 4;
364 h.checksumm = 0;
365
366 crypt_fwrite(&h,HEADER_SIZE,p->f);
367 crypt_fwrite(&id,4,p->f);
368
369 return 0;
370 }
371
372 void pak_close(pak_t *p)
373 {
374 char buff[BLKSIZE];
375 uint32_t len;
376 uint32_t cs;
377 int bcnt;
378
379 len = ftell(p->f);
380 cs = checksumm_init();
381 fseek(p->f, HEADER_SIZE, SEEK_SET);
382
383 bcnt = crypt_fread(buff, BLKSIZE, p->f);
384 while (bcnt>0){
385 cs = checksumm_update(cs, buff, bcnt);
386 bcnt = crypt_fread(buff, BLKSIZE, p->f);
387 }
388
389 fseek(p->f, 32*3, SEEK_SET);
390 crypt_fwrite(&len, 4, p->f);
391 crypt_fwrite(&cs, 4, p->f);
392
393 fclose(p->f);
394 free(p);
395 }
396
397 int usage()
398 {
399 printf("pakfir <out_fir_file> <in_wif_file> <camera_id> [version]\n");
400 exit(1);
401 }
402
403 int main(int argc, char **argv)
404 {
405 char cbuf[100];
406 pak_t *p;
407 char *fir_fn;
408 char *wif_fn;
409 int camid, ver;
410
411
412 if (argc < 4){
413 usage();
414 }
415
416 fir_fn = argv[1];
417 wif_fn = argv[2];
418 camid = strtoul(argv[3], NULL, 0);
419
420 if (argc == 5){
421 ver = strtoul(argv[4], NULL, 0);
422 } else {
423 ver = 0x01000100;
424 }
425
426 printf("Building FIR for camera %04X ver %08X\n", camid, ver);
427
428 p = pak_create(fir_fn);
429
430 sprintf(cbuf, "0x%08X", ver);
431 pak_add_4byteid(p,"VersionID", cbuf, ver);
432 pak_add_4byteid(p,"VersionCheck", "0(no)", 0);
433 pak_add_4byteid(p,"CipherToLog", "1(yes)", 1);
434
435 pak_add_file(p,"PROGRAM", "Program", "WriterInFIR.bin", wif_fn, 0xffffffff);
436
437
438
439
440
441
442 pak_add_4byteid(p,"ModelID", "0x01660000", 0x01660000);
443
444 sprintf(cbuf, "0x%04X", camid);
445 pak_add_4byteid(p,"ProductID", cbuf, camid);
446
447 pak_close(p);
448 return 0;
449 }