Improve insert of playlists. Decode M3U files using current code page and strip any...
[kugel-rb.git] / tools / mkboot.c
blob4fd99e2ccd2e25c2a08f5dc3a8c240fa17172fdd
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>
21 #include <string.h>
23 void usage(void)
25 printf("usage: mkboot [-h300] <firmware file> <boot file> <output file>\n");
27 exit(1);
30 unsigned char image[0x400000 + 0x220 + 0x400000/0x200];
32 int main(int argc, char *argv[])
34 char *infile, *bootfile, *outfile;
35 FILE *f;
36 long pos;
37 int i;
38 int file_length;
39 int len;
40 int actual_length, total_length, binary_length, num_chksums;
41 int origin = 0x1f0000; /* H1x0 bootloader address */
43 if(argc < 3) {
44 usage();
47 if(!strcmp(argv[1], "-h300")) {
48 infile = argv[2];
49 bootfile = argv[3];
50 outfile = argv[4];
52 origin = 0x3f0000; /* H3x0 bootloader address */
54 else
56 infile = argv[1];
57 bootfile = argv[2];
58 outfile = argv[3];
61 memset(image, 0xff, sizeof(image));
63 /* First, read the iriver original firmware into the image */
64 f = fopen(infile, "rb");
65 if(!f) {
66 perror(infile);
67 exit(1);
70 i = fread(image, 1, 16, f);
71 if(i < 16) {
72 perror(infile);
73 exit(1);
76 /* This is the length of the binary image without the scrambling
77 overhead (but including the ESTFBINR header) */
78 binary_length = image[4] + (image[5] << 8) +
79 (image[6] << 16) + (image[7] << 24);
81 /* Read the rest of the binary data, but not the checksum block */
82 len = binary_length+0x200-16;
83 i = fread(image+16, 1, len, f);
84 if(i < len) {
85 perror(infile);
86 exit(1);
89 fclose(f);
91 /* Now, read the boot loader into the image */
92 f = fopen(bootfile, "rb");
93 if(!f) {
94 perror(bootfile);
95 exit(1);
98 fseek(f, 0, SEEK_END);
99 len = ftell(f);
101 fseek(f, 0, SEEK_SET);
103 i = fread(image+0x220 + origin, 1, len, f);
104 if(i < len) {
105 perror(bootfile);
106 exit(1);
109 fclose(f);
111 f = fopen(outfile, "wb");
112 if(!f) {
113 perror(outfile);
114 exit(1);
117 /* Patch the reset vector to start the boot loader */
118 image[0x220 + 4] = image[origin + 0x220 + 4];
119 image[0x220 + 5] = image[origin + 0x220 + 5];
120 image[0x220 + 6] = image[origin + 0x220 + 6];
121 image[0x220 + 7] = image[origin + 0x220 + 7];
123 /* This is the actual length of the binary, excluding all headers */
124 actual_length = origin + len;
126 /* Patch the ESTFBINR header */
127 image[0x20c] = (actual_length >> 24) & 0xff;
128 image[0x20d] = (actual_length >> 16) & 0xff;
129 image[0x20e] = (actual_length >> 8) & 0xff;
130 image[0x20f] = actual_length & 0xff;
132 image[0x21c] = (actual_length >> 24) & 0xff;
133 image[0x21d] = (actual_length >> 16) & 0xff;
134 image[0x21e] = (actual_length >> 8) & 0xff;
135 image[0x21f] = actual_length & 0xff;
137 /* This is the length of the binary, including the ESTFBINR header and
138 rounded up to the nearest 0x200 boundary */
139 binary_length = (actual_length + 0x20 + 0x1ff) & 0xfffffe00;
141 /* The number of checksums, i.e number of 0x200 byte blocks */
142 num_chksums = binary_length / 0x200;
144 /* The total file length, including all headers and checksums */
145 total_length = binary_length + num_chksums + 0x200;
147 /* Patch the scrambler header with the new length info */
148 image[0] = total_length & 0xff;
149 image[1] = (total_length >> 8) & 0xff;
150 image[2] = (total_length >> 16) & 0xff;
151 image[3] = (total_length >> 24) & 0xff;
153 image[4] = binary_length & 0xff;
154 image[5] = (binary_length >> 8) & 0xff;
155 image[6] = (binary_length >> 16) & 0xff;
156 image[7] = (binary_length >> 24) & 0xff;
158 image[8] = num_chksums & 0xff;
159 image[9] = (num_chksums >> 8) & 0xff;
160 image[10] = (num_chksums >> 16) & 0xff;
161 image[11] = (num_chksums >> 24) & 0xff;
163 i = fwrite(image, 1, total_length, f);
164 if(i < total_length) {
165 perror(outfile);
166 exit(1);
169 printf("Wrote 0x%x bytes in %s\n", total_length, outfile);
171 fclose(f);
173 return 0;