aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdward O'Callaghan <quasisec@google.com>2022-08-12 11:03:00 +1000
committerEdward O'Callaghan <quasisec@chromium.org>2022-08-25 00:25:12 +0000
commit3c1aa889d867669ac98a204de7b7dbe2778f814a (patch)
tree3958b4fba4171fe72b2da19f3a955bda50e58892
parenteb1b1f6f7dc27bb978a8e4577c83b722f770af1e (diff)
downloadflashrom-3c1aa889d867669ac98a204de7b7dbe2778f814a.tar.gz
flashrom-3c1aa889d867669ac98a204de7b7dbe2778f814a.tar.bz2
flashrom-3c1aa889d867669ac98a204de7b7dbe2778f814a.zip
flashrom.c: Move read_buf_from_file() to helpers_fileio.c
Constructing a buffer from a read file is auxiliary functionality to the core flashrom algorithms. Move aside to decrease the overall complexity of flashrom.c BUG=b:242246291 TEST=builds Change-Id: Ia6e1eeb876722899defb5b75346d1f22c70bfbd1 Signed-off-by: Edward O'Callaghan <quasisec@google.com> Reviewed-on: https://review.coreboot.org/c/flashrom/+/66645 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Evan Benn <evanbenn@google.com> Reviewed-by: Sam McNally <sammc@google.com> Reviewed-by: Nico Huber <nico.h@gmx.de> Reviewed-by: Thomas Heijligen <src@posteo.de>
-rw-r--r--Makefile2
-rw-r--r--flashrom.c44
-rw-r--r--helpers_fileio.c71
-rw-r--r--meson.build1
4 files changed, 73 insertions, 45 deletions
diff --git a/Makefile b/Makefile
index 099379bf..db8c8c9a 100644
--- a/Makefile
+++ b/Makefile
@@ -387,7 +387,7 @@ CHIP_OBJS = jedec.o stm50.o w39.o w29ee011.o \
# Library code.
LIB_OBJS = libflashrom.o layout.o flashrom.o udelay.o programmer.o programmer_table.o \
- helpers.o ich_descriptors.o fmap.o platform/endian_$(ENDIAN).o platform/memaccess.o
+ helpers.o helpers_fileio.o ich_descriptors.o fmap.o platform/endian_$(ENDIAN).o platform/memaccess.o
###############################################################################
diff --git a/flashrom.c b/flashrom.c
index 5a6f4e66..3888bca5 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -874,50 +874,6 @@ notfound:
return chip - flashchips;
}
-int read_buf_from_file(unsigned char *buf, unsigned long size,
- const char *filename)
-{
-#ifdef __LIBPAYLOAD__
- msg_gerr("Error: No file I/O support in libpayload\n");
- return 1;
-#else
- int ret = 0;
-
- FILE *image;
- 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;
- }
-
- struct stat image_stat;
- if (fstat(fileno(image), &image_stat) != 0) {
- msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
- ret = 1;
- goto out;
- }
- if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
- 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;
- goto out;
- }
-
- unsigned long numbytes = fread(buf, 1, size, image);
- if (numbytes != size) {
- msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
- "wanted %ld!\n", numbytes, size);
- ret = 1;
- }
-out:
- (void)fclose(image);
- return ret;
-#endif
-}
-
/**
* @brief Reads content to buffer from one or more files.
*
diff --git a/helpers_fileio.c b/helpers_fileio.c
new file mode 100644
index 00000000..550d98d2
--- /dev/null
+++ b/helpers_fileio.c
@@ -0,0 +1,71 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2009-2010 Carl-Daniel Hailfinger
+ * Copyright (C) 2013 Stefan Tauner
+ *
+ * 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 2 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.
+ */
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#ifndef __LIBPAYLOAD__
+#include <sys/stat.h>
+#endif
+
+#include "flash.h"
+
+int read_buf_from_file(unsigned char *buf, unsigned long size,
+ const char *filename)
+{
+#ifdef __LIBPAYLOAD__
+ msg_gerr("Error: No file I/O support in libpayload\n");
+ return 1;
+#else
+ int ret = 0;
+
+ FILE *image;
+ 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;
+ }
+
+ struct stat image_stat;
+ if (fstat(fileno(image), &image_stat) != 0) {
+ msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
+ ret = 1;
+ goto out;
+ }
+ if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
+ 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;
+ goto out;
+ }
+
+ unsigned long numbytes = fread(buf, 1, size, image);
+ if (numbytes != size) {
+ msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
+ "wanted %ld!\n", numbytes, size);
+ ret = 1;
+ }
+out:
+ (void)fclose(image);
+ return ret;
+#endif
+}
diff --git a/meson.build b/meson.build
index 065578e4..98283aa2 100644
--- a/meson.build
+++ b/meson.build
@@ -91,6 +91,7 @@ srcs = files(
'flashrom.c',
'fmap.c',
'helpers.c',
+ 'helpers_fileio.c',
'ich_descriptors.c',
'jedec.c',
'layout.c',