From patchwork Mon Aug 14 13:32:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 713475 Delivered-To: patch@linaro.org Received: by 2002:a5d:4012:0:b0:317:ecd7:513f with SMTP id n18csp1242321wrp; Mon, 14 Aug 2023 06:36:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG8qJYi8SkxGGizjLnBRs18UpV0Uy4zfDxpySTCSR45Udbf3ZuO+YrrtLRr5n7dq7I++IaG X-Received: by 2002:a05:600c:280b:b0:3fb:e1d5:7f48 with SMTP id m11-20020a05600c280b00b003fbe1d57f48mr7679951wmb.5.1692020175274; Mon, 14 Aug 2023 06:36:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692020175; cv=none; d=google.com; s=arc-20160816; b=Am6obIJg1wOVS6lTKK1Ot3XPrS92XWXNz0XeRd/N4pQSA2DRwytMnVUfpTgvhScfFN HVxhfZiLA0p+lnQlOSw6cylVJFPKmSyzzXs4gqc6P5fiYKkg781BSij+STESMi9XJuk0 yIcfCQZWmxV1Aze1ldlqvmgUqxr5942S+NBH92G7hoTWikL9VS4OYlQRlFWa4LK+L0Yn 7C8L86Ey9Nw5EEdhxIsbtcIUIF+XvKKd20ypFfhTbdiEsKsONYOPCNEYRkTZosXfni1E 3xYXhGRcPRWs8+1FSnZbNWZoxYgUaVJh3kllqaMS8SqlD6nw3v1KIGFvyDjURolhu5cX MlyA== 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=yIEJ87jVxrGRTAvkJzgEaHHXvegpju2xj+haGRixQXI=; fh=KONqw1VFBW9nCv9imase+52c+XJ0MeVkzLj6SOLcJqQ=; b=IXCmU2Zzi/0/z+XcDgJUX5clohglWsGewTSXfMHeZnANt4URUOLXnjbrtz9fb2xVvI 8LNgqsdvGnVbuUiNoAQYinVal8FzpeOTUEoZjbpf+uwhpxXIbA5kV1ezT/fdbQtGLzaX UgWEQu+4YSpLa/Lz4NT/fU+HByKW67nyTZhxlswv5upimJd7K4HwwKIyhR49WBaR84vo 3Zl/KmlnQnlKVNXa+pfRZafCHv1anbO2XeWImqUKy8/rbbWWiF1YedxwL52cncVFU8zC mFYZbwuKyk6UCAyP6pvxNDqYE/KPjGo/0CSgzQ3rrexjWi7mFWArvYekVgtQ8d74Gc2m xzuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Y6WBCFsS; 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 n2-20020adff082000000b0031790746dfcsi2745261wro.945.2023.08.14.06.36.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:36:15 -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=Y6WBCFsS; 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 398508698F; Mon, 14 Aug 2023 15:35:30 +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="Y6WBCFsS"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 106A38698E; Mon, 14 Aug 2023 15:35:29 +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-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) (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 123E68696D for ; Mon, 14 Aug 2023 15:35:27 +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-x232.google.com with SMTP id 38308e7fff4ca-2b9e6cc93c6so65251561fa.2 for ; Mon, 14 Aug 2023 06:35:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1692020126; x=1692624926; 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=yIEJ87jVxrGRTAvkJzgEaHHXvegpju2xj+haGRixQXI=; b=Y6WBCFsSn9n0m293329+I9uxh+p843ib7rPTO9qxS/oJ3+L/sVUxThHpJOE26Cocb0 GjANYE3/eCh29OGH8VLJ2QkL9dPfnn4lasgUdvMpQF1l3WxJRZUmSvJyHhLWKPCO4f/R uy3pd9mzkglkftQr/CRdIxpDWsb3TalB0bDy0i7d0N7NfBhMSjWSWbenXwXaaMCVg6Hb f0Nx7qS85wtfkv4dl3tgtoJKton88hNzSZBcoxKhMO3HZMy+5cTVUqWOclCOiStY7Klf zyZVwJJKXb9gRdk+z+g1pI35SgsFa3CqX21AEOUehQ5ZsfGdHgZ5p179cvqSC9+gKO/l Y2lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692020126; x=1692624926; 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=yIEJ87jVxrGRTAvkJzgEaHHXvegpju2xj+haGRixQXI=; b=jSBNVvp8Aw/Xv0Ss3Qy5DO79iAR+/lfwzFjUmmolC4LHe4CwQCHkU6QtTMBnU9vWRA pisPoxIkuDutf89Zuj5G0WUFHQwl7KcHn6M3ph8QFN4gwbH7ZgoBoZUQLI8AvFAg/vXF u2kbzPswqR4uqexRerOV6J4K643A2iYII8hLpODBt6NEPTHcOvBU6KpW5gYhYU9/BJQW qPX6tDLVK2sDPD+U+A2Hl9RAVu9qRPr4C8n/u/ii4cSWWf0t5ndyo0WkPcpPWwalxikR 4dilxGeQkJFYhN8CG0IcXNkha3ln+nCu0moPPAv1M36lXQrifEqsDysBBtdW3SJ6c8eV LW4Q== X-Gm-Message-State: AOJu0YyqzloXyn0W4eb5qOZ98EtJ2JWhzHJf1YgCcbruSMgk8cC6XtUT HNS3fRjzdDkn9tWoqbqVuk4zeV3K8Ghyzn+dUSg= X-Received: by 2002:a05:651c:14a:b0:2b9:d3b6:769e with SMTP id c10-20020a05651c014a00b002b9d3b6769emr6372335ljd.24.1692020126243; Mon, 14 Aug 2023 06:35:26 -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.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 06:35:25 -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 06/14] net/lwip: implement wget cmd Date: Mon, 14 Aug 2023 19:32:45 +0600 Message-Id: <20230814133253.4150-7-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 Implement function for wget command with lwIP variant. Usage and output is the same as the original command. This code called by compatibility code between U-Boot and lwIP. Signed-off-by: Maxim Uvarov --- include/net/lwip.h | 13 ++++ net/lwip/Makefile | 1 + net/lwip/apps/http/Makefile | 13 ++++ net/lwip/apps/http/lwip-wget.c | 131 +++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 net/lwip/apps/http/Makefile create mode 100644 net/lwip/apps/http/lwip-wget.c diff --git a/include/net/lwip.h b/include/net/lwip.h index 2f035280eb..f1f2c94fc2 100644 --- a/include/net/lwip.h +++ b/include/net/lwip.h @@ -25,3 +25,16 @@ int ulwip_dns(char *name, char *varname); * Other value < 0, if error */ int ulwip_dhcp(void); + +/* +* This function creates the HTTP request to download file from url to the address +* specified in parameters. After this function you need to invoke the polling +* loop to process network communication. +* +* +* @addr start address to download result +* @url url in format http://host[:port]/url +* Return: 0 for success +* !0 if error +*/ +int ulwip_wget(ulong addr, char *url); diff --git a/net/lwip/Makefile b/net/lwip/Makefile index 0337d82cf5..4c6df94807 100644 --- a/net/lwip/Makefile +++ b/net/lwip/Makefile @@ -68,3 +68,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/ +obj-$(CONFIG_CMD_WGET) += apps/http/ diff --git a/net/lwip/apps/http/Makefile b/net/lwip/apps/http/Makefile new file mode 100644 index 0000000000..3e92b0ef1b --- /dev/null +++ b/net/lwip/apps/http/Makefile @@ -0,0 +1,13 @@ +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)/http_clinet.o: $(obj)/http_client.c +.PHONY: $(obj)/http_client.c +$(obj)/http_client.c: + cp $(srctree)/net/lwip/lwip-external/src/apps/http/http_client.c $(obj)/http_client.c + cp $(srctree)/net/lwip/lwip-external/src/include/lwip/apps/http_client.h $(obj)/http_client.h + +obj-$(CONFIG_CMD_WGET) += http_client.o +obj-$(CONFIG_CMD_WGET) += lwip-wget.o + diff --git a/net/lwip/apps/http/lwip-wget.c b/net/lwip/apps/http/lwip-wget.c new file mode 100644 index 0000000000..73d82225a2 --- /dev/null +++ b/net/lwip/apps/http/lwip-wget.c @@ -0,0 +1,131 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * (C) Copyright 2023 Linaro Ltd. + */ + +#include +#include +#include +#include + +#include "http_client.h" +#include + +static ulong daddr; +static httpc_connection_t settings; + +#define SERVER_NAME_SIZE 200 + +static err_t httpc_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *p, + err_t unused_err) +{ + struct pbuf *q; + + if (!p) + return ERR_BUF; + + for (q = p; q != NULL; q = q->next) { + memcpy((void *)daddr, q->payload, q->len); + log_debug("downloaded chunk size %d, to addr 0x%lx\n", q->len, daddr); + daddr += q->len; + } + altcp_recved(pcb, p->tot_len); + pbuf_free(p); + return ERR_OK; +} + +static void httpc_result(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, + u32_t srv_res, err_t err) +{ + if (httpc_result == HTTPC_RESULT_OK) { + log_info("\n%d bytes successfully downloaded.\n", rx_content_len); + env_set_ulong("filesize", rx_content_len); + ulwip_exit(0); + } else { + log_err("\nhttp eroror: %d\n", httpc_result); + ulwip_exit(-1); + } +} + +/* http://hostname:port/url */ +static int parse_url(char *url, char *host, u16 *port) +{ + char *p, *pp; + + p = strstr(url, "http://"); + if (!p) { + printf("err: no http://!\n"); + return -1; + } + + p += strlen("http://"); + + /* parse hostname */ + pp = strchr(p, ':'); + if (pp) { +#define PORT_STR_SIZE 5 + char portstr[PORT_STR_SIZE]; + + if (pp - p >= SERVER_NAME_SIZE) + return -2; + memcpy(host, p, pp - p); + host[pp - p + 1] = '\0'; + + p = pp + 1; + pp = strchr(p, '/'); + if (!pp) { + printf("wrong url\n"); + return -3; + } + + if (pp - p >= PORT_STR_SIZE) + return -4; + memcpy(portstr, p, pp - p); + portstr[pp - p] = '\0'; + *port = (u16)dectoul(portstr, NULL); + } else { + pp = strchr(p, '/'); + if (!pp) { + printf("wrong url\n"); + return -5; + } + + if (pp - p >= SERVER_NAME_SIZE) + return -6; + memcpy(host, p, pp - p); + host[pp - p + 1] = '\0'; + *port = 80; /* default */ + } + + return 0; +} + +int ulwip_wget(ulong addr, char *url) +{ + err_t err; + u16 port; + char server_name[SERVER_NAME_SIZE]; + httpc_state_t *connection; + + daddr = addr; + + err = parse_url(url, server_name, &port); + if (err) { + log_err("error parse_url\n"); + return -1; + } + + log_info("downloading %s to addr 0x%lx\n", url, addr); + memset(&settings, 0, sizeof(settings)); + settings.result_fn = httpc_result; + err = httpc_get_file_dns(server_name, port, url, &settings, + httpc_recv, NULL, &connection); + if (err != ERR_OK) { + log_err("httpc_init_connection failed\n"); + return err; + } + + env_set_hex("fileaddr", addr); + return 0; +}