maint: added example command in release.sh
[barry.git] / tools / bjavaloader.cc
bloba0659df217a71ac970ec58c208788bb7f79f716d
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 <time.h>
36 #include "i18n.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"
56 using namespace std;
57 using namespace Barry;
59 void Usage()
61 int major, minor;
62 const char *Version = Barry::Version(major, minor);
64 cerr
65 << "bjavaloader - Command line USB Blackberry Java Loader\n"
66 << " Copyright 2008-2009, Nicolas VIVIEN.\n"
67 << " Copyright 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
68 << " Using: " << Version << "\n"
69 << "\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"
74 << " -h This help\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"
80 << "\n"
81 << "commands\n"
82 << "\n"
83 << " " << CMD_LIST << " [-s]\n"
84 << " Lists modules on the handheld\n"
85 << "\n"
86 << " " << CMD_DEVICEINFO << "\n"
87 << " Provides information on the handheld\n"
88 << "\n"
89 << " " << CMD_LOAD << " <.cod file> ...\n"
90 << " Loads modules onto the handheld\n"
91 << "\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"
95 << "\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"
102 << "\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"
108 << "\n"
109 << " " << CMD_ERASE << " [-f] <module name> ...\n"
110 << " Erase module from handheld\n"
111 << "\n"
112 << " " << CMD_EVENTLOG << "\n"
113 << " Retrieves the handheld event log\n"
114 << "\n"
115 << " " << CMD_CLEAR_LOG << "\n"
116 << " Clears the handheld event log\n"
117 << "\n"
118 << " " << CMD_LOGSTRACES << "\n"
119 << " Dump the stack traces for all threads to the event log\n"
120 << "\n"
121 << " " << CMD_SCREENSHOT << " <.bmp file>\n"
122 << " Make a screenshot of handheld\n"
123 << "\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"
128 << endl;
132 class AutoClose
134 FILE *fp;
136 public:
137 AutoClose(FILE *fh) : fp(fh) {}
138 ~AutoClose()
140 fclose(fp);
144 void SetTime(Barry::Mode::JavaLoader *javaloader, const char *timestr)
146 time_t when;
148 if( timestr ) {
149 struct tm timeinfo;
150 memset(&timeinfo, 0, sizeof(timeinfo));
152 // parse time string
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
164 time(&when);
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)
179 // Take a screenshot
180 // - info object contains the screenshot properties (width, height...)
181 // - image will be filled with the raw pixel screenshot data
182 JLScreenInfo info;
183 Data image;
184 javaloader->GetScreenshot(info, image);
187 // Convert to BMP format
188 Data bitmap(-1, GetTotalBitmapSize(info));
189 ScreenshotToBitmap(info, image, bitmap);
191 // Write BMP file
192 FILE *fp = fopen(filename, "wb");
193 if (fp == NULL) {
194 throw runtime_error(string("Can't open: ") + filename);
196 AutoClose ac(fp);
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);
210 else {
211 // filename does not contain .cod extension, use it as module name
212 module = fname;
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[])
224 INIT_I18N(PACKAGE);
226 cout.sync_with_stdio(true); // leave this on, since libusb uses
227 // stdio for debug messages
229 try {
231 uint32_t pin = 0;
232 bool list_siblings = false,
233 force_erase = false,
234 data_dump = false,
235 all_modules = false,
236 wipe_apps = true,
237 wipe_fs = true;
238 string password;
239 vector<string> params;
240 string busname;
241 string devname;
242 string iconvCharset;
243 Usb::EndpointPair epOverride;
245 // process command line options
246 for(;;) {
247 int cmd = getopt(argc, argv, "Aaifhsp:P:v");
248 if( cmd == -1 )
249 break;
251 switch( cmd )
253 case 'p': // Blackberry PIN
254 pin = strtoul(optarg, NULL, 16);
255 break;
257 case 'P': // Device password
258 password = optarg;
259 break;
261 case 'f': // turn on 'force' mode for erase
262 force_erase = true;
263 break;
265 case 's': // turn on listing of sibling modules
266 list_siblings = true;
267 break;
269 case 'v': // data dump on
270 data_dump = true;
271 break;
273 case 'A': // save all modules
274 all_modules = true;
275 break;
277 case 'a': // wipe apps only
278 wipe_fs = false;
279 break;
281 case 'i': // wipe filesystem
282 wipe_apps = false;
283 break;
285 case 'h': // help
286 default:
287 Usage();
288 return 0;
292 argc -= optind;
293 argv += optind;
295 if( argc < 1 ) {
296 cerr << "missing command" << endl;
297 Usage();
298 return 1;
301 // Fetch command from remaining arguments
302 string cmd = argv[0];
303 argc --;
304 argv ++;
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
312 // anything else.
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
318 Barry::Probe probe;
319 int activeDevice = probe.FindActive(pin);
320 if( activeDevice == -1 ) {
321 cerr << "No device selected, or PIN not found" << endl;
322 return 1;
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 ) {
336 JLDirectory dir;
337 javaloader.GetDirectory(dir, list_siblings);
338 cout << dir;
340 else if( cmd == CMD_LOAD ) {
341 if( params.size() == 0 ) {
342 cerr << "specify at least one .cod file to load" << endl;
343 Usage();
344 return 1;
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;
357 Usage();
358 return 1;
361 vector<string>::iterator i = params.begin(), end = params.end();
362 for( ; i != end; ++i ) {
363 cout << "erasing: " << (*i) << "... ";
364 if( force_erase )
365 javaloader.ForceErase((*i));
366 else
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;
374 Usage();
375 return 1;
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());
383 } else {
384 SetTime(&javaloader, NULL);
387 else if( cmd == CMD_EVENTLOG ) {
388 JLEventlog log;
389 javaloader.GetEventlog(log);
390 cout << 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 ) {
399 if( all_modules ) {
400 JLDirectory dir;
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;
411 Usage();
412 return 1;
414 else {
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 ) {
424 JLDeviceInfo info;
425 javaloader.DeviceInfo(info);
426 cout << info;
428 else if( cmd == CMD_WIPE ) {
429 cout
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 << "Continue with wipe? (yes/no) ";
435 string confirm;
436 getline(cin, confirm);
437 if( confirm == "yes" ) {
438 javaloader.Wipe(wipe_apps, wipe_fs);
440 else {
441 cout << "Response of 'yes' not received, aborting." << endl;
444 else if( cmd == CMD_RESETFACTORY ) {
445 cout
446 << "Use Caution: Resetting IT policy to factory defaults will\n"
447 << " also perform a filesystem wipe which will remove\n"
448 << " all data such as messages, contacts, etc.\n\n"
449 << "Continue with wipe? (yes/no) ";
450 string confirm;
451 getline(cin, confirm);
452 if( confirm == "yes" ) {
453 javaloader.ResetToFactory();
455 else {
456 cout << "Response of 'yes' not received, aborting." << endl;
459 else {
460 cerr << "invalid command \"" << cmd << "\"" << endl;
461 Usage();
462 return 1;
465 // Stop
466 javaloader.StopStream();
469 catch( Usb::Error &ue) {
470 std::cout << endl; // flush any normal output first
471 std::cerr << "Usb::Error caught: " << ue.what() << endl;
472 return 1;
474 catch( Barry::Error &se ) {
475 std::cout << endl;
476 std::cerr << "Barry::Error caught: " << se.what() << endl;
477 return 1;
479 catch( std::exception &e ) {
480 std::cout << endl;
481 std::cerr << "std::exception caught: " << e.what() << endl;
482 return 1;
485 return 0;