From 4b8e78585171787794611205d661b97bc5f4dd83 Mon Sep 17 00:00:00 2001 From: Jerome Forissier Date: Thu, 9 Oct 2025 14:30:14 +0200 Subject: net: make dhcp_run() common for NET and NET_LWIP There are currently two implementations of dhcp_run(): one in cmd/net.c for NET and one in net/lwip/dhcp.c for NET_LWIP. There is no justification for that. Therefore, move the NET version into net/net-common.c to be used by both stacks, and drop the NET_LWIP version which by the way does not look totally correct. Signed-off-by: Jerome Forissier Suggested-by: Tom Rini Acked-by: Benjamin Hahn --- net/lwip/dhcp.c | 22 ---------------------- net/net-common.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 22 deletions(-) (limited to 'net') diff --git a/net/lwip/dhcp.c b/net/lwip/dhcp.c index 531bf2c6705..b798014ebcb 100644 --- a/net/lwip/dhcp.c +++ b/net/lwip/dhcp.c @@ -150,25 +150,3 @@ int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) return CMD_RET_SUCCESS; } - -int dhcp_run(ulong addr, const char *fname, bool autoload) -{ - char *dhcp_argv[] = {"dhcp", NULL, }; -#ifdef CONFIG_CMD_TFTPBOOT - char *tftp_argv[] = {"tftpboot", boot_file_name, NULL, }; -#endif - struct cmd_tbl cmdtp = {}; /* dummy */ - - if (autoload) { -#ifdef CONFIG_CMD_TFTPBOOT - /* Assume DHCP was already performed */ - if (boot_file_name[0]) - return do_tftpb(&cmdtp, 0, 2, tftp_argv); - return 0; -#else - return -EOPNOTSUPP; -#endif - } - - return do_dhcp(&cmdtp, 0, 1, dhcp_argv); -} diff --git a/net/net-common.c b/net/net-common.c index b064557d524..442b0597558 100644 --- a/net/net-common.c +++ b/net/net-common.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include #include @@ -48,3 +49,37 @@ void net_sntp_set_rtc(u32 seconds) tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); } + +#if defined(CONFIG_CMD_DHCP) +int dhcp_run(ulong addr, const char *fname, bool autoload) +{ + char *dhcp_argv[] = {"dhcp", NULL, (char *)fname, NULL}; + struct cmd_tbl cmdtp = {}; /* dummy */ + char file_addr[17]; + int old_autoload; + int ret, result; + + log_debug("addr=%lx, fname=%s, autoload=%d\n", addr, fname, autoload); + old_autoload = env_get_yesno("autoload"); + ret = env_set("autoload", autoload ? "y" : "n"); + if (ret) + return log_msg_ret("en1", -EINVAL); + + if (autoload) { + sprintf(file_addr, "%lx", addr); + dhcp_argv[1] = file_addr; + } + + result = do_dhcp(&cmdtp, 0, !autoload ? 1 : fname ? 3 : 2, dhcp_argv); + + ret = env_set("autoload", old_autoload == -1 ? NULL : + old_autoload ? "y" : "n"); + if (ret) + return log_msg_ret("en2", -EINVAL); + + if (result) + return log_msg_ret("res", -ENOENT); + + return 0; +} +#endif -- cgit v1.2.3