aboutsummaryrefslogtreecommitdiffstats
path: root/flashrom.c
diff options
context:
space:
mode:
authorDaniel Campello <campello@chromium.org>2021-04-13 10:47:25 -0600
committerEdward O'Callaghan <quasisec@chromium.org>2021-04-27 23:41:53 +0000
commit45d50a101e8073191e6d88143990ed91d3bfe815 (patch)
treeae1493908fd9a015e09283c8af9609917a59288d /flashrom.c
parent3f19ba95f1ea2fbd83f96fe9aa3ee78651e33899 (diff)
downloadflashrom-45d50a101e8073191e6d88143990ed91d3bfe815.tar.gz
flashrom-45d50a101e8073191e6d88143990ed91d3bfe815.tar.bz2
flashrom-45d50a101e8073191e6d88143990ed91d3bfe815.zip
layout: Add -i <region>[:<file>] support
Add an optional sub-parameter to the -i parameter to allow building the image to be written from multiple files. This will also allow regions to be read from flash and written to separate image files. This is a rebase of a patch that was ported from chromiumos. A lot of things have changed, but the idea is the same. Original patch by Louis Yung-Chieh Lo <yjlou@chromium.org>: Summary: Support -i partition:file feature for both read and write. Commit: 9c7525f Review URL: http://codereview.chromium.org/6611015 Ported version by Stefan Tauner <stefan.tauner@student.tuwien.ac.at> and Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>: Summary: [PATCH 2/6] layout: Add -i <region>[:<file>] support. Review URL: https://mail.coreboot.org/pipermail/flashrom/2013-October/011729.html Change-Id: Ic5465659605d8431d931053967b40290195cfd99 Signed-off-by: David Hendricks <dhendrix@chromium.org> Signed-off-by: Stefan Tauner <stefan.tauner@student.tuwien.ac.at> Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net> Signed-off-by: Arthur Heymans <arthur@aheymans.xyz> Signed-off-by: Nico Huber <nico.huber@secunet.com> Signed-off-by: Edward O'Callaghan <quasisec@google.com> Signed-off-by: Daniel Campello <campello@chromium.org> Co-Authored-by: Edward O'Callaghan <quasisec@google.com> Co-Authored-by: Daniel Campello <campello@chromium.org> Reviewed-on: https://review.coreboot.org/c/flashrom/+/23021 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Sam McNally <sammc@google.com> Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Diffstat (limited to 'flashrom.c')
-rw-r--r--flashrom.c94
1 files changed, 93 insertions, 1 deletions
diff --git a/flashrom.c b/flashrom.c
index 056fc57e..3a733df6 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -1360,7 +1360,7 @@ int read_buf_from_file(unsigned char *buf, unsigned long size,
goto out;
}
if (image_stat.st_size != (intmax_t)size) {
- msg_gerr("Error: Image size (%jd B) doesn't match the flash chip's size (%lu B)!\n",
+ 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;
@@ -1378,6 +1378,51 @@ out:
#endif
}
+/**
+ * @brief Reads content to buffer from one or more files.
+ *
+ * Reads content to supplied buffer from files. If a filename is specified for
+ * individual regions using the partial read syntax ('-i <region>[:<filename>]')
+ * then this will read file data into the corresponding region in the
+ * supplied buffer.
+ *
+ * @param flashctx Flash context to be used.
+ * @param buf Chip-sized buffer to write data to
+ * @return 0 on success
+ */
+static int read_buf_from_include_args(const struct flashctx *const flash,
+ unsigned char *buf)
+{
+ const struct flashrom_layout *const layout = get_layout(flash);
+ const struct romentry *entry = NULL;
+
+ /*
+ * Content will be read from -i args, so they must not overlap since
+ * we need to know exactly what content to write to the ROM.
+ */
+ if (included_regions_overlap(layout)) {
+ msg_gerr("Error: Included regions must not overlap when writing.\n");
+ return 1;
+ }
+
+ while ((entry = layout_next_included(layout, entry))) {
+ if (!entry->file)
+ continue;
+ if (read_buf_from_file(buf + entry->start,
+ entry->end - entry->start + 1, entry->file))
+ return 1;
+ }
+ return 0;
+}
+
+/**
+ * @brief Writes passed data buffer into a file
+ *
+ * @param buf Buffer with data to write
+ * @param size Size of buffer
+ * @param filename File path to write to
+ * @return 0 on success
+ */
int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
{
#ifdef __LIBPAYLOAD__
@@ -1430,6 +1475,35 @@ out:
#endif
}
+/**
+ * @brief Writes content from buffer to one or more files.
+ *
+ * Writes content from supplied buffer to files. If a filename is specified for
+ * individual regions using the partial read syntax ('-i <region>[:<filename>]')
+ * then this will write files using data from the corresponding region in the
+ * supplied buffer.
+ *
+ * @param flashctx Flash context to be used.
+ * @param buf Chip-sized buffer to read data from
+ * @return 0 on success
+ */
+static int write_buf_to_include_args(const struct flashctx *const flash,
+ unsigned char *buf)
+{
+ const struct flashrom_layout *const layout = get_layout(flash);
+ const struct romentry *entry = NULL;
+
+ while ((entry = layout_next_included(layout, entry))) {
+ if (!entry->file)
+ continue;
+ if (write_buf_to_file(buf + entry->start,
+ entry->end - entry->start + 1, entry->file))
+ return 1;
+ }
+
+ return 0;
+}
+
static int read_by_layout(struct flashctx *, uint8_t *);
int read_flash_to_file(struct flashctx *flash, const char *filename)
{
@@ -1453,6 +1527,10 @@ int read_flash_to_file(struct flashctx *flash, const char *filename)
ret = 1;
goto out_free;
}
+ if (write_buf_to_include_args(flash, buf)) {
+ ret = 1;
+ goto out_free;
+ }
ret = write_buf_to_file(buf, size, filename);
out_free:
@@ -2643,8 +2721,15 @@ int do_write(struct flashctx *const flash, const char *const filename, const cha
goto _free_ret;
}
+ /* Read '-w' argument first... */
if (read_buf_from_file(newcontents, flash_size, filename))
goto _free_ret;
+ /*
+ * ... then update newcontents with contents from files provided to '-i'
+ * args if needed.
+ */
+ if (read_buf_from_include_args(flash, newcontents))
+ goto _free_ret;
if (referencefile) {
if (read_buf_from_file(refcontents, flash_size, referencefile))
@@ -2670,8 +2755,15 @@ int do_verify(struct flashctx *const flash, const char *const filename)
goto _free_ret;
}
+ /* Read '-v' argument first... */
if (read_buf_from_file(newcontents, flash_size, filename))
goto _free_ret;
+ /*
+ * ... then update newcontents with contents from files provided to '-i'
+ * args if needed.
+ */
+ if (read_buf_from_include_args(flash, newcontents))
+ goto _free_ret;
ret = flashrom_image_verify(flash, newcontents, flash_size);