diff mbox series

[v3,2/7] net: wget: add wget with dns utility function

Message ID 20230915101640.1166332-3-masahisa.kojima@linaro.org
State Superseded
Headers show
Series Add EFI HTTP boot support | expand

Commit Message

Masahisa Kojima Sept. 15, 2023, 10:16 a.m. UTC
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>
---
 include/net.h |  2 ++
 net/wget.c    | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)

Comments

Simon Glass Sept. 21, 2023, 1:02 a.m. UTC | #1
Hi Masahisa,

On Fri, 15 Sept 2023 at 04:17, Masahisa Kojima
<masahisa.kojima@linaro.org> wrote:
>
> 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>
> ---
>  include/net.h |  2 ++
>  net/wget.c    | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 56 insertions(+)
>
> diff --git a/include/net.h b/include/net.h
> index e254df7d7f..b9cba5244a 100644
> --- a/include/net.h
> +++ b/include/net.h
> @@ -926,4 +926,6 @@ void eth_set_enable_bootdevs(bool enable);
>  static inline void eth_set_enable_bootdevs(bool enable) {}
>  #endif
>
> +int wget_with_dns(ulong dst_addr, char *uri);

Please add a comment about what this does, including args and return
value. We comment all global functions and non-trvial static ones.

> +
>  #endif /* __NET_H__ */
> diff --git a/net/wget.c b/net/wget.c
> index 9690e4539c..bdf940a315 100644
> --- a/net/wget.c
> +++ b/net/wget.c
> @@ -15,6 +15,7 @@
>  #include <net.h>
>  #include <net/tcp.h>
>  #include <net/wget.h>
> +#include <stdlib.h>
>
>  DECLARE_GLOBAL_DATA_PTR;
>
> @@ -504,3 +505,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
> --
> 2.34.1
>

Regards,
Simon
Masahisa Kojima Sept. 22, 2023, 6:50 a.m. UTC | #2
On Thu, 21 Sept 2023 at 10:02, Simon Glass <sjg@chromium.org> wrote:
>
> Hi Masahisa,
>
> On Fri, 15 Sept 2023 at 04:17, Masahisa Kojima
> <masahisa.kojima@linaro.org> wrote:
> >
> > 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>
> > ---
> >  include/net.h |  2 ++
> >  net/wget.c    | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
> >  2 files changed, 56 insertions(+)
> >
> > diff --git a/include/net.h b/include/net.h
> > index e254df7d7f..b9cba5244a 100644
> > --- a/include/net.h
> > +++ b/include/net.h
> > @@ -926,4 +926,6 @@ void eth_set_enable_bootdevs(bool enable);
> >  static inline void eth_set_enable_bootdevs(bool enable) {}
> >  #endif
> >
> > +int wget_with_dns(ulong dst_addr, char *uri);
>
> Please add a comment about what this does, including args and return
> value. We comment all global functions and non-trvial static ones.

Yes, I will add a comment for the global function.

Thanks,
Masahisa Kojima

>
> > +
> >  #endif /* __NET_H__ */
> > diff --git a/net/wget.c b/net/wget.c
> > index 9690e4539c..bdf940a315 100644
> > --- a/net/wget.c
> > +++ b/net/wget.c
> > @@ -15,6 +15,7 @@
> >  #include <net.h>
> >  #include <net/tcp.h>
> >  #include <net/wget.h>
> > +#include <stdlib.h>
> >
> >  DECLARE_GLOBAL_DATA_PTR;
> >
> > @@ -504,3 +505,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
> > --
> > 2.34.1
> >
>
> Regards,
> Simon
diff mbox series

Patch

diff --git a/include/net.h b/include/net.h
index e254df7d7f..b9cba5244a 100644
--- a/include/net.h
+++ b/include/net.h
@@ -926,4 +926,6 @@  void eth_set_enable_bootdevs(bool enable);
 static inline void eth_set_enable_bootdevs(bool enable) {}
 #endif
 
+int wget_with_dns(ulong dst_addr, char *uri);
+
 #endif /* __NET_H__ */
diff --git a/net/wget.c b/net/wget.c
index 9690e4539c..bdf940a315 100644
--- a/net/wget.c
+++ b/net/wget.c
@@ -15,6 +15,7 @@ 
 #include <net.h>
 #include <net/tcp.h>
 #include <net/wget.h>
+#include <stdlib.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -504,3 +505,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