From patchwork Mon Aug 14 13:32:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 713480 Delivered-To: patch@linaro.org Received: by 2002:a5d:4012:0:b0:317:ecd7:513f with SMTP id n18csp1242753wrp; Mon, 14 Aug 2023 06:37:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHSRehPtTNNWgIQM6B6944RNKE+m+4QZ2n26tj7j5HLsLu8aRNs40Y/dDmgSSc360WK3Jw0 X-Received: by 2002:a05:600c:11cf:b0:3fe:19cf:93c9 with SMTP id b15-20020a05600c11cf00b003fe19cf93c9mr7110674wmi.1.1692020222088; Mon, 14 Aug 2023 06:37:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692020222; cv=none; d=google.com; s=arc-20160816; b=DF9yWGQmmGcGlCT+yXRibgpItGbaNnR/2xX3hbvGhYuXKNXaLVgrvhhmC0O4mZlsxE WUQmMv5HlGjrzs4Evj8fjgcOb8Ob+ktkEX6pD6YuFJeUM2+NETiEd3v/v3lqi+4sLIIq 5ScFDy+d/0wWczyWhPSolb38ueLxc0vr0CkA9UWurbE42OnplwiohUQVhwdMKiFmA2Jp Toju6PDokehUXofLM7gjMmZUXKXgNDTkQJGM6h5nVYfykfCCOFkckLWbBHgeFf75IAZx vaQaeH7xr5lNb5Chz6NBobjcLWWifhGqynHNiIyTeJwb5bytwGybRUtlC7VyOKWxINxj vSgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=FRoLHHce4JOrxPpx2ezZa+k60uaaJrxdQ0nPimAy89s=; fh=KONqw1VFBW9nCv9imase+52c+XJ0MeVkzLj6SOLcJqQ=; b=Ur+JXiyqOvpMZYAawfhduwra+C7GmROlM8hgT40JhwyAVCPMUcXKh/E3C1LCvoskog Wt/8BnSIfjroIZ8f18OVCE7lL1boKEImZaAUSmYvOfA+Va9BdNZsz1tlnWvx/iY9gOal k1tq5NVjeaLWMDShioidhzox50MoWvDfVdNFkQK/AAFMGvhGi1xCxetaSd1BWrRw9Py1 Adhn2uvKSdWS23rmi17YCvr96kkvovCvgD6S+sp4toGghrLv4lZF5DlPWL4P6+rN9uOK +sRkpPYH/aLBgD6EdYXmO5i7yHPfSvHfc5z4kJuQBWpjVHq53WQCWcD4timNACHUR3+t e0VQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=B+zoiKjA; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id n3-20020adffe03000000b003195cb4dcc0si4027204wrr.385.2023.08.14.06.37.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:37:02 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=B+zoiKjA; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9F1A5869A6; Mon, 14 Aug 2023 15:35:43 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="B+zoiKjA"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 52467869A4; Mon, 14 Aug 2023 15:35:43 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [IPv6:2a00:1450:4864:20::22e]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C6B02869A5 for ; Mon, 14 Aug 2023 15:35:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=maxim.uvarov@linaro.org Received: by mail-lj1-x22e.google.com with SMTP id 38308e7fff4ca-2b9f0b7af65so64862791fa.1 for ; Mon, 14 Aug 2023 06:35:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692020140; x=1692624940; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FRoLHHce4JOrxPpx2ezZa+k60uaaJrxdQ0nPimAy89s=; b=B+zoiKjAgb2fNE4DHJv+uRxjrvEq2tzL7t0t2agkYnjUP3qFVWa75w8WEke6DFN0h6 /gbYrPB7jvOddGdukHU8RznI+SzdyGOqfiGhxLcLENI+nO4AcoLMdQxu7lXhqH5KNwP0 tYcNsBKHXosfbmLX9fEDUoCC9dNWa3e+s4R8IuYgJsfQeskG4JyXkWw6zJsO6Efd9olX h77NOjtNYPkuZ/26+WknvvMOfrtCu/+o/u1CmRCc6rPDjH7OmPNFLIhBedkYye2UrjP8 ODM+koEJGyTd/jzuV+YeIuaOpz7BK0bgbL+D5MnGlY3hWdswKldN3wUjbRhovpSA35Wu KKGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692020140; x=1692624940; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FRoLHHce4JOrxPpx2ezZa+k60uaaJrxdQ0nPimAy89s=; b=cQ5NpihInj5Lw15DRfzl5Nu3VXTZSp4DACvAn1cLRrqF2WZPpcdxMPAZ0iPb3OfICS Wa7bkUNlAQuV+0X1EnGWlj46RZkWV3QnBB+5f023IUPO2pIQ73+Q4ERUuujnzTa5yaSu 3/HMW9eHkgSp5/lMyVvi7GBrWxFZ0g1552yrEtB9JaFY5ao9tG967kxc8SaGV+SMz68y hqYw8VGdq9WAqTqI2BBE+V9wC8OQio2miNYb5o12gcwvHpaneQJwl+EUcs1po4abQl/d 4ZU12EMwXjS5h0Vaw0lBYr/ubA62x1H5wYRXymeKZ6d0QB/zbZVMpV+Z9Ux1yH2SJssZ CN3Q== X-Gm-Message-State: AOJu0YyK2q6KeQ11QK5fD5eF0HyRqEFRGCIyd5zdeIJAtMfZLcP+gjM8 oo13K1b3BugqnoqUNgtyZ1pFbDWDN7TrnvcAuKc= X-Received: by 2002:a2e:7809:0:b0:2b6:e625:ba55 with SMTP id t9-20020a2e7809000000b002b6e625ba55mr7144464ljc.41.1692020139750; Mon, 14 Aug 2023 06:35:39 -0700 (PDT) Received: from localhost.localdomain ([45.82.14.220]) by smtp.gmail.com with ESMTPSA id z24-20020a2e9658000000b002b9415597d0sm2411102ljh.78.2023.08.14.06.35.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:35:39 -0700 (PDT) From: Maxim Uvarov To: u-boot@lists.denx.de Cc: pbrobinson@redhat.com, ilias.apalodimas@linaro.org, joe.hershberger@ni.com, rfried.dev@gmail.com, trini@konsulko.com, goldsimon@gmx.de, lwip-devel@nongnu.org, Maxim Uvarov Subject: [PATCHv6 11/14] net/lwip: connection between cmd and lwip apps Date: Mon, 14 Aug 2023 19:32:50 +0600 Message-Id: <20230814133253.4150-12-maxim.uvarov@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230814133253.4150-1-maxim.uvarov@linaro.org> References: <20230814133253.4150-1-maxim.uvarov@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Signed-off-by: Maxim Uvarov --- cmd/Makefile | 1 + cmd/net-lwip.c | 306 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 cmd/net-lwip.c diff --git a/cmd/Makefile b/cmd/Makefile index 9bebf321c3..6ab6b16aab 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -128,6 +128,7 @@ endif obj-$(CONFIG_CMD_MUX) += mux.o obj-$(CONFIG_CMD_NAND) += nand.o obj-$(CONFIG_CMD_NET) += net.o +obj-$(CONFIG_CMD_NET) += net-lwip.o obj-$(CONFIG_CMD_NVEDIT_EFI) += nvedit_efi.o obj-$(CONFIG_CMD_ONENAND) += onenand.o obj-$(CONFIG_CMD_OSD) += osd.o diff --git a/cmd/net-lwip.c b/cmd/net-lwip.c new file mode 100644 index 0000000000..46fe4f091f --- /dev/null +++ b/cmd/net-lwip.c @@ -0,0 +1,306 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * (C) Copyright 2023 Maxim Uvarov, maxim.uvarov@linaro.org + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "net/lwip.h" +#include "net/ulwip.h" + +static int do_lwip_init(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + if (!ulwip_init()) + return CMD_RET_SUCCESS; + return CMD_RET_FAILURE; +} + +static int lwip_empty_tmo(void) { return 0; }; +int (*ulwip_tmo)(void) = lwip_empty_tmo; +void ulwip_set_tmo(int (*tmo)(void)) +{ + ulwip_tmo = tmo; +} + +static void ulwip_clear_tmo(void) +{ + ulwip_tmo = lwip_empty_tmo; +} + +static void ulwip_timeout_handler(void) +{ + eth_halt(); + ulwip_tmo(); + net_set_state(NETLOOP_FAIL); /* we did not get the reply */ + ulwip_loop_set(0); +} + +int ulwip_loop(void) +{ + int ret = CMD_RET_FAILURE; + + ulwip_loop_set(1); + if (!net_loop(LWIP)) + ret = CMD_RET_SUCCESS; + ulwip_loop_set(0); + return ret; +} + +#if defined(CONFIG_CMD_PING) +int do_lwip_ping(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + int ret; + + if (argc < 2) { + printf("argc = %d, error\n", argc); + return CMD_RET_USAGE; + } + + ret = ulwip_init(); + if (ret) { + log_err("ulwip_init err %d\n", ret); + return CMD_RET_FAILURE; + } + + printf("Using %s device\n", eth_get_name()); + printf("pinging addr: %s\n", argv[1]); + + net_set_timeout_handler(1000UL, ulwip_timeout_handler); + + if (ulwip_ping(argv[1])) { + printf("ping init fail\n"); + return CMD_RET_FAILURE; + } + + return ulwip_loop(); +} +#endif /* CONFIG_CMD_PING */ + +#if defined(CONFIG_CMD_WGET) +int do_lwip_wget(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + char *url; + int ret; + + if (argc < 2) + return CMD_RET_USAGE; + + url = argv[1]; + + ret = ulwip_init(); + if (ret) { + log_err("ulwip_init err %d\n", ret); + return CMD_RET_FAILURE; + } + + ret = ulwip_wget(image_load_addr, url); + if (ret) { + log_err("lwip_wget err %d\n", ret); + return CMD_RET_FAILURE; + } + + + return ulwip_loop(); +} +#endif + +#if defined(CONFIG_CMD_TFTPBOOT) +int do_lwip_tftp(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + char *filename; + ulong addr; + char *end; + int ret; + + switch (argc) { + case 1: + filename = env_get("bootfile"); + break; + case 2: + /* + * Only one arg - accept two forms: + * Just load address, or just boot file name. The latter + * form must be written in a format which can not be + * mis-interpreted as a valid number. + */ + addr = hextoul(argv[1], &end); + if (end == (argv[1] + strlen(argv[1]))) { + image_load_addr = addr; + filename = env_get("bootfile"); + } else { + filename = argv[1]; + } + break; + case 3: + image_load_addr = hextoul(argv[1], NULL); + filename = argv[2]; + break; + default: + return CMD_RET_USAGE; + } + + ret = ulwip_init(); + if (ret) { + log_err("ulwip_init err %d\n", ret); + return CMD_RET_FAILURE; + } + + ret = ulwip_tftp(image_load_addr, filename); + if (ret) + return ret; + + return ulwip_loop(); +} +#endif /* CONFIG_CMD_TFTPBOOT */ + +#if defined(CONFIG_CMD_DHCP) +int do_lwip_dhcp(void) +{ + int ret; + char *filename; + + ret = ulwip_init(); + if (ret) { + log_err("ulwip_init err %d\n", ret); + return CMD_RET_FAILURE; + } + + ret = ulwip_dhcp(); + + net_set_timeout_handler(2000UL, ulwip_timeout_handler); + + ulwip_loop(); + if (IS_ENABLED(CONFIG_CMD_TFTPBOOT)) { + ulwip_clear_tmo(); + + filename = env_get("bootfile"); + if (!filename) { + log_notice("no bootfile\n"); + return CMD_RET_SUCCESS; + } + + ret = ulwip_init(); + if (ret) { + log_err("ulwip_init err %d\n", ret); + return CMD_RET_FAILURE; + } + + net_set_timeout_handler(20000UL, ulwip_timeout_handler); + ulwip_tftp(image_load_addr, filename); + + ret = ulwip_loop(); + } + + return ret; +} + +static int _do_lwip_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + return do_lwip_dhcp(); +} +#endif /* CONFIG_CMD_DHCP */ + +#if defined(CONFIG_CMD_DNS) +int do_lwip_dns(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + int ret; + char *name; + char *varname; + int LWIP_ERR_INPROGRESS = -5; /* lwip/err.h */ + + if (argc == 1) + return CMD_RET_USAGE; + + name = argv[1]; + + if (argc == 3) + varname = argv[2]; + else + varname = NULL; + + ret = ulwip_init(); + if (ret) { + log_err("ulwip_init err %d\n", ret); + return CMD_RET_FAILURE; + } + + ret = ulwip_dns(name, varname); + if (ret == 0) + return CMD_RET_SUCCESS; + if (ret != LWIP_ERR_INPROGRESS) + return CMD_RET_FAILURE; + + net_set_timeout_handler(1000UL, ulwip_timeout_handler); + + return ulwip_loop(); +} +#endif /* CONFIG_CMD_DNS */ + +static struct cmd_tbl cmds[] = { + U_BOOT_CMD_MKENT(init, 1, 0, do_lwip_init, + "initialize lwip stack", ""), +#if defined(CONFIG_CMD_PING) + U_BOOT_CMD_MKENT(ping, 2, 0, do_lwip_ping, + "send ICMP ECHO_REQUEST to network host", + "pingAddress"), +#endif +#if defined(CONFIG_CMD_WGET) + U_BOOT_CMD_MKENT(wget, 2, 0, do_lwip_wget, "", ""), +#endif +#if defined(CONFIG_CMD_TFTPBOOT) + U_BOOT_CMD_MKENT(tftp, 3, 0, do_lwip_tftp, + "boot image via network using TFTP protocol\n", + "[loadAddress] [[hostIPaddr:]bootfilename]"), +#endif +#if defined(CONFIG_CMD_DHCP) + U_BOOT_CMD_MKENT(dhcp, 1, 0, _do_lwip_dhcp, + "boot image via network using DHCP/TFTP protocol", + ""), +#endif +#if defined(CONFIG_CMD_DNS) + U_BOOT_CMD_MKENT(dns, 3, 0, do_lwip_dns, + "lookup dns name [and store address at variable]", + ""), +#endif +}; + +static int do_ops(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + struct cmd_tbl *cp; + + cp = find_cmd_tbl(argv[1], cmds, ARRAY_SIZE(cmds)); + + argc--; + argv++; + + if (cp == NULL || argc > cp->maxargs) + return CMD_RET_USAGE; + if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) + return CMD_RET_SUCCESS; + + return cp->cmd(cmdtp, flag, argc, argv); +} + +U_BOOT_CMD( + lwip, 4, 1, do_ops, + "LWIP sub system", + "init - init LWIP\n" + "ping addr - pingAddress\n" + "wget http://IPadress/url/\n" + "tftp [loadAddress] [[hostIPaddr:]bootfilename]\n" + "dhcp - boot image via network using DHCP/TFTP protocol\n" + );