diff options
author | Tomer Shalev <shalev.tomer@gmail.com> | 2009-10-29 21:31:50 +0000 |
---|---|---|
committer | Tomer Shalev <shalev.tomer@gmail.com> | 2009-10-29 21:31:50 +0000 |
commit | ad785518111eabe4518dca485421e533186f63a5 (patch) | |
tree | 1933da66814ac5bb19b7b7f87bc2ab56ea27ed1d /rbutil/mktccboot | |
parent | 0f90f0b423674494a648a0bb8fd2cfe31f29b68b (diff) | |
download | rockbox-ad785518111eabe4518dca485421e533186f63a5.tar.gz rockbox-ad785518111eabe4518dca485421e533186f63a5.zip |
FS#10728 - Cowon D2: Add support for D2 in rbutil
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23410 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'rbutil/mktccboot')
-rw-r--r-- | rbutil/mktccboot/Makefile | 108 | ||||
-rw-r--r-- | rbutil/mktccboot/README | 35 | ||||
-rw-r--r-- | rbutil/mktccboot/mktccboot.c | 174 | ||||
-rw-r--r-- | rbutil/mktccboot/mktccboot.h | 42 |
4 files changed, 294 insertions, 65 deletions
diff --git a/rbutil/mktccboot/Makefile b/rbutil/mktccboot/Makefile index f3c0409b2a..9399a0c09f 100644 --- a/rbutil/mktccboot/Makefile +++ b/rbutil/mktccboot/Makefile | |||
@@ -6,16 +6,112 @@ | |||
6 | # \/ \/ \/ \/ \/ | 6 | # \/ \/ \/ \/ \/ |
7 | # $Id$ | 7 | # $Id$ |
8 | # | 8 | # |
9 | |||
10 | # We use the Telechips code available in the Rockbox tools/ directory | ||
9 | TOOLSDIR=../../tools | 11 | TOOLSDIR=../../tools |
10 | CFLAGS := -O -g -W -Wall -Wshadow -pedantic -I$(TOOLSDIR) | 12 | CFLAGS := -O -g -W -Wall -Wshadow -pedantic -I$(TOOLSDIR) |
11 | 13 | ||
12 | all: mktccboot | 14 | PLAT=tcc |
15 | |||
16 | CC = gcc | ||
17 | MKPLATBOOT=mk$(PLAT)boot | ||
18 | LIB_MKPLATBOOT=lib$(MKPLATBOOT) | ||
19 | |||
20 | #change for releases | ||
21 | ifndef APPVERSION | ||
22 | APPVERSION=`$(TOOLSDIR)/version.sh` | ||
23 | endif | ||
24 | |||
25 | ifndef V | ||
26 | SILENT = @ | ||
27 | endif | ||
28 | |||
29 | ifeq ($(findstring CYGWIN,$(shell uname)),CYGWIN) | ||
30 | OUTPUT=$(MKPLATBOOT).exe | ||
31 | CFLAGS+=-mno-cygwin | ||
32 | else | ||
33 | ifeq ($(findstring MINGW,$(shell uname)),MINGW) | ||
34 | OUTPUT=$(MKPLATBOOT).exe | ||
35 | else | ||
36 | ifeq ($(findstring mingw,$(CC)),mingw) | ||
37 | OUTPUT=$(MKPLATBOOT).exe | ||
38 | else | ||
39 | OUTPUT=$(MKPLATBOOT) | ||
40 | endif | ||
41 | endif | ||
42 | endif | ||
43 | |||
44 | ifdef RBARCH | ||
45 | CFLAGS += -arch $(RBARCH) | ||
46 | endif | ||
47 | |||
48 | OUT = $(TARGET_DIR)build$(RBARCH) | ||
49 | |||
50 | MKPLATBOOT_OBJ=$(OUT)/$(MKPLATBOOT).o | ||
51 | LIBMKPLATBOOT_OBJ=$(OUT)/$(LIB_MKPLATBOOT).o | ||
52 | |||
53 | all: $(OUTPUT) | ||
54 | |||
55 | # Dependant modules | ||
56 | TELECHIPS=$(TOOLSDIR)/telechips | ||
57 | TELECHIPS_OBJ=$(TELECHIPS).o | ||
13 | 58 | ||
14 | telechips.o: $(TOOLSDIR)/telechips.c $(TOOLSDIR)/telechips.h | 59 | $(TELECHIPS_OBJ): $(TELECHIPS).[ch] |
15 | $(SILENT)$(CC) $(CFLAGS) $(TOOLSDIR)/telechips.c -c -o $@ | 60 | make -C $(TOOLSDIR) $(TARGET_DIR)telechips.o |
16 | 61 | ||
17 | mktccboot: mktccboot.c telechips.o | 62 | DEPENDANT_OBJS=$(TELECHIPS_OBJ) |
18 | $(SILENT)$(CC) $(CFLAGS) $+ -o $@ | ||
19 | 63 | ||
64 | $(MKPLATBOOT_OBJ): $(MKPLATBOOT).[ch] $(DEPENDANT_OBJS) | ||
65 | @echo CC $< | ||
66 | $(SILENT)$(CC) $(CFLAGS) -c -o $(MKPLATBOOT_OBJ) -W -Wall $(MKPLATBOOT).c -DVERSION=\"$(APPVERSION)\" | ||
67 | |||
68 | $(OUTPUT): $(OUT) $(MKPLATBOOT_OBJ) | ||
69 | @echo CC $< | ||
70 | $(SILENT)$(CC) $(CFLAGS) -o $(OUTPUT) $(MKPLATBOOT_OBJ) $(DEPENDANT_OBJS) | ||
71 | |||
72 | $(LIBMKPLATBOOT_OBJ): $(MKPLATBOOT_OBJ) | ||
73 | @echo CC $< | ||
74 | $(SILENT)$(CC) $(CFLAGS) -DLIB -c -o $(LIBMKPLATBOOT_OBJ) -W -Wall $(MKPLATBOOT).c | ||
75 | |||
76 | $(LIB_MKPLATBOOT)$(RBARCH).a: $(OUT) $(LIBMKPLATBOOT_OBJ) | ||
77 | @echo AR $@ | ||
78 | $(SILENT)$(AR) ruc $(TARGET_DIR)$(LIB_MKPLATBOOT)$(RBARCH).a $(LIBMKPLATBOOT_OBJ) | ||
79 | |||
80 | # some trickery to build ppc and i386 from a single call | ||
81 | ifeq ($(RBARCH),) | ||
82 | $(LIB_MKPLATBOOT)i386.a: | ||
83 | make RBARCH=i386 TARGET_DIR=$(TARGET_DIR) $(LIB_MKPLATBOOT)i386.a | ||
84 | |||
85 | $(LIB_MKPLATBOOT)ppc.a: | ||
86 | make RBARCH=ppc TARGET_DIR=$(TARGET_DIR) $(LIB_MKPLATBOOT)ppc.a | ||
87 | endif | ||
88 | |||
89 | $(LIB_MKPLATBOOT)-universal: $(LIB_MKPLATBOOT)i386.a $(LIB_MKPLATBOOT)ppc.a | ||
90 | @echo lipo $(TARGET_DIR)$(LIB_MKPLATBOOT).a | ||
91 | $(SILENT) rm -f $(TARGET_DIR)$(LIB_MKPLATBOOT).a | ||
92 | lipo -create $(TARGET_DIR)$(LIB_MKPLATBOOT)ppc.a $(TARGET_DIR)$(LIB_MKPLATBOOT)i386.a -output $(TARGET_DIR)$(LIB_MKPLATBOOT).a | ||
93 | |||
20 | clean: | 94 | clean: |
21 | rm -f telechips.o mktccboot | 95 | rm -f $(OUTPUT) $(LIB_MKPLATBOOT).o $(TARGET_DIR)$(LIB_MKPLATBOOT)*.a $(MKPLATBOOT).dmg |
96 | rm -f $(DEPENDANT_OBJS) | ||
97 | rm -rf build* $(MKPLATBOOT)-* | ||
98 | |||
99 | $(MKPLATBOOT)-i386: | ||
100 | $(MAKE) RBARCH=i386 | ||
101 | mv $(MKPLATBOOT) $(MKPLATBOOT)-i386 | ||
102 | |||
103 | $(MKPLATBOOT)-ppc: | ||
104 | make RBARCH=ppc | ||
105 | mv $(MKPLATBOOT) $(MKPLATBOOT)-ppc | ||
106 | |||
107 | $(MKPLATBOOT)-mac: $(MKPLATBOOT)-i386 $(MKPLATBOOT)-ppc | ||
108 | lipo -create $(MKPLATBOOT)-ppc $(MKPLATBOOT)-i386 -output $(MKPLATBOOT)-mac | ||
109 | |||
110 | $(MKPLATBOOT).dmg: $(MKPLATBOOT)-mac | ||
111 | mkdir -p $(MKPLATBOOT)-dmg | ||
112 | cp -p $(MKPLATBOOT)-mac $(MKPLATBOOT)-dmg | ||
113 | hdiutil create -srcfolder $(MKPLATBOOT)-dmg $(MKPLATBOOT).dmg | ||
114 | |||
115 | $(OUT): | ||
116 | @echo MKDIR $(OUT) | ||
117 | $(SILENT)mkdir $(OUT) | ||
diff --git a/rbutil/mktccboot/README b/rbutil/mktccboot/README new file mode 100644 index 0000000000..719846f2e8 --- /dev/null +++ b/rbutil/mktccboot/README | |||
@@ -0,0 +1,35 @@ | |||
1 | mktccboot | ||
2 | --------- | ||
3 | |||
4 | A tool to inject a bootloader into a Telechips 77X/78X firmware file. | ||
5 | |||
6 | Usage | ||
7 | ----- | ||
8 | |||
9 | mktccboot <firmware file> <boot file> <output file> | ||
10 | |||
11 | <firmware file> is an original Telechips firmware file. | ||
12 | |||
13 | <boot file> is the code you want to execute (a rockbox bootloader), previously | ||
14 | scrambled with tools/scramble utility. | ||
15 | |||
16 | <output file> is the resulting firmware file which you'll have to copy on your | ||
17 | player. See "Firmware filenames". | ||
18 | |||
19 | Dual-Boot | ||
20 | --------- | ||
21 | |||
22 | The purpose of this program is to provide dual-boot between the original | ||
23 | firmware and the new (rockbox) firmware. | ||
24 | |||
25 | By default the player will boot into the new firmware. | ||
26 | |||
27 | To boot into the Original Firmware, you need to press the <???> key. | ||
28 | |||
29 | The player will boot into the Original Firmware as well if it is powered up by | ||
30 | inserting an usb cable. | ||
31 | |||
32 | Hacking | ||
33 | ------- | ||
34 | |||
35 | See comments in mktccboot.c for more information. | ||
diff --git a/rbutil/mktccboot/mktccboot.c b/rbutil/mktccboot/mktccboot.c index e135b7e506..2c6b08890e 100644 --- a/rbutil/mktccboot/mktccboot.c +++ b/rbutil/mktccboot/mktccboot.c | |||
@@ -81,7 +81,7 @@ void usage(void) | |||
81 | exit(1); | 81 | exit(1); |
82 | } | 82 | } |
83 | 83 | ||
84 | off_t filesize(int fd) { | 84 | static off_t filesize(int fd) { |
85 | struct stat buf; | 85 | struct stat buf; |
86 | 86 | ||
87 | if (fstat(fd,&buf) < 0) { | 87 | if (fstat(fd,&buf) < 0) { |
@@ -92,16 +92,97 @@ off_t filesize(int fd) { | |||
92 | } | 92 | } |
93 | } | 93 | } |
94 | 94 | ||
95 | #define DRAMORIG 0x20000000 | ||
96 | /* Injects a bootloader into a Telechips 77X/78X firmware file */ | ||
97 | unsigned char *patch_firmware_tcc(unsigned char *of_buf, int of_size, | ||
98 | unsigned char *boot_buf, int boot_size, int *patched_size) | ||
99 | { | ||
100 | unsigned char *patched_buf; | ||
101 | uint32_t ldr, old_ep_offset, new_ep_offset; | ||
102 | int of_offset; | ||
103 | |||
104 | patched_buf = malloc(of_size + boot_size); | ||
105 | if (!patched_buf) | ||
106 | return NULL; | ||
107 | |||
108 | memcpy(patched_buf, of_buf, of_size); | ||
109 | memcpy(patched_buf + of_size, boot_buf, boot_size); | ||
110 | |||
111 | ldr = get_uint32le(patched_buf); | ||
112 | |||
113 | /* TODO: Verify it's a LDR instruction */ | ||
114 | of_offset = (ldr & 0xfff) + 8; | ||
115 | old_ep_offset = get_uint32le(patched_buf + of_offset); | ||
116 | new_ep_offset = DRAMORIG + of_size; | ||
117 | |||
118 | printf("OF entry point: 0x%08x\n", old_ep_offset); | ||
119 | printf("New entry point: 0x%08x\n", new_ep_offset + 8); | ||
120 | |||
121 | /* Save the OF entry point at the start of the bootloader image */ | ||
122 | put_uint32le(old_ep_offset, patched_buf + of_size); | ||
123 | put_uint32le(new_ep_offset, patched_buf + of_size + 4); | ||
124 | |||
125 | /* Change the OF entry point to the third word in our bootloader */ | ||
126 | put_uint32le(new_ep_offset + 8, patched_buf + of_offset); | ||
127 | |||
128 | telechips_encode_crc(patched_buf, of_size + boot_size); | ||
129 | *patched_size = of_size + boot_size; | ||
130 | |||
131 | return patched_buf; | ||
132 | } | ||
133 | |||
134 | unsigned char *file_read(char *filename, int *size) | ||
135 | { | ||
136 | unsigned char *buf = NULL; | ||
137 | int n, fd = -1; | ||
138 | |||
139 | /* Open file for reading */ | ||
140 | fd = open(filename, O_RDONLY|O_BINARY); | ||
141 | if (fd < 0) | ||
142 | { | ||
143 | printf("[ERR] Could open file for reading, aborting\n"); | ||
144 | perror(filename); | ||
145 | goto error; | ||
146 | } | ||
147 | |||
148 | /* Get file size, and allocate a buffer of that size */ | ||
149 | *size = filesize(fd); | ||
150 | buf = malloc(*size); | ||
151 | if (buf == NULL) | ||
152 | { | ||
153 | printf("[ERR] Could not allocate memory, aborting\n"); | ||
154 | goto error; | ||
155 | } | ||
156 | |||
157 | /* Read the file's content to the buffer */ | ||
158 | n = read(fd, buf, *size); | ||
159 | if (n != *size) | ||
160 | { | ||
161 | printf("[ERR] Could not read from %s\n", filename); | ||
162 | goto error; | ||
163 | } | ||
164 | |||
165 | return buf; | ||
95 | 166 | ||
167 | error: | ||
168 | if (fd >= 0) | ||
169 | close(fd); | ||
170 | |||
171 | if (buf) | ||
172 | free(buf); | ||
173 | |||
174 | return NULL; | ||
175 | } | ||
176 | |||
177 | #ifndef LIB | ||
96 | int main(int argc, char *argv[]) | 178 | int main(int argc, char *argv[]) |
97 | { | 179 | { |
98 | char *infile, *bootfile, *outfile; | 180 | char *infile, *bootfile, *outfile; |
99 | int fdin = -1, fdboot = -1, fdout = -1; | 181 | int fdout = -1; |
100 | int n; | 182 | int n, of_size, boot_size, patched_size; |
101 | int inlength,bootlength; | 183 | unsigned char *of_buf; |
102 | uint32_t ldr; | 184 | unsigned char *boot_buf = NULL; |
103 | unsigned char* image; | 185 | unsigned char* image = NULL; |
104 | int origoffset; | ||
105 | int ret = 0; | 186 | int ret = 0; |
106 | 187 | ||
107 | if(argc < 3) { | 188 | if(argc < 3) { |
@@ -112,79 +193,50 @@ int main(int argc, char *argv[]) | |||
112 | bootfile = argv[2]; | 193 | bootfile = argv[2]; |
113 | outfile = argv[3]; | 194 | outfile = argv[3]; |
114 | 195 | ||
115 | fdin = open(infile, O_RDONLY|O_BINARY); | 196 | /* Read OF and boot files */ |
116 | if (fdin < 0) | 197 | of_buf = file_read(infile, &of_size); |
198 | if (!of_buf) | ||
117 | { | 199 | { |
118 | perror(infile); | ||
119 | ret = 1; | 200 | ret = 1; |
120 | goto error_exit; | 201 | goto error_exit; |
121 | } | 202 | } |
122 | 203 | ||
123 | fdboot = open(bootfile, O_RDONLY|O_BINARY); | 204 | boot_buf = file_read(bootfile, &boot_size); |
124 | if (fdboot < 0) | 205 | if (!boot_buf) |
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 | { | 206 | { |
138 | printf("[ERR] Could not allocate memory, aborting\n"); | ||
139 | ret = 3; | 207 | ret = 3; |
140 | goto error_exit; | 208 | goto error_exit; |
141 | } | 209 | } |
142 | 210 | ||
143 | n = read(fdin, image, inlength); | 211 | /* Allocate buffer for patched firmware */ |
144 | if (n != inlength) | 212 | image = malloc(of_size + boot_size); |
213 | if (image == NULL) | ||
145 | { | 214 | { |
146 | printf("[ERR] Could not read from %s\n",infile); | 215 | printf("[ERR] Could not allocate memory, aborting\n"); |
147 | ret = 4; | 216 | ret = 4; |
148 | goto error_exit; | 217 | goto error_exit; |
149 | } | 218 | } |
150 | 219 | ||
151 | n = read(fdboot, image + inlength, bootlength); | 220 | /* Create the patched firmware */ |
152 | if (n != bootlength) | 221 | image = patch_firmware_tcc(of_buf, of_size, boot_buf, boot_size, |
222 | &patched_size); | ||
223 | if (!image) | ||
153 | { | 224 | { |
154 | printf("[ERR] Could not read from %s\n",bootfile); | 225 | printf("[ERR] Error creating patched firmware, aborting\n"); |
155 | ret = 5; | 226 | ret = 5; |
156 | goto error_exit; | 227 | goto error_exit; |
157 | } | 228 | } |
158 | 229 | ||
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); | 230 | fdout = open(outfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644); |
179 | if (fdout < 0) | 231 | if (fdout < 0) |
180 | { | 232 | { |
181 | perror(bootfile); | 233 | perror(outfile); |
182 | ret = 6; | 234 | ret = 6; |
183 | goto error_exit; | 235 | goto error_exit; |
184 | } | 236 | } |
185 | 237 | ||
186 | n = write(fdout, image, inlength + bootlength); | 238 | n = write(fdout, image, patched_size); |
187 | if (n != inlength + bootlength) | 239 | if (n != patched_size) |
188 | { | 240 | { |
189 | printf("[ERR] Could not write output file %s\n",outfile); | 241 | printf("[ERR] Could not write output file %s\n",outfile); |
190 | ret = 7; | 242 | ret = 7; |
@@ -193,14 +245,18 @@ int main(int argc, char *argv[]) | |||
193 | 245 | ||
194 | error_exit: | 246 | error_exit: |
195 | 247 | ||
196 | if (fdin >= 0) | ||
197 | close(fdin); | ||
198 | |||
199 | if (fdboot >= 0) | ||
200 | close(fdboot); | ||
201 | |||
202 | if (fdout >= 0) | 248 | if (fdout >= 0) |
203 | close(fdout); | 249 | close(fdout); |
204 | 250 | ||
251 | if (of_buf) | ||
252 | free(of_buf); | ||
253 | |||
254 | if (boot_buf) | ||
255 | free(boot_buf); | ||
256 | |||
257 | if (image) | ||
258 | free(image); | ||
259 | |||
205 | return ret; | 260 | return ret; |
206 | } | 261 | } |
262 | #endif | ||
diff --git a/rbutil/mktccboot/mktccboot.h b/rbutil/mktccboot/mktccboot.h new file mode 100644 index 0000000000..6c6410c258 --- /dev/null +++ b/rbutil/mktccboot/mktccboot.h | |||
@@ -0,0 +1,42 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * mktccboot.h - a tool to inject a bootloader into a Telechips 77X/78X firmware | ||
11 | * file. | ||
12 | * | ||
13 | * Copyright (C) 2009 Tomer Shalev | ||
14 | * | ||
15 | * This program is free software; you can redistribute it and/or | ||
16 | * modify it under the terms of the GNU General Public License | ||
17 | * as published by the Free Software Foundation; either version 2 | ||
18 | * of the License, or (at your option) any later version. | ||
19 | * | ||
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
21 | * KIND, either express or implied. | ||
22 | * | ||
23 | ****************************************************************************/ | ||
24 | |||
25 | #ifndef _MKTCCBOOT_H_ | ||
26 | #define _MKTCCBOOT_H_ | ||
27 | |||
28 | #ifdef __cplusplus | ||
29 | extern "C" { | ||
30 | #endif | ||
31 | |||
32 | /* Injects a bootloader into a Telechips 77X/78X firmware file */ | ||
33 | unsigned char *patch_firmware_tcc(unsigned char *of_buf, int of_size, | ||
34 | unsigned char *boot_buf, int boot_size, int *patched_size); | ||
35 | |||
36 | unsigned char *file_read(char *filename, int *size); | ||
37 | |||
38 | #ifdef __cplusplus | ||
39 | }; | ||
40 | #endif | ||
41 | |||
42 | #endif | ||