mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 2/4] libfile: implement new helper write_file_flash()
Date: Fri,  1 Jun 2018 20:07:48 +0200	[thread overview]
Message-ID: <20180601180750.5436-2-u.kleine-koenig@pengutronix.de> (raw)
In-Reply-To: <20180601180750.5436-1-u.kleine-koenig@pengutronix.de>

Compared to write_file() this new function also calls erase() to be
suitable for flash devices.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 include/libfile.h |  1 +
 lib/libfile.c     | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/include/libfile.h b/include/libfile.h
index fd2fadeaa81a..2c5eef71f169 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -12,6 +12,7 @@ int read_file_2(const char *filename, size_t *size, void **outbuf,
 		loff_t max_size);
 
 int write_file(const char *filename, const void *buf, size_t size);
+int write_file_flash(const char *filename, const void *buf, size_t size);
 
 int copy_file(const char *src, const char *dst, int verbose);
 
diff --git a/lib/libfile.c b/lib/libfile.c
index b7db22d6940a..2d4335db7dbd 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -250,6 +250,39 @@ int write_file(const char *filename, const void *buf, size_t size)
 }
 EXPORT_SYMBOL(write_file);
 
+/**
+ * write_file_flash - write a buffer to a file backed by flash
+ * @filename:    The filename to write
+ * @size:        The size of the buffer
+ *
+ * Functional this is identical to write_file but calls erase() before writing.
+ *
+ * Return: 0 for success or negative error value
+ */
+int write_file_flash(const char *filename, const void *buf, size_t size)
+{
+	int fd, ret;
+
+	fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT);
+	if (fd < 0)
+		return fd;
+
+	ret = erase(fd, size, 0);
+	if (ret < 0)
+		goto out_close;
+
+	ret = write_full(fd, buf, size);
+
+out_close:
+	close(fd);
+
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+EXPORT_SYMBOL(write_file_flash);
+
 /**
  * copy_file - Copy a file
  * @src:	The source filename
-- 
2.17.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2018-06-01 18:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-01 18:07 [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Uwe Kleine-König
2018-06-01 18:07 ` Uwe Kleine-König [this message]
2018-06-07 20:53   ` [PATCH 2/4] libfile: implement new helper write_file_flash() Uwe Kleine-König
2018-06-08  7:10     ` Sascha Hauer
2018-06-08  8:44       ` Uwe Kleine-König
2018-06-01 18:07 ` [PATCH 3/4] omap/am33xx_bbu: use file_write_flash() instead of its own variant Uwe Kleine-König
2018-06-01 18:07 ` [PATCH 4/4] scripts/kwboot: fix image check for v0 images Uwe Kleine-König
2018-06-04  7:01 ` [PATCH 1/4] kwbimage_v0: add support to detect and boot a mvebu v0 image Sascha Hauer
2018-06-04  7:02   ` Uwe Kleine-König

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180601180750.5436-2-u.kleine-koenig@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox