Fix for ff/rw in long MP3 files.
[kugel-rb.git] / tools / mkboot.c
blob420ae248e6870cbc45f423cecbee452455217146
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
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 ****************************************************************************/
19 #include <stdio.h>
20 #include <stdlib.h>
22 void usage(void)
24 printf("usage: mkboot <firmware file> <boot file> <output file>\n");
26 exit(1);
29 unsigned char image[0x200000 + 0x220 + 0x200000/0x200];
31 int main(int argc, char *argv[])
33 char *infile, *bootfile, *outfile;
34 FILE *f;
35 long pos;
36 int i;
37 int file_length;
38 int len;
39 int actual_length, total_length, binary_length, num_chksums;
41 if(argc < 3) {
42 usage();
45 infile = argv[1];
46 bootfile = argv[2];
47 outfile = argv[3];
49 memset(image, 0xff, sizeof(image));
51 /* First, read the iriver original firmware into the image */
52 f = fopen(infile, "rb");
53 if(!f) {
54 perror(infile);
55 exit(1);
58 i = fread(image, 1, 16, f);
59 if(i < 16) {
60 perror(infile);
61 exit(1);
64 /* This is the length of the binary image without the scrambling
65 overhead (but including the ESTFBINR header) */
66 binary_length = image[4] + (image[5] << 8) +
67 (image[6] << 16) + (image[7] << 24);
69 /* Read the rest of the binary data, but not the checksum block */
70 len = binary_length+0x200-16;
71 i = fread(image+16, 1, len, f);
72 if(i < len) {
73 perror(infile);
74 exit(1);
77 fclose(f);
79 /* Now, read the boot loader into the image */
80 f = fopen(bootfile, "rb");
81 if(!f) {
82 perror(bootfile);
83 exit(1);
86 fseek(f, 0, SEEK_END);
87 len = ftell(f);
89 fseek(f, 0, SEEK_SET);
91 i = fread(image+0x220 + 0x1f0000, 1, len, f);
92 if(i < len) {
93 perror(bootfile);
94 exit(1);
97 fclose(f);
99 f = fopen(outfile, "wb");
100 if(!f) {
101 perror(outfile);
102 exit(1);
105 /* Patch the reset vector to start the boot loader */
106 image[0x220 + 4] = image[0x1f0000 + 0x220 + 4];
107 image[0x220 + 5] = image[0x1f0000 + 0x220 + 5];
108 image[0x220 + 6] = image[0x1f0000 + 0x220 + 6];
109 image[0x220 + 7] = image[0x1f0000 + 0x220 + 7];
111 /* This is the actual length of the binary, excluding all headers */
112 actual_length = 0x1f0000 + len;
114 /* Patch the ESTFBINR header */
115 image[0x20c] = (actual_length >> 24) & 0xff;
116 image[0x20d] = (actual_length >> 16) & 0xff;
117 image[0x20e] = (actual_length >> 8) & 0xff;
118 image[0x20f] = actual_length & 0xff;
120 image[0x21c] = (actual_length >> 24) & 0xff;
121 image[0x21d] = (actual_length >> 16) & 0xff;
122 image[0x21e] = (actual_length >> 8) & 0xff;
123 image[0x21f] = actual_length & 0xff;
125 /* This is the length of the binary, including the ESTFBINR header and
126 rounded up to the nearest 0x200 boundary */
127 binary_length = (actual_length + 0x20 + 0x1ff) & 0xfffffe00;
129 /* The number of checksums, i.e number of 0x200 byte blocks */
130 num_chksums = binary_length / 0x200;
132 /* The total file length, including all headers and checksums */
133 total_length = binary_length + num_chksums + 0x200;
135 /* Patch the scrambler header with the new length info */
136 image[0] = total_length & 0xff;
137 image[1] = (total_length >> 8) & 0xff;
138 image[2] = (total_length >> 16) & 0xff;
139 image[3] = (total_length >> 24) & 0xff;
141 image[4] = binary_length & 0xff;
142 image[5] = (binary_length >> 8) & 0xff;
143 image[6] = (binary_length >> 16) & 0xff;
144 image[7] = (binary_length >> 24) & 0xff;
146 image[8] = num_chksums & 0xff;
147 image[9] = (num_chksums >> 8) & 0xff;
148 image[10] = (num_chksums >> 16) & 0xff;
149 image[11] = (num_chksums >> 24) & 0xff;
151 i = fwrite(image, 1, total_length, f);
152 if(i < total_length) {
153 perror(outfile);
154 exit(1);
157 printf("Wrote 0x%x bytes in %s\n", total_length, outfile);
159 fclose(f);
161 return 0;