Daniel Campello has uploaded this change for review.

View Change

flashrom.c: allow - as filename for stdin/stdout

Allows - as filename for -r/-w/-v options

Signed-off-by: Daniel Campello <campello@chromium.org>
Change-Id: I97889cfdf7ba9a257e182c4ee2b20075cfa58d4d
---
M cli_classic.c
M flashrom.c
2 files changed, 21 insertions(+), 8 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/83/52383/1
diff --git a/cli_classic.c b/cli_classic.c
index 203bda9..caa06bb 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -46,9 +46,12 @@

printf(" -h | --help print this help text\n"
" -R | --version print version (release)\n"
- " -r | --read [<file>] read flash and save to <file>\n"
- " -w | --write [<file>] write <file> to flash\n"
- " -v | --verify [<file>] verify flash against <file>\n"
+ " -r | --read [<file|->] read flash and save to <file>\n"
+ " or write on the standard output\n"
+ " -w | --write [<file|->] write <file> or the content provided\n"
+ " on the standard input to flash\n"
+ " -v | --verify [<file|->] verify flash against <file>\n"
+ " or the content provided on the standard input\n"
" -E | --erase erase flash memory\n"
" -V | --verbose more verbose output\n"
" -c | --chip <chipname> probe only for specified flash chip\n"
@@ -145,7 +148,8 @@
if (optarg != NULL)
filename = strdup(optarg);
/* filename is on optind if it is not another flag (i.e. -r filename) */
- else if (optarg == NULL && argv[optind] != NULL && argv[optind][0] != '-')
+ else if (optarg == NULL && argv[optind] != NULL &&
+ (argv[optind][0] != '-' || argv[optind][1] == '\0'))
filename = strdup(argv[optind++]);

return filename;
@@ -458,7 +462,7 @@

if (optind < argc)
cli_classic_abort_usage("Error: Extra parameter found.\n");
- if ((read_it | write_it | verify_it) && (!filename || check_filename(filename, "image")))
+ if ((read_it | write_it | verify_it) && (!filename || filename[0] == '\0'))
cli_classic_abort_usage(NULL);
if (layoutfile && check_filename(layoutfile, "layout"))
cli_classic_abort_usage(NULL);
diff --git a/flashrom.c b/flashrom.c
index 5b52491..b8061a5 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -1348,7 +1348,11 @@
int ret = 0;

FILE *image;
- if ((image = fopen(filename, "rb")) == NULL) {
+ if (!strncmp(filename, "-", sizeof("-")))
+ image = fdopen(STDIN_FILENO, "rb");
+ else
+ image = fopen(filename, "rb");
+ if (image == NULL) {
msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
return 1;
}
@@ -1359,7 +1363,8 @@
ret = 1;
goto out;
}
- if (image_stat.st_size != (intmax_t)size) {
+ if ((image_stat.st_size != (__off_t)size) &&
+ (strncmp(filename, "-", sizeof("-")))) {
msg_gerr("Error: Image size (%jd B) doesn't match the expected size (%lu B)!\n",
(intmax_t)image_stat.st_size, size);
ret = 1;
@@ -1436,7 +1441,11 @@
msg_gerr("No filename specified.\n");
return 1;
}
- if ((image = fopen(filename, "wb")) == NULL) {
+ if (!strncmp(filename, "-", sizeof("-")))
+ image = fdopen(STDOUT_FILENO, "wb");
+ else
+ image = fopen(filename, "wb");
+ if (image == NULL) {
msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
return 1;
}

To view, visit change 52383. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I97889cfdf7ba9a257e182c4ee2b20075cfa58d4d
Gerrit-Change-Number: 52383
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Campello <campello@chromium.org>
Gerrit-MessageType: newchange