]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
net: wget: add wget with dns utility function
authorMasahisa Kojima <masahisa.kojima@linaro.org>
Fri, 10 Nov 2023 04:25:35 +0000 (13:25 +0900)
committerIlias Apalodimas <ilias.apalodimas@linaro.org>
Sat, 18 Nov 2023 08:08:08 +0000 (10:08 +0200)
Current wget takes the target uri in this format:
 "<http server ip>:<file path>"  e.g.) 192.168.1.1:/bar
The http server ip address must be resolved before
calling wget.

This commit adds the utility function runs wget with dhs.
User can call wget with the uri like "http://foo/bar".

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Ramon Fried <rfried.dev@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
include/net.h
net/wget.c

index e63a946002df94edfde0a16f00f909594625ee8d..61f9018769a3b7726577c05338bd6ef4272d9460 100644 (file)
@@ -930,4 +930,13 @@ void eth_set_enable_bootdevs(bool enable);
 static inline void eth_set_enable_bootdevs(bool enable) {}
 #endif
 
+/**
+ * wget_with_dns() - runs dns host IP address resulution before wget
+ *
+ * @dst_addr:  destination address to download the file
+ * @uri:       uri string of target file of wget
+ * Return:     downloaded file size, negative if failed
+ */
+int wget_with_dns(ulong dst_addr, char *uri);
+
 #endif /* __NET_H__ */
index 6f97eb1d125d0615ea8d893a7f45909b3cca5e0c..2087146b3771d1124c32d59be0bbd8cd031d2065 100644 (file)
@@ -15,6 +15,7 @@
 #include <net.h>
 #include <net/tcp.h>
 #include <net/wget.h>
+#include <stdlib.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -512,3 +513,56 @@ void wget_start(void)
 
        wget_send(TCP_SYN, 0, 0, 0);
 }
+
+#if (IS_ENABLED(CONFIG_CMD_DNS))
+int wget_with_dns(ulong dst_addr, char *uri)
+{
+       int ret;
+       char *s, *host_name, *file_name, *str_copy;
+
+       /*
+        * Download file using wget.
+        *
+        * U-Boot wget takes the target uri in this format.
+        *  "<http server ip>:<file path>"  e.g.) 192.168.1.1:/sample/test.iso
+        * Need to resolve the http server ip address before starting wget.
+        */
+       str_copy = strdup(uri);
+       if (!str_copy)
+               return -ENOMEM;
+
+       s = str_copy + strlen("http://");
+       host_name = strsep(&s, "/");
+       if (!s) {
+               log_err("Error: invalied uri, no file path\n");
+               ret = -EINVAL;
+               goto out;
+       }
+       file_name = s;
+
+       /* TODO: If the given uri has ip address for the http server, skip dns */
+       net_dns_resolve = host_name;
+       net_dns_env_var = "httpserverip";
+       if (net_loop(DNS) < 0) {
+               log_err("Error: dns lookup of %s failed, check setup\n", net_dns_resolve);
+               ret = -EINVAL;
+               goto out;
+       }
+       s = env_get("httpserverip");
+       if (!s) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       strlcpy(net_boot_file_name, s, sizeof(net_boot_file_name));
+       strlcat(net_boot_file_name, ":/", sizeof(net_boot_file_name)); /* append '/' which is removed by strsep() */
+       strlcat(net_boot_file_name, file_name, sizeof(net_boot_file_name));
+       image_load_addr = dst_addr;
+       ret = net_loop(WGET);
+
+out:
+       free(str_copy);
+
+       return ret;
+}
+#endif