This patch moves some code out of the "write" handler with the
intention to reuse it later for similar commands.

At the same time, it also discards the previous transfer rate output
when using "-v", as that has become redundant with the 'progress bar'
display available by "-p".

Signed-off-by: Bernhard Nortmann <bernhard.nortm...@web.de>
---
 fel.c | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)

diff --git a/fel.c b/fel.c
index 595b682..c9081b6 100644
--- a/fel.c
+++ b/fel.c
@@ -32,6 +32,7 @@
 #include <stdarg.h>
 #include <errno.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #include "endian_compat.h"
 #include "progress.h"
@@ -333,6 +334,14 @@ void hexdump(void *data, uint32_t offset, size_t size)
        }
 }
 
+off_t file_size(const char *name)
+{
+       struct stat st;
+       if (stat(name, &st) == 0)
+               return st.st_size;
+       return 0;
+}
+
 int save_file(const char *name, void *data, size_t size)
 {
        FILE *out = fopen(name, "wb");
@@ -1218,6 +1227,21 @@ static int aw_fel_get_endpoint(libusb_device_handle *usb)
        return 0;
 }
 
+/* private helper function, gets used for "write*" and "multi*" transfers */
+static void file_upload(libusb_device_handle *handle,
+                       const char *filename, uint32_t offset)
+{
+       size_t size;
+       void *buf = load_file(filename, &size);
+       aw_write_buffer(handle, buf, offset, size, true);
+
+       // If we transferred a script, try to inform U-Boot about its address.
+       if (get_image_type(buf, size) == IH_TYPE_SCRIPT)
+               pass_fel_information(handle, offset);
+
+       free(buf);
+}
+
 int main(int argc, char **argv)
 {
        bool pflag_active = false; /* -p switch, causing "write" to output 
progress */
@@ -1308,22 +1332,11 @@ int main(int argc, char **argv)
                        aw_fel_print_version(handle);
                        skip=1;
                } else if (strcmp(argv[1], "write") == 0 && argc > 3) {
-                       size_t size;
-                       void *buf = load_file(argv[3], &size);
-                       uint32_t offset = strtoul(argv[2], NULL, 0);
-                       progress_start(pflag_active ? progress_bar : NULL, 
size);
-                       double elapsed = aw_write_buffer(handle, buf, offset, 
size, true);
-                       if (elapsed > 0)
-                               pr_info("%.1f kB written in %.1f sec (speed: 
%.1f kB/s)\n",
-                                       kilo(size), elapsed, kilo(size) / 
elapsed);
-                       /*
-                        * If we have transferred a script, try to inform U-Boot
-                        * about its address.
-                        */
-                       if (get_image_type(buf, size) == IH_TYPE_SCRIPT)
-                               pass_fel_information(handle, offset);
-
-                       free(buf);
+                       size_t size = file_size(argv[3]);
+                       if (size > 0) {
+                               progress_start(pflag_active ? progress_bar : 
NULL, size);
+                               file_upload(handle, argv[3], strtoul(argv[2], 
NULL, 0));
+                       }
                        skip=3;
                } else if (strcmp(argv[1], "read") == 0 && argc > 4) {
                        size_t size = strtoul(argv[3], NULL, 0);
-- 
2.4.6

-- 
You received this message because you are subscribed to the Google Groups 
"linux-sunxi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to linux-sunxi+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to