1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 by Dave Chapman
12 * Based on mkboot, Copyright (C) 2005 by Linus Nielsen Feltzing
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
27 #include <sys/types.h>
31 #include "telechips.h"
35 Append a Rockbox bootloader to a Telechips original firmware file.
37 The first instruction in a TCC firmware file is always of the form:
41 where [pc, #xxx] is the entry point of the firmware - e.g. 0x20000020
43 mktccboot appends the Rockbox bootloader to the end of the original
44 firmware image and replaces the contents of [pc, #xxx] with the entry
45 point of our bootloader - i.e. the length of the original firmware plus
48 It then stores the original entry point from [pc, #xxx] in a fixed
49 offset in the Rockbox boootloader, which is used by the bootloader to
52 Finally, mktccboot corrects the length and CRCs in the main firmware
53 header, creating a new legal firmware file which can be installed on
58 /* win32 compatibility */
64 static void put_uint32le(uint32_t x
, unsigned char* p
)
67 p
[1] = (x
>> 8) & 0xff;
68 p
[2] = (x
>> 16) & 0xff;
69 p
[3] = (x
>> 24) & 0xff;
72 static uint32_t get_uint32le(unsigned char* p
)
74 return (p
[3] << 24) | (p
[2] << 16) | (p
[1]<<8) | p
[0];
79 printf("Usage: mktccboot <firmware file> <boot file> <output file>\n");
84 off_t
filesize(int fd
) {
87 if (fstat(fd
,&buf
) < 0) {
88 perror("[ERR] Checking filesize of input file");
96 int main(int argc
, char *argv
[])
98 char *infile
, *bootfile
, *outfile
;
99 int fdin
, fdboot
,fdout
;
101 int inlength
,bootlength
;
103 unsigned char* image
;
114 fdin
= open(infile
, O_RDONLY
|O_BINARY
);
120 fdboot
= open(bootfile
, O_RDONLY
|O_BINARY
);
126 inlength
= filesize(fdin
);
127 bootlength
= filesize(fdboot
);
129 image
= malloc(inlength
+ bootlength
);
133 printf("[ERR] Could not allocate memory, aborting\n");
137 n
= read(fdin
, image
, inlength
);
140 printf("[ERR] Could not read from %s\n",infile
);
144 n
= read(fdboot
, image
+ inlength
, bootlength
);
147 printf("[ERR] Could not read from %s\n",bootfile
);
151 ldr
= get_uint32le(image
);
153 /* TODO: Verify it's a LDR instruction */
154 origoffset
= (ldr
&0xfff) + 8;
156 printf("original firmware entry point: 0x%08x\n",get_uint32le(image
+ origoffset
));
157 printf("New entry point: 0x%08x\n",0x20000000 + inlength
+ 8);
159 /* Save the original firmware entry point at the start of the bootloader image */
160 put_uint32le(get_uint32le(image
+ origoffset
),image
+inlength
);
161 put_uint32le(0x20000000 + inlength
,image
+ inlength
+ 4);
163 /* Change the original firmware entry point to the third word in our bootloader */
164 put_uint32le(0x20000000 + inlength
+ 8,image
+origoffset
);
167 telechips_encode_crc(image
, inlength
+ bootlength
);
169 fdout
= open(outfile
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_BINARY
, 0644);
175 n
= write(fdout
, image
, inlength
+ bootlength
);
176 if (n
!= inlength
+ bootlength
)
178 printf("[ERR] Could not write output file %s\n",outfile
);