2 /// \file bjavaloader.cc
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
10 Some parts are inspired from btool.cc
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU General Public License in the COPYING file at the
22 root directory of this project for more details.
26 #include <barry/barry.h>
37 // supported javaloader commands
38 #define CMD_LIST "dir"
39 #define CMD_ERASE "erase"
40 #define CMD_LOAD "load"
41 #define CMD_SCREENSHOT "screenshot"
42 #define CMD_SETTIME "settime"
43 #define CMD_EVENTLOG "eventlog"
44 #define CMD_CLEAR_LOG "cleareventlog"
45 #define CMD_SAVE "save"
46 #define CMD_DEVICEINFO "deviceinfo"
47 #define CMD_WIPE "wipe"
48 #define CMD_LOGSTRACES "logstacktraces"
49 #define CMD_RESETFACTORY "resettofactory"
51 // time string format specifier and user friendly description
52 #define TIME_FMT "%Y-%m-%d %H:%M:%S"
53 #define TIME_FMT_EXAMPLE "yyyy-mm-dd HH:MM:SS"
56 using namespace Barry
;
61 const char *Version
= Barry::Version(major
, minor
);
64 << "bjavaloader - Command line USB Blackberry Java Loader\n"
65 << " Copyright 2008-2009, Nicolas VIVIEN.\n"
66 << " Copyright 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
67 << " Using: " << Version
<< "\n"
69 << " -a Wipe applications only\n"
70 << " -i Wipe filesystem only\n"
71 << " -f Force erase, if module is in use\n"
73 << " -s List sibling in module list\n"
74 << " -p pin PIN of device to talk with\n"
75 << " If only one device is plugged in, this flag is optional\n"
76 << " -P pass Simplistic method to specify device password\n"
77 << " -v Dump protocol data during operation\n"
81 << " " << CMD_LIST
<< " [-s]\n"
82 << " Lists modules on the handheld\n"
84 << " " << CMD_DEVICEINFO
<< "\n"
85 << " Provides information on the handheld\n"
87 << " " << CMD_LOAD
<< " <.cod file> ...\n"
88 << " Loads modules onto the handheld\n"
90 << " " << CMD_SAVE
<< " <module name> ...\n"
91 << " Retrieves modules from the handheld and writes to .cod file\n"
92 << " Note: will overwrite existing files!\n"
94 << " " << CMD_WIPE
<< " [-a | -i]\n"
95 << " Wipes the handheld\n"
96 << " Use Caution: Wiping filesystem will remove all data\n"
97 << " such as messages, contacts, etc.\n"
98 << " Wiping applications will remove all .cod files\n"
99 << " on the device, including OS .cod files.\n"
101 << " " << CMD_RESETFACTORY
<< "\n"
102 << " Reset IT policy to factory defaults\n"
103 << " Use Caution: Resetting IT policy to factory defaults will\n"
104 << " also perform a filesystem wipe which will remove\n"
105 << " all data such as messages, contacts, etc.\n"
107 << " " << CMD_ERASE
<< " [-f] <module name> ...\n"
108 << " Erase module from handheld\n"
110 << " " << CMD_EVENTLOG
<< "\n"
111 << " Retrieves the handheld event log\n"
113 << " " << CMD_CLEAR_LOG
<< "\n"
114 << " Clears the handheld event log\n"
116 << " " << CMD_LOGSTRACES
<< "\n"
117 << " Dump the stack traces for all threads to the event log\n"
119 << " " << CMD_SCREENSHOT
<< " <.bmp file>\n"
120 << " Make a screenshot of handheld\n"
122 << " " << CMD_SETTIME
<< " [" << TIME_FMT_EXAMPLE
<< "]\n"
123 << " Sets the time on the handheld to the current time\n"
124 << " Or the time specified as an argument to " << CMD_SETTIME
<< "\n"
125 << " If given as argument, current system timezone is assumed\n"
135 AutoClose(FILE *fh
) : fp(fh
) {}
142 void SetTime(Barry::Mode::JavaLoader
*javaloader
, const char *timestr
)
148 memset(&timeinfo
, 0, sizeof(timeinfo
));
151 char *p
= strptime(timestr
, TIME_FMT
, &timeinfo
);
153 // NULL is return when strptime fails to match all of the format
154 // string, and returns a pointer to the NULL byte at the end of
155 // the input string on success
156 if( p
== NULL
|| p
!= (timestr
+ strlen(timestr
)) ) {
157 throw runtime_error(string("Unable to parse time string: ") + timestr
);
160 when
= mktime(&timeinfo
);
161 } else { // time string is NULL, get current time
165 javaloader
->SetTime(when
);
168 void SendAppFile(Barry::Mode::JavaLoader
*javaloader
, const char *filename
)
170 ifstream
file(filename
);
171 javaloader
->LoadApp(file
);
174 void GetScreenshot(Barry::Mode::JavaLoader
*javaloader
, const char *filename
)
178 // - info object contains the screenshot properties (width, height...)
179 // - image will be filled with the raw pixel screenshot data
182 javaloader
->GetScreenshot(info
, image
);
185 // Convert to BMP format
186 Data
bitmap(-1, GetTotalBitmapSize(info
));
187 ScreenshotToBitmap(info
, image
, bitmap
);
190 FILE *fp
= fopen(filename
, "wb");
192 throw runtime_error(string("Can't open: ") + filename
);
196 fwrite(bitmap
.GetData(), bitmap
.GetSize(), 1, fp
);
199 void SaveModule(Barry::Mode::JavaLoader
*javaloader
, const char *filename
)
201 string
fname(filename
), module
;
203 size_t ext_index
= fname
.rfind(".cod");
204 if( ext_index
!= string::npos
) {
205 // filename contains .cod extension, strip it for module name
206 module
= fname
.substr(0, ext_index
);
209 // filename does not contain .cod extension, use it as module name
211 // append extension to file name
212 fname
.append(".cod");
215 ofstream
file(fname
.c_str(), ios::binary
| ios::trunc
);
216 javaloader
->Save(module
.c_str(), file
);
220 int main(int argc
, char *argv
[])
224 cout
.sync_with_stdio(true); // leave this on, since libusb uses
225 // stdio for debug messages
230 bool list_siblings
= false,
236 vector
<string
> params
;
240 Usb::EndpointPair epOverride
;
242 // process command line options
244 int cmd
= getopt(argc
, argv
, "aifhsp:P:v");
250 case 'p': // Blackberry PIN
251 pin
= strtoul(optarg
, NULL
, 16);
254 case 'P': // Device password
258 case 'f': // turn on 'force' mode for erase
262 case 's': // turn on listing of sibling modules
263 list_siblings
= true;
266 case 'v': // data dump on
270 case 'a': // wipe apps only
274 case 'i': // wipe filesystem
289 cerr
<< "missing command" << endl
;
294 // Fetch command from remaining arguments
295 string cmd
= argv
[0];
299 // Put the remaining arguments into an array
300 for (; argc
> 0; argc
--, argv
++) {
301 params
.push_back(string(argv
[0]));
304 // Initialize the barry library. Must be called before
306 Barry::Init(data_dump
);
308 // Probe the USB bus for Blackberry devices and display.
309 // If user has specified a PIN, search for it in the
310 // available device list here as well
312 int activeDevice
= probe
.FindActive(pin
);
313 if( activeDevice
== -1 ) {
314 cerr
<< "No device selected, or PIN not found" << endl
;
318 // Create our controller object
319 Barry::Controller
con(probe
.Get(activeDevice
));
320 Barry::Mode::JavaLoader
javaloader(con
);
323 // execute each mode that was turned on
325 javaloader
.Open(password
.c_str());
326 javaloader
.StartStream();
328 if( cmd
== CMD_LIST
) {
330 javaloader
.GetDirectory(dir
, list_siblings
);
333 else if( cmd
== CMD_LOAD
) {
334 if( params
.size() == 0 ) {
335 cerr
<< "specify at least one .cod file to load" << endl
;
340 vector
<string
>::iterator i
= params
.begin(), end
= params
.end();
341 for( ; i
!= end
; ++i
) {
342 cout
<< "loading " << (*i
) << "... ";
343 SendAppFile(&javaloader
, (*i
).c_str());
344 cout
<< "done." << endl
;
347 else if( cmd
== CMD_ERASE
) {
348 if( params
.size() == 0 ) {
349 cerr
<< "specify at least one module to erase" << endl
;
354 vector
<string
>::iterator i
= params
.begin(), end
= params
.end();
355 for( ; i
!= end
; ++i
) {
356 cout
<< "erasing: " << (*i
) << "... ";
358 javaloader
.ForceErase((*i
));
360 javaloader
.Erase((*i
));
361 cout
<< "done." << endl
;
364 else if( cmd
== CMD_SCREENSHOT
) {
365 if( params
.size() == 0 ) {
366 cerr
<< "specify a .bmp filename" << endl
;
371 GetScreenshot(&javaloader
, params
[0].c_str());
373 else if( cmd
== CMD_SETTIME
) {
374 if( params
.size() > 0 ) {
375 SetTime(&javaloader
, params
[0].c_str());
377 SetTime(&javaloader
, NULL
);
380 else if( cmd
== CMD_EVENTLOG
) {
382 javaloader
.GetEventlog(log
);
385 else if( cmd
== CMD_CLEAR_LOG
) {
386 javaloader
.ClearEventlog();
388 else if( cmd
== CMD_LOGSTRACES
) {
389 javaloader
.LogStackTraces();
391 else if( cmd
== CMD_SAVE
) {
392 if( params
.size() == 0 ) {
393 cerr
<< "specify at least one module to save" << endl
;
398 vector
<string
>::iterator i
= params
.begin(), end
= params
.end();
399 for( ; i
!= end
; ++i
) {
400 cout
<< "saving: " << (*i
) << "... ";
401 SaveModule(&javaloader
, (*i
).c_str());
402 cout
<< "done." << endl
;
405 else if( cmd
== CMD_DEVICEINFO
) {
407 javaloader
.DeviceInfo(info
);
410 else if( cmd
== CMD_WIPE
) {
412 << "Use Caution: Wiping filesystem will remove all data\n"
413 << " such as messages, contacts, etc.\n"
414 << " Wiping applications will remove all .cod files\n"
415 << " on the device, including OS .cod files.\n\n"
416 << "Continue with wipe? (yes/no) ";
418 getline(cin
, confirm
);
419 if( confirm
== "yes" ) {
420 javaloader
.Wipe(wipe_apps
, wipe_fs
);
423 cout
<< "Response of 'yes' not received, aborting." << endl
;
426 else if( cmd
== CMD_RESETFACTORY
) {
428 << "Use Caution: Resetting IT policy to factory defaults will\n"
429 << " also perform a filesystem wipe which will remove\n"
430 << " all data such as messages, contacts, etc.\n\n"
431 << "Continue with wipe? (yes/no) ";
433 getline(cin
, confirm
);
434 if( confirm
== "yes" ) {
435 javaloader
.ResetToFactory();
438 cout
<< "Response of 'yes' not received, aborting." << endl
;
442 cerr
<< "invalid command \"" << cmd
<< "\"" << endl
;
448 javaloader
.StopStream();
451 catch( Usb::Error
&ue
) {
452 std::cout
<< endl
; // flush any normal output first
453 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
456 catch( Barry::Error
&se
) {
458 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
461 catch( std::exception
&e
) {
463 std::cerr
<< "std::exception caught: " << e
.what() << endl
;