ideviceenterrecovery.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <stdio.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #include <libimobiledevice/libimobiledevice.h>
  26. #include <libimobiledevice/lockdown.h>
  27. static void print_usage(int argc, char **argv)
  28. {
  29. char *name = NULL;
  30. name = strrchr(argv[0], '/');
  31. printf("Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0]));
  32. printf("Makes a device with the supplied 40-digit UDID enter recovery mode immediately.\n\n");
  33. printf(" -d, --debug\t\tenable communication debugging\n");
  34. printf(" -h, --help\t\tprints usage information\n");
  35. printf("\n");
  36. printf("Homepage: <http://libimobiledevice.org>\n");
  37. }
  38. int main(int argc, char *argv[])
  39. {
  40. lockdownd_client_t client = NULL;
  41. lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
  42. idevice_t device = NULL;
  43. idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
  44. int i;
  45. const char* udid = NULL;
  46. /* parse cmdline args */
  47. for (i = 1; i < argc; i++) {
  48. if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
  49. idevice_set_debug_level(1);
  50. continue;
  51. }
  52. else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
  53. print_usage(argc, argv);
  54. return 0;
  55. }
  56. }
  57. i--;
  58. if (!argv[i] || (strlen(argv[i]) != 40)) {
  59. print_usage(argc, argv);
  60. return 0;
  61. }
  62. udid = argv[i];
  63. ret = idevice_new(&device, udid);
  64. if (ret != IDEVICE_E_SUCCESS) {
  65. printf("No device found with udid %s, is it plugged in?\n", udid);
  66. return -1;
  67. }
  68. if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new(device, &client, "ideviceenterrecovery"))) {
  69. printf("ERROR: Could not connect to lockdownd, error code %d\n", ldret);
  70. idevice_free(device);
  71. return -1;
  72. }
  73. /* run query and output information */
  74. printf("Telling device with udid %s to enter recovery mode.\n", udid);
  75. if(lockdownd_enter_recovery(client) != LOCKDOWN_E_SUCCESS)
  76. {
  77. printf("Failed to enter recovery mode.\n");
  78. }
  79. printf("Device is successfully switching to recovery mode.\n");
  80. lockdownd_client_free(client);
  81. idevice_free(device);
  82. return 0;
  83. }