Debian Codegraph @

tools/ideviceenterrecovery.c

raw
1 /*
2 * ideviceenterrecovery.c
3 * Simple utility to make a device in normal mode enter recovery mode.
4 *
5 * Copyright (c) 2009 Martin Szulecki All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
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
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #define TOOL_NAME "ideviceenterrecovery"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <getopt.h>
32 #include <errno.h>
33 #ifndef _WIN32
34 #include <signal.h>
35 #endif
36
37 #include <libimobiledevice/libimobiledevice.h>
38 #include <libimobiledevice/lockdown.h>
39
40 static void print_usage(int argc, char **argv, int is_error)
41 {
42 char *name = strrchr(argv[0], '/');
43 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0]));
44 fprintf(is_error ? stderr : stdout,
45 "\n"
46 "Makes a device with the supplied UDID enter recovery mode immediately.\n"
47 "\n"
48 "OPTIONS:\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 );
56 }
57
58 int main(int argc, char *argv[])
59 {
60 lockdownd_client_t client = NULL;
61 lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
62 idevice_t device = NULL;
63 idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
64 const char* udid = NULL;
65 int c = 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 };
72
73 #ifndef _WIN32
74 signal(SIGPIPE, SIG_IGN);
75 #endif
76 /* parse cmdline args */
77 while ((c = getopt_long(argc, argv, "dhv", longopts, NULL)) != -1) {
78 switch (c) {
79 case 'd':
80 idevice_set_debug_level(1);
81 break;
82 case 'h':
83 print_usage(argc, argv, 0);
84 return 0;
85 case 'v':
86 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
87 return 0;
88 default:
89 print_usage(argc, argv, 1);
90 return 2;
91 }
92 }
93 argc -= optind;
94 argv += optind;
95
96 if (!argv[0]) {
97 fprintf(stderr, "ERROR: No UDID specified\n");
98 print_usage(argc+optind, argv-optind, 1);
99 return 2;
100 }
101 udid = argv[0];
102
103 ret = idevice_new(&device, udid);
104 if (ret != IDEVICE_E_SUCCESS) {
105 printf("No device found with udid %s.\n", udid);
106 return 1;
107 }
108
109 if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new(device, &client, TOOL_NAME))) {
110 printf("ERROR: Could not connect to lockdownd: %s (%d)\n", lockdownd_strerror(ldret), ldret);
111 idevice_free(device);
112 return 1;
113 }
114
115 int res = 0;
116 printf("Telling device with udid %s to enter recovery mode.\n", udid);
117 ldret = lockdownd_enter_recovery(client);
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) {
129 printf("Failed to enter recovery mode.\n");
130 res = 1;
131 } else {
132 printf("Device is successfully switching to recovery mode.\n");
133 }
134
135 lockdownd_client_free(client);
136 idevice_free(device);
137
138 return res;
139 }