summaryrefslogtreecommitdiffstats
path: root/src/img3.c
blob: 902bea479f471450dc1d7a9225fcbdcc6174e2a8 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * img3.c
 * Functions for handling with Apple's IMG3 format
 *
 * 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 <string.h>

#include "img3.h"
#include "common.h"
#include "idevicerestore.h"

img3_file* img3_parse_file(char* data, int size) {
	int data_offset = 0;
	img3_header* header = (img3_header*) data;
	if (header->signature != kImg3Container) {
		error("ERROR: Invalid IMG3 file\n");
		return NULL;
	}

	img3_file* image = (img3_file*) malloc(sizeof(img3_file));
	if (image == NULL) {
		error("ERROR: Unable to allocate memory for IMG3 file\n");
		return NULL;
	}
	memset(image, '\0', sizeof(img3_file));

	image->header = (img3_header*) malloc(sizeof(img3_header));
	if (image->header == NULL) {
		error("ERROR: Unable to allocate memory for IMG3 header\n");
		img3_free(image);
		return NULL;
	}
	memcpy(image->header, data, sizeof(img3_header));
	data_offset += sizeof(img3_header);

	img3_element_header* current = NULL;
	while (data_offset < size) {
		current = (img3_element_header*) &data[data_offset];
		switch (current->signature) {
		case kTypeElement:
			image->type_element = img3_parse_element(&data[data_offset]);
			if (image->type_element == NULL) {
				error("ERROR: Unable to parse TYPE element\n");
				img3_free(image);
				return NULL;
			}
			debug("Parsed TYPE element\n");
			break;

		case kDataElement:
			image->data_element = img3_parse_element(&data[data_offset]);
			if (image->data_element == NULL) {
				error("ERROR: Unable to parse DATA element\n");
				img3_free(image);
				return NULL;
			}
			debug("Parsed DATA element\n");
			break;

		case kVersElement:
			image->vers_element = img3_parse_element(&data[data_offset]);
			if (image->vers_element == NULL) {
				error("ERROR: Unable to parse VERS element\n");
				img3_free(image);
				return NULL;
			}
			debug("Parsed VERS element\n");
			break;

		case kSepoElement:
			image->sepo_element = img3_parse_element(&data[data_offset]);
			if (image->sepo_element == NULL) {
				error("ERROR: Unable to parse SEPO element\n");
				img3_free(image);
				return NULL;
			}
			debug("Parsed SEPO element\n");
			break;

		case kBordElement:
			image->bord_element = img3_parse_element(&data[data_offset]);
			if (image->bord_element == NULL) {
				error("ERROR: Unable to parse BORD element\n");
				img3_free(image);
				return NULL;
			}
			debug("Parsed BORD element\n");
			break;

		case kKbagElement:
			if (image->kbag1_element == NULL) {
				image->kbag1_element = img3_parse_element(&data[data_offset]);
				if (image->kbag1_element == NULL) {
					error("ERROR: Unable to parse first KBAG element\n");
					img3_free(image);
					return NULL;
				}

			} else {
				image->kbag2_element = img3_parse_element(&data[data_offset]);
				if (image->kbag2_element == NULL) {
					error("ERROR: Unable to parse second KBAG element\n");
					img3_free(image);
					return NULL;
				}
			}
			debug("Parsed KBAG element\n");
			break;

		case kEcidElement:
			image->ecid_element = img3_parse_element(&data[data_offset]);
			if (image->ecid_element == NULL) {
				error("ERROR: Unable to parse ECID element\n");
				img3_free(image);
				return NULL;
			}
			debug("Parsed ECID element\n");
			break;

		case kShshElement:
			image->shsh_element = img3_parse_element(&data[data_offset]);
			if (image->shsh_element == NULL) {
				error("ERROR: Unable to parse SHSH element\n");
				img3_free(image);
				return NULL;
			}
			debug("Parsed SHSH element\n");
			break;

		case kCertElement:
			image->cert_element = img3_parse_element(&data[data_offset]);
			if (image->cert_element == NULL) {
				error("ERROR: Unable to parse CERT element\n");
				img3_free(image);
				return NULL;
			}
			debug("Parsed CERT element\n");
			break;

		default:
			error("ERROR: Unknown IMG3 element type\n");
			img3_free(image);
			return NULL;
		}
		data_offset += current->full_size;
	}

	return image;
}

img3_element* img3_parse_element(char* data) {
	img3_element_header* element_header = (img3_element_header*) data;
	img3_element* element = (img3_element*) malloc(sizeof(img3_element));
	if (element == NULL) {
		error("ERROR: Unable to allocate memory for IMG3 element\n");
		return NULL;
	}
	memset(element, '\0', sizeof(img3_element));

	element->data = (char*) malloc(element_header->full_size);
	if (element->data == NULL) {
		error("ERROR: Unable to allocate memory for IMG3 element data\n");
		free(element);
		return NULL;
	}
	memcpy(element->data, data, element_header->full_size);
	element->header = (img3_element_header*) element->data;
	element->type = (img3_element_type) element->header->signature;

	return element;
}

void img3_free(img3_file* image) {
	if (image != NULL) {
		if (image->header != NULL) {
			free(image->header);
		}

		if (image->type_element != NULL) {
			img3_free_element(image->type_element);
			image->type_element = NULL;
		}

		if (image->data_element != NULL) {
			img3_free_element(image->data_element);
			image->data_element = NULL;
		}

		if (image->vers_element != NULL) {
			img3_free_element(image->vers_element);
			image->vers_element = NULL;
		}

		if (image->sepo_element != NULL) {
			img3_free_element(image->sepo_element);
			image->sepo_element = NULL;
		}

		if (image->bord_element != NULL) {
			img3_free_element(image->bord_element);
			image->bord_element = NULL;
		}

		if (image->kbag1_element != NULL) {
			img3_free_element(image->kbag1_element);
			image->kbag1_element = NULL;
		}

		if (image->kbag2_element != NULL) {
			img3_free_element(image->kbag2_element);
			image->kbag2_element = NULL;
		}

		if (image->ecid_element != NULL) {
			img3_free_element(image->ecid_element);
			image->ecid_element = NULL;
		}

		if (image->shsh_element != NULL) {
			img3_free_element(image->shsh_element);
			image->shsh_element = NULL;
		}

		if (image->cert_element != NULL) {
			img3_free_element(image->cert_element);
			image->cert_element = NULL;
		}

		free(image);
		image = NULL;
	}
}

void img3_free_element(img3_element* element) {
	if (element != NULL) {
		if (element->data != NULL) {
			free(element->data);
			element->data = NULL;
		}
		free(element);
		element = NULL;
	}
}

int img3_replace_signature(img3_file* image, char* signature) {
	int offset = 0;
	img3_element* ecid = img3_parse_element(&signature[offset]);
	if (ecid == NULL || ecid->type != kEcidElement) {
		error("ERROR: Unable to find ECID element in signature\n");
		return -1;
	}
	offset += ecid->header->full_size;

	img3_element* shsh = img3_parse_element(&signature[offset]);
	if (shsh == NULL || shsh->type != kShshElement) {
		error("ERROR: Unable to find SHSH element in signature\n");
		return -1;
	}
	offset += shsh->header->full_size;

	img3_element* cert = img3_parse_element(&signature[offset]);
	if (cert == NULL || cert->type != kCertElement) {
		error("ERROR: Unable to find CERT element in signature\n");
		return -1;
	}
	offset += cert->header->full_size;

	if (image->ecid_element != NULL) {
		img3_free_element(image->ecid_element);
	}
	image->ecid_element = ecid;

	if (image->shsh_element != NULL) {
		img3_free_element(image->shsh_element);
	}
	image->shsh_element = shsh;

	if (image->cert_element != NULL) {
		img3_free_element(image->cert_element);
	}
	image->cert_element = cert;

	return 0;
}

int img3_get_data(img3_file* image, char** pdata, int* psize) {
	int offset = 0;
	int size = sizeof(img3_header);

	// Add up the size of the image first so we can allocate our memory
	if (image->type_element != NULL) {
		size += image->type_element->header->full_size;
	}
	if (image->data_element != NULL) {
		size += image->data_element->header->full_size;
	}
	if (image->vers_element != NULL) {
		size += image->vers_element->header->full_size;
	}
	if (image->sepo_element != NULL) {
		size += image->sepo_element->header->full_size;
	}
	if (image->bord_element != NULL) {
		size += image->bord_element->header->full_size;
	}
	if (image->kbag1_element != NULL) {
		size += image->kbag1_element->header->full_size;
	}
	if (image->kbag2_element != NULL) {
		size += image->kbag2_element->header->full_size;
	}
	if (image->ecid_element != NULL) {
		size += image->ecid_element->header->full_size;
	}
	if (image->shsh_element != NULL) {
		size += image->shsh_element->header->full_size;
	}
	if (image->cert_element != NULL) {
		size += image->cert_element->header->full_size;
	}

	char* data = (char*) malloc(size);
	if (data == NULL) {
		error("ERROR: Unable to allocate memory for IMG3 data\n");
		return -1;
	}

	// Add data to our new header (except shsh_offset)
	img3_header* header = (img3_header*) data;
	header->full_size = size;
	header->signature = image->header->signature;
	header->data_size = size - sizeof(img3_header);
	header->image_type = image->header->image_type;
	offset += sizeof(img3_header);

	// Copy each section over to the new buffer
	if (image->type_element != NULL) {
		memcpy(&data[offset], image->type_element->data, image->type_element->header->full_size);
		offset += image->type_element->header->full_size;
	}
	if (image->data_element != NULL) {
		memcpy(&data[offset], image->data_element->data, image->data_element->header->full_size);
		offset += image->data_element->header->full_size;
	}
	if (image->vers_element != NULL) {
		memcpy(&data[offset], image->vers_element->data, image->vers_element->header->full_size);
		offset += image->vers_element->header->full_size;
	}
	if (image->sepo_element != NULL) {
		memcpy(&data[offset], image->sepo_element->data, image->sepo_element->header->full_size);
		offset += image->sepo_element->header->full_size;
	}
	if (image->bord_element != NULL) {
		memcpy(&data[offset], image->bord_element->data, image->bord_element->header->full_size);
		offset += image->bord_element->header->full_size;
	}
	if (image->kbag1_element != NULL) {
		memcpy(&data[offset], image->kbag1_element->data, image->kbag1_element->header->full_size);
		offset += image->kbag1_element->header->full_size;
	}
	if (image->kbag2_element != NULL) {
		memcpy(&data[offset], image->kbag2_element->data, image->kbag2_element->header->full_size);
		offset += image->kbag2_element->header->full_size;
	}
	if (image->ecid_element != NULL) {
		memcpy(&data[offset], image->ecid_element->data, image->ecid_element->header->full_size);
		offset += image->ecid_element->header->full_size;
	}
	if (image->shsh_element != NULL) {
		memcpy(&data[offset], image->shsh_element->data, image->shsh_element->header->full_size);
		header->shsh_offset = offset - sizeof(img3_header);
		offset += image->shsh_element->header->full_size;
	}
	if (image->cert_element != NULL) {
		memcpy(&data[offset], image->cert_element->data, image->cert_element->header->full_size);
		offset += image->cert_element->header->full_size;
	}

	if (offset != size) {
		error("ERROR: Incorrectly sized image data\n");
		free(data);
		*pdata = 0;
		*psize = 0;
		return -1;
	}

	*pdata = data;
	*psize = size;
	return 0;
}