Updated ChangeLog
[barry.git] / tools / bjavaloader.cc
blobdea69be713eb0ccc1f8c4b17ef432f90e95f3536
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 Save all modules found\n"
70 << " -a Wipe applications only\n"
71 << " -i Wipe filesystem only\n"
72 << " -f Force erase, if module is in use\n"
73 << " -h This help\n"
74 << " -s List sibling in module list\n"
75 << " -p pin PIN of device to talk with\n"
76 << " If only one device is plugged in, this flag is optional\n"
77 << " -P pass Simplistic method to specify device password\n"
78 << " -v Dump protocol data during operation\n"
79 << "\n"
80 << "commands\n"
81 << "\n"
82 << " " << CMD_LIST << " [-s]\n"
83 << " Lists modules on the handheld\n"
84 << "\n"
85 << " " << CMD_DEVICEINFO << "\n"
86 << " Provides information on the handheld\n"
87 << "\n"
88 << " " << CMD_LOAD << " <.cod file> ...\n"
89 << " Loads modules onto the handheld\n"
90 << "\n"
91 << " " << CMD_SAVE << " [-A] <module name> ...\n"
92 << " Retrieves modules from the handheld and writes to .cod file\n"
93 << " Note: will overwrite existing files!\n"
94 << "\n"
95 << " " << CMD_WIPE << " [-a | -i]\n"
96 << " Wipes the handheld\n"
97 << " Use Caution: Wiping filesystem will remove all data\n"
98 << " such as messages, contacts, etc.\n"
99 << " Wiping applications will remove all .cod files\n"
100 << " on the device, including OS .cod files.\n"
101 << "\n"
102 << " " << CMD_RESETFACTORY << "\n"
103 << " Reset IT policy to factory defaults\n"
104 << " Use Caution: Resetting IT policy to factory defaults will\n"
105 << " also perform a filesystem wipe which will remove\n"
106 << " all data such as messages, contacts, etc.\n"
107 << "\n"
108 << " " << CMD_ERASE << " [-f] <module name> ...\n"
109 << " Erase module from handheld\n"
110 << "\n"
111 << " " << CMD_EVENTLOG << "\n"
112 << " Retrieves the handheld event log\n"
113 << "\n"
114 << " " << CMD_CLEAR_LOG << "\n"
115 << " Clears the handheld event log\n"
116 << "\n"
117 << " " << CMD_LOGSTRACES << "\n"
118 << " Dump the stack traces for all threads to the event log\n"
119 << "\n"
120 << " " << CMD_SCREENSHOT << " <.bmp file>\n"
121 << " Make a screenshot of handheld\n"
122 << "\n"
123 << " " << CMD_SETTIME << " [" << TIME_FMT_EXAMPLE << "]\n"
124 << " Sets the time on the handheld to the current time\n"
125 << " Or the time specified as an argument to " << CMD_SETTIME << "\n"
126 << " If given as argument, current system timezone is assumed\n"
127 << endl;
131 class AutoClose
133 FILE *fp;
135 public:
136 AutoClose(FILE *fh) : fp(fh) {}
137 ~AutoClose()
139 fclose(fp);
143 void SetTime(Barry::Mode::JavaLoader *javaloader, const char *timestr)
145 time_t when;
147 if( timestr ) {
148 struct tm timeinfo;
149 memset(&timeinfo, 0, sizeof(timeinfo));
151 // parse time string
152 char *p = strptime(timestr, TIME_FMT, &timeinfo);
154 // NULL is return when strptime fails to match all of the format
155 // string, and returns a pointer to the NULL byte at the end of
156 // the input string on success
157 if( p == NULL || p != (timestr + strlen(timestr)) ) {
158 throw runtime_error(string("Unable to parse time string: ") + timestr);
161 when = mktime(&timeinfo);
162 } else { // time string is NULL, get current time
163 time(&when);
166 javaloader->SetTime(when);
169 void SendAppFile(Barry::Mode::JavaLoader *javaloader, const char *filename)
171 ifstream file(filename);
172 javaloader->LoadApp(file);
175 void GetScreenshot(Barry::Mode::JavaLoader *javaloader, const char *filename)
178 // Take a screenshot
179 // - info object contains the screenshot properties (width, height...)
180 // - image will be filled with the raw pixel screenshot data
181 JLScreenInfo info;
182 Data image;
183 javaloader->GetScreenshot(info, image);
186 // Convert to BMP format
187 Data bitmap(-1, GetTotalBitmapSize(info));
188 ScreenshotToBitmap(info, image, bitmap);
190 // Write BMP file
191 FILE *fp = fopen(filename, "wb");
192 if (fp == NULL) {
193 throw runtime_error(string("Can't open: ") + filename);
195 AutoClose ac(fp);
197 fwrite(bitmap.GetData(), bitmap.GetSize(), 1, fp);
200 void SaveModule(Barry::Mode::JavaLoader *javaloader, const char *filename)
202 string fname(filename), module;
204 size_t ext_index = fname.rfind(".cod");
205 if( ext_index != string::npos ) {
206 // filename contains .cod extension, strip it for module name
207 module = fname.substr(0, ext_index);
209 else {
210 // filename does not contain .cod extension, use it as module name
211 module = fname;
212 // append extension to file name
213 fname.append(".cod");
216 ofstream file(fname.c_str(), ios::binary | ios::trunc);
217 javaloader->Save(module.c_str(), file);
221 int main(int argc, char *argv[])
223 INIT_I18N(PACKAGE);
225 cout.sync_with_stdio(true); // leave this on, since libusb uses
226 // stdio for debug messages
228 try {
230 uint32_t pin = 0;
231 bool list_siblings = false,
232 force_erase = false,
233 data_dump = false,
234 all_modules = false,
235 wipe_apps = true,
236 wipe_fs = true;
237 string password;
238 vector<string> params;
239 string busname;
240 string devname;
241 string iconvCharset;
242 Usb::EndpointPair epOverride;
244 // process command line options
245 for(;;) {
246 int cmd = getopt(argc, argv, "Aaifhsp:P:v");
247 if( cmd == -1 )
248 break;
250 switch( cmd )
252 case 'p': // Blackberry PIN
253 pin = strtoul(optarg, NULL, 16);
254 break;
256 case 'P': // Device password
257 password = optarg;
258 break;
260 case 'f': // turn on 'force' mode for erase
261 force_erase = true;
262 break;
264 case 's': // turn on listing of sibling modules
265 list_siblings = true;
266 break;
268 case 'v': // data dump on
269 data_dump = true;
270 break;
272 case 'A': // save all modules
273 all_modules = true;
274 break;
276 case 'a': // wipe apps only
277 wipe_fs = false;
278 break;
280 case 'i': // wipe filesystem
281 wipe_apps = false;
282 break;
284 case 'h': // help
285 default:
286 Usage();
287 return 0;
291 argc -= optind;
292 argv += optind;
294 if( argc < 1 ) {
295 cerr << "missing command" << endl;
296 Usage();
297 return 1;
300 // Fetch command from remaining arguments
301 string cmd = argv[0];
302 argc --;
303 argv ++;
305 // Put the remaining arguments into an array
306 for (; argc > 0; argc --, argv ++) {
307 params.push_back(string(argv[0]));
310 // Initialize the barry library. Must be called before
311 // anything else.
312 Barry::Init(data_dump);
314 // Probe the USB bus for Blackberry devices and display.
315 // If user has specified a PIN, search for it in the
316 // available device list here as well
317 Barry::Probe probe;
318 int activeDevice = probe.FindActive(pin);
319 if( activeDevice == -1 ) {
320 cerr << "No device selected, or PIN not found" << endl;
321 return 1;
324 // Create our controller object
325 Barry::Controller con(probe.Get(activeDevice));
326 Barry::Mode::JavaLoader javaloader(con);
329 // execute each mode that was turned on
331 javaloader.Open(password.c_str());
332 javaloader.StartStream();
334 if( cmd == CMD_LIST ) {
335 JLDirectory dir;
336 javaloader.GetDirectory(dir, list_siblings);
337 cout << dir;
339 else if( cmd == CMD_LOAD ) {
340 if( params.size() == 0 ) {
341 cerr << "specify at least one .cod file to load" << endl;
342 Usage();
343 return 1;
346 vector<string>::iterator i = params.begin(), end = params.end();
347 for( ; i != end; ++i ) {
348 cout << "loading " << (*i) << "... ";
349 SendAppFile(&javaloader, (*i).c_str());
350 cout << "done." << endl;
353 else if( cmd == CMD_ERASE ) {
354 if( params.size() == 0 ) {
355 cerr << "specify at least one module to erase" << endl;
356 Usage();
357 return 1;
360 vector<string>::iterator i = params.begin(), end = params.end();
361 for( ; i != end; ++i ) {
362 cout << "erasing: " << (*i) << "... ";
363 if( force_erase )
364 javaloader.ForceErase((*i));
365 else
366 javaloader.Erase((*i));
367 cout << "done." << endl;
370 else if( cmd == CMD_SCREENSHOT ) {
371 if( params.size() == 0 ) {
372 cerr << "specify a .bmp filename" << endl;
373 Usage();
374 return 1;
377 GetScreenshot(&javaloader, params[0].c_str());
379 else if( cmd == CMD_SETTIME ) {
380 if( params.size() > 0 ) {
381 SetTime(&javaloader, params[0].c_str());
382 } else {
383 SetTime(&javaloader, NULL);
386 else if( cmd == CMD_EVENTLOG ) {
387 JLEventlog log;
388 javaloader.GetEventlog(log);
389 cout << log;
391 else if( cmd == CMD_CLEAR_LOG ) {
392 javaloader.ClearEventlog();
394 else if( cmd == CMD_LOGSTRACES ) {
395 javaloader.LogStackTraces();
397 else if( cmd == CMD_SAVE ) {
398 if( all_modules ) {
399 JLDirectory dir;
400 javaloader.GetDirectory(dir, false);
401 JLDirectory::BaseIterator i = dir.begin();
402 for( ; i != dir.end(); ++i ) {
403 cout << "saving: " << i->Name << "... ";
404 SaveModule(&javaloader,i->Name.c_str());
405 cout << "done." << endl;
408 else if( params.size() == 0 ) {
409 cerr << "specify at least one module to save" << endl;
410 Usage();
411 return 1;
413 else {
414 vector<string>::iterator i = params.begin(), end = params.end();
415 for( ; i != end; ++i ) {
416 cout << "saving: " << (*i) << "... ";
417 SaveModule(&javaloader, (*i).c_str());
418 cout << "done." << endl;
422 else if( cmd == CMD_DEVICEINFO ) {
423 JLDeviceInfo info;
424 javaloader.DeviceInfo(info);
425 cout << info;
427 else if( cmd == CMD_WIPE ) {
428 cout
429 << "Use Caution: Wiping filesystem will remove all data\n"
430 << " such as messages, contacts, etc.\n"
431 << " Wiping applications will remove all .cod files\n"
432 << " on the device, including OS .cod files.\n\n"
433 << "Continue with wipe? (yes/no) ";
434 string confirm;
435 getline(cin, confirm);
436 if( confirm == "yes" ) {
437 javaloader.Wipe(wipe_apps, wipe_fs);
439 else {
440 cout << "Response of 'yes' not received, aborting." << endl;
443 else if( cmd == CMD_RESETFACTORY ) {
444 cout
445 << "Use Caution: Resetting IT policy to factory defaults will\n"
446 << " also perform a filesystem wipe which will remove\n"
447 << " all data such as messages, contacts, etc.\n\n"
448 << "Continue with wipe? (yes/no) ";
449 string confirm;
450 getline(cin, confirm);
451 if( confirm == "yes" ) {
452 javaloader.ResetToFactory();
454 else {
455 cout << "Response of 'yes' not received, aborting." << endl;
458 else {
459 cerr << "invalid command \"" << cmd << "\"" << endl;
460 Usage();
461 return 1;
464 // Stop
465 javaloader.StopStream();
468 catch( Usb::Error &ue) {
469 std::cout << endl; // flush any normal output first
470 std::cerr << "Usb::Error caught: " << ue.what() << endl;
471 return 1;
473 catch( Barry::Error &se ) {
474 std::cout << endl;
475 std::cerr << "Barry::Error caught: " << se.what() << endl;
476 return 1;
478 catch( std::exception &e ) {
479 std::cout << endl;
480 std::cerr << "std::exception caught: " << e.what() << endl;
481 return 1;
484 return 0;