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

Archive Download this file



interactive