When we read the year from the RTC, it can be so totally messed up so that
[kugel-rb.git] / tools / scramble.c
blobca3cea5a5896c783aa8dac7ead4a90ed25c9b525
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h>
21 #include <stdlib.h>
23 int main (int argc, char** argv)
25 unsigned long length,i,slen;
26 unsigned char *inbuf,*outbuf;
27 unsigned short crc=0;
28 unsigned char header[24];
29 unsigned char *iname = argv[1];
30 unsigned char *oname = argv[2];
31 int headerlen = 6;
32 FILE* file;
34 if (argc < 3) {
35 printf("usage: %s [-fm] <input file> <output file>\n",argv[0]);
36 return -1;
39 if (argv[1][0] == '-') { /* assume any parameter is -fm :-) */
40 headerlen = 24;
41 iname = argv[2];
42 oname = argv[3];
45 /* open file */
46 file = fopen(iname,"rb");
47 if (!file) {
48 perror(iname);
49 return -1;
51 fseek(file,0,SEEK_END);
52 length = ftell(file);
53 length = (length + 3) & ~3; /* Round up to nearest 4 byte boundary */
55 if ((length + headerlen) >= 0x32000) {
56 printf("error: max firmware size is 200KB!\n");
57 fclose(file);
58 return -1;
61 fseek(file,0,SEEK_SET);
62 inbuf = malloc(length);
63 outbuf = malloc(length);
64 if ( !inbuf || !outbuf ) {
65 printf("out of memory!\n");
66 return -1;
69 /* read file */
70 i=fread(inbuf,1,length,file);
71 if ( !i ) {
72 perror(iname);
73 return -1;
75 fclose(file);
77 /* scramble */
78 slen = length/4;
79 for (i = 0; i < length; i++) {
80 unsigned long addr = (i >> 2) + ((i % 4) * slen);
81 unsigned char data = inbuf[i];
82 data = ~((data << 1) | ((data >> 7) & 1)); /* poor man's ROL */
83 outbuf[addr] = data;
86 /* calculate checksum */
87 for (i=0;i<length;i++)
88 crc += inbuf[i];
90 /* make header */
91 memset(header, 0, sizeof header);
92 if (headerlen == 6) {
93 header[0] = (length >> 24) & 0xff;
94 header[1] = (length >> 16) & 0xff;
95 header[2] = (length >> 8) & 0xff;
96 header[3] = length & 0xff;
97 header[4] = (crc >> 8) & 0xff;
98 header[5] = crc & 0xff;
100 else {
101 header[0] =
102 header[1] =
103 header[2] =
104 header[3] = 0xff; /* ??? */
106 header[6] = (crc >> 8) & 0xff;
107 header[7] = crc & 0xff;
109 header[11] = 4; /* ??? */
111 header[15] = headerlen; /* really? */
113 header[20] = (length >> 24) & 0xff;
114 header[21] = (length >> 16) & 0xff;
115 header[22] = (length >> 8) & 0xff;
116 header[23] = length & 0xff;
119 /* write file */
120 file = fopen(oname,"wb");
121 if ( !file ) {
122 perror(oname);
123 return -1;
125 if ( !fwrite(header,headerlen,1,file) ) {
126 perror(oname);
127 return -1;
129 if ( !fwrite(outbuf,length,1,file) ) {
130 perror(oname);
131 return -1;
133 fclose(file);
135 free(inbuf);
136 free(outbuf);
138 return 0;