u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] dfu: use pointer to pass length to dfu_write()
@ 2021-09-30 16:22 Frieder Schrempf
  2021-09-30 16:22 ` [PATCH 2/3] dfu: dfu_fs: Simplify find_sector() Frieder Schrempf
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Frieder Schrempf @ 2021-09-30 16:22 UTC (permalink / raw)
  To: Lukasz Majewski, u-boot
  Cc: Heiko Thiery, Michael Walle, Frieder Schrempf, AKASHI Takahiro,
	Aswath Govindraju, Jaehoon Chung, Marek Szyprowski,
	Oleksandr Suvorov, Patrice Chotard, Patrick Delaunay

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-01-28 15:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-30 16:22 [PATCH 1/3] dfu: use pointer to pass length to dfu_write() Frieder Schrempf
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).