lib: added ability to do a USB ClearHalt() from socket level
[barry.git] / tools / bjavaloader.cc
blob1b15595c6d2f4bd4a8c617f8ae80d96ed1fdc5b8
1 ///
2 /// \file bjavaloader.cc
3 ///
4 ///
6 /*
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>
27 #include <iostream>
28 #include <vector>
29 #include <string>
30 #include <cstring>
31 #include <algorithm>
32 #include <getopt.h>
33 #include <fstream>
34 #include <string.h>
35 #include "i18n.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"
55 using namespace std;
56 using namespace Barry;
58 void Usage()
60 int major, minor;
61 const char *Version = Barry::Version(major, minor);
63 cerr
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"
68 << "\n"
69 << " -a Wipe applications only\n"
70 << " -i Wipe filesystem only\n"
71 << " -f Force erase, if module is in use\n"
72 << " -h This help\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"
78 << "\n"
79 << "commands\n"
80 << "\n"
81 << " " << CMD_LIST << " [-s]\n"
82 << " Lists modules on the handheld\n"
83 << "\n"
84 << " " << CMD_DEVICEINFO << "\n"
85 << " Provides information on the handheld\n"
86 << "\n"
87 << " " << CMD_LOAD << " <.cod file> ...\n"
88 << " Loads modules onto the handheld\n"
89 << "\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"
93 << "\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"
100 << "\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"
106 << "\n"
107 << " " << CMD_ERASE << " [-f] <module name> ...\n"
108 << " Erase module from handheld\n"
109 << "\n"
110 << " " << CMD_EVENTLOG << "\n"
111 << " Retrieves the handheld event log\n"
112 << "\n"
113 << " " << CMD_CLEAR_LOG << "\n"
114 << " Clears the handheld event log\n"
115 << "\n"
116 << " " << CMD_LOGSTRACES << "\n"
117 << " Dump the stack traces for all threads to the event log\n"
118 << "\n"
119 << " " << CMD_SCREENSHOT << " <.bmp file>\n"
120 << " Make a screenshot of handheld\n"
121 << "\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"
126 << endl;
130 class AutoClose
132 FILE *fp;
134 public:
135 AutoClose(FILE *fh) : fp(fh) {}
136 ~AutoClose()
138 fclose(fp);
142 void SetTime(Barry::Mode::JavaLoader *javaloader, const char *timestr)
144 time_t when;
146 if( timestr ) {
147 struct tm timeinfo;
148 memset(&timeinfo, 0, sizeof(timeinfo));
150 // parse time string
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
162 time(&when);
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)
177 // Take a screenshot
178 // - info object contains the screenshot properties (width, height...)
179 // - image will be filled with the raw pixel screenshot data
180 JLScreenInfo info;
181 Data image;
182 javaloader->GetScreenshot(info, image);
185 // Convert to BMP format
186 Data bitmap(-1, GetTotalBitmapSize(info));
187 ScreenshotToBitmap(info, image, bitmap);
189 // Write BMP file
190 FILE *fp = fopen(filename, "wb");
191 if (fp == NULL) {
192 throw runtime_error(string("Can't open: ") + filename);
194 AutoClose ac(fp);
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);
208 else {
209 // filename does not contain .cod extension, use it as module name
210 module = fname;
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[])
222 INIT_I18N(PACKAGE);
224 cout.sync_with_stdio(true); // leave this on, since libusb uses
225 // stdio for debug messages
227 try {
229 uint32_t pin = 0;
230 bool list_siblings = false,
231 force_erase = false,
232 data_dump = false,
233 wipe_apps = true,
234 wipe_fs = true;
235 string password;
236 vector<string> params;
237 string busname;
238 string devname;
239 string iconvCharset;
240 Usb::EndpointPair epOverride;
242 // process command line options
243 for(;;) {
244 int cmd = getopt(argc, argv, "aifhsp:P:v");
245 if( cmd == -1 )
246 break;
248 switch( cmd )
250 case 'p': // Blackberry PIN
251 pin = strtoul(optarg, NULL, 16);
252 break;
254 case 'P': // Device password
255 password = optarg;
256 break;
258 case 'f': // turn on 'force' mode for erase
259 force_erase = true;
260 break;
262 case 's': // turn on listing of sibling modules
263 list_siblings = true;
264 break;
266 case 'v': // data dump on
267 data_dump = true;
268 break;
270 case 'a': // wipe apps only
271 wipe_fs = false;
272 break;
274 case 'i': // wipe filesystem
275 wipe_apps = false;
276 break;
278 case 'h': // help
279 default:
280 Usage();
281 return 0;
285 argc -= optind;
286 argv += optind;
288 if( argc < 1 ) {
289 cerr << "missing command" << endl;
290 Usage();
291 return 1;
294 // Fetch command from remaining arguments
295 string cmd = argv[0];
296 argc --;
297 argv ++;
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
305 // anything else.
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
311 Barry::Probe probe;
312 int activeDevice = probe.FindActive(pin);
313 if( activeDevice == -1 ) {
314 cerr << "No device selected, or PIN not found" << endl;
315 return 1;
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 ) {
329 JLDirectory dir;
330 javaloader.GetDirectory(dir, list_siblings);
331 cout << dir;
333 else if( cmd == CMD_LOAD ) {
334 if( params.size() == 0 ) {
335 cerr << "specify at least one .cod file to load" << endl;
336 Usage();
337 return 1;
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;
350 Usage();
351 return 1;
354 vector<string>::iterator i = params.begin(), end = params.end();
355 for( ; i != end; ++i ) {
356 cout << "erasing: " << (*i) << "... ";
357 if( force_erase )
358 javaloader.ForceErase((*i));
359 else
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;
367 Usage();
368 return 1;
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());
376 } else {
377 SetTime(&javaloader, NULL);
380 else if( cmd == CMD_EVENTLOG ) {
381 JLEventlog log;
382 javaloader.GetEventlog(log);
383 cout << 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;
394 Usage();
395 return 1;
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 ) {
406 JLDeviceInfo info;
407 javaloader.DeviceInfo(info);
408 cout << info;
410 else if( cmd == CMD_WIPE ) {
411 cout
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) ";
417 string confirm;
418 getline(cin, confirm);
419 if( confirm == "yes" ) {
420 javaloader.Wipe(wipe_apps, wipe_fs);
422 else {
423 cout << "Response of 'yes' not received, aborting." << endl;
426 else if( cmd == CMD_RESETFACTORY ) {
427 cout
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) ";
432 string confirm;
433 getline(cin, confirm);
434 if( confirm == "yes" ) {
435 javaloader.ResetToFactory();
437 else {
438 cout << "Response of 'yes' not received, aborting." << endl;
441 else {
442 cerr << "invalid command \"" << cmd << "\"" << endl;
443 Usage();
444 return 1;
447 // Stop
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;
454 return 1;
456 catch( Barry::Error &se ) {
457 std::cout << endl;
458 std::cerr << "Barry::Error caught: " << se.what() << endl;
459 return 1;
461 catch( std::exception &e ) {
462 std::cout << endl;
463 std::cerr << "std::exception caught: " << e.what() << endl;
464 return 1;
467 return 0;