[PATCH] net: make dhcp_run() common for NET and NET_LWIP

Heinrich Schuchardt xypron.glpk at gmx.de
Thu Oct 16 14:53:50 CEST 2025


On 10/9/25 14:30, Jerome Forissier wrote:
> 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 <jerome.forissier at linaro.org>
> Suggested-by: Tom Rini <trini at konsulko.com>
> ---
> 
>   cmd/net.c            | 36 ++----------------------------------
>   include/net-common.h | 10 ++++++++++
>   include/net-lwip.h   |  1 -
>   net/lwip/dhcp.c      | 22 ----------------------
>   net/net-common.c     | 35 +++++++++++++++++++++++++++++++++++
>   5 files changed, 47 insertions(+), 57 deletions(-)
> 
> diff --git a/cmd/net.c b/cmd/net.c
> index 886735ea14f6..24099764493e 100644
> --- a/cmd/net.c
> +++ b/cmd/net.c
> @@ -134,8 +134,8 @@ U_BOOT_CMD(dhcp6,	3,	1,	do_dhcp6,
>   #endif
>   
>   #if defined(CONFIG_CMD_DHCP)
> -static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
> -		   char *const argv[])
> +int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
> +	    char *const argv[])

Thank you for all this clean up work.

Should we move the dhcp command to a new file cmd/dhcp.c to get rid of 
all the related '#if' statements?

Best regards

Heinrich

>   {
>   	return netboot_common(DHCP, cmdtp, argc, argv);
>   }
> @@ -145,38 +145,6 @@ U_BOOT_CMD(
>   	"boot image via network using DHCP/TFTP protocol",
>   	"[loadAddress] [[hostIPaddr:]bootfilename]"
>   );
> -
> -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
>   
>   #if defined(CONFIG_CMD_NFS)
> diff --git a/include/net-common.h b/include/net-common.h
> index 1112af381a98..78d98e5bba07 100644
> --- a/include/net-common.h
> +++ b/include/net-common.h
> @@ -479,6 +479,16 @@ int net_loop(enum proto_t protocol);
>    */
>   int dhcp_run(ulong addr, const char *fname, bool autoload);
>   
> +/**
> + * do_dhcp - Run the dhcp command
> + *
> + * @cmdtp: Unused
> + * @flag: Command flags (CMD_FLAG_...)
> + * @argc: Number of arguments
> + * @argv: List of arguments
> + * Return: result (see enum command_ret_t)
> + */
> +int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
>   
>   /**
>    * do_ping - Run the ping command
> diff --git a/include/net-lwip.h b/include/net-lwip.h
> index e88e2186635b..c910def5719e 100644
> --- a/include/net-lwip.h
> +++ b/include/net-lwip.h
> @@ -50,7 +50,6 @@ int net_lwip_dns_resolve(char *name_or_ip, ip_addr_t *ip);
>    */
>   bool wget_validate_uri(char *uri);
>   
> -int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
>   int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
>   int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
>   
> diff --git a/net/lwip/dhcp.c b/net/lwip/dhcp.c
> index 531bf2c67057..b798014ebcb9 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 b064557d5241..442b05975581 100644
> --- a/net/net-common.c
> +++ b/net/net-common.c
> @@ -1,6 +1,7 @@
>   // SPDX-License-Identifier: GPL-2.0
>   
>   #include <dm/uclass.h>
> +#include <env.h>
>   #include <net-common.h>
>   #include <linux/time.h>
>   #include <rtc.h>
> @@ -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



More information about the U-Boot mailing list