diff options
Diffstat (limited to 'rbutil/mktccboot/mktccboot.c')
-rw-r--r-- | rbutil/mktccboot/mktccboot.c | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/rbutil/mktccboot/mktccboot.c b/rbutil/mktccboot/mktccboot.c new file mode 100644 index 0000000000..e135b7e506 --- /dev/null +++ b/rbutil/mktccboot/mktccboot.c | |||
@@ -0,0 +1,206 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2007 by Dave Chapman | ||
11 | * | ||
12 | * Based on mkboot, Copyright (C) 2005 by Linus Nielsen Feltzing | ||
13 | * | ||
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. | ||
18 | * | ||
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
20 | * KIND, either express or implied. | ||
21 | * | ||
22 | ****************************************************************************/ | ||
23 | #include <stdio.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <string.h> | ||
26 | #include <unistd.h> | ||
27 | #include <sys/types.h> | ||
28 | #include <sys/stat.h> | ||
29 | #include <fcntl.h> | ||
30 | #include <inttypes.h> | ||
31 | #include "telechips.h" | ||
32 | |||
33 | /* | ||
34 | |||
35 | Append a Rockbox bootloader to a Telechips original firmware file. | ||
36 | |||
37 | The first instruction in a TCC firmware file is always of the form: | ||
38 | |||
39 | ldr pc, [pc, #xxx] | ||
40 | |||
41 | where [pc, #xxx] is the entry point of the firmware - e.g. 0x20000020 | ||
42 | |||
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 | ||
46 | 0x20000000. | ||
47 | |||
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 | ||
50 | dual-boot. | ||
51 | |||
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 | ||
54 | the device. | ||
55 | |||
56 | */ | ||
57 | |||
58 | /* win32 compatibility */ | ||
59 | |||
60 | #ifndef O_BINARY | ||
61 | #define O_BINARY 0 | ||
62 | #endif | ||
63 | |||
64 | static void put_uint32le(uint32_t x, unsigned char* p) | ||
65 | { | ||
66 | p[0] = x & 0xff; | ||
67 | p[1] = (x >> 8) & 0xff; | ||
68 | p[2] = (x >> 16) & 0xff; | ||
69 | p[3] = (x >> 24) & 0xff; | ||
70 | } | ||
71 | |||
72 | static uint32_t get_uint32le(unsigned char* p) | ||
73 | { | ||
74 | return (p[3] << 24) | (p[2] << 16) | (p[1]<<8) | p[0]; | ||
75 | } | ||
76 | |||
77 | void usage(void) | ||
78 | { | ||
79 | printf("Usage: mktccboot <firmware file> <boot file> <output file>\n"); | ||
80 | |||
81 | exit(1); | ||
82 | } | ||
83 | |||
84 | off_t filesize(int fd) { | ||
85 | struct stat buf; | ||
86 | |||
87 | if (fstat(fd,&buf) < 0) { | ||
88 | perror("[ERR] Checking filesize of input file"); | ||
89 | return -1; | ||
90 | } else { | ||
91 | return(buf.st_size); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | |||
96 | int main(int argc, char *argv[]) | ||
97 | { | ||
98 | char *infile, *bootfile, *outfile; | ||
99 | int fdin = -1, fdboot = -1, fdout = -1; | ||
100 | int n; | ||
101 | int inlength,bootlength; | ||
102 | uint32_t ldr; | ||
103 | unsigned char* image; | ||
104 | int origoffset; | ||
105 | int ret = 0; | ||
106 | |||
107 | if(argc < 3) { | ||
108 | usage(); | ||
109 | } | ||
110 | |||
111 | infile = argv[1]; | ||
112 | bootfile = argv[2]; | ||
113 | outfile = argv[3]; | ||
114 | |||
115 | fdin = open(infile, O_RDONLY|O_BINARY); | ||
116 | if (fdin < 0) | ||
117 | { | ||
118 | perror(infile); | ||
119 | ret = 1; | ||
120 | goto error_exit; | ||
121 | } | ||
122 | |||
123 | fdboot = open(bootfile, O_RDONLY|O_BINARY); | ||
124 | if (fdboot < 0) | ||
125 | { | ||
126 | perror(bootfile); | ||
127 | ret = 2; | ||
128 | goto error_exit; | ||
129 | } | ||
130 | |||
131 | inlength = filesize(fdin); | ||
132 | bootlength = filesize(fdboot); | ||
133 | |||
134 | image = malloc(inlength + bootlength); | ||
135 | |||
136 | if (image==NULL) | ||
137 | { | ||
138 | printf("[ERR] Could not allocate memory, aborting\n"); | ||
139 | ret = 3; | ||
140 | goto error_exit; | ||
141 | } | ||
142 | |||
143 | n = read(fdin, image, inlength); | ||
144 | if (n != inlength) | ||
145 | { | ||
146 | printf("[ERR] Could not read from %s\n",infile); | ||
147 | ret = 4; | ||
148 | goto error_exit; | ||
149 | } | ||
150 | |||
151 | n = read(fdboot, image + inlength, bootlength); | ||
152 | if (n != bootlength) | ||
153 | { | ||
154 | printf("[ERR] Could not read from %s\n",bootfile); | ||
155 | ret = 5; | ||
156 | goto error_exit; | ||
157 | } | ||
158 | |||
159 | ldr = get_uint32le(image); | ||
160 | |||
161 | /* TODO: Verify it's a LDR instruction */ | ||
162 | origoffset = (ldr&0xfff) + 8; | ||
163 | |||
164 | printf("original firmware entry point: 0x%08x\n", | ||
165 | (unsigned int) get_uint32le(image + origoffset)); | ||
166 | printf("New entry point: 0x%08x\n",0x20000000 + inlength + 8); | ||
167 | |||
168 | /* Save the original firmware entry point at the start of the bootloader image */ | ||
169 | put_uint32le(get_uint32le(image + origoffset),image+inlength); | ||
170 | put_uint32le(0x20000000 + inlength,image + inlength + 4); | ||
171 | |||
172 | /* Change the original firmware entry point to the third word in our bootloader */ | ||
173 | put_uint32le(0x20000000 + inlength + 8,image+origoffset); | ||
174 | |||
175 | |||
176 | telechips_encode_crc(image, inlength + bootlength); | ||
177 | |||
178 | fdout = open(outfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644); | ||
179 | if (fdout < 0) | ||
180 | { | ||
181 | perror(bootfile); | ||
182 | ret = 6; | ||
183 | goto error_exit; | ||
184 | } | ||
185 | |||
186 | n = write(fdout, image, inlength + bootlength); | ||
187 | if (n != inlength + bootlength) | ||
188 | { | ||
189 | printf("[ERR] Could not write output file %s\n",outfile); | ||
190 | ret = 7; | ||
191 | goto error_exit; | ||
192 | } | ||
193 | |||
194 | error_exit: | ||
195 | |||
196 | if (fdin >= 0) | ||
197 | close(fdin); | ||
198 | |||
199 | if (fdboot >= 0) | ||
200 | close(fdboot); | ||
201 | |||
202 | if (fdout >= 0) | ||
203 | close(fdout); | ||
204 | |||
205 | return ret; | ||
206 | } | ||