Subversion Repositories freemyipod

Rev

Rev 693 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 693 Rev 694
Line -... Line 1...
-
 
1
//
-
 
2
//
-
 
3
//    Copyright 2011 user890104
-
 
4
//
-
 
5
//
-
 
6
//    This file is part of ipoddfu.
-
 
7
//
-
 
8
//    ipoddfu is free software: you can redistribute it and/or
-
 
9
//    modify it under the terms of the GNU General Public License as
-
 
10
//    published by the Free Software Foundation, either version 2 of the
-
 
11
//    License, or (at your option) any later version.
-
 
12
//
-
 
13
//    ipoddfu is distributed in the hope that it will be useful,
-
 
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
-
 
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-
 
16
//    See the GNU General Public License for more details.
-
 
17
//
-
 
18
//    You should have received a copy of the GNU General Public License along
-
 
19
//    with ipoddfu.  If not, see <http://www.gnu.org/licenses/>.
-
 
20
//
-
 
21
//
-
 
22
 
-
 
23
 
1
#include <stdio.h>
24
#include <stdio.h>
2
#include <string.h>
25
#include <string.h>
3
 
26
 
4
#include <libusb-1.0/libusb.h>
27
#include <libusb-1.0/libusb.h>
5
 
28
 
6
#include "usb.h"
29
#include "usb.h"
7
 
30
 
8
int dfu_write(const unsigned char i, const unsigned int length, const unsigned char *data) {
31
int dfu_write(const unsigned char i, const unsigned int length, const unsigned char *data)
-
 
32
{
9
	return usb_control_transfer(0x21, 1, i, 0, (unsigned char *) data, length);
33
    return usb_control_transfer(0x21, 1, i, 0, (unsigned char *) data, length);
10
}
34
}
11
 
35
 
12
int dfu_read(unsigned char result[6]) {
36
int dfu_read(unsigned char result[6])
-
 
37
{
13
	return usb_control_transfer(0xa1, 3, 0, 0, result, 6);
38
    return usb_control_transfer(0xa1, 3, 0, 0, result, 6);
14
}
39
}
15
 
40
 
16
int dfu_send(const unsigned long int size, const unsigned char *data) {
41
int dfu_send(const unsigned long int size, const unsigned char *data)
-
 
42
{
17
	unsigned char result[6];
43
    unsigned char result[6];
18
	unsigned int i;
44
    unsigned int i;
19
	int res;
45
    int res;
20
	
46
    
21
	printf("Uploading... ");
47
    printf("Uploading... ");
22
	fflush(stdout);
48
    fflush(stdout);
23
 
49
 
24
	for (i = 0; i < (size + 4 + 2047) / 2048; ++i) {
50
    for (i = 0; i < (size + 4 + 2047) / 2048; ++i)
-
 
51
    {
25
		res = dfu_write(i, ((i + 1) * 2048 > size + 4) ? (size + 4) - (i * 2048) : 2048, (unsigned char *) (data + (i * 2048)));
52
        res = dfu_write(i, ((i + 1) * 2048 > size + 4) ? (size + 4) - (i * 2048) : 2048,
-
 
53
            (unsigned char *) (data + (i * 2048)));
26
		
54
        
27
		if (LIBUSB_SUCCESS > res) {
55
        if (LIBUSB_SUCCESS > res)
-
 
56
        {
28
			return res;
57
            return res;
29
		}
58
        }
30
		
59
        
31
		memset(result, 0, sizeof(result));
60
        memset(result, 0, sizeof(result));
32
		
61
        
33
		while ('\x05' != result[4]) {
62
        while ('\x05' != result[4])
-
 
63
        {
34
			res = dfu_read(result);
64
            res = dfu_read(result);
35
		
65
        
36
			if (LIBUSB_SUCCESS > res) {
66
            if (LIBUSB_SUCCESS > res)
-
 
67
            {
37
				return res;
68
                return res;
38
			}
69
            }
39
		}
70
        }
40
		
71
        
41
		printf("#");
72
        printf("#");
42
		fflush(stdout);
73
        fflush(stdout);
43
	}
74
    }
44
	
75
    
45
	res = dfu_write(i, 0, NULL);
76
    res = dfu_write(i, 0, NULL);
46
 
77
 
47
	if (LIBUSB_SUCCESS > res) {
78
    if (LIBUSB_SUCCESS > res)
-
 
79
    {
48
		return res;
80
        return res;
49
	}
81
    }
50
	
82
    
51
	memset(result, 0, sizeof(result));
83
    memset(result, 0, sizeof(result));
52
 
84
 
53
	i = 0;
85
    i = 0;
54
 
86
 
55
	while ('\x02' != result[4] && i++ < 1000) {
87
    while ('\x02' != result[4] && i++ < 1000)
-
 
88
    {
56
		dfu_read(result);
89
        dfu_read(result);
57
		
90
        
58
		if (LIBUSB_SUCCESS > res) {
91
        if (LIBUSB_SUCCESS > res)
-
 
92
        {
59
			return res;
93
            return res;
60
		}
94
        }
61
	}
95
    }
62
 
96
 
63
	if (1000 == i || '\x02' == result[4]) {
97
    if (1000 == i || '\x02' == result[4])
-
 
98
    {
64
		printf(" failed: %d / %d\n", result[4], result[0]);
99
        printf(" failed: %d / %d\n", result[4], result[0]);
65
	}
100
    }
-
 
101
    else
66
	else {
102
    {
67
		printf(" OK\n");
103
        printf(" OK\n");
68
	}
104
    }
69
	
105
    
70
	return 0;
106
    return 0;
71
}
107
}