From patchwork Tue Aug 22 09:36:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 715726 Delivered-To: patch@linaro.org Received: by 2002:adf:f747:0:b0:317:ecd7:513f with SMTP id z7csp163994wrp; Tue, 22 Aug 2023 02:39:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEg3XYfoZ6DzfNkSC1gWMFplHcyT4UBRLqlMHcTVRkKgZOljFB0xEMvPibb8jW7+057JN8F X-Received: by 2002:a05:6512:114d:b0:4f8:67f0:7253 with SMTP id m13-20020a056512114d00b004f867f07253mr8543048lfg.49.1692697163094; Tue, 22 Aug 2023 02:39:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692697163; cv=none; d=google.com; s=arc-20160816; b=fpz7x6Wt1E2UEhEPEq/DHPcuZaBdIjoECEIlEam/QNDmYvE0ZmXlCvoYNm1gRFyoBk ij+D7xaD13un/x7GJi3bdDqfhUIwMu8UjE6yhH+Ft8rYTyhWfRJh6+0e/t2xOxCrC0Gx 34JGfNRsEmBerWFSdqq5NB/rc2tMsfH1FNyRWiHfxnEz5/9sHDCr0fIp2ygKzMZwfhQr 4wq+DVX0GUPjh5ZUtkoSoKphAD+lw4JZpXO2o5JeMaQFtTrAARozk2/faqyXfBmrJMoL +/WZ9WfGSaA6tXApG0vRdlvQjDHg/wnGeo7Ep9hhAK8VjzBegEzHx2+NKtEeDuXfKGEg Lc9A== 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=l3zmAwXtqc3guciP0kryq60f+uDEjSPEHUuH41ebGGY=; fh=7lWG6Sv/Sng5Xrn4qLGn/rvsS3kka2hgHZWjTWRbkb8=; b=KDmw2W7LoK2HxGM5HsxcyjGI5SLUFn3mDC2g4IH3n0Wp1/uedPzTwP2Y/innF+NmAP LYHCIEZELH+B6r7pySKvCh2Hk7wr5LdbGI1rNpCVbLR7J++1h3NZDy8HIrzaQ7NabBRG NU3xz1sNuxlHCrXAJAvHU9pzuKXhcc4uxOyawZ9Fjo9e2ruvto2GRW4zqrgX5Q5L5fJH DfSqmd1ZMCg09kNa2/Wtd0Y6YyNRDHfxvDxhorbQdlZEGI4A/QgTSy/LxVZNVov9Ibr5 7845UFBIp9tHxx52N4fllkT8WlCuSzfL5RayxL3Ensl3PyjN2AhxzSZZ4eHByWrh4qM5 Vw5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RO8JZVsz; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id n16-20020a05600c181000b003fed86af7c5si4301035wmp.178.2023.08.22.02.39.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 02:39:23 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RO8JZVsz; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 82BE98648C; Tue, 22 Aug 2023 11:38:37 +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="RO8JZVsz"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 52194864A7; Tue, 22 Aug 2023 11:38:36 +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-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) (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 961018648C for ; Tue, 22 Aug 2023 11:38:33 +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-x22c.google.com with SMTP id 38308e7fff4ca-2bbbda48904so48127031fa.2 for ; Tue, 22 Aug 2023 02:38:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692697112; x=1693301912; 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=l3zmAwXtqc3guciP0kryq60f+uDEjSPEHUuH41ebGGY=; b=RO8JZVszvFlozgS1CXG4+QOXF5V5pfgTluws3SQjuoowSx3EdG8TVhWvy1E09L4edp HoslhiWvvDcxmLlpRlGtiexhtFyC7qfeZKyvuDxZ5YYom3vUTBYIVkvSLyrkPPxe0jyZ LkEim0FkZ4kSJFF0pP1k+ziYDyfFxiaG2MePIK6TkY4lNaaszyQupHW5Z8gidfiRzx06 DG8T6zslpeMwveviqchMWMdvkdhnqdeLyqMvxr7BHI+A0lu2iR4ea02Zvzc3tSUDIN4Y Lzd5aaAnNt6J4VQ2V0GCGG2JucijBv0BPpDSO2Mh6jiEQa1aRcP0HZfQ+GoghyP8Wf/T XJIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692697112; x=1693301912; 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=l3zmAwXtqc3guciP0kryq60f+uDEjSPEHUuH41ebGGY=; b=GsUwtHvFbz7KfYDpZZnLdhtEowhVz05MwFM7ICNWd8iNC5p+1MhqWspNaCPKr45BVO 6R5QiLQY47jct8INAbBqL6NINXTPexPLjRZOZnX/4FEeOyt7ugPXhMc/R9bD6nOdPivn tDZp+aCUkBX5LcQ14Trw2sc2L82HmtzsLA1fhoP/rEdmTU3RHCJrCKNnEMdPHcG6sT9M pOtSZVep+DM+wnb/BywElfMO6UTZ04TyjBK8XqZnkh78rjqNQfuO9O+eXMT2ouR4Zfmo ZBQwV7J/EalmcKMlg3Yw9OriY9v5F63Ex5LCTHeiaRRGtZcFIUe8tcxRhB6EgcPXkVTL dp+Q== X-Gm-Message-State: AOJu0YxgUghteXCW+tcT9g/2/ByEDCoX1/UAgabmxLAVWmJ2wKAhkyBJ CuXAvUbAwYaLHVPzw2jrziJD1Bi5fxSwwFZeLlnAXw== X-Received: by 2002:a2e:91d8:0:b0:2bc:c3c0:a997 with SMTP id u24-20020a2e91d8000000b002bcc3c0a997mr3512761ljg.38.1692697112730; Tue, 22 Aug 2023 02:38:32 -0700 (PDT) Received: from localhost.localdomain ([45.82.14.220]) by smtp.gmail.com with ESMTPSA id i9-20020a2e8089000000b002b724063010sm2649637ljg.47.2023.08.22.02.38.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 02:38:32 -0700 (PDT) From: Maxim Uvarov To: u-boot@lists.denx.de Cc: pbrobinson@gmail.com, ilias.apalodimas@linaro.org, joe.hershberger@ni.com, rfried.dev@gmail.com, trini@konsulko.com, goldsimon@gmx.de, Maxim Uvarov Subject: [PATCHv7 05/15] net/lwip: implement tftp cmd Date: Tue, 22 Aug 2023 15:36:04 +0600 Message-Id: <20230822093614.4717-6-maxim.uvarov@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230822093614.4717-1-maxim.uvarov@linaro.org> References: <20230822093614.4717-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 U-Boot recently got support for an alternative network stack using LWIP. Replace tftp command with the LWIP variant while keeping the output and error messages identical. Signed-off-by: Maxim Uvarov --- include/net/lwip.h | 14 +++- net/lwip/Makefile | 1 + net/lwip/apps/tftp/Makefile | 16 +++++ net/lwip/apps/tftp/lwip-tftp.c | 124 +++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 net/lwip/apps/tftp/Makefile create mode 100644 net/lwip/apps/tftp/lwip-tftp.c diff --git a/include/net/lwip.h b/include/net/lwip.h index 240ebba354..14a65470ee 100644 --- a/include/net/lwip.h +++ b/include/net/lwip.h @@ -28,4 +28,16 @@ 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 + * + * Load file with tftp to specific address + * + * @addr: Address to store downloaded file + * @filename: File name on remote tftp server to download + * + * + * Returns: 0 if success, !0 if error + */ +int ulwip_tftp(ulong addr, const char *filename); diff --git a/net/lwip/Makefile b/net/lwip/Makefile index 59323fb325..0337d82cf5 100644 --- a/net/lwip/Makefile +++ b/net/lwip/Makefile @@ -67,3 +67,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/ diff --git a/net/lwip/apps/tftp/Makefile b/net/lwip/apps/tftp/Makefile new file mode 100644 index 0000000000..0e50fcad45 --- /dev/null +++ b/net/lwip/apps/tftp/Makefile @@ -0,0 +1,16 @@ + +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)/tftp.o: $(obj)/tftp.c +.PHONY: $(obj)/tftp.c +$(obj)/tftp.c: + cp $(srctree)/net/lwip/lwip-external/src/apps/tftp/tftp.c $(obj)/tftp.c + cp $(srctree)/net/lwip/lwip-external/src/include/lwip/apps/tftp_client.h $(obj)/tftp_client.h + cp $(srctree)/net/lwip/lwip-external/src/include/lwip/apps/tftp_common.h $(obj)/tftp_common.h + cp $(srctree)/net/lwip/lwip-external/contrib/examples/tftp/tftp_example.h $(obj)/tftp_example.h + +obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o +obj-$(CONFIG_CMD_TFTPBOOT) += lwip-tftp.o + diff --git a/net/lwip/apps/tftp/lwip-tftp.c b/net/lwip/apps/tftp/lwip-tftp.c new file mode 100644 index 0000000000..e6e4883de8 --- /dev/null +++ b/net/lwip/apps/tftp/lwip-tftp.c @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * (C) Copyright 2023 Linaro Ltd. + */ + +#include +#include +#include +#include + +#include "lwip/apps/tftp_client.h" +#include "lwip/apps/tftp_server.h" +#include + +#include + +#include + +static ulong daddr; +static ulong size; + +static void *tftp_open(const char *fname, const char *mode, u8_t is_write) +{ + LWIP_UNUSED_ARG(mode); + return NULL; +} + +static void tftp_close(void *handle) +{ + log_info("\ndone\n"); + log_info("Bytes transferred = %ld (0x%lx hex)\n", size, size); + + bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done"); + env_set_ulong("filesize", size); + ulwip_exit(0); +} + +static int tftp_read(void *handle, void *buf, int bytes) +{ + return 0; +} + +static int tftp_write(void *handle, struct pbuf *p) +{ + struct pbuf *q; + + for (q = p; q != NULL; q = q->next) { + memcpy((void *)daddr, q->payload, q->len); + daddr += q->len; + size += q->len; + log_info("#"); + } + + return 0; +} + +static void tftp_error(void *handle, int err, const char *msg, int size) +{ + char message[100]; + + LWIP_UNUSED_ARG(handle); + + memset(message, 0, sizeof(message)); + MEMCPY(message, msg, LWIP_MIN(sizeof(message)-1, (size_t)size)); + + log_info("TFTP error: %d (%s)", err, message); +} + +static const struct tftp_context tftp = { + tftp_open, + tftp_close, + tftp_read, + tftp_write, + tftp_error +}; + +int ulwip_tftp(ulong addr, char *fname) +{ + void *f = (void *)0x1; /* unused fake file handle*/ + err_t err; + ip_addr_t srv; + int ret; + char *server_ip; + + if (!fname || addr == 0) + return CMD_RET_FAILURE; + + size = 0; + daddr = addr; + server_ip = env_get("serverip"); + if (!server_ip) { + log_err("error: serverip variable has to be set\n"); + return CMD_RET_FAILURE; + } + + ret = ipaddr_aton(server_ip, &srv); + if (!ret) { + log_err("error: ipaddr_aton\n"); + return CMD_RET_FAILURE; + } + + log_info("TFTP from server %s; our IP address is %s\n", + server_ip, env_get("ipaddr")); + log_info("Filename '%s'.\n", fname); + log_info("Load address: 0x%lx\n", daddr); + log_info("Loading:"); + + bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start"); + + err = tftp_init_client(&tftp); + if (!(err == ERR_OK || err == ERR_USE)) + log_err("tftp_init_client err: %d\n", err); + + err = tftp_get(f, &srv, TFTP_PORT, fname, TFTP_MODE_OCTET); + /* might return different errors, like routing problems */ + if (err != ERR_OK) { + log_err("tftp_get err=%d\n", err); + return CMD_RET_FAILURE; + } + + env_set_hex("fileaddr", addr); + return err; +}