1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2010 Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
27 void put32le(uint8_t *buf
, uint32_t i
)
30 *buf
++ = (i
>> 8) & 0xff;
31 *buf
++ = (i
>> 16) & 0xff;
32 *buf
++ = (i
>> 24) & 0xff;
35 void put32be(uint8_t *buf
, uint32_t i
)
37 *buf
++ = (i
>> 24) & 0xff;
38 *buf
++ = (i
>> 16) & 0xff;
39 *buf
++ = (i
>> 8) & 0xff;
50 struct dev_info_t g_dev_info
[] =
52 {0x066f, 0x3780, 1024}, /* i.MX233 / STMP3780 */
53 {0x066f, 0x3770, 48}, /* STMP3770 */
54 {0x15A2, 0x004F, 1024}, /* i.MX28 */
57 int main(int argc
, char **argv
)
61 int i
, xfer_size
, nr_xfers
, recv_size
;
65 printf("usage: %s <xfer size> <file>\n", argv
[0]);
66 printf("If <xfer size> is set to zero, the preferred one is used.\n");
71 xfer_size
= strtol(argv
[1], &end
, 0);
72 if(end
!= (argv
[1] + strlen(argv
[1])))
74 printf("Invalid transfer size !\n");
78 libusb_device_handle
*dev
;
82 libusb_set_debug(NULL
, 3);
84 for(unsigned i
= 0; i
< sizeof(g_dev_info
) / sizeof(g_dev_info
[0]); i
++)
86 dev
= libusb_open_device_with_vid_pid(NULL
,
87 g_dev_info
[i
].vendor_id
, g_dev_info
[i
].product_id
);
91 xfer_size
= g_dev_info
[i
].xfer_size
;
92 printf("Found a match for %04x:%04x\n",
93 g_dev_info
[i
].vendor_id
, g_dev_info
[i
].product_id
);
98 printf("Cannot open device\n");
102 libusb_detach_kernel_driver(dev
, 0);
103 libusb_detach_kernel_driver(dev
, 4);
105 libusb_claim_interface (dev
, 0);
106 libusb_claim_interface (dev
, 4);
114 f
= fopen(argv
[2], "r");
117 perror("cannot open file");
120 fseek(f
, 0, SEEK_END
);
121 size_t size
= ftell(f
);
122 fseek(f
, 0, SEEK_SET
);
124 printf("Transfer size: %d\n", xfer_size
);
125 nr_xfers
= (size
+ xfer_size
- 1) / xfer_size
;
126 uint8_t *file_buf
= malloc(nr_xfers
* xfer_size
);
127 memset(file_buf
, 0xff, nr_xfers
* xfer_size
); // pad with 0xff
128 if(fread(file_buf
, size
, 1, f
) != 1)
130 perror("read error");
136 uint8_t *xfer_buf
= malloc(1 + xfer_size
);
137 uint8_t *p
= xfer_buf
;
139 *p
++ = 0x01; /* Report id */
141 /* Command block wrapper */
142 *p
++ = 'B'; /* Signature */
146 put32le(p
, 0x1); /* Tag */
148 put32le(p
, size
); /* Payload size */
150 *p
++ = 0; /* Flags (host to device) */
151 p
+= 2; /* Reserved */
153 /* Command descriptor block */
154 *p
++ = 0x02; /* Firmware download */
155 put32be(p
, size
); /* Download size */
157 ret
= libusb_control_transfer(dev
,
158 LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE
, 0x9, 0x201, 0,
159 xfer_buf
, xfer_size
+ 1, 1000);
162 printf("transfer error at init step\n");
166 for(i
= 0; i
< nr_xfers
; i
++)
169 memcpy(&xfer_buf
[1], &file_buf
[i
* xfer_size
], xfer_size
);
171 ret
= libusb_control_transfer(dev
,
172 LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_RECIPIENT_INTERFACE
,
173 0x9, 0x202, 0, xfer_buf
, xfer_size
+ 1, 1000);
176 printf("transfer error at send step %d\n", i
);
181 ret
= libusb_interrupt_transfer(dev
, 0x81, xfer_buf
, xfer_size
, &recv_size
,
185 printf("transfer error at final stage\n");
189 printf("ret %i\n", ret
);