| 1 | /* |
| 2 | * Copyright(C) 2009 Qi Hardware Inc., |
| 3 | * Authors: Xiangfu Liu <xiangfu@qi-hardware.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 <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 "/etc/xburst-tools/usbboot.cfg" |
| 30 | |
| 31 | struct ingenic_dev ingenic_dev; |
| 32 | |
| 33 | static void help(void) |
| 34 | { |
| 35 | printf("Usage: usbboot [options] ...(must run as root)\n" |
| 36 | " -h --help\t\t\tPrint this help message\n" |
| 37 | " -v --version\t\t\tPrint the version number\n" |
| 38 | " -c --command\t\t\tDirect run the commands, split by ';'\n" |
| 39 | " -f --configure\t\t\tconfigure file path\n" |
| 40 | " <run without options to enter commands via usbboot prompt>\n\n" |
| 41 | "Report bugs to <xiangfu@qi-hardware.com>.\n" |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | static void print_version(void) |
| 46 | { |
| 47 | printf("usbboot version: %s\n", XBURST_TOOLS_VERSION); |
| 48 | } |
| 49 | |
| 50 | static struct option opts[] = { |
| 51 | { "help", 0, 0, 'h' }, |
| 52 | { "version", 0, 0, 'v' }, |
| 53 | { "command", 1, 0, 'c' }, |
| 54 | { "configure", 1, 0, 'f' }, |
| 55 | { 0, 0, 0, 0 } |
| 56 | }; |
| 57 | |
| 58 | int main(int argc, char **argv) |
| 59 | { |
| 60 | char *cptr; |
| 61 | char com_buf[256] = {0}; |
| 62 | char *cmdpt = NULL; |
| 63 | char *cfgpath = CONFIG_FILE_PATH; |
| 64 | |
| 65 | printf("usbboot - Ingenic XBurst USB Boot Utility\n" |
| 66 | "(c) 2009 Ingenic Semiconductor Inc., Qi Hardware Inc., Xiangfu Liu, Marek Lindner\n" |
| 67 | "This program is Free Software and comes with ABSOLUTELY NO WARRANTY.\n\n"); |
| 68 | |
| 69 | while(1) { |
| 70 | int c, option_index = 0; |
| 71 | c = getopt_long(argc, argv, "hvc:f:", opts, |
| 72 | &option_index); |
| 73 | if (c == -1) |
| 74 | break; |
| 75 | |
| 76 | switch (c) { |
| 77 | case 'h': |
| 78 | help(); |
| 79 | exit(EXIT_SUCCESS); |
| 80 | case 'v': |
| 81 | print_version(); |
| 82 | exit(EXIT_SUCCESS); |
| 83 | case 'c': |
| 84 | cmdpt = optarg; |
| 85 | break; |
| 86 | case 'f': |
| 87 | cfgpath = optarg; |
| 88 | break; |
| 89 | default: |
| 90 | help(); |
| 91 | exit(2); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* if ((getuid()) || (getgid())) { |
| 96 | fprintf(stderr, "Error - you must be root to run '%s'\n", argv[0]); |
| 97 | return EXIT_FAILURE; |
| 98 | }*/ |
| 99 | |
| 100 | if (usb_ingenic_init(&ingenic_dev)) { |
| 101 | printf("A\n"); |
| 102 | return EXIT_FAILURE; |
| 103 | } |
| 104 | |
| 105 | if (parse_configure(&ingenic_dev.config, cfgpath)) { |
| 106 | printf("B\n"); |
| 107 | return EXIT_FAILURE; |
| 108 | } |
| 109 | |
| 110 | if (cmdpt) { /* direct run command */ |
| 111 | char *delim=";"; |
| 112 | char *p; |
| 113 | p = strtok(cmdpt, delim); |
| 114 | strcpy(com_buf, p); |
| 115 | printf(" Execute command: %s \n",com_buf); |
| 116 | command_handle(com_buf); |
| 117 | |
| 118 | while((p = strtok(NULL,delim))) { |
| 119 | strcpy(com_buf, p); |
| 120 | printf(" Execute command: %s \n",com_buf); |
| 121 | command_handle(com_buf); |
| 122 | } |
| 123 | goto out; |
| 124 | } |
| 125 | |
| 126 | while (1) { |
| 127 | printf("usbboot :> "); |
| 128 | cptr = fgets(com_buf, 256, stdin); |
| 129 | if (cptr == NULL) |
| 130 | continue; |
| 131 | |
| 132 | command_handle(com_buf); |
| 133 | } |
| 134 | |
| 135 | out: |
| 136 | usb_ingenic_cleanup(&ingenic_dev); |
| 137 | return EXIT_SUCCESS; |
| 138 | } |
| 139 | |