| 1 |
/* |
| 2 |
* idevicediagnostics.c |
| 3 |
* Retrieves diagnostics information from device |
| 4 |
* |
| 5 |
* Copyright (c) 2012 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 "idevicediagnostics" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <string.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <getopt.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <time.h> |
| 34 |
#ifndef _WIN32 |
| 35 |
#include <signal.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <libimobiledevice/libimobiledevice.h> |
| 39 |
#include <libimobiledevice/lockdown.h> |
| 40 |
#include <libimobiledevice/diagnostics_relay.h> |
| 41 |
|
| 42 |
enum cmd_mode { |
| 43 |
CMD_NONE = 0, |
| 44 |
CMD_SLEEP, |
| 45 |
CMD_RESTART, |
| 46 |
CMD_SHUTDOWN, |
| 47 |
CMD_DIAGNOSTICS, |
| 48 |
CMD_MOBILEGESTALT, |
| 49 |
CMD_IOREGISTRY, |
| 50 |
CMD_IOREGISTRY_ENTRY |
| 51 |
}; |
| 52 |
|
| 53 |
static void print_xml(plist_t node) |
| 54 |
{ |
| 55 |
char *xml = NULL; |
| 56 |
uint32_t len = 0; |
| 57 |
plist_to_xml(node, &xml, &len); |
| 58 |
if (xml) { |
| 59 |
puts(xml); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static void print_usage(int argc, char **argv, int is_error) |
| 64 |
{ |
| 65 |
char *name = strrchr(argv[0], '/'); |
| 66 |
fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0])); |
| 67 |
fprintf(is_error ? stderr : stdout, |
| 68 |
"\n" |
| 69 |
"Use diagnostics interface of a device running iOS 4 or later.\n" |
| 70 |
"\n" |
| 71 |
"Where COMMAND is one of:\n" |
| 72 |
" diagnostics [TYPE] print diagnostics information from device by TYPE (All, WiFi, GasGauge, NAND)\n" |
| 73 |
" mobilegestalt KEY [...] print mobilegestalt keys passed as arguments separated by a space.\n" |
| 74 |
" ioreg [PLANE] print IORegistry of device, optionally by PLANE (IODeviceTree, IOPower, IOService) (iOS 5+ only)\n" |
| 75 |
" ioregentry [KEY] print IORegistry entry of device (AppleARMPMUCharger, ASPStorage, ...) (iOS 5+ only)\n" |
| 76 |
" shutdown shutdown device\n" |
| 77 |
" restart restart device\n" |
| 78 |
" sleep put device into sleep mode (disconnects from host)\n" |
| 79 |
"\n" |
| 80 |
"The following OPTIONS are accepted:\n" |
| 81 |
" -u, --udid UDID target specific device by UDID\n" |
| 82 |
" -n, --network connect to network device\n" |
| 83 |
" -d, --debug enable communication debugging\n" |
| 84 |
" -h, --help prints usage information\n" |
| 85 |
" -v, --version prints version information\n" |
| 86 |
"\n" |
| 87 |
"Homepage: <" PACKAGE_URL ">\n" |
| 88 |
"Bug Reports: <" PACKAGE_BUGREPORT ">\n" |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
int main(int argc, char **argv) |
| 93 |
{ |
| 94 |
idevice_t device = NULL; |
| 95 |
lockdownd_client_t lockdown_client = NULL; |
| 96 |
diagnostics_relay_client_t diagnostics_client = NULL; |
| 97 |
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR; |
| 98 |
lockdownd_service_descriptor_t service = NULL; |
| 99 |
int result = EXIT_FAILURE; |
| 100 |
const char *udid = NULL; |
| 101 |
int use_network = 0; |
| 102 |
int cmd = CMD_NONE; |
| 103 |
char* cmd_arg = NULL; |
| 104 |
plist_t node = NULL; |
| 105 |
plist_t keys = NULL; |
| 106 |
int c = 0; |
| 107 |
const struct option longopts[] = { |
| 108 |
{ "debug", no_argument, NULL, 'd' }, |
| 109 |
{ "help", no_argument, NULL, 'h' }, |
| 110 |
{ "udid", required_argument, NULL, 'u' }, |
| 111 |
{ "network", no_argument, NULL, 'n' }, |
| 112 |
{ "version", no_argument, NULL, 'v' }, |
| 113 |
{ NULL, 0, NULL, 0} |
| 114 |
}; |
| 115 |
|
| 116 |
#ifndef _WIN32 |
| 117 |
signal(SIGPIPE, SIG_IGN); |
| 118 |
#endif |
| 119 |
/* parse cmdline args */ |
| 120 |
while ((c = getopt_long(argc, argv, "dhu:nv", longopts, NULL)) != -1) { |
| 121 |
switch (c) { |
| 122 |
case 'd': |
| 123 |
idevice_set_debug_level(1); |
| 124 |
break; |
| 125 |
case 'u': |
| 126 |
if (!*optarg) { |
| 127 |
fprintf(stderr, "ERROR: UDID argument must not be empty!\n"); |
| 128 |
print_usage(argc, argv, 1); |
| 129 |
return 2; |
| 130 |
} |
| 131 |
udid = optarg; |
| 132 |
break; |
| 133 |
case 'n': |
| 134 |
use_network = 1; |
| 135 |
break; |
| 136 |
case 'h': |
| 137 |
print_usage(argc, argv, 0); |
| 138 |
return 0; |
| 139 |
case 'v': |
| 140 |
printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); |
| 141 |
return 0; |
| 142 |
default: |
| 143 |
print_usage(argc, argv, 1); |
| 144 |
return 2; |
| 145 |
} |
| 146 |
} |
| 147 |
argc -= optind; |
| 148 |
argv += optind; |
| 149 |
|
| 150 |
if (!argv[0]) { |
| 151 |
fprintf(stderr, "ERROR: No command specified\n"); |
| 152 |
print_usage(argc+optind, argv-optind, 1); |
| 153 |
return 2; |
| 154 |
} |
| 155 |
|
| 156 |
if (!strcmp(argv[0], "sleep")) { |
| 157 |
cmd = CMD_SLEEP; |
| 158 |
} |
| 159 |
else if (!strcmp(argv[0], "restart")) { |
| 160 |
cmd = CMD_RESTART; |
| 161 |
} |
| 162 |
else if (!strcmp(argv[0], "shutdown")) { |
| 163 |
cmd = CMD_SHUTDOWN; |
| 164 |
} |
| 165 |
else if (!strcmp(argv[0], "diagnostics")) { |
| 166 |
cmd = CMD_DIAGNOSTICS; |
| 167 |
/* read type */ |
| 168 |
if (!argv[1] || ((strcmp(argv[1], "All") != 0) && (strcmp(argv[1], "WiFi") != 0) && (strcmp(argv[1], "GasGauge") != 0) && (strcmp(argv[1], "NAND") != 0) && (strcmp(argv[1], "HDMI") != 0))) { |
| 169 |
if (argv[1] == NULL) { |
| 170 |
cmd_arg = strdup("All"); |
| 171 |
} else { |
| 172 |
fprintf(stderr, "ERROR: Unknown TYPE %s\n", argv[1]); |
| 173 |
print_usage(argc+optind, argv-optind, 1); |
| 174 |
goto cleanup; |
| 175 |
} |
| 176 |
} |
| 177 |
cmd_arg = strdup(argv[1]); |
| 178 |
} |
| 179 |
else if (!strcmp(argv[0], "mobilegestalt")) { |
| 180 |
cmd = CMD_MOBILEGESTALT; |
| 181 |
/* read keys */ |
| 182 |
if (!argv[1] || !*argv[1]) { |
| 183 |
fprintf(stderr, "ERROR: Please supply the key to query.\n"); |
| 184 |
print_usage(argc, argv, 1); |
| 185 |
goto cleanup; |
| 186 |
} |
| 187 |
int i = 1; |
| 188 |
keys = plist_new_array(); |
| 189 |
while (argv[i] && *argv[i]) { |
| 190 |
plist_array_append_item(keys, plist_new_string(argv[i])); |
| 191 |
i++; |
| 192 |
} |
| 193 |
} |
| 194 |
else if (!strcmp(argv[0], "ioreg")) { |
| 195 |
cmd = CMD_IOREGISTRY; |
| 196 |
/* read plane */ |
| 197 |
if (argv[1]) { |
| 198 |
cmd_arg = strdup(argv[1]); |
| 199 |
} |
| 200 |
} |
| 201 |
else if (!strcmp(argv[0], "ioregentry")) { |
| 202 |
cmd = CMD_IOREGISTRY_ENTRY; |
| 203 |
/* read key */ |
| 204 |
if (argv[1]) { |
| 205 |
cmd_arg = strdup(argv[1]); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/* verify options */ |
| 210 |
if (cmd == CMD_NONE) { |
| 211 |
fprintf(stderr, "ERROR: Unsupported command '%s'\n", argv[0]); |
| 212 |
print_usage(argc+optind, argv-optind, 1); |
| 213 |
goto cleanup; |
| 214 |
} |
| 215 |
|
| 216 |
if (IDEVICE_E_SUCCESS != idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX)) { |
| 217 |
if (udid) { |
| 218 |
printf("No device found with udid %s.\n", udid); |
| 219 |
} else { |
| 220 |
printf("No device found.\n"); |
| 221 |
} |
| 222 |
goto cleanup; |
| 223 |
} |
| 224 |
|
| 225 |
if (LOCKDOWN_E_SUCCESS != (ret = lockdownd_client_new_with_handshake(device, &lockdown_client, TOOL_NAME))) { |
| 226 |
idevice_free(device); |
| 227 |
printf("ERROR: Could not connect to lockdownd, error code %d\n", ret); |
| 228 |
goto cleanup; |
| 229 |
} |
| 230 |
|
| 231 |
/* attempt to use newer diagnostics service available on iOS 5 and later */ |
| 232 |
ret = lockdownd_start_service(lockdown_client, "com.apple.mobile.diagnostics_relay", &service); |
| 233 |
if (ret == LOCKDOWN_E_INVALID_SERVICE) { |
| 234 |
/* attempt to use older diagnostics service */ |
| 235 |
ret = lockdownd_start_service(lockdown_client, "com.apple.iosdiagnostics.relay", &service); |
| 236 |
} |
| 237 |
lockdownd_client_free(lockdown_client); |
| 238 |
|
| 239 |
if (ret != LOCKDOWN_E_SUCCESS) { |
| 240 |
idevice_free(device); |
| 241 |
printf("ERROR: Could not start diagnostics relay service: %s\n", lockdownd_strerror(ret)); |
| 242 |
goto cleanup; |
| 243 |
} |
| 244 |
|
| 245 |
result = EXIT_FAILURE; |
| 246 |
|
| 247 |
if ((ret == LOCKDOWN_E_SUCCESS) && service && (service->port > 0)) { |
| 248 |
if (diagnostics_relay_client_new(device, service, &diagnostics_client) != DIAGNOSTICS_RELAY_E_SUCCESS) { |
| 249 |
printf("ERROR: Could not connect to diagnostics_relay!\n"); |
| 250 |
} else { |
| 251 |
switch (cmd) { |
| 252 |
case CMD_SLEEP: |
| 253 |
if (diagnostics_relay_sleep(diagnostics_client) == DIAGNOSTICS_RELAY_E_SUCCESS) { |
| 254 |
printf("Putting device into deep sleep mode.\n"); |
| 255 |
result = EXIT_SUCCESS; |
| 256 |
} else { |
| 257 |
printf("ERROR: Failed to put device into deep sleep mode.\n"); |
| 258 |
} |
| 259 |
break; |
| 260 |
case CMD_RESTART: |
| 261 |
if (diagnostics_relay_restart(diagnostics_client, DIAGNOSTICS_RELAY_ACTION_FLAG_WAIT_FOR_DISCONNECT) == DIAGNOSTICS_RELAY_E_SUCCESS) { |
| 262 |
printf("Restarting device.\n"); |
| 263 |
result = EXIT_SUCCESS; |
| 264 |
} else { |
| 265 |
printf("ERROR: Failed to restart device.\n"); |
| 266 |
} |
| 267 |
break; |
| 268 |
case CMD_SHUTDOWN: |
| 269 |
if (diagnostics_relay_shutdown(diagnostics_client, DIAGNOSTICS_RELAY_ACTION_FLAG_WAIT_FOR_DISCONNECT) == DIAGNOSTICS_RELAY_E_SUCCESS) { |
| 270 |
printf("Shutting down device.\n"); |
| 271 |
result = EXIT_SUCCESS; |
| 272 |
} else { |
| 273 |
printf("ERROR: Failed to shutdown device.\n"); |
| 274 |
} |
| 275 |
break; |
| 276 |
case CMD_MOBILEGESTALT: |
| 277 |
if (diagnostics_relay_query_mobilegestalt(diagnostics_client, keys, &node) == DIAGNOSTICS_RELAY_E_SUCCESS) { |
| 278 |
if (node) { |
| 279 |
print_xml(node); |
| 280 |
result = EXIT_SUCCESS; |
| 281 |
} |
| 282 |
} else { |
| 283 |
printf("ERROR: Unable to query mobilegestalt keys.\n"); |
| 284 |
} |
| 285 |
break; |
| 286 |
case CMD_IOREGISTRY_ENTRY: |
| 287 |
if (diagnostics_relay_query_ioregistry_entry(diagnostics_client, cmd_arg == NULL ? "": cmd_arg, "", &node) == DIAGNOSTICS_RELAY_E_SUCCESS) { |
| 288 |
if (node) { |
| 289 |
print_xml(node); |
| 290 |
result = EXIT_SUCCESS; |
| 291 |
} |
| 292 |
} else { |
| 293 |
printf("ERROR: Unable to retrieve IORegistry from device.\n"); |
| 294 |
} |
| 295 |
break; |
| 296 |
case CMD_IOREGISTRY: |
| 297 |
if (diagnostics_relay_query_ioregistry_plane(diagnostics_client, cmd_arg == NULL ? "": cmd_arg, &node) == DIAGNOSTICS_RELAY_E_SUCCESS) { |
| 298 |
if (node) { |
| 299 |
print_xml(node); |
| 300 |
result = EXIT_SUCCESS; |
| 301 |
} |
| 302 |
} else { |
| 303 |
printf("ERROR: Unable to retrieve IORegistry from device.\n"); |
| 304 |
} |
| 305 |
break; |
| 306 |
case CMD_DIAGNOSTICS: |
| 307 |
default: |
| 308 |
if (diagnostics_relay_request_diagnostics(diagnostics_client, cmd_arg, &node) == DIAGNOSTICS_RELAY_E_SUCCESS) { |
| 309 |
if (node) { |
| 310 |
print_xml(node); |
| 311 |
result = EXIT_SUCCESS; |
| 312 |
} |
| 313 |
} else { |
| 314 |
printf("ERROR: Unable to retrieve diagnostics from device.\n"); |
| 315 |
} |
| 316 |
break; |
| 317 |
} |
| 318 |
|
| 319 |
diagnostics_relay_goodbye(diagnostics_client); |
| 320 |
diagnostics_relay_client_free(diagnostics_client); |
| 321 |
} |
| 322 |
} else { |
| 323 |
printf("ERROR: Could not start diagnostics service!\n"); |
| 324 |
} |
| 325 |
|
| 326 |
if (service) { |
| 327 |
lockdownd_service_descriptor_free(service); |
| 328 |
service = NULL; |
| 329 |
} |
| 330 |
|
| 331 |
idevice_free(device); |
| 332 |
|
| 333 |
cleanup: |
| 334 |
if (node) { |
| 335 |
plist_free(node); |
| 336 |
} |
| 337 |
if (keys) { |
| 338 |
plist_free(keys); |
| 339 |
} |
| 340 |
if (cmd_arg) { |
| 341 |
free(cmd_arg); |
| 342 |
} |
| 343 |
return result; |
| 344 |
} |