summaryrefslogtreecommitdiffstats
path: root/tools/ideviceenterrecovery.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/ideviceenterrecovery.c')
-rw-r--r--tools/ideviceenterrecovery.c128
1 files changed, 87 insertions, 41 deletions
diff --git a/tools/ideviceenterrecovery.c b/tools/ideviceenterrecovery.c
index 827946b..29cc5c9 100644
--- a/tools/ideviceenterrecovery.c
+++ b/tools/ideviceenterrecovery.c
@@ -8,86 +8,132 @@
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version. 10 * version 2.1 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details. 15 * Lesser General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Lesser General Public 17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software 18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */ 20 */
21 21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
26#define TOOL_NAME "ideviceenterrecovery"
27
22#include <stdio.h> 28#include <stdio.h>
23#include <string.h> 29#include <string.h>
24#include <errno.h>
25#include <stdlib.h> 30#include <stdlib.h>
31#include <getopt.h>
32#include <errno.h>
33#ifndef WIN32
34#include <signal.h>
35#endif
26 36
27#include <libimobiledevice/libimobiledevice.h> 37#include <libimobiledevice/libimobiledevice.h>
28#include <libimobiledevice/lockdown.h> 38#include <libimobiledevice/lockdown.h>
29 39
30static void print_usage(int argc, char **argv) 40static void print_usage(int argc, char **argv, int is_error)
31{ 41{
32 char *name = NULL; 42 char *name = strrchr(argv[0], '/');
33 43 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0]));
34 name = strrchr(argv[0], '/'); 44 fprintf(is_error ? stderr : stdout,
35 printf("Usage: %s [OPTIONS] UUID\n", (name ? name + 1: argv[0])); 45 "\n"
36 printf("Makes a device with the supplied 40-digit UUID enter recovery mode immediately.\n\n"); 46 "Makes a device with the supplied UDID enter recovery mode immediately.\n"
37 printf(" -d, --debug\t\tenable communication debugging\n"); 47 "\n"
38 printf(" -h, --help\t\tprints usage information\n"); 48 "OPTIONS:\n"
39 printf("\n"); 49 " -d, --debug enable communication debugging\n"
50 " -h, --help prints usage information\n"
51 " -v, --version prints version information\n"
52 "\n"
53 "Homepage: <" PACKAGE_URL ">\n"
54 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
55 );
40} 56}
41 57
42int main(int argc, char *argv[]) 58int main(int argc, char *argv[])
43{ 59{
44 lockdownd_client_t client = NULL; 60 lockdownd_client_t client = NULL;
45 idevice_t phone = NULL; 61 lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
62 idevice_t device = NULL;
46 idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; 63 idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
47 int i; 64 const char* udid = NULL;
48 char uuid[41]; 65 int c = 0;
49 uuid[0] = 0; 66 const struct option longopts[] = {
67 { "debug", no_argument, NULL, 'd' },
68 { "help", no_argument, NULL, 'h' },
69 { "version", no_argument, NULL, 'v' },
70 { NULL, 0, NULL, 0}
71 };
50 72
73#ifndef WIN32
74 signal(SIGPIPE, SIG_IGN);
75#endif
51 /* parse cmdline args */ 76 /* parse cmdline args */
52 for (i = 1; i < argc; i++) { 77 while ((c = getopt_long(argc, argv, "dhv", longopts, NULL)) != -1) {
53 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { 78 switch (c) {
79 case 'd':
54 idevice_set_debug_level(1); 80 idevice_set_debug_level(1);
55 continue; 81 break;
56 } 82 case 'h':
57 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { 83 print_usage(argc, argv, 0);
58 print_usage(argc, argv); 84 return 0;
85 case 'v':
86 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
59 return 0; 87 return 0;
88 default:
89 print_usage(argc, argv, 1);
90 return 2;
60 } 91 }
61 } 92 }
93 argc -= optind;
94 argv += optind;
62 95
63 i--; 96 if (!argv[0]) {
64 if (!argv[i] || (strlen(argv[i]) != 40)) { 97 fprintf(stderr, "ERROR: No UDID specified\n");
65 print_usage(argc, argv); 98 print_usage(argc+optind, argv-optind, 1);
66 return 0; 99 return 2;
67 } 100 }
68 strcpy(uuid, argv[i]); 101 udid = argv[0];
69 102
70 ret = idevice_new(&phone, uuid); 103 ret = idevice_new(&device, udid);
71 if (ret != IDEVICE_E_SUCCESS) { 104 if (ret != IDEVICE_E_SUCCESS) {
72 printf("No device found with uuid %s, is it plugged in?\n", uuid); 105 printf("No device found with udid %s.\n", udid);
73 return -1; 106 return 1;
74 } 107 }
75 108
76 if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client, "ideviceenterrecovery")) { 109 if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new(device, &client, TOOL_NAME))) {
77 idevice_free(phone); 110 printf("ERROR: Could not connect to lockdownd: %s (%d)\n", lockdownd_strerror(ldret), ldret);
78 return -1; 111 idevice_free(device);
112 return 1;
79 } 113 }
80 114
81 /* run query and output information */ 115 int res = 0;
82 printf("Telling device with uuid %s to enter recovery mode.\n", uuid); 116 printf("Telling device with udid %s to enter recovery mode.\n", udid);
83 if(lockdownd_enter_recovery(client) != LOCKDOWN_E_SUCCESS) 117 ldret = lockdownd_enter_recovery(client);
84 { 118 if (ldret == LOCKDOWN_E_SESSION_INACTIVE) {
119 lockdownd_client_free(client);
120 client = NULL;
121 if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new_with_handshake(device, &client, TOOL_NAME))) {
122 printf("ERROR: Could not connect to lockdownd: %s (%d)\n", lockdownd_strerror(ldret), ldret);
123 idevice_free(device);
124 return 1;
125 }
126 ldret = lockdownd_enter_recovery(client);
127 }
128 if (ldret != LOCKDOWN_E_SUCCESS) {
85 printf("Failed to enter recovery mode.\n"); 129 printf("Failed to enter recovery mode.\n");
130 res = 1;
131 } else {
132 printf("Device is successfully switching to recovery mode.\n");
86 } 133 }
87 printf("Device is successfully switching to recovery mode.\n");
88 134
89 lockdownd_client_free(client); 135 lockdownd_client_free(client);
90 idevice_free(phone); 136 idevice_free(device);
91 137
92 return 0; 138 return res;
93} 139}