flashrom.c: allow - as filename for stdin

Allows - as filename for -w/-v options. It is sometimes useful to
script flashrom and allowing it to work with pipes allows for more
flexibility in this specific use-case.

Signed-off-by: Daniel Campello <campello@chromium.org>
Change-Id: I97889cfdf7ba9a257e182c4ee2b20075cfa58d4d
Original-Reviewed-on: https://review.coreboot.org/c/flashrom/+/52383
Original-Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/71350
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/cli_classic.c b/cli_classic.c
index 73cc417..ea25d19 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -45,8 +45,10 @@
 	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"
+	       " -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"
@@ -98,7 +100,7 @@
 		return 1;
 	}
 	/* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
-	if (filename[0] == '-')
+	if (filename[0] == '-' && filename[1] != '\0')
 		fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
 	return 0;
 }
diff --git a/flashrom.c b/flashrom.c
index b48f860..9b9c2fc 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -1294,7 +1294,11 @@
 	int ret = 0;
 
 	FILE *image;
-	if ((image = fopen(filename, "rb")) == NULL) {
+	if (!strcmp(filename, "-"))
+		image = fdopen(fileno(stdin), "rb");
+	else
+		image = fopen(filename, "rb");
+	if (image == NULL) {
 		msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
 		return 1;
 	}
@@ -1305,7 +1309,7 @@
 		ret = 1;
 		goto out;
 	}
-	if (image_stat.st_size != (intmax_t)size) {
+	if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
 		msg_gerr("Error: Image size (%jd B) doesn't match the flash chip's size (%lu B)!\n",
 			 (intmax_t)image_stat.st_size, size);
 		ret = 1;