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 | |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
| 29 | #include <stdlib.h> |
Nikias Bassen | 8c7321b | 2019-09-28 12:08:12 +0200 | [diff] [blame^] | 30 | #ifndef WIN32 |
| 31 | #include <signal.h> |
| 32 | #endif |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 33 | |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 34 | #include <libimobiledevice/libimobiledevice.h> |
| 35 | #include <libimobiledevice/lockdown.h> |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 36 | |
| 37 | static void print_usage(int argc, char **argv) |
| 38 | { |
| 39 | char *name = NULL; |
Martin Szulecki | 24ce2e2 | 2015-01-28 01:27:59 +0100 | [diff] [blame] | 40 | |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 41 | name = strrchr(argv[0], '/'); |
Martin Szulecki | 7457346 | 2012-03-22 16:07:07 +0100 | [diff] [blame] | 42 | printf("Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0])); |
Nikias Bassen | b34e343 | 2018-10-01 02:32:51 +0200 | [diff] [blame] | 43 | printf("Makes a device with the supplied UDID enter recovery mode immediately.\n\n"); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 44 | printf(" -d, --debug\t\tenable communication debugging\n"); |
| 45 | printf(" -h, --help\t\tprints usage information\n"); |
| 46 | printf("\n"); |
Martin Szulecki | d93043e | 2015-10-06 22:10:56 +0200 | [diff] [blame] | 47 | printf("Homepage: <" PACKAGE_URL ">\n"); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | int main(int argc, char *argv[]) |
| 51 | { |
| 52 | lockdownd_client_t client = NULL; |
Nikias Bassen | fd43544 | 2014-10-11 22:12:04 +0200 | [diff] [blame] | 53 | lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; |
Nikias Bassen | 14bacd9 | 2012-11-29 04:02:18 +0100 | [diff] [blame] | 54 | idevice_t device = NULL; |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 55 | idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 56 | int i; |
Nikias Bassen | 36c0192 | 2012-11-29 03:42:06 +0100 | [diff] [blame] | 57 | const char* udid = NULL; |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 58 | |
Nikias Bassen | 8c7321b | 2019-09-28 12:08:12 +0200 | [diff] [blame^] | 59 | #ifndef WIN32 |
| 60 | signal(SIGPIPE, SIG_IGN); |
| 61 | #endif |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 62 | /* parse cmdline args */ |
| 63 | for (i = 1; i < argc; i++) { |
| 64 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 65 | idevice_set_debug_level(1); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 66 | continue; |
| 67 | } |
| 68 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { |
| 69 | print_usage(argc, argv); |
| 70 | return 0; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | i--; |
Nikias Bassen | b34e343 | 2018-10-01 02:32:51 +0200 | [diff] [blame] | 75 | if (argc < 2 || !argv[i] || !*argv[i]) { |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 76 | print_usage(argc, argv); |
| 77 | return 0; |
| 78 | } |
Nikias Bassen | 36c0192 | 2012-11-29 03:42:06 +0100 | [diff] [blame] | 79 | udid = argv[i]; |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 80 | |
Nikias Bassen | 14bacd9 | 2012-11-29 04:02:18 +0100 | [diff] [blame] | 81 | ret = idevice_new(&device, udid); |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 82 | if (ret != IDEVICE_E_SUCCESS) { |
Martin Szulecki | 7457346 | 2012-03-22 16:07:07 +0100 | [diff] [blame] | 83 | printf("No device found with udid %s, is it plugged in?\n", udid); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 84 | return -1; |
| 85 | } |
| 86 | |
Nikias Bassen | fd43544 | 2014-10-11 22:12:04 +0200 | [diff] [blame] | 87 | if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new(device, &client, "ideviceenterrecovery"))) { |
| 88 | printf("ERROR: Could not connect to lockdownd, error code %d\n", ldret); |
Nikias Bassen | 14bacd9 | 2012-11-29 04:02:18 +0100 | [diff] [blame] | 89 | idevice_free(device); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | /* run query and output information */ |
Martin Szulecki | 7457346 | 2012-03-22 16:07:07 +0100 | [diff] [blame] | 94 | printf("Telling device with udid %s to enter recovery mode.\n", udid); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 95 | if(lockdownd_enter_recovery(client) != LOCKDOWN_E_SUCCESS) |
| 96 | { |
| 97 | printf("Failed to enter recovery mode.\n"); |
| 98 | } |
| 99 | printf("Device is successfully switching to recovery mode.\n"); |
| 100 | |
| 101 | lockdownd_client_free(client); |
Nikias Bassen | 14bacd9 | 2012-11-29 04:02:18 +0100 | [diff] [blame] | 102 | idevice_free(device); |
Martin Szulecki | 5844d66 | 2009-08-01 18:54:59 +0200 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |