Simplify progress emit in httpget class a bit.
[Rockbox.git] / tools / mkboot.c
blobce743e9414725812796accb7ce987aef4cba08fd
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 int i;
37 int len;
38 int actual_length, total_length, binary_length, num_chksums;
39 int origin = 0x1f0000; /* H1x0 bootloader address */
41 if(argc < 3) {
42 usage();
45 if(!strcmp(argv[1], "-h300")) {
46 infile = argv[2];
47 bootfile = argv[3];
48 outfile = argv[4];
50 origin = 0x3f0000; /* H3x0 bootloader address */
52 else
54 infile = argv[1];
55 bootfile = argv[2];
56 outfile = argv[3];
59 memset(image, 0xff, sizeof(image));
61 /* First, read the iriver original firmware into the image */
62 f = fopen(infile, "rb");
63 if(!f) {
64 perror(infile);
65 exit(1);
68 i = fread(image, 1, 16, f);
69 if(i < 16) {
70 perror(infile);
71 exit(1);
74 /* This is the length of the binary image without the scrambling
75 overhead (but including the ESTFBINR header) */
76 binary_length = image[4] + (image[5] << 8) +
77 (image[6] << 16) + (image[7] << 24);
79 /* Read the rest of the binary data, but not the checksum block */
80 len = binary_length+0x200-16;
81 i = fread(image+16, 1, len, f);
82 if(i < len) {
83 perror(infile);
84 exit(1);
87 fclose(f);
89 /* Now, read the boot loader into the image */
90 f = fopen(bootfile, "rb");
91 if(!f) {
92 perror(bootfile);
93 exit(1);
96 fseek(f, 0, SEEK_END);
97 len = ftell(f);
99 fseek(f, 0, SEEK_SET);
101 i = fread(image+0x220 + origin, 1, len, f);
102 if(i < len) {
103 perror(bootfile);
104 exit(1);
107 fclose(f);
109 f = fopen(outfile, "wb");
110 if(!f) {
111 perror(outfile);
112 exit(1);
115 /* Patch the reset vector to start the boot loader */
116 image[0x220 + 4] = image[origin + 0x220 + 4];
117 image[0x220 + 5] = image[origin + 0x220 + 5];
118 image[0x220 + 6] = image[origin + 0x220 + 6];
119 image[0x220 + 7] = image[origin + 0x220 + 7];
121 /* This is the actual length of the binary, excluding all headers */
122 actual_length = origin + len;
124 /* Patch the ESTFBINR header */
125 image[0x20c] = (actual_length >> 24) & 0xff;
126 image[0x20d] = (actual_length >> 16) & 0xff;
127 image[0x20e] = (actual_length >> 8) & 0xff;
128 image[0x20f] = actual_length & 0xff;
130 image[0x21c] = (actual_length >> 24) & 0xff;
131 image[0x21d] = (actual_length >> 16) & 0xff;
132 image[0x21e] = (actual_length >> 8) & 0xff;
133 image[0x21f] = actual_length & 0xff;
135 /* This is the length of the binary, including the ESTFBINR header and
136 rounded up to the nearest 0x200 boundary */
137 binary_length = (actual_length + 0x20 + 0x1ff) & 0xfffffe00;
139 /* The number of checksums, i.e number of 0x200 byte blocks */
140 num_chksums = binary_length / 0x200;
142 /* The total file length, including all headers and checksums */
143 total_length = binary_length + num_chksums + 0x200;
145 /* Patch the scrambler header with the new length info */
146 image[0] = total_length & 0xff;
147 image[1] = (total_length >> 8) & 0xff;
148 image[2] = (total_length >> 16) & 0xff;
149 image[3] = (total_length >> 24) & 0xff;
151 image[4] = binary_length & 0xff;
152 image[5] = (binary_length >> 8) & 0xff;
153 image[6] = (binary_length >> 16) & 0xff;
154 image[7] = (binary_length >> 24) & 0xff;
156 image[8] = num_chksums & 0xff;
157 image[9] = (num_chksums >> 8) & 0xff;
158 image[10] = (num_chksums >> 16) & 0xff;
159 image[11] = (num_chksums >> 24) & 0xff;
161 i = fwrite(image, 1, total_length, f);
162 if(i < total_length) {
163 perror(outfile);
164 exit(1);
167 printf("Wrote 0x%x bytes in %s\n", total_length, outfile);
169 fclose(f);
171 return 0;