Root/xbboot/host-app/host_main.c

1//
2// Authors: Wolfgang Spraul <wolfgang@sharism.cc>
3// Authors: Xiangfu Liu <xiangfu@sharism.cc>
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version
8// 3 of the License, or (at your option) any later version.
9//
10
11#include <stdio.h>
12#include <stdint.h>
13#include <string.h>
14#include <usb.h>
15#include <time.h>
16#include "xburst-tools-config.h"
17
18#define HIWORD(dw) (((dw) >> 16) & 0xFFFF)
19#define LOWORD(dw) ((dw) & 0xFFFF)
20
21#define INGENIC_VENDOR_ID 0x601A
22#define INGENIC_XBURST_JZ4740 0x4740
23#define INGENIC_XBURST_JZ4760 0x4760
24
25// REQ_ values are negative so they can be mixed together with the VR_ types in a signed integer.
26#define REQ_BULK_READ -1
27#define REQ_BULK_WRITE -2
28
29#define VR_GET_CPU_INFO 0x00
30#define VR_SET_DATA_ADDRESS 0x01
31#define VR_SET_DATA_LENGTH 0x02
32#define VR_FLUSH_CACHES 0x03
33#define VR_PROGRAM_START1 0x04
34#define VR_PROGRAM_START2 0x05
35#define VR_NOR_OPS 0x06
36#define VR_NAND_OPS 0x07
37#define VR_SDRAM_OPS 0x08
38#define VR_CONFIGRATION 0x09
39#define VR_GET_NUM 0x0a
40
41#define USB_TIMEOUT 3000
42#define VR_GET_CPU_INFO_LEN 8
43#define INGENIC_IN_ENDPOINT 0x81
44#define INGENIC_OUT_ENDPOINT 0x01
45
46#define STAGE1_FILE_PATH (DATADIR "stage1.bin")
47#define STAGE1_ADDRESS ("0x80002000")
48
49uint8_t xburst_interface = 0;
50uint8_t option_upload = 0;
51uint16_t xburst_cpu = INGENIC_XBURST_JZ4760;
52
53struct usb_dev_handle* open_xburst_device();
54void close_xburst_device(struct usb_dev_handle* xburst_h);
55int send_request(struct usb_dev_handle* xburst_h, char* request, char* str_param);
56void show_help();
57
58int main(int argc, char** argv)
59{
60    struct usb_dev_handle* xburst_h;
61
62    if (argc < 2
63        || !strcmp(argv[1], "-h")
64        || !strcmp(argv[1], "--help")) {
65        show_help();
66        return EXIT_SUCCESS;
67    }
68    if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
69        printf("xbboot version %s\n", PACKAGE_VERSION);
70        return EXIT_SUCCESS;
71    }
72
73    xburst_h = NULL;
74    if (!strcmp(argv[1], "-u") || !strcmp(argv[1], "--upload")) {
75        if (argc != 4) {
76            show_help();
77            goto xquit;
78        }
79
80        option_upload = 1;
81        struct timespec timx,tim1;
82
83        tim1.tv_sec = 1;
84        tim1.tv_nsec = 0;
85        while(1) {
86            nanosleep(&tim1,&timx);
87
88            xburst_h = open_xburst_device();
89            if (xburst_h) {
90                printf("\nInfo - found XBurst boot device.\n");
91                if (send_request(xburst_h, "set_addr", STAGE1_ADDRESS)) {
92                    close_xburst_device(xburst_h);
93                    continue;
94                }
95                if (send_request(xburst_h, "bulk_write", STAGE1_FILE_PATH)) {
96                    close_xburst_device(xburst_h);
97                    continue;
98                }
99                if (send_request(xburst_h, "start1", STAGE1_ADDRESS)) {
100                    close_xburst_device(xburst_h);
101                    continue;
102                }
103                if (send_request(xburst_h, "get_info", "NULL")) {
104                    close_xburst_device(xburst_h);
105                    continue;
106                }
107                if (send_request(xburst_h, "set_addr", argv[2])) {
108                    close_xburst_device(xburst_h);
109                    continue;
110                }
111                if (send_request(xburst_h, "bulk_write", argv[3])) {
112                    close_xburst_device(xburst_h);
113                    continue;
114                }
115                if (send_request(xburst_h, "flush_cache","NULL")) {
116                    close_xburst_device(xburst_h);
117                    continue;
118                }
119                if (send_request(xburst_h, "start2", argv[2])) {
120                    close_xburst_device(xburst_h);
121                    continue;
122                }
123
124                goto xquit;
125            }
126        }
127    }
128
129    xburst_h = open_xburst_device();
130    if (xburst_h)
131        if (send_request(xburst_h, argv[1], (argc == 2 ? NULL : argv[2]))) {
132            close_xburst_device(xburst_h);
133            return EXIT_FAILURE;
134        }
135xquit:
136    close_xburst_device(xburst_h);
137    return EXIT_SUCCESS;
138}
139
140
141void close_xburst_device(struct usb_dev_handle* xburst_h)
142{
143        if (xburst_h && xburst_interface)
144        usb_release_interface(xburst_h, xburst_interface);
145
146        if (xburst_h)
147        usb_close(xburst_h);
148}
149
150struct usb_dev_handle* open_xburst_device()
151{
152    struct usb_device* xburst_dev = 0;
153    struct usb_dev_handle* xburst_h = 0;
154
155    usb_init();
156    /* usb_set_debug(255); */
157    usb_find_busses();
158    usb_find_devices();
159
160    // look for Ingenic XBurst USB boot device & interface
161    {
162        {
163            struct usb_bus* usb_bus;
164            struct usb_device* usb_dev;
165
166            for (usb_bus = usb_get_busses(); usb_bus != 0; usb_bus = usb_bus->next) {
167                for (usb_dev = usb_bus->devices; usb_dev != 0; usb_dev = usb_dev->next) {
168                    if (usb_dev->descriptor.idVendor == INGENIC_VENDOR_ID) {
169                        if (usb_dev->descriptor.idProduct == INGENIC_XBURST_JZ4740 ||
170                            usb_dev->descriptor.idProduct == INGENIC_XBURST_JZ4760) {
171                            if (xburst_dev) {
172                                fprintf(stderr, "Error - more than one XBurst boot device found.\n");
173                                goto xout;
174                            }
175                            xburst_dev = usb_dev;
176                            xburst_cpu = usb_dev->descriptor.idProduct;
177                            // keep searching to make sure there is only 1 XBurst device
178                        }
179                    }
180                }
181            }
182            if (!xburst_dev) {
183                fprintf(stderr, "Info - no XBurst boot device found.\n");
184                goto xout;
185            }
186        }
187        {
188            struct usb_config_descriptor* usb_config_desc;
189            struct usb_interface_descriptor* usb_if_desc;
190            struct usb_interface* usb_if;
191            int cfg_index, if_index, alt_index;
192
193            for (cfg_index = 0; cfg_index < xburst_dev->descriptor.bNumConfigurations; cfg_index++) {
194                usb_config_desc = &xburst_dev->config[cfg_index];
195                if (!usb_config_desc) {
196                    fprintf(stderr, "Error - usb_config_desc NULL\n");
197                    goto xout;
198                }
199                for (if_index = 0; if_index < usb_config_desc->bNumInterfaces; if_index++) {
200                    usb_if = &usb_config_desc->interface[if_index];
201                    if (!usb_if) {
202                        fprintf(stderr, "Error - usb_if NULL\n");
203                        goto xout;
204                    }
205                    for (alt_index = 0; alt_index < usb_if->num_altsetting; alt_index++) {
206                        usb_if_desc = &usb_if->altsetting[alt_index];
207                        if (!usb_if_desc) {
208                            fprintf(stderr, "Error - usb_if_desc NULL\n");
209                            goto xout;
210                        }
211                        if (usb_if_desc->bInterfaceClass == 0xFF
212                            && usb_if_desc->bInterfaceSubClass == 0) {
213                            xburst_interface = usb_if_desc->bInterfaceNumber;
214                            goto interface_found;
215                        }
216                    }
217                }
218            }
219        interface_found: ;
220        }
221    }
222    xburst_h = usb_open(xburst_dev);
223    if (!xburst_h) {
224        fprintf(stderr, "Error - can't open XBurst device: %s\n", usb_strerror());
225        goto xout;
226    }
227    if (usb_claim_interface(xburst_h, xburst_interface) < 0) {
228        fprintf(stderr, "Error - can't claim XBurst interface: %s\n", usb_strerror());
229        goto xout_xburst_h;
230    }
231
232    return xburst_h;
233
234xout_xburst_h:
235    usb_close(xburst_h);
236xout:
237    return NULL;
238}
239
240int send_request(struct usb_dev_handle* xburst_h, char* request, char* str_param)
241{
242    int request_type;
243    int usb_status;
244    uint32_t u32_param;
245
246    if (!strcmp(request, "bulk_read"))
247        request_type = REQ_BULK_READ;
248    else if (!strcmp(request, "bulk_write"))
249        request_type = REQ_BULK_WRITE;
250    else if (!strcmp(request, "VR_GET_CPU_INFO") || !strcmp(request, "get_info"))
251        request_type = VR_GET_CPU_INFO;
252    else if (!strcmp(request, "VR_SET_DATA_ADDRESS") || !strcmp(request, "set_addr"))
253        request_type = VR_SET_DATA_ADDRESS;
254    else if (!strcmp(request, "VR_SET_DATA_LENGTH") || !strcmp(request, "set_len"))
255        request_type = VR_SET_DATA_LENGTH;
256    else if (!strcmp(request, "VR_FLUSH_CACHES") || !strcmp(request, "flush_cache"))
257        request_type = VR_FLUSH_CACHES;
258    else if (!strcmp(request, "VR_PROGRAM_START1") || !strcmp(request, "start1"))
259        request_type = VR_PROGRAM_START1;
260    else if (!strcmp(request, "VR_PROGRAM_START2") || !strcmp(request, "start2"))
261        request_type = VR_PROGRAM_START2;
262    else {
263        fprintf(stderr, "Error - unknown vendor request %s - run with --help to see all requests\n", request);
264        return 1;
265    }
266
267    switch (request_type) {
268        case VR_GET_CPU_INFO: {
269            char cpu_info_buf[VR_GET_CPU_INFO_LEN+1] = {0};
270            usb_status = usb_control_msg(xburst_h,
271                /* requesttype */ USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
272                /* request */ VR_GET_CPU_INFO,
273                /* value */ 0,
274                /* index */ 0,
275                /* bytes */ cpu_info_buf,
276                /* size */ VR_GET_CPU_INFO_LEN,
277                /* timeout */ USB_TIMEOUT);
278
279            if (usb_status != VR_GET_CPU_INFO_LEN) {
280                fprintf(stderr, "Error - %s() returned %i\n", request, usb_status);
281                goto xout_xburst_interface;
282            }
283            printf("VR_GET_CPU_INFO %s\n", cpu_info_buf);
284            break;
285        }
286        case VR_SET_DATA_ADDRESS:
287        case VR_SET_DATA_LENGTH:
288        case VR_PROGRAM_START1:
289        case VR_PROGRAM_START2: {
290            if (str_param == NULL) {
291                fprintf(stderr, "Error - number of parameters %s\n", request);
292                goto xout_xburst_interface;
293            }
294
295            u32_param = strtoul(str_param, 0, 0);
296            usb_status = usb_control_msg(xburst_h,
297                /* requesttype */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
298                /* request */ request_type,
299                /* value */ HIWORD(u32_param),
300                /* index */ LOWORD(u32_param),
301                /* bytes */ 0,
302                /* size */ 0,
303                /* timeout */ USB_TIMEOUT);
304            if (usb_status) {
305                fprintf(stderr, "Error - %s() returned %i\n", request, usb_status);
306                goto xout_xburst_interface;
307            }
308            printf("%s %lxh\n", request, (unsigned long) u32_param);
309            break;
310        }
311        case VR_FLUSH_CACHES: {
312            usb_status = usb_control_msg(xburst_h,
313                /* requesttype */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
314                /* request */ VR_FLUSH_CACHES,
315                /* value */ 0,
316                /* index */ 0,
317                /* bytes */ 0,
318                /* size */ 0,
319                /* timeout */ USB_TIMEOUT);
320            if (usb_status) {
321                fprintf(stderr, "Error - %s() returned %i\n", request, usb_status);
322                goto xout_xburst_interface;
323            }
324            printf("VR_FLUSH_CACHES\n");
325            break;
326        }
327        case REQ_BULK_READ: {
328            int read_len;
329            char* read_buf;
330
331            if (str_param == NULL) {
332                fprintf(stderr, "Error - number of parameters %s\n", request);
333                goto xout_xburst_interface;
334            }
335
336            if (str_param[0] == '0' && str_param[1] == 'x')
337                read_len = strtol(&str_param[2], 0, 16);
338            else
339                read_len = strtol(str_param, 0, 10);
340
341            read_buf = (char*) malloc(read_len);
342            if (!read_buf) {
343                fprintf(stderr, "Error - cannot allocate %i bytes read buffer.\n", read_len);
344                goto xout_xburst_interface;
345            }
346            usb_status = usb_bulk_read(xburst_h,
347                /* endpoint */ INGENIC_IN_ENDPOINT,
348                /* bytes */ read_buf,
349                /* size */ read_len,
350                /* timeout */ USB_TIMEOUT);
351            if (usb_status > 0)
352                fwrite(read_buf, 1 /* size */, usb_status, stdout);
353            free(read_buf);
354            if (usb_status < read_len) {
355                fprintf(stderr, "Error reading %d bytes (result %i).\n", read_len, usb_status);
356                goto xout_xburst_interface;
357            }
358            break;
359        }
360        case REQ_BULK_WRITE: {
361            char* file_data;
362            int file_len;
363            FILE* file_h;
364            size_t num_read;
365
366            if (str_param == NULL) {
367                fprintf(stderr, "Error - number of parameters %s\n", request);
368                goto xout_xburst_interface;
369            }
370            file_h = fopen(str_param, "rb");
371            if (!file_h) {
372                fprintf(stderr, "Error opening %s.\n", str_param);
373                goto xout_xburst_interface;
374            }
375            if (fseek(file_h, 0, SEEK_END)) {
376                fprintf(stderr, "Error seeking to end of %s.\n", str_param);
377                fclose(file_h);
378                goto xout_xburst_interface;
379            }
380            file_len = ftell(file_h);
381            if (fseek(file_h, 0, SEEK_SET)) {
382                fprintf(stderr, "Error seeking to beginning of %s.\n", str_param);
383                fclose(file_h);
384                goto xout_xburst_interface;
385            }
386            file_data = (char*) malloc(file_len);
387            if (!file_data) {
388                fprintf(stderr, "Error allocating %d bytes data.\n", file_len);
389                fclose(file_h);
390                goto xout_xburst_interface;
391            }
392            num_read = fread(file_data, 1, file_len, file_h);
393            fclose(file_h);
394            if (num_read != (size_t) file_len) {
395                fprintf(stderr, "Error reading %d bytes (got %d).\n", file_len, num_read);
396                free(file_data);
397                goto xout_xburst_interface;
398            }
399            {
400                if(option_upload) {
401                    memcpy(file_data + 8, &xburst_cpu, sizeof(unsigned int));
402                    option_upload = 0;
403                }
404            }
405            usb_status = usb_bulk_write(xburst_h,
406                /* endpoint */ INGENIC_OUT_ENDPOINT,
407                /* bytes */ file_data,
408                /* size */ file_len,
409                /* timeout */ USB_TIMEOUT);
410            free(file_data);
411            if (usb_status < file_len) {
412                fprintf(stderr, "Error writing %d bytes (result %i).\n", file_len, usb_status);
413                goto xout_xburst_interface;
414            }
415            printf("bulk_write successfully wrote %i bytes.\n", usb_status);
416            break;
417        }
418    }
419
420    usleep(100);
421    return 0;
422
423xout_xburst_interface:
424    return 1;
425}
426
427void show_help()
428{
429    printf("\n"
430           "xbboot version %s - Ingenic XBurst USB Boot Vendor Requests\n"
431           "(c) 2009 Wolfgang Spraul\n"
432           "Report bugs to <wolfgang@sharism.cc>, <xiangfu@sharism.cc>.\n"
433           "\n"
434           "xbboot [vendor_request] ...\n"
435           " -h --help print this help message\n"
436           " -v --version print the version number\n"
437           " [-u | --upload] <address> <path> upload file at <path> to <address> then jump to <address>\n"
438           "\n"
439           " bulk_read <len> read len bulk bytes from USB, write to stdout\n"
440           " bulk_write <path> write file at <path> to USB\n"
441           " [get_info | VR_GET_CPU_INFO] read 8-byte CPU info and write to stdout\n"
442           " [set_addr | VR_SET_DATA_ADDRESS] <addr> send memory address\n"
443           " [set_len | VR_SET_DATA_LENGTH] <len> send data length\n"
444           " [flush_cache | VR_FLUSH_CACHES] flush I-Cache and D-Cache\n"
445           " [start1 | VR_PROGRAM_START1] <addr> transfer data from D-Cache to I-Cache and branch to I-Cache\n"
446           " [start2 | VR_PROGRAM_START2] <addr> branch to <addr> directly\n"
447           "\n"
448           "- all numbers can be prefixed 0x for hex otherwise decimal\n"
449           "\n", PACKAGE_VERSION);
450}
451

Archive Download this file



interactive