[PATCHv7 06/15] net/lwip: implement wget cmd
Maxim Uvarov
maxim.uvarov at linaro.org
Tue Aug 22 11:36:05 CEST 2023
U-Boot recently got support for an alternative network stack using LWIP.
Replace wget command with the LWIP variant while keeping the output and
error messages identical.
Signed-off-by: Maxim Uvarov <maxim.uvarov at linaro.org>
---
include/net/lwip.h | 15 +++++
net/lwip/Makefile | 1 +
net/lwip/apps/http/Makefile | 13 ++++
net/lwip/apps/http/lwip-wget.c | 111 +++++++++++++++++++++++++++++++++
4 files changed, 140 insertions(+)
create mode 100644 net/lwip/apps/http/Makefile
create mode 100644 net/lwip/apps/http/lwip-wget.c
diff --git a/include/net/lwip.h b/include/net/lwip.h
index 14a65470ee..724f68b148 100644
--- a/include/net/lwip.h
+++ b/include/net/lwip.h
@@ -28,6 +28,7 @@ int ulwip_dns(char *name, char *varname);
* Returns: 0 if success
* Other value < 0, if error
*/
+int ulwip_dhcp(void);
/**
* ulwip_tftp() - load file with tftp
@@ -41,3 +42,17 @@ int ulwip_dns(char *name, char *varname);
* Returns: 0 if success, !0 if error
*/
int ulwip_tftp(ulong addr, const char *filename);
+
+/**
+ * ulwip_wget() - creates the HTTP request to download file
+ *
+ * This function creates the HTTP request to download file from url to the address
+ * specified in parameters. After this function you need to invoke the polling
+ * loop to process network communication.
+ *
+ *
+ * @addr: start address to download result
+ * @url: url in format http://host/url
+ * Returns: 0 for success, !0 if error
+*/
+int ulwip_wget(ulong addr, char *url);
diff --git a/net/lwip/Makefile b/net/lwip/Makefile
index 0337d82cf5..4c6df94807 100644
--- a/net/lwip/Makefile
+++ b/net/lwip/Makefile
@@ -68,3 +68,4 @@ obj-$(CONFIG_NET) += port/sys-arch.o
obj-$(CONFIG_CMD_DHCP) += apps/dhcp/lwip-dhcp.o
obj-$(CONFIG_CMD_DNS) += apps/dns/lwip-dns.o
obj-$(CONFIG_CMD_TFTPBOOT) += apps/tftp/
+obj-$(CONFIG_CMD_WGET) += apps/http/
diff --git a/net/lwip/apps/http/Makefile b/net/lwip/apps/http/Makefile
new file mode 100644
index 0000000000..3e92b0ef1b
--- /dev/null
+++ b/net/lwip/apps/http/Makefile
@@ -0,0 +1,13 @@
+ccflags-y += -I$(srctree)/net/lwip/port/include
+ccflags-y += -I$(srctree)/net/lwip/lwip-external/src/include -I$(srctree)/net/lwip
+ccflags-y += -I$(obj)
+
+$(obj)/http_clinet.o: $(obj)/http_client.c
+.PHONY: $(obj)/http_client.c
+$(obj)/http_client.c:
+ cp $(srctree)/net/lwip/lwip-external/src/apps/http/http_client.c $(obj)/http_client.c
+ cp $(srctree)/net/lwip/lwip-external/src/include/lwip/apps/http_client.h $(obj)/http_client.h
+
+obj-$(CONFIG_CMD_WGET) += http_client.o
+obj-$(CONFIG_CMD_WGET) += lwip-wget.o
+
diff --git a/net/lwip/apps/http/lwip-wget.c b/net/lwip/apps/http/lwip-wget.c
new file mode 100644
index 0000000000..a9bb29e156
--- /dev/null
+++ b/net/lwip/apps/http/lwip-wget.c
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * (C) Copyright 2023 Linaro Ltd. <maxim.uvarov at linaro.org>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <console.h>
+#include <vsprintf.h>
+
+#include "http_client.h"
+#include <net/ulwip.h>
+
+static ulong daddr;
+static httpc_connection_t settings;
+
+#define SERVER_NAME_SIZE 200
+#define HTTP_PORT_DEFAULT 80
+
+static err_t httpc_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf,
+ err_t unused_err)
+{
+ struct pbuf *buf;
+
+ if (!pbuf)
+ return ERR_BUF;
+
+ for (buf = pbuf; buf != NULL; buf = buf->next) {
+ memcpy((void *)daddr, buf->payload, buf->len);
+ log_debug("downloaded chunk size %d, to addr 0x%lx\n",
+ buf->len, daddr);
+ daddr += buf->len;
+ }
+
+ altcp_recved(pcb, pbuf->tot_len);
+ pbuf_free(pbuf);
+ return ERR_OK;
+}
+
+static void httpc_result(void *arg, httpc_result_t httpc_result, u32_t rx_content_len,
+ u32_t srv_res, err_t err)
+{
+ if (httpc_result == HTTPC_RESULT_OK) {
+ log_info("\n%d bytes successfully downloaded.\n", rx_content_len);
+ env_set_ulong("filesize", rx_content_len);
+ ulwip_exit(0);
+ } else {
+ log_err("\nhttp eroror: %d\n", httpc_result);
+ ulwip_exit(-1);
+ }
+}
+
+/* http://hostname:port/url */
+static int parse_url(char *url, char *host, u16 *port)
+{
+ char *p, *pp;
+
+ p = strstr(url, "http://");
+ if (!p) {
+ printf("err: no http://!\n");
+ return -1;
+ }
+
+ p += strlen("http://");
+
+ /* parse hostname */
+ pp = strchr(p, '/');
+ if (!pp) {
+ printf("wrong url\n");
+ return -2;
+ }
+
+ if (pp - p >= SERVER_NAME_SIZE)
+ return -3;
+
+ memcpy(host, p, pp - p);
+ host[pp - p + 1] = '\0';
+ *port = HTTP_PORT_DEFAULT;
+
+ return 0;
+}
+
+int ulwip_wget(ulong addr, char *url)
+{
+ err_t err;
+ u16 port;
+ char server_name[SERVER_NAME_SIZE];
+ httpc_state_t *connection;
+
+ daddr = addr;
+
+ err = parse_url(url, server_name, &port);
+ if (err) {
+ log_err("error parse_url\n");
+ return -1;
+ }
+
+ log_info("downloading %s to addr 0x%lx\n", url, addr);
+ memset(&settings, 0, sizeof(settings));
+ settings.result_fn = httpc_result;
+ err = httpc_get_file_dns(server_name, port, url, &settings,
+ httpc_recv, NULL, &connection);
+ if (err != ERR_OK) {
+ log_err("httpc_init_connection failed\n");
+ return err;
+ }
+
+ env_set_hex("fileaddr", addr);
+ return 0;
+}
--
2.30.2
More information about the U-Boot
mailing list