Root/
Source at commit 40602c22354a988b7edc688e0672ab0ce479bf12 created 14 years 7 months ago. By Wolfgang Spraul, added 0x for hex | |
---|---|
1 | // |
2 | // Authors: Wolfgang Spraul <wolfgang@qi-hardware.com> |
3 | // |
4 | // This program is free software; you can redistribute it and/or |
5 | // modify it under the terms of the GNU General Public License |
6 | // as published by the Free Software Foundation; either version |
7 | // 3 of the License, or (at your option) any later version. |
8 | // |
9 | |
10 | #include <stdio.h> |
11 | #include <string.h> |
12 | #include <usb.h> |
13 | #include "xbboot_version.h" |
14 | |
15 | #define HIWORD(dw) (((dw) >> 16) & 0xFFFF) |
16 | #define LOWORD(dw) ((dw) & 0xFFFF) |
17 | |
18 | #define INGENIC_VENDOR_ID 0x601A |
19 | #define INGENIC_XBURST_USBBOOT 0x4740 |
20 | |
21 | // REQ_ values are negative so they can be mixed together with the VR_ types in a signed integer. |
22 | #define REQ_BULK_READ -1 |
23 | #define REQ_BULK_WRITE -2 |
24 | |
25 | #define VR_GET_CPU_INFO 0x00 |
26 | #define VR_SET_DATA_ADDRESS 0x01 |
27 | #define VR_SET_DATA_LENGTH 0x02 |
28 | #define VR_FLUSH_CACHES 0x03 |
29 | #define VR_PROGRAM_START1 0x04 |
30 | #define VR_PROGRAM_START2 0x05 |
31 | #define VR_NOR_OPS 0x06 |
32 | #define VR_NAND_OPS 0x07 |
33 | #define VR_SDRAM_OPS 0x08 |
34 | #define VR_CONFIGRATION 0x09 |
35 | #define VR_GET_NUM 0x0a |
36 | |
37 | #define USB_TIMEOUT 3000 |
38 | #define VR_GET_CPU_INFO_LEN 8 |
39 | #define INGENIC_IN_ENDPOINT 0x81 |
40 | #define INGENIC_OUT_ENDPOINT 0x01 |
41 | |
42 | int main(int argc, char** argv) |
43 | { |
44 | struct usb_device* xburst_dev = 0; |
45 | uint8_t xburst_interface = 0; |
46 | struct usb_dev_handle* xburst_h = 0; |
47 | int request_type, usb_status; |
48 | |
49 | if (argc < 2 |
50 | || !strcmp(argv[1], "-h") |
51 | || !strcmp(argv[1], "--help")) { |
52 | printf("\n" |
53 | "xbboot version %s - Ingenic XBurst USB Boot Vendor Requests\n" |
54 | "(c) 2009 Wolfgang Spraul\n" |
55 | "Report bugs to <wolfgang@qi-hardware.com>.\n" |
56 | "\n" |
57 | "xbboot [vendor_request] ... (must run as root)\n" |
58 | " -h --help print this help message\n" |
59 | " -v --version print the version number\n" |
60 | "\n" |
61 | " bulk_read <len> read len bulk bytes from USB, write to stdout\n" |
62 | " bulk_write <path> write file at <path> to USB\n" |
63 | " [get_info | VR_GET_CPU_INFO] read 8-byte CPU info and write to stdout\n" |
64 | " [set_addr | VR_SET_DATA_ADDRESS] <addr> send memory address\n" |
65 | " [set_len | VR_SET_DATA_LENGTH] <len> send data length\n" |
66 | " [flush_cache | VR_FLUSH_CACHES] flush I-Cache and D-Cache\n" |
67 | " [start1 | VR_PROGRAM_START1] <addr> transfer data from D-Cache to I-Cache and branch to I-Cache\n" |
68 | " [start2 | VR_PROGRAM_START2] <addr> branch to <addr> directly\n" |
69 | "\n" |
70 | "- all numbers can be prefixed 0x for hex otherwise decimal\n" |
71 | "\n", XBBOOT_VERSION); |
72 | // stage1: 0x80002000 |
73 | // stage2: 0x81C00000 |
74 | // u-boot: 0x80600000 |
75 | // uImage: 0x80010000 |
76 | return EXIT_SUCCESS; |
77 | } |
78 | if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) { |
79 | printf("xbboot version %s\n", XBBOOT_VERSION); |
80 | return EXIT_SUCCESS; |
81 | } |
82 | if (!strcmp(argv[1], "bulk_read")) |
83 | request_type = REQ_BULK_READ; |
84 | else if (!strcmp(argv[1], "bulk_write")) |
85 | request_type = REQ_BULK_WRITE; |
86 | else if (!strcmp(argv[1], "VR_GET_CPU_INFO") || !strcmp(argv[1], "get_info")) |
87 | request_type = VR_GET_CPU_INFO; |
88 | else if (!strcmp(argv[1], "VR_SET_DATA_ADDRESS") || !strcmp(argv[1], "set_addr")) |
89 | request_type = VR_SET_DATA_ADDRESS; |
90 | else if (!strcmp(argv[1], "VR_SET_DATA_LENGTH") || !strcmp(argv[1], "set_len")) |
91 | request_type = VR_SET_DATA_LENGTH; |
92 | else if (!strcmp(argv[1], "VR_FLUSH_CACHES") || !strcmp(argv[1], "flush_cache")) |
93 | request_type = VR_FLUSH_CACHES; |
94 | else if (!strcmp(argv[1], "VR_PROGRAM_START1") || !strcmp(argv[1], "start1")) |
95 | request_type = VR_PROGRAM_START1; |
96 | else if (!strcmp(argv[1], "VR_PROGRAM_START2") || !strcmp(argv[1], "start2")) |
97 | request_type = VR_PROGRAM_START2; |
98 | else { |
99 | fprintf(stderr, "Error - unknown vendor request %s - run with --help to see all requests\n", argv[1]); |
100 | return EXIT_FAILURE; |
101 | } |
102 | if ((getuid()) || (getgid())) { |
103 | fprintf(stderr, "Error - you must be root to run '%s'\n", argv[0]); |
104 | return EXIT_FAILURE; |
105 | } |
106 | |
107 | usb_init(); |
108 | usb_find_busses(); |
109 | usb_find_devices(); |
110 | |
111 | // look for Ingenic XBurst USB boot device & interface |
112 | { |
113 | { |
114 | struct usb_bus* usb_bus; |
115 | struct usb_device* usb_dev; |
116 | |
117 | for (usb_bus = usb_get_busses(); usb_bus != 0; usb_bus = usb_bus->next) { |
118 | for (usb_dev = usb_bus->devices; usb_dev != 0; usb_dev = usb_dev->next) { |
119 | if (usb_dev->descriptor.idVendor == INGENIC_VENDOR_ID |
120 | && usb_dev->descriptor.idProduct == INGENIC_XBURST_USBBOOT) { |
121 | if (xburst_dev) { |
122 | fprintf(stderr, "Error - more than one XBurst boot device found.\n"); |
123 | goto xout; |
124 | } |
125 | xburst_dev = usb_dev; |
126 | // keep searching to make sure there is only 1 XBurst device |
127 | } |
128 | } |
129 | } |
130 | if (!xburst_dev) { |
131 | fprintf(stderr, "Error - no XBurst boot device found.\n"); |
132 | goto xout; |
133 | } |
134 | } |
135 | { |
136 | struct usb_config_descriptor* usb_config_desc; |
137 | struct usb_interface_descriptor* usb_if_desc; |
138 | struct usb_interface* usb_if; |
139 | int cfg_index, if_index, alt_index; |
140 | |
141 | for (cfg_index = 0; cfg_index < xburst_dev->descriptor.bNumConfigurations; cfg_index++) { |
142 | usb_config_desc = &xburst_dev->config[cfg_index]; |
143 | if (!usb_config_desc) { |
144 | fprintf(stderr, "Error - usb_config_desc NULL\n"); |
145 | goto xout; |
146 | } |
147 | for (if_index = 0; if_index < usb_config_desc->bNumInterfaces; if_index++) { |
148 | usb_if = &usb_config_desc->interface[if_index]; |
149 | if (!usb_if) { |
150 | fprintf(stderr, "Error - usb_if NULL\n"); |
151 | goto xout; |
152 | } |
153 | for (alt_index = 0; alt_index < usb_if->num_altsetting; alt_index++) { |
154 | usb_if_desc = &usb_if->altsetting[alt_index]; |
155 | if (!usb_if_desc) { |
156 | fprintf(stderr, "Error - usb_if_desc NULL\n"); |
157 | goto xout; |
158 | } |
159 | if (usb_if_desc->bInterfaceClass == 0xFF |
160 | && usb_if_desc->bInterfaceSubClass == 0) { |
161 | xburst_interface = usb_if_desc->bInterfaceNumber; |
162 | goto interface_found; |
163 | } |
164 | } |
165 | } |
166 | } |
167 | interface_found: ; |
168 | } |
169 | } |
170 | xburst_h = usb_open(xburst_dev); |
171 | if (!xburst_h) { |
172 | fprintf(stderr, "Error - can't open XBurst device: %s\n", usb_strerror()); |
173 | goto xout; |
174 | } |
175 | if (usb_claim_interface(xburst_h, xburst_interface) < 0) { |
176 | fprintf(stderr, "Error - can't claim XBurst interface: %s\n", usb_strerror()); |
177 | goto xout_xburst_h; |
178 | } |
179 | switch (request_type) { |
180 | case VR_GET_CPU_INFO: { |
181 | char cpu_info_buf[VR_GET_CPU_INFO_LEN+1] = {0}; |
182 | usb_status = usb_control_msg(xburst_h, |
183 | /* requesttype */ USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
184 | /* request */ VR_GET_CPU_INFO, |
185 | /* value */ 0, |
186 | /* index */ 0, |
187 | /* bytes */ cpu_info_buf, |
188 | /* size */ VR_GET_CPU_INFO_LEN, |
189 | /* timeout */ USB_TIMEOUT); |
190 | if (usb_status != VR_GET_CPU_INFO_LEN) { |
191 | fprintf(stderr, "Error - %s() returned %i\n", argv[1], usb_status); |
192 | goto xout_xburst_interface; |
193 | } |
194 | printf("VR_GET_CPU_INFO %s\n", cpu_info_buf); |
195 | break; |
196 | } |
197 | case VR_SET_DATA_ADDRESS: |
198 | case VR_SET_DATA_LENGTH: |
199 | case VR_PROGRAM_START1: |
200 | case VR_PROGRAM_START2: { |
201 | uint32_t u32_param; |
202 | if (argc != 3) { |
203 | fprintf(stderr, "Error - number of %s parameters %i\n", argv[1], argc); |
204 | goto xout_xburst_interface; |
205 | } |
206 | if (argv[2][0] == '0' && argv[2][1] == 'x') |
207 | u32_param = strtoul(&argv[2][2], 0 /* endptr */, 16 /* base */); |
208 | else |
209 | u32_param = strtoul(argv[2], 0 /* endptr */, 10 /* base */); |
210 | |
211 | usb_status = usb_control_msg(xburst_h, |
212 | /* requesttype */ USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
213 | /* request */ request_type, |
214 | /* value */ HIWORD(u32_param), |
215 | /* index */ LOWORD(u32_param), |
216 | /* bytes */ 0, |
217 | /* size */ 0, |
218 | /* timeout */ USB_TIMEOUT); |
219 | if (usb_status) { |
220 | fprintf(stderr, "Error - %s() returned %i\n", argv[1], usb_status); |
221 | goto xout_xburst_interface; |
222 | } |
223 | printf("%s %lxh\n", argv[1], (unsigned long) u32_param); |
224 | break; |
225 | } |
226 | case VR_FLUSH_CACHES: { |
227 | usb_status = usb_control_msg(xburst_h, |
228 | /* requesttype */ USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
229 | /* request */ VR_FLUSH_CACHES, |
230 | /* value */ 0, |
231 | /* index */ 0, |
232 | /* bytes */ 0, |
233 | /* size */ 0, |
234 | /* timeout */ USB_TIMEOUT); |
235 | if (usb_status) { |
236 | fprintf(stderr, "Error - %s() returned %i\n", argv[1], usb_status); |
237 | goto xout_xburst_interface; |
238 | } |
239 | printf("VR_FLUSH_CACHES\n"); |
240 | break; |
241 | } |
242 | case REQ_BULK_READ: { |
243 | int read_len; |
244 | char* read_buf; |
245 | |
246 | if (argc != 3) { |
247 | fprintf(stderr, "Error - number of %s parameters %i\n", argv[1], argc); |
248 | goto xout_xburst_interface; |
249 | } |
250 | if (argv[2][0] == '0' && argv[2][1] == 'x') |
251 | read_len = strtol(&argv[2][2], 0 /* endptr */, 16 /* base */); |
252 | else |
253 | read_len = strtol(argv[2], 0 /* endptr */, 10 /* base */); |
254 | read_buf = (char*) malloc(read_len); |
255 | if (!read_buf) { |
256 | fprintf(stderr, "Error - cannot allocate %i bytes read buffer.\n", read_len); |
257 | goto xout_xburst_interface; |
258 | } |
259 | usb_status = usb_bulk_read(xburst_h, |
260 | /* endpoint */ INGENIC_IN_ENDPOINT, |
261 | /* bytes */ read_buf, |
262 | /* size */ read_len, |
263 | /* timeout */ USB_TIMEOUT); |
264 | if (usb_status > 0) |
265 | fwrite(read_buf, 1 /* size */, usb_status, stdout); |
266 | free(read_buf); |
267 | if (usb_status < read_len) { |
268 | fprintf(stderr, "Error reading %d bytes (result %i).\n", read_len, usb_status); |
269 | goto xout_xburst_interface; |
270 | } |
271 | break; |
272 | } |
273 | case REQ_BULK_WRITE: { |
274 | char* file_data; |
275 | int file_len; |
276 | FILE* file_h; |
277 | size_t num_read; |
278 | |
279 | if (argc != 3) { |
280 | fprintf(stderr, "Error - number of parameters %i\n", argc); |
281 | goto xout_xburst_interface; |
282 | } |
283 | file_h = fopen(argv[2], "rb"); |
284 | if (!file_h) { |
285 | fprintf(stderr, "Error opening %s.\n", argv[2]); |
286 | goto xout_xburst_interface; |
287 | } |
288 | if (fseek(file_h, 0, SEEK_END)) { |
289 | fprintf(stderr, "Error seeking to end of %s.\n", argv[2]); |
290 | fclose(file_h); |
291 | goto xout_xburst_interface; |
292 | } |
293 | file_len = ftell(file_h); |
294 | if (fseek(file_h, 0, SEEK_SET)) { |
295 | fprintf(stderr, "Error seeking to beginning of %s.\n", argv[2]); |
296 | fclose(file_h); |
297 | goto xout_xburst_interface; |
298 | } |
299 | file_data = (char*) malloc(file_len); |
300 | if (!file_data) { |
301 | fprintf(stderr, "Error allocating %d bytes data.\n", file_len); |
302 | fclose(file_h); |
303 | goto xout_xburst_interface; |
304 | } |
305 | num_read = fread(file_data, 1, file_len, file_h); |
306 | fclose(file_h); |
307 | if (num_read != (size_t) file_len) { |
308 | fprintf(stderr, "Error reading %d bytes (got %d).\n", file_len, num_read); |
309 | free(file_data); |
310 | goto xout_xburst_interface; |
311 | } |
312 | usb_status = usb_bulk_write(xburst_h, |
313 | /* endpoint */ INGENIC_OUT_ENDPOINT, |
314 | /* bytes */ file_data, |
315 | /* size */ file_len, |
316 | /* timeout */ USB_TIMEOUT); |
317 | free(file_data); |
318 | if (usb_status < file_len) { |
319 | fprintf(stderr, "Error writing %d bytes (result %i).\n", file_len, usb_status); |
320 | goto xout_xburst_interface; |
321 | } |
322 | printf("bulk_write successfully wrote %i bytes.\n", usb_status); |
323 | break; |
324 | } |
325 | } |
326 | usb_release_interface(xburst_h, xburst_interface); |
327 | usb_close(xburst_h); |
328 | return EXIT_SUCCESS; |
329 | |
330 | xout_xburst_interface: |
331 | usb_release_interface(xburst_h, xburst_interface); |
332 | xout_xburst_h: |
333 | usb_close(xburst_h); |
334 | xout: |
335 | return EXIT_FAILURE; |
336 | } |
337 |