SDMMD_AFCConnectionRef SDMMD_AFCConnectionCreate(SDMMD_AMConnectionRef conn) { SDMMD_AFCConnectionRef afc = calloc(1, sizeof(struct sdmmd_AFCConnectionClass)); afc->handle = conn; CFStringRef name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s.%s.%s"), "com.samdmarshall.sdmmobiledevice.afc", SDMCFStringGetString((conn->ivars.device)->ivars.unique_device_id), SDMCFStringGetString(SDMGetCurrentDateString())); afc->operationQueue = dispatch_queue_create(SDMCFStringGetString(name), NULL); afc->operationCount = 0; return afc; }
SDMMD_AFCConnectionRef SDMMD_AFCConnectionCreate(SDMMD_AMConnectionRef conn) { SDMMD_AFCConnectionRef afc = calloc(1, sizeof(struct sdmmd_AFCConnectionClass)); if (afc != NULL) { afc->handle = conn; CFStringRef udidString = SDMMD_AMDeviceCopyUDID(conn->ivars.device); CFStringRef date_string = SDMCreateCurrentDateString(); char *dateString = SDMCFStringGetString(date_string); CFSafeRelease(date_string); CFStringRef name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s.%@.%s"), "com.samdmarshall.sdmmobiledevice.afc", udidString, dateString); CFSafeRelease(udidString); Safe(free, dateString); char *queueName = SDMCFStringGetString(name); afc->operationQueue = dispatch_queue_create(queueName, NULL); Safe(free, queueName); afc->operationCount = 0; } return afc; }
char* SDMDaodanFindTargetName(char *targetPath) { CFStringRef targetExecutablePath = CFStringCreateWithCString(kCFAllocatorDefault, targetPath, kCFStringEncodingUTF8); CFURLRef executableURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, targetExecutablePath, kCFURLPOSIXPathStyle, false); CFRelease(targetExecutablePath); CFStringRef targetIdentifier = CFURLCopyLastPathComponent(executableURL); char *targetName = SDMCFStringGetString(targetIdentifier); CFRelease(targetIdentifier); CFRelease(executableURL); return targetName; }
SDMMD_AFCOperationRef SDMMD_AFCOperationCreateGetFileInfo(CFStringRef path) { SDMMD_AFCOperationRef op = calloc(1, sizeof(struct sdmmd_AFCOperation)); op->packet = calloc(1, sizeof(struct sdmmd_AFCPacket)); char *cpath = SDMCFStringGetString(path); op->packet->data = calloc(1, strlen(cpath)+1); memcpy(op->packet->data, cpath, strlen(cpath)); SDMMD_AFCHeaderInit(&op->packet->header, 0xa, strlen(cpath)+0x29, 0x0, 0x0); free(cpath); return op; }
SDMMD_AFCOperationRef SDMMD_AFCOperationCreateReadDirectory(CFStringRef path) { // _AFCOperationCreateReadDirectory SDMMD_AFCOperationRef op = calloc(1, sizeof(struct sdmmd_AFCOperation)); op->packet = calloc(1, sizeof(struct sdmmd_AFCPacket)); char *cpath = SDMCFStringGetString(path); uint32_t data_length = (uint32_t)strlen(cpath)+1; op->packet->data = calloc(data_length, sizeof(char)); memcpy(op->packet->data, cpath, data_length); SDMMD_AFCHeaderInit(&(op->packet->header), SDMMD_AFC_Packet_ReadDirectory, data_length+sizeof(struct SDMMD_AFCPacketHeader), 0x0, 0x0); free(cpath); return op; }
SDMMD_AFCOperationRef SDMMD_AFCOperationCreateOpenFile(CFStringRef path, uint16_t mode, void* *fileRef) { SDMMD_AFCOperationRef op = calloc(1, sizeof(struct sdmmd_AFCOperation)); op->packet = calloc(1, sizeof(struct sdmmd_AFCPacket)); char *cpath = SDMCFStringGetString(path); op->packet->data = calloc(1, strlen(cpath)+2); op->packet->data = (void*)mode; memcpy(&op->packet->data[2], cpath, strlen(cpath)); SDMMD_AFCHeaderInit(&op->packet->header, 0xd, strlen(cpath)+0x31, 0x0, 0x0); free(cpath); return op; }
SDMMD_AFCOperationRef SDMMD_AFCOperationCreateFileRefOpen(CFStringRef path, uint16_t mode, void* *fileRef) { SDMMD_AFCOperationRef op = calloc(1, sizeof(struct sdmmd_AFCOperation)); op->packet = calloc(1, sizeof(struct sdmmd_AFCPacket)); char *cpath = SDMCFStringGetString(path); uint32_t data_length = (uint32_t)strlen(cpath)+1+8; op->packet->data = calloc(data_length, sizeof(char)); memcpy(op->packet->data, &mode, sizeof(mode)); memcpy(&(op->packet->data[sizeof(mode)]), cpath, strlen(cpath)); SDMMD_AFCHeaderInit(&(op->packet->header), SDMMD_AFC_Packet_FileRefOpen, data_length+sizeof(struct SDMMD_AFCPacketHeader), 0x0, 0x0); free(cpath); return op; }
CFStringRef SDMMD_PathToDeviceSupport(SDMMD_AMDeviceRef device) { CFStringRef dev_support_path = NULL; if (device) { SDMMD_AMDeviceConnect(device); SDMMD_AMDeviceStartSession(device); CFStringRef full_os_version = SDMMD_AMDeviceCopyValue(device, NULL, CFSTR(kProductVersion)); CFStringRef os_version = CFStringCreateWithSubstring(kCFAllocatorDefault, full_os_version, CFRangeMake(0, 3)); CFSafeRelease(full_os_version); CFStringRef build_version = SDMMD_AMDeviceCopyValue(device, NULL, CFSTR(kBuildVersion)); SDMMD_AMDeviceStopSession(device); SDMMD_AMDeviceDisconnect(device); if (os_version && build_version) { CFStringRef sdk_path = SDMMD_CopyDeviceSupportPathFromXCRUN(); CFStringRef device_support = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@/DeviceSupport/%@"), sdk_path, os_version); char *device_support_cstr = SDMCFStringGetString(device_support); bool isDir = false; bool file_exists = FileExistsAtPathIsDir(device_support_cstr, &isDir); if (file_exists && isDir) { dev_support_path = CFStringCreateCopy(kCFAllocatorDefault, device_support); } else { CFStringRef device_support_build = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ (%@)"), device_support, build_version); char *device_support_build_cstr = SDMCFStringGetString(device_support_build); file_exists = FileExistsAtPathIsDir(device_support_build_cstr, &isDir); if (file_exists && isDir) { dev_support_path = CFStringCreateCopy(kCFAllocatorDefault, device_support_build); } Safe(free, device_support_build_cstr); CFSafeRelease(device_support_build); } Safe(free, device_support_cstr); CFSafeRelease(sdk_path); CFSafeRelease(device_support); } CFSafeRelease(os_version); CFSafeRelease(build_version); } return dev_support_path; }
CFDictionaryRef SDMMD_CreateImageDictionary(CFStringRef device_support_image) { CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); if (dict) { CFStringRef signature_path = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@.signature"), device_support_image); char *signature_path_cstr = SDMCFStringGetString(signature_path); if (FileExistsAtPath(signature_path_cstr)) { CFDataRef signature_data = CFDataCreateFromFilePath(signature_path_cstr); CFDictionarySetValue(dict, CFSTR("ImageType"), CFSTR("Developer")); CFDictionarySetValue(dict, CFSTR("ImageSignature"), signature_data); } Safe(free, signature_path_cstr); CFSafeRelease(signature_path); } return dict; }
int main(int argc, const char * argv[]) { SDMMobileDevice; char *udid = NULL; char *service = NULL; char *help = NULL; char *domain = NULL; char *key = NULL; char *bundle = NULL; char *diagArg = NULL; bool searchArgs = true; char *installPath = NULL; int c; while (searchArgs) { int option_index = 0x0; c = getopt_long (argc, (char * const *)argv, "lh:d:ais:q:p:t:c:",long_options, &option_index); if (c == -1) { break; } switch (c) { case 'h': { if (optarg) { help = optarg; } optionsEnable[OptionsHelp] = true; searchArgs = false; break; }; case 'l': { optionsEnable[OptionsList] = true; searchArgs = false; break; }; case 'd': { if (optarg && !optionsEnable[OptionsDevice]) { optionsEnable[OptionsDevice] = true; udid = optarg; } break; } case 's': { if (optarg && !optionsEnable[OptionsAttach]) { service = optarg; optionsEnable[OptionsAttach] = true; } else { printf("please specify a service name to attach"); } break; }; case 'q': { if (optarg && !optionsEnable[OptionsQuery]) { CFStringRef argValue = CFStringCreateWithCString(kCFAllocatorDefault, optarg, kCFStringEncodingUTF8); CFArrayRef argsArray = CFStringCreateArrayBySeparatingStrings(kCFAllocatorDefault, argValue, CFSTR("=")); CFIndex argsCounter = CFArrayGetCount(argsArray); if (argsCounter >= 0x1) { domain = (char*)SDMCFStringGetString(CFArrayGetValueAtIndex(argsArray, 0x0)); if (strncmp(domain, "all", sizeof(char)*0x3) == 0x0) { Safe(free, domain); domain = calloc(0x1, sizeof(char)*(strlen(kAllDomains)+0x1)); memcpy(domain, kAllDomains, strlen(kAllDomains)); } optionsEnable[OptionsQuery] = true; } if (argsCounter == 0x2) { key = (char*)SDMCFStringGetString(CFArrayGetValueAtIndex(argsArray, 0x1)); } else { key = calloc(0x1, sizeof(char)*(strlen(kAllKeys)+0x1)); memcpy(key, kAllKeys, strlen(kAllKeys)); } CFSafeRelease(argsArray); CFSafeRelease(argValue); } break; }; case 'a': { optionsEnable[OptionsApps] = true; break; }; case 'i': { optionsEnable[OptionsInfo] = true; break; }; case 'r': { if (optarg && !optionsEnable[OptionsRun]) { bundle = optarg; optionsEnable[OptionsRun] = true; } break; }; case 'p': { if (optarg && !optionsEnable[OptionsDiag]) { diagArg = optarg; optionsEnable[OptionsDiag] = true; } break; }; case 'x': { optionsEnable[OptionsDev] = true; break; } case 't': { if (optarg) { optionsEnable[OptionsInstall] = true; installPath = optarg; } break; } case 'c': { if (optarg) { optionsEnable[OptionsConfig] = true; installPath = optarg; } break; } default: { printf("--help for help"); break; }; } } if (optionsEnable[OptionsHelp]) { if (!help) { printf("%s [service|query] : list available services or queries\n",helpArg); printf("%s : list attached devices\n",listArg); printf("%s [UDID] : specify a device\n",deviceArg); printf("%s [service] : attach to [service]\n",attachArg); printf("%s <domain>=<key> : query value for <key> in <domain>, specify 'null' for global domain\n",queryArg); printf("%s : display installed apps\n",appsArg); printf("%s : display info of a device\n",infoArg); printf("%s [bundle id] : run an application with specified [bundle id]\n",runArg); printf("%s [sleep|reboot|shutdown] : perform diag power operations on a device\n",powerArg); printf("%s : setup device for development\n",devArg); printf("%s [.app path] : install specificed .app to a device\n",installArg); printf("%s [.mobileconfig path] : install specificed .mobileconfig to a device\n",profileArg); } else { if (strncmp(help, "service", strlen("service")) == 0x0) { printf(" shorthand : service identifier\n--------------------------------\n"); for (uint32_t i = 0x0; i < SDM_MD_Service_Count; i++) { printf("%10s : %s\n",SDMMDServiceIdentifiers[i].shorthand, SDMMDServiceIdentifiers[i].identifier); } } if (strncmp(help, "query", strlen("query")) == 0x0) { for (uint32_t i = 0x0; i < SDM_MD_Domain_Count; i++) { printf("Domain: %s\n",SDMMDKnownDomain[i].domain); for (uint32_t j = 0x0; j < SDMMDKnownDomain[i].keyCount; j++) { printf("\t%s\n",SDMMDKnownDomain[i].keys[j]); } printf("\n\n"); } } } } if (optionsEnable[OptionsList]) { ListConnectedDevices(); } if (optionsEnable[OptionsDevice]) { if (optionsEnable[OptionsInfo]) { } else if (optionsEnable[OptionsApps]) { LookupAppsOnDevice(udid); } else if (optionsEnable[OptionsAttach]) { PerformService(udid, service); } else if (optionsEnable[OptionsQuery]) { PerformQuery(udid, domain, key); } else if (optionsEnable[OptionsRun]) { RunAppOnDeviceWithIdentifier(udid, bundle); } else if (optionsEnable[OptionsDiag]) { if (diagArg) { if (strncmp(diagArg, "sleep", strlen("sleep")) == 0x0) { SendSleepToDevice(udid); } else if (strncmp(diagArg, "reboot", strlen("reboot")) == 0x0) { SendRebootToDevice(udid); } else if (strncmp(diagArg, "shutdown", strlen("shutdown")) == 0x0) { SendShutdownToDevice(udid); } } } else if (optionsEnable[OptionsDev]) { SetupDeviceForDevelopment(udid); } else if (optionsEnable[OptionsInstall]) { InstallAppToDevice(udid, installPath); } else if (optionsEnable[OptionsConfig]) { InstallProfileToDevice(udid, installPath); } } Safe(free, domain); Safe(free, key); return 0; }
return op; } // rdi, rsi, rdx SDMMD_AFCOperationRef SDMMD_AFCOperationCreateSetConnectionOptions() { // _AFCOperationCreateSetConnectionOptions SDMMD_AFCOperationRef op = calloc(1, sizeof(struct sdmmd_AFCOperation)); op->packet = calloc(1, sizeof(struct sdmmd_AFCPacket)); SDMMD_AFCHeaderInit(&(op->packet->header), SDMMD_AFC_Packet_SetConnectionOptions, sizeof(struct SDMMD_AFCPacketHeader), 0x0, 0x0); return op; } SDMMD_AFCOperationRef SDMMD_AFCOperationCreateRenamePath(CFStringRef old, CFStringRef new) { SDMMD_AFCOperationRef op = calloc(1, sizeof(struct sdmmd_AFCOperation)); op->packet = calloc(1, sizeof(struct sdmmd_AFCPacket)); char *oldPath = SDMCFStringGetString(old); char *newPath = SDMCFStringGetString(new); uint32_t path_length = (uint32_t)(strlen(oldPath)+1+(uint32_t)strlen(newPath)+1); op->packet->data = calloc(1, sizeof(char[path_length])); memcpy(&op->packet->data, oldPath, strlen(oldPath)); memcpy(&op->packet->data[strlen(oldPath)+1], newPath, strlen(newPath)); SDMMD_AFCHeaderInit(&(op->packet->header), SDMMD_AFC_Packet_RenamePath, path_length+sizeof(struct SDMMD_AFCPacketHeader), 0x0, 0x0); free(oldPath); free(newPath); return op; } SDMMD_AFCOperationRef SDMMD_AFCOperationCreateSetFSBlockSize() { // ___AFCConnectionSetBlockSize SDMMD_AFCOperationRef op = calloc(1, sizeof(struct sdmmd_AFCOperation)); op->packet = calloc(1, sizeof(struct sdmmd_AFCPacket)); uint32_t data_length = 8;