Root/jzboot/src/main.c

1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <unistd.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include "usbdev.h"
23#include "debug.h"
24#include "devmgr.h"
25#include "ingenic.h"
26#include "shell.h"
27#include "xburst-tools-config.h"
28
29static void usage(const char *app) {
30    printf(
31        "Usage: \n"
32        " Enumeration: %1$s -e\n"
33                " Interactive mode: %1$s -i <INDEX>\n"
34                " Batch mode (with script): %1$s -i <INDEX> -b <FILE>\n"
35                " Batch mode (command list): %1$s -i <INDEX> -c <COMMAND>\n\n"
36
37        "USB loader tool for Ingenic Semiconductor XBurst-based SoC\n\n"
38        " -e Enumerate devices only\n"
39        " -i <INDEX> Open device with index INDEX in interactive or batch mode\n"
40        " -c <CMD> Run semicolon-separated commands and exit\n"
41                " -d <DEBUG> Set output level (0 - no reporting, 4 - max reporting), default = 1 (errors only)\n"
42                " -C <FILE> Execute configuration script FILE before anything else\n"
43        " -b <FILE> Execute script in FILE\n"
44        " -v Print program version\n\n", app);
45}
46
47static void dev_handler(int idx, uint16_t vid, uint16_t pid, void *data) {
48    printf(" Device %d: %04hX:%04hX\n", idx, vid, pid);
49}
50
51int main(int argc, char *argv[]) {
52    int ch;
53    int idx = -1, enumerate = 0;
54    char *cmd = NULL, *script = NULL, *config = NULL;
55
56    while((ch = getopt(argc, argv, "b:i:ec:d:C:v")) != -1) {
57        switch(ch) {
58        case 'e':
59            enumerate = 1;
60
61            break;
62
63        case 'i':
64            idx = atoi(optarg);
65
66            break;
67
68        case 'c':
69            cmd = optarg;
70
71            break;
72
73        case 'b':
74            script = optarg;
75
76            break;
77
78        case 'C':
79            config = optarg;
80
81            break;
82
83        case 'd':
84            set_debug_level(atoi(optarg));
85
86            break;
87
88        case 'v':
89            printf("JZboot version %s\n", PACKAGE_VERSION);
90
91            return 0;
92
93        default:
94            usage(argv[0]);
95
96            return 1;
97        }
98    }
99
100    if(!enumerate && idx < 0) {
101        usage(argv[0]);
102
103        return 1;
104    }
105
106    if(usbdev_init() == -1) {
107        perror("usbdev_init");
108
109        return 1;
110    }
111
112    atexit(usbdev_fini);
113
114    if(usbdev_enumerate() == -1) {
115        perror("usbdev_enumerate");
116
117        return 1;
118    }
119
120    if(enumerate) {
121        printf("Ingenic devices list:\n");
122
123        enum_devices(dev_handler);
124
125        return 0;
126    }
127
128    void *data = get_device(idx);
129
130    if(data == NULL) {
131        fprintf(stderr, "Device with index %d not found\n", idx);
132
133        return 1;
134    }
135
136    void *hndl = usbdev_open(data);
137
138    if(hndl == NULL) {
139        perror("usbdev_open");
140
141        return 1;
142    }
143
144    int ret = 0;
145
146    void *ingenic = ingenic_open(hndl);
147
148    if(ingenic == NULL) {
149        perror("ingenic_open");
150
151        ret = 1;
152
153        goto exit_usb;
154    }
155
156    shell_context_t *shell = shell_init(ingenic);
157
158    if(shell == NULL) {
159        perror("shell_init");
160
161        ret = 1;
162
163        goto exit_ingenic;
164    }
165
166    if(config) {
167        if(shell_source(shell, config) == -1) {
168            perror("shell_source");
169
170            ret = 1;
171
172            goto exit_shell;
173        }
174    }
175
176    if(cmd != NULL) {
177        if(shell_execute(shell, cmd) == -1) {
178            perror("shell_execute");
179
180            ret = 1;
181        }
182
183    } else if(script != NULL) {
184        if(shell_source(shell, script) == -1) {
185            perror("shell_source");
186
187            ret = 1;
188        }
189    } else
190        shell_interactive(shell);
191
192exit_shell:
193    shell_fini(shell);
194
195exit_ingenic:
196    ingenic_close(ingenic);
197
198exit_usb:
199    usbdev_close(hndl);
200
201    return ret;
202}
203

Archive Download this file



interactive