Properly clear clear-on-write bits in the PP502x USB controller
[kugel-rb.git] / tools / gigabeat.c
blob761e8c31a0e1868bccad4deb665fe9ffb365f2b6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Christian Hack
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 extern void int2le(unsigned int val, unsigned char* addr);
24 extern unsigned int le2int(unsigned char* buf);
26 static FILE * openinfile( const char * filename )
28 FILE * F = fopen( filename, "rb" );
29 if( F == NULL )
31 fprintf( stderr, "Couldn't open input file %s\n", filename );
32 perror( "Error was " );
33 exit( -1 );
35 return F;
38 static FILE * openoutfile( const char * filename )
40 FILE * F = fopen( filename, "wb" );
41 if( F == NULL )
43 fprintf( stderr, "Couldn't open output file %s\n", filename );
44 perror( "Error was " );
45 exit( -1 );
47 return F;
50 int gigabeat_code(char *infile, char *outfile)
52 FILE *in, *out;
53 unsigned long size = 0;
54 unsigned long bytes_read;
55 unsigned char buf[4];
56 unsigned long data;
57 unsigned long key = 0x19751217;
59 in = openinfile(infile);
60 out = openoutfile(outfile);
62 while (!feof(in)) {
63 bytes_read = fread(buf, 1, 4, in);
65 /* Read in little-endian */
66 data = le2int(buf);
68 data = data ^ key;
70 key = key + (key << 1);
71 key = key + 0x19751217;
73 size += bytes_read;
75 /* Write out little-endian */
76 int2le(data, buf);
78 fwrite(buf, 1, bytes_read, out);
81 fprintf(stderr, "File processed successfully\n" );
83 fclose(in);
84 fclose(out);
85 return(0);