[PATCH 2/2] dfu_load.c: Do not download file suffix

Tormod Volden lists.tormod at gmail.com
Sat May 21 01:49:15 CEST 2011


From: Tormod Volden <debian.tormod at gmail.com>

If a DFU file suffix is detected, it will be stripped off and
not sent to the device.

The length of the suffix is available in file.suffixlen so it
can be used to calculate how much of the file is left to send.

For this the download function call interface is changed to pass
the dfu_file structure instead of only a file descriptor.

Also some sanity checks could be removed since they are already
taken care of by parse_dfu_suffix().

Signed-off-by: Tormod Volden <debian.tormod at gmail.com>
---

This could need even more testing...

Tormod

 src/dfu_load.c |   32 ++++++++++++--------------------
 src/dfu_load.h |    4 ++--
 src/main.c     |    4 ++--
 3 files changed, 16 insertions(+), 24 deletions(-)

diff --git a/src/dfu_load.c b/src/dfu_load.c
index 2d1fb26..31f9858 100644
--- a/src/dfu_load.c
+++ b/src/dfu_load.c
@@ -33,10 +33,11 @@
 #include "config.h"
 #include "dfu.h"
 #include "usb_dfu.h"
+#include "dfu_file.h"
 #include "dfu_load.h"
 #include "quirks.h"
 
-int dfuload_do_upload(struct dfu_if *dif, int xfer_size, int fd)
+int dfuload_do_upload(struct dfu_if *dif, int xfer_size, struct dfu_file file)
 {
 	int total_bytes = 0;
 	unsigned char *buf;
@@ -58,7 +59,7 @@ int dfuload_do_upload(struct dfu_if *dif, int xfer_size, int fd)
 			ret = rc;
 			goto out_free;
 		}
-		write_rc = write(fd, buf, rc);
+		write_rc = write(file.fd, buf, rc);
 		if (write_rc < rc) {
 			fprintf(stderr, "Short file write: %s\n",
 				strerror(errno));
@@ -87,12 +88,11 @@ out_free:
 
 #define PROGRESS_BAR_WIDTH 50
 
-int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, int fd)
+int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file file)
 {
 	int bytes_sent = 0;
 	unsigned int bytes_per_hash, hashes = 0;
 	unsigned char *buf;
-	struct stat st;
 	struct dfu_status dst;
 	int ret;
 
@@ -100,32 +100,24 @@ int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, int fd)
 	if (!buf)
 		return -ENOMEM;
 
-	ret = fstat(fd, &st);
-	if (ret < 0) {
-		perror(NULL);
-		goto out_free;
-	}
-
-	if (st.st_size <= 0 /* + DFU_HDR */) {
-		fprintf(stderr, "File seems a bit too small...\n");
-		ret = -EINVAL;
-		goto out_free;	
-	}
-
-	bytes_per_hash = st.st_size / PROGRESS_BAR_WIDTH;
+	bytes_per_hash = (file.size - file.suffixlen) / PROGRESS_BAR_WIDTH;
 	if (bytes_per_hash == 0)
 		bytes_per_hash = 1;
 	printf("bytes_per_hash=%u\n", bytes_per_hash);
 #if 0
-	read(fd, DFU_HDR);
+	read(file.fd, DFU_HDR);
 #endif
 	printf("Copying data from PC to DFU device\n");
 	printf("Starting download: [");
 	fflush(stdout);
-	while (bytes_sent < st.st_size /* - DFU_HDR */) {
+	while (bytes_sent < file.size - file.suffixlen) {
 		int hashes_todo;
+		int chunk_size;
 
-		ret = read(fd, buf, xfer_size);
+		chunk_size = file.size - file.suffixlen - bytes_sent;
+		if (chunk_size > xfer_size)
+			chunk_size = xfer_size;
+		ret = read(file.fd, buf, chunk_size);
 		if (ret < 0) {
 			perror(NULL);
 			goto out_free;
diff --git a/src/dfu_load.h b/src/dfu_load.h
index ac33221..ba50506 100644
--- a/src/dfu_load.h
+++ b/src/dfu_load.h
@@ -1,7 +1,7 @@
 #ifndef _SAM7DFU_H
 #define _SAM7DFU_H
 
-int dfuload_do_upload(struct dfu_if *dif, int xfer_size, int fd);
-int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, int fd);
+int dfuload_do_upload(struct dfu_if *dif, int xfer_size, struct dfu_file file);
+int dfuload_do_dnload(struct dfu_if *dif, int xfer_size, struct dfu_file file);
 
 #endif
diff --git a/src/main.c b/src/main.c
index 5e27fc7..05894af 100644
--- a/src/main.c
+++ b/src/main.c
@@ -875,7 +875,7 @@ status_again:
 			perror(file.name);
 			exit(1);
 		}
-		if (dfuload_do_upload(dif, transfer_size, file.fd) < 0)
+		if (dfuload_do_upload(dif, transfer_size, file) < 0)
 			exit(1);
 		close(file.fd);
 		break;
@@ -905,7 +905,7 @@ status_again:
 			fprintf(stderr, "Warning: File product ID %04x does "
 				"not match device\n", file.idProduct);
 		}
-		if (dfuload_do_dnload(dif, transfer_size, file.fd) < 0)
+		if (dfuload_do_dnload(dif, transfer_size, file) < 0)
 			exit(1);
 		close(file.fd);
 		break;
-- 
1.7.0.4




More information about the devel mailing list