Index: tools/ipoddfu_c/dfu.c |
— | — | @@ -1,71 +1,107 @@ |
2 | | -#include <stdio.h>
|
3 | | -#include <string.h>
|
4 | | -
|
5 | | -#include <libusb-1.0/libusb.h>
|
6 | | -
|
7 | | -#include "usb.h"
|
8 | | -
|
9 | | -int dfu_write(const unsigned char i, const unsigned int length, const unsigned char *data) {
|
10 | | - return usb_control_transfer(0x21, 1, i, 0, (unsigned char *) data, length);
|
11 | | -}
|
12 | | -
|
13 | | -int dfu_read(unsigned char result[6]) {
|
14 | | - return usb_control_transfer(0xa1, 3, 0, 0, result, 6);
|
15 | | -}
|
16 | | -
|
17 | | -int dfu_send(const unsigned long int size, const unsigned char *data) {
|
18 | | - unsigned char result[6];
|
19 | | - unsigned int i;
|
20 | | - int res;
|
21 | | -
|
22 | | - printf("Uploading... ");
|
23 | | - fflush(stdout);
|
24 | | -
|
25 | | - for (i = 0; i < (size + 4 + 2047) / 2048; ++i) {
|
26 | | - res = dfu_write(i, ((i + 1) * 2048 > size + 4) ? (size + 4) - (i * 2048) : 2048, (unsigned char *) (data + (i * 2048)));
|
27 | | -
|
28 | | - if (LIBUSB_SUCCESS > res) {
|
29 | | - return res;
|
30 | | - }
|
31 | | -
|
32 | | - memset(result, 0, sizeof(result));
|
33 | | -
|
34 | | - while ('\x05' != result[4]) {
|
35 | | - res = dfu_read(result);
|
36 | | -
|
37 | | - if (LIBUSB_SUCCESS > res) {
|
38 | | - return res;
|
39 | | - }
|
40 | | - }
|
41 | | -
|
42 | | - printf("#");
|
43 | | - fflush(stdout);
|
44 | | - }
|
45 | | -
|
46 | | - res = dfu_write(i, 0, NULL);
|
47 | | -
|
48 | | - if (LIBUSB_SUCCESS > res) {
|
49 | | - return res;
|
50 | | - }
|
51 | | -
|
52 | | - memset(result, 0, sizeof(result));
|
53 | | -
|
54 | | - i = 0;
|
55 | | -
|
56 | | - while ('\x02' != result[4] && i++ < 1000) {
|
57 | | - dfu_read(result);
|
58 | | -
|
59 | | - if (LIBUSB_SUCCESS > res) {
|
60 | | - return res;
|
61 | | - }
|
62 | | - }
|
63 | | -
|
64 | | - if (1000 == i || '\x02' == result[4]) {
|
65 | | - printf(" failed: %d / %d\n", result[4], result[0]);
|
66 | | - }
|
67 | | - else {
|
68 | | - printf(" OK\n");
|
69 | | - }
|
70 | | -
|
71 | | - return 0;
|
72 | | -}
|
| 2 | +// |
| 3 | +// |
| 4 | +// Copyright 2011 user890104 |
| 5 | +// |
| 6 | +// |
| 7 | +// This file is part of ipoddfu. |
| 8 | +// |
| 9 | +// ipoddfu is free software: you can redistribute it and/or |
| 10 | +// modify it under the terms of the GNU General Public License as |
| 11 | +// published by the Free Software Foundation, either version 2 of the |
| 12 | +// License, or (at your option) any later version. |
| 13 | +// |
| 14 | +// ipoddfu is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +// See the GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License along |
| 20 | +// with ipoddfu. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +// |
| 22 | +// |
| 23 | + |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | +#include <string.h> |
| 27 | + |
| 28 | +#include <libusb-1.0/libusb.h> |
| 29 | + |
| 30 | +#include "usb.h" |
| 31 | + |
| 32 | +int dfu_write(const unsigned char i, const unsigned int length, const unsigned char *data) |
| 33 | +{ |
| 34 | + return usb_control_transfer(0x21, 1, i, 0, (unsigned char *) data, length); |
| 35 | +} |
| 36 | + |
| 37 | +int dfu_read(unsigned char result[6]) |
| 38 | +{ |
| 39 | + return usb_control_transfer(0xa1, 3, 0, 0, result, 6); |
| 40 | +} |
| 41 | + |
| 42 | +int dfu_send(const unsigned long int size, const unsigned char *data) |
| 43 | +{ |
| 44 | + unsigned char result[6]; |
| 45 | + unsigned int i; |
| 46 | + int res; |
| 47 | + |
| 48 | + printf("Uploading... "); |
| 49 | + fflush(stdout); |
| 50 | + |
| 51 | + for (i = 0; i < (size + 4 + 2047) / 2048; ++i) |
| 52 | + { |
| 53 | + res = dfu_write(i, ((i + 1) * 2048 > size + 4) ? (size + 4) - (i * 2048) : 2048, |
| 54 | + (unsigned char *) (data + (i * 2048))); |
| 55 | + |
| 56 | + if (LIBUSB_SUCCESS > res) |
| 57 | + { |
| 58 | + return res; |
| 59 | + } |
| 60 | + |
| 61 | + memset(result, 0, sizeof(result)); |
| 62 | + |
| 63 | + while ('\x05' != result[4]) |
| 64 | + { |
| 65 | + res = dfu_read(result); |
| 66 | + |
| 67 | + if (LIBUSB_SUCCESS > res) |
| 68 | + { |
| 69 | + return res; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + printf("#"); |
| 74 | + fflush(stdout); |
| 75 | + } |
| 76 | + |
| 77 | + res = dfu_write(i, 0, NULL); |
| 78 | + |
| 79 | + if (LIBUSB_SUCCESS > res) |
| 80 | + { |
| 81 | + return res; |
| 82 | + } |
| 83 | + |
| 84 | + memset(result, 0, sizeof(result)); |
| 85 | + |
| 86 | + i = 0; |
| 87 | + |
| 88 | + while ('\x02' != result[4] && i++ < 1000) |
| 89 | + { |
| 90 | + dfu_read(result); |
| 91 | + |
| 92 | + if (LIBUSB_SUCCESS > res) |
| 93 | + { |
| 94 | + return res; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (1000 == i || '\x02' == result[4]) |
| 99 | + { |
| 100 | + printf(" failed: %d / %d\n", result[4], result[0]); |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + printf(" OK\n"); |
| 105 | + } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
Index: tools/ipoddfu_c/usb.h |
— | — | @@ -1,7 +1,35 @@ |
2 | | -int usb_init(void);
|
3 | | -int usb_find(unsigned char *reattach);
|
4 | | -int usb_open(libusb_device *dev, unsigned char *reattach);
|
5 | | -int usb_bulk_transfer(const unsigned char endpoint, unsigned char *data, const unsigned int length);
|
6 | | -int usb_control_transfer(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength);
|
7 | | -int usb_close(const unsigned char reattach);
|
8 | | -void usb_exit(void);
|
| 2 | +// |
| 3 | +// |
| 4 | +// Copyright 2011 user890104 |
| 5 | +// |
| 6 | +// |
| 7 | +// This file is part of ipoddfu. |
| 8 | +// |
| 9 | +// ipoddfu is free software: you can redistribute it and/or |
| 10 | +// modify it under the terms of the GNU General Public License as |
| 11 | +// published by the Free Software Foundation, either version 2 of the |
| 12 | +// License, or (at your option) any later version. |
| 13 | +// |
| 14 | +// ipoddfu is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +// See the GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License along |
| 20 | +// with ipoddfu. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +// |
| 22 | +// |
| 23 | + |
| 24 | + |
| 25 | +#ifndef __USB_H__ |
| 26 | +#define __USB_H__ |
| 27 | + |
| 28 | +int usb_init(void); |
| 29 | +int usb_find(unsigned char *reattach); |
| 30 | +int usb_open(libusb_device *dev, unsigned char *reattach); |
| 31 | +int usb_bulk_transfer(const unsigned char endpoint, unsigned char *data, const unsigned int length); |
| 32 | +int usb_control_transfer(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength); |
| 33 | +int usb_close(const unsigned char reattach); |
| 34 | +void usb_exit(void); |
| 35 | + |
| 36 | +#endif |
Index: tools/ipoddfu_c/ipoddfu.h |
— | — | @@ -1,2 +1,30 @@ |
2 | | -libusb_context *usb_ctx;
|
3 | | -libusb_device_handle *usb_handle;
|
| 2 | +// |
| 3 | +// |
| 4 | +// Copyright 2011 user890104 |
| 5 | +// |
| 6 | +// |
| 7 | +// This file is part of ipoddfu. |
| 8 | +// |
| 9 | +// ipoddfu is free software: you can redistribute it and/or |
| 10 | +// modify it under the terms of the GNU General Public License as |
| 11 | +// published by the Free Software Foundation, either version 2 of the |
| 12 | +// License, or (at your option) any later version. |
| 13 | +// |
| 14 | +// ipoddfu is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +// See the GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License along |
| 20 | +// with ipoddfu. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +// |
| 22 | +// |
| 23 | + |
| 24 | + |
| 25 | +#ifndef __IPODDFU_H__ |
| 26 | +#define __IPODDFU_H__ |
| 27 | + |
| 28 | +libusb_context *usb_ctx; |
| 29 | +libusb_device_handle *usb_handle; |
| 30 | + |
| 31 | +#endif |
Index: tools/ipoddfu_c/misc.h |
— | — | @@ -1,3 +1,31 @@ |
| 2 | +// |
| 3 | +// |
| 4 | +// Copyright 2011 user890104 |
| 5 | +// |
| 6 | +// |
| 7 | +// This file is part of ipoddfu. |
| 8 | +// |
| 9 | +// ipoddfu is free software: you can redistribute it and/or |
| 10 | +// modify it under the terms of the GNU General Public License as |
| 11 | +// published by the Free Software Foundation, either version 2 of the |
| 12 | +// License, or (at your option) any later version. |
| 13 | +// |
| 14 | +// ipoddfu is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +// See the GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License along |
| 20 | +// with ipoddfu. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +// |
| 22 | +// |
| 23 | + |
| 24 | + |
| 25 | +#ifndef __MISC_H__ |
| 26 | +#define __MISC_H__ |
| 27 | + |
2 | 28 | long int fgetsize(FILE *fp); |
3 | 29 | void dump_packet(const unsigned char *data, const unsigned int length); |
4 | 30 | void print_error(const int code); |
| 31 | + |
| 32 | +#endif |
Index: tools/ipoddfu_c/dfu.h |
— | — | @@ -1 +1,31 @@ |
2 | | -int dfu_send(const unsigned long int size, const unsigned char *data);
|
| 2 | +// |
| 3 | +// |
| 4 | +// Copyright 2011 user890104 |
| 5 | +// |
| 6 | +// |
| 7 | +// This file is part of ipoddfu. |
| 8 | +// |
| 9 | +// ipoddfu is free software: you can redistribute it and/or |
| 10 | +// modify it under the terms of the GNU General Public License as |
| 11 | +// published by the Free Software Foundation, either version 2 of the |
| 12 | +// License, or (at your option) any later version. |
| 13 | +// |
| 14 | +// ipoddfu is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +// See the GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License along |
| 20 | +// with ipoddfu. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +// |
| 22 | +// |
| 23 | + |
| 24 | + |
| 25 | +#ifndef __DFU_H__ |
| 26 | +#define __DFU_H__ |
| 27 | + |
| 28 | +int dfu_write(const unsigned char i, const unsigned int length, const unsigned char *data); |
| 29 | +int dfu_read(unsigned char result[6]); |
| 30 | +int dfu_send(const unsigned long int size, const unsigned char *data); |
| 31 | + |
| 32 | +#endif |
Index: tools/ipoddfu_c/usb.c |
— | — | @@ -1,268 +1,323 @@ |
2 | | -#include <stdio.h>
|
3 | | -
|
4 | | -#include <libusb-1.0/libusb.h>
|
5 | | -
|
6 | | -#include "misc.h"
|
7 | | -#include "usb.h"
|
8 | | -
|
9 | | -libusb_context *usb_ctx = NULL;
|
10 | | -libusb_device_handle *usb_handle = NULL;
|
11 | | -
|
12 | | -int usb_init(void) {
|
13 | | - int res;
|
14 | | -
|
15 | | - printf("Initialising USB library... ");
|
16 | | -
|
17 | | - res = libusb_init(&usb_ctx);
|
18 | | -
|
19 | | - if (LIBUSB_SUCCESS != res) {
|
20 | | - return res;
|
21 | | - }
|
22 | | -
|
23 | | - printf("OK\n");
|
24 | | -
|
25 | | -#ifdef DEBUG
|
26 | | - libusb_set_debug(usb_ctx, 3);
|
27 | | -#endif
|
28 | | -
|
29 | | - return LIBUSB_SUCCESS;
|
30 | | -}
|
31 | | -
|
32 | | -int usb_find(unsigned char *reattach) {
|
33 | | - libusb_device **devs, *dev;
|
34 | | - ssize_t devs_cnt;
|
35 | | - int res, i;
|
36 | | - struct libusb_device_descriptor dev_desc;
|
37 | | - unsigned char found = 0;
|
38 | | - struct libusb_config_descriptor *cfg_desc;
|
39 | | - const struct libusb_interface *iface;
|
40 | | - const struct libusb_interface_descriptor *iface_desc;
|
41 | | -
|
42 | | - printf("Getting USB device list... ");
|
43 | | -
|
44 | | - devs_cnt = libusb_get_device_list(usb_ctx, &devs);
|
45 | | -
|
46 | | - if (devs_cnt < 0) {
|
47 | | - return devs_cnt;
|
48 | | - }
|
49 | | -
|
50 | | - printf("Found %d USB devices!\n", devs_cnt);
|
51 | | -
|
52 | | - for (i = 0; i < devs_cnt; ++i) {
|
53 | | - dev = devs[i];
|
54 | | -
|
55 | | - printf("Getting device descriptor of USB device %d...\n", i);
|
56 | | -
|
57 | | - res = libusb_get_device_descriptor(dev, &dev_desc);
|
58 | | -
|
59 | | - if (LIBUSB_SUCCESS != res) {
|
60 | | - fprintf(stderr, "Unable to get device descriptor of device %d!\n", i);
|
61 | | -
|
62 | | - continue;
|
63 | | - }
|
64 | | -
|
65 | | - printf("[%04x:%04x] bus %d, device %d, USB ver. %04x\n", dev_desc.idVendor,
|
66 | | - dev_desc.idProduct, libusb_get_bus_number(dev),
|
67 | | - libusb_get_device_address(dev), dev_desc.bcdUSB);
|
68 | | -
|
69 | | - if (0x05ac == dev_desc.idVendor && (
|
70 | | - /* DFU */
|
71 | | - 0x1220 == dev_desc.idProduct || /* iPod Nano 2G */
|
72 | | - 0x1223 == dev_desc.idProduct || /* iPod Classic 1G */
|
73 | | - 0x1224 == dev_desc.idProduct || /* iPod Nano 3G */
|
74 | | - 0x1225 == dev_desc.idProduct || /* iPod Nano 4G */
|
75 | | - 0x1231 == dev_desc.idProduct || /* iPod Nano 5G */
|
76 | | - 0x1232 == dev_desc.idProduct || /* iPod Nano 6G */
|
77 | | - 0x1233 == dev_desc.idProduct || /* iPod Shuffle 4G */
|
78 | | - /* WTF */
|
79 | | - 0x1240 == dev_desc.idProduct || /* iPod Nano 2G */
|
80 | | - 0x1241 == dev_desc.idProduct || /* iPod Classic 1G */
|
81 | | - 0x1242 == dev_desc.idProduct || /* iPod Nano 3G */
|
82 | | - 0x1243 == dev_desc.idProduct || /* iPod Nano 4G */
|
83 | | - 0x1245 == dev_desc.idProduct || /* iPod Classic 2G */
|
84 | | - 0x1246 == dev_desc.idProduct || /* iPod Nano 5G */
|
85 | | - 0x1247 == dev_desc.idProduct || /* iPod Classic 3G */
|
86 | | - 0x1248 == dev_desc.idProduct /* iPod Nano 6G */
|
87 | | - )) {
|
88 | | - printf("Found DFU USB device!\n");
|
89 | | -
|
90 | | - if (1 != dev_desc.bNumConfigurations) {
|
91 | | - fprintf(stderr, "Number of configs is different than 1, not the right device...\n");
|
92 | | -
|
93 | | - continue;
|
94 | | - }
|
95 | | -
|
96 | | - printf("Getting config descriptor 0 of device...\n");
|
97 | | -
|
98 | | - res = libusb_get_config_descriptor(dev, 0, &cfg_desc);
|
99 | | -
|
100 | | - if (LIBUSB_SUCCESS != res) {
|
101 | | - return res;
|
102 | | - }
|
103 | | -
|
104 | | - if (1 != cfg_desc->bNumInterfaces) {
|
105 | | - fprintf(stderr, "Wrong USB device, it should have exactly 1 interface\n");
|
106 | | -
|
107 | | - continue;
|
108 | | - }
|
109 | | -
|
110 | | - iface = &cfg_desc->interface[0];
|
111 | | -
|
112 | | - if (1 != iface->num_altsetting) {
|
113 | | - fprintf(stderr, "Wrong USB device, it should have exactly 1 altsetting\n");
|
114 | | -
|
115 | | - continue;
|
116 | | - }
|
117 | | -
|
118 | | - iface_desc = &iface->altsetting[0];
|
119 | | -
|
120 | | - if (0 != iface_desc->bNumEndpoints) {
|
121 | | - fprintf(stderr, "Wrong USB device, it should have no endpoints\n");
|
122 | | -
|
123 | | - continue;
|
124 | | - }
|
125 | | -
|
126 | | - found = 1;
|
127 | | - }
|
128 | | - }
|
129 | | -
|
130 | | - if (found) {
|
131 | | - res = usb_open(dev, reattach);
|
132 | | - }
|
133 | | - else {
|
134 | | - fprintf(stderr, "DFU USB device not found!\n");
|
135 | | -
|
136 | | - res = 1; // not found
|
137 | | - }
|
138 | | -
|
139 | | - printf("Freeing device list...\n");
|
140 | | -
|
141 | | - libusb_free_device_list(devs, 1);
|
142 | | -
|
143 | | - return res;
|
144 | | -}
|
145 | | -
|
146 | | -int usb_open(libusb_device *dev, unsigned char *reattach) {
|
147 | | - int res;
|
148 | | -
|
149 | | - printf("Opening USB device... ");
|
150 | | -
|
151 | | - res = libusb_open(dev, &usb_handle);
|
152 | | -
|
153 | | - if (LIBUSB_SUCCESS != res) {
|
154 | | - return res;
|
155 | | - }
|
156 | | -
|
157 | | - printf("OK\n");
|
158 | | -
|
159 | | - printf("Setting USB configuration 1... ");
|
160 | | -
|
161 | | - res = libusb_set_configuration(usb_handle, 1);
|
162 | | -
|
163 | | - if (LIBUSB_SUCCESS != res) {
|
164 | | - return res;
|
165 | | - }
|
166 | | -
|
167 | | - printf("OK\n");
|
168 | | -
|
169 | | - res = libusb_kernel_driver_active(usb_handle, 0);
|
170 | | -
|
171 | | - if (1 == res) {
|
172 | | - printf("Kernel driver active, detaching... ");
|
173 | | -
|
174 | | - *reattach = 1;
|
175 | | -
|
176 | | - res = libusb_detach_kernel_driver(usb_handle, 0);
|
177 | | -
|
178 | | - if (LIBUSB_SUCCESS == res) {
|
179 | | - printf("OK\n");
|
180 | | - }
|
181 | | - }
|
182 | | -
|
183 | | - if (LIBUSB_SUCCESS != res) {
|
184 | | - return res;
|
185 | | - }
|
186 | | -
|
187 | | - printf("Claiming interface 0... ");
|
188 | | -
|
189 | | - res = libusb_claim_interface(usb_handle, 0);
|
190 | | -
|
191 | | - if (LIBUSB_SUCCESS != res) {
|
192 | | - return res;
|
193 | | - }
|
194 | | -
|
195 | | - printf("OK\n");
|
196 | | -
|
197 | | - return LIBUSB_SUCCESS;
|
198 | | -}
|
199 | | -
|
200 | | -int usb_bulk_transfer(const unsigned char endpoint, unsigned char *data, const unsigned int length) {
|
201 | | - int transferred;
|
202 | | - int res;
|
203 | | -
|
204 | | - res = libusb_bulk_transfer(usb_handle, endpoint, data, length, &transferred, 30000);
|
205 | | -
|
206 | | - if (LIBUSB_SUCCESS != res) {
|
207 | | - return res;
|
208 | | - }
|
209 | | -
|
210 | | - if ((unsigned int) transferred != length) {
|
211 | | - return 2; // incomplete
|
212 | | - }
|
213 | | -
|
214 | | - return LIBUSB_SUCCESS;
|
215 | | -}
|
216 | | -
|
217 | | -int usb_control_transfer(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength) {
|
218 | | - int res;
|
219 | | -
|
220 | | - res = libusb_control_transfer(usb_handle, bmRequestType, bRequest, wValue, wIndex, data, wLength, 30000);
|
221 | | -
|
222 | | - if (LIBUSB_SUCCESS != res) {
|
223 | | - return res;
|
224 | | - }
|
225 | | -
|
226 | | - return LIBUSB_SUCCESS;
|
227 | | -}
|
228 | | -
|
229 | | -int usb_close(const unsigned char reattach) {
|
230 | | - int res;
|
231 | | -
|
232 | | - printf("Releasing USB interface... ");
|
233 | | -
|
234 | | - res = libusb_release_interface(usb_handle, 0);
|
235 | | -
|
236 | | - if (LIBUSB_SUCCESS != res) {
|
237 | | - return res;
|
238 | | - }
|
239 | | -
|
240 | | - printf("OK\n");
|
241 | | -
|
242 | | - if (reattach) {
|
243 | | - printf("Reattaching kernel driver... ");
|
244 | | -
|
245 | | - res = libusb_attach_kernel_driver(usb_handle, 0);
|
246 | | -
|
247 | | - if (LIBUSB_SUCCESS == res) {
|
248 | | - printf("OK\n");
|
249 | | - }
|
250 | | - else {
|
251 | | - printf("\n");
|
252 | | - print_error(res);
|
253 | | -
|
254 | | - res = LIBUSB_SUCCESS;
|
255 | | - }
|
256 | | - }
|
257 | | -
|
258 | | - printf("Closing USB device handle...\n");
|
259 | | -
|
260 | | - libusb_close(usb_handle);
|
261 | | -
|
262 | | - return res;
|
263 | | -}
|
264 | | -
|
265 | | -void usb_exit(void) {
|
266 | | - printf("Deinitializing USB library...\n");
|
267 | | -
|
268 | | - libusb_exit(usb_ctx);
|
269 | | -}
|
| 2 | +// |
| 3 | +// |
| 4 | +// Copyright 2011 user890104 |
| 5 | +// |
| 6 | +// |
| 7 | +// This file is part of ipoddfu. |
| 8 | +// |
| 9 | +// ipoddfu is free software: you can redistribute it and/or |
| 10 | +// modify it under the terms of the GNU General Public License as |
| 11 | +// published by the Free Software Foundation, either version 2 of the |
| 12 | +// License, or (at your option) any later version. |
| 13 | +// |
| 14 | +// ipoddfu is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +// See the GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License along |
| 20 | +// with ipoddfu. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +// |
| 22 | +// |
| 23 | + |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | + |
| 27 | +#include <libusb-1.0/libusb.h> |
| 28 | + |
| 29 | +#include "misc.h" |
| 30 | +#include "usb.h" |
| 31 | + |
| 32 | +libusb_context *usb_ctx = NULL; |
| 33 | +libusb_device_handle *usb_handle = NULL; |
| 34 | + |
| 35 | +int usb_init(void) |
| 36 | +{ |
| 37 | + int res; |
| 38 | + |
| 39 | + printf("Initialising USB library... "); |
| 40 | + |
| 41 | + res = libusb_init(&usb_ctx); |
| 42 | + |
| 43 | + if (LIBUSB_SUCCESS != res) |
| 44 | + { |
| 45 | + return res; |
| 46 | + } |
| 47 | + |
| 48 | + printf("OK\n"); |
| 49 | + |
| 50 | +#ifdef DEBUG |
| 51 | + libusb_set_debug(usb_ctx, 3); |
| 52 | +#endif |
| 53 | + |
| 54 | + return LIBUSB_SUCCESS; |
| 55 | +} |
| 56 | + |
| 57 | +int usb_find(unsigned char *reattach) |
| 58 | +{ |
| 59 | + libusb_device **devs, *dev; |
| 60 | + ssize_t devs_cnt; |
| 61 | + int res, i; |
| 62 | + struct libusb_device_descriptor dev_desc; |
| 63 | + unsigned char found = 0; |
| 64 | + struct libusb_config_descriptor *cfg_desc; |
| 65 | + const struct libusb_interface *iface; |
| 66 | + const struct libusb_interface_descriptor *iface_desc; |
| 67 | + |
| 68 | + printf("Getting USB device list... "); |
| 69 | + |
| 70 | + devs_cnt = libusb_get_device_list(usb_ctx, &devs); |
| 71 | + |
| 72 | + if (devs_cnt < 0) |
| 73 | + { |
| 74 | + return devs_cnt; |
| 75 | + } |
| 76 | + |
| 77 | + printf("Found %d USB devices!\n", (int) devs_cnt); |
| 78 | + |
| 79 | + for (i = 0; i < devs_cnt; ++i) |
| 80 | + { |
| 81 | + dev = devs[i]; |
| 82 | + |
| 83 | + printf("Getting device descriptor of USB device %d...\n", i); |
| 84 | + |
| 85 | + res = libusb_get_device_descriptor(dev, &dev_desc); |
| 86 | + |
| 87 | + if (LIBUSB_SUCCESS != res) |
| 88 | + { |
| 89 | + fprintf(stderr, "Unable to get device descriptor of device %d!\n", i); |
| 90 | + |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + printf("[%04x:%04x] bus %d, device %d, USB ver. %04x\n", dev_desc.idVendor, |
| 95 | + dev_desc.idProduct, libusb_get_bus_number(dev), |
| 96 | + libusb_get_device_address(dev), dev_desc.bcdUSB); |
| 97 | + |
| 98 | + if (0x05ac == dev_desc.idVendor && ( |
| 99 | + // DFU |
| 100 | + 0x1220 == dev_desc.idProduct || // iPod Nano 2G |
| 101 | + 0x1223 == dev_desc.idProduct || // iPod Classic 1G |
| 102 | + 0x1224 == dev_desc.idProduct || // iPod Nano 3G |
| 103 | + 0x1225 == dev_desc.idProduct || // iPod Nano 4G |
| 104 | + 0x1231 == dev_desc.idProduct || // iPod Nano 5G |
| 105 | + 0x1232 == dev_desc.idProduct || // iPod Nano 6G |
| 106 | + 0x1233 == dev_desc.idProduct || // iPod Shuffle 4G |
| 107 | + // WTF |
| 108 | + 0x1240 == dev_desc.idProduct || // iPod Nano 2G |
| 109 | + 0x1241 == dev_desc.idProduct || // iPod Classic 1G |
| 110 | + 0x1242 == dev_desc.idProduct || // iPod Nano 3G |
| 111 | + 0x1243 == dev_desc.idProduct || // iPod Nano 4G |
| 112 | + 0x1245 == dev_desc.idProduct || // iPod Classic 2G |
| 113 | + 0x1246 == dev_desc.idProduct || // iPod Nano 5G |
| 114 | + 0x1247 == dev_desc.idProduct || // iPod Classic 3G |
| 115 | + 0x1248 == dev_desc.idProduct // iPod Nano 6G |
| 116 | + )) |
| 117 | + { |
| 118 | + printf("Found DFU USB device!\n"); |
| 119 | + |
| 120 | + if (1 != dev_desc.bNumConfigurations) |
| 121 | + { |
| 122 | + fprintf(stderr, "Number of configs is different than 1, not the right device...\n"); |
| 123 | + |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + printf("Getting config descriptor 0 of device...\n"); |
| 128 | + |
| 129 | + res = libusb_get_config_descriptor(dev, 0, &cfg_desc); |
| 130 | + |
| 131 | + if (LIBUSB_SUCCESS != res) |
| 132 | + { |
| 133 | + return res; |
| 134 | + } |
| 135 | + |
| 136 | + if (1 != cfg_desc->bNumInterfaces) |
| 137 | + { |
| 138 | + fprintf(stderr, "Wrong USB device, it should have exactly 1 interface\n"); |
| 139 | + |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + iface = &cfg_desc->interface[0]; |
| 144 | + |
| 145 | + if (1 != iface->num_altsetting) |
| 146 | + { |
| 147 | + fprintf(stderr, "Wrong USB device, it should have exactly 1 altsetting\n"); |
| 148 | + |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + iface_desc = &iface->altsetting[0]; |
| 153 | + |
| 154 | + if (0 != iface_desc->bNumEndpoints) |
| 155 | + { |
| 156 | + fprintf(stderr, "Wrong USB device, it should have no endpoints\n"); |
| 157 | + |
| 158 | + continue; |
| 159 | + } |
| 160 | + |
| 161 | + found = 1; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if (found) |
| 166 | + { |
| 167 | + res = usb_open(dev, reattach); |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + fprintf(stderr, "DFU USB device not found!\n"); |
| 172 | + |
| 173 | + res = 1; // not found |
| 174 | + } |
| 175 | + |
| 176 | + printf("Freeing device list...\n"); |
| 177 | + |
| 178 | + libusb_free_device_list(devs, 1); |
| 179 | + |
| 180 | + return res; |
| 181 | +} |
| 182 | + |
| 183 | +int usb_open(libusb_device *dev, unsigned char *reattach) |
| 184 | +{ |
| 185 | + int res; |
| 186 | + |
| 187 | + printf("Opening USB device... "); |
| 188 | + |
| 189 | + res = libusb_open(dev, &usb_handle); |
| 190 | + |
| 191 | + if (LIBUSB_SUCCESS != res) |
| 192 | + { |
| 193 | + return res; |
| 194 | + } |
| 195 | + |
| 196 | + printf("OK\n"); |
| 197 | + |
| 198 | + printf("Setting USB configuration 1... "); |
| 199 | + |
| 200 | + res = libusb_set_configuration(usb_handle, 1); |
| 201 | + |
| 202 | + if (LIBUSB_SUCCESS != res) |
| 203 | + { |
| 204 | + return res; |
| 205 | + } |
| 206 | + |
| 207 | + printf("OK\n"); |
| 208 | + |
| 209 | + res = libusb_kernel_driver_active(usb_handle, 0); |
| 210 | + |
| 211 | + if (1 == res) |
| 212 | + { |
| 213 | + printf("Kernel driver active, detaching... "); |
| 214 | + |
| 215 | + *reattach = 1; |
| 216 | + |
| 217 | + res = libusb_detach_kernel_driver(usb_handle, 0); |
| 218 | + |
| 219 | + if (LIBUSB_SUCCESS == res) |
| 220 | + { |
| 221 | + printf("OK\n"); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + if (LIBUSB_SUCCESS != res) |
| 226 | + { |
| 227 | + return res; |
| 228 | + } |
| 229 | + |
| 230 | + printf("Claiming interface 0... "); |
| 231 | + |
| 232 | + res = libusb_claim_interface(usb_handle, 0); |
| 233 | + |
| 234 | + if (LIBUSB_SUCCESS != res) |
| 235 | + { |
| 236 | + return res; |
| 237 | + } |
| 238 | + |
| 239 | + printf("OK\n"); |
| 240 | + |
| 241 | + return LIBUSB_SUCCESS; |
| 242 | +} |
| 243 | + |
| 244 | +int usb_bulk_transfer(const unsigned char endpoint, unsigned char *data, const unsigned int length) |
| 245 | +{ |
| 246 | + int transferred; |
| 247 | + int res; |
| 248 | + |
| 249 | + res = libusb_bulk_transfer(usb_handle, endpoint, data, length, &transferred, 30000); |
| 250 | + |
| 251 | + if (LIBUSB_SUCCESS != res) |
| 252 | + { |
| 253 | + return res; |
| 254 | + } |
| 255 | + |
| 256 | + if ((unsigned int) transferred != length) |
| 257 | + { |
| 258 | + return 2; // incomplete |
| 259 | + } |
| 260 | + |
| 261 | + return LIBUSB_SUCCESS; |
| 262 | +} |
| 263 | + |
| 264 | +int usb_control_transfer(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength) |
| 265 | +{ |
| 266 | + int res; |
| 267 | + |
| 268 | + res = libusb_control_transfer(usb_handle, bmRequestType, bRequest, wValue, wIndex, data, wLength, 30000); |
| 269 | + |
| 270 | + if (LIBUSB_SUCCESS != res) |
| 271 | + { |
| 272 | + return res; |
| 273 | + } |
| 274 | + |
| 275 | + return LIBUSB_SUCCESS; |
| 276 | +} |
| 277 | + |
| 278 | +int usb_close(const unsigned char reattach) |
| 279 | +{ |
| 280 | + int res; |
| 281 | + |
| 282 | + printf("Releasing USB interface... "); |
| 283 | + |
| 284 | + res = libusb_release_interface(usb_handle, 0); |
| 285 | + |
| 286 | + if (LIBUSB_SUCCESS != res) |
| 287 | + { |
| 288 | + return res; |
| 289 | + } |
| 290 | + |
| 291 | + printf("OK\n"); |
| 292 | + |
| 293 | + if (reattach) |
| 294 | + { |
| 295 | + printf("Reattaching kernel driver... "); |
| 296 | + |
| 297 | + res = libusb_attach_kernel_driver(usb_handle, 0); |
| 298 | + |
| 299 | + if (LIBUSB_SUCCESS == res) |
| 300 | + { |
| 301 | + printf("OK\n"); |
| 302 | + } |
| 303 | + else |
| 304 | + { |
| 305 | + printf("\n"); |
| 306 | + print_error(res); |
| 307 | + |
| 308 | + res = LIBUSB_SUCCESS; |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + printf("Closing USB device handle...\n"); |
| 313 | + |
| 314 | + libusb_close(usb_handle); |
| 315 | + |
| 316 | + return res; |
| 317 | +} |
| 318 | + |
| 319 | +void usb_exit(void) |
| 320 | +{ |
| 321 | + printf("Deinitializing USB library...\n"); |
| 322 | + |
| 323 | + libusb_exit(usb_ctx); |
| 324 | +} |
Index: tools/ipoddfu_c/ipoddfu.c |
— | — | @@ -4,6 +4,29 @@ |
5 | 5 | |
6 | 6 | #include <libusb-1.0/libusb.h> |
7 | 7 | |
| 8 | +// |
| 9 | +// |
| 10 | +// Copyright 2011 user890104 |
| 11 | +// |
| 12 | +// |
| 13 | +// This file is part of ipoddfu. |
| 14 | +// |
| 15 | +// ipoddfu is free software: you can redistribute it and/or |
| 16 | +// modify it under the terms of the GNU General Public License as |
| 17 | +// published by the Free Software Foundation, either version 2 of the |
| 18 | +// License, or (at your option) any later version. |
| 19 | +// |
| 20 | +// ipoddfu is distributed in the hope that it will be useful, |
| 21 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 23 | +// See the GNU General Public License for more details. |
| 24 | +// |
| 25 | +// You should have received a copy of the GNU General Public License along |
| 26 | +// with ipoddfu. If not, see <http://www.gnu.org/licenses/>. |
| 27 | +// |
| 28 | +// |
| 29 | + |
| 30 | + |
8 | 31 | #include "usb.h" |
9 | 32 | #include "dfu.h" |
10 | 33 | #include "crc32.h" |
— | — | @@ -10,82 +33,95 @@ |
11 | 34 | #include "misc.h" |
12 | 35 | #include "ipoddfu.h" |
13 | 36 | |
14 | | -int main(int argc, char *argv[]) { |
15 | | - int res = 0; |
16 | | - unsigned char reattach = 0, *data; |
17 | | - FILE *fp; |
18 | | - long int size; |
19 | | - unsigned int checksum; |
20 | | - |
21 | | - if (2 != argc) { |
22 | | - fprintf(stderr, "usage: %s <file>\n", argv[0]); |
23 | | - |
24 | | - return 1; |
25 | | - } |
26 | | - |
27 | | - fp = fopen(argv[1], "r"); |
28 | | - |
29 | | - if (!fp) { |
30 | | - perror("fopen"); |
31 | | - |
32 | | - return 1; |
33 | | - } |
34 | | - |
35 | | - size = fgetsize(fp); |
36 | | - |
37 | | - if (-1L == size) { |
38 | | - return 1; |
39 | | - } |
40 | | - |
41 | | - data = (unsigned char *) malloc(size + 4); |
42 | | - |
43 | | - if ((unsigned long int) size != fread(data, sizeof(unsigned char), size, fp)) { |
44 | | - perror("fread"); |
45 | | - |
46 | | - return 1; |
47 | | - } |
48 | | - |
49 | | - if (fclose(fp)) { |
50 | | - perror("fclose"); |
51 | | - |
52 | | - return 1; |
53 | | - } |
54 | | - |
55 | | - crc32_init(); |
56 | | - |
57 | | - checksum = crc32(data, size, CRC32_DEFAULT_SEED); |
58 | | - |
59 | | - memcpy(data + size, &checksum, 4); |
60 | | - |
61 | | - res = usb_init(); |
62 | | - |
63 | | - if (LIBUSB_SUCCESS == res) { |
64 | | - res = usb_find(&reattach); |
65 | | - } |
66 | | - |
67 | | - if (LIBUSB_SUCCESS == res) { |
68 | | - res = dfu_send((unsigned long int) size + 4, data); |
69 | | - } |
70 | | - |
71 | | - if (data) { |
72 | | - free(data); |
73 | | - } |
74 | | - |
75 | | - if (0 != res) { |
76 | | - print_error(res); |
77 | | - } |
78 | | - |
79 | | - if (usb_handle) { |
80 | | - usb_close(reattach); |
81 | | - } |
82 | | - |
83 | | - if (usb_ctx) { |
84 | | - usb_exit(); |
85 | | - } |
86 | | - |
87 | | - if (res < 0) { |
88 | | - res = -res; |
89 | | - } |
90 | | - |
91 | | - return res; |
| 37 | +int main(int argc, char *argv[]) |
| 38 | +{ |
| 39 | + int res = 0; |
| 40 | + unsigned char reattach = 0, *data; |
| 41 | + FILE *fp; |
| 42 | + long int size; |
| 43 | + unsigned int checksum; |
| 44 | + |
| 45 | + if (2 != argc) |
| 46 | + { |
| 47 | + fprintf(stderr, "usage: %s <file>\n", argv[0]); |
| 48 | + |
| 49 | + return 1; |
| 50 | + } |
| 51 | + |
| 52 | + fp = fopen(argv[1], "r"); |
| 53 | + |
| 54 | + if (!fp) |
| 55 | + { |
| 56 | + perror("fopen"); |
| 57 | + |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + size = fgetsize(fp); |
| 62 | + |
| 63 | + if (-1L == size) |
| 64 | + { |
| 65 | + return 1; |
| 66 | + } |
| 67 | + |
| 68 | + data = (unsigned char *) malloc(size + 4); |
| 69 | + |
| 70 | + if ((unsigned long int) size != fread(data, sizeof(unsigned char), size, fp)) |
| 71 | + { |
| 72 | + perror("fread"); |
| 73 | + |
| 74 | + return 1; |
| 75 | + } |
| 76 | + |
| 77 | + if (fclose(fp)) |
| 78 | + { |
| 79 | + perror("fclose"); |
| 80 | + |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + crc32_init(); |
| 85 | + |
| 86 | + checksum = crc32(data, size, CRC32_DEFAULT_SEED); |
| 87 | + |
| 88 | + memcpy(data + size, &checksum, 4); |
| 89 | + |
| 90 | + res = usb_init(); |
| 91 | + |
| 92 | + if (LIBUSB_SUCCESS == res) |
| 93 | + { |
| 94 | + res = usb_find(&reattach); |
| 95 | + } |
| 96 | + |
| 97 | + if (LIBUSB_SUCCESS == res) |
| 98 | + { |
| 99 | + res = dfu_send((unsigned long int) size + 4, data); |
| 100 | + } |
| 101 | + |
| 102 | + if (data) |
| 103 | + { |
| 104 | + free(data); |
| 105 | + } |
| 106 | + |
| 107 | + if (0 != res) |
| 108 | + { |
| 109 | + print_error(res); |
| 110 | + } |
| 111 | + |
| 112 | + if (usb_handle) |
| 113 | + { |
| 114 | + usb_close(reattach); |
| 115 | + } |
| 116 | + |
| 117 | + if (usb_ctx) |
| 118 | + { |
| 119 | + usb_exit(); |
| 120 | + } |
| 121 | + |
| 122 | + if (res < 0) |
| 123 | + { |
| 124 | + res = -res; |
| 125 | + } |
| 126 | + |
| 127 | + return res; |
92 | 128 | } |
Index: tools/ipoddfu_c/misc.c |
— | — | @@ -1,82 +1,117 @@ |
2 | | -#include <stdio.h>
|
3 | | -
|
4 | | -#include <libusb-1.0/libusb.h>
|
5 | | -
|
6 | | -#include "misc.h"
|
7 | | -
|
8 | | -const char *libusb_error_messages[13] = {
|
9 | | - "No error", /* LIBUSB_SUCCESS = 0 */
|
10 | | - "Input/output error", /* LIBUSB_ERROR_IO = -1 */
|
11 | | - "Invalid parameter", /* LIBUSB_ERROR_INVALID_PARAM = -2 */
|
12 | | - "Access denied", /* LIBUSB_ERROR_ACCESS = -3 */
|
13 | | - "No such device", /* LIBUSB_ERROR_NO_DEVICE = -4 */
|
14 | | - "Entity not found", /* LIBUSB_ERROR_NOT_FOUND = -5 */
|
15 | | - "Resource busy", /* LIBUSB_ERROR_BUSY = -6 */
|
16 | | - "Operation timed out", /* LIBUSB_ERROR_TIMEOUT = -7 */
|
17 | | - "Overflow", /* LIBUSB_ERROR_OVERFLOW = -8 */
|
18 | | - "Pipe error", /* LIBUSB_ERROR_PIPE = -9 */
|
19 | | - "System call interrupted", /* LIBUSB_ERROR_INTERRUPTED = -10 */
|
20 | | - "Insufficient memory", /* LIBUSB_ERROR_NO_MEM = -11 */
|
21 | | - "Operation not supported", /* LIBUSB_ERROR_NOT_SUPPORTED = -12 */
|
22 | | -};
|
23 | | -
|
24 | | -long int fgetsize(FILE *fp) {
|
25 | | - static long int pos, size;
|
26 | | -
|
27 | | - if (-1L == (pos = ftell(fp))) {
|
28 | | - perror("ftell");
|
29 | | -
|
30 | | - return -1L;
|
31 | | - }
|
32 | | -
|
33 | | - if(fseek(fp, 0L, SEEK_END)) {
|
34 | | - perror("fseek");
|
35 | | -
|
36 | | - return -1L;
|
37 | | - }
|
38 | | -
|
39 | | - if (-1L == (size = ftell(fp))) {
|
40 | | - perror("ftell");
|
41 | | -
|
42 | | - return -1L;
|
43 | | - }
|
44 | | -
|
45 | | - if (fseek(fp, pos, SEEK_SET)) {
|
46 | | - perror("fseek");
|
47 | | -
|
48 | | - return -1L;
|
49 | | - }
|
50 | | -
|
51 | | - return size;
|
52 | | -}
|
53 | | -
|
54 | | -void dump_packet(const unsigned char *data, const unsigned int length) {
|
55 | | - static unsigned int i;
|
56 | | -
|
57 | | - for (i = 0; i < length; ++i) {
|
58 | | - printf("%02x ", data[i]);
|
59 | | -
|
60 | | - if (i % 4 == 3) {
|
61 | | - printf(" ");
|
62 | | - }
|
63 | | -
|
64 | | - if (i % 16 == 15 && i + 1 < length) {
|
65 | | - printf("\n");
|
66 | | - }
|
67 | | - }
|
68 | | -
|
69 | | - printf("\n");
|
70 | | -}
|
71 | | -
|
72 | | -void print_error(const int code) {
|
73 | | - if (code > 0) {
|
74 | | - fprintf(stderr, "error: code %d\n", code);
|
75 | | - }
|
76 | | - else {
|
77 | | - fprintf(stderr, "libusb error: %s (code %d)\n", (
|
78 | | - LIBUSB_ERROR_OTHER == code || code > 0 ?
|
79 | | - "unspecified" :
|
80 | | - libusb_error_messages[-code]
|
81 | | - ), code);
|
82 | | - }
|
83 | | -}
|
| 2 | +// |
| 3 | +// |
| 4 | +// Copyright 2011 user890104 |
| 5 | +// |
| 6 | +// |
| 7 | +// This file is part of ipoddfu. |
| 8 | +// |
| 9 | +// ipoddfu is free software: you can redistribute it and/or |
| 10 | +// modify it under the terms of the GNU General Public License as |
| 11 | +// published by the Free Software Foundation, either version 2 of the |
| 12 | +// License, or (at your option) any later version. |
| 13 | +// |
| 14 | +// ipoddfu is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +// See the GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License along |
| 20 | +// with ipoddfu. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +// |
| 22 | +// |
| 23 | + |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | + |
| 27 | +#include <libusb-1.0/libusb.h> |
| 28 | + |
| 29 | +#include "misc.h" |
| 30 | + |
| 31 | +const char *libusb_error_messages[13] = |
| 32 | +{ |
| 33 | + "No error", // LIBUSB_SUCCESS = 0 |
| 34 | + "Input/output error", // LIBUSB_ERROR_IO = -1 |
| 35 | + "Invalid parameter", // LIBUSB_ERROR_INVALID_PARAM = -2 |
| 36 | + "Access denied", // LIBUSB_ERROR_ACCESS = -3 |
| 37 | + "No such device", // LIBUSB_ERROR_NO_DEVICE = -4 |
| 38 | + "Entity not found", // LIBUSB_ERROR_NOT_FOUND = -5 |
| 39 | + "Resource busy", // LIBUSB_ERROR_BUSY = -6 |
| 40 | + "Operation timed out", // LIBUSB_ERROR_TIMEOUT = -7 |
| 41 | + "Overflow", // LIBUSB_ERROR_OVERFLOW = -8 |
| 42 | + "Pipe error", // LIBUSB_ERROR_PIPE = -9 |
| 43 | + "System call interrupted", // LIBUSB_ERROR_INTERRUPTED = -10 |
| 44 | + "Insufficient memory", // LIBUSB_ERROR_NO_MEM = -11 |
| 45 | + "Operation not supported", // LIBUSB_ERROR_NOT_SUPPORTED = -12 |
| 46 | +}; |
| 47 | + |
| 48 | +long int fgetsize(FILE *fp) |
| 49 | +{ |
| 50 | + static long int pos, size; |
| 51 | + |
| 52 | + if (-1L == (pos = ftell(fp))) |
| 53 | + { |
| 54 | + perror("ftell"); |
| 55 | + |
| 56 | + return -1L; |
| 57 | + } |
| 58 | + |
| 59 | + if(fseek(fp, 0L, SEEK_END)) |
| 60 | + { |
| 61 | + perror("fseek"); |
| 62 | + |
| 63 | + return -1L; |
| 64 | + } |
| 65 | + |
| 66 | + if (-1L == (size = ftell(fp))) |
| 67 | + { |
| 68 | + perror("ftell"); |
| 69 | + |
| 70 | + return -1L; |
| 71 | + } |
| 72 | + |
| 73 | + if (fseek(fp, pos, SEEK_SET)) |
| 74 | + { |
| 75 | + perror("fseek"); |
| 76 | + |
| 77 | + return -1L; |
| 78 | + } |
| 79 | + |
| 80 | + return size; |
| 81 | +} |
| 82 | + |
| 83 | +void dump_packet(const unsigned char *data, const unsigned int length) |
| 84 | +{ |
| 85 | + static unsigned int i; |
| 86 | + |
| 87 | + for (i = 0; i < length; ++i) |
| 88 | + { |
| 89 | + printf("%02x ", data[i]); |
| 90 | + |
| 91 | + if (i % 4 == 3) |
| 92 | + { |
| 93 | + printf(" "); |
| 94 | + } |
| 95 | + |
| 96 | + if (i % 16 == 15 && i + 1 < length) |
| 97 | + { |
| 98 | + printf("\n"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + printf("\n"); |
| 103 | +} |
| 104 | + |
| 105 | +void print_error(const int code) |
| 106 | +{ |
| 107 | + if (code > 0) |
| 108 | + { |
| 109 | + fprintf(stderr, "error: code %d\n", code); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + fprintf(stderr, "libusb error: %s (code %d)\n", ( |
| 114 | + LIBUSB_ERROR_OTHER == code || code > 0 ? |
| 115 | + "unspecified" : libusb_error_messages[-code] |
| 116 | + ), code); |
| 117 | + } |
| 118 | +} |
Index: tools/ipoddfu_c/crc32.c |
— | — | @@ -17,29 +17,34 @@ |
18 | 18 | * Standard seed is 0xffffffff or 0. |
19 | 19 | * Some implementations xor result with 0xffffffff after calculation. |
20 | 20 | */ |
21 | | -uint32_t crc32(void *data, unsigned int len, uint32_t seed) { |
22 | | - uint8_t *d = data; |
23 | | - |
24 | | - while (len--) { |
25 | | - seed = ((seed >> 8) & 0x00FFFFFF) ^ crc32table [(seed ^ *d++) & 0xFF]; |
26 | | - } |
27 | | - |
28 | | - return seed; |
| 21 | +uint32_t crc32(void *data, unsigned int len, uint32_t seed) |
| 22 | +{ |
| 23 | + uint8_t *d = data; |
| 24 | + |
| 25 | + while (len--) |
| 26 | + { |
| 27 | + seed = ((seed >> 8) & 0x00FFFFFF) ^ crc32table [(seed ^ *d++) & 0xFF]; |
| 28 | + } |
| 29 | + |
| 30 | + return seed; |
29 | 31 | } |
30 | 32 | |
31 | 33 | /* Calculate crc32table */ |
32 | | -void crc32_init() { |
33 | | - uint32_t poly = 0xEDB88320L; |
34 | | - uint32_t crc; |
35 | | - int i, j; |
| 34 | +void crc32_init() |
| 35 | +{ |
| 36 | + uint32_t poly = 0xEDB88320L; |
| 37 | + uint32_t crc; |
| 38 | + int i, j; |
36 | 39 | |
37 | | - for (i = 0; i < 256; ++i) { |
38 | | - crc = i; |
39 | | - |
40 | | - for (j = 8; j > 0; --j) { |
41 | | - crc = (crc >> 1) ^ ((crc & 1) ? poly : 0); |
42 | | - } |
43 | | - |
44 | | - crc32table[i] = crc; |
45 | | - } |
| 40 | + for (i = 0; i < 256; ++i) |
| 41 | + { |
| 42 | + crc = i; |
| 43 | + |
| 44 | + for (j = 8; j > 0; --j) |
| 45 | + { |
| 46 | + crc = (crc >> 1) ^ ((crc & 1) ? poly : 0); |
| 47 | + } |
| 48 | + |
| 49 | + crc32table[i] = crc; |
| 50 | + } |
46 | 51 | } |