connect progress signal directly to the logger.
[Rockbox.git] / tools / mktccboot.c
blobfcfd80a6762951f6dbbed70dd71791f7321b09df
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Dave Chapman
12 * Based on mkboot, Copyright (C) 2005 by Linus Nielsen Feltzing
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <inttypes.h>
29 #include "telechips.h"
33 Append a Rockbox bootloader to a Telechips original firmware file.
35 The first instruction in a TCC firmware file is always of the form:
37 ldr pc, [pc, #xxx]
39 where [pc, #xxx] is the entry point of the firmware - e.g. 0x20000020
41 mktccboot appends the Rockbox bootloader to the end of the original
42 firmware image and replaces the contents of [pc, #xxx] with the entry
43 point of our bootloader - i.e. the length of the original firmware plus
44 0x20000000.
46 It then stores the original entry point from [pc, #xxx] in a fixed
47 offset in the Rockbox boootloader, which is used by the bootloader to
48 dual-boot.
50 Finally, mktccboot corrects the length and CRCs in the main firmware
51 header, creating a new legal firmware file which can be installed on
52 the device.
56 /* win32 compatibility */
58 #ifndef O_BINARY
59 #define O_BINARY 0
60 #endif
62 static void put_uint32le(uint32_t x, unsigned char* p)
64 p[0] = x & 0xff;
65 p[1] = (x >> 8) & 0xff;
66 p[2] = (x >> 16) & 0xff;
67 p[3] = (x >> 24) & 0xff;
70 static uint32_t get_uint32le(unsigned char* p)
72 return (p[3] << 24) | (p[2] << 16) | (p[1]<<8) | p[0];
75 void usage(void)
77 printf("Usage: mktccboot <firmware file> <boot file> <output file>\n");
79 exit(1);
82 off_t filesize(int fd) {
83 struct stat buf;
85 if (fstat(fd,&buf) < 0) {
86 perror("[ERR] Checking filesize of input file");
87 return -1;
88 } else {
89 return(buf.st_size);
94 int main(int argc, char *argv[])
96 char *infile, *bootfile, *outfile;
97 int fdin, fdboot,fdout;
98 int n;
99 int inlength,bootlength;
100 uint32_t ldr;
101 unsigned char* image;
102 int origoffset;
104 if(argc < 3) {
105 usage();
108 infile = argv[1];
109 bootfile = argv[2];
110 outfile = argv[3];
112 fdin = open(infile, O_RDONLY|O_BINARY);
113 if (fdin < 0)
115 perror(infile);
118 fdboot = open(bootfile, O_RDONLY|O_BINARY);
119 if (fdboot < 0)
121 perror(bootfile);
124 inlength = filesize(fdin);
125 bootlength = filesize(fdboot);
127 image = malloc(inlength + bootlength);
129 if (image==NULL)
131 printf("[ERR] Could not allocate memory, aborting\n");
132 return 1;
135 n = read(fdin, image, inlength);
136 if (n != inlength)
138 printf("[ERR] Could not read from %s\n",infile);
139 return 2;
142 n = read(fdboot, image + inlength, bootlength);
143 if (n != bootlength)
145 printf("[ERR] Could not read from %s\n",bootfile);
146 return 3;
149 ldr = get_uint32le(image);
151 /* TODO: Verify it's a LDR instruction */
152 origoffset = (ldr&0xfff) + 8;
154 printf("original firmware entry point: 0x%08x\n",get_uint32le(image + origoffset));
155 printf("New entry point: 0x%08x\n",0x20000000 + inlength + 8);
157 /* Save the original firmware entry point at the start of the bootloader image */
158 put_uint32le(get_uint32le(image + origoffset),image+inlength);
159 put_uint32le(0x20000000 + inlength,image + inlength + 4);
161 /* Change the original firmware entry point to the third word in our bootloader */
162 put_uint32le(0x20000000 + inlength + 8,image+origoffset);
165 telechips_encode_crc(image, inlength + bootlength);
167 fdout = open(outfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
168 if (fdout < 0)
170 perror(bootfile);
173 n = write(fdout, image, inlength + bootlength);
174 if (n != inlength + bootlength)
176 printf("[ERR] Could not write output file %s\n",outfile);
177 return 3;
180 close(fdin);
181 close(fdboot);
182 close(fdout);
184 return 0;