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

Tormod Volden lists.tormod at gmail.com
Tue May 24 23:12:39 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.

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

This is version 2 of the patch.

Tormod

 src/dfu_load.c |   39 +++++++++++++++++----------------------
 src/dfu_load.h |    4 ++--
 src/main.c     |    4 ++--
 3 files changed, 21 insertions(+), 26 deletions(-)

diff --git a/src/dfu_load.c b/src/dfu_load.c
index 2d1fb26..cc4b1be 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,34 +100,29 @@ 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;
-
-		ret = read(fd, buf, xfer_size);
+		int bytes_left;
+		int chunk_size;
+
+		bytes_left = file.size - file.suffixlen - bytes_sent;
+		if (bytes_left < xfer_size)
+			chunk_size = bytes_left;
+		else
+			chunk_size = xfer_size;
+		ret = read(file.fd, buf, chunk_size);
 		if (ret < 0) {
-			perror(NULL);
+			perror(file.name);
 			goto out_free;
 		}
 		ret = dfu_download(dif->dev_handle, dif->interface, ret, ret ? buf : NULL);
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 2896723..d51b816 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