From a84835a7eadadb715a61d993da6766bdf1b37c5e Mon Sep 17 00:00:00 2001 From: Carl-Daniel Hailfinger Date: Thu, 7 Jan 2010 03:24:05 +0000 Subject: Move the CLI related functions main() and cli_usage() to cli_classic.c And rename them accordingly. For now, main() just calls cli_classic(), but alternative frontends can be switched in main(). Annotate remaining help texts with CLI dependency inside flashrom.c with a FIXME comment. Now people can go and create different frontends and be happy. Please note that any other frontend will have to sort of duplicate the probing code in cli_classic.c. Refactoring that part of the code is possible, but not easy because we still want to print instructive help messages for users. Corresponding to flashrom svn r833. Signed-off-by: Carl-Daniel Hailfinger Acked-by: Sean Nelson --- flashrom.c | 355 +------------------------------------------------------------ 1 file changed, 3 insertions(+), 352 deletions(-) (limited to 'flashrom.c') diff --git a/flashrom.c b/flashrom.c index added61..8befab3 100644 --- a/flashrom.c +++ b/flashrom.c @@ -692,6 +692,7 @@ int check_max_decode(enum chipbustype buses, uint32_t size) * and the limit is not exceeded on all buses. Tell the user. */ if (bitcount(buses) > limitexceeded) + /* FIXME: This message is designed towards CLI users. */ printf_debug("There is at least one common chip/programmer " "interface which can support a chip of this size. " "You can try --force at your own risk.\n"); @@ -911,73 +912,6 @@ void list_programmers(char *delim) printf("\n"); } -void cli_usage(const char *name) -{ - const char *pname; - int pnamelen; - int remaining = 0; - enum programmer p; - - printf("cli_usage: %s [-VfLzhR] [-E|-r file|-w file|-v file] [-c chipname]\n" - " [-m [vendor:]part] [-l file] [-i image] [-p programmer]\n\n", name); - - printf("Please note that the command line interface for flashrom will " - "change before\nflashrom 1.0. Do not use flashrom in scripts " - "or other automated tools without\nchecking that your flashrom" - " version won't interpret options in a different way.\n\n"); - - printf - (" -r | --read: read flash and save into file\n" - " -w | --write: write file into flash\n" - " -v | --verify: verify flash against file\n" - " -n | --noverify: don't verify flash against file\n" - " -E | --erase: erase flash device\n" - " -V | --verbose: more verbose output\n" - " -c | --chip : probe only for specified flash chip\n" -#if INTERNAL_SUPPORT == 1 - " -m | --mainboard <[vendor:]part>: override mainboard settings\n" -#endif - " -f | --force: force write without checking image\n" - " -l | --layout : read ROM layout from file\n" - " -i | --image : only flash image name from flash layout\n" - " -L | --list-supported: print supported devices\n" -#if PRINT_WIKI_SUPPORT == 1 - " -z | --list-supported-wiki: print supported devices in wiki syntax\n" -#endif - " -p | --programmer : specify the programmer device"); - - for (p = 0; p < PROGRAMMER_INVALID; p++) { - pname = programmer_table[p].name; - pnamelen = strlen(pname); - if (remaining - pnamelen - 2 < 0) { - printf("\n "); - remaining = 43; - } else { - printf(" "); - remaining--; - } - if (p == 0) { - printf("("); - remaining--; - } - printf("%s", pname); - remaining -= pnamelen; - if (p < PROGRAMMER_INVALID - 1) { - printf(","); - remaining--; - } else { - printf(")\n"); - } - } - - printf( - " -h | --help: print this help text\n" - " -R | --version: print the version (release)\n" - "\nYou can specify one of -E, -r, -w, -v or no operation. If no operation is\n" - "specified, then all that happens is that flash info is dumped.\n\n"); - exit(1); -} - void print_version(void) { printf("flashrom v%s\n", flashrom_version); @@ -1034,6 +968,7 @@ void check_chip_supported(struct flashchip *flash) printf(" WRITE"); printf("\n"); } + /* FIXME: This message is designed towards CLI users. */ printf("Please email a report to flashrom@flashrom.org if any " "of the above operations\nwork correctly for you with " "this flash part. Please include the flashrom\noutput " @@ -1046,291 +981,7 @@ void check_chip_supported(struct flashchip *flash) int main(int argc, char *argv[]) { - unsigned long size; - /* Probe for up to three flash chips. */ - struct flashchip *flash, *flashes[3]; - const char *name; - int namelen; - int opt; - int option_index = 0; - int force = 0; - int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0; - int dont_verify_it = 0, list_supported = 0; -#if PRINT_WIKI_SUPPORT == 1 - int list_supported_wiki = 0; -#endif - int operation_specified = 0; - int i; - -#if PRINT_WIKI_SUPPORT == 1 - const char *optstring = "rRwvnVEfc:m:l:i:p:Lzh"; -#else - const char *optstring = "rRwvnVEfc:m:l:i:p:Lh"; -#endif - static struct option long_options[] = { - {"read", 0, 0, 'r'}, - {"write", 0, 0, 'w'}, - {"erase", 0, 0, 'E'}, - {"verify", 0, 0, 'v'}, - {"noverify", 0, 0, 'n'}, - {"chip", 1, 0, 'c'}, - {"mainboard", 1, 0, 'm'}, - {"verbose", 0, 0, 'V'}, - {"force", 0, 0, 'f'}, - {"layout", 1, 0, 'l'}, - {"image", 1, 0, 'i'}, - {"list-supported", 0, 0, 'L'}, -#if PRINT_WIKI_SUPPORT == 1 - {"list-supported-wiki", 0, 0, 'z'}, -#endif - {"programmer", 1, 0, 'p'}, - {"help", 0, 0, 'h'}, - {"version", 0, 0, 'R'}, - {0, 0, 0, 0} - }; - - char *filename = NULL; - - char *tempstr = NULL; - - print_version(); - - if (argc > 1) { - /* Yes, print them. */ - int i; - printf_debug("The arguments are:\n"); - for (i = 1; i < argc; ++i) - printf_debug("%s\n", argv[i]); - } - - if (selfcheck()) - exit(1); - - setbuf(stdout, NULL); - while ((opt = getopt_long(argc, argv, optstring, - long_options, &option_index)) != EOF) { - switch (opt) { - case 'r': - if (++operation_specified > 1) { - fprintf(stderr, "More than one operation " - "specified. Aborting.\n"); - exit(1); - } - read_it = 1; - break; - case 'w': - if (++operation_specified > 1) { - fprintf(stderr, "More than one operation " - "specified. Aborting.\n"); - exit(1); - } - write_it = 1; - break; - case 'v': - //FIXME: gracefully handle superfluous -v - if (++operation_specified > 1) { - fprintf(stderr, "More than one operation " - "specified. Aborting.\n"); - exit(1); - } - if (dont_verify_it) { - fprintf(stderr, "--verify and --noverify are" - "mutually exclusive. Aborting.\n"); - exit(1); - } - verify_it = 1; - break; - case 'n': - if (verify_it) { - fprintf(stderr, "--verify and --noverify are" - "mutually exclusive. Aborting.\n"); - exit(1); - } - dont_verify_it = 1; - break; - case 'c': - chip_to_probe = strdup(optarg); - break; - case 'V': - verbose = 1; - break; - case 'E': - if (++operation_specified > 1) { - fprintf(stderr, "More than one operation " - "specified. Aborting.\n"); - exit(1); - } - erase_it = 1; - break; -#if INTERNAL_SUPPORT == 1 - case 'm': - tempstr = strdup(optarg); - lb_vendor_dev_from_string(tempstr); - break; -#endif - case 'f': - force = 1; - break; - case 'l': - tempstr = strdup(optarg); - if (read_romlayout(tempstr)) - exit(1); - break; - case 'i': - tempstr = strdup(optarg); - find_romentry(tempstr); - break; - case 'L': - list_supported = 1; - break; -#if PRINT_WIKI_SUPPORT == 1 - case 'z': - list_supported_wiki = 1; - break; -#endif - case 'p': - for (programmer = 0; programmer < PROGRAMMER_INVALID; programmer++) { - name = programmer_table[programmer].name; - namelen = strlen(name); - if (strncmp(optarg, name, namelen) == 0) { - switch (optarg[namelen]) { - case ':': - programmer_param = strdup(optarg + namelen + 1); - break; - case '\0': - break; - default: - /* The continue refers to the - * for loop. It is here to be - * able to differentiate between - * foo and foobar. - */ - continue; - } - break; - } - } - if (programmer == PROGRAMMER_INVALID) { - printf("Error: Unknown programmer %s.\n", optarg); - exit(1); - } - break; - case 'R': - /* print_version() is always called during startup. */ - exit(0); - break; - case 'h': - default: - cli_usage(argv[0]); - break; - } - } - - if (list_supported) { - print_supported(); - exit(0); - } - -#if PRINT_WIKI_SUPPORT == 1 - if (list_supported_wiki) { - print_supported_wiki(); - exit(0); - } -#endif - - if (read_it && write_it) { - printf("Error: -r and -w are mutually exclusive.\n"); - cli_usage(argv[0]); - } - - if (optind < argc) - filename = argv[optind++]; - - if (optind < argc) { - printf("Error: Extra parameter found.\n"); - cli_usage(argv[0]); - } - - if (programmer_init()) { - fprintf(stderr, "Error: Programmer initialization failed.\n"); - exit(1); - } - - // FIXME: Delay calibration should happen in programmer code. - myusec_calibrate_delay(); - - for (i = 0; i < ARRAY_SIZE(flashes); i++) { - flashes[i] = - probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0); - if (!flashes[i]) - for (i++; i < ARRAY_SIZE(flashes); i++) - flashes[i] = NULL; - } - - if (flashes[1]) { - printf("Multiple flash chips were detected:"); - for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++) - printf(" %s", flashes[i]->name); - printf("\nPlease specify which chip to use with the -c option.\n"); - programmer_shutdown(); - exit(1); - } else if (!flashes[0]) { - printf("No EEPROM/flash device found.\n"); - if (!force || !chip_to_probe) { - printf("If you know which flash chip you have, and if this version of flashrom\n"); - printf("supports a similar flash chip, you can try to force read your chip. Run:\n"); - printf("flashrom -f -r -c similar_supported_flash_chip filename\n"); - printf("\n"); - printf("Note: flashrom can never write when the flash chip isn't found automatically.\n"); - } - if (force && read_it && chip_to_probe) { - printf("Force read (-f -r -c) requested, forcing chip probe success:\n"); - flashes[0] = probe_flash(flashchips, 1); - if (!flashes[0]) { - printf("flashrom does not support a flash chip named '%s'.\n", chip_to_probe); - printf("Run flashrom -L to view the hardware supported in this flashrom version.\n"); - exit(1); - } - printf("Please note that forced reads most likely contain garbage.\n"); - return read_flash(flashes[0], filename); - } - // FIXME: flash writes stay enabled! - programmer_shutdown(); - exit(1); - } - - flash = flashes[0]; - - check_chip_supported(flash); - - size = flash->total_size * 1024; - if (check_max_decode((buses_supported & flash->bustype), size) && - (!force)) { - fprintf(stderr, "Chip is too big for this programmer " - "(-V gives details). Use --force to override.\n"); - programmer_shutdown(); - return 1; - } - - if (!(read_it | write_it | verify_it | erase_it)) { - printf("No operations were specified.\n"); - // FIXME: flash writes stay enabled! - programmer_shutdown(); - exit(1); - } - - if (!filename && !erase_it) { - printf("Error: No filename specified.\n"); - // FIXME: flash writes stay enabled! - programmer_shutdown(); - exit(1); - } - - /* Always verify write operations unless -n is used. */ - if (write_it && !dont_verify_it) - verify_it = 1; - - return doit(flash, force, filename, read_it, write_it, erase_it, verify_it); + return cli_classic(argc, argv); } /* This function signature is horrible. We need to design a better interface, -- cgit v1.1