From patchwork Wed Aug 2 14:06: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: 709169 Delivered-To: patch@linaro.org Received: by 2002:a5d:55c5:0:b0:317:2194:b2bc with SMTP id i5csp930934wrw; Wed, 2 Aug 2023 07:10:19 -0700 (PDT) X-Google-Smtp-Source: APBJJlHDLu2x/veGSOyXNvuHKuhGHD4rWQol6qIPxAhj1OOE5uBaXOxTnlrbyppMl81+kVfEvn/f X-Received: by 2002:a05:600c:2948:b0:3fe:2b8c:9f07 with SMTP id n8-20020a05600c294800b003fe2b8c9f07mr3384953wmd.1.1690985419290; Wed, 02 Aug 2023 07:10:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690985419; cv=none; d=google.com; s=arc-20160816; b=Ag46J0LS613Ch+cY98v+ng74ukghxHpnZiXztAj5Gzi4Iedu+ySGqwS3mb7VQMWiDY vgR5U9lqIFFzs9z1FVlclgJaVS8xr9S4Ik1W0Vxdia/1CQ+c86/Lnsn/t0eKgsd/rK9H BaRa6/6rD53ZBu97S0nNwIhzvGpcGpUc6HCa3Ya3ShG6QJVTRqH6WQCir3dac2h0uMpl eYafrUJ6ll3KmcFn1zAUJzCO0YxBWRxlK/kv+ZYC6t5eIJpTDr9YSyCmY+mbKpQL53wX eznTMeB3qOsv94aFbGZJEomdrnaK9E0/O9bD63Z6YP9ZNbYbG18j/UXIGDH1TtdJHd0Y htmQ== 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=xwLXe18a4WYA06Yd9AyLjmRg+DGIXrFk3hfuX+AV2wY=; fh=KONqw1VFBW9nCv9imase+52c+XJ0MeVkzLj6SOLcJqQ=; b=MfdHdPLQcKo247WQQKiaBHWzXUOu2lUMTCtmSeT8hUSBGLYF96LpqOWUUeY1yUAcq+ aGbXaAU2Kuu7gLTrYpvIvd3pqYa/ndwYYH9MRejONtbCWCqxzlsZyzypah47+JjjdX8d t33eZSB0/65XWnJJ/NlVm4iDfpIQkqVtNSZFonZk4TnEKu47LMLSyoFu69/HiUmoWoax hALwQP3NpCzrIO4wNzeSgeENBwTeMjiokLkhciCDmQUxli20fUqGYJR3dIDh7mu9Cw5l 20AE2bjmrBANTWF9bAk5q48sywTH4rYYmJcmvRJKxbXSJA3YNax0bUn5nnqHvhMAsok2 UlCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="riocfqA/"; 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 j17-20020a05600c485100b003fe28e72c93si955170wmo.109.2023.08.02.07.10.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 07:10:19 -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="riocfqA/"; 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 EC6B886ACB; Wed, 2 Aug 2023 16:09:38 +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="riocfqA/"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 667E186C81; Wed, 2 Aug 2023 16:09:37 +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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) (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 E345386898 for ; Wed, 2 Aug 2023 16:09:34 +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-lf1-x12a.google.com with SMTP id 2adb3069b0e04-4fe3678010eso5661705e87.3 for ; Wed, 02 Aug 2023 07:09:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690985374; x=1691590174; 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=xwLXe18a4WYA06Yd9AyLjmRg+DGIXrFk3hfuX+AV2wY=; b=riocfqA/vQ99yvNnXRA8kvxrJhJG3WxA69xgb5r5gFWRZN5ZfE7c/si6XPM69gk7pu lu77lBNNqKet2mlacVv3iQb5U4ZeCyq//j0rtozJvVk0FbbU1H6zR6OZY+/G2JGqJTxu qlWuZDE7zY4vxaSUlVLdLTI1/XVobDXaoWnqAi5djSXK7X9WY2msZFNcP2eBjbIMdc/a 5cQmmgACWpHpur/I2fDemB7oI/NtSSKpIfl8HkUFjfoVUTsnRZ6Zof1MytarcrqbsH3H bCef3yIQqhP84MPbxdnh4ANHxzMBoSNqYjJFWF9mbWnTLYb7ScPeKcUNUhAXFlvApPjQ qTeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690985374; x=1691590174; 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=xwLXe18a4WYA06Yd9AyLjmRg+DGIXrFk3hfuX+AV2wY=; b=Qo+3qfrYJtVDWXBvmLPGiShhXKXcwT4jhQF9SNvT2ArbfPO6qIz2U1IRRHMP7tCBFS oYiNIFic7uv+KtR7NKUVE6GscWYXJzSKnwr02gKwu1NsIH2qBoPPcqw5i50G0XWiAJ0Q iMNKz1Cx3/qMTyNCOoq80jKQ8SI/yylI0u3r/LX+M8PU6J3kJx2jTa5X4au862tXzjcI Tj7EQ1x7NT1n+ZBJ38AGeDIpNou6eGcA9HFahYxJ6AVgdYmkGtZR7363laupLhzWrX56 TzoIFms2Vs9e3/fd4+bpi1rNIbTfXkmCh7wzXGnlNHLylovYbc+smekltziC8G1A2aQa pWcw== X-Gm-Message-State: ABy/qLaIufrND35TIoLOiEgUR2Fxf2JZSaFVBJXDyIFTZND0C19Dhs+x 8hLQfubfUssZCgLe9y9/CmL04D1CV1MS09BcN1awrg== X-Received: by 2002:a05:6512:2019:b0:4fd:d078:2e3f with SMTP id a25-20020a056512201900b004fdd0782e3fmr4250101lfb.42.1690985373639; Wed, 02 Aug 2023 07:09:33 -0700 (PDT) Received: from localhost.localdomain ([45.82.14.220]) by smtp.gmail.com with ESMTPSA id a12-20020a056512020c00b004fbbef8d4dbsm2982026lfo.121.2023.08.02.07.09.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 07:09:32 -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: [PATCHv5 05/13] net/lwip: implement tftp cmd Date: Wed, 2 Aug 2023 20:06:50 +0600 Message-Id: <20230802140658.10319-6-maxim.uvarov@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230802140658.10319-1-maxim.uvarov@linaro.org> References: <20230802140658.10319-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 --- lib/lwip/Makefile | 1 + lib/lwip/apps/tftp/Makefile | 16 +++++ lib/lwip/apps/tftp/lwip-tftp.c | 124 +++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 lib/lwip/apps/tftp/Makefile create mode 100644 lib/lwip/apps/tftp/lwip-tftp.c diff --git a/lib/lwip/Makefile b/lib/lwip/Makefile index a3521a9d18..1893162c1a 100644 --- a/lib/lwip/Makefile +++ b/lib/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/lib/lwip/apps/tftp/Makefile b/lib/lwip/apps/tftp/Makefile new file mode 100644 index 0000000000..299c95e9c0 --- /dev/null +++ b/lib/lwip/apps/tftp/Makefile @@ -0,0 +1,16 @@ + +ccflags-y += -I$(srctree)/lib/lwip/port/include +ccflags-y += -I$(srctree)/lib/lwip/lwip-external/src/include -I$(srctree)/lib/lwip +ccflags-y += -I$(obj) + +$(obj)/tftp.o: $(obj)/tftp.c +.PHONY: $(obj)/tftp.c +$(obj)/tftp.c: + cp $(srctree)/lib/lwip/lwip-external/src/apps/tftp/tftp.c $(obj)/tftp.c + cp $(srctree)/lib/lwip/lwip-external/src/include/lwip/apps/tftp_client.h $(obj)/tftp_client.h + cp $(srctree)/lib/lwip/lwip-external/src/include/lwip/apps/tftp_common.h $(obj)/tftp_common.h + cp $(srctree)/lib/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/lib/lwip/apps/tftp/lwip-tftp.c b/lib/lwip/apps/tftp/lwip-tftp.c new file mode 100644 index 0000000000..511d82e600 --- /dev/null +++ b/lib/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 "lwip/apps/tftp_client.h" +#include "lwip/apps/tftp_server.h" +#include + +#include + +#include "../../../lwip/ulwip.h" + +#if LWIP_UDP + +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) +{ + printf("\ndone\n"); + printf("Bytes transferred = %ld (0x%lx hex)\n", size, size); + + 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); + /* printf("downloaded chunk size %d, to addr 0x%lx\n", q->len, daddr); */ + daddr += q->len; + size += q->len; + printf("#"); + } + + return 0; +} + +/* For TFTP client only */ +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)); + + printf("TFTP error: %d (%s)", err, message); +} + +static const struct tftp_context tftp = { + tftp_open, + tftp_close, + tftp_read, + tftp_write, + tftp_error +}; + +int lwip_tftp(ulong addr, char *fname) +{ + void *f = (void *)0x1; /*fake 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) { + printf("error: serverip variable has to be set\n"); + return CMD_RET_FAILURE; + } + + ret = ipaddr_aton(server_ip, &srv); + LWIP_ASSERT("ipaddr_aton failed", ret == 1); + + printf("TFTP from server %s; our IP address is %s\n", + server_ip, env_get("ipaddr")); + printf("Filename '%s'.\n", fname); + printf("Load address: 0x%lx\n", daddr); + printf("Loading:"); + + err = tftp_init_client(&tftp); + if (!(err == ERR_OK || err == ERR_USE)) + printf("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) { + printf("tftp_get err=%d\n", err); + } + LWIP_ASSERT("tftp_get failed", err == ERR_OK); + + env_set_hex("fileaddr", addr); + return err; +} +#else +#error "UDP has to be supported" +#endif /* LWIP_UDP */