| 1 |
/* |
| 2 |
* ideviceimagemounter.c |
| 3 |
* Mount developer/debug disk images on the device |
| 4 |
* |
| 5 |
* Copyright (C) 2010 Nikias Bassen <[email protected]> |
| 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 "ideviceimagemounter" |
| 27 |
|
| 28 |
#include <stdlib.h> |
| 29 |
#define _GNU_SOURCE 1 |
| 30 |
#define __USE_GNU 1 |
| 31 |
#include <stdio.h> |
| 32 |
#include <string.h> |
| 33 |
#include <getopt.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <libgen.h> |
| 36 |
#include <time.h> |
| 37 |
#include <sys/time.h> |
| 38 |
#include <inttypes.h> |
| 39 |
#include <sys/stat.h> |
| 40 |
#ifndef _WIN32 |
| 41 |
#include <signal.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <libimobiledevice/libimobiledevice.h> |
| 45 |
#include <libimobiledevice/lockdown.h> |
| 46 |
#include <libimobiledevice/afc.h> |
| 47 |
#include <libimobiledevice/notification_proxy.h> |
| 48 |
#include <libimobiledevice/mobile_image_mounter.h> |
| 49 |
#include <libimobiledevice-glue/sha.h> |
| 50 |
#include <libimobiledevice-glue/utils.h> |
| 51 |
#include <asprintf.h> |
| 52 |
#include <plist/plist.h> |
| 53 |
#include <libtatsu/tss.h> |
| 54 |
|
| 55 |
static int list_mode = 0; |
| 56 |
static int use_network = 0; |
| 57 |
static int xml_mode = 0; |
| 58 |
static const char *udid = NULL; |
| 59 |
static const char *imagetype = NULL; |
| 60 |
|
| 61 |
static const char PKG_PATH[] = "PublicStaging"; |
| 62 |
static const char PATH_PREFIX[] = "/private/var/mobile/Media"; |
| 63 |
|
| 64 |
typedef enum { |
| 65 |
DISK_IMAGE_UPLOAD_TYPE_AFC, |
| 66 |
DISK_IMAGE_UPLOAD_TYPE_UPLOAD_IMAGE |
| 67 |
} disk_image_upload_type_t; |
| 68 |
|
| 69 |
enum cmd_mode { |
| 70 |
CMD_NONE = 0, |
| 71 |
CMD_MOUNT, |
| 72 |
CMD_UNMOUNT, |
| 73 |
CMD_LIST, |
| 74 |
CMD_DEVMODESTATUS |
| 75 |
}; |
| 76 |
|
| 77 |
int cmd = CMD_NONE; |
| 78 |
|
| 79 |
static void print_usage(int argc, char **argv, int is_error) |
| 80 |
{ |
| 81 |
char *name = strrchr(argv[0], '/'); |
| 82 |
fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] COMMAND [COMMAND OPTIONS...]\n", (name ? name + 1: argv[0])); |
| 83 |
fprintf(is_error ? stderr : stdout, |
| 84 |
"\n" |
| 85 |
"Mount, list, or unmount a disk image on the device.\n" |
| 86 |
"\n" |
| 87 |
"COMMANDS:\n" |
| 88 |
" mount PATH Mount the developer disk image at PATH.\n" |
| 89 |
" For iOS 17+, PATH is a directory containing a .dmg image,\n" |
| 90 |
" a BuildManifest.plist, and a Firmware sub-directory;\n" |
| 91 |
" for older versions PATH is a .dmg filename with a\n" |
| 92 |
" .dmg.signature in the same directory, or with another\n" |
| 93 |
" parameter pointing to a file elsewhere.\n" |
| 94 |
" list List mounted disk images.\n" |
| 95 |
" unmount PATH Unmount the image mounted at PATH.\n" |
| 96 |
" devmodestatus Query the developer mode status (iOS 16+)\n" |
| 97 |
"\n" |
| 98 |
"OPTIONS:\n" |
| 99 |
" -u, --udid UDID target specific device by UDID\n" |
| 100 |
" -n, --network connect to network device\n" |
| 101 |
" -t, --imagetype TYPE Image type to use, default is 'Developer'\n" |
| 102 |
" -x, --xml Use XML output\n" |
| 103 |
" -d, --debug enable communication debugging\n" |
| 104 |
" -h, --help prints usage information\n" |
| 105 |
" -v, --version prints version information\n" |
| 106 |
"\n" |
| 107 |
"Homepage: <" PACKAGE_URL ">\n" |
| 108 |
"Bug Reports: <" PACKAGE_BUGREPORT ">\n" |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
static void parse_opts(int argc, char **argv) |
| 113 |
{ |
| 114 |
int debug_level = 0; |
| 115 |
static struct option longopts[] = { |
| 116 |
{ "help", no_argument, NULL, 'h' }, |
| 117 |
{ "udid", required_argument, NULL, 'u' }, |
| 118 |
{ "network", no_argument, NULL, 'n' }, |
| 119 |
{ "imagetype", required_argument, NULL, 't' }, |
| 120 |
{ "xml", no_argument, NULL, 'x' }, |
| 121 |
{ "debug", no_argument, NULL, 'd' }, |
| 122 |
{ "version", no_argument, NULL, 'v' }, |
| 123 |
{ NULL, 0, NULL, 0 } |
| 124 |
}; |
| 125 |
int c; |
| 126 |
|
| 127 |
while (1) { |
| 128 |
c = getopt_long(argc, argv, "hu:t:xdnv", longopts, NULL); |
| 129 |
if (c == -1) { |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
switch (c) { |
| 134 |
case 'h': |
| 135 |
print_usage(argc, argv, 0); |
| 136 |
exit(0); |
| 137 |
case 'u': |
| 138 |
if (!*optarg) { |
| 139 |
fprintf(stderr, "ERROR: UDID must not be empty!\n"); |
| 140 |
print_usage(argc, argv, 1); |
| 141 |
exit(2); |
| 142 |
} |
| 143 |
udid = optarg; |
| 144 |
break; |
| 145 |
case 'n': |
| 146 |
use_network = 1; |
| 147 |
break; |
| 148 |
case 't': |
| 149 |
imagetype = optarg; |
| 150 |
break; |
| 151 |
case 'x': |
| 152 |
xml_mode = 1; |
| 153 |
break; |
| 154 |
case 'd': |
| 155 |
debug_level++; |
| 156 |
break; |
| 157 |
case 'v': |
| 158 |
printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); |
| 159 |
exit(0); |
| 160 |
default: |
| 161 |
print_usage(argc, argv, 1); |
| 162 |
exit(2); |
| 163 |
} |
| 164 |
} |
| 165 |
idevice_set_debug_level(debug_level); |
| 166 |
tss_set_debug_level(debug_level); |
| 167 |
} |
| 168 |
|
| 169 |
static ssize_t mim_upload_cb(void* buf, size_t size, void* userdata) |
| 170 |
{ |
| 171 |
return fread(buf, 1, size, (FILE*)userdata); |
| 172 |
} |
| 173 |
|
| 174 |
int main(int argc, char **argv) |
| 175 |
{ |
| 176 |
idevice_t device = NULL; |
| 177 |
lockdownd_client_t lckd = NULL; |
| 178 |
lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; |
| 179 |
mobile_image_mounter_client_t mim = NULL; |
| 180 |
afc_client_t afc = NULL; |
| 181 |
lockdownd_service_descriptor_t service = NULL; |
| 182 |
int res = -1; |
| 183 |
char *image_path = NULL; |
| 184 |
size_t image_size = 0; |
| 185 |
char *image_sig_path = NULL; |
| 186 |
|
| 187 |
#ifndef _WIN32 |
| 188 |
signal(SIGPIPE, SIG_IGN); |
| 189 |
#endif |
| 190 |
parse_opts(argc, argv); |
| 191 |
|
| 192 |
argc -= optind; |
| 193 |
argv += optind; |
| 194 |
|
| 195 |
if (argc == 0) { |
| 196 |
fprintf(stderr, "ERROR: Missing command.\n\n"); |
| 197 |
print_usage(argc+optind, argv-optind, 1); |
| 198 |
return 2; |
| 199 |
} |
| 200 |
|
| 201 |
char* cmdstr = argv[0]; |
| 202 |
|
| 203 |
int optind2 = 0; |
| 204 |
if (!strcmp(cmdstr, "mount")) { |
| 205 |
cmd = CMD_MOUNT; |
| 206 |
optind2++; |
| 207 |
} else if (!strcmp(cmdstr, "list")) { |
| 208 |
cmd = CMD_LIST; |
| 209 |
optind2++; |
| 210 |
} else if (!strcmp(cmdstr, "umount") || !strcmp(cmdstr, "unmount")) { |
| 211 |
cmd = CMD_UNMOUNT; |
| 212 |
optind2++; |
| 213 |
} else if (!strcmp(cmdstr, "devmodestatus")) { |
| 214 |
cmd = CMD_DEVMODESTATUS; |
| 215 |
optind2++; |
| 216 |
} else { |
| 217 |
// assume mount command, unless -l / --list was specified |
| 218 |
if (list_mode) { |
| 219 |
cmd = CMD_LIST; |
| 220 |
} else { |
| 221 |
cmd = CMD_MOUNT; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
argc -= optind2; |
| 226 |
argv += optind2; |
| 227 |
optind += optind2; |
| 228 |
|
| 229 |
switch (cmd) { |
| 230 |
case CMD_MOUNT: |
| 231 |
if (argc < 1) { |
| 232 |
fprintf(stderr, "ERROR: Missing IMAGE_FILE for mount command\n"); |
| 233 |
print_usage(argc+optind, argv-optind, 1); |
| 234 |
return 2; |
| 235 |
} |
| 236 |
image_path = strdup(argv[0]); |
| 237 |
if (argc >= 2) { |
| 238 |
image_sig_path = strdup(argv[1]); |
| 239 |
} else { |
| 240 |
if (asprintf(&image_sig_path, "%s.signature", image_path) < 0) { |
| 241 |
printf("Out of memory?!\n"); |
| 242 |
return 1; |
| 243 |
} |
| 244 |
} |
| 245 |
break; |
| 246 |
case CMD_UNMOUNT: |
| 247 |
if (argc != 1) { |
| 248 |
fprintf(stderr, "ERROR: Missing mount path (argc = %d)\n", argc); |
| 249 |
print_usage(argc+optind, argv-optind, 1); |
| 250 |
return 2; |
| 251 |
} |
| 252 |
break; |
| 253 |
default: |
| 254 |
break; |
| 255 |
} |
| 256 |
|
| 257 |
if (IDEVICE_E_SUCCESS != idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX)) { |
| 258 |
if (udid) { |
| 259 |
printf("No device found with udid %s.\n", udid); |
| 260 |
} else { |
| 261 |
printf("No device found.\n"); |
| 262 |
} |
| 263 |
return 1; |
| 264 |
} |
| 265 |
|
| 266 |
if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new_with_handshake(device, &lckd, TOOL_NAME))) { |
| 267 |
printf("ERROR: Could not connect to lockdown, error code %d.\n", ldret); |
| 268 |
goto leave; |
| 269 |
} |
| 270 |
|
| 271 |
unsigned int device_version = idevice_get_device_version(device); |
| 272 |
|
| 273 |
disk_image_upload_type_t disk_image_upload_type = DISK_IMAGE_UPLOAD_TYPE_AFC; |
| 274 |
if (device_version >= IDEVICE_DEVICE_VERSION(7,0,0)) { |
| 275 |
disk_image_upload_type = DISK_IMAGE_UPLOAD_TYPE_UPLOAD_IMAGE; |
| 276 |
} |
| 277 |
|
| 278 |
if (device_version >= IDEVICE_DEVICE_VERSION(16,0,0)) { |
| 279 |
uint8_t dev_mode_status = 0; |
| 280 |
plist_t val = NULL; |
| 281 |
ldret = lockdownd_get_value(lckd, "com.apple.security.mac.amfi", "DeveloperModeStatus", &val); |
| 282 |
if (ldret == LOCKDOWN_E_SUCCESS) { |
| 283 |
plist_get_bool_val(val, &dev_mode_status); |
| 284 |
plist_free(val); |
| 285 |
} |
| 286 |
if (!dev_mode_status) { |
| 287 |
printf("ERROR: You have to enable Developer Mode on the given device in order to allowing mounting a developer disk image.\n"); |
| 288 |
goto leave; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
lockdownd_start_service(lckd, "com.apple.mobile.mobile_image_mounter", &service); |
| 293 |
|
| 294 |
if (!service || service->port == 0) { |
| 295 |
printf("ERROR: Could not start mobile_image_mounter service!\n"); |
| 296 |
goto leave; |
| 297 |
} |
| 298 |
|
| 299 |
if (mobile_image_mounter_new(device, service, &mim) != MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 300 |
printf("ERROR: Could not connect to mobile_image_mounter!\n"); |
| 301 |
goto leave; |
| 302 |
} |
| 303 |
|
| 304 |
if (service) { |
| 305 |
lockdownd_service_descriptor_free(service); |
| 306 |
service = NULL; |
| 307 |
} |
| 308 |
|
| 309 |
if (cmd == CMD_MOUNT) { |
| 310 |
struct stat fst; |
| 311 |
if (disk_image_upload_type == DISK_IMAGE_UPLOAD_TYPE_AFC) { |
| 312 |
if ((lockdownd_start_service(lckd, "com.apple.afc", &service) != |
| 313 |
LOCKDOWN_E_SUCCESS) || !service || !service->port) { |
| 314 |
fprintf(stderr, "Could not start com.apple.afc!\n"); |
| 315 |
goto leave; |
| 316 |
} |
| 317 |
if (afc_client_new(device, service, &afc) != AFC_E_SUCCESS) { |
| 318 |
fprintf(stderr, "Could not connect to AFC!\n"); |
| 319 |
goto leave; |
| 320 |
} |
| 321 |
if (service) { |
| 322 |
lockdownd_service_descriptor_free(service); |
| 323 |
service = NULL; |
| 324 |
} |
| 325 |
} |
| 326 |
if (stat(image_path, &fst) != 0) { |
| 327 |
fprintf(stderr, "ERROR: stat: %s: %s\n", image_path, strerror(errno)); |
| 328 |
goto leave; |
| 329 |
} |
| 330 |
image_size = fst.st_size; |
| 331 |
if (device_version < IDEVICE_DEVICE_VERSION(17,0,0) && stat(image_sig_path, &fst) != 0) { |
| 332 |
fprintf(stderr, "ERROR: stat: %s: %s\n", image_sig_path, strerror(errno)); |
| 333 |
goto leave; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
lockdownd_client_free(lckd); |
| 338 |
lckd = NULL; |
| 339 |
|
| 340 |
mobile_image_mounter_error_t err = MOBILE_IMAGE_MOUNTER_E_UNKNOWN_ERROR; |
| 341 |
plist_t result = NULL; |
| 342 |
|
| 343 |
if (cmd == CMD_LIST) { |
| 344 |
/* list mounts mode */ |
| 345 |
if (!imagetype) { |
| 346 |
if (device_version < IDEVICE_DEVICE_VERSION(17,0,0)) { |
| 347 |
imagetype = "Developer"; |
| 348 |
} else { |
| 349 |
imagetype = "Personalized"; |
| 350 |
} |
| 351 |
} |
| 352 |
err = mobile_image_mounter_lookup_image(mim, imagetype, &result); |
| 353 |
if (err == MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 354 |
res = 0; |
| 355 |
plist_write_to_stream(result, stdout, (xml_mode) ? PLIST_FORMAT_XML : PLIST_FORMAT_LIMD, 0); |
| 356 |
} else { |
| 357 |
printf("Error: lookup_image returned %d\n", err); |
| 358 |
} |
| 359 |
} else if (cmd == CMD_MOUNT) { |
| 360 |
unsigned char *sig = NULL; |
| 361 |
size_t sig_length = 0; |
| 362 |
FILE *f; |
| 363 |
struct stat fst; |
| 364 |
plist_t mount_options = NULL; |
| 365 |
|
| 366 |
if (device_version < IDEVICE_DEVICE_VERSION(17,0,0)) { |
| 367 |
f = fopen(image_sig_path, "rb"); |
| 368 |
if (!f) { |
| 369 |
fprintf(stderr, "Error opening signature file '%s': %s\n", image_sig_path, strerror(errno)); |
| 370 |
goto leave; |
| 371 |
} |
| 372 |
if (fstat(fileno(f), &fst) != 0) { |
| 373 |
fprintf(stderr, "Error: fstat: %s\n", strerror(errno)); |
| 374 |
goto leave; |
| 375 |
} |
| 376 |
sig = malloc(fst.st_size); |
| 377 |
sig_length = fread(sig, 1, fst.st_size, f); |
| 378 |
fclose(f); |
| 379 |
if (sig_length == 0) { |
| 380 |
fprintf(stderr, "Could not read signature from file '%s'\n", image_sig_path); |
| 381 |
goto leave; |
| 382 |
} |
| 383 |
|
| 384 |
f = fopen(image_path, "rb"); |
| 385 |
if (!f) { |
| 386 |
fprintf(stderr, "Error opening image file '%s': %s\n", image_path, strerror(errno)); |
| 387 |
goto leave; |
| 388 |
} |
| 389 |
} else { |
| 390 |
if (stat(image_path, &fst) != 0) { |
| 391 |
fprintf(stderr, "Error: stat: '%s': %s\n", image_path, strerror(errno)); |
| 392 |
goto leave; |
| 393 |
} |
| 394 |
if (!S_ISDIR(fst.st_mode)) { |
| 395 |
fprintf(stderr, "Error: Personalized Disk Image mount expects a directory as image path.\n"); |
| 396 |
goto leave; |
| 397 |
} |
| 398 |
char* build_manifest_path = string_build_path(image_path, "BuildManifest.plist", NULL); |
| 399 |
plist_t build_manifest = NULL; |
| 400 |
if (plist_read_from_file(build_manifest_path, &build_manifest, NULL) != 0) { |
| 401 |
free(build_manifest_path); |
| 402 |
build_manifest_path = string_build_path(image_path, "Restore", "BuildManifest.plist", NULL); |
| 403 |
if (plist_read_from_file(build_manifest_path, &build_manifest, NULL) == 0) { |
| 404 |
char* image_path_new = string_build_path(image_path, "Restore", NULL); |
| 405 |
free(image_path); |
| 406 |
image_path = image_path_new; |
| 407 |
} |
| 408 |
} |
| 409 |
if (!build_manifest) { |
| 410 |
fprintf(stderr, "Error: Could not locate BuildManifest.plist inside given disk image path!\n"); |
| 411 |
goto leave; |
| 412 |
} |
| 413 |
|
| 414 |
plist_t identifiers = NULL; |
| 415 |
mobile_image_mounter_error_t merr = mobile_image_mounter_query_personalization_identifiers(mim, NULL, &identifiers); |
| 416 |
if (merr != MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 417 |
fprintf(stderr, "Failed to query personalization identifiers: %d\n", merr); |
| 418 |
goto error_out; |
| 419 |
} |
| 420 |
|
| 421 |
unsigned int board_id = plist_dict_get_uint(identifiers, "BoardId"); |
| 422 |
unsigned int chip_id = plist_dict_get_uint(identifiers, "ChipID"); |
| 423 |
|
| 424 |
plist_t build_identities = plist_dict_get_item(build_manifest, "BuildIdentities"); |
| 425 |
plist_array_iter iter; |
| 426 |
plist_array_new_iter(build_identities, &iter); |
| 427 |
plist_t item = NULL; |
| 428 |
plist_t build_identity = NULL; |
| 429 |
do { |
| 430 |
plist_array_next_item(build_identities, iter, &item); |
| 431 |
if (!item) { |
| 432 |
break; |
| 433 |
} |
| 434 |
unsigned int bi_board_id = (unsigned int)plist_dict_get_uint(item, "ApBoardID"); |
| 435 |
unsigned int bi_chip_id = (unsigned int)plist_dict_get_uint(item, "ApChipID"); |
| 436 |
if (bi_chip_id == chip_id && bi_board_id == board_id) { |
| 437 |
build_identity = item; |
| 438 |
break; |
| 439 |
} |
| 440 |
} while (item); |
| 441 |
plist_mem_free(iter); |
| 442 |
if (!build_identity) { |
| 443 |
fprintf(stderr, "Error: The given disk image is not compatible with the current device.\n"); |
| 444 |
goto leave; |
| 445 |
} |
| 446 |
plist_t p_tc_path = plist_access_path(build_identity, 4, "Manifest", "LoadableTrustCache", "Info", "Path"); |
| 447 |
if (!p_tc_path) { |
| 448 |
fprintf(stderr, "Error: Could not determine path for trust cache!\n"); |
| 449 |
goto leave; |
| 450 |
} |
| 451 |
plist_t p_dmg_path = plist_access_path(build_identity, 4, "Manifest", "PersonalizedDMG", "Info", "Path"); |
| 452 |
if (!p_dmg_path) { |
| 453 |
fprintf(stderr, "Error: Could not determine path for disk image!\n"); |
| 454 |
goto leave; |
| 455 |
} |
| 456 |
char *tc_path = string_build_path(image_path, plist_get_string_ptr(p_tc_path, NULL), NULL); |
| 457 |
unsigned char* trust_cache = NULL; |
| 458 |
uint64_t trust_cache_size = 0; |
| 459 |
if (!buffer_read_from_filename(tc_path, (char**)&trust_cache, &trust_cache_size)) { |
| 460 |
fprintf(stderr, "Error: Trust cache does not exist at '%s'!\n", tc_path); |
| 461 |
goto leave; |
| 462 |
} |
| 463 |
mount_options = plist_new_dict(); |
| 464 |
plist_dict_set_item(mount_options, "ImageTrustCache", plist_new_data((char*)trust_cache, trust_cache_size)); |
| 465 |
free(trust_cache); |
| 466 |
char *dmg_path = string_build_path(image_path, plist_get_string_ptr(p_dmg_path, NULL), NULL); |
| 467 |
free(image_path); |
| 468 |
image_path = dmg_path; |
| 469 |
f = fopen(image_path, "rb"); |
| 470 |
if (!f) { |
| 471 |
fprintf(stderr, "Error opening image file '%s': %s\n", image_path, strerror(errno)); |
| 472 |
goto leave; |
| 473 |
} |
| 474 |
|
| 475 |
unsigned char buf[8192]; |
| 476 |
unsigned char sha384_digest[48]; |
| 477 |
sha384_context ctx; |
| 478 |
sha384_init(&ctx); |
| 479 |
fstat(fileno(f), &fst); |
| 480 |
image_size = fst.st_size; |
| 481 |
while (!feof(f)) { |
| 482 |
ssize_t fr = fread(buf, 1, sizeof(buf), f); |
| 483 |
if (fr <= 0) { |
| 484 |
break; |
| 485 |
} |
| 486 |
sha384_update(&ctx, buf, fr); |
| 487 |
} |
| 488 |
rewind(f); |
| 489 |
sha384_final(&ctx, sha384_digest); |
| 490 |
unsigned char* manifest = NULL; |
| 491 |
unsigned int manifest_size = 0; |
| 492 |
/* check if the device already has a personalization manifest for this image */ |
| 493 |
if (mobile_image_mounter_query_personalization_manifest(mim, "DeveloperDiskImage", sha384_digest, sizeof(sha384_digest), &manifest, &manifest_size) == MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 494 |
printf("Using existing personalization manifest from device.\n"); |
| 495 |
} else { |
| 496 |
/* we need to re-connect in this case */ |
| 497 |
mobile_image_mounter_free(mim); |
| 498 |
mim = NULL; |
| 499 |
if (mobile_image_mounter_start_service(device, &mim, TOOL_NAME) != MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 500 |
goto error_out; |
| 501 |
} |
| 502 |
printf("No personalization manifest, requesting from TSS...\n"); |
| 503 |
unsigned char* nonce = NULL; |
| 504 |
unsigned int nonce_size = 0; |
| 505 |
|
| 506 |
/* create new TSS request and fill parameters */ |
| 507 |
plist_t request = tss_request_new(NULL); |
| 508 |
plist_t params = plist_new_dict(); |
| 509 |
tss_parameters_add_from_manifest(params, build_identity, 1); |
| 510 |
|
| 511 |
/* copy all `Ap,*` items from identifiers */ |
| 512 |
plist_dict_iter di = NULL; |
| 513 |
plist_dict_new_iter(identifiers, &di); |
| 514 |
plist_t node = NULL; |
| 515 |
do { |
| 516 |
char* key = NULL; |
| 517 |
plist_dict_next_item(identifiers, di, &key, &node); |
| 518 |
if (node) { |
| 519 |
if (!strncmp(key, "Ap,", 3)) { |
| 520 |
plist_dict_set_item(request, key, plist_copy(node)); |
| 521 |
} |
| 522 |
} |
| 523 |
free(key); |
| 524 |
} while (node); |
| 525 |
plist_mem_free(di); |
| 526 |
|
| 527 |
plist_dict_copy_uint(params, identifiers, "ApECID", "UniqueChipID"); |
| 528 |
plist_dict_set_item(params, "ApProductionMode", plist_new_bool(1)); |
| 529 |
plist_dict_set_item(params, "ApSecurityMode", plist_new_bool(1)); |
| 530 |
plist_dict_set_item(params, "ApSupportsImg4", plist_new_bool(1)); |
| 531 |
|
| 532 |
/* query nonce from image mounter service */ |
| 533 |
merr = mobile_image_mounter_query_nonce(mim, "DeveloperDiskImage", &nonce, &nonce_size); |
| 534 |
if (merr == MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 535 |
plist_dict_set_item(params, "ApNonce", plist_new_data((char*)nonce, nonce_size)); |
| 536 |
} else { |
| 537 |
fprintf(stderr, "ERROR: Failed to query nonce for developer disk image: %d\n", merr); |
| 538 |
goto error_out; |
| 539 |
} |
| 540 |
mobile_image_mounter_free(mim); |
| 541 |
mim = NULL; |
| 542 |
|
| 543 |
plist_dict_set_item(params, "ApSepNonce", plist_new_data("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 20)); |
| 544 |
plist_dict_set_item(params, "UID_MODE", plist_new_bool(0)); |
| 545 |
tss_request_add_ap_tags(request, params, NULL); |
| 546 |
tss_request_add_common_tags(request, params, NULL); |
| 547 |
tss_request_add_ap_img4_tags(request, params); |
| 548 |
plist_free(params); |
| 549 |
|
| 550 |
/* request IM4M from TSS */ |
| 551 |
plist_t response = tss_request_send(request, NULL); |
| 552 |
plist_free(request); |
| 553 |
|
| 554 |
plist_t p_manifest = plist_dict_get_item(response, "ApImg4Ticket"); |
| 555 |
if (!PLIST_IS_DATA(p_manifest)) { |
| 556 |
fprintf(stderr, "Failed to get Img4Ticket\n"); |
| 557 |
goto error_out; |
| 558 |
} |
| 559 |
|
| 560 |
uint64_t m4m_len = 0; |
| 561 |
plist_get_data_val(p_manifest, (char**)&manifest, &m4m_len); |
| 562 |
manifest_size = m4m_len; |
| 563 |
plist_free(response); |
| 564 |
printf("Done.\n"); |
| 565 |
} |
| 566 |
sig = manifest; |
| 567 |
sig_length = manifest_size; |
| 568 |
|
| 569 |
imagetype = "Personalized"; |
| 570 |
} |
| 571 |
|
| 572 |
char *targetname = NULL; |
| 573 |
if (asprintf(&targetname, "%s/%s", PKG_PATH, "staging.dimage") < 0) { |
| 574 |
fprintf(stderr, "Out of memory!?\n"); |
| 575 |
goto leave; |
| 576 |
} |
| 577 |
char *mountname = NULL; |
| 578 |
if (asprintf(&mountname, "%s/%s", PATH_PREFIX, targetname) < 0) { |
| 579 |
fprintf(stderr, "Out of memory!?\n"); |
| 580 |
goto leave; |
| 581 |
} |
| 582 |
|
| 583 |
if (!imagetype) { |
| 584 |
imagetype = "Developer"; |
| 585 |
} |
| 586 |
|
| 587 |
if (!mim) { |
| 588 |
if (mobile_image_mounter_start_service(device, &mim, TOOL_NAME) != MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 589 |
goto error_out; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
switch(disk_image_upload_type) { |
| 594 |
case DISK_IMAGE_UPLOAD_TYPE_UPLOAD_IMAGE: |
| 595 |
printf("Uploading %s\n", image_path); |
| 596 |
err = mobile_image_mounter_upload_image(mim, imagetype, image_size, sig, sig_length, mim_upload_cb, f); |
| 597 |
break; |
| 598 |
case DISK_IMAGE_UPLOAD_TYPE_AFC: |
| 599 |
default: |
| 600 |
printf("Uploading %s --> afc:///%s\n", image_path, targetname); |
| 601 |
plist_t fileinfo = NULL; |
| 602 |
if (afc_get_file_info_plist(afc, PKG_PATH, &fileinfo) != AFC_E_SUCCESS) { |
| 603 |
if (afc_make_directory(afc, PKG_PATH) != AFC_E_SUCCESS) { |
| 604 |
fprintf(stderr, "WARNING: Could not create directory '%s' on device!\n", PKG_PATH); |
| 605 |
} |
| 606 |
} |
| 607 |
plist_free(fileinfo); |
| 608 |
|
| 609 |
uint64_t af = 0; |
| 610 |
if ((afc_file_open(afc, targetname, AFC_FOPEN_WRONLY, &af) != |
| 611 |
AFC_E_SUCCESS) || !af) { |
| 612 |
fclose(f); |
| 613 |
fprintf(stderr, "afc_file_open on '%s' failed!\n", targetname); |
| 614 |
goto leave; |
| 615 |
} |
| 616 |
|
| 617 |
char buf[8192]; |
| 618 |
size_t amount = 0; |
| 619 |
do { |
| 620 |
amount = fread(buf, 1, sizeof(buf), f); |
| 621 |
if (amount > 0) { |
| 622 |
uint32_t written, total = 0; |
| 623 |
while (total < amount) { |
| 624 |
written = 0; |
| 625 |
if (afc_file_write(afc, af, buf + total, amount - total, &written) != |
| 626 |
AFC_E_SUCCESS) { |
| 627 |
fprintf(stderr, "AFC Write error!\n"); |
| 628 |
break; |
| 629 |
} |
| 630 |
total += written; |
| 631 |
} |
| 632 |
if (total != amount) { |
| 633 |
fprintf(stderr, "Error: wrote only %d of %d\n", total, |
| 634 |
(unsigned int)amount); |
| 635 |
afc_file_close(afc, af); |
| 636 |
fclose(f); |
| 637 |
goto leave; |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
while (amount > 0); |
| 642 |
|
| 643 |
afc_file_close(afc, af); |
| 644 |
break; |
| 645 |
} |
| 646 |
|
| 647 |
fclose(f); |
| 648 |
|
| 649 |
if (err != MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 650 |
if (err == MOBILE_IMAGE_MOUNTER_E_DEVICE_LOCKED) { |
| 651 |
printf("ERROR: Device is locked, can't mount. Unlock device and try again.\n"); |
| 652 |
} else { |
| 653 |
printf("ERROR: Unknown error occurred, can't mount.\n"); |
| 654 |
} |
| 655 |
goto error_out; |
| 656 |
} |
| 657 |
printf("done.\n"); |
| 658 |
|
| 659 |
printf("Mounting...\n"); |
| 660 |
err = mobile_image_mounter_mount_image_with_options(mim, mountname, sig, sig_length, imagetype, mount_options, &result); |
| 661 |
if (err == MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 662 |
if (result) { |
| 663 |
plist_t node = plist_dict_get_item(result, "Status"); |
| 664 |
if (node) { |
| 665 |
char *status = NULL; |
| 666 |
plist_get_string_val(node, &status); |
| 667 |
if (status) { |
| 668 |
if (!strcmp(status, "Complete")) { |
| 669 |
printf("Done.\n"); |
| 670 |
res = 0; |
| 671 |
} else { |
| 672 |
printf("unexpected status value:\n"); |
| 673 |
plist_write_to_stream(result, stdout, (xml_mode) ? PLIST_FORMAT_XML : PLIST_FORMAT_LIMD, 0); |
| 674 |
} |
| 675 |
free(status); |
| 676 |
} else { |
| 677 |
printf("unexpected result:\n"); |
| 678 |
plist_write_to_stream(result, stdout, (xml_mode) ? PLIST_FORMAT_XML : PLIST_FORMAT_LIMD, 0); |
| 679 |
} |
| 680 |
} |
| 681 |
node = plist_dict_get_item(result, "Error"); |
| 682 |
if (node) { |
| 683 |
char *error = NULL; |
| 684 |
plist_get_string_val(node, &error); |
| 685 |
if (error) { |
| 686 |
printf("Error: %s\n", error); |
| 687 |
free(error); |
| 688 |
} else { |
| 689 |
printf("unexpected result:\n"); |
| 690 |
plist_write_to_stream(result, stdout, (xml_mode) ? PLIST_FORMAT_XML : PLIST_FORMAT_LIMD, 0); |
| 691 |
} |
| 692 |
node = plist_dict_get_item(result, "DetailedError"); |
| 693 |
if (node) { |
| 694 |
printf("DetailedError: %s\n", plist_get_string_ptr(node, NULL)); |
| 695 |
} |
| 696 |
} else { |
| 697 |
plist_write_to_stream(result, stdout, (xml_mode) ? PLIST_FORMAT_XML : PLIST_FORMAT_LIMD, 0); |
| 698 |
} |
| 699 |
} |
| 700 |
} else { |
| 701 |
printf("Error: mount_image returned %d\n", err); |
| 702 |
|
| 703 |
} |
| 704 |
} else if (cmd == CMD_UNMOUNT) { |
| 705 |
err = mobile_image_mounter_unmount_image(mim, argv[0]); |
| 706 |
switch (err) { |
| 707 |
case MOBILE_IMAGE_MOUNTER_E_SUCCESS: |
| 708 |
printf("Success\n"); |
| 709 |
res = 0; |
| 710 |
break; |
| 711 |
case MOBILE_IMAGE_MOUNTER_E_COMMAND_FAILED: |
| 712 |
printf("Error: '%s' is not mounted\n", argv[0]); |
| 713 |
res = 1; |
| 714 |
break; |
| 715 |
case MOBILE_IMAGE_MOUNTER_E_NOT_SUPPORTED: |
| 716 |
printf("Error: 'unmount' is not supported on this device\n"); |
| 717 |
res = 1; |
| 718 |
break; |
| 719 |
case MOBILE_IMAGE_MOUNTER_E_DEVICE_LOCKED: |
| 720 |
printf("Error: device is locked\n"); |
| 721 |
res = 1; |
| 722 |
break; |
| 723 |
default: |
| 724 |
printf("Error: unmount returned %d\n", err); |
| 725 |
break; |
| 726 |
} |
| 727 |
} else if (cmd == CMD_DEVMODESTATUS) { |
| 728 |
err = mobile_image_mounter_query_developer_mode_status(mim, &result); |
| 729 |
if (err == MOBILE_IMAGE_MOUNTER_E_SUCCESS) { |
| 730 |
res = 0; |
| 731 |
plist_write_to_stream(result, stdout, (xml_mode) ? PLIST_FORMAT_XML : PLIST_FORMAT_LIMD, 0); |
| 732 |
} else { |
| 733 |
printf("Error: query_developer_mode_status returned %d\n", err); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
if (result) { |
| 738 |
plist_free(result); |
| 739 |
} |
| 740 |
|
| 741 |
error_out: |
| 742 |
/* perform hangup command */ |
| 743 |
mobile_image_mounter_hangup(mim); |
| 744 |
/* free client */ |
| 745 |
mobile_image_mounter_free(mim); |
| 746 |
|
| 747 |
leave: |
| 748 |
if (afc) { |
| 749 |
afc_client_free(afc); |
| 750 |
} |
| 751 |
if (lckd) { |
| 752 |
lockdownd_client_free(lckd); |
| 753 |
} |
| 754 |
idevice_free(device); |
| 755 |
|
| 756 |
if (image_path) |
| 757 |
free(image_path); |
| 758 |
if (image_sig_path) |
| 759 |
free(image_sig_path); |
| 760 |
|
| 761 |
return res; |
| 762 |
} |