Subversion Repositories freemyipod

Rev

Rev 693 | Rev 754 | Go to most recent revision | Details | Compare with Previous | 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
 
694 user890104 7
//
8
//
9
//    Copyright 2011 user890104
10
//
11
//
12
//    This file is part of ipoddfu.
13
//
14
//    ipoddfu is free software: you can redistribute it and/or
15
//    modify it under the terms of the GNU General Public License as
16
//    published by the Free Software Foundation, either version 2 of the
17
//    License, or (at your option) any later version.
18
//
19
//    ipoddfu is distributed in the hope that it will be useful,
20
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
21
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22
//    See the GNU General Public License for more details.
23
//
24
//    You should have received a copy of the GNU General Public License along
25
//    with ipoddfu.  If not, see <http://www.gnu.org/licenses/>.
26
//
27
//
28
 
29
 
693 user890104 30
#include "usb.h"
31
#include "dfu.h"
32
#include "crc32.h"
33
#include "misc.h"
34
#include "ipoddfu.h"
35
 
694 user890104 36
int main(int argc, char *argv[])
37
{
38
    int res = 0;
39
    unsigned char reattach = 0, *data;
40
    FILE *fp;
41
    long int size;
42
    unsigned int checksum;
43
 
44
    if (2 != argc)
45
    {
46
        fprintf(stderr, "usage: %s <file>\n", argv[0]);
47
 
48
        return 1;
49
    }
50
 
51
    fp = fopen(argv[1], "r");
52
 
53
    if (!fp)
54
    {
55
        perror("fopen");
56
 
57
        return 1;
58
    }
59
 
60
    size = fgetsize(fp);
61
 
62
    if (-1L == size)
63
    {
64
        return 1;
65
    }
66
 
67
    data = (unsigned char *) malloc(size + 4);
68
 
69
    if ((unsigned long int) size != fread(data, sizeof(unsigned char), size, fp))
70
    {
71
        perror("fread");
72
 
73
        return 1;
74
    }
75
 
76
    if (fclose(fp))
77
    {
78
        perror("fclose");
79
 
80
        return 1;
81
    }
82
 
83
    crc32_init();
84
 
85
    checksum = crc32(data, size, CRC32_DEFAULT_SEED);
86
 
87
    memcpy(data + size, &checksum, 4);
88
 
89
    res = usb_init();
90
 
91
    if (LIBUSB_SUCCESS == res)
92
    {
93
        res = usb_find(&reattach);
94
    }
95
 
96
    if (LIBUSB_SUCCESS == res)
97
    {
98
        res = dfu_send((unsigned long int) size + 4, data);
99
    }
100
 
101
    if (data)
102
    {
103
        free(data);
104
    }
105
 
106
    if (0 != res)
107
    {
108
        print_error(res);
109
    }
110
 
111
    if (usb_handle)
112
    {
113
        usb_close(reattach);
114
    }
115
 
116
    if (usb_ctx)
117
    {
118
        usb_exit();
119
    }
120
 
121
    if (res < 0)
122
    {
123
        res = -res;
124
    }
125
 
126
    return res;
693 user890104 127
}