| 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-config.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 | |
| 33 | extern struct ingenic_dev ingenic_dev; |
| 34 | extern struct hand hand; |
| 35 | extern char * stage1; |
| 36 | extern char * stage2; |
| 37 | |
| 38 | static 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\t\tstage1 file path\n" |
| 47 | " -2 --stage2\t\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 | |
| 53 | static void print_version(void) |
| 54 | { |
| 55 | printf("usbboot %s\n", PACKAGE_VERSION); |
| 56 | } |
| 57 | |
| 58 | static 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 | |
| 68 | int 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 | while(1) { |
| 80 | int c, option_index = 0; |
| 81 | c = getopt_long(argc, argv, "hvc:f:1:2:", opts, |
| 82 | &option_index); |
| 83 | if (c == -1) |
| 84 | break; |
| 85 | |
| 86 | switch (c) { |
| 87 | case 'h': |
| 88 | help(); |
| 89 | exit(EXIT_SUCCESS); |
| 90 | case 'v': |
| 91 | print_version(); |
| 92 | exit(EXIT_SUCCESS); |
| 93 | case 'c': |
| 94 | command = 1; |
| 95 | cmdpt = optarg; |
| 96 | break; |
| 97 | case 'f': |
| 98 | cfgpath = optarg; |
| 99 | break; |
| 100 | case '1': |
| 101 | stage1 = optarg; |
| 102 | break; |
| 103 | case '2': |
| 104 | stage2 = optarg; |
| 105 | break; |
| 106 | default: |
| 107 | help(); |
| 108 | exit(2); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | printf("\nusbboot version %s - Ingenic XBurst USB Boot Utility\n" |
| 113 | "(c) 2009 Ingenic Semiconductor Inc., Qi Hardware Inc., Xiangfu Liu, Marek Lindner\n" |
| 114 | "This program is Free Software and comes with ABSOLUTELY NO WARRANTY.\n\n", PACKAGE_VERSION); |
| 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, ";")) != 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 | |
| 149 | out: |
| 150 | usb_ingenic_cleanup(&ingenic_dev); |
| 151 | |
| 152 | return EXIT_SUCCESS; |
| 153 | } |
| 154 | |