diff options
Diffstat (limited to 'tools/gigabeats.c')
-rw-r--r-- | tools/gigabeats.c | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/tools/gigabeats.c b/tools/gigabeats.c new file mode 100644 index 0000000000..0d60eae781 --- /dev/null +++ b/tools/gigabeats.c | |||
@@ -0,0 +1,171 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2007 by Will Robertson | ||
11 | * | ||
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. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include <stdio.h> | ||
21 | #include <stdlib.h> | ||
22 | |||
23 | static FILE * openinfile( const char * filename ) | ||
24 | { | ||
25 | FILE * F = fopen( filename, "rb" ); | ||
26 | if( F == NULL ) | ||
27 | { | ||
28 | fprintf( stderr, "Couldn't open input file %s\n", filename ); | ||
29 | perror( "Error was " ); | ||
30 | exit( -1 ); | ||
31 | }; | ||
32 | return F; | ||
33 | }; | ||
34 | |||
35 | static FILE * openoutfile( const char * filename ) | ||
36 | { | ||
37 | FILE * F = fopen( filename, "wb" ); | ||
38 | if( F == NULL ) | ||
39 | { | ||
40 | fprintf( stderr, "Couldn't open output file %s\n", filename ); | ||
41 | perror( "Error was " ); | ||
42 | exit( -1 ); | ||
43 | }; | ||
44 | return F; | ||
45 | }; | ||
46 | |||
47 | unsigned long calc_csum(const unsigned char* pb, int cb) | ||
48 | { | ||
49 | unsigned long l = 0; | ||
50 | while (cb--) | ||
51 | l += *pb++; | ||
52 | return l; | ||
53 | } | ||
54 | |||
55 | int gigabeat_s_code(char *infile, char *outfile) | ||
56 | { | ||
57 | FILE *in, *out; | ||
58 | unsigned long size = 0; | ||
59 | unsigned long data; | ||
60 | int imagelength; | ||
61 | |||
62 | in = openinfile(infile); | ||
63 | out = openoutfile(outfile); | ||
64 | |||
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; | ||
72 | } | ||
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... | ||
78 | */ | ||
79 | |||
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); | ||
86 | |||
87 | unsigned long record[4]; | ||
88 | unsigned long extra; | ||
89 | |||
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); | ||
96 | |||
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); | ||
105 | |||
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); | ||
112 | |||
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); | ||
120 | |||
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); | ||
128 | |||
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); | ||
158 | |||
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); | ||
165 | |||
166 | fprintf(stderr, "File processed successfully\n" ); | ||
167 | |||
168 | fclose(in); | ||
169 | fclose(out); | ||
170 | return(0); | ||
171 | } | ||