Subversion Repositories freemyipod

Rev

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

Rev 693 Rev 694
Line 2... Line 2...
2
#include <stdio.h>
2
#include <stdio.h>
3
#include <string.h>
3
#include <string.h>
4
 
4
 
5
#include <libusb-1.0/libusb.h>
5
#include <libusb-1.0/libusb.h>
6
 
6
 
-
 
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
 
7
#include "usb.h"
30
#include "usb.h"
8
#include "dfu.h"
31
#include "dfu.h"
9
#include "crc32.h"
32
#include "crc32.h"
10
#include "misc.h"
33
#include "misc.h"
11
#include "ipoddfu.h"
34
#include "ipoddfu.h"
12
 
35
 
13
int main(int argc, char *argv[]) {
36
int main(int argc, char *argv[])
-
 
37
{
14
	int res = 0;
38
    int res = 0;
15
	unsigned char reattach = 0, *data;
39
    unsigned char reattach = 0, *data;
16
	FILE *fp;
40
    FILE *fp;
17
	long int size;
41
    long int size;
18
	unsigned int checksum;
42
    unsigned int checksum;
19
		
43
        
20
	if (2 != argc) {
44
    if (2 != argc)
-
 
45
    {
21
		fprintf(stderr, "usage: %s <file>\n", argv[0]);
46
        fprintf(stderr, "usage: %s <file>\n", argv[0]);
22
		
47
        
23
		return 1;
48
        return 1;
24
	}
49
    }
25
	
50
    
26
	fp = fopen(argv[1], "r");
51
    fp = fopen(argv[1], "r");
27
	
52
    
28
	if (!fp) {
53
    if (!fp)
-
 
54
    {
29
		perror("fopen");
55
        perror("fopen");
30
		
56
        
31
		return 1;
57
        return 1;
32
	}
58
    }
33
	
59
    
34
	size = fgetsize(fp);
60
    size = fgetsize(fp);
35
	
61
    
36
	if (-1L == size) {
62
    if (-1L == size)
-
 
63
    {
37
		return 1;
64
        return 1;
38
	}
65
    }
39
	
66
    
40
	data = (unsigned char *) malloc(size + 4);
67
    data = (unsigned char *) malloc(size + 4);
41
	
68
    
42
	if ((unsigned long int) size != fread(data, sizeof(unsigned char), size, fp)) {
69
    if ((unsigned long int) size != fread(data, sizeof(unsigned char), size, fp))
-
 
70
    {
43
		perror("fread");
71
        perror("fread");
44
		
72
        
45
		return 1;
73
        return 1;
46
	}
74
    }
47
	
75
    
48
	if (fclose(fp)) {
76
    if (fclose(fp))
-
 
77
    {
49
		perror("fclose");
78
        perror("fclose");
50
		
79
        
51
		return 1;
80
        return 1;
52
	}
81
    }
53
	
82
    
54
	crc32_init();
83
    crc32_init();
55
	
84
    
56
	checksum = crc32(data, size, CRC32_DEFAULT_SEED);
85
    checksum = crc32(data, size, CRC32_DEFAULT_SEED);
57
	
86
    
58
	memcpy(data + size, &checksum, 4);
87
    memcpy(data + size, &checksum, 4);
59
	
88
    
60
	res = usb_init();
89
    res = usb_init();
61
	
90
    
62
	if (LIBUSB_SUCCESS == res) {
91
    if (LIBUSB_SUCCESS == res)
-
 
92
    {
63
		res = usb_find(&reattach);
93
        res = usb_find(&reattach);
64
	}
94
    }
65
	
95
    
66
	if (LIBUSB_SUCCESS == res) {
96
    if (LIBUSB_SUCCESS == res)
-
 
97
    {
67
		res = dfu_send((unsigned long int) size + 4, data);
98
        res = dfu_send((unsigned long int) size + 4, data);
68
	}
99
    }
69
	
100
    
70
	if (data) {
101
    if (data)
-
 
102
    {
71
		free(data);
103
        free(data);
72
	}
104
    }
73
	
105
    
74
	if (0 != res) {
106
    if (0 != res)
-
 
107
    {
75
		print_error(res);
108
        print_error(res);
76
	}
109
    }
77
	
110
    
78
	if (usb_handle) {
111
    if (usb_handle)
-
 
112
    {
79
		usb_close(reattach);
113
        usb_close(reattach);
80
	}
114
    }
81
	
115
    
82
	if (usb_ctx) {
116
    if (usb_ctx)
-
 
117
    {
83
		usb_exit();
118
        usb_exit();
84
	}
119
    }
85
	
120
    
86
	if (res < 0) {
121
    if (res < 0)
-
 
122
    {
87
		res = -res;
123
        res = -res;
88
	}
124
    }
89
	
125
    
90
	return res;
126
    return res;
91
}
127
}