[PATCHv8 04/15] net/lwip: implement dhcp cmd

Maxim Uvarov maxim.uvarov at linaro.org
Fri Sep 8 15:53:09 CEST 2023


U-Boot recently got support for an alternative network stack using LWIP.
Replace dhcp 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             | 12 +++++++
 net/lwip/Makefile              |  1 +
 net/lwip/apps/dhcp/lwip-dhcp.c | 61 ++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+)
 create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c

diff --git a/include/net/lwip.h b/include/net/lwip.h
index ab3db1a214..6a8fcef392 100644
--- a/include/net/lwip.h
+++ b/include/net/lwip.h
@@ -17,3 +17,15 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int flag, int argc,
  *          Other value < 0, if error
  */
 int ulwip_dns(char *name, char *varname);
+
+/**
+ * ulwip_dhcp() -  create the DHCP request to obtain IP address.
+ *
+ * This function creates the DHCP request to obtain IP address. If DHCP server
+ * returns file name, this file will be downloaded with tftp.  After this
+ * function you need to invoke the polling loop to process network communication.
+ *
+ * Returns: 0 if success
+ *         Other value < 0, if error
+*/
+int ulwip_dhcp(void);
diff --git a/net/lwip/Makefile b/net/lwip/Makefile
index 5d8d5527c6..a3a33b7f71 100644
--- a/net/lwip/Makefile
+++ b/net/lwip/Makefile
@@ -63,4 +63,5 @@ obj-$(CONFIG_NET) += lwip-external/src/netif/ethernet.o
 obj-$(CONFIG_NET) += port/if.o
 obj-$(CONFIG_NET) += port/sys-arch.o
 
+obj-y += apps/dhcp/lwip-dhcp.o
 obj-y += apps/dns/lwip-dns.o
diff --git a/net/lwip/apps/dhcp/lwip-dhcp.c b/net/lwip/apps/dhcp/lwip-dhcp.c
new file mode 100644
index 0000000000..68334a02bb
--- /dev/null
+++ b/net/lwip/apps/dhcp/lwip-dhcp.c
@@ -0,0 +1,61 @@
+// 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 <lwip/dhcp.h>
+#include <lwip/prot/dhcp.h>
+#include "lwip/timeouts.h"
+
+#include <net/eth.h>
+#include <net/ulwip.h>
+
+#define DHCP_WAIT_MS 2000
+
+static void dhcp_tmo(void *arg)
+{
+	struct netif *netif = (struct netif *)arg;
+	struct dhcp *dhcp;
+	int err = 0;
+
+	dhcp = netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
+	if (!dhcp)
+		return;
+
+	switch (dhcp->state) {
+	case DHCP_STATE_BOUND:
+		err += env_set("bootfile", dhcp->boot_file_name);
+		err += env_set("ipaddr", ip4addr_ntoa(&dhcp->offered_ip_addr));
+		err += env_set("netmask", ip4addr_ntoa(&dhcp->offered_sn_mask));
+		err += env_set("serverip", ip4addr_ntoa(&dhcp->server_ip_addr));
+		if (err)
+			log_err("error update envs\n");
+		log_info("DHCP client bound to address %s\n", ip4addr_ntoa(&dhcp->offered_ip_addr));
+		break;
+	default:
+		return;
+	}
+}
+
+int ulwip_dhcp(void)
+{
+	struct netif *netif;
+	int eth_idx;
+
+	eth_idx = eth_get_dev_index();
+	if (eth_idx < 0)
+		return -EPERM;
+
+	netif = netif_get_by_index(eth_idx + 1);
+	if (!netif)
+		return -ENOENT;
+
+	sys_timeout(DHCP_WAIT_MS, dhcp_tmo, netif);
+
+	return dhcp_start(netif) ? 0 : -EPERM;
+}
-- 
2.30.2



More information about the U-Boot mailing list