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] ...(must run as root)\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    int command = 0;
71    char *cptr;
72    char com_buf[256] = {0};
73    char *cmdpt;
74    char *cfgpath = CONFIG_FILE_PATH;
75    stage1 = STAGE1_FILE_PATH;
76    stage2 = STAGE2_FILE_PATH;
77
78    printf("usbboot - Ingenic XBurst USB Boot Utility\n"
79           "(c) 2009 Ingenic Semiconductor Inc., Qi Hardware Inc., Xiangfu Liu, Marek Lindner\n"
80           "This program is Free Software and comes with ABSOLUTELY NO WARRANTY.\n\n");
81
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 ((getuid()) || (getgid())) {
117        fprintf(stderr, "Error - you must be root to run '%s'\n", argv[0]);
118        return EXIT_FAILURE;
119    }
120
121    if (usb_ingenic_init(&ingenic_dev) < 1)
122         return EXIT_FAILURE;
123
124    if (parse_configure(&hand, cfgpath) < 1)
125        return EXIT_FAILURE;
126
127    if (command) { /* direct run command */
128        char *p[10];
129        int i, loop = 0;
130        p[loop++] = strtok(cmdpt, ";");
131        while(p[loop++] = strtok(NULL, ";"));
132
133        for(i = 0; i < loop - 1 && i < 10; i++) {
134            printf(" Execute command: %s \n",p[i]);
135            command_handle(p[i]);
136        }
137        goto out;
138    }
139
140    while (1) {
141        printf("usbboot :> ");
142        cptr = fgets(com_buf, 256, stdin);
143        if (cptr == NULL)
144            continue;
145
146        if (command_handle(com_buf) == -1 )
147            break;
148    }
149
150out:
151    usb_ingenic_cleanup(&ingenic_dev);
152    return EXIT_SUCCESS;
153}
154

Archive Download this file



interactive