Update the russian translation.
[kugel-rb.git] / tools / gigabeats.c
blob2d2d48986507c09f82ef41e7ddcc6ff892b1eff6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Will Robertson
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <inttypes.h>
25 #include <sys/stat.h>
27 /* Entry point (and load address) for the main Rockbox bootloader */
28 #define BL_ENTRY_POINT 0x8a000000
31 static FILE * openinfile( const char * filename )
33 FILE * F = fopen( filename, "rb" );
34 if( F == NULL )
36 fprintf( stderr, "Couldn't open input file %s\n", filename );
37 perror( "Error was " );
38 exit( -1 );
40 return F;
43 static FILE * openoutfile( const char * filename )
45 FILE * F = fopen( filename, "wb" );
46 if( F == NULL )
48 fprintf( stderr, "Couldn't open output file %s\n", filename );
49 perror( "Error was " );
50 exit( -1 );
52 return F;
55 static uint32_t calc_csum(const unsigned char* pb, int cb)
57 uint32_t l = 0;
58 while (cb--)
59 l += *pb++;
60 return l;
63 static void put_uint32le(uint32_t x, unsigned char* p)
65 p[0] = x & 0xff;
66 p[1] = (x >> 8) & 0xff;
67 p[2] = (x >> 16) & 0xff;
68 p[3] = (x >> 24) & 0xff;
71 int gigabeat_s_code(char *infile, char *outfile)
73 FILE *in, *out;
74 unsigned int size;
75 unsigned int newsize;
76 unsigned char* buf;
77 size_t rc;
79 in = openinfile(infile);
80 out = openoutfile(outfile);
82 /* Step 1: Load the binary file into memory */
83 fseek(in, 0, SEEK_END);
84 size = ftell(in);
86 /* 15 bytes for header, 16 for signature bypass,
87 * 12 for record header, 12 for footer */
88 newsize = 15 + 16 + 12 + size + 12;
89 buf = malloc(newsize);
90 if(buf == NULL) {
91 fprintf(stderr,
92 "Not enough memory to perform the operation. Aborting.\n" );
93 return 1;
95 fseek(in, 0, SEEK_SET);
96 rc = fread(buf + 43, 1, size, in);
97 fclose(in);
99 if(rc != size) {
100 /* failed to read the wanted amount */
101 fprintf(stderr, "Failed reading from %s.\n", infile);
102 return 2;
104 /* Step 2: Create the file header */
105 sprintf((char *)buf, "B000FF\n");
106 put_uint32le(0x88200000, buf+7);
107 /* If the value below is too small, the update will attempt to flash.
108 * Be careful when changing this (leaving it as is won't cause issues) */
109 put_uint32le(0xCC0CD8, buf+11);
111 /* Step 3: Add the signature bypass record */
112 put_uint32le(0x88065A10, buf+15);
113 put_uint32le(4, buf+19);
114 put_uint32le(0xE3A00001, buf+27);
115 put_uint32le(calc_csum(buf+27,4), buf+23);
117 /* Step 4: Create a record for the actual code */
118 put_uint32le(BL_ENTRY_POINT, buf+31);
119 put_uint32le(size, buf+35);
120 put_uint32le(calc_csum(buf + 43, size), buf+39);
122 /* Step 5: Write the footer */
123 put_uint32le(0, buf+newsize-12);
124 put_uint32le(BL_ENTRY_POINT, buf+newsize-8);
125 put_uint32le(0, buf+newsize-4);
127 /* Step 6: Write the resulting file */
128 fwrite(buf, newsize, 1, out);
129 fclose(out);
131 fprintf(stderr, "File processed successfully\n" );
133 return(0);