diff options
Diffstat (limited to 'src/irecovery.c')
| -rw-r--r-- | src/irecovery.c | 90 |
1 files changed, 38 insertions, 52 deletions
diff --git a/src/irecovery.c b/src/irecovery.c index 56b0453..00db72d 100644 --- a/src/irecovery.c +++ b/src/irecovery.c | |||
| @@ -35,12 +35,12 @@ static unsigned int verbose = 0; | |||
| 35 | 35 | ||
| 36 | void print_shell_usage() { | 36 | void print_shell_usage() { |
| 37 | printf("Usage:\n"); | 37 | printf("Usage:\n"); |
| 38 | printf("\t/upload <file>\tSend file to device.\n"); | 38 | printf("\t/upload <file>\tSend file to client.\n"); |
| 39 | printf("\t/help\t\tShow this help.\n"); | 39 | printf("\t/help\t\tShow this help.\n"); |
| 40 | printf("\t/exit\t\tExit interactive shell.\n"); | 40 | printf("\t/exit\t\tExit interactive shell.\n"); |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | void parse_command(irecv_device_t* device, unsigned char* command, unsigned int size) { | 43 | void parse_command(irecv_client_t client, unsigned char* command, unsigned int size) { |
| 44 | char* cmd = strtok(strdup(command), " "); | 44 | char* cmd = strtok(strdup(command), " "); |
| 45 | debug("Executing %s %s\n", cmd, command); | 45 | debug("Executing %s %s\n", cmd, command); |
| 46 | if(!strcmp(cmd, "/exit")) { | 46 | if(!strcmp(cmd, "/exit")) { |
| @@ -50,25 +50,18 @@ void parse_command(irecv_device_t* device, unsigned char* command, unsigned int | |||
| 50 | if(!strcmp(cmd, "/help")) { | 50 | if(!strcmp(cmd, "/help")) { |
| 51 | print_shell_usage(); | 51 | print_shell_usage(); |
| 52 | } else | 52 | } else |
| 53 | |||
| 54 | if(!strcmp(cmd, "/reconnect")) { | ||
| 55 | char* uuid = strdup(device->uuid); | ||
| 56 | irecv_close(device); | ||
| 57 | irecv_open(device, uuid); | ||
| 58 | free(uuid); | ||
| 59 | } else | ||
| 60 | 53 | ||
| 61 | if(!strcmp(cmd, "/upload")) { | 54 | if(!strcmp(cmd, "/upload")) { |
| 62 | char* filename = strtok(NULL, " "); | 55 | char* filename = strtok(NULL, " "); |
| 63 | debug("Sending %s\n", filename); | 56 | debug("Sending %s\n", filename); |
| 64 | if(filename != NULL) { | 57 | if(filename != NULL) { |
| 65 | irecv_send_file(device, filename); | 58 | irecv_send_file(client, filename); |
| 66 | } | 59 | } |
| 67 | } | 60 | } |
| 68 | free(cmd); | 61 | free(cmd); |
| 69 | } | 62 | } |
| 70 | 63 | ||
| 71 | int recv_callback(irecv_device_t* device, unsigned char* data, int size) { | 64 | int recv_callback(irecv_client_t client, unsigned char* data, int size) { |
| 72 | int i = 0; | 65 | int i = 0; |
| 73 | for(i = 0; i < size; i++) { | 66 | for(i = 0; i < size; i++) { |
| 74 | printf("%c", data[i]); | 67 | printf("%c", data[i]); |
| @@ -76,23 +69,23 @@ int recv_callback(irecv_device_t* device, unsigned char* data, int size) { | |||
| 76 | return size; | 69 | return size; |
| 77 | } | 70 | } |
| 78 | 71 | ||
| 79 | int send_callback(irecv_device_t* device, unsigned char* command, int size) { | 72 | int send_callback(irecv_client_t client, unsigned char* command, int size) { |
| 80 | irecv_error_t error = 0; | 73 | irecv_error_t error = 0; |
| 81 | if(command[0] == '/') { | 74 | if(command[0] == '/') { |
| 82 | parse_command(device, command, size); | 75 | parse_command(client, command, size); |
| 83 | return 0; | 76 | return 0; |
| 84 | } | 77 | } |
| 85 | 78 | ||
| 86 | if(strstr(command, "getenv") != NULL) { | 79 | if(strstr(command, "getenv") != NULL) { |
| 87 | unsigned char* value = NULL; | 80 | unsigned char* value = NULL; |
| 88 | error = irecv_send_command(device, command); | 81 | error = irecv_send_command(client, command); |
| 89 | if(error != IRECV_SUCCESS) { | 82 | if(error != IRECV_E_SUCCESS) { |
| 90 | debug("%s\n", irecv_strerror(error)); | 83 | debug("%s\n", irecv_strerror(error)); |
| 91 | return error; | 84 | return error; |
| 92 | } | 85 | } |
| 93 | 86 | ||
| 94 | error = irecv_getenv(device, &value); | 87 | error = irecv_getenv(client, &value); |
| 95 | if(error != IRECV_SUCCESS) { | 88 | if(error != IRECV_E_SUCCESS) { |
| 96 | debug("%s\n", irecv_strerror(error)); | 89 | debug("%s\n", irecv_strerror(error)); |
| 97 | return error; | 90 | return error; |
| 98 | } | 91 | } |
| @@ -103,8 +96,8 @@ int send_callback(irecv_device_t* device, unsigned char* command, int size) { | |||
| 103 | } | 96 | } |
| 104 | 97 | ||
| 105 | if(!strcmp(command, "reboot")) { | 98 | if(!strcmp(command, "reboot")) { |
| 106 | error = irecv_send_command(device, command); | 99 | error = irecv_send_command(client, command); |
| 107 | if(error != IRECV_SUCCESS) { | 100 | if(error != IRECV_E_SUCCESS) { |
| 108 | debug("%s\n", irecv_strerror(error)); | 101 | debug("%s\n", irecv_strerror(error)); |
| 109 | return error; | 102 | return error; |
| 110 | } | 103 | } |
| @@ -124,22 +117,22 @@ void append_command_to_history(char* cmd) { | |||
| 124 | write_history(FILE_HISTORY_PATH); | 117 | write_history(FILE_HISTORY_PATH); |
| 125 | } | 118 | } |
| 126 | 119 | ||
| 127 | void init_shell(irecv_device_t* device) { | 120 | void init_shell(irecv_client_t client) { |
| 128 | irecv_error_t error = 0; | 121 | irecv_error_t error = 0; |
| 129 | load_command_history(); | 122 | load_command_history(); |
| 130 | irecv_set_sender(device, &send_callback); | 123 | irecv_set_sender(client, &send_callback); |
| 131 | irecv_set_receiver(device, &recv_callback); | 124 | irecv_set_receiver(client, &recv_callback); |
| 132 | while(!quit) { | 125 | while(!quit) { |
| 133 | error = irecv_receive(device); | 126 | error = irecv_receive(client); |
| 134 | if(error != IRECV_SUCCESS) { | 127 | if(error != IRECV_E_SUCCESS) { |
| 135 | debug("%s\n", irecv_strerror(error)); | 128 | debug("%s\n", irecv_strerror(error)); |
| 136 | break; | 129 | break; |
| 137 | } | 130 | } |
| 138 | 131 | ||
| 139 | char* cmd = readline("> "); | 132 | char* cmd = readline("> "); |
| 140 | if(cmd && *cmd) { | 133 | if(cmd && *cmd) { |
| 141 | error = irecv_send(device, cmd); | 134 | error = irecv_send(client, cmd); |
| 142 | if(error != IRECV_SUCCESS) { | 135 | if(error != IRECV_E_SUCCESS) { |
| 143 | quit = 1; | 136 | quit = 1; |
| 144 | } | 137 | } |
| 145 | 138 | ||
| @@ -153,16 +146,17 @@ void print_usage() { | |||
| 153 | printf("iRecovery - iDevice Recovery Utility\n"); | 146 | printf("iRecovery - iDevice Recovery Utility\n"); |
| 154 | printf("Usage: ./irecovery [args]\n"); | 147 | printf("Usage: ./irecovery [args]\n"); |
| 155 | printf("\t-v\t\tStart irecovery in verbose mode.\n"); | 148 | printf("\t-v\t\tStart irecovery in verbose mode.\n"); |
| 156 | printf("\t-u <uuid>\ttarget specific device by its 40-digit device UUID\n"); | 149 | printf("\t-u <uuid>\ttarget specific client by its 40-digit client UUID\n"); |
| 157 | printf("\t-c <cmd>\tSend command to device.\n"); | 150 | printf("\t-c <cmd>\tSend command to client.\n"); |
| 158 | printf("\t-f <file>\tSend file to device.\n"); | 151 | printf("\t-f <file>\tSend file to client.\n"); |
| 159 | printf("\t-h\t\tShow this help.\n"); | 152 | printf("\t-h\t\tShow this help.\n"); |
| 160 | printf("\t-r\t\tReset device.\n"); | 153 | printf("\t-r\t\tReset client.\n"); |
| 161 | printf("\t-s\t\tStart interactive shell.\n"); | 154 | printf("\t-s\t\tStart interactive shell.\n"); |
| 162 | exit(1); | 155 | exit(1); |
| 163 | } | 156 | } |
| 164 | 157 | ||
| 165 | int main(int argc, char** argv) { | 158 | int main(int argc, char** argv) { |
| 159 | int i = 0; | ||
| 166 | int opt = 0; | 160 | int opt = 0; |
| 167 | int action = 0; | 161 | int action = 0; |
| 168 | char* argument = NULL; | 162 | char* argument = NULL; |
| @@ -179,14 +173,14 @@ int main(int argc, char** argv) { | |||
| 179 | print_usage(); | 173 | print_usage(); |
| 180 | break; | 174 | break; |
| 181 | 175 | ||
| 182 | case 'r': | ||
| 183 | action = kResetDevice; | ||
| 184 | break; | ||
| 185 | |||
| 186 | case 'u': | 176 | case 'u': |
| 187 | uuid = optarg; | 177 | uuid = optarg; |
| 188 | break; | 178 | break; |
| 189 | 179 | ||
| 180 | case 'r': | ||
| 181 | action = kResetDevice; | ||
| 182 | break; | ||
| 183 | |||
| 190 | case 's': | 184 | case 's': |
| 191 | action = kStartShell; | 185 | action = kStartShell; |
| 192 | break; | 186 | break; |
| @@ -207,45 +201,37 @@ int main(int argc, char** argv) { | |||
| 207 | } | 201 | } |
| 208 | } | 202 | } |
| 209 | 203 | ||
| 210 | irecv_device_t* device = irecv_init(); | 204 | irecv_client_t client = NULL; |
| 211 | if(device == NULL) { | ||
| 212 | fprintf(stderr, "Unable to initialize libirecovery\n"); | ||
| 213 | return -1; | ||
| 214 | } | ||
| 215 | if(verbose) irecv_set_debug(device, verbose); | ||
| 216 | |||
| 217 | int i = 0; | ||
| 218 | for(i = 0; i <= 5; i++) { | 205 | for(i = 0; i <= 5; i++) { |
| 219 | debug("Attempting to connect... "); | 206 | debug("Attempting to connect... \n"); |
| 220 | 207 | ||
| 221 | if(irecv_open(device, uuid) < 0) sleep(1); | 208 | if(irecv_open(&client, uuid) != IRECV_E_SUCCESS) sleep(1); |
| 222 | else break; | 209 | else break; |
| 223 | 210 | ||
| 224 | debug("failed. No recovery device found.\n"); | ||
| 225 | |||
| 226 | if(i == 5) { | 211 | if(i == 5) { |
| 227 | irecv_exit(device); | ||
| 228 | return -1; | 212 | return -1; |
| 229 | } | 213 | } |
| 230 | } | 214 | } |
| 231 | 215 | ||
| 216 | if(verbose) irecv_set_debug(client, verbose); | ||
| 217 | |||
| 232 | switch(action) { | 218 | switch(action) { |
| 233 | case kResetDevice: | 219 | case kResetDevice: |
| 234 | irecv_reset(device); | 220 | irecv_reset(client); |
| 235 | break; | 221 | break; |
| 236 | 222 | ||
| 237 | case kSendFile: | 223 | case kSendFile: |
| 238 | error = irecv_send_file(device, argument); | 224 | error = irecv_send_file(client, argument); |
| 239 | debug("%s\n", irecv_strerror(error)); | 225 | debug("%s\n", irecv_strerror(error)); |
| 240 | break; | 226 | break; |
| 241 | 227 | ||
| 242 | case kSendCommand: | 228 | case kSendCommand: |
| 243 | error = irecv_send_command(device, argument); | 229 | error = irecv_send_command(client, argument); |
| 244 | debug("%s\n", irecv_strerror(error)); | 230 | debug("%s\n", irecv_strerror(error)); |
| 245 | break; | 231 | break; |
| 246 | 232 | ||
| 247 | case kStartShell: | 233 | case kStartShell: |
| 248 | init_shell(device); | 234 | init_shell(client); |
| 249 | break; | 235 | break; |
| 250 | 236 | ||
| 251 | default: | 237 | default: |
| @@ -253,7 +239,7 @@ int main(int argc, char** argv) { | |||
| 253 | break; | 239 | break; |
| 254 | } | 240 | } |
| 255 | 241 | ||
| 256 | irecv_exit(device); | 242 | irecv_close(client); |
| 257 | return 0; | 243 | return 0; |
| 258 | } | 244 | } |
| 259 | 245 | ||
