Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 1 | /* |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 2 | * ideviceenterrecovery.c |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 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. |
Martin Szulecki | 24ce2e2 | 2015-01-28 01:27:59 +0100 | [diff] [blame] | 11 | * |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 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. |
Martin Szulecki | 24ce2e2 | 2015-01-28 01:27:59 +0100 | [diff] [blame] | 16 | * |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 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 |
Martin Szulecki | 24ce2e2 | 2015-01-28 01:27:59 +0100 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
Martin Szulecki | d93043e | 2015-10-06 22:10:56 +0200 | [diff] [blame] | 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | |
Nikias Bassen | 3aa4e24 | 2020-06-05 18:42:20 +0200 | [diff] [blame] | 26 | #define TOOL_NAME "ideviceenterrecovery" |
| 27 | |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <errno.h> |
| 31 | #include <stdlib.h> |
Nikias Bassen | 8c7321b | 2019-09-28 12:08:12 +0200 | [diff] [blame] | 32 | #ifndef WIN32 |
| 33 | #include <signal.h> |
| 34 | #endif |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 35 | |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 36 | #include <libimobiledevice/libimobiledevice.h> |
| 37 | #include <libimobiledevice/lockdown.h> |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 38 | |
| 39 | static void print_usage(int argc, char **argv) |
| 40 | { |
| 41 | char *name = NULL; |
Martin Szulecki | 24ce2e2 | 2015-01-28 01:27:59 +0100 | [diff] [blame] | 42 | |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 43 | name = strrchr(argv[0], '/'); |
Martin Szulecki | 7457346 | 2012-03-22 16:07:07 +0100 | [diff] [blame] | 44 | printf("Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0])); |
Martin Szulecki | bc2a155 | 2020-06-04 02:46:09 +0200 | [diff] [blame] | 45 | printf("\n"); |
| 46 | printf("Makes a device with the supplied UDID enter recovery mode immediately.\n"); |
| 47 | printf("\n"); |
| 48 | printf("OPTIONS:\n"); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 49 | printf(" -d, --debug\t\tenable communication debugging\n"); |
| 50 | printf(" -h, --help\t\tprints usage information\n"); |
Nikias Bassen | 3aa4e24 | 2020-06-05 18:42:20 +0200 | [diff] [blame] | 51 | printf(" -v, --version\t\tprints version information\n"); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 52 | printf("\n"); |
Martin Szulecki | bc2a155 | 2020-06-04 02:46:09 +0200 | [diff] [blame] | 53 | printf("Homepage: <" PACKAGE_URL ">\n"); |
| 54 | printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | int main(int argc, char *argv[]) |
| 58 | { |
| 59 | lockdownd_client_t client = NULL; |
Nikias Bassen | fd43544 | 2014-10-11 22:12:04 +0200 | [diff] [blame] | 60 | lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; |
Nikias Bassen | 14bacd9 | 2012-11-29 04:02:18 +0100 | [diff] [blame] | 61 | idevice_t device = NULL; |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 62 | idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 63 | int i; |
Nikias Bassen | 36c0192 | 2012-11-29 03:42:06 +0100 | [diff] [blame] | 64 | const char* udid = NULL; |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 65 | |
Nikias Bassen | 8c7321b | 2019-09-28 12:08:12 +0200 | [diff] [blame] | 66 | #ifndef WIN32 |
| 67 | signal(SIGPIPE, SIG_IGN); |
| 68 | #endif |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 69 | /* parse cmdline args */ |
| 70 | for (i = 1; i < argc; i++) { |
| 71 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 72 | idevice_set_debug_level(1); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 73 | continue; |
| 74 | } |
| 75 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { |
| 76 | print_usage(argc, argv); |
| 77 | return 0; |
| 78 | } |
Nikias Bassen | 3aa4e24 | 2020-06-05 18:42:20 +0200 | [diff] [blame] | 79 | else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) { |
| 80 | printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); |
| 81 | return 0; |
| 82 | } |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | i--; |
Nikias Bassen | b34e343 | 2018-10-01 02:32:51 +0200 | [diff] [blame] | 86 | if (argc < 2 || !argv[i] || !*argv[i]) { |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 87 | print_usage(argc, argv); |
| 88 | return 0; |
| 89 | } |
Nikias Bassen | 36c0192 | 2012-11-29 03:42:06 +0100 | [diff] [blame] | 90 | udid = argv[i]; |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 91 | |
Nikias Bassen | 14bacd9 | 2012-11-29 04:02:18 +0100 | [diff] [blame] | 92 | ret = idevice_new(&device, udid); |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 93 | if (ret != IDEVICE_E_SUCCESS) { |
Nikias Bassen | 489a673 | 2020-06-05 20:50:34 +0200 | [diff] [blame] | 94 | printf("No device found with udid %s.\n", udid); |
Nikias Bassen | 25059d4 | 2021-02-09 02:45:06 +0100 | [diff] [blame] | 95 | return 1; |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Nikias Bassen | 3aa4e24 | 2020-06-05 18:42:20 +0200 | [diff] [blame] | 98 | if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new(device, &client, TOOL_NAME))) { |
Nikias Bassen | 25059d4 | 2021-02-09 02:45:06 +0100 | [diff] [blame] | 99 | printf("ERROR: Could not connect to lockdownd: %s (%d)\n", lockdownd_strerror(ldret), ldret); |
Nikias Bassen | 14bacd9 | 2012-11-29 04:02:18 +0100 | [diff] [blame] | 100 | idevice_free(device); |
Nikias Bassen | 25059d4 | 2021-02-09 02:45:06 +0100 | [diff] [blame] | 101 | return 1; |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Nikias Bassen | 25059d4 | 2021-02-09 02:45:06 +0100 | [diff] [blame] | 104 | int res = 0; |
Martin Szulecki | 7457346 | 2012-03-22 16:07:07 +0100 | [diff] [blame] | 105 | printf("Telling device with udid %s to enter recovery mode.\n", udid); |
Nikias Bassen | 25059d4 | 2021-02-09 02:45:06 +0100 | [diff] [blame] | 106 | ldret = lockdownd_enter_recovery(client); |
| 107 | if (ldret == LOCKDOWN_E_SESSION_INACTIVE) { |
| 108 | lockdownd_client_free(client); |
| 109 | client = NULL; |
| 110 | if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new_with_handshake(device, &client, TOOL_NAME))) { |
| 111 | printf("ERROR: Could not connect to lockdownd: %s (%d)\n", lockdownd_strerror(ldret), ldret); |
| 112 | idevice_free(device); |
| 113 | return 1; |
| 114 | } |
| 115 | ldret = lockdownd_enter_recovery(client); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 116 | } |
Nikias Bassen | 25059d4 | 2021-02-09 02:45:06 +0100 | [diff] [blame] | 117 | if (ldret != LOCKDOWN_E_SUCCESS) { |
| 118 | printf("Failed to enter recovery mode.\n"); |
| 119 | res = 1; |
| 120 | } else { |
| 121 | printf("Device is successfully switching to recovery mode.\n"); |
| 122 | } |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 123 | |
| 124 | lockdownd_client_free(client); |
Nikias Bassen | 14bacd9 | 2012-11-29 04:02:18 +0100 | [diff] [blame] | 125 | idevice_free(device); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 126 | |
Nikias Bassen | 25059d4 | 2021-02-09 02:45:06 +0100 | [diff] [blame] | 127 | return res; |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 128 | } |