Fix building on Windows
[Rockbox.git] / rbutil / e200rpatcher / e200rpatcher.c
blobbdc8da69fe61e54ea5d501b4ec476ad73e9213c7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Dave Chapman
12 * USB code based on ifp-line - http://ifp-driver.sourceforge.net
14 * ifp-line is (C) Pavel Kriz, Jun Yamishiro and Joe Roback and
15 * licensed under the GPL (v2)
18 * All files in this archive are subject to the GNU General Public License.
19 * See the file COPYING in the source tree root for full license agreement.
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
24 ****************************************************************************/
27 #include <stdio.h>
28 #include <inttypes.h>
29 #include <usb.h>
30 #include <string.h>
31 #include "stdbool.h"
33 #include "bootimg.h"
35 #define VERSION "0.2"
37 /* USB IDs for Manufacturing Mode */
38 #define E200R_VENDORID 0x0781
39 #define E200R_PRODUCTID 0x0720
41 #define E200R_BULK_TO 1
42 #define TOUT 5000
43 #define MAX_TRANSFER 64 /* Number of bytes to send in one write */
45 #ifndef MAX
46 #define MAX(a,b) (((a)>(b))?(a):(b))
47 #endif
49 static void put_int32le(uint32_t x, char* p)
51 p[0] = x & 0xff;
52 p[1] = (x >> 8) & 0xff;
53 p[2] = (x >> 16) & 0xff;
54 p[3] = (x >> 24) & 0xff;
57 int upload_app(usb_dev_handle* dh)
59 char buf[4];
60 int err;
61 int tosend;
62 char* p = (char*)bootimg;
63 int bytesleft = LEN_bootimg;
65 /* Write the data length */
67 put_int32le(LEN_bootimg, buf);
69 err = usb_bulk_write(dh, E200R_BULK_TO, buf, 4, TOUT);
71 if (err < 0)
73 fprintf(stderr,"[ERR] Error writing data length\n");
74 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
75 return -1;
78 /* Now send the data, MAX_TRANSFER bytes at a time. */
80 while (bytesleft > 0)
82 tosend = MAX(MAX_TRANSFER, bytesleft);
84 err = usb_bulk_write(dh, E200R_BULK_TO, p, tosend, TOUT);
86 if (err < 0)
88 fprintf(stderr,"[ERR] Error writing data\n");
89 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
90 return -1;
93 p += tosend;
94 bytesleft -= tosend;
97 return 0;
101 /* The main function */
103 void do_patching(void)
105 struct usb_bus *busses;
106 struct usb_bus *bus;
107 struct usb_device *tmp_dev;
108 struct usb_device *dev = NULL;
109 usb_dev_handle *dh;
110 int err;
112 fprintf(stderr,"[INFO] Searching for E200R\n");
114 usb_init();
116 if(usb_find_busses() < 0) {
117 fprintf(stderr, "[ERR] Could not find any USB busses.\n");
118 return;
121 if (usb_find_devices() < 0) {
122 fprintf(stderr, "[ERR] USB devices not found(nor hubs!).\n");
123 return;
126 /* C calling convention, it's not nice to use global stuff */
127 busses = usb_get_busses();
129 for (bus = busses; bus; bus = bus->next) {
130 for (tmp_dev = bus->devices; tmp_dev; tmp_dev = tmp_dev->next) {
131 if (tmp_dev->descriptor.idVendor == E200R_VENDORID &&
132 tmp_dev->descriptor.idProduct == E200R_PRODUCTID ) {
134 dev = tmp_dev;
135 goto found;
141 if (dev == NULL) {
142 fprintf(stderr, "[ERR] E200R device not found.\n");
143 fprintf(stderr, "[ERR] Ensure your E200R is in manufacturing mode and run e200rpatcher again.\n");
144 return;
147 found:
148 if ( (dh = usb_open(dev)) == NULL) {
149 fprintf(stderr,"[ERR] Unable to open E200R device.\n");
150 return;
153 err = usb_set_configuration(dh, 1);
155 if (err < 0) {
156 fprintf(stderr, "[ERR] usb_set_configuration failed (%d)\n", err);
157 usb_close(dh);
158 return;
161 /* "must be called" written in the libusb documentation */
162 err = usb_claim_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
163 if (err < 0) {
164 fprintf(stderr, "[ERR] Unable to claim interface (%d)\n", err);
165 usb_close(dh);
166 return;
169 fprintf(stderr,"[INFO] Found E200R, uploading patching application.\n");
171 /* Now we can transfer the application to the device. */
173 if (upload_app(dh) < 0)
175 fprintf(stderr,"[ERR] Upload of application failed.\n");
177 else
179 fprintf(stderr,"[INFO] Patching application uploaded successfully!\n");
182 /* release claimed interface */
183 usb_release_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
185 usb_close(dh);
187 void print_usage(void)
189 fprintf(stderr,"Usage: e200rpatcher [options]\n");
190 fprintf(stderr,"Options:\n");
191 fprintf(stderr," -s, --silent\t\tDont display instructions\n");
194 int main(int argc, char* argv[])
196 char input[4];
197 int silent = 0;
198 int i;
200 /* check args */
201 if ((argc > 1) && ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))) {
202 print_usage();
203 return 1;
205 for (i=1;i<argc;i++)
207 if (!strcmp(argv[i], "--silent") || !strcmp(argv[i], "-s"))
208 silent = 1;
211 printf("e200rpatcher v" VERSION " - (C) 2007 Jonathan Gordon & Dave Chapman\n");
212 printf("This is free software; see the source for copying conditions. There is NO\n");
213 printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
215 if (!silent)
217 printf("Attach your E200R in \"manufacturing mode\" as follows:\n");
218 printf(" 1) Power-off your E200R\n");
219 printf(" 2) Turn ON the lock/hold switch\n");
220 printf(" 3) Press and hold the SELECT button and whilst it is held down,\n");
221 printf(" attach your E200R to your computer via USB\n");
222 printf(" 4) After attaching to USB, keep the SELECT button held for 10 seconds.\n");
223 printf("\n");
224 printf("NOTE: If your E200R starts in the normal Sansa firmware, you have\n");
225 printf(" failed to enter manufacturing mode and should try again at step 1).\n\n");
227 printf("[INFO] Press Enter to continue:");
228 fgets(input, 4, stdin);
230 do_patching();
232 if (!silent)
234 printf("[INFO] Press ENTER to exit: ");
235 fgets(input, 4, stdin);
238 return 0;