1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-04-27 15:12:36 +02:00

Convert printf to msg_* where appropriate

Clean up cli_output.c to be more readable.
Use enum instead of #define for message levels.
Kill a few exit(0) calls.
Print the command line arguments in verbose mode.
Move actions (--list-supported etc.) after argument sanity checks.
Reduce the number of code paths which have their own
programmer_shutdown().

Corresponding to flashrom svn r1536.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Acked-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
This commit is contained in:
Carl-Daniel Hailfinger 2012-05-14 22:54:58 +00:00
parent 2cef9164ef
commit 901a3ba023
4 changed files with 98 additions and 88 deletions

View File

@ -159,6 +159,18 @@ static void cli_classic_abort_usage(void)
exit(1); exit(1);
} }
static int check_filename(char *filename, char *type)
{
if (!filename || (filename[0] == '\0')) {
fprintf(stderr, "Error: No %s file specified.\n", type);
return 1;
}
/* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
if (filename[0] == '-')
fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
return 0;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unsigned long size; unsigned long size;
@ -377,36 +389,51 @@ int main(int argc, char *argv[])
cli_classic_abort_usage(); cli_classic_abort_usage();
} }
/* FIXME: Print the actions flashrom will take. */ if ((read_it | write_it | verify_it) && check_filename(filename, "image")) {
cli_classic_abort_usage();
if (list_supported) { }
print_supported(); if (layoutfile && check_filename(layoutfile, "layout")) {
exit(0); cli_classic_abort_usage();
} }
#if CONFIG_PRINT_WIKI == 1 #if CONFIG_PRINT_WIKI == 1
if (list_supported_wiki) { if (list_supported_wiki) {
print_supported_wiki(); print_supported_wiki();
exit(0); ret = 0;
goto out;
} }
#endif #endif
if (layoutfile && read_romlayout(layoutfile)) if (list_supported) {
cli_classic_abort_usage(); print_supported();
if (process_include_args()) ret = 0;
cli_classic_abort_usage(); goto out;
}
msg_gdbg("Command line (%i args):", argc - 1);
for (i = 0; i < argc; i++) {
msg_gdbg(" %s", argv[i]);
}
msg_gdbg("\n");
if (layoutfile && read_romlayout(layoutfile)) {
ret = 1;
goto out;
}
if (process_include_args()) {
ret = 1;
goto out;
}
/* Does a chip with the requested name exist in the flashchips array? */ /* Does a chip with the requested name exist in the flashchips array? */
if (chip_to_probe) { if (chip_to_probe) {
for (flash = flashchips; flash && flash->name; flash++) for (flash = flashchips; flash && flash->name; flash++)
if (!strcmp(flash->name, chip_to_probe)) if (!strcmp(flash->name, chip_to_probe))
break; break;
if (!flash || !flash->name) { if (!flash || !flash->name) {
fprintf(stderr, "Error: Unknown chip '%s' specified.\n", msg_cerr("Error: Unknown chip '%s' specified.\n", chip_to_probe);
chip_to_probe); msg_gerr("Run flashrom -L to view the hardware supported in this flashrom version.\n");
printf("Run flashrom -L to view the hardware supported " ret = 1;
"in this flashrom version.\n"); goto out;
exit(1);
} }
/* Clean up after the check. */ /* Clean up after the check. */
flash = NULL; flash = NULL;
@ -419,7 +446,7 @@ int main(int argc, char *argv[])
myusec_calibrate_delay(); myusec_calibrate_delay();
if (programmer_init(prog, pparam)) { if (programmer_init(prog, pparam)) {
fprintf(stderr, "Error: Programmer initialization failed.\n"); msg_perr("Error: Programmer initialization failed.\n");
ret = 1; ret = 1;
goto out_shutdown; goto out_shutdown;
} }
@ -442,25 +469,22 @@ int main(int argc, char *argv[])
} }
if (chipcount > 1) { if (chipcount > 1) {
printf("Multiple flash chips were detected: \"%s\"", msg_cinfo("Multiple flash chips were detected: \"%s\"", flashes[0].name);
flashes[0].name);
for (i = 1; i < chipcount; i++) for (i = 1; i < chipcount; i++)
printf(", \"%s\"", flashes[i].name); msg_cinfo(", \"%s\"", flashes[i].name);
printf("\nPlease specify which chip to use with the " msg_cinfo("\nPlease specify which chip to use with the -c <chipname> option.\n");
"-c <chipname> option.\n");
ret = 1; ret = 1;
goto out_shutdown; goto out_shutdown;
} else if (!chipcount) { } else if (!chipcount) {
printf("No EEPROM/flash device found.\n"); msg_cinfo("No EEPROM/flash device found.\n");
if (!force || !chip_to_probe) { if (!force || !chip_to_probe) {
printf("Note: flashrom can never write if the flash " msg_cinfo("Note: flashrom can never write if the flash chip isn't found "
"chip isn't found automatically.\n"); "automatically.\n");
} }
if (force && read_it && chip_to_probe) { if (force && read_it && chip_to_probe) {
struct registered_programmer *pgm; struct registered_programmer *pgm;
int compatible_programmers = 0; int compatible_programmers = 0;
printf("Force read (-f -r -c) requested, pretending " msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
"the chip is there:\n");
/* This loop just counts compatible controllers. */ /* This loop just counts compatible controllers. */
for (j = 0; j < registered_programmer_count; j++) { for (j = 0; j < registered_programmer_count; j++) {
pgm = &registered_programmers[j]; pgm = &registered_programmers[j];
@ -468,9 +492,8 @@ int main(int argc, char *argv[])
compatible_programmers++; compatible_programmers++;
} }
if (compatible_programmers > 1) if (compatible_programmers > 1)
printf("More than one compatible controller " msg_cinfo("More than one compatible controller found for the requested flash "
"found for the requested flash chip, " "chip, using the first one.\n");
"using the first one.\n");
for (j = 0; j < registered_programmer_count; j++) { for (j = 0; j < registered_programmer_count; j++) {
pgm = &registered_programmers[j]; pgm = &registered_programmers[j];
startchip = probe_flash(pgm, 0, &flashes[0], 1); startchip = probe_flash(pgm, 0, &flashes[0], 1);
@ -478,14 +501,13 @@ int main(int argc, char *argv[])
break; break;
} }
if (startchip == -1) { if (startchip == -1) {
printf("Probing for flash chip '%s' failed.\n", msg_cinfo("Probing for flash chip '%s' failed.\n", chip_to_probe);
chip_to_probe);
ret = 1; ret = 1;
goto out_shutdown; goto out_shutdown;
} }
printf("Please note that forced reads most likely " msg_cinfo("Please note that forced reads most likely contain garbage.\n");
"contain garbage.\n"); ret = read_flash_to_file(&flashes[0], filename);
return read_flash_to_file(&flashes[0], filename); goto out_shutdown;
} }
ret = 1; ret = 1;
goto out_shutdown; goto out_shutdown;
@ -503,22 +525,14 @@ int main(int argc, char *argv[])
check_chip_supported(fill_flash); check_chip_supported(fill_flash);
size = fill_flash->total_size * 1024; size = fill_flash->total_size * 1024;
if (check_max_decode(fill_flash->pgm->buses_supported & fill_flash->bustype, size) && if (check_max_decode(fill_flash->pgm->buses_supported & fill_flash->bustype, size) && (!force)) {
(!force)) { msg_cerr("Chip is too big for this programmer (-V gives details). Use --force to override.\n");
fprintf(stderr, "Chip is too big for this programmer "
"(-V gives details). Use --force to override.\n");
ret = 1; ret = 1;
goto out_shutdown; goto out_shutdown;
} }
if (!(read_it | write_it | verify_it | erase_it)) { if (!(read_it | write_it | verify_it | erase_it)) {
printf("No operations were specified.\n"); msg_ginfo("No operations were specified.\n");
goto out_shutdown;
}
if (!filename && !erase_it) {
printf("Error: No filename specified.\n");
ret = 1;
goto out_shutdown; goto out_shutdown;
} }
@ -531,9 +545,12 @@ int main(int argc, char *argv[])
* Give the chip time to settle. * Give the chip time to settle.
*/ */
programmer_delay(100000); programmer_delay(100000);
return doit(fill_flash, force, filename, read_it, write_it, erase_it, verify_it); ret |= doit(fill_flash, force, filename, read_it, write_it, erase_it, verify_it);
/* Note: doit() already calls programmer_shutdown(). */
goto out;
out_shutdown: out_shutdown:
programmer_shutdown(); programmer_shutdown();
out:
return ret; return ret;
} }

View File

@ -22,34 +22,25 @@
#include <stdarg.h> #include <stdarg.h>
#include "flash.h" #include "flash.h"
int print(int type, const char *fmt, ...) /* Please note that level is the verbosity, not the importance of the message. */
int print(enum msglevel level, const char *fmt, ...)
{ {
va_list ap; va_list ap;
int ret; int ret = 0;
FILE *output_type; FILE *output_type = stdout;
switch (type) { if (level == MSG_ERROR)
case MSG_ERROR:
output_type = stderr; output_type = stderr;
break;
case MSG_BARF:
if (verbose < 3)
return 0;
case MSG_DEBUG2:
if (verbose < 2)
return 0;
case MSG_DEBUG:
if (verbose < 1)
return 0;
case MSG_INFO:
default:
output_type = stdout;
break;
}
if (level <= verbose) {
va_start(ap, fmt); va_start(ap, fmt);
ret = vfprintf(output_type, fmt, ap); ret = vfprintf(output_type, fmt, ap);
va_end(ap); va_end(ap);
/* msg_*spew usually happens inside chip accessors in possibly
* time-critical operations. Don't slow them down by flushing.
*/
if (level != MSG_SPEW)
fflush(output_type); fflush(output_type);
}
return ret; return ret;
} }

20
flash.h
View File

@ -268,13 +268,15 @@ int write_buf_to_file(unsigned char *buf, unsigned long size, const char *filena
#define ERROR_FLASHROM_LIMIT -201 #define ERROR_FLASHROM_LIMIT -201
/* cli_output.c */ /* cli_output.c */
enum msglevel {
MSG_ERROR = 0,
MSG_INFO = 1,
MSG_DEBUG = 2,
MSG_DEBUG2 = 3,
MSG_SPEW = 4,
};
/* Let gcc and clang check for correct printf-style format strings. */ /* Let gcc and clang check for correct printf-style format strings. */
int print(int type, const char *fmt, ...) __attribute__((format(printf, 2, 3))); int print(enum msglevel level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
#define MSG_ERROR 0
#define MSG_INFO 1
#define MSG_DEBUG 2
#define MSG_DEBUG2 3
#define MSG_BARF 4
#define msg_gerr(...) print(MSG_ERROR, __VA_ARGS__) /* general errors */ #define msg_gerr(...) print(MSG_ERROR, __VA_ARGS__) /* general errors */
#define msg_perr(...) print(MSG_ERROR, __VA_ARGS__) /* programmer errors */ #define msg_perr(...) print(MSG_ERROR, __VA_ARGS__) /* programmer errors */
#define msg_cerr(...) print(MSG_ERROR, __VA_ARGS__) /* chip errors */ #define msg_cerr(...) print(MSG_ERROR, __VA_ARGS__) /* chip errors */
@ -287,9 +289,9 @@ int print(int type, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
#define msg_gdbg2(...) print(MSG_DEBUG2, __VA_ARGS__) /* general debug2 */ #define msg_gdbg2(...) print(MSG_DEBUG2, __VA_ARGS__) /* general debug2 */
#define msg_pdbg2(...) print(MSG_DEBUG2, __VA_ARGS__) /* programmer debug2 */ #define msg_pdbg2(...) print(MSG_DEBUG2, __VA_ARGS__) /* programmer debug2 */
#define msg_cdbg2(...) print(MSG_DEBUG2, __VA_ARGS__) /* chip debug2 */ #define msg_cdbg2(...) print(MSG_DEBUG2, __VA_ARGS__) /* chip debug2 */
#define msg_gspew(...) print(MSG_BARF, __VA_ARGS__) /* general debug barf */ #define msg_gspew(...) print(MSG_SPEW, __VA_ARGS__) /* general debug spew */
#define msg_pspew(...) print(MSG_BARF, __VA_ARGS__) /* programmer debug barf */ #define msg_pspew(...) print(MSG_SPEW, __VA_ARGS__) /* programmer debug spew */
#define msg_cspew(...) print(MSG_BARF, __VA_ARGS__) /* chip debug barf */ #define msg_cspew(...) print(MSG_SPEW, __VA_ARGS__) /* chip debug spew */
/* layout.c */ /* layout.c */
int register_include_arg(char *name); int register_include_arg(char *name);

View File

@ -40,7 +40,7 @@
const char flashrom_version[] = FLASHROM_VERSION; const char flashrom_version[] = FLASHROM_VERSION;
char *chip_to_probe = NULL; char *chip_to_probe = NULL;
int verbose = 0; int verbose = MSG_INFO;
static enum programmer programmer = PROGRAMMER_INVALID; static enum programmer programmer = PROGRAMMER_INVALID;
@ -1457,27 +1457,27 @@ void list_programmers_linebreak(int startcol, int cols, int paren)
if (firstline) if (firstline)
firstline = 0; firstline = 0;
else else
printf("\n"); msg_ginfo("\n");
for (i = 0; i < startcol; i++) for (i = 0; i < startcol; i++)
printf(" "); msg_ginfo(" ");
remaining = cols - startcol; remaining = cols - startcol;
} else { } else {
printf(" "); msg_ginfo(" ");
remaining--; remaining--;
} }
if (paren && (p == 0)) { if (paren && (p == 0)) {
printf("("); msg_ginfo("(");
remaining--; remaining--;
} }
printf("%s", pname); msg_ginfo("%s", pname);
remaining -= pnamelen; remaining -= pnamelen;
if (p < PROGRAMMER_INVALID - 1) { if (p < PROGRAMMER_INVALID - 1) {
printf(","); msg_ginfo(",");
remaining--; remaining--;
} else { } else {
if (paren) if (paren)
printf(")"); msg_ginfo(")");
printf("\n"); msg_ginfo("\n");
} }
} }
} }