1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 static FILE * openinfile( const char * filename
)
25 FILE * F
= fopen( filename
, "rb" );
28 fprintf( stderr
, "Couldn't open input file %s\n", filename
);
29 perror( "Error was " );
35 static FILE * openoutfile( const char * filename
)
37 FILE * F
= fopen( filename
, "wb" );
40 fprintf( stderr
, "Couldn't open output file %s\n", filename
);
41 perror( "Error was " );
47 unsigned long calc_csum(const unsigned char* pb
, int cb
)
55 int gigabeat_s_code(char *infile
, char *outfile
)
58 unsigned long size
= 0;
62 in
= openinfile(infile
);
63 out
= openoutfile(outfile
);
65 fseek(in
, 0, SEEK_END
);
67 fseek(in
, 0, SEEK_SET
);
68 unsigned long *binptr
= malloc(size
);
70 fprintf(stderr
, "Not enough memory to perform the requested operation. Aborting.\n" );
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];
91 record
[0] = 0x88200000;
94 record
[3] = 0xEA0003FE;
95 fwrite(record
, sizeof(record
), 1, out
);
98 record
[0] = 0x88200040;
101 record
[3] = 0x43454345;
103 fwrite(record
, sizeof(record
), 1, out
);
104 fwrite(&extra
, sizeof(extra
), 1, out
);
107 record
[0] = 0x88200048;
110 record
[3] = 0x00CBF274;
111 fwrite(record
, sizeof(record
), 1, out
);
113 /*Signature bypass record*/
114 unsigned long magic
= 0xE3A00001;
115 record
[0] = 0x88065A10;
117 record
[2] = calc_csum((unsigned char*)&magic
,4);
119 fwrite(record
, sizeof(record
), 1, out
);
122 header
[0] = 0x88201000;
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;
136 binptr
[5] = 0x88080000;
137 binptr
[6] = 0x8809C000;
138 binptr
[7] = 0x88100000;
145 binptr
[14] = 0x80808080;
148 binptr
[17] = 0x201C2;
149 binptr
[18] = 0x88050940;
152 header
[0] = 0x88EBF274;
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
);
161 header
[1] = 0x88201000;
163 fwrite(header
, sizeof(header
), 1, out
);
164 fwrite(&extra
, sizeof(extra
), 1, out
);
166 fprintf(stderr
, "File processed successfully\n" );