Skip to content

Commit

Permalink
Started on shell
Browse files Browse the repository at this point in the history
  • Loading branch information
GreySyntax committed Oct 16, 2010
1 parent 2fe99f7 commit 8fc6b11
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 20 deletions.
2 changes: 2 additions & 0 deletions BUGS
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
=== Known Bugs ===
:make backup is flakey
:LazyUSB::Write() doesnt work
:LazyUSB::Read() doesnt work
:IDevice::Shell() errors with LazyUSB::Read()
Binary file added iRecv++.zip
Binary file not shown.
1 change: 1 addition & 0 deletions include/IDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class IDevice {
void Reset();
bool SendCommand(const char* argv);
bool SendBuffer(char* data, int length, int* actual_length);
void Shell();
bool Upload(const char* file);

private:
Expand Down
7 changes: 4 additions & 3 deletions include/LazyUSB.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class LazyUSB {
LazyUSB();

bool ClaimInterface(int interface);
bool ClaimAltInterface(int interface);
bool ClaimAltInterface(int interface, int alt_interface);
bool Close();
bool Configure(int mode);

Expand All @@ -46,11 +46,12 @@ class LazyUSB {
void Reset();

int Transfer(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index, const char* data, uint16_t length, int timeout);
int Write(int endPoint, char *data, int length, int* actual_length, int timeout);
int Write(unsigned char endPoint, char *data, int length, int* actual_length, int timeout);
int Read(unsigned char endPoint, char *data, int length, int* actual_length, int timeout);

bool IsConnected();

private:
//private:
#if defined(WINDOWS)
struct usb_device_handle *handle;
#else
Expand Down
59 changes: 54 additions & 5 deletions src/IDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ void IDevice::Disconnect() {

bool IDevice::Exploit(const char* file) {

if (! USB.IsConnected()) {
return false;

/*
* I was going to add the 3.1.2 usb_control_msg(0x21) exploit here but,
* i couldnt really find a reason todo so.
*/

if (! USB.IsConnected()) {
Connect();
}


}

void IDevice::Reset() {
Expand Down Expand Up @@ -145,15 +150,59 @@ bool IDevice::SendCommand(const char* argv) {
}

bool IDevice::SendBuffer(char* data, int length, int* actual_length) {

if (USB.Write(4, data, length, actual_length, kUploadTimeout) != 0) {

if (! USB.IsConnected()) {
Connect();
}

if (USB.Write(0x04, data, length, actual_length, kUploadTimeout) != 0) {

return false;
}

return true;
}

void IDevice::Shell() {

if (! USB.IsConnected()) {
Connect();
}

if (! USB.IsConnected() || ! USB.Configure(1) || ! USB.ClaimAltInterface(1, 1)) {

return;
}

char* buffer = (char*)malloc(kBufferSize);

if (buffer == NULL) {

return;
}

//TODO Log to file

bool runShell = true;

while (runShell) {

int available = 0;

memset(buffer, 0, kBufferSize);
libusb_bulk_transfer(USB.handle, 0x81, (unsigned char*)buffer, 0x10000, &available, 500);
// USB.Read(0x81, buffer, kBufferSize, &available, kCommandTimeout);

if (available > 0) {

cout << &buffer[available];

free(buffer);
buffer = (char*)malloc(kBufferSize);
}
}
}

bool IDevice::Upload(const char* file) {

FILE* data = fopen(file, "rb");
Expand Down
70 changes: 61 additions & 9 deletions src/LazyUSB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,40 @@ bool LazyUSB::ClaimInterface(int interface) {
cout << "[LazyUSB::ClaimInterface] Failed to claim usb interface (" << interface << ")." << endl;
return false;
}


cout << "[LazyUSB::ClaimInterface] Claimed interface (" << interface << ")." << endl;
return true;

}

bool LazyUSB::ClaimAltInterface(int interface) {
bool LazyUSB::ClaimAltInterface(int interface, int alt_interface) {

if (handle == NULL) {

cout << "[LazyUSB::ClaimAltInterface] Device handle not initialized." << endl;
return false;
}

cout << "[LazyUSB::ClaimAltInterface] Claiming interface (" << interface << ", " << alt_interface << ")." << endl;
#if defined(WINDOWS)

if (usb_set_altinterface(handle, interface) < 0) {

cout << "[LazyUSB::ClaimAltInterface] Failed to claim alt interface (" << interface << ")." << endl;
return false;
}

return true;
#else
//Not supported directly -- AVOID USING IF POSSIBLE
return ClaimInterface(interface);

if (libusb_set_interface_alt_setting(handle, interface, alt_interface) < 0) {

cout << "[LazyUSB::ClaimAltInterface] Failed to claim alt interface (" << interface << ")." << endl;
return false;
}

#endif

return true;
}


Expand Down Expand Up @@ -102,15 +112,35 @@ bool LazyUSB::Configure(int mode) {
return false;
}


#if defined(WINDOWS)

//Since we cant easily check the configuration using this old api, lets just do it anyway.
if (usb_set_configuration(handle, mode) < 0) {

cout << "[LazyUSB::Configure] Failed to set confiuration (" << mode << ")" << endl;
return false;
}

#else

/*int configuration = 0;
libusb_get_configuration(handle, &configuration);
if (configuration == mode) {
cout << "[LazyUSB::Configure] Requested configuration allready set." << endl;
return true;
}*/

if (libusb_set_configuration(handle, mode) < 0) {
#endif
cout << "[LazyUSB::Configure] Failed to set confiuration (" << mode << ")" << endl;
return false;
}

#endif

return true;
}

Expand Down Expand Up @@ -204,7 +234,29 @@ int LazyUSB::Transfer(uint8_t requestType, uint8_t request, uint16_t value, uint
return res;
}

int LazyUSB::Write(int endPoint, char* data, int length, int* actual_length, int timeout) {
int LazyUSB::Read(unsigned char endPoint, char* data, int length, int* actual_length, int timeout) {

if (handle == NULL) {

cout << "[LazyUSB::Read] Device handle not initialized." << endl;
return -1;
}

int res = 0;

#if defined(WINDOWS)
res = usb_bulk_read(handle, (int*)endPoint, data, length, timeout);
#else
res = libusb_bulk_transfer(handle, endPoint, (unsigned char*)data, length, actual_length, timeout);
#endif

cout << "[LazyUSB::Read] Result: #" << res << endl;

return res;
}


int LazyUSB::Write(unsigned char endPoint, char* data, int length, int* actual_length, int timeout) {

if (handle == NULL) {

Expand All @@ -215,9 +267,9 @@ int LazyUSB::Write(int endPoint, char* data, int length, int* actual_length, int
int res = 0;

#if defined(WINDOWS)
res = usb_bulk_write(handle, endPoint, data, length, timeout);
res = usb_bulk_write(handle, (int*)endPoint, data, length, timeout);
#else
res = libusb_bulk_transfer(handle, (unsigned char)endPoint, (unsigned char*)data, length, actual_length, timeout);
res = libusb_bulk_transfer(handle, endPoint, (unsigned char*)data, length, actual_length, timeout);
#endif

cout << "[LazyUSB::Write] Result: #" << res << endl;
Expand Down
6 changes: 3 additions & 3 deletions src/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ int main(int argc, char *argv[]) {
if (Device.Connect()) {

/*
cout << "AutoBoot: " << endl;
cout << "AutoBoot: " << endl;
Device.SendCommand("getenv auto-boot");
Device.SendCommand("setenv auto-boot true");
Device.SendCommand("saveenv");
Device.SendCommand("reboot");
*/
Device.Upload("../iBEC.n90ap.RELEASE.dfu");
*/
Device.Shell();
Device.Disconnect();
}

Expand Down

0 comments on commit 8fc6b11

Please sign in to comment.