diff options
Diffstat (limited to 'src/irecovery.c')
| -rw-r--r-- | src/irecovery.c | 355 |
1 files changed, 0 insertions, 355 deletions
diff --git a/src/irecovery.c b/src/irecovery.c deleted file mode 100644 index 5a59cd0..0000000 --- a/src/irecovery.c +++ /dev/null | |||
| @@ -1,355 +0,0 @@ | |||
| 1 | /** | ||
| 2 | * iRecovery - Utility for DFU 2.0, WTF and Recovery Mode | ||
| 3 | * Copyright (C) 2008 - 2009 westbaer | ||
| 4 | * | ||
| 5 | * This program is free software: you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation, either version 3 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | **/ | ||
| 18 | |||
| 19 | #include <stdio.h> | ||
| 20 | #include <stdlib.h> | ||
| 21 | #include <unistd.h> | ||
| 22 | #include <libirecovery.h> | ||
| 23 | #include <readline/readline.h> | ||
| 24 | #include <readline/history.h> | ||
| 25 | |||
| 26 | #define FILE_HISTORY_PATH ".irecovery" | ||
| 27 | #define debug(...) if(verbose) fprintf(stderr, __VA_ARGS__) | ||
| 28 | |||
| 29 | enum { | ||
| 30 | kResetDevice, kStartShell, kSendCommand, kSendFile, kSendExploit, kSendScript | ||
| 31 | }; | ||
| 32 | |||
| 33 | static unsigned int quit = 0; | ||
| 34 | static unsigned int verbose = 0; | ||
| 35 | |||
| 36 | void print_progress_bar(double progress); | ||
| 37 | int received_cb(irecv_client_t client, const irecv_event_t* event); | ||
| 38 | int progress_cb(irecv_client_t client, const irecv_event_t* event); | ||
| 39 | int precommand_cb(irecv_client_t client, const irecv_event_t* event); | ||
| 40 | int postcommand_cb(irecv_client_t client, const irecv_event_t* event); | ||
| 41 | |||
| 42 | void shell_usage() { | ||
| 43 | printf("Usage:\n"); | ||
| 44 | printf("\t/upload <file>\tSend file to client.\n"); | ||
| 45 | printf("\t/exploit [file]\tSend usb exploit with optional payload\n"); | ||
| 46 | printf("\t/help\t\tShow this help.\n"); | ||
| 47 | printf("\t/exit\t\tExit interactive shell.\n"); | ||
| 48 | } | ||
| 49 | |||
| 50 | void parse_command(irecv_client_t client, unsigned char* command, unsigned int size) { | ||
| 51 | irecv_error_t error = 0; | ||
| 52 | char* cmd = strdup(command); | ||
| 53 | char* action = strtok(cmd, " "); | ||
| 54 | debug("Executing %s\n", action); | ||
| 55 | if (!strcmp(cmd, "/exit")) { | ||
| 56 | quit = 1; | ||
| 57 | } else | ||
| 58 | |||
| 59 | if (!strcmp(cmd, "/help")) { | ||
| 60 | shell_usage(); | ||
| 61 | } else | ||
| 62 | |||
| 63 | if (!strcmp(cmd, "/upload")) { | ||
| 64 | char* filename = strtok(NULL, " "); | ||
| 65 | debug("Uploading files %s\n", filename); | ||
| 66 | if (filename != NULL) { | ||
| 67 | error = irecv_send_file(client, filename); | ||
| 68 | debug("%s\n", irecv_strerror(error)); | ||
| 69 | } | ||
| 70 | } else | ||
| 71 | |||
| 72 | if (!strcmp(cmd, "/exploit")) { | ||
| 73 | char* filename = strtok(NULL, " "); | ||
| 74 | debug("Sending exploit %s\n", filename); | ||
| 75 | if (filename != NULL) { | ||
| 76 | error = irecv_send_file(client, filename); | ||
| 77 | debug("%s\n", irecv_strerror(error)); | ||
| 78 | } | ||
| 79 | irecv_send_exploit(client); | ||
| 80 | } else | ||
| 81 | |||
| 82 | if (!strcmp(cmd, "/execute")) { | ||
| 83 | char* filename = strtok(NULL, " "); | ||
| 84 | debug("Executing script %s\n", filename); | ||
| 85 | if (filename != NULL) { | ||
| 86 | irecv_execute_script(client, filename); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | free(action); | ||
| 92 | } | ||
| 93 | |||
| 94 | void load_command_history() { | ||
| 95 | read_history(FILE_HISTORY_PATH); | ||
| 96 | } | ||
| 97 | |||
| 98 | void append_command_to_history(char* cmd) { | ||
| 99 | add_history(cmd); | ||
| 100 | write_history(FILE_HISTORY_PATH); | ||
| 101 | } | ||
| 102 | |||
| 103 | void init_shell(irecv_client_t client) { | ||
| 104 | irecv_error_t error = 0; | ||
| 105 | load_command_history(); | ||
| 106 | irecv_event_subscribe(client, IRECV_PROGRESS, &progress_cb, NULL); | ||
| 107 | irecv_event_subscribe(client, IRECV_RECEIVED, &received_cb, NULL); | ||
| 108 | irecv_event_subscribe(client, IRECV_PRECOMMAND, &precommand_cb, NULL); | ||
| 109 | irecv_event_subscribe(client, IRECV_POSTCOMMAND, &postcommand_cb, NULL); | ||
| 110 | |||
| 111 | error = irecv_receive(client); | ||
| 112 | if (error != IRECV_E_SUCCESS) { | ||
| 113 | debug("%s\n", irecv_strerror(error)); | ||
| 114 | } | ||
| 115 | |||
| 116 | while (!quit) { | ||
| 117 | char* cmd = readline("> "); | ||
| 118 | if (cmd && *cmd) { | ||
| 119 | error = irecv_send_command(client, cmd); | ||
| 120 | if (error != IRECV_E_SUCCESS) { | ||
| 121 | quit = 1; | ||
| 122 | } | ||
| 123 | |||
| 124 | append_command_to_history(cmd); | ||
| 125 | free(cmd); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | int received_cb(irecv_client_t client, const irecv_event_t* event) { | ||
| 131 | if (event->type == IRECV_RECEIVED) { | ||
| 132 | int i = 0; | ||
| 133 | int size = event->size; | ||
| 134 | char* data = event->data; | ||
| 135 | for (i = 0; i < size; i++) { | ||
| 136 | printf("%c", data[i]); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | return 0; | ||
| 140 | } | ||
| 141 | |||
| 142 | int precommand_cb(irecv_client_t client, const irecv_event_t* event) { | ||
| 143 | char* value = NULL; | ||
| 144 | char* action = NULL; | ||
| 145 | char* command = NULL; | ||
| 146 | char* argument = NULL; | ||
| 147 | irecv_error_t error = IRECV_E_SUCCESS; | ||
| 148 | |||
| 149 | if (event->type == IRECV_PRECOMMAND) { | ||
| 150 | if (event->data[0] == '/') { | ||
| 151 | parse_command(client, event->data, event->size); | ||
| 152 | return -1; | ||
| 153 | } | ||
| 154 | |||
| 155 | command = strdup(event->data); | ||
| 156 | action = strtok(command, " "); | ||
| 157 | |||
| 158 | if (!strcmp(action, "getenv")) { | ||
| 159 | argument = strtok(NULL, " "); | ||
| 160 | error = irecv_getenv(client, argument, &value); | ||
| 161 | if (error != IRECV_E_SUCCESS) { | ||
| 162 | debug("%s\n", irecv_strerror(error)); | ||
| 163 | free(command); | ||
| 164 | return error; | ||
| 165 | } | ||
| 166 | if (value) { | ||
| 167 | printf("%s\n", value); | ||
| 168 | free(value); | ||
| 169 | } | ||
| 170 | return 1; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | return 0; | ||
| 174 | } | ||
| 175 | |||
| 176 | int postcommand_cb(irecv_client_t client, const irecv_event_t* event) { | ||
| 177 | char* action = NULL; | ||
| 178 | char* command = NULL; | ||
| 179 | |||
| 180 | if (event->type == IRECV_POSTCOMMAND) { | ||
| 181 | command = strdup(event->data); | ||
| 182 | action = strtok(command, " "); | ||
| 183 | |||
| 184 | if (!strcmp(action, "reboot")) { | ||
| 185 | quit = 1; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | if (command) free(command); | ||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | int progress_cb(irecv_client_t client, const irecv_event_t* event) { | ||
| 194 | if (event->type == IRECV_PROGRESS) { | ||
| 195 | print_progress_bar(event->progress); | ||
| 196 | } | ||
| 197 | return 0; | ||
| 198 | } | ||
| 199 | |||
| 200 | void print_progress_bar(double progress) { | ||
| 201 | int i = 0; | ||
| 202 | if(progress < 0) { | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | if(progress > 100) { | ||
| 207 | progress = 100; | ||
| 208 | } | ||
| 209 | |||
| 210 | printf("\r["); | ||
| 211 | for(i = 0; i < 50; i++) { | ||
| 212 | if(i < progress / 2) { | ||
| 213 | printf("="); | ||
| 214 | } else { | ||
| 215 | printf(" "); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | printf("] %3.1f%%", progress); | ||
| 220 | fflush(stdout); | ||
| 221 | if(progress == 100) { | ||
| 222 | printf("\n"); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | void print_usage() { | ||
| 227 | printf("iRecovery - iDevice Recovery Utility\n"); | ||
| 228 | printf("Usage: ./irecovery [args]\n"); | ||
| 229 | printf("\t-v\t\tStart irecovery in verbose mode.\n"); | ||
| 230 | printf("\t-c <cmd>\tSend command to client.\n"); | ||
| 231 | printf("\t-f <file>\tSend file to client.\n"); | ||
| 232 | printf("\t-k [payload]\tSend usb exploit to client.\n"); | ||
| 233 | printf("\t-h\t\tShow this help.\n"); | ||
| 234 | printf("\t-r\t\tReset client.\n"); | ||
| 235 | printf("\t-s\t\tStart interactive shell.\n"); | ||
| 236 | printf("\t-e <script>\tExecutes recovery shell script.\n"); | ||
| 237 | exit(1); | ||
| 238 | } | ||
| 239 | |||
| 240 | int main(int argc, char** argv) { | ||
| 241 | int i = 0; | ||
| 242 | int opt = 0; | ||
| 243 | int action = 0; | ||
| 244 | char* argument = NULL; | ||
| 245 | irecv_error_t error = 0; | ||
| 246 | if (argc == 1) print_usage(); | ||
| 247 | while ((opt = getopt(argc, argv, "vhrsc:f:e:k::")) > 0) { | ||
| 248 | switch (opt) { | ||
| 249 | case 'v': | ||
| 250 | verbose += 1; | ||
| 251 | break; | ||
| 252 | |||
| 253 | case 'h': | ||
| 254 | print_usage(); | ||
| 255 | break; | ||
| 256 | |||
| 257 | case 'r': | ||
| 258 | action = kResetDevice; | ||
| 259 | break; | ||
| 260 | |||
| 261 | case 's': | ||
| 262 | action = kStartShell; | ||
| 263 | break; | ||
| 264 | |||
| 265 | case 'f': | ||
| 266 | action = kSendFile; | ||
| 267 | argument = optarg; | ||
| 268 | break; | ||
| 269 | |||
| 270 | case 'c': | ||
| 271 | action = kSendCommand; | ||
| 272 | argument = optarg; | ||
| 273 | break; | ||
| 274 | |||
| 275 | case 'k': | ||
| 276 | action = kSendExploit; | ||
| 277 | argument = optarg; | ||
| 278 | break; | ||
| 279 | |||
| 280 | case 'e': | ||
| 281 | action = kSendScript; | ||
| 282 | argument = optarg; | ||
| 283 | break; | ||
| 284 | |||
| 285 | default: | ||
| 286 | fprintf(stderr, "Unknown argument\n"); | ||
| 287 | return -1; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | if (verbose) irecv_set_debug_level(verbose); | ||
| 292 | |||
| 293 | irecv_client_t client = NULL; | ||
| 294 | for (i = 0; i <= 5; i++) { | ||
| 295 | debug("Attempting to connect... \n"); | ||
| 296 | |||
| 297 | if (irecv_open(&client) != IRECV_E_SUCCESS) | ||
| 298 | sleep(1); | ||
| 299 | else | ||
| 300 | break; | ||
| 301 | |||
| 302 | if (i == 5) { | ||
| 303 | return -1; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | switch (action) { | ||
| 308 | case kResetDevice: | ||
| 309 | irecv_reset(client); | ||
| 310 | break; | ||
| 311 | |||
| 312 | case kSendFile: | ||
| 313 | irecv_event_subscribe(client, IRECV_PROGRESS, &progress_cb, NULL); | ||
| 314 | error = irecv_send_file(client, argument); | ||
| 315 | debug("%s\n", irecv_strerror(error)); | ||
| 316 | break; | ||
| 317 | |||
| 318 | case kSendCommand: | ||
| 319 | error = irecv_send_command(client, argument); | ||
| 320 | debug("%s\n", irecv_strerror(error)); | ||
| 321 | break; | ||
| 322 | |||
| 323 | case kSendExploit: | ||
| 324 | if (argument != NULL) { | ||
| 325 | irecv_event_subscribe(client, IRECV_PROGRESS, &progress_cb, NULL); | ||
| 326 | error = irecv_send_file(client, argument); | ||
| 327 | if (error != IRECV_E_SUCCESS) { | ||
| 328 | debug("%s\n", irecv_strerror(error)); | ||
| 329 | break; | ||
| 330 | } | ||
| 331 | } | ||
| 332 | error = irecv_send_exploit(client); | ||
| 333 | debug("%s\n", irecv_strerror(error)); | ||
| 334 | break; | ||
| 335 | |||
| 336 | case kStartShell: | ||
| 337 | init_shell(client); | ||
| 338 | break; | ||
| 339 | |||
| 340 | case kSendScript: | ||
| 341 | error = irecv_execute_script(client, argument); | ||
| 342 | if(error != IRECV_E_SUCCESS) { | ||
| 343 | debug("%s\n", irecv_strerror(error)); | ||
| 344 | } | ||
| 345 | break; | ||
| 346 | |||
| 347 | default: | ||
| 348 | fprintf(stderr, "Unknown action\n"); | ||
| 349 | break; | ||
| 350 | } | ||
| 351 | |||
| 352 | irecv_close(client); | ||
| 353 | return 0; | ||
| 354 | } | ||
| 355 | |||
