Subversion Repositories freemyipod

Rev

Rev 694 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
694 user890104 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
 
24
#include <stdio.h>
25
#include <string.h>
26
 
972 user890104 27
#include <libusb.h>
694 user890104 28
 
29
#include "usb.h"
30
 
31
int dfu_write(const unsigned char i, const unsigned int length, const unsigned char *data)
32
{
33
    return usb_control_transfer(0x21, 1, i, 0, (unsigned char *) data, length);
34
}
35
 
36
int dfu_read(unsigned char result[6])
37
{
38
    return usb_control_transfer(0xa1, 3, 0, 0, result, 6);
39
}
40
 
41
int dfu_send(const unsigned long int size, const unsigned char *data)
42
{
43
    unsigned char result[6];
44
    unsigned int i;
45
    int res;
46
 
47
    printf("Uploading... ");
48
    fflush(stdout);
49
 
50
    for (i = 0; i < (size + 4 + 2047) / 2048; ++i)
51
    {
52
        res = dfu_write(i, ((i + 1) * 2048 > size + 4) ? (size + 4) - (i * 2048) : 2048,
53
            (unsigned char *) (data + (i * 2048)));
54
 
55
        if (LIBUSB_SUCCESS > res)
56
        {
57
            return res;
58
        }
59
 
60
        memset(result, 0, sizeof(result));
61
 
62
        while ('\x05' != result[4])
63
        {
64
            res = dfu_read(result);
65
 
66
            if (LIBUSB_SUCCESS > res)
67
            {
68
                return res;
69
            }
70
        }
71
 
72
        printf("#");
73
        fflush(stdout);
74
    }
75
 
76
    res = dfu_write(i, 0, NULL);
77
 
78
    if (LIBUSB_SUCCESS > res)
79
    {
80
        return res;
81
    }
82
 
83
    memset(result, 0, sizeof(result));
84
 
85
    i = 0;
86
 
87
    while ('\x02' != result[4] && i++ < 1000)
88
    {
89
        dfu_read(result);
90
 
91
        if (LIBUSB_SUCCESS > res)
92
        {
93
            return res;
94
        }
95
    }
96
 
97
    if (1000 == i || '\x02' == result[4])
98
    {
99
        printf(" failed: %d / %d\n", result[4], result[0]);
100
    }
101
    else
102
    {
103
        printf(" OK\n");
104
    }
105
 
106
    return 0;
107
}