00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "common.h"
00023 #include "probe.h"
00024 #include "usbwrap.h"
00025 #include "data.h"
00026 #include "endian.h"
00027 #include "error.h"
00028 #include "debug.h"
00029 #include "packet.h"
00030 #include "socket.h"
00031 #include "protocol.h"
00032 #include "record-internal.h"
00033 #include "strnlen.h"
00034 #include <iomanip>
00035 #include <errno.h>
00036 #include <string.h>
00037
00038 using namespace Usb;
00039
00040 namespace Barry {
00041
00042 unsigned char Intro_Sends[][32] = {
00043
00044 { 0x00, 0x00, 0x10, 0x00, 0x01, 0xff, 0x00, 0x00,
00045 0xa8, 0x18, 0xda, 0x8d, 0x6c, 0x02, 0x00, 0x00 }
00046 };
00047
00048
00049 unsigned char Intro_Receives[][32] = {
00050
00051 { 0x00, 0x00, 0x10, 0x00, 0x02, 0xff, 0x00, 0x00,
00052 0xa8, 0x18, 0xda, 0x8d, 0x6c, 0x02, 0x00, 0x00 }
00053 };
00054
00055 namespace {
00056
00057 unsigned int GetSize(const unsigned char *packet)
00058 {
00059 uint16_t size = *((uint16_t *)&packet[2]);
00060 return btohs(size);
00061 }
00062
00063 bool Intro(int IntroIndex, const EndpointPair &ep, Device &dev, Data &response)
00064 {
00065 dev.BulkWrite(ep.write, Intro_Sends[IntroIndex],
00066 GetSize(Intro_Sends[IntroIndex]));
00067 try {
00068 dev.BulkRead(ep.read, response, 500);
00069 }
00070 catch( Usb::Timeout &to ) {
00071 ddout("BulkRead: " << to.what());
00072 return false;
00073 }
00074 ddout("BulkRead (" << (unsigned int)ep.read << "):\n" << response);
00075 return true;
00076 }
00077
00078 }
00079
00080
00081 bool Probe::CheckSize(const Data &data, unsigned int required)
00082 {
00083 const unsigned char *pd = data.GetData();
00084
00085 if( GetSize(pd) != (unsigned int) data.GetSize() ||
00086 data.GetSize() < required ||
00087 pd[4] != SB_COMMAND_FETCHED_ATTRIBUTE )
00088 {
00089 dout("Probe: Parse data failure: GetSize(pd): " << GetSize(pd)
00090 << ", data.GetSize(): " << data.GetSize()
00091 << ", pd[4]: " << (unsigned int) pd[4]);
00092 return false;
00093 }
00094
00095 return true;
00096 }
00097
00098 bool Probe::ParsePIN(const Data &data, uint32_t &pin)
00099 {
00100
00101 const unsigned char *pd = data.GetData();
00102
00103 if( !CheckSize(data, 0x14) )
00104 return false;
00105
00106
00107 pin = btohl(*((uint32_t *) &pd[16]));
00108
00109 return true;
00110 }
00111
00112 bool Probe::ParseDesc(const Data &data, std::string &desc)
00113 {
00114 if( !CheckSize(data, 29) )
00115 return false;
00116
00117
00118 const char *d = (const char*) &data.GetData()[28];
00119 int maxlen = data.GetSize() - 28;
00120 desc.assign(d, strnlen(d, maxlen));
00121
00122 return true;
00123 }
00124
00125 Probe::Probe(const char *busname, const char *devname)
00126 : m_fail_count(0)
00127 {
00128
00129 if( busname && !strlen(busname) )
00130 busname = 0;
00131 if( devname && !strlen(devname) )
00132 devname = 0;
00133
00134
00135 ProbeMatching(VENDOR_RIM, PRODUCT_RIM_BLACKBERRY, busname, devname);
00136
00137
00138
00139
00140
00141
00142
00143 ProbeMatching(VENDOR_RIM, PRODUCT_RIM_PEARL_DUAL, busname, devname);
00144
00145
00146 ProbeMatching(VENDOR_RIM, PRODUCT_RIM_PEARL_8120, busname, devname);
00147 }
00148
00149 void Probe::ProbeMatching(int vendor, int product,
00150 const char *busname, const char *devname)
00151 {
00152 Usb::DeviceIDType devid;
00153
00154 Match match(vendor, product, busname, devname);
00155 while( match.next_device(&devid) ) try {
00156 ProbeDevice(devid);
00157 }
00158 catch( Usb::Error &e ) {
00159 dout("Usb::Error exception caught: " << e.what());
00160 if( e.libusb_errcode() == -EBUSY ) {
00161 m_fail_count++;
00162 m_fail_msgs.push_back(e.what());
00163 }
00164 else {
00165 throw;
00166 }
00167 }
00168 }
00169
00170 void Probe::ProbeDevice(Usb::DeviceIDType devid)
00171 {
00172
00173 DeviceDiscovery discover(devid);
00174 ConfigDesc &config = discover.configs[BLACKBERRY_CONFIGURATION];
00175
00176
00177 InterfaceDiscovery::base_type::iterator idi = config.interfaces.begin();
00178 for( ; idi != config.interfaces.end(); idi++ ) {
00179 if( idi->second.desc.bInterfaceClass == BLACKBERRY_DB_CLASS )
00180 break;
00181 }
00182 if( idi == config.interfaces.end() ) {
00183 dout("Probe: Interface with BLACKBERRY_DB_CLASS ("
00184 << BLACKBERRY_DB_CLASS << ") not found.");
00185 return;
00186 }
00187
00188 unsigned char InterfaceNumber = idi->second.desc.bInterfaceNumber;
00189 dout("Probe: using InterfaceNumber: " << (unsigned int) InterfaceNumber);
00190
00191
00192 EndpointDiscovery &ed = config.interfaces[InterfaceNumber].endpoints;
00193 if( !ed.IsValid() || ed.GetEndpointPairs().size() == 0 ) {
00194 dout("Probe: endpoint invalid. ed.IsValud() == "
00195 << (ed.IsValid() ? "true" : "false")
00196 << ", ed.GetEndpointPairs().size() == "
00197 << ed.GetEndpointPairs().size());
00198 return;
00199 }
00200
00201 ProbeResult result;
00202 result.m_dev = devid;
00203 result.m_interface = InterfaceNumber;
00204 result.m_zeroSocketSequence = 0;
00205
00206
00207 Device dev(devid);
00208
00209
00210
00211
00212 unsigned char cfg;
00213 if( !dev.GetConfiguration(cfg) )
00214 throw Usb::Error(dev.GetLastError(),
00215 "Probe: GetConfiguration failed");
00216 if( cfg != BLACKBERRY_CONFIGURATION ) {
00217 if( !dev.SetConfiguration(BLACKBERRY_CONFIGURATION) )
00218 throw Usb::Error(dev.GetLastError(),
00219 "Probe: SetConfiguration failed");
00220 }
00221
00222
00223 Interface iface(dev, InterfaceNumber);
00224
00225
00226
00227
00228
00229 size_t i;
00230 for(i = ed.GetEndpointPairs().size() > 1 ? 1 : 0;
00231 i < ed.GetEndpointPairs().size();
00232 i++ )
00233 {
00234 const EndpointPair &ep = ed.GetEndpointPairs()[i];
00235 if( ep.type == USB_ENDPOINT_TYPE_BULK ) {
00236
00237 uint32_t pin;
00238 uint8_t zeroSocketSequence;
00239 std::string desc;
00240 if( ProbePair(dev, ep, pin, desc, zeroSocketSequence) ) {
00241 result.m_ep = ep;
00242 result.m_pin = pin;
00243 result.m_description = desc;
00244 result.m_zeroSocketSequence = zeroSocketSequence;
00245 break;
00246 }
00247 }
00248 else {
00249 dout("Probe: Skipping non-bulk endpoint pair (offset: "
00250 << i-1 << ") ");
00251 }
00252 }
00253
00254
00255 i++;
00256 if( i < ed.GetEndpointPairs().size() ) {
00257 const EndpointPair &ep = ed.GetEndpointPairs()[i];
00258 if( ProbeModem(dev, ep) ) {
00259 result.m_epModem = ep;
00260 }
00261 }
00262
00263
00264 if( result.m_ep.IsComplete() ) {
00265 m_results.push_back(result);
00266 ddout("Using ReadEndpoint: " << (unsigned int)result.m_ep.read);
00267 ddout(" WriteEndpoint: " << (unsigned int)result.m_ep.write);
00268 }
00269 else {
00270 ddout("Unable to discover endpoint pair for one device.");
00271 }
00272 }
00273
00274 bool Probe::ProbePair(Usb::Device &dev,
00275 const Usb::EndpointPair &ep,
00276 uint32_t &pin,
00277 std::string &desc,
00278 uint8_t &zeroSocketSequence)
00279 {
00280 dev.ClearHalt(ep.read);
00281 dev.ClearHalt(ep.write);
00282
00283 Data data;
00284 dev.BulkDrain(ep.read);
00285 if( !Intro(0, ep, dev, data) ) {
00286 dout("Probe: Intro(0) failed");
00287 return false;
00288 }
00289
00290 SocketZero socket(dev, ep.write, ep.read);
00291
00292 Data send, receive;
00293 ZeroPacket packet(send, receive);
00294
00295
00296 packet.GetAttribute(SB_OBJECT_INITIAL_UNKNOWN,
00297 SB_ATTR_INITIAL_UNKNOWN);
00298 socket.Send(packet);
00299
00300
00301 packet.GetAttribute(SB_OBJECT_PROFILE, SB_ATTR_PROFILE_PIN);
00302 socket.Send(packet);
00303 if( packet.ObjectID() != SB_OBJECT_PROFILE ||
00304 packet.AttributeID() != SB_ATTR_PROFILE_PIN ||
00305 !ParsePIN(receive, pin) )
00306 {
00307 dout("Probe: unable to fetch PIN");
00308 return false;
00309 }
00310
00311
00312 packet.GetAttribute(SB_OBJECT_PROFILE, SB_ATTR_PROFILE_DESC);
00313 socket.Send(packet);
00314
00315 if(
00316 packet.AttributeID() != SB_ATTR_PROFILE_DESC ||
00317 !ParseDesc(receive, desc) )
00318 {
00319 dout("Probe: unable to fetch description");
00320 }
00321
00322
00323 for( uint16_t attr = 5; attr < 9; attr++ ) {
00324 packet.GetAttribute(SB_OBJECT_SOCKET_UNKNOWN, attr);
00325 socket.Send(packet);
00326
00327
00328 }
00329
00330
00331 zeroSocketSequence = socket.GetZeroSocketSequence();
00332 return true;
00333 }
00334
00335 bool Probe::ProbeModem(Usb::Device &dev, const Usb::EndpointPair &ep)
00336 {
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 return true;
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 }
00371
00372 int Probe::FindActive(uint32_t pin) const
00373 {
00374 for( int i = 0; i < GetCount(); i++ ) {
00375 if( Get(i).m_pin == pin )
00376 return i;
00377 }
00378 if( pin == 0 ) {
00379
00380 if( GetCount() == 1 )
00381 return 0;
00382 }
00383
00384
00385 return -1;
00386 }
00387
00388 void ProbeResult::DumpAll(std::ostream &os) const
00389 {
00390 os << *this
00391 << ", Interface: 0x" << std::hex << (unsigned int) m_interface
00392 << ", Endpoints: (read: 0x" << std::hex << (unsigned int) m_ep.read
00393 << ", write: 0x" << std::hex << (unsigned int) m_ep.write
00394 << ", type: 0x" << std::hex << (unsigned int) m_ep.type
00395 << ", ZeroSocketSequence: 0x" << std::hex << (unsigned int) m_zeroSocketSequence;
00396 }
00397
00398 std::ostream& operator<< (std::ostream &os, const ProbeResult &pr)
00399 {
00400 os << "Device ID: " << pr.m_dev
00401 << std::hex << ". PIN: " << pr.m_pin
00402 << ", Description: " << pr.m_description;
00403 return os;
00404 }
00405
00406 }
00407