Minor cleanup
[Rockbox.git] / tools / gigabeats.c
blob0d60eae7816b4160d68b08d7e4b2d40f64c7ea93
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Will Robertson
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 static FILE * openinfile( const char * filename )
25 FILE * F = fopen( filename, "rb" );
26 if( F == NULL )
28 fprintf( stderr, "Couldn't open input file %s\n", filename );
29 perror( "Error was " );
30 exit( -1 );
32 return F;
35 static FILE * openoutfile( const char * filename )
37 FILE * F = fopen( filename, "wb" );
38 if( F == NULL )
40 fprintf( stderr, "Couldn't open output file %s\n", filename );
41 perror( "Error was " );
42 exit( -1 );
44 return F;
47 unsigned long calc_csum(const unsigned char* pb, int cb)
49 unsigned long l = 0;
50 while (cb--)
51 l += *pb++;
52 return l;
55 int gigabeat_s_code(char *infile, char *outfile)
57 FILE *in, *out;
58 unsigned long size = 0;
59 unsigned long data;
60 int imagelength;
62 in = openinfile(infile);
63 out = openoutfile(outfile);
65 fseek(in, 0, SEEK_END);
66 size = ftell(in);
67 fseek(in, 0, SEEK_SET);
68 unsigned long *binptr = malloc(size);
69 if(binptr == NULL) {
70 fprintf(stderr, "Not enough memory to perform the requested operation. Aborting.\n" );
71 return 0;
73 fread(binptr, size/4, 4, in);
74 /* 15 bytes for header, three 12 byte headers, the data for the first three
75 * records, 12 byte header for code, code and the 12 byte footer
76 * However, the original nk.bin's length doesn't correspond with
77 * the length of the file, so I don't know what's up...
80 unsigned long header[2];
81 header[0] = 0x88200000;
82 /* header[1] = 15 + 12 + 4 + 12 + 8 + 12 + 4 + 12 + size + 12; */
83 header[1] = 0xCC0CD8; /* The bootloader checks this value and compares */
84 fwrite("B000FF\n", 7, 1, out);
85 fwrite(header, sizeof(header), 1, out);
87 unsigned long record[4];
88 unsigned long extra;
90 /*First record*/
91 record[0] = 0x88200000;
92 record[1] = 4;
93 record[2] = 0x1eb;
94 record[3] = 0xEA0003FE;
95 fwrite(record, sizeof(record), 1, out);
97 /*Second record*/
98 record[0] = 0x88200040;
99 record[1] = 8;
100 record[2] = 0x3e9;
101 record[3] = 0x43454345;
102 extra = 0x88EBF274;
103 fwrite(record, sizeof(record), 1, out);
104 fwrite(&extra, sizeof(extra), 1, out);
106 /*Third record*/
107 record[0] = 0x88200048;
108 record[1] = 4;
109 record[2] = 0x231;
110 record[3] = 0x00CBF274;
111 fwrite(record, sizeof(record), 1, out);
113 /*Signature bypass record*/
114 unsigned long magic = 0xE3A00001;
115 record[0] = 0x88065A10;
116 record[1] = 4;
117 record[2] = calc_csum((unsigned char*)&magic,4);
118 record[3] = magic;
119 fwrite(record, sizeof(record), 1, out);
121 /*The actual code*/
122 header[0] = 0x88201000;
123 header[1] = size;
124 extra = calc_csum((unsigned char*)binptr, size);
125 fwrite(header, sizeof(header), 1, out);
126 fwrite(&extra, sizeof(extra), 1, out);
127 fwrite(binptr, size, 1, out);
129 /* Table of contents. It's a start, but it still won't boot.
130 * Looks like it needs the file/module info as well... */
131 binptr[0] = 0x02000000;
132 binptr[1] = 0x02000000;
133 binptr[2] = 0x88040000;
134 binptr[3] = 0x88076338;
135 binptr[4] = 0x1;
136 binptr[5] = 0x88080000;
137 binptr[6] = 0x8809C000;
138 binptr[7] = 0x88100000;
139 binptr[8] = 0x0;
140 binptr[9] = 0x0;
141 binptr[10] = 0x0;
142 binptr[11] = 0x0;
143 binptr[12] = 0x0;
144 binptr[13] = 0x0;
145 binptr[14] = 0x80808080;
146 binptr[15] = 0x0;
147 binptr[16] = 0x0;
148 binptr[17] = 0x201C2;
149 binptr[18] = 0x88050940;
150 binptr[19] = 0x0;
151 binptr[20] = 0x0;
152 header[0] = 0x88EBF274;
153 header[1] = 0x54;
154 extra = calc_csum((unsigned char*)binptr, 84);
155 fwrite(header, sizeof(header), 1, out);
156 fwrite(&extra, sizeof(extra), 1, out);
157 fwrite(binptr, 84, 1, out);
159 /*The footer*/
160 header[0] = 0;
161 header[1] = 0x88201000;
162 extra = 0;
163 fwrite(header, sizeof(header), 1, out);
164 fwrite(&extra, sizeof(extra), 1, out);
166 fprintf(stderr, "File processed successfully\n" );
168 fclose(in);
169 fclose(out);
170 return(0);