blob: ec9093bb734357014a94e1691664afda2802a551 [file] [log] [blame]
Martin Szulecki5844d662009-08-01 18:54:59 +02001/*
Nikias Bassen96101a12010-01-28 22:18:41 +01002 * ideviceenterrecovery.c
Martin Szulecki5844d662009-08-01 18:54:59 +02003 * 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 Szulecki24ce2e22015-01-28 01:27:59 +010011 *
Martin Szulecki5844d662009-08-01 18:54:59 +020012 * 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 Szulecki24ce2e22015-01-28 01:27:59 +010016 *
Martin Szulecki5844d662009-08-01 18:54:59 +020017 * 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 Szulecki24ce2e22015-01-28 01:27:59 +010019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Martin Szulecki5844d662009-08-01 18:54:59 +020020 */
21
Martin Szuleckid93043e2015-10-06 22:10:56 +020022#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
Martin Szulecki5844d662009-08-01 18:54:59 +020026#include <stdio.h>
27#include <string.h>
28#include <errno.h>
29#include <stdlib.h>
Nikias Bassen8c7321b2019-09-28 12:08:12 +020030#ifndef WIN32
31#include <signal.h>
32#endif
Martin Szulecki5844d662009-08-01 18:54:59 +020033
Nikias Bassen96101a12010-01-28 22:18:41 +010034#include <libimobiledevice/libimobiledevice.h>
35#include <libimobiledevice/lockdown.h>
Martin Szulecki5844d662009-08-01 18:54:59 +020036
37static void print_usage(int argc, char **argv)
38{
39 char *name = NULL;
Martin Szulecki24ce2e22015-01-28 01:27:59 +010040
Martin Szulecki5844d662009-08-01 18:54:59 +020041 name = strrchr(argv[0], '/');
Martin Szulecki74573462012-03-22 16:07:07 +010042 printf("Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0]));
Nikias Bassenb34e3432018-10-01 02:32:51 +020043 printf("Makes a device with the supplied UDID enter recovery mode immediately.\n\n");
Martin Szulecki5844d662009-08-01 18:54:59 +020044 printf(" -d, --debug\t\tenable communication debugging\n");
45 printf(" -h, --help\t\tprints usage information\n");
46 printf("\n");
Martin Szuleckid93043e2015-10-06 22:10:56 +020047 printf("Homepage: <" PACKAGE_URL ">\n");
Martin Szulecki5844d662009-08-01 18:54:59 +020048}
49
50int main(int argc, char *argv[])
51{
52 lockdownd_client_t client = NULL;
Nikias Bassenfd435442014-10-11 22:12:04 +020053 lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
Nikias Bassen14bacd92012-11-29 04:02:18 +010054 idevice_t device = NULL;
Nikias Bassen96101a12010-01-28 22:18:41 +010055 idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
Martin Szulecki5844d662009-08-01 18:54:59 +020056 int i;
Nikias Bassen36c01922012-11-29 03:42:06 +010057 const char* udid = NULL;
Martin Szulecki5844d662009-08-01 18:54:59 +020058
Nikias Bassen8c7321b2019-09-28 12:08:12 +020059#ifndef WIN32
60 signal(SIGPIPE, SIG_IGN);
61#endif
Martin Szulecki5844d662009-08-01 18:54:59 +020062 /* parse cmdline args */
63 for (i = 1; i < argc; i++) {
64 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
Nikias Bassen96101a12010-01-28 22:18:41 +010065 idevice_set_debug_level(1);
Martin Szulecki5844d662009-08-01 18:54:59 +020066 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 Bassenb34e3432018-10-01 02:32:51 +020075 if (argc < 2 || !argv[i] || !*argv[i]) {
Martin Szulecki5844d662009-08-01 18:54:59 +020076 print_usage(argc, argv);
77 return 0;
78 }
Nikias Bassen36c01922012-11-29 03:42:06 +010079 udid = argv[i];
Martin Szulecki5844d662009-08-01 18:54:59 +020080
Nikias Bassen14bacd92012-11-29 04:02:18 +010081 ret = idevice_new(&device, udid);
Nikias Bassen96101a12010-01-28 22:18:41 +010082 if (ret != IDEVICE_E_SUCCESS) {
Martin Szulecki74573462012-03-22 16:07:07 +010083 printf("No device found with udid %s, is it plugged in?\n", udid);
Martin Szulecki5844d662009-08-01 18:54:59 +020084 return -1;
85 }
86
Nikias Bassenfd435442014-10-11 22:12:04 +020087 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 Bassen14bacd92012-11-29 04:02:18 +010089 idevice_free(device);
Martin Szulecki5844d662009-08-01 18:54:59 +020090 return -1;
91 }
92
93 /* run query and output information */
Martin Szulecki74573462012-03-22 16:07:07 +010094 printf("Telling device with udid %s to enter recovery mode.\n", udid);
Martin Szulecki5844d662009-08-01 18:54:59 +020095 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 Bassen14bacd92012-11-29 04:02:18 +0100102 idevice_free(device);
Martin Szulecki5844d662009-08-01 18:54:59 +0200103
104 return 0;
105}