From 520c99d33e18009660f30bd38b31a2f1cd432fe6 Mon Sep 17 00:00:00 2001 From: Joshua Hill Date: Thu, 13 May 2010 08:58:46 -0400 Subject: Implemented command line argument parsing and irecv_command() function. --- src/irecovery.c | 60 ++++++++++++++++++++++++++++++++++++++++++------------ src/libirecovery.c | 34 +++++++++++++++++++++++++++---- 2 files changed, 77 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/irecovery.c b/src/irecovery.c index 90f975b..51320a5 100644 --- a/src/irecovery.c +++ b/src/irecovery.c @@ -17,9 +17,50 @@ **/ #include +#include +#include #include +enum { + kResetDevice, kSendCommand +}; + +void print_usage() { + printf("iRecovery - iDevice Recovery Utility\n"); + printf("Usage: ./irecovery [args]\n"); + exit(1); +} + int main(int argc, char** argv) { + int opt = 0; + int action = 0; + char* argument = NULL; + if(argc == 1) print_usage(); + while ((opt = getopt(argc, argv, "dhrc:f:")) > 0) { + switch (opt) { + case 'd': + irecv_set_debug(1); + break; + + case 'h': + print_usage(); + break; + + case 'r': + action = kResetDevice; + break; + + case 'c': + argument = optarg; + action = kSendCommand; + break; + + default: + fprintf(stderr, "Unknown argument\n"); + break; + } + } + irecv_device* device = NULL; if(irecv_init(&device) < 0) { fprintf(stderr, "Unable to initialize libirecovery\n"); @@ -31,27 +72,20 @@ int main(int argc, char** argv) { return -1; } - switch (device->mode) { - case kRecoveryMode: - printf("Found device in recovery mode\n"); + switch(action) { + case kResetDevice: + irecv_reset(device); break; - case kDfuMode: - printf("Found device in DFU mode\n"); - break; - - case kKernelMode: - printf("Found device in kernel mode\n"); + case kSendCommand: + irecv_command(device, argument); break; default: - printf("No device found\n"); + fprintf(stderr, "Unknown action\n"); break; } - printf("Sending USB reset...\n"); - irecv_reset(device); - irecv_exit(device); return 0; } diff --git a/src/libirecovery.c b/src/libirecovery.c index a5b5c84..85967ec 100644 --- a/src/libirecovery.c +++ b/src/libirecovery.c @@ -23,14 +23,13 @@ #include "libirecovery.h" -static unsigned int debug = 1; +static unsigned int irecv_debug = 0; int irecv_init(irecv_device** p_device) { struct libusb_context* usb_context = NULL; libusb_init(&usb_context); - if (debug) - libusb_set_debug(usb_context, 3); + if (irecv_debug) libusb_set_debug(usb_context, 3); irecv_device* device = (irecv_device*) malloc(sizeof(irecv_device)); if (device == NULL) { @@ -60,7 +59,7 @@ int irecv_open(irecv_device* device) { for (i = 0; i < usb_device_count; i++) { usb_device = usb_device_list[i]; libusb_get_device_descriptor(usb_device, &usb_descriptor); - if (usb_descriptor.idVendor == kAppleVendorId) { + if (usb_descriptor.idVendor == kAppleId) { libusb_open(usb_device, &usb_handle); if (usb_handle == NULL) { @@ -115,3 +114,30 @@ int irecv_exit(irecv_device* device) { return IRECV_SUCCESS; } + +void irecv_set_debug(int level) { + printf("Debug has been set to %d\n", level); + irecv_debug = level; +} + +int irecv_command(irecv_device* device, const char* command) { + if(device == NULL || device->handle == NULL) { + return IRECV_ERROR_NO_DEVICE; + } + + ssize_t length = strlen(command); + if(length >= 0x100) { + return IRECV_ERROR_INVALID_INPUT; + } + + int ret = libusb_control_transfer(device->handle, 0x40, 0, 0, 0, (unsigned char*) command, length+1, 100); + if(ret < 0) { + return IRECV_ERROR_UNKNOWN; + } + + return IRECV_SUCCESS; +} + +int irecv_upload(irecv_device* device, const char* filename) { + return IRECV_SUCCESS; +} -- cgit v1.1-32-gdbae