diff options
author | Amaury Pouly <amaury.pouly@gmail.com> | 2012-05-23 11:03:35 +0200 |
---|---|---|
committer | Amaury Pouly <amaury.pouly@gmail.com> | 2012-05-31 13:57:25 +0200 |
commit | a87a9ef37372b4380808ec2efa7c762e137668f1 (patch) | |
tree | 9c759088f0f9cf6717d96789b6805812f6b187ea /utils/imxtools/sbtools/sbloader.c | |
parent | ba8e4367fb4d116ffc01c12cc619bfc714e582c9 (diff) | |
download | rockbox-a87a9ef37372b4380808ec2efa7c762e137668f1.tar.gz rockbox-a87a9ef37372b4380808ec2efa7c762e137668f1.zip |
imxtools: move tools to a new sbtools/ subdirectory
Change-Id: I0d8d6831b35037725486f61fc363de87bc8ba92e
Diffstat (limited to 'utils/imxtools/sbtools/sbloader.c')
-rw-r--r-- | utils/imxtools/sbtools/sbloader.c | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/utils/imxtools/sbtools/sbloader.c b/utils/imxtools/sbtools/sbloader.c new file mode 100644 index 0000000000..e1534ea7d5 --- /dev/null +++ b/utils/imxtools/sbtools/sbloader.c | |||
@@ -0,0 +1,193 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 Amaury Pouly | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include <stdio.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <string.h> | ||
24 | #include <libusb.h> | ||
25 | #include <stdint.h> | ||
26 | |||
27 | void put32le(uint8_t *buf, uint32_t i) | ||
28 | { | ||
29 | *buf++ = i & 0xff; | ||
30 | *buf++ = (i >> 8) & 0xff; | ||
31 | *buf++ = (i >> 16) & 0xff; | ||
32 | *buf++ = (i >> 24) & 0xff; | ||
33 | } | ||
34 | |||
35 | void put32be(uint8_t *buf, uint32_t i) | ||
36 | { | ||
37 | *buf++ = (i >> 24) & 0xff; | ||
38 | *buf++ = (i >> 16) & 0xff; | ||
39 | *buf++ = (i >> 8) & 0xff; | ||
40 | *buf++ = i & 0xff; | ||
41 | } | ||
42 | |||
43 | struct dev_info_t | ||
44 | { | ||
45 | uint16_t vendor_id; | ||
46 | uint16_t product_id; | ||
47 | unsigned xfer_size; | ||
48 | }; | ||
49 | |||
50 | struct dev_info_t g_dev_info[] = | ||
51 | { | ||
52 | {0x066f, 0x3780, 1024}, /* i.MX233 / STMP3780 */ | ||
53 | {0x066f, 0x3770, 48}, /* STMP3770 */ | ||
54 | {0x15A2, 0x004F, 1024}, /* i.MX28 */ | ||
55 | }; | ||
56 | |||
57 | int main(int argc, char **argv) | ||
58 | { | ||
59 | int ret; | ||
60 | FILE *f; | ||
61 | int i, xfer_size, nr_xfers, recv_size; | ||
62 | |||
63 | if(argc != 3) | ||
64 | { | ||
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"); | ||
67 | return 1; | ||
68 | } | ||
69 | |||
70 | char *end; | ||
71 | xfer_size = strtol(argv[1], &end, 0); | ||
72 | if(end != (argv[1] + strlen(argv[1]))) | ||
73 | { | ||
74 | printf("Invalid transfer size !\n"); | ||
75 | return 1; | ||
76 | } | ||
77 | |||
78 | libusb_device_handle *dev; | ||
79 | |||
80 | libusb_init(NULL); | ||
81 | |||
82 | libusb_set_debug(NULL, 3); | ||
83 | |||
84 | for(unsigned i = 0; i < sizeof(g_dev_info) / sizeof(g_dev_info[0]); i++) | ||
85 | { | ||
86 | dev = libusb_open_device_with_vid_pid(NULL, | ||
87 | g_dev_info[i].vendor_id, g_dev_info[i].product_id); | ||
88 | if(dev == NULL) | ||
89 | continue; | ||
90 | if(xfer_size == 0) | ||
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); | ||
94 | break; | ||
95 | } | ||
96 | if(dev == NULL) | ||
97 | { | ||
98 | printf("Cannot open device\n"); | ||
99 | return 1; | ||
100 | } | ||
101 | |||
102 | libusb_detach_kernel_driver(dev, 0); | ||
103 | libusb_detach_kernel_driver(dev, 4); | ||
104 | |||
105 | libusb_claim_interface (dev, 0); | ||
106 | libusb_claim_interface (dev, 4); | ||
107 | |||
108 | if(!dev) | ||
109 | { | ||
110 | printf("No dev\n"); | ||
111 | exit(1); | ||
112 | } | ||
113 | |||
114 | f = fopen(argv[2], "r"); | ||
115 | if(f == NULL) | ||
116 | { | ||
117 | perror("cannot open file"); | ||
118 | return 1; | ||
119 | } | ||
120 | fseek(f, 0, SEEK_END); | ||
121 | size_t size = ftell(f); | ||
122 | fseek(f, 0, SEEK_SET); | ||
123 | |||
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) | ||
129 | { | ||
130 | perror("read error"); | ||
131 | fclose(f); | ||
132 | return 1; | ||
133 | } | ||
134 | fclose(f); | ||
135 | |||
136 | uint8_t *xfer_buf = malloc(1 + xfer_size); | ||
137 | uint8_t *p = xfer_buf; | ||
138 | |||
139 | *p++ = 0x01; /* Report id */ | ||
140 | |||
141 | /* Command block wrapper */ | ||
142 | *p++ = 'B'; /* Signature */ | ||
143 | *p++ = 'L'; | ||
144 | *p++ = 'T'; | ||
145 | *p++ = 'C'; | ||
146 | put32le(p, 0x1); /* Tag */ | ||
147 | p += 4; | ||
148 | put32le(p, size); /* Payload size */ | ||
149 | p += 4; | ||
150 | *p++ = 0; /* Flags (host to device) */ | ||
151 | p += 2; /* Reserved */ | ||
152 | |||
153 | /* Command descriptor block */ | ||
154 | *p++ = 0x02; /* Firmware download */ | ||
155 | put32be(p, size); /* Download size */ | ||
156 | |||
157 | ret = libusb_control_transfer(dev, | ||
158 | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, 0x9, 0x201, 0, | ||
159 | xfer_buf, xfer_size + 1, 1000); | ||
160 | if(ret < 0) | ||
161 | { | ||
162 | printf("transfer error at init step\n"); | ||
163 | return 1; | ||
164 | } | ||
165 | |||
166 | for(i = 0; i < nr_xfers; i++) | ||
167 | { | ||
168 | xfer_buf[0] = 0x2; | ||
169 | memcpy(&xfer_buf[1], &file_buf[i * xfer_size], xfer_size); | ||
170 | |||
171 | ret = libusb_control_transfer(dev, | ||
172 | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, | ||
173 | 0x9, 0x202, 0, xfer_buf, xfer_size + 1, 1000); | ||
174 | if(ret < 0) | ||
175 | { | ||
176 | printf("transfer error at send step %d\n", i); | ||
177 | return 1; | ||
178 | } | ||
179 | } | ||
180 | |||
181 | ret = libusb_interrupt_transfer(dev, 0x81, xfer_buf, xfer_size, &recv_size, | ||
182 | 1000); | ||
183 | if(ret < 0) | ||
184 | { | ||
185 | printf("transfer error at final stage\n"); | ||
186 | return 1; | ||
187 | } | ||
188 | |||
189 | printf("ret %i\n", ret); | ||
190 | |||
191 | return 0; | ||
192 | } | ||
193 | |||