Fix building the ipod sims.
[kugel-rb.git] / tools / gigabeats.c
blobc109d6ad1cf426251e122c10283d4ed606813175
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>
22 #include <inttypes.h>
23 #include <sys/stat.h>
25 /* Entry point (and load address) for the main Rockbox bootloader */
26 #define BL_ENTRY_POINT 0x8a000000
29 static FILE * openinfile( const char * filename )
31 FILE * F = fopen( filename, "rb" );
32 if( F == NULL )
34 fprintf( stderr, "Couldn't open input file %s\n", filename );
35 perror( "Error was " );
36 exit( -1 );
38 return F;
41 static FILE * openoutfile( const char * filename )
43 FILE * F = fopen( filename, "wb" );
44 if( F == NULL )
46 fprintf( stderr, "Couldn't open output file %s\n", filename );
47 perror( "Error was " );
48 exit( -1 );
50 return F;
53 static uint32_t calc_csum(const unsigned char* pb, int cb)
55 uint32_t l = 0;
56 while (cb--)
57 l += *pb++;
58 return l;
61 static void put_uint32le(uint32_t x, unsigned char* p)
63 p[0] = x & 0xff;
64 p[1] = (x >> 8) & 0xff;
65 p[2] = (x >> 16) & 0xff;
66 p[3] = (x >> 24) & 0xff;
69 int gigabeat_s_code(char *infile, char *outfile)
71 FILE *in, *out;
72 unsigned int size;
73 unsigned int newsize;
74 unsigned char* buf;
76 in = openinfile(infile);
77 out = openoutfile(outfile);
79 /* Step 1: Load the binary file into memory */
80 fseek(in, 0, SEEK_END);
81 size = ftell(in);
83 /* 15 bytes for header, 16 for signature bypass,
84 * 12 for record header, 12 for footer */
85 newsize = 15 + 16 + 12 + size + 12;
86 buf = malloc(newsize);
87 if(buf == NULL) {
88 fprintf(stderr, "Not enough memory to perform the requested operation. Aborting.\n" );
89 return 0;
91 fseek(in, 0, SEEK_SET);
92 fread(buf + 43, size, 1, in);
93 fclose(in);
95 /* Step 2: Create the file header */
96 sprintf(buf, "B000FF\n");
97 put_uint32le(0x88200000, buf+7);
98 /* If the value below is too small, the update will attempt to flash.
99 * Be careful when changing this (leaving it as is won't cause issues) */
100 put_uint32le(0xCC0CD8, buf+11);
102 /* Step 3: Add the signature bypass record */
103 put_uint32le(0x88065A10, buf+15);
104 put_uint32le(4, buf+19);
105 put_uint32le(0xE3A00001, buf+27);
106 put_uint32le(calc_csum(buf+27,4), buf+23);
108 /* Step 4: Create a record for the actual code */
109 put_uint32le(BL_ENTRY_POINT, buf+31);
110 put_uint32le(size, buf+35);
111 put_uint32le(calc_csum(buf + 43, size), buf+39);
113 /* Step 5: Write the footer */
114 put_uint32le(0, buf+newsize-12);
115 put_uint32le(BL_ENTRY_POINT, buf+newsize-8);
116 put_uint32le(0, buf+newsize-4);
118 /* Step 6: Write the resulting file */
119 fwrite(buf, newsize, 1, out);
120 fclose(out);
122 fprintf(stderr, "File processed successfully\n" );
124 return(0);