summaryrefslogtreecommitdiff
path: root/utils/MTP/beastpatcher/beastpatcher.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/MTP/beastpatcher/beastpatcher.c')
-rw-r--r--utils/MTP/beastpatcher/beastpatcher.c94
1 files changed, 62 insertions, 32 deletions
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);