summaryrefslogtreecommitdiffstats
path: root/src/recovery.c
blob: 3bfb97e8c4abcc2390b6fa1db8cc2a84fd1e30bf (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * recovery.c
 * Functions for handling idevices in recovery mode
 *
 * Copyright (c) 2010 Joshua Hill. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <libirecovery.h>

#include "tss.h"
#include "img3.h"
#include "recovery.h"
#include "idevicerestore.h"

int recovery_check_mode() {
	irecv_client_t recovery = NULL;
	irecv_error_t recovery_error = IRECV_E_SUCCESS;

	recovery_error = irecv_open(&recovery);
	if (recovery_error != IRECV_E_SUCCESS) {
		return -1;
	}

	if(recovery->mode == kDfuMode) {
		irecv_close(recovery);
		return -1;
	}

	irecv_close(recovery);
	recovery = NULL;
	return 0;
}

int recovery_enter_restore(const char* ipsw, plist_t tss) {
	// upload data to make device boot restore mode
	if (recovery_send_ibec(ipsw, tss) < 0) {
		error("ERROR: Unable to send iBEC\n");
		return -1;
	}
	sleep(1);

	if (recovery_send_applelogo(ipsw, tss) < 0) {
		error("ERROR: Unable to send AppleLogo\n");
		return -1;
	}

	if (recovery_send_devicetree(ipsw, tss) < 0) {
		error("ERROR: Unable to send DeviceTree\n");
		return -1;
	}

	if (recovery_send_ramdisk(ipsw, tss) < 0) {
		error("ERROR: Unable to send Ramdisk\n");
		return -1;
	}

	// for some reason iboot requires a hard reset after ramdisk
	//   or things start getting wacky
	printf("Please unplug your device, then plug it back in\n");
	printf("Hit any key to continue...");
	getchar();

	if (recovery_send_kernelcache(ipsw, tss) < 0) {
		error("ERROR: Unable to send KernelCache\n");
		return -1;
	}

	return 0;
}

int recovery_send_signed_component(irecv_client_t client, const char* ipsw, plist_t tss, char* component) {
	int size = 0;
	char* data = NULL;
	char* path = NULL;
	char* blob = NULL;
	img3_file* img3 = NULL;
	irecv_error_t error = 0;

	if (get_signed_component_by_name(ipsw, tss, component, &data, &size) < 0) {
		error("ERROR: Unable to get signed component: %s\n", component);
		return -1;
	}

	info("Sending %s...\n", component);
	error = irecv_send_buffer(client, data, size);
	if (error != IRECV_E_SUCCESS) {
		error("ERROR: Unable to send IMG3: %s\n", path);
		img3_free(img3);
		free(data);
		free(path);
		return -1;
	}

	if (data) {
		free(data);
		data = NULL;
	}

	return 0;
}

irecv_error_t recovery_open_with_timeout(irecv_client_t* client) {
	int i = 0;
	irecv_error_t error = 0;
	for (i = 10; i > 0; i--) {
		error = irecv_open(client);
		if (error == IRECV_E_SUCCESS) {
			return error;
		}

		sleep(2);
		info("Retrying connection...\n");
	}

	error("ERROR: Unable to connect to recovery device.\n");
	return error;
}

int recovery_send_ibec(const char* ipsw, plist_t tss) {
	irecv_error_t error = 0;
	irecv_client_t client = NULL;
	char* component = "iBEC";

	error = recovery_open_with_timeout(&client);
	if (error != IRECV_E_SUCCESS) {
		return -1;
	}

	error = irecv_send_command(client, "setenv auto-boot true");
	if (error != IRECV_E_SUCCESS) {
		error("ERROR: Unable to set auto-boot environmental variable\n");
		irecv_close(client);
		client = NULL;
		return -1;
	}

	error = irecv_send_command(client, "saveenv");
	if (error != IRECV_E_SUCCESS) {
		error("ERROR: Unable to save environmental variable\n");
		irecv_close(client);
		client = NULL;
		return -1;
	}

	if (recovery_send_signed_component(client, ipsw, tss, component) < 0) {
		error("ERROR: Unable to send %s to device.\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	error = irecv_send_command(client, "go");
	if (error != IRECV_E_SUCCESS) {
		error("ERROR: Unable to execute %s\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	if (client) {
		irecv_close(client);
		client = NULL;
	}
	return 0;
}

int recovery_send_applelogo(const char* ipsw, plist_t tss) {
	irecv_error_t error = 0;
	irecv_client_t client = NULL;
	char* component = "AppleLogo";

	info("Sending %s...\n", component);

	error = recovery_open_with_timeout(&client);
	if (error != IRECV_E_SUCCESS) {
		return -1;
	}

	if (recovery_send_signed_component(client, ipsw, tss, component) < 0) {
		error("ERROR: Unable to send %s to device.\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	error = irecv_send_command(client, "setpicture 1");
	if (error != IRECV_E_SUCCESS) {
		error("ERROR: Unable to set %s\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	error = irecv_send_command(client, "bgcolor 0 0 0");
	if (error != IRECV_E_SUCCESS) {
		error("ERROR: Unable to display %s\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	if (client) {
		irecv_close(client);
		client = NULL;
	}
	return 0;
}

int recovery_send_devicetree(const char* ipsw, plist_t tss) {
	irecv_error_t error = 0;
	irecv_client_t client = NULL;
	char *component = "RestoreDeviceTree";

	error = recovery_open_with_timeout(&client);
	if (error != IRECV_E_SUCCESS) {
		return -1;
	}

	if (recovery_send_signed_component(client, ipsw, tss, component) < 0) {
		error("ERROR: Unable to send %s to device.\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	error = irecv_send_command(client, "devicetree");
	if (error != IRECV_E_SUCCESS) {
		error("ERROR: Unable to execute %s\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	if (client) {
		irecv_close(client);
		client = NULL;
	}
	return 0;
}

int recovery_send_ramdisk(const char* ipsw, plist_t tss) {
	irecv_error_t error = 0;
	irecv_client_t client = NULL;
	char *component = "RestoreRamDisk";

	error = recovery_open_with_timeout(&client);
	if (error != IRECV_E_SUCCESS) {
		return -1;
	}

	if (recovery_send_signed_component(client, ipsw, tss, component) < 0) {
		error("ERROR: Unable to send %s to device.\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	error = irecv_send_command(client, "ramdisk");
	if (error != IRECV_E_SUCCESS) {
		error("ERROR: Unable to execute %s\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	if (client) {
		irecv_close(client);
		client = NULL;
	}
	return 0;
}

int recovery_send_kernelcache(const char* ipsw, plist_t tss) {
	irecv_error_t error = 0;
	irecv_client_t client = NULL;
	char *component = "RestoreKernelCache";

	error = recovery_open_with_timeout(&client);
	if (error != IRECV_E_SUCCESS) {
		return -1;
	}

	if (recovery_send_signed_component(client, ipsw, tss, component) < 0) {
		error("ERROR: Unable to send %s to device.\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	error = irecv_send_command(client, "bootx");
	if (error != IRECV_E_SUCCESS) {
		error("ERROR: Unable to execute %s\n", component);
		irecv_close(client);
		client = NULL;
		return -1;
	}

	if (client) {
		irecv_close(client);
		client = NULL;
	}
	return 0;
}


int recovery_get_ecid(uint64_t* ecid) {
	irecv_client_t recovery = NULL;
	if(recovery_open_with_timeout(&recovery) < 0) {
		return -1;
	}

	irecv_error_t error = irecv_get_ecid(recovery, ecid);
	if (error != IRECV_E_SUCCESS) {
		irecv_close(recovery);
		return -1;
	}

	irecv_close(recovery);
	recovery = NULL;
	return 0;
}

int recovery_get_cpid(uint32_t* cpid) {
	irecv_client_t recovery = NULL;
	if(recovery_open_with_timeout(&recovery) < 0) {
		return -1;
	}

	irecv_error_t error = irecv_get_cpid(recovery, cpid);
	if (error != IRECV_E_SUCCESS) {
		irecv_close(recovery);
		return -1;
	}

	irecv_close(recovery);
	recovery = NULL;
	return 0;
}

int recovery_get_bdid(uint32_t* bdid) {
	irecv_client_t recovery = NULL;
	if(recovery_open_with_timeout(&recovery) < 0) {
		return -1;
	}

	irecv_error_t error = irecv_get_bdid(recovery, bdid);
	if (error != IRECV_E_SUCCESS) {
		irecv_close(recovery);
		return -1;
	}

	irecv_close(recovery);
	recovery = NULL;
	return 0;
}