Split the do_tftpb() into do_tftpb() which is the command line interface
and parser of various parameters passed on command line, and do_tftpb_raw()
which is the core TFTP loop invocation. The purpose of do_tftpb_raw() is
to be called from within U-Boot, namely from the DFU TFTP code, which is
passing in custom load address, filename and timeout, and which uses the
default server IP and ports from U-Boot environment.

Signed-off-by: Marek Vasut <[email protected]>
---
Cc: Ilias Apalodimas <[email protected]>
Cc: James Hilliard <[email protected]>
Cc: Jerome Forissier <[email protected]>
Cc: Lukasz Majewski <[email protected]>
Cc: Mattijs Korpershoek <[email protected]>
Cc: Quentin Schulz <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: [email protected]
---
 include/net-common.h | 13 ++++++
 net/lwip/tftp.c      | 97 ++++++++++++++++++++++++--------------------
 2 files changed, 65 insertions(+), 45 deletions(-)

diff --git a/include/net-common.h b/include/net-common.h
index f1f670dbd8b..ad777949dda 100644
--- a/include/net-common.h
+++ b/include/net-common.h
@@ -568,6 +568,19 @@ int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, 
char *const argv[]);
  */
 int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 
+/**
+ * do_tftpb_raw - Run the tftpboot from within U-Boot
+ *
+ * @laddr: Load address
+ * @fname: File name to load
+ * @server_ip: Optional server IP string, or NULL
+ * @server_port: Optional server port string, or NULL
+ * @timeout: Transfer timeout
+ * Return: 0 if OK, -ve on error
+ */
+int do_tftpb_raw(ulong laddr, char *fname, char *server_ip, char *server_port,
+                unsigned long timeout);
+
 /**
  * wget_do_request() - sends a wget request
  *
diff --git a/net/lwip/tftp.c b/net/lwip/tftp.c
index 1d0e95d826b..a49d5a72852 100644
--- a/net/lwip/tftp.c
+++ b/net/lwip/tftp.c
@@ -428,9 +428,53 @@ out:
        return ret;
 }
 
-int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+int do_tftpb_raw(ulong laddr, char *fname, char *server_ip, char *server_port,
+                unsigned long timeout)
 {
        struct net_lwip_ctx net = {};
+       u16 port = TFTP_PORT;
+       ip_addr_t srvip;
+       int ret;
+
+       if (!server_ip)
+               server_ip = env_get("tftpserverip");
+       if (!server_ip)
+               server_ip = env_get("serverip");
+       if (!server_ip) {
+               log_err("error: tftpserverip/serverip has to be set\n");
+               return -EINVAL;
+       }
+
+       if (server_port)
+               port = dectoul(server_port, NULL);
+
+       if (!ipaddr_aton(server_ip, &srvip)) {
+               log_err("error: ipaddr_aton\n");
+               return -EINVAL;
+       }
+
+       if (!fname) {
+               log_err("error: no file name\n");
+               return -EINVAL;
+       }
+
+       if (!laddr) {
+               log_err("error: no load address\n");
+               return -EINVAL;
+       }
+
+       if (net_lwip_start(&net, NET_LWIP_ADDR_ENV_STRICT))
+               return -EINVAL;
+
+       ret = tftp_loop(&net, laddr, fname, srvip, port, timeout);
+
+       net_lwip_stop(&net);
+
+       return ret;
+}
+
+int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
        int ret = CMD_RET_SUCCESS;
        char *arg = NULL;
        char *words[3] = { };
@@ -438,8 +482,6 @@ int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, 
char *const argv[])
        char *server_ip = NULL;
        char *server_port = NULL;
        char *end;
-       ip_addr_t srvip;
-       u16 port = TFTP_PORT;
        ulong laddr;
        ulong addr;
        int i;
@@ -470,8 +512,7 @@ int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, 
char *const argv[])
                arg = strdup(argv[2]);
                break;
        default:
-               ret = CMD_RET_USAGE;
-               goto out;
+               return CMD_RET_USAGE;
        }
 
        if (!arg)
@@ -501,49 +542,15 @@ int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, 
char *const argv[])
                }
        }
 
-       if (!server_ip)
-               server_ip = env_get("tftpserverip");
-       if (!server_ip)
-               server_ip = env_get("serverip");
-       if (!server_ip) {
-               log_err("error: tftpserverip/serverip has to be set\n");
-               ret = CMD_RET_FAILURE;
-               goto out;
-       }
-
-       if (server_port)
-               port = dectoul(server_port, NULL);
-
-       if (!ipaddr_aton(server_ip, &srvip)) {
-               log_err("error: ipaddr_aton\n");
-               ret = CMD_RET_FAILURE;
-               goto out;
-       }
+       ret = do_tftpb_raw(laddr, fname, server_ip, server_port, 
TFTP_TIMEOUT_MSECS);
 
-       if (!fname) {
-               log_err("error: no file name\n");
-               ret = CMD_RET_FAILURE;
-               goto out;
-       }
+       if (arg != net_boot_file_name)
+               free(arg);
 
-       if (!laddr) {
-               log_err("error: no load address\n");
-               ret = CMD_RET_FAILURE;
-               goto out;
-       }
+       if (ret)
+               return ret;
 
-       if (net_lwip_start(&net, NET_LWIP_ADDR_ENV_STRICT)) {
-               ret = CMD_RET_FAILURE;
-               goto out;
-       }
+       image_load_addr = laddr;
 
-       if (tftp_loop(&net, laddr, fname, srvip, port, TFTP_TIMEOUT_MSECS) < 0)
-               ret = CMD_RET_FAILURE;
-       else
-               image_load_addr = laddr;
-out:
-       net_lwip_stop(&net);
-       if (arg != net_boot_file_name)
-               free(arg);
        return ret;
 }
-- 
2.53.0

Reply via email to