Factor out scramble / mkboot functions to allow easier reuse (for rbutil).
[Rockbox.git] / tools / mkboot.c
blobd86a4fc177ebbfdc7c022c4bf7e7a3af33096869
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>
22 #include "mkboot.h"
24 #ifndef RBUTIL
25 static void usage(void)
27 printf("usage: mkboot [-h300] <firmware file> <boot file> <output file>\n");
29 exit(1);
31 #endif
33 static unsigned char image[0x400000 + 0x220 + 0x400000/0x200];
35 #ifndef RBUTIL
36 int main(int argc, char *argv[])
38 char *infile, *bootfile, *outfile;
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];
58 return mkboot(infile, bootfile, outfile, origin);
60 #endif
62 int mkboot(const char* infile, const char* bootfile, const char* outfile, int origin)
64 FILE *f;
65 int i;
66 int len;
67 int actual_length, total_length, binary_length, num_chksums;
69 memset(image, 0xff, sizeof(image));
71 /* First, read the iriver original firmware into the image */
72 f = fopen(infile, "rb");
73 if(!f) {
74 perror(infile);
75 return -1;
78 i = fread(image, 1, 16, f);
79 if(i < 16) {
80 perror(infile);
81 fclose(f);
82 return -2;
85 /* This is the length of the binary image without the scrambling
86 overhead (but including the ESTFBINR header) */
87 binary_length = image[4] + (image[5] << 8) +
88 (image[6] << 16) + (image[7] << 24);
90 /* Read the rest of the binary data, but not the checksum block */
91 len = binary_length+0x200-16;
92 i = fread(image+16, 1, len, f);
93 if(i < len) {
94 perror(infile);
95 fclose(f);
96 return -3;
99 fclose(f);
101 /* Now, read the boot loader into the image */
102 f = fopen(bootfile, "rb");
103 if(!f) {
104 perror(bootfile);
105 fclose(f);
106 return -4;
109 fseek(f, 0, SEEK_END);
110 len = ftell(f);
112 fseek(f, 0, SEEK_SET);
114 i = fread(image+0x220 + origin, 1, len, f);
115 if(i < len) {
116 perror(bootfile);
117 fclose(f);
118 return -5;
121 fclose(f);
123 f = fopen(outfile, "wb");
124 if(!f) {
125 perror(outfile);
126 return -6;
129 /* Patch the reset vector to start the boot loader */
130 image[0x220 + 4] = image[origin + 0x220 + 4];
131 image[0x220 + 5] = image[origin + 0x220 + 5];
132 image[0x220 + 6] = image[origin + 0x220 + 6];
133 image[0x220 + 7] = image[origin + 0x220 + 7];
135 /* This is the actual length of the binary, excluding all headers */
136 actual_length = origin + len;
138 /* Patch the ESTFBINR header */
139 image[0x20c] = (actual_length >> 24) & 0xff;
140 image[0x20d] = (actual_length >> 16) & 0xff;
141 image[0x20e] = (actual_length >> 8) & 0xff;
142 image[0x20f] = actual_length & 0xff;
144 image[0x21c] = (actual_length >> 24) & 0xff;
145 image[0x21d] = (actual_length >> 16) & 0xff;
146 image[0x21e] = (actual_length >> 8) & 0xff;
147 image[0x21f] = actual_length & 0xff;
149 /* This is the length of the binary, including the ESTFBINR header and
150 rounded up to the nearest 0x200 boundary */
151 binary_length = (actual_length + 0x20 + 0x1ff) & 0xfffffe00;
153 /* The number of checksums, i.e number of 0x200 byte blocks */
154 num_chksums = binary_length / 0x200;
156 /* The total file length, including all headers and checksums */
157 total_length = binary_length + num_chksums + 0x200;
159 /* Patch the scrambler header with the new length info */
160 image[0] = total_length & 0xff;
161 image[1] = (total_length >> 8) & 0xff;
162 image[2] = (total_length >> 16) & 0xff;
163 image[3] = (total_length >> 24) & 0xff;
165 image[4] = binary_length & 0xff;
166 image[5] = (binary_length >> 8) & 0xff;
167 image[6] = (binary_length >> 16) & 0xff;
168 image[7] = (binary_length >> 24) & 0xff;
170 image[8] = num_chksums & 0xff;
171 image[9] = (num_chksums >> 8) & 0xff;
172 image[10] = (num_chksums >> 16) & 0xff;
173 image[11] = (num_chksums >> 24) & 0xff;
175 i = fwrite(image, 1, total_length, f);
176 if(i < total_length) {
177 perror(outfile);
178 fclose(f);
179 return -7;
182 printf("Wrote 0x%x bytes in %s\n", total_length, outfile);
184 fclose(f);
186 return 0;