summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/mknkboot.c2
-rw-r--r--utils/MTP/beastpatcher/Makefile15
-rw-r--r--utils/MTP/beastpatcher/beastpatcher.c94
-rw-r--r--utils/MTP/beastpatcher/beastpatcher.h2
-rw-r--r--utils/MTP/beastpatcher/main.c33
5 files changed, 102 insertions, 44 deletions
diff --git a/tools/mknkboot.c b/tools/mknkboot.c
index f17d1e9070..e63840427e 100644
--- a/tools/mknkboot.c
+++ b/tools/mknkboot.c
@@ -124,6 +124,7 @@ static void put_uint32le(uint32_t x, unsigned char* p)
124 p[3] = (x >> 24) & 0xff; 124 p[3] = (x >> 24) & 0xff;
125} 125}
126 126
127#if !defined(BEASTPATCHER)
127static off_t filesize(int fd) { 128static off_t filesize(int fd) {
128 struct stat buf; 129 struct stat buf;
129 130
@@ -134,6 +135,7 @@ static off_t filesize(int fd) {
134 return(buf.st_size); 135 return(buf.st_size);
135 } 136 }
136} 137}
138#endif
137 139
138 140
139int mknkboot(const struct filebuf *indata, const struct filebuf *bootdata, 141int mknkboot(const struct filebuf *indata, const struct filebuf *bootdata,
diff --git a/utils/MTP/beastpatcher/Makefile b/utils/MTP/beastpatcher/Makefile
index 52887f06b2..571274034b 100644
--- a/utils/MTP/beastpatcher/Makefile
+++ b/utils/MTP/beastpatcher/Makefile
@@ -1,4 +1,4 @@
1CFLAGS=-Wall -W -DWITH_BOOTOBJS 1CFLAGS=-Wall -W -DWITH_BOOTOBJS -DBEASTPATCHER -I../../../tools
2 2
3ifeq ($(findstring CYGWIN,$(shell uname)),CYGWIN) 3ifeq ($(findstring CYGWIN,$(shell uname)),CYGWIN)
4OUTPUT=beastpatcher.exe 4OUTPUT=beastpatcher.exe
@@ -30,12 +30,17 @@ CC = $(CROSS)gcc
30 30
31all: $(OUTPUT) 31all: $(OUTPUT)
32 32
33beastpatcher: beastpatcher.c bootimg.c mtp_common.h mtp_libmtp.c main.c 33SOURCES = beastpatcher.c bootimg.c ../../../tools/mknkboot.c main.c
34 gcc $(CFLAGS) -o beastpatcher beastpatcher.c bootimg.c mtp_libmtp.c main.c $(LIBS) 34HEADERS = beastpatcher.h mtp_common.h bootimg.h ../../../tools/mknkboot.h
35MTPSRCS_W32 = mtp_win32.c
36MTPSRCS_MTP = mtp_libmtp.c
37
38beastpatcher: $(SOURCES) $(HEADERS) $(MTPSRCS_MTP)
39 gcc $(CFLAGS) -o beastpatcher $(SOURCES) $(MTPSRCS_MTP) $(LIBS)
35 strip beastpatcher 40 strip beastpatcher
36 41
37beastpatcher.exe: beastpatcher.c bootimg.c mtp_common.h mtp_win32.c main.c $(WINLIBS) 42beastpatcher.exe: $(SOURCES) $(HEADERS) $(MTPSRCS_W32) $(WINLIBS)
38 $(CC) $(CFLAGS) -o beastpatcher.exe beastpatcher.c bootimg.c mtp_win32.c main.c $(WINLIBS) 43 $(CC) $(CFLAGS) -o beastpatcher.exe $(SOURCES) $(MTPSRCS_W32) $(WINLIBS)
39 $(CROSS)strip beastpatcher.exe 44 $(CROSS)strip beastpatcher.exe
40 45
41beastpatcher-mac: beastpatcher-i386 beastpatcher-ppc 46beastpatcher-mac: beastpatcher-i386 beastpatcher-ppc
diff --git a/utils/MTP/beastpatcher/beastpatcher.c b/utils/MTP/beastpatcher/beastpatcher.c
index bd49e87f6d..9d216d37bf 100644
--- a/utils/MTP/beastpatcher/beastpatcher.c
+++ b/utils/MTP/beastpatcher/beastpatcher.c
@@ -54,6 +54,7 @@
54 54
55#include "mtp_common.h" 55#include "mtp_common.h"
56#include "bootimg.h" 56#include "bootimg.h"
57#include "mknkboot.h"
57 58
58/* Code to create a single-boot bootloader. 59/* Code to create a single-boot bootloader.
59 Based on tools/gigabeats.c by Will Robertson. 60 Based on tools/gigabeats.c by Will Robertson.
@@ -79,7 +80,7 @@ static uint32_t calc_csum(const unsigned char* pb, int cb)
79} 80}
80 81
81static void create_single_boot(unsigned char* boot, int bootlen, 82static void create_single_boot(unsigned char* boot, int bootlen,
82 unsigned char** fwbuf, int* fwsize) 83 unsigned char** fwbuf, off_t* fwsize)
83{ 84{
84 unsigned char* buf; 85 unsigned char* buf;
85 86
@@ -127,37 +128,53 @@ static void create_single_boot(unsigned char* boot, int bootlen,
127 return; 128 return;
128} 129}
129 130
130int beastpatcher(const char* bootfile) 131static int readfile(const char* filename, struct filebuf *buf)
132{
133 int res;
134 FILE* fp;
135 size_t bread;
136#ifdef _LARGEFILE64_SOURCE
137 struct stat64 sb;
138 res = stat64(filename, &sb);
139#else
140 struct stat sb;
141 res = stat(filename, &sb);
142#endif
143 if(res == -1) {
144 fprintf(stderr, "[ERR] Getting firmware file size failed!\n");
145 return 1;
146 }
147 buf->len = sb.st_size;
148 buf->buf = (unsigned char*)malloc(buf->len);
149 /* load firmware binary to memory. */
150 fp = fopen(filename, "rb");
151 bread = fread(buf->buf, sizeof(unsigned char), buf->len, fp);
152 if((off_t)(bread * sizeof(unsigned char)) != buf->len) {
153 fprintf(stderr, "[ERR] Error reading file %s!\n", filename);
154 return 1;
155 }
156 fclose(fp);
157 return 0;
158}
159
160
161int beastpatcher(const char* bootfile, const char* firmfile)
131{ 162{
132 char yesno[4]; 163 char yesno[4];
133 unsigned char* fwbuf;
134 int fwsize;
135 struct mtp_info_t mtp_info; 164 struct mtp_info_t mtp_info;
136 unsigned char* bootloader = bootimg; 165 struct filebuf bootloader;
137 unsigned int len_bootloader = LEN_bootimg; 166 struct filebuf firmware;
167 struct filebuf fw;
168 bootloader.buf = bootimg;
169 bootloader.len = LEN_bootimg;
138 170
139 if (bootfile) { 171 if (bootfile) {
140 int res; 172 if(readfile(bootfile, &bootloader) != 0) {
141 FILE* fp;
142 size_t bread;
143#ifdef _LARGEFILE64_SOURCE
144 struct stat64 sb;
145 res = stat64(bootfile, &sb);
146#else
147 struct stat sb;
148 res = stat(bootfile, &sb);
149#endif
150 if(res == -1) {
151 fprintf(stderr, "[ERR] Getting bootloader file size failed!\n");
152 return 1; 173 return 1;
153 } 174 }
154 len_bootloader = sb.st_size; 175 }
155 bootloader = (unsigned char*)malloc(len_bootloader); 176 if (firmfile) {
156 /* load bootloader binary to memory. */ 177 if(readfile(firmfile, &firmware) != 0) {
157 fp = fopen(bootfile, "rb");
158 bread = fread(bootloader, sizeof(unsigned char), len_bootloader, fp);
159 if(bread * sizeof(unsigned char) != len_bootloader) {
160 fprintf(stderr, "[ERR] Error reading firmware file!\n");
161 return 1; 178 return 1;
162 } 179 }
163 } 180 }
@@ -178,20 +195,30 @@ int beastpatcher(const char* bootfile)
178 mtp_info.modelname); 195 mtp_info.modelname);
179 printf("[INFO] Device version: \"%s\"\n",mtp_info.version); 196 printf("[INFO] Device version: \"%s\"\n",mtp_info.version);
180 197
181 198 if(firmfile) {
182 printf("\nEnter i to install the Rockbox bootloader or c to cancel and do nothing (i/c): "); 199 printf("\nEnter i to install the Rockbox dualboot bootloader or c to cancel and do nothing (i/c): ");
200 }
201 else {
202 printf("\nEnter i to install the Rockbox bootloader or c to cancel and do nothing (i/c): ");
203 }
183 204
184 if (fgets(yesno,4,stdin)) 205 if (fgets(yesno,4,stdin))
185 { 206 {
186 if (yesno[0]=='i') 207 if (yesno[0]=='i')
187 { 208 {
209 if(firmfile) {
210 /* if a firmware file is given create a dualboot image. */
211 mknkboot(&firmware, &bootloader, &fw);
212 }
213 else {
188 /* Create a single-boot bootloader from the embedded bootloader */ 214 /* Create a single-boot bootloader from the embedded bootloader */
189 create_single_boot(bootloader, len_bootloader, &fwbuf, &fwsize); 215 create_single_boot(bootloader.buf, bootloader.len, &fw.buf, &fw.len);
216 }
190 217
191 if (fwbuf == NULL) 218 if (fw.buf == NULL)
192 return 1; 219 return 1;
193 220
194 if (mtp_send_firmware(&mtp_info, fwbuf, fwsize) == 0) 221 if (mtp_send_firmware(&mtp_info, fw.buf, fw.len) == 0)
195 { 222 {
196 fprintf(stderr,"[INFO] Bootloader installed successfully.\n"); 223 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
197 } 224 }
@@ -201,7 +228,7 @@ int beastpatcher(const char* bootfile)
201 } 228 }
202 229
203 /* We are now done with the firmware image */ 230 /* We are now done with the firmware image */
204 free(fwbuf); 231 free(fw.buf);
205 } 232 }
206 else 233 else
207 { 234 {
@@ -209,7 +236,10 @@ int beastpatcher(const char* bootfile)
209 } 236 }
210 } 237 }
211 if(bootfile) { 238 if(bootfile) {
212 free(bootloader); 239 free(bootloader.buf);
240 }
241 if(firmfile) {
242 free(firmware.buf);
213 } 243 }
214 244
215 mtp_finished(&mtp_info); 245 mtp_finished(&mtp_info);
diff --git a/utils/MTP/beastpatcher/beastpatcher.h b/utils/MTP/beastpatcher/beastpatcher.h
index 899393272f..79246dbb17 100644
--- a/utils/MTP/beastpatcher/beastpatcher.h
+++ b/utils/MTP/beastpatcher/beastpatcher.h
@@ -40,7 +40,7 @@
40#ifndef BEASTPATCHER_H 40#ifndef BEASTPATCHER_H
41#define BEASTPATCHER_H 41#define BEASTPATCHER_H
42 42
43int beastpatcher(const char* bootfile); 43int beastpatcher(const char* bootfile, const char* firmfile);
44int sendfirm(const char* filename); 44int sendfirm(const char* filename);
45 45
46#endif 46#endif
diff --git a/utils/MTP/beastpatcher/main.c b/utils/MTP/beastpatcher/main.c
index 873dad5c7f..315f78b264 100644
--- a/utils/MTP/beastpatcher/main.c
+++ b/utils/MTP/beastpatcher/main.c
@@ -56,6 +56,7 @@
56enum actions { 56enum actions {
57 NONE, 57 NONE,
58 INSTALL, 58 INSTALL,
59 DUALBOOT,
59 SEND, 60 SEND,
60 HELP 61 HELP
61}; 62};
@@ -66,11 +67,13 @@ static void print_usage(void)
66 fprintf(stderr,"\n"); 67 fprintf(stderr,"\n");
67 fprintf(stderr,"Where [action] is one of the following options:\n"); 68 fprintf(stderr,"Where [action] is one of the following options:\n");
68#ifdef WITH_BOOTOBJS 69#ifdef WITH_BOOTOBJS
69 fprintf(stderr," -i, --install <bootloader.bin>\n"); 70 fprintf(stderr," -i, --install [bootloader.bin]\n");
71 fprintf(stderr," -d, --dual-boot <nk.bin> [bootloader.bin]\n");
70#else 72#else
71 fprintf(stderr," -i, --install bootloader.bin\n"); 73 fprintf(stderr," -i, --install <bootloader.bin>\n");
74 fprintf(stderr," -d --dual-boot <nk.bin> <bootloader.bin>\n");
72#endif 75#endif
73 fprintf(stderr," -s, --send nk.bin\n"); 76 fprintf(stderr," -s, --send <nk.bin>\n");
74 fprintf(stderr," -h, --help\n"); 77 fprintf(stderr," -h, --help\n");
75 fprintf(stderr,"\n"); 78 fprintf(stderr,"\n");
76#ifdef WITH_BOOTOBJS 79#ifdef WITH_BOOTOBJS
@@ -88,9 +91,9 @@ int main(int argc, char* argv[])
88 char* bootloader = NULL; 91 char* bootloader = NULL;
89 char* firmware = NULL; 92 char* firmware = NULL;
90#ifdef WITH_BOOTOBJS 93#ifdef WITH_BOOTOBJS
91 int action = INSTALL; 94 enum actions action = INSTALL;
92#else 95#else
93 int action = NONE; 96 enum actions action = NONE;
94#endif 97#endif
95 98
96 fprintf(stderr,"beastpatcher v" VERSION " - (C) 2009 by the Rockbox developers\n"); 99 fprintf(stderr,"beastpatcher v" VERSION " - (C) 2009 by the Rockbox developers\n");
@@ -113,6 +116,20 @@ int main(int argc, char* argv[])
113 } 116 }
114#endif 117#endif
115 } 118 }
119 else if(strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--dual-boot") == 0) {
120 action = DUALBOOT;
121 if(((i + 1) < argc) && argv[i + 1][0] != '-') {
122 firmware = argv[++i];
123#ifndef WITH_BOOTOBJS
124 if(((i + 1) < argc) && argv[i + 1][0] != '-') {
125 bootloader = argv[++i];
126 }
127#endif
128 }
129 else {
130 action = NONE;
131 }
132 }
116 else if(((strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--send") == 0) 133 else if(((strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--send") == 0)
117 && (i + 1) < argc)) { 134 && (i + 1) < argc)) {
118 action = SEND; 135 action = SEND;
@@ -132,8 +149,11 @@ int main(int argc, char* argv[])
132 else if(action == SEND) { 149 else if(action == SEND) {
133 res = sendfirm(firmware); 150 res = sendfirm(firmware);
134 } 151 }
152 else if(action == DUALBOOT) {
153 res = beastpatcher(bootloader, firmware);
154 }
135 else if(action == INSTALL) { 155 else if(action == INSTALL) {
136 res = beastpatcher(bootloader); 156 res = beastpatcher(bootloader, NULL);
137 /* don't ask for enter if started with command line arguments */ 157 /* don't ask for enter if started with command line arguments */
138 if(argc == 1) { 158 if(argc == 1) {
139 printf("\nPress ENTER to exit beastpatcher: "); 159 printf("\nPress ENTER to exit beastpatcher: ");
@@ -142,3 +162,4 @@ int main(int argc, char* argv[])
142 } 162 }
143 return res; 163 return res;
144} 164}
165