1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
24 printf("usage: mkboot <firmware file> <boot file> <output file>\n");
29 unsigned char image
[0x200000 + 0x220 + 0x200000/0x200];
31 int main(int argc
, char *argv
[])
33 char *infile
, *bootfile
, *outfile
;
39 int actual_length
, total_length
, binary_length
, num_chksums
;
49 memset(image
, 0xff, sizeof(image
));
51 /* First, read the iriver original firmware into the image */
52 f
= fopen(infile
, "rb");
58 i
= fread(image
, 1, 16, f
);
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
);
79 /* Now, read the boot loader into the image */
80 f
= fopen(bootfile
, "rb");
86 fseek(f
, 0, SEEK_END
);
89 fseek(f
, 0, SEEK_SET
);
91 i
= fread(image
+0x220 + 0x1f0000, 1, len
, f
);
99 f
= fopen(outfile
, "wb");
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
) {
157 printf("Wrote 0x%x bytes in %s\n", total_length
, outfile
);