[U-Boot,v2,3/6] dfu: mmc: rearrange the code
diff mbox series

Message ID 20190924131111.1691-4-m.szyprowski@samsung.com
State Superseded
Delegated to: Matthias Brugger
Headers show
Series
  • Raspberry Pi4: add support for DFU over USB
Related show

Commit Message

Marek Szyprowski Sept. 24, 2019, 1:11 p.m. UTC
Rename functions for bufferred file io operations to make them easier to
understand. Also add missing file offset argument to them (currently
unused). All this is a preparation to remove predefined file size limit
(CONFIG_SYS_DFU_MAX_FILE_SIZE) for DFU read/write operations.

Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
---
 drivers/dfu/dfu_mmc.c | 61 ++++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 27 deletions(-)

Comments

Lukasz Majewski Sept. 24, 2019, 7:48 p.m. UTC | #1
Hi Marek,

> Rename functions for bufferred file io operations to make them easier
> to understand. Also add missing file offset argument to them
> (currently unused). All this is a preparation to remove predefined
> file size limit (CONFIG_SYS_DFU_MAX_FILE_SIZE) for DFU read/write
> operations.

Acked-by: Lukasz Majewski <lukma@denx.de>

> 
> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
> ---
>  drivers/dfu/dfu_mmc.c | 61
> ++++++++++++++++++++++++------------------- 1 file changed, 34
> insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/dfu/dfu_mmc.c b/drivers/dfu/dfu_mmc.c
> index 403fd5351d..2d97f4528c 100644
> --- a/drivers/dfu/dfu_mmc.c
> +++ b/drivers/dfu/dfu_mmc.c
> @@ -91,22 +91,8 @@ static int mmc_block_op(enum dfu_op op, struct
> dfu_entity *dfu, return 0;
>  }
>  
> -static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long
> *len) -{
> -	if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
> -		dfu_file_buf_len = 0;
> -		return -EINVAL;
> -	}
> -
> -	/* Add to the current buffer. */
> -	memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
> -	dfu_file_buf_len += *len;
> -
> -	return 0;
> -}
> -
>  static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
> -			void *buf, u64 *len)
> +			u64 offset, void *buf, u64 *len)
>  {
>  	char dev_part_str[8];
>  	int ret;
> @@ -137,7 +123,7 @@ static int mmc_file_op(enum dfu_op op, struct
> dfu_entity *dfu, 
>  	switch (op) {
>  	case DFU_OP_READ:
> -		ret = fs_read(dfu->name, (size_t)buf, 0, 0, &size);
> +		ret = fs_read(dfu->name, (size_t)buf, offset, 0,
> &size); if (ret) {
>  			puts("dfu: fs_read error!\n");
>  			return ret;
> @@ -145,7 +131,7 @@ static int mmc_file_op(enum dfu_op op, struct
> dfu_entity *dfu, *len = size;
>  		break;
>  	case DFU_OP_WRITE:
> -		ret = fs_write(dfu->name, (size_t)buf, 0, *len,
> &size);
> +		ret = fs_write(dfu->name, (size_t)buf, offset, *len,
> &size); if (ret) {
>  			puts("dfu: fs_write error!\n");
>  			return ret;
> @@ -166,6 +152,30 @@ static int mmc_file_op(enum dfu_op op, struct
> dfu_entity *dfu, return ret;
>  }
>  
> +static int mmc_file_buf_write(struct dfu_entity *dfu, u64 offset,
> void *buf, long *len) +{
> +	if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
> +		dfu_file_buf_len = 0;
> +		return -EINVAL;
> +	}
> +
> +	/* Add to the current buffer. */
> +	memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
> +	dfu_file_buf_len += *len;
> +
> +	return 0;
> +}
> +
> +static int mmc_file_buf_write_finish(struct dfu_entity *dfu)
> +{
> +	int ret = mmc_file_op(DFU_OP_WRITE, dfu, 0, dfu_file_buf,
> +			&dfu_file_buf_len);
> +
> +	/* Now that we're done */
> +	dfu_file_buf_len = 0;
> +	return ret;
> +}
> +
>  int dfu_write_medium_mmc(struct dfu_entity *dfu,
>  		u64 offset, void *buf, long *len)
>  {
> @@ -177,7 +187,7 @@ int dfu_write_medium_mmc(struct dfu_entity *dfu,
>  		break;
>  	case DFU_FS_FAT:
>  	case DFU_FS_EXT4:
> -		ret = mmc_file_buffer(dfu, buf, len);
> +		ret = mmc_file_buf_write(dfu, offset, buf, len);
>  		break;
>  	default:
>  		printf("%s: Layout (%s) not (yet) supported!\n",
> __func__, @@ -193,11 +203,7 @@ int dfu_flush_medium_mmc(struct
> dfu_entity *dfu) 
>  	if (dfu->layout != DFU_RAW_ADDR) {
>  		/* Do stuff here. */
> -		ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf,
> -				&dfu_file_buf_len);
> -
> -		/* Now that we're done */
> -		dfu_file_buf_len = 0;
> +		ret = mmc_file_buf_write_finish(dfu);
>  	}
>  
>  	return ret;
> @@ -214,7 +220,7 @@ int dfu_get_medium_size_mmc(struct dfu_entity
> *dfu, u64 *size) case DFU_FS_FAT:
>  	case DFU_FS_EXT4:
>  		dfu_file_buf_filled = -1;
> -		ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, size);
> +		ret = mmc_file_op(DFU_OP_SIZE, dfu, 0, NULL, size);
>  		if (ret < 0)
>  			return ret;
>  		if (*size > CONFIG_SYS_DFU_MAX_FILE_SIZE)
> @@ -227,14 +233,15 @@ int dfu_get_medium_size_mmc(struct dfu_entity
> *dfu, u64 *size) }
>  }
>  
> -static int mmc_file_unbuffer(struct dfu_entity *dfu, u64 offset,
> void *buf, +
> +static int mmc_file_buf_read(struct dfu_entity *dfu, u64 offset,
> void *buf, long *len)
>  {
>  	int ret;
>  	u64 file_len;
>  
>  	if (dfu_file_buf_filled == -1) {
> -		ret = mmc_file_op(DFU_OP_READ, dfu, dfu_file_buf,
> &file_len);
> +		ret = mmc_file_op(DFU_OP_READ, dfu, 0, dfu_file_buf,
> &file_len); if (ret < 0)
>  			return ret;
>  		dfu_file_buf_filled = file_len;
> @@ -259,7 +266,7 @@ int dfu_read_medium_mmc(struct dfu_entity *dfu,
> u64 offset, void *buf, break;
>  	case DFU_FS_FAT:
>  	case DFU_FS_EXT4:
> -		ret = mmc_file_unbuffer(dfu, offset, buf, len);
> +		ret = mmc_file_buf_read(dfu, offset, buf, len);
>  		break;
>  	default:
>  		printf("%s: Layout (%s) not (yet) supported!\n",
> __func__,




Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma@denx.de

Patch
diff mbox series

diff --git a/drivers/dfu/dfu_mmc.c b/drivers/dfu/dfu_mmc.c
index 403fd5351d..2d97f4528c 100644
--- a/drivers/dfu/dfu_mmc.c
+++ b/drivers/dfu/dfu_mmc.c
@@ -91,22 +91,8 @@  static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
 	return 0;
 }
 
-static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
-{
-	if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
-		dfu_file_buf_len = 0;
-		return -EINVAL;
-	}
-
-	/* Add to the current buffer. */
-	memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
-	dfu_file_buf_len += *len;
-
-	return 0;
-}
-
 static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
-			void *buf, u64 *len)
+			u64 offset, void *buf, u64 *len)
 {
 	char dev_part_str[8];
 	int ret;
@@ -137,7 +123,7 @@  static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
 
 	switch (op) {
 	case DFU_OP_READ:
-		ret = fs_read(dfu->name, (size_t)buf, 0, 0, &size);
+		ret = fs_read(dfu->name, (size_t)buf, offset, 0, &size);
 		if (ret) {
 			puts("dfu: fs_read error!\n");
 			return ret;
@@ -145,7 +131,7 @@  static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
 		*len = size;
 		break;
 	case DFU_OP_WRITE:
-		ret = fs_write(dfu->name, (size_t)buf, 0, *len, &size);
+		ret = fs_write(dfu->name, (size_t)buf, offset, *len, &size);
 		if (ret) {
 			puts("dfu: fs_write error!\n");
 			return ret;
@@ -166,6 +152,30 @@  static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
 	return ret;
 }
 
+static int mmc_file_buf_write(struct dfu_entity *dfu, u64 offset, void *buf, long *len)
+{
+	if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
+		dfu_file_buf_len = 0;
+		return -EINVAL;
+	}
+
+	/* Add to the current buffer. */
+	memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
+	dfu_file_buf_len += *len;
+
+	return 0;
+}
+
+static int mmc_file_buf_write_finish(struct dfu_entity *dfu)
+{
+	int ret = mmc_file_op(DFU_OP_WRITE, dfu, 0, dfu_file_buf,
+			&dfu_file_buf_len);
+
+	/* Now that we're done */
+	dfu_file_buf_len = 0;
+	return ret;
+}
+
 int dfu_write_medium_mmc(struct dfu_entity *dfu,
 		u64 offset, void *buf, long *len)
 {
@@ -177,7 +187,7 @@  int dfu_write_medium_mmc(struct dfu_entity *dfu,
 		break;
 	case DFU_FS_FAT:
 	case DFU_FS_EXT4:
-		ret = mmc_file_buffer(dfu, buf, len);
+		ret = mmc_file_buf_write(dfu, offset, buf, len);
 		break;
 	default:
 		printf("%s: Layout (%s) not (yet) supported!\n", __func__,
@@ -193,11 +203,7 @@  int dfu_flush_medium_mmc(struct dfu_entity *dfu)
 
 	if (dfu->layout != DFU_RAW_ADDR) {
 		/* Do stuff here. */
-		ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf,
-				&dfu_file_buf_len);
-
-		/* Now that we're done */
-		dfu_file_buf_len = 0;
+		ret = mmc_file_buf_write_finish(dfu);
 	}
 
 	return ret;
@@ -214,7 +220,7 @@  int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size)
 	case DFU_FS_FAT:
 	case DFU_FS_EXT4:
 		dfu_file_buf_filled = -1;
-		ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, size);
+		ret = mmc_file_op(DFU_OP_SIZE, dfu, 0, NULL, size);
 		if (ret < 0)
 			return ret;
 		if (*size > CONFIG_SYS_DFU_MAX_FILE_SIZE)
@@ -227,14 +233,15 @@  int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size)
 	}
 }
 
-static int mmc_file_unbuffer(struct dfu_entity *dfu, u64 offset, void *buf,
+
+static int mmc_file_buf_read(struct dfu_entity *dfu, u64 offset, void *buf,
 			     long *len)
 {
 	int ret;
 	u64 file_len;
 
 	if (dfu_file_buf_filled == -1) {
-		ret = mmc_file_op(DFU_OP_READ, dfu, dfu_file_buf, &file_len);
+		ret = mmc_file_op(DFU_OP_READ, dfu, 0, dfu_file_buf, &file_len);
 		if (ret < 0)
 			return ret;
 		dfu_file_buf_filled = file_len;
@@ -259,7 +266,7 @@  int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
 		break;
 	case DFU_FS_FAT:
 	case DFU_FS_EXT4:
-		ret = mmc_file_unbuffer(dfu, offset, buf, len);
+		ret = mmc_file_buf_read(dfu, offset, buf, len);
 		break;
 	default:
 		printf("%s: Layout (%s) not (yet) supported!\n", __func__,