Subversion Repositories freemyipod

Rev

Rev 694 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
693 user890104 1
#include <stdlib.h>
2
#include <stdio.h>
3
#include <string.h>
4
 
5
#include <libusb-1.0/libusb.h>
6
 
7
#include "usb.h"
8
#include "dfu.h"
9
#include "crc32.h"
10
#include "misc.h"
11
#include "ipoddfu.h"
12
 
13
int main(int argc, char *argv[]) {
14
	int res = 0;
15
	unsigned char reattach = 0, *data;
16
	FILE *fp;
17
	long int size;
18
	unsigned int checksum;
19
 
20
	if (2 != argc) {
21
		fprintf(stderr, "usage: %s <file>\n", argv[0]);
22
 
23
		return 1;
24
	}
25
 
26
	fp = fopen(argv[1], "r");
27
 
28
	if (!fp) {
29
		perror("fopen");
30
 
31
		return 1;
32
	}
33
 
34
	size = fgetsize(fp);
35
 
36
	if (-1L == size) {
37
		return 1;
38
	}
39
 
40
	data = (unsigned char *) malloc(size + 4);
41
 
42
	if ((unsigned long int) size != fread(data, sizeof(unsigned char), size, fp)) {
43
		perror("fread");
44
 
45
		return 1;
46
	}
47
 
48
	if (fclose(fp)) {
49
		perror("fclose");
50
 
51
		return 1;
52
	}
53
 
54
	crc32_init();
55
 
56
	checksum = crc32(data, size, CRC32_DEFAULT_SEED);
57
 
58
	memcpy(data + size, &checksum, 4);
59
 
60
	res = usb_init();
61
 
62
	if (LIBUSB_SUCCESS == res) {
63
		res = usb_find(&reattach);
64
	}
65
 
66
	if (LIBUSB_SUCCESS == res) {
67
		res = dfu_send((unsigned long int) size + 4, data);
68
	}
69
 
70
	if (data) {
71
		free(data);
72
	}
73
 
74
	if (0 != res) {
75
		print_error(res);
76
	}
77
 
78
	if (usb_handle) {
79
		usb_close(reattach);
80
	}
81
 
82
	if (usb_ctx) {
83
		usb_exit();
84
	}
85
 
86
	if (res < 0) {
87
		res = -res;
88
	}
89
 
90
	return res;
91
}