Created
January 19, 2018 01:30
-
-
Save lhchavez/31b0ad864e0490f2e3b29528ab64da36 to your computer and use it in GitHub Desktop.
Android Debug Bridge Debugger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <errno.h> | |
| #include <fcntl.h> | |
| #include <linux/usb/ch9.h> | |
| #include <linux/usbdevice_fs.h> | |
| #include <sys/ioctl.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| #include <cstdio> | |
| #include <iostream> | |
| constexpr uint8_t kCNXN[] = { | |
| 0x43, 0x4e, 0x58, 0x4e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x04, | |
| 0x00, 0x1b, 0x00, 0x00, 0x00, 0x4d, 0x0a, 0x00, 0x00, 0xbc, 0xb1, | |
| 0xa7, 0xb1, 0x68, 0x6f, 0x73, 0x74, 0x3a, 0x3a, 0x66, 0x65, 0x61, | |
| 0x74, 0x75, 0x72, 0x65, 0x73, 0x3d, 0x63, 0x6d, 0x64, 0x2c, 0x73, | |
| 0x68, 0x65, 0x6c, 0x6c, 0x5f, 0x76, 0x32, | |
| }; | |
| ssize_t usb_bulk_write(int fd, const void *data, size_t len) { | |
| usbdevfs_urb urb = { | |
| .type = USBDEVFS_URB_TYPE_BULK, .endpoint = 0x01, .status = -1, | |
| }; | |
| urb.buffer = const_cast<void *>(data); | |
| urb.buffer_length = len; | |
| if (ioctl(fd, USBDEVFS_SUBMITURB, &urb) == -1) { | |
| perror("ioctl(USBDEVFS_SUBMITURB)"); | |
| return -1; | |
| } | |
| usbdevfs_urb* result = nullptr; | |
| if (ioctl(fd, USBDEVFS_REAPURB, &result) == -1) { | |
| perror("ioctl(USBDEVFS_REAPURB)"); | |
| return -1; | |
| } | |
| if (result != &urb) { | |
| fprintf(stderr, "Unknown urb!\n"); | |
| return -1; | |
| } | |
| if (urb.status != 0) { | |
| errno = -urb.status; | |
| perror("write"); | |
| return -1; | |
| } | |
| return urb.actual_length; | |
| } | |
| ssize_t usb_bulk_read(int fd, const void *data, size_t len) { | |
| usbdevfs_urb urb = { | |
| .type = USBDEVFS_URB_TYPE_BULK, .endpoint = 0x81, .status = -1, | |
| }; | |
| urb.buffer = const_cast<void *>(data); | |
| urb.buffer_length = len; | |
| if (ioctl(fd, USBDEVFS_SUBMITURB, &urb) == -1) { | |
| perror("ioctl(USBDEVFS_SUBMITURB)"); | |
| return -1; | |
| } | |
| usbdevfs_urb* result = nullptr; | |
| if (ioctl(fd, USBDEVFS_REAPURB, &result) == -1) { | |
| perror("ioctl(USBDEVFS_REAPURB)"); | |
| return -1; | |
| } | |
| if (result != &urb) { | |
| fprintf(stderr, "Unknown urb!\n"); | |
| return -1; | |
| } | |
| if (urb.status != 0) { | |
| errno = -urb.status; | |
| perror("read"); | |
| return -1; | |
| } | |
| return urb.actual_length; | |
| } | |
| int main(int argc, char* argv[]) { | |
| int fd = open(argv[1], O_RDWR | O_CLOEXEC); | |
| if (fd == -1) { | |
| perror("open"); | |
| return 1; | |
| } | |
| int interface = 0; | |
| if (ioctl(fd, USBDEVFS_CLAIMINTERFACE, &interface) != 0) { | |
| perror("ioctl(USBDEVFS_CLAIMINTERFACE)"); | |
| return 1; | |
| } | |
| ssize_t written = usb_bulk_write(fd, kCNXN, sizeof(kCNXN)); | |
| if (written <= 0) { | |
| perror("usb_bulk_write"); | |
| return 1; | |
| } | |
| printf("Wrote %zd / %zd\n", written, sizeof(kCNXN)); | |
| char buf[4096]; | |
| ssize_t read_bytes = usb_bulk_read(fd, buf, sizeof(buf)); | |
| if (read_bytes <= 0) { | |
| perror("usb_bulk_read"); | |
| return 1; | |
| } | |
| printf("Read %zd bytes!\n", read_bytes); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment