forked from jkcoxson/idevice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmount_personalized.c
More file actions
220 lines (194 loc) · 5.99 KB
/
mount_personalized.c
File metadata and controls
220 lines (194 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "idevice.h"
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// Helper function to read file contents
static uint8_t *read_file(const char *path, size_t *len) {
FILE *file = fopen(path, "rb");
if (!file) {
perror("Failed to open file");
return NULL;
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
uint8_t *buffer = malloc(file_size);
if (!buffer) {
fclose(file);
return NULL;
}
if (fread(buffer, 1, file_size, file) != (size_t)file_size) {
free(buffer);
fclose(file);
return NULL;
}
fclose(file);
*len = file_size;
return buffer;
}
// Callback function to show progress
void progress_callback(size_t progress, size_t total, void *context) {
size_t percent = (progress * 100) / total;
printf("\rUpload progress: %zu%%", percent);
fflush(stdout);
// Print newline when complete
if (progress == total) {
printf("\n");
}
}
int main(int argc, char **argv) {
if (argc < 5) {
fprintf(stderr,
"Usage: %s <device_ip> <image> <trustcache> <build_manifest> "
"[pairing_file]\n",
argv[0]);
return 1;
}
const char *device_ip = argv[1];
const char *image_path = argv[2];
const char *trustcache_path = argv[3];
const char *manifest_path = argv[4];
const char *pairing_file_path = argc > 5 ? argv[5] : "pairing_file.plist";
// Initialize logger
idevice_init_logger(Debug, Disabled, NULL);
// Read files
size_t image_len, trustcache_len, manifest_len;
uint8_t *image = read_file(image_path, &image_len);
uint8_t *trustcache = read_file(trustcache_path, &trustcache_len);
uint8_t *build_manifest = read_file(manifest_path, &manifest_len);
if (!image || !trustcache || !build_manifest) {
fprintf(stderr, "Failed to read one or more files\n");
free(image);
free(trustcache);
free(build_manifest);
return 1;
}
// Create the socket address
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(LOCKDOWN_PORT);
inet_pton(AF_INET, device_ip, &addr.sin_addr);
// Read pairing file
IdevicePairingFile *pairing_file = NULL;
IdeviceFfiError *err =
idevice_pairing_file_read(pairing_file_path, &pairing_file);
if (err != NULL) {
fprintf(stderr, "Failed to read pairing file: [%d] %s", err->code,
err->message);
idevice_error_free(err);
free(image);
free(trustcache);
free(build_manifest);
return 1;
}
// Create TCP provider
IdeviceProviderHandle *provider = NULL;
err = idevice_tcp_provider_new((struct sockaddr *)&addr, pairing_file,
"ImageMounterTest", &provider);
if (err != NULL) {
fprintf(stderr, "Failed to create TCP provider: [%d] %s", err->code,
err->message);
idevice_error_free(err);
idevice_pairing_file_free(pairing_file);
free(image);
free(trustcache);
free(build_manifest);
return 1;
}
// Read pairing file
IdevicePairingFile *pairing_file_2 = NULL;
err = idevice_pairing_file_read(pairing_file_path, &pairing_file_2);
if (err != NULL) {
fprintf(stderr, "Failed to read pairing file: [%d] %s", err->code,
err->message);
idevice_error_free(err);
free(image);
free(trustcache);
free(build_manifest);
return 1;
}
// Connect to lockdownd
LockdowndClientHandle *lockdown_client = NULL;
err = lockdownd_connect(provider, &lockdown_client);
if (err != NULL) {
fprintf(stderr, "Failed to connect to lockdownd: [%d] %s", err->code,
err->message);
idevice_error_free(err);
idevice_provider_free(provider);
free(image);
free(trustcache);
free(build_manifest);
return 1;
}
// Start session
err = lockdownd_start_session(lockdown_client, pairing_file_2);
if (err != NULL) {
fprintf(stderr, "Failed to start session: [%d] %s", err->code,
err->message);
idevice_error_free(err);
lockdownd_client_free(lockdown_client);
idevice_provider_free(provider);
idevice_pairing_file_free(pairing_file_2);
free(image);
free(trustcache);
free(build_manifest);
return 1;
}
idevice_pairing_file_free(pairing_file_2);
// Get UniqueChipID
plist_t unique_chip_id_plist = NULL;
err = lockdownd_get_value(lockdown_client, "UniqueChipID", NULL,
&unique_chip_id_plist);
if (err != NULL) {
fprintf(stderr, "Failed to get UniqueChipID: [%d] %s", err->code,
err->message);
idevice_error_free(err);
lockdownd_client_free(lockdown_client);
idevice_provider_free(provider);
free(image);
free(trustcache);
free(build_manifest);
return 1;
}
uint64_t unique_chip_id = 0;
plist_get_uint_val(unique_chip_id_plist, &unique_chip_id);
plist_free(unique_chip_id_plist);
// Connect to image mounter
ImageMounterHandle *mounter_client = NULL;
err = image_mounter_connect(provider, &mounter_client);
if (err != NULL) {
fprintf(stderr, "Failed to connect to image mounter: [%d] %s", err->code,
err->message);
idevice_error_free(err);
lockdownd_client_free(lockdown_client);
idevice_provider_free(provider);
free(image);
free(trustcache);
free(build_manifest);
return 1;
}
// Mount personalized image with progress callback
err = image_mounter_mount_personalized_with_callback(
mounter_client, provider, image, image_len, trustcache, trustcache_len,
build_manifest, manifest_len,
NULL, // info_plist
unique_chip_id, progress_callback, NULL);
if (err != NULL) {
fprintf(stderr, "Failed to mount personalized image: [%d] %s", err->code,
err->message);
idevice_error_free(err);
} else {
printf("Successfully mounted personalized image!\n");
}
// Cleanup
image_mounter_free(mounter_client);
lockdownd_client_free(lockdown_client);
idevice_provider_free(provider);
free(image);
free(trustcache);
free(build_manifest);
return err == NULL ? 0 : 1;
}