public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Frieder Schrempf <frieder@fris.de>
To: Lukasz Majewski <lukma@denx.de>, u-boot@lists.denx.de
Cc: Heiko Thiery <heiko.thiery@gmail.com>,
	Michael Walle <michael@walle.cc>,
	Frieder Schrempf <frieder.schrempf@kontron.de>,
	AKASHI Takahiro <takahiro.akashi@linaro.org>,
	Aswath Govindraju <a-govindraju@ti.com>,
	Jaehoon Chung <jh80.chung@samsung.com>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	Oleksandr Suvorov <oleksandr.suvorov@toradex.com>,
	Patrice Chotard <patrice.chotard@foss.st.com>,
	Patrick Delaunay <patrick.delaunay@foss.st.com>
Subject: [PATCH 1/3] dfu: use pointer to pass length to dfu_write()
Date: Thu, 30 Sep 2021 18:22:06 +0200	[thread overview]
Message-ID: <20210930162208.2749155-1-frieder@fris.de> (raw)

From: Frieder Schrempf <frieder.schrempf@kontron.de>

This doesn't include any functional changes, but allows us to use the
size parameter to report the actual written bytes back to the caller.

This is useful in cases of unaligned writes to serial flash where the
first write covers only a part of the buffer (until the next sector
boundary) and the remaining bytes need to be written in a further call
to dfu_write().

While at it, we also adjust the data type of the 'write' and 'left'
variable in dfu_write_from_mem_addr() to match the values passed.

Signed-off-by: Frieder Schrempf <frieder.schrempf@kontron.de>
---
 drivers/dfu/dfu.c | 23 ++++++++++++-----------
 include/dfu.h     |  2 +-
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c
index ff1859d946..5b2659ee7d 100644
--- a/drivers/dfu/dfu.c
+++ b/drivers/dfu/dfu.c
@@ -334,12 +334,12 @@ int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
 	return ret;
 }
 
-int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
+int dfu_write(struct dfu_entity *dfu, void *buf, int *size, int blk_seq_num)
 {
 	int ret;
 
 	debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n",
-	      __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
+	      __func__, dfu->name, buf, *size, blk_seq_num, dfu->offset,
 	      (unsigned long)(dfu->i_buf - dfu->i_buf_start));
 
 	ret = dfu_transaction_initiate(dfu, false);
@@ -371,7 +371,7 @@ int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
 	dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
 
 	/* flush buffer if overflow */
-	if ((dfu->i_buf + size) > dfu->i_buf_end) {
+	if ((dfu->i_buf + *size) > dfu->i_buf_end) {
 		ret = dfu_write_buffer_drain(dfu);
 		if (ret) {
 			dfu_transaction_cleanup(dfu);
@@ -381,19 +381,19 @@ int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
 	}
 
 	/* we should be in buffer now (if not then size too large) */
-	if ((dfu->i_buf + size) > dfu->i_buf_end) {
+	if ((dfu->i_buf + *size) > dfu->i_buf_end) {
 		pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
-		      size, dfu->i_buf_end);
+		      *size, dfu->i_buf_end);
 		dfu_transaction_cleanup(dfu);
 		dfu_error_callback(dfu, "Buffer overflow");
 		return -1;
 	}
 
-	memcpy(dfu->i_buf, buf, size);
-	dfu->i_buf += size;
+	memcpy(dfu->i_buf, buf, *size);
+	dfu->i_buf += *size;
 
 	/* if end or if buffer full flush */
-	if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
+	if (*size == 0 || (dfu->i_buf + *size) > dfu->i_buf_end) {
 		ret = dfu_write_buffer_drain(dfu);
 		if (ret) {
 			dfu_transaction_cleanup(dfu);
@@ -704,7 +704,8 @@ int dfu_get_alt(char *name)
 
 int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
 {
-	unsigned long dfu_buf_size, write, left = size;
+	unsigned int write, left = size;
+	unsigned long dfu_buf_size;
 	int i, ret = 0;
 	void *dp = buf;
 
@@ -720,9 +721,9 @@ int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size)
 	for (i = 0; left > 0; i++) {
 		write = min(dfu_buf_size, left);
 
-		debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__,
+		debug("%s: dp: 0x%p left: %lu write: %u\n", __func__,
 		      dp, left, write);
-		ret = dfu_write(dfu, dp, write, i);
+		ret = dfu_write(dfu, dp, &write, i);
 		if (ret) {
 			pr_err("DFU write failed\n");
 			return ret;
diff --git a/include/dfu.h b/include/dfu.h
index f6868982df..2df55e507e 100644
--- a/include/dfu.h
+++ b/include/dfu.h
@@ -335,7 +335,7 @@ int dfu_read(struct dfu_entity *de, void *buf, int size, int blk_seq_num);
  * @blk_seq_num:	block sequence number
  * Return:		0 for success, -1 for error
  */
-int dfu_write(struct dfu_entity *de, void *buf, int size, int blk_seq_num);
+int dfu_write(struct dfu_entity *de, void *buf, int *size, int blk_seq_num);
 
 /**
  * dfu_flush() - flush to dfu entity
-- 
2.33.0


             reply	other threads:[~2021-09-30 16:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-30 16:22 Frieder Schrempf [this message]
2021-09-30 16:22 ` [PATCH 2/3] dfu: dfu_fs: Simplify find_sector() Frieder Schrempf
2021-09-30 16:22 ` [PATCH 3/3] dfu: dfu_sf: Support unaligned offsets for writing to serial flash Frieder Schrempf
2022-01-28 15:21 ` [PATCH 1/3] dfu: use pointer to pass length to dfu_write() Tom Rini

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=20210930162208.2749155-1-frieder@fris.de \
    --to=frieder@fris.de \
    --cc=a-govindraju@ti.com \
    --cc=frieder.schrempf@kontron.de \
    --cc=heiko.thiery@gmail.com \
    --cc=jh80.chung@samsung.com \
    --cc=lukma@denx.de \
    --cc=m.szyprowski@samsung.com \
    --cc=michael@walle.cc \
    --cc=oleksandr.suvorov@toradex.com \
    --cc=patrice.chotard@foss.st.com \
    --cc=patrick.delaunay@foss.st.com \
    --cc=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    /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