Subversion Repositories freemyipod

Rev

Rev 694 | Go to most recent revision | 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
 
754 user890104 24
#include <stdlib.h>
25
#include <stdio.h>
26
#include <string.h>
27
 
28
#include <libusb-1.0/libusb.h>
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
}