Root/usbboot/src/main.c

1/*
2 * Copyright(C) 2009 Qi Hardware Inc.,
3 * Authors: Xiangfu Liu <xiangfu@sharism.cc>
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 <stdio.h>
20#include <stdlib.h>
21#include <getopt.h>
22#include <unistd.h>
23#include <string.h>
24#include "xburst-tools_version.h"
25#include "command_line.h"
26#include "ingenic_usb.h"
27#include "ingenic_cfg.h"
28
29#define CONFIG_FILE_PATH (CFGDIR "usbboot.cfg")
30#define STAGE1_FILE_PATH (DATADIR "xburst_stage1.bin")
31#define STAGE2_FILE_PATH (DATADIR "xburst_stage2.bin")
32
33extern struct ingenic_dev ingenic_dev;
34extern struct hand hand;
35extern char * stage1;
36extern char * stage2;
37
38static void help(void)
39{
40    printf("Usage: usbboot [options] ...\n"
41           " -h --help\t\t\tPrint this help message\n"
42           " -v --version\t\t\tPrint the version number\n"
43           " -c --command\t\t\tDirect run the commands, split by ';'\n"
44           " \t\t\tNOTICE: the max commands count is 10!\n"
45           " -f --configure\t\tconfigure file path\n"
46           " -1 --stage1\t\tstage1 file path\n"
47           " -2 --stage2\t\tstage2 file path\n"
48           " <run without options to enter commands via usbboot prompt>\n\n"
49           "Report bugs to <xiangfu@sharism.cc>.\n"
50        );
51}
52
53static void print_version(void)
54{
55    printf("usbboot version: %s\n", XBURST_TOOLS_VERSION);
56}
57
58static struct option opts[] = {
59    { "help", 0, 0, 'h' },
60    { "version", 0, 0, 'v' },
61    { "command", 1, 0, 'c' },
62    { "configure", 1, 0, 'f' },
63    { "stage1", 1, 0, '1' },
64    { "stage2", 1, 0, '2' },
65    { 0, 0, 0, 0 }
66};
67
68int main(int argc, char **argv)
69{
70    char *cptr;
71    char *cmdpt;
72    int command = 0;
73    char cmd_buf[512] = {0};
74    char *cfgpath = CONFIG_FILE_PATH;
75
76    stage1 = STAGE1_FILE_PATH;
77    stage2 = STAGE2_FILE_PATH;
78
79    printf("usbboot - Ingenic XBurst USB Boot Utility\n"
80           "(c) 2009 Ingenic Semiconductor Inc., Qi Hardware Inc., Xiangfu Liu, Marek Lindner\n"
81           "This program is Free Software and comes with ABSOLUTELY NO WARRANTY.\n\n");
82
83    while(1) {
84        int c, option_index = 0;
85        c = getopt_long(argc, argv, "hvc:f:1:2:", opts,
86                &option_index);
87        if (c == -1)
88            break;
89
90        switch (c) {
91        case 'h':
92            help();
93            exit(EXIT_SUCCESS);
94        case 'v':
95            print_version();
96            exit(EXIT_SUCCESS);
97        case 'c':
98            command = 1;
99            cmdpt = optarg;
100            break;
101        case 'f':
102            cfgpath = optarg;
103            break;
104        case '1':
105            stage1 = optarg;
106            break;
107        case '2':
108            stage2 = optarg;
109            break;
110        default:
111            help();
112            exit(2);
113        }
114    }
115
116    if (usb_ingenic_init(&ingenic_dev) < 1)
117         return EXIT_FAILURE;
118
119    if (parse_configure(&hand, cfgpath) < 1)
120        return EXIT_FAILURE;
121
122#define MAX_COMMANDS 10
123    if (command) { /* direct run command */
124        char *sub_cmd[MAX_COMMANDS];
125        int i, loop = 0;
126
127        sub_cmd[loop++] = strtok(cmdpt, ";");
128        while (sub_cmd[loop++] = strtok(NULL, ";"))
129            if (loop >= MAX_COMMANDS) {
130                printf(" -c only support 10 commands\n");
131                break;
132            }
133
134        for (i = 0; i < loop - 1; i++) {
135            printf(" Execute command: %s \n", sub_cmd[i]);
136            command_handle(sub_cmd[i]);
137        }
138        goto out;
139    }
140
141    while (1) {
142        printf("usbboot# ");
143        cptr = fgets(cmd_buf, sizeof(cmd_buf), stdin);
144        if (cptr != NULL)
145            if (command_handle(cmd_buf))
146                break;
147    }
148
149out:
150    usb_ingenic_cleanup(&ingenic_dev);
151
152    return EXIT_SUCCESS;
153}
154

Archive Download this file



interactive