2 /// \file bjavaloader.cc
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2005-2011, 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>
38 // supported javaloader commands
39 #define CMD_LIST "dir"
40 #define CMD_ERASE "erase"
41 #define CMD_LOAD "load"
42 #define CMD_SCREENSHOT "screenshot"
43 #define CMD_SETTIME "settime"
44 #define CMD_EVENTLOG "eventlog"
45 #define CMD_CLEAR_LOG "cleareventlog"
46 #define CMD_SAVE "save"
47 #define CMD_DEVICEINFO "deviceinfo"
48 #define CMD_WIPE "wipe"
49 #define CMD_LOGSTRACES "logstacktraces"
50 #define CMD_RESETFACTORY "resettofactory"
52 // time string format specifier and user friendly description
53 #define TIME_FMT "%Y-%m-%d %H:%M:%S"
54 #define TIME_FMT_EXAMPLE "yyyy-mm-dd HH:MM:SS"
57 using namespace Barry
;
61 int logical
, major
, minor
;
62 const char *Version
= Barry::Version(logical
, major
, minor
);
65 << "bjavaloader - Command line USB Blackberry Java Loader\n"
66 << " Copyright 2008-2009, Nicolas VIVIEN.\n"
67 << " Copyright 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)\n"
68 << " Using: " << Version
<< "\n"
70 << " -A Save all modules found\n"
71 << " -a Wipe applications only\n"
72 << " -i Wipe filesystem only\n"
73 << " -f Force erase, if module is in use\n"
75 << " -s List sibling in module list\n"
76 << " -p pin PIN of device to talk with\n"
77 << " If only one device is plugged in, this flag is optional\n"
78 << " -P pass Simplistic method to specify device password\n"
79 << " -v Dump protocol data during operation\n"
83 << " " << CMD_LIST
<< " [-s]\n"
84 << " Lists modules on the handheld\n"
86 << " " << CMD_DEVICEINFO
<< "\n"
87 << " Provides information on the handheld\n"
89 << " " << CMD_LOAD
<< " <.cod file> ...\n"
90 << " Loads modules onto the handheld\n"
92 << " " << CMD_SAVE
<< " [-A] <module name> ...\n"
93 << " Retrieves modules from the handheld and writes to .cod file\n"
94 << " Note: will overwrite existing files!\n"
96 << " " << CMD_WIPE
<< " [-a | -i]\n"
97 << " Wipes the handheld\n"
98 << " Use Caution: Wiping filesystem will remove all data\n"
99 << " such as messages, contacts, etc.\n"
100 << " Wiping applications will remove all .cod files\n"
101 << " on the device, including OS .cod files.\n"
103 << " " << CMD_RESETFACTORY
<< "\n"
104 << " Reset IT policy to factory defaults\n"
105 << " Use Caution: Resetting IT policy to factory defaults will\n"
106 << " also perform a filesystem wipe which will remove\n"
107 << " all data such as messages, contacts, etc.\n"
109 << " " << CMD_ERASE
<< " [-f] <module name> ...\n"
110 << " Erase module from handheld\n"
112 << " " << CMD_EVENTLOG
<< "\n"
113 << " Retrieves the handheld event log\n"
115 << " " << CMD_CLEAR_LOG
<< "\n"
116 << " Clears the handheld event log\n"
118 << " " << CMD_LOGSTRACES
<< "\n"
119 << " Dump the stack traces for all threads to the event log\n"
121 << " " << CMD_SCREENSHOT
<< " <.bmp file>\n"
122 << " Make a screenshot of handheld\n"
124 << " " << CMD_SETTIME
<< " [" << TIME_FMT_EXAMPLE
<< "]\n"
125 << " Sets the time on the handheld to the current time\n"
126 << " Or the time specified as an argument to " << CMD_SETTIME
<< "\n"
127 << " If given as argument, current system timezone is assumed\n"
137 AutoClose(FILE *fh
) : fp(fh
) {}
144 void SetTime(Barry::Mode::JavaLoader
*javaloader
, const char *timestr
)
150 memset(&timeinfo
, 0, sizeof(timeinfo
));
153 char *p
= strptime(timestr
, TIME_FMT
, &timeinfo
);
155 // NULL is return when strptime fails to match all of the format
156 // string, and returns a pointer to the NULL byte at the end of
157 // the input string on success
158 if( p
== NULL
|| p
!= (timestr
+ strlen(timestr
)) ) {
159 throw runtime_error(string("Unable to parse time string: ") + timestr
);
162 when
= mktime(&timeinfo
);
163 } else { // time string is NULL, get current time
167 javaloader
->SetTime(when
);
170 void SendAppFile(Barry::Mode::JavaLoader
*javaloader
, const char *filename
)
172 ifstream
file(filename
);
173 javaloader
->LoadApp(file
);
176 void GetScreenshot(Barry::Mode::JavaLoader
*javaloader
, const char *filename
)
180 // - info object contains the screenshot properties (width, height...)
181 // - image will be filled with the raw pixel screenshot data
184 javaloader
->GetScreenshot(info
, image
);
187 // Convert to BMP format
188 Data
bitmap(-1, GetTotalBitmapSize(info
));
189 ScreenshotToBitmap(info
, image
, bitmap
);
192 FILE *fp
= fopen(filename
, "wb");
194 throw runtime_error(string("Can't open: ") + filename
);
198 fwrite(bitmap
.GetData(), bitmap
.GetSize(), 1, fp
);
201 void SaveModule(Barry::Mode::JavaLoader
*javaloader
, const char *filename
)
203 string
fname(filename
), module
;
205 size_t ext_index
= fname
.rfind(".cod");
206 if( ext_index
!= string::npos
) {
207 // filename contains .cod extension, strip it for module name
208 module
= fname
.substr(0, ext_index
);
211 // filename does not contain .cod extension, use it as module name
213 // append extension to file name
214 fname
.append(".cod");
217 ofstream
file(fname
.c_str(), ios::binary
| ios::trunc
);
218 javaloader
->Save(module
.c_str(), file
);
222 int main(int argc
, char *argv
[])
226 cout
.sync_with_stdio(true); // leave this on, since libusb uses
227 // stdio for debug messages
232 bool list_siblings
= false,
239 vector
<string
> params
;
243 Usb::EndpointPair epOverride
;
245 // process command line options
247 int cmd
= getopt(argc
, argv
, "Aaifhsp:P:v");
253 case 'p': // Blackberry PIN
254 pin
= strtoul(optarg
, NULL
, 16);
257 case 'P': // Device password
261 case 'f': // turn on 'force' mode for erase
265 case 's': // turn on listing of sibling modules
266 list_siblings
= true;
269 case 'v': // data dump on
273 case 'A': // save all modules
277 case 'a': // wipe apps only
281 case 'i': // wipe filesystem
296 cerr
<< "missing command" << endl
;
301 // Fetch command from remaining arguments
302 string cmd
= argv
[0];
306 // Put the remaining arguments into an array
307 for (; argc
> 0; argc
--, argv
++) {
308 params
.push_back(string(argv
[0]));
311 // Initialize the barry library. Must be called before
313 Barry::Init(data_dump
);
315 // Probe the USB bus for Blackberry devices and display.
316 // If user has specified a PIN, search for it in the
317 // available device list here as well
319 int activeDevice
= probe
.FindActive(pin
);
320 if( activeDevice
== -1 ) {
321 cerr
<< "No device selected, or PIN not found" << endl
;
325 // Create our controller object
326 Barry::Controller
con(probe
.Get(activeDevice
));
327 Barry::Mode::JavaLoader
javaloader(con
);
330 // execute each mode that was turned on
332 javaloader
.Open(password
.c_str());
333 javaloader
.StartStream();
335 if( cmd
== CMD_LIST
) {
337 javaloader
.GetDirectory(dir
, list_siblings
);
340 else if( cmd
== CMD_LOAD
) {
341 if( params
.size() == 0 ) {
342 cerr
<< "specify at least one .cod file to load" << endl
;
347 vector
<string
>::iterator i
= params
.begin(), end
= params
.end();
348 for( ; i
!= end
; ++i
) {
349 cout
<< "loading " << (*i
) << "... ";
350 SendAppFile(&javaloader
, (*i
).c_str());
351 cout
<< "done." << endl
;
354 else if( cmd
== CMD_ERASE
) {
355 if( params
.size() == 0 ) {
356 cerr
<< "specify at least one module to erase" << endl
;
361 vector
<string
>::iterator i
= params
.begin(), end
= params
.end();
362 for( ; i
!= end
; ++i
) {
363 cout
<< "erasing: " << (*i
) << "... ";
365 javaloader
.ForceErase((*i
));
367 javaloader
.Erase((*i
));
368 cout
<< "done." << endl
;
371 else if( cmd
== CMD_SCREENSHOT
) {
372 if( params
.size() == 0 ) {
373 cerr
<< "specify a .bmp filename" << endl
;
378 GetScreenshot(&javaloader
, params
[0].c_str());
380 else if( cmd
== CMD_SETTIME
) {
381 if( params
.size() > 0 ) {
382 SetTime(&javaloader
, params
[0].c_str());
384 SetTime(&javaloader
, NULL
);
387 else if( cmd
== CMD_EVENTLOG
) {
389 javaloader
.GetEventlog(log
);
392 else if( cmd
== CMD_CLEAR_LOG
) {
393 javaloader
.ClearEventlog();
395 else if( cmd
== CMD_LOGSTRACES
) {
396 javaloader
.LogStackTraces();
398 else if( cmd
== CMD_SAVE
) {
401 javaloader
.GetDirectory(dir
, false);
402 JLDirectory::BaseIterator i
= dir
.begin();
403 for( ; i
!= dir
.end(); ++i
) {
404 cout
<< "saving: " << i
->Name
<< "... ";
405 SaveModule(&javaloader
,i
->Name
.c_str());
406 cout
<< "done." << endl
;
409 else if( params
.size() == 0 ) {
410 cerr
<< "specify at least one module to save" << endl
;
415 vector
<string
>::iterator i
= params
.begin(), end
= params
.end();
416 for( ; i
!= end
; ++i
) {
417 cout
<< "saving: " << (*i
) << "... ";
418 SaveModule(&javaloader
, (*i
).c_str());
419 cout
<< "done." << endl
;
423 else if( cmd
== CMD_DEVICEINFO
) {
425 javaloader
.DeviceInfo(info
);
428 else if( cmd
== CMD_WIPE
) {
430 << "Use Caution: Wiping filesystem will remove all data\n"
431 << " such as messages, contacts, etc.\n"
432 << " Wiping applications will remove all .cod files\n"
433 << " on the device, including OS .cod files.\n\n"
434 << "You have selected to wipe the filesystem of device '" << probe
.Get(activeDevice
).m_pin
.Str() << "'\n"
435 << "Continue with wipe? (yes/no) ";
437 getline(cin
, confirm
);
438 if( confirm
== "yes" ) {
439 javaloader
.Wipe(wipe_apps
, wipe_fs
);
442 cout
<< "Response of 'yes' not received, aborting." << endl
;
445 else if( cmd
== CMD_RESETFACTORY
) {
447 << "Use Caution: Resetting IT policy to factory defaults will\n"
448 << " also perform a filesystem wipe which will remove\n"
449 << " all data such as messages, contacts, etc.\n\n"
450 << "You have selected to reset device '" << probe
.Get(activeDevice
).m_pin
.Str() << "' to factory defaults\n"
451 << "Continue with wipe? (yes/no) ";
453 getline(cin
, confirm
);
454 if( confirm
== "yes" ) {
455 javaloader
.ResetToFactory();
458 cout
<< "Response of 'yes' not received, aborting." << endl
;
462 cerr
<< "invalid command \"" << cmd
<< "\"" << endl
;
468 javaloader
.StopStream();
471 catch( Usb::Error
&ue
) {
472 std::cout
<< endl
; // flush any normal output first
473 std::cerr
<< "Usb::Error caught: " << ue
.what() << endl
;
476 catch( Barry::Error
&se
) {
478 std::cerr
<< "Barry::Error caught: " << se
.what() << endl
;
481 catch( std::exception
&e
) {
483 std::cerr
<< "std::exception caught: " << e
.what() << endl
;