/* * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors. * Copyright (C) 2010 Sergey Gridassov * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include "usbdev.h" #include "debug.h" #include "devmgr.h" #include "ingenic.h" #include "shell.h" static void usage(const char *app) { printf( "Usage: \n" " Enumeration: %1$s -e\n" " Interactive mode: %1$s -i \n" " Batch mode (with script): %1$s -i -b \n" " Batch mode (command list): %1$s -i -c \n\n" "USB loader tool for Ingenic Semiconductor XBurst-based SoC\n\n" " -e Enumerate devices only\n" " -i Open device with index INDEX in interactive or batch mode\n" " -c Run semicolon-separated commands and exit\n" " -d Set output level (0 - no reporting, 4 - max reporting), default = 1 (errors only)\n" " -C Execute configuration script FILE before anything else\n" " -b Execute script in FILE\n\n", app); } static void dev_handler(int idx, uint16_t vid, uint16_t pid, void *data) { printf(" Device %d: %04hX:%04hX\n", idx, vid, pid); } int main(int argc, char *argv[]) { int ch; int idx = -1, enumerate = 0; char *cmd = NULL, *script = NULL, *config = NULL; while((ch = getopt(argc, argv, "b:i:ec:d:C:")) != -1) { switch(ch) { case 'e': enumerate = 1; break; case 'i': idx = atoi(optarg); break; case 'c': cmd = optarg; break; case 'b': script = optarg; break; case 'C': config = optarg; break; case 'd': set_debug_level(atoi(optarg)); break; default: usage(argv[0]); return 1; } } if(!enumerate && idx < 0) { usage(argv[0]); return 1; } if(usbdev_init() == -1) { perror("usbdev_init"); return 1; } atexit(usbdev_fini); if(usbdev_enumerate() == -1) { perror("usbdev_enumerate"); return 1; } if(enumerate) { printf("Ingenic devices list:\n"); enum_devices(dev_handler); return 0; } void *data = get_device(idx); if(data == NULL) { fprintf(stderr, "Device with index %d not found\n", idx); return 1; } void *hndl = usbdev_open(data); if(hndl == NULL) { perror("usbdev_open"); return 1; } int ret = 0; void *ingenic = ingenic_open(hndl); if(ingenic == NULL) { perror("ingenic_open"); ret = 1; goto exit_usb; } if(shell_init(ingenic) == -1) { perror("shell_init"); ret = 1; goto exit_ingenic; } if(config) if(shell_source(config) == -1) { perror("shell_source"); ret = 1; goto exit_shell; } if(cmd != NULL) { if(shell_execute(cmd) == -1) { perror("shell_execute"); ret = 1; } } else if(script != NULL) { if(shell_source(script) == -1) { perror("shell_source"); ret = 1; } } else shell_interactive(); exit_shell: shell_fini(); exit_ingenic: ingenic_close(ingenic); exit_usb: usbdev_close(hndl); return ret; }