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 <stdio.h>
2
#include <string.h>
3
 
4
#include <libusb-1.0/libusb.h>
5
 
6
#include "usb.h"
7
 
8
int dfu_write(const unsigned char i, const unsigned int length, const unsigned char *data) {
9
	return usb_control_transfer(0x21, 1, i, 0, (unsigned char *) data, length);
10
}
11
 
12
int dfu_read(unsigned char result[6]) {
13
	return usb_control_transfer(0xa1, 3, 0, 0, result, 6);
14
}
15
 
16
int dfu_send(const unsigned long int size, const unsigned char *data) {
17
	unsigned char result[6];
18
	unsigned int i;
19
	int res;
20
 
21
	printf("Uploading... ");
22
	fflush(stdout);
23
 
24
	for (i = 0; i < (size + 4 + 2047) / 2048; ++i) {
25
		res = dfu_write(i, ((i + 1) * 2048 > size + 4) ? (size + 4) - (i * 2048) : 2048, (unsigned char *) (data + (i * 2048)));
26
 
27
		if (LIBUSB_SUCCESS > res) {
28
			return res;
29
		}
30
 
31
		memset(result, 0, sizeof(result));
32
 
33
		while ('\x05' != result[4]) {
34
			res = dfu_read(result);
35
 
36
			if (LIBUSB_SUCCESS > res) {
37
				return res;
38
			}
39
		}
40
 
41
		printf("#");
42
		fflush(stdout);
43
	}
44
 
45
	res = dfu_write(i, 0, NULL);
46
 
47
	if (LIBUSB_SUCCESS > res) {
48
		return res;
49
	}
50
 
51
	memset(result, 0, sizeof(result));
52
 
53
	i = 0;
54
 
55
	while ('\x02' != result[4] && i++ < 1000) {
56
		dfu_read(result);
57
 
58
		if (LIBUSB_SUCCESS > res) {
59
			return res;
60
		}
61
	}
62
 
63
	if (1000 == i || '\x02' == result[4]) {
64
		printf(" failed: %d / %d\n", result[4], result[0]);
65
	}
66
	else {
67
		printf(" OK\n");
68
	}
69
 
70
	return 0;
71
}