summaryrefslogtreecommitdiffstats
path: root/src/libirecovery.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libirecovery.c')
-rw-r--r--src/libirecovery.c672
1 files changed, 0 insertions, 672 deletions
diff --git a/src/libirecovery.c b/src/libirecovery.c
deleted file mode 100644
index dd5c734..0000000
--- a/src/libirecovery.c
+++ /dev/null
@@ -1,672 +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 <string.h>
22#include <unistd.h>
23#include <libusb-1.0/libusb.h>
24
25#include "libirecovery.h"
26
27#define BUFFER_SIZE 0x1000
28#define debug(...) if(libirecovery_debug) fprintf(stderr, __VA_ARGS__)
29
30static int libirecovery_debug = 0;
31static libusb_context* libirecovery_context = NULL;
32
33int irecv_write_file(const char* filename, const void* data, size_t size);
34int irecv_read_file(const char* filename, char** data, uint32_t* size);
35
36irecv_error_t irecv_open(irecv_client_t* pclient) {
37 int i = 0;
38 char serial[256];
39 struct libusb_device* usb_device = NULL;
40 struct libusb_device** usb_device_list = NULL;
41 struct libusb_device_handle* usb_handle = NULL;
42 struct libusb_device_descriptor usb_descriptor;
43
44 *pclient = NULL;
45 libusb_init(&libirecovery_context);
46 if(libirecovery_debug) {
47 irecv_set_debug_level(libirecovery_debug);
48 }
49
50 irecv_error_t error = IRECV_E_SUCCESS;
51 int usb_device_count = libusb_get_device_list(libirecovery_context, &usb_device_list);
52 for (i = 0; i < usb_device_count; i++) {
53 usb_device = usb_device_list[i];
54 libusb_get_device_descriptor(usb_device, &usb_descriptor);
55 if (usb_descriptor.idVendor == APPLE_VENDOR_ID) {
56 /* verify this device is in a mode we understand */
57 if (usb_descriptor.idProduct == kRecoveryMode1 ||
58 usb_descriptor.idProduct == kRecoveryMode2 ||
59 usb_descriptor.idProduct == kRecoveryMode3 ||
60 usb_descriptor.idProduct == kRecoveryMode4 ||
61 usb_descriptor.idProduct == kDfuMode) {
62
63 libusb_open(usb_device, &usb_handle);
64 if (usb_handle == NULL) {
65 libusb_free_device_list(usb_device_list, 1);
66 libusb_close(usb_handle);
67 libusb_exit(libirecovery_context);
68 return IRECV_E_UNABLE_TO_CONNECT;
69 }
70 libusb_free_device_list(usb_device_list, 1);
71
72 irecv_client_t client = (irecv_client_t) malloc(sizeof(struct irecv_client));
73 if (client == NULL) {
74 libusb_close(usb_handle);
75 libusb_exit(libirecovery_context);
76 return IRECV_E_OUT_OF_MEMORY;
77 }
78
79 memset(client, '\0', sizeof(struct irecv_client));
80 client->interface = 0;
81 client->handle = usb_handle;
82 client->mode = usb_descriptor.idProduct;
83
84 error = irecv_set_configuration(client, 1);
85 if (error != IRECV_E_SUCCESS) {
86 return error;
87 }
88
89 error = irecv_set_interface(client, 1, 1);
90 if (error != IRECV_E_SUCCESS) {
91 return error;
92 }
93
94 *pclient = client;
95 return IRECV_E_SUCCESS;
96 }
97 }
98 }
99
100 return IRECV_E_UNABLE_TO_CONNECT;
101}
102
103irecv_error_t irecv_set_configuration(irecv_client_t client, int configuration) {
104 if (client == NULL || client->handle == NULL) {
105 return IRECV_E_NO_DEVICE;
106 }
107
108 debug("Setting to configuration %d", configuration);
109
110 int current = 0;
111 libusb_get_configuration(client->handle, &current);
112 if (current != configuration) {
113 if (libusb_set_configuration(client->handle, configuration) < 0) {
114 return IRECV_E_USB_CONFIGURATION;
115 }
116 }
117
118 client->config = configuration;
119 return IRECV_E_SUCCESS;
120}
121
122irecv_error_t irecv_set_interface(irecv_client_t client, int interface, int alt_interface) {
123 if (client == NULL || client->handle == NULL) {
124 return IRECV_E_NO_DEVICE;
125 }
126
127 if (client->interface == interface) {
128 return IRECV_E_SUCCESS;
129 }
130
131 debug("Setting to interface %d:%d", interface, alt_interface);
132 if (libusb_claim_interface(client->handle, interface) < 0) {
133 return IRECV_E_USB_INTERFACE;
134 }
135
136 if (libusb_set_interface_alt_setting(client->handle, interface, alt_interface) < 0) {
137 return IRECV_E_USB_INTERFACE;
138 }
139
140 client->interface = interface;
141 client->alt_interface = alt_interface;
142 return IRECV_E_SUCCESS;
143}
144
145irecv_error_t irecv_reset(irecv_client_t client) {
146 if (client == NULL || client->handle == NULL) {
147 return IRECV_E_NO_DEVICE;
148 }
149
150 libusb_reset_device(client->handle);
151
152 return IRECV_E_SUCCESS;
153}
154
155irecv_error_t irecv_event_subscribe(irecv_client_t client, irecv_event_type type, irecv_event_cb_t callback, void* user_data) {
156 switch(type) {
157 case IRECV_RECEIVED:
158 client->received_callback = callback;
159 break;
160
161 case IRECV_PROGRESS:
162 client->progress_callback = callback;
163
164 case IRECV_CONNECTED:
165 client->connected_callback = callback;
166
167 case IRECV_PRECOMMAND:
168 client->precommand_callback = callback;
169 break;
170
171 case IRECV_POSTCOMMAND:
172 client->postcommand_callback = callback;
173 break;
174
175 case IRECV_DISCONNECTED:
176 client->disconnected_callback = callback;
177
178 default:
179 return IRECV_E_UNKNOWN_ERROR;
180 }
181
182 return IRECV_E_SUCCESS;
183}
184
185irecv_error_t irecv_event_unsubscribe(irecv_client_t client, irecv_event_type type) {
186 switch(type) {
187 case IRECV_RECEIVED:
188 client->received_callback = NULL;
189 break;
190
191 case IRECV_PROGRESS:
192 client->progress_callback = NULL;
193
194 case IRECV_CONNECTED:
195 client->connected_callback = NULL;
196
197 case IRECV_PRECOMMAND:
198 client->precommand_callback = NULL;
199 break;
200
201 case IRECV_POSTCOMMAND:
202 client->postcommand_callback = NULL;
203 break;
204
205 case IRECV_DISCONNECTED:
206 client->disconnected_callback = NULL;
207
208 default:
209 return IRECV_E_UNKNOWN_ERROR;
210 }
211
212 return IRECV_E_SUCCESS;
213}
214
215irecv_error_t irecv_close(irecv_client_t client) {
216 if (client != NULL) {
217 if(client->disconnected_callback != NULL) {
218 irecv_event_t event;
219 event.size = 0;
220 event.data = NULL;
221 event.progress = 0;
222 event.type = IRECV_DISCONNECTED;
223 client->disconnected_callback(client, &event);
224 }
225
226 if (client->handle != NULL) {
227 libusb_release_interface(client->handle, client->interface);
228 libusb_close(client->handle);
229 client->handle = NULL;
230 }
231
232 if (libirecovery_context != NULL) {
233 libusb_exit(libirecovery_context);
234 libirecovery_context = NULL;
235 }
236
237 free(client);
238 client = NULL;
239 }
240
241 return IRECV_E_SUCCESS;
242}
243
244void irecv_set_debug_level(int level) {
245 libirecovery_debug = level;
246 if(libirecovery_context) {
247 libusb_set_debug(libirecovery_context, libirecovery_debug);
248 }
249}
250
251irecv_error_t irecv_send_command(irecv_client_t client, unsigned char* command) {
252 if (client == NULL || client->handle == NULL) {
253 return IRECV_E_NO_DEVICE;
254 }
255
256 unsigned int length = strlen(command);
257 if (length >= 0x100) {
258 length = 0xFF;
259 }
260
261 irecv_event_t event;
262 if(client->precommand_callback != NULL) {
263 event.size = length;
264 event.data = command;
265 event.type = IRECV_PRECOMMAND;
266 if(client->precommand_callback(client, &event)) {
267 return IRECV_E_SUCCESS;
268 }
269 }
270
271 if (length > 0) {
272 libusb_control_transfer(client->handle, 0x40, 0, 0, 0, command, length + 1, 100);
273 }
274
275 if(client->postcommand_callback != NULL) {
276 event.size = length;
277 event.data = command;
278 event.type = IRECV_POSTCOMMAND;
279 if(client->postcommand_callback(client, &event)) {
280 return IRECV_E_SUCCESS;
281 }
282 }
283
284 return IRECV_E_SUCCESS;
285}
286
287irecv_error_t irecv_send_file(irecv_client_t client, const char* filename) {
288 if (client == NULL || client->handle == NULL) {
289 return IRECV_E_NO_DEVICE;
290 }
291
292 FILE* file = fopen(filename, "rb");
293 if (file == NULL) {
294 return IRECV_E_FILE_NOT_FOUND;
295 }
296
297 fseek(file, 0, SEEK_END);
298 int length = ftell(file);
299 fseek(file, 0, SEEK_SET);
300
301 unsigned char* buffer = (unsigned char*) malloc(length);
302 if (buffer == NULL) {
303 fclose(file);
304 return IRECV_E_OUT_OF_MEMORY;
305 }
306
307 int bytes = fread(buffer, 1, length, file);
308 fclose(file);
309
310 if (bytes != length) {
311 free(buffer);
312 return IRECV_E_UNKNOWN_ERROR;
313 }
314
315 irecv_error_t error = irecv_send_buffer(client, buffer, length);
316 free(buffer);
317 return error;
318}
319
320irecv_error_t irecv_get_status(irecv_client_t client, unsigned int* status) {
321 if (client == NULL || client->handle == NULL) {
322 *status = 0;
323 return IRECV_E_NO_DEVICE;
324 }
325
326 unsigned char buffer[6];
327 memset(buffer, '\0', 6);
328 if (libusb_control_transfer(client->handle, 0xA1, 3, 0, 0, buffer, 6, 1000) != 6) {
329 *status = 0;
330 return IRECV_E_USB_STATUS;
331 }
332
333 debug("status: %d\n", (unsigned int) buffer[4]);
334 *status = (unsigned int) buffer[4];
335 return IRECV_E_SUCCESS;
336}
337
338irecv_error_t irecv_send_buffer(irecv_client_t client, unsigned char* buffer, unsigned int length) {
339 irecv_error_t error = 0;
340 if (client == NULL || client->handle == NULL) {
341 return IRECV_E_NO_DEVICE;
342 }
343
344 int last = length % 0x800;
345 int packets = length / 0x800;
346 if (last != 0) {
347 packets++;
348 }
349
350 int i = 0;
351 double progress = 0;
352 unsigned int count = 0;
353 unsigned int status = 0;
354 for (i = 0; i < packets; i++) {
355 int size = i + 1 < packets ? 0x800 : last;
356 int bytes = libusb_control_transfer(client->handle, 0x21, 1, 0, 0, &buffer[i * 0x800], size, 1000);
357 if (bytes != size) {
358 return IRECV_E_USB_UPLOAD;
359 }
360
361 error = irecv_get_status(client, &status);
362 if (error != IRECV_E_SUCCESS) {
363 return error;
364 }
365
366 if (status != 5) {
367 return IRECV_E_USB_UPLOAD;
368 }
369
370 count += size;
371 if(client->progress_callback != NULL) {
372 irecv_event_t event;
373 event.progress = ((double) count/ (double) length) * 100.0;
374 event.type = IRECV_PROGRESS;
375 event.data = "Uploading";
376 event.size = count;
377 client->progress_callback(client, &event);
378 } else {
379 debug("Sent: %d bytes - %d of %d\n", bytes, count, length);
380 }
381 }
382
383 libusb_control_transfer(client->handle, 0x21, 1, 0, 0, buffer, 0, 1000);
384 for (i = 0; i < 3; i++) {
385 error = irecv_get_status(client, &status);
386 if (error != IRECV_E_SUCCESS) {
387 return error;
388 }
389 }
390
391 return IRECV_E_SUCCESS;
392}
393
394irecv_error_t irecv_receive(irecv_client_t client) {
395 unsigned char buffer[BUFFER_SIZE];
396 memset(buffer, '\0', BUFFER_SIZE);
397 if (client == NULL || client->handle == NULL) {
398 return IRECV_E_NO_DEVICE;
399 }
400
401 int bytes = 0;
402 while (libusb_bulk_transfer(client->handle, 0x81, buffer, BUFFER_SIZE, &bytes, 100) == 0) {
403 if (bytes > 0) {
404 if (client->received_callback != NULL) {
405 irecv_event_t event;
406 event.size = bytes;
407 event.data = buffer;
408 event.type = IRECV_RECEIVED;
409 if (client->received_callback(client, &event) != 0) {
410 return IRECV_E_SUCCESS;
411 }
412 }
413 } else break;
414 }
415
416 return IRECV_E_SUCCESS;
417}
418
419irecv_error_t irecv_getenv(irecv_client_t client, const char* variable, char** value) {
420 char command[256];
421 if (client == NULL || client->handle == NULL) {
422 return IRECV_E_NO_DEVICE;
423 }
424
425 *value = NULL;
426
427 if(variable == NULL) {
428 return IRECV_E_UNKNOWN_ERROR;
429 }
430
431 memset(command, '\0', sizeof(command));
432 snprintf(command, sizeof(command)-1, "getenv %s", variable);
433 irecv_error_t error = irecv_send_command(client, command);
434 if(error != IRECV_E_SUCCESS) {
435 return error;
436 }
437
438 unsigned char* response = (unsigned char*) malloc(256);
439 if (response == NULL) {
440 return IRECV_E_OUT_OF_MEMORY;
441 }
442
443 memset(response, '\0', 256);
444 int ret = libusb_control_transfer(client->handle, 0xC0, 0, 0, 0, response, 255, 500);
445 if (ret < 0) {
446 return IRECV_E_UNKNOWN_ERROR;
447 }
448
449 *value = response;
450 return IRECV_E_SUCCESS;
451}
452
453irecv_error_t irecv_get_cpid(irecv_client_t client, unsigned int* cpid) {
454 char info[256];
455 memset(info, '\0', 256);
456
457 if (client == NULL || client->handle == NULL) {
458 return IRECV_E_NO_DEVICE;
459 }
460
461 libusb_get_string_descriptor_ascii(client->handle, 3, info, 255);
462
463 unsigned char* cpid_string = strstr(info, "CPID:");
464 if (cpid_string == NULL) {
465 *cpid = 0;
466 return IRECV_E_UNKNOWN_ERROR;
467 }
468 sscanf(cpid_string, "CPID:%d", cpid);
469
470 return IRECV_E_SUCCESS;
471}
472
473irecv_error_t irecv_get_bdid(irecv_client_t client, unsigned int* bdid) {
474 char info[256];
475 memset(info, '\0', 256);
476
477 if (client == NULL || client->handle == NULL) {
478 return IRECV_E_NO_DEVICE;
479 }
480
481 libusb_get_string_descriptor_ascii(client->handle, 3, info, 255);
482
483 unsigned char* bdid_string = strstr(info, "BDID:");
484 if (bdid_string == NULL) {
485 *bdid = 0;
486 return IRECV_E_UNKNOWN_ERROR;
487 }
488 sscanf(bdid_string, "BDID:%d", bdid);
489
490 return IRECV_E_SUCCESS;
491}
492
493irecv_error_t irecv_get_ecid(irecv_client_t client, unsigned long long* ecid) {
494 char info[256];
495 memset(info, '\0', 256);
496
497 if (client == NULL || client->handle == NULL) {
498 return IRECV_E_NO_DEVICE;
499 }
500
501 libusb_get_string_descriptor_ascii(client->handle, 3, info, 255);
502
503 unsigned char* ecid_string = strstr(info, "ECID:");
504 if (ecid_string == NULL) {
505 *ecid = 0;
506 return IRECV_E_UNKNOWN_ERROR;
507 }
508 sscanf(ecid_string, "ECID:%qX", ecid);
509
510 return IRECV_E_SUCCESS;
511}
512
513irecv_error_t irecv_send_exploit(irecv_client_t client) {
514 if (client == NULL || client->handle == NULL) {
515 return IRECV_E_NO_DEVICE;
516 }
517
518 libusb_control_transfer(client->handle, 0x21, 2, 0, 0, NULL, 0, 100);
519 return IRECV_E_SUCCESS;
520}
521
522irecv_error_t irecv_execute_script(irecv_client_t client, const char* filename) {
523 irecv_error_t error = IRECV_E_SUCCESS;
524 if (client == NULL || client->handle == NULL) {
525 return IRECV_E_NO_DEVICE;
526 }
527
528 int file_size = 0;
529 char* file_data = NULL;
530 if(irecv_read_file(filename, &file_data, &file_size) < 0) {
531 return IRECV_E_FILE_NOT_FOUND;
532 }
533
534 char* line = strtok(file_data, "\n");
535 while(line != NULL) {
536 if(line[0] != '#') {
537 error = irecv_send_command(client, line);
538 if(error != IRECV_E_SUCCESS) {
539 return error;
540 }
541
542 error = irecv_receive(client);
543 if(error != IRECV_E_SUCCESS) {
544 return error;
545 }
546 }
547 line = strtok(NULL, "\n");
548 }
549
550 return IRECV_E_SUCCESS;
551}
552
553irecv_error_t irecv_setenv(irecv_client_t client, const char* variable, const char* value) {
554 char command[256];
555 if (client == NULL || client->handle == NULL) {
556 return IRECV_E_NO_DEVICE;
557 }
558
559 if(variable == NULL || value == NULL) {
560 return IRECV_E_UNKNOWN_ERROR;
561 }
562
563 memset(command, '\0', sizeof(command));
564 snprintf(command, sizeof(command)-1, "setenv %s %s", variable, value);
565 irecv_error_t error = irecv_send_command(client, command);
566 if(error != IRECV_E_SUCCESS) {
567 return error;
568 }
569
570 return IRECV_E_SUCCESS;
571}
572
573const char* irecv_strerror(irecv_error_t error) {
574 switch (error) {
575 case IRECV_E_SUCCESS:
576 return "Command completed successfully";
577
578 case IRECV_E_NO_DEVICE:
579 return "Unable to find device";
580
581 case IRECV_E_OUT_OF_MEMORY:
582 return "Out of memory";
583
584 case IRECV_E_UNABLE_TO_CONNECT:
585 return "Unable to connect to device";
586
587 case IRECV_E_INVALID_INPUT:
588 return "Invalid input";
589
590 case IRECV_E_FILE_NOT_FOUND:
591 return "File not found";
592
593 case IRECV_E_USB_UPLOAD:
594 return "Unable to upload data to device";
595
596 case IRECV_E_USB_STATUS:
597 return "Unable to get device status";
598
599 case IRECV_E_USB_INTERFACE:
600 return "Unable to set device interface";
601
602 case IRECV_E_USB_CONFIGURATION:
603 return "Unable to set device configuration";
604
605 default:
606 return "Unknown error";
607 }
608
609 return NULL;
610}
611
612int irecv_write_file(const char* filename, const void* data, size_t size) {
613 size_t bytes = 0;
614 FILE* file = NULL;
615
616 debug("Writing data to %s\n", filename);
617 file = fopen(filename, "wb");
618 if (file == NULL) {
619 error("read_file: Unable to open file %s\n", filename);
620 return -1;
621 }
622
623 bytes = fwrite(data, 1, size, file);
624 fclose(file);
625
626 if (bytes != size) {
627 error("ERROR: Unable to write entire file: %s: %d of %d\n", filename, bytes, size);
628 return -1;
629 }
630
631 return size;
632}
633
634int irecv_read_file(const char* filename, char** data, uint32_t* size) {
635 size_t bytes = 0;
636 size_t length = 0;
637 FILE* file = NULL;
638 char* buffer = NULL;
639 debug("Reading data from %s\n", filename);
640
641 *size = 0;
642 *data = NULL;
643
644 file = fopen(filename, "rb");
645 if (file == NULL) {
646 error("read_file: File %s not found\n", filename);
647 return -1;
648 }
649
650 fseek(file, 0, SEEK_END);
651 length = ftell(file);
652 rewind(file);
653
654 buffer = (char*) malloc(length);
655 if(buffer == NULL) {
656 error("ERROR: Out of memory\n");
657 fclose(file);
658 return -1;
659 }
660 bytes = fread(buffer, 1, length, file);
661 fclose(file);
662
663 if(bytes != length) {
664 error("ERROR: Unable to read entire file\n");
665 free(buffer);
666 return -1;
667 }
668
669 *size = length;
670 *data = buffer;
671 return 0;
672}