From patchwork Fri Aug 2 13:14:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 816241 Delivered-To: patch@linaro.org Received: by 2002:adf:e00d:0:b0:367:895a:4699 with SMTP id s13csp270474wrh; Fri, 2 Aug 2024 06:16:20 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW4jhUEoxvmEmknzmlwnoZYaLIzjEaUWf58FwOQ1lvNAMTHGsVyNP0itxC0MPQ75pJqX+Mp1gFBDuJR9hCUopVh X-Google-Smtp-Source: AGHT+IHybBl17Q49dAT/Zhvx5LAQh9+Odn7X2pTvpKQTujUyrHl8JWjVXLk7yEQIBgx1gvMJv1+r X-Received: by 2002:a17:907:5c7:b0:a72:7b17:5d68 with SMTP id a640c23a62f3a-a7dc5f6b5a0mr243024166b.3.1722604580136; Fri, 02 Aug 2024 06:16:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722604580; cv=none; d=google.com; s=arc-20160816; b=Wwjg0voODU5fKQ/cip8hMmrgeNcULYWJuBRl23QDhGXqyi3vCmbcKXKemw2N62cdsO ATwwCbiIEBqf5xODQf+lejCNh1Q4ecJzOT5vg+Wk+c4b7FJQ58zkQOuAuN6tbwaewLgG HiY+Z6h0ebvv+4GN1xkeQHWt1QdkXUJktW5GLwNq6VIDUIMLUZZ/816EJfRiVASUzR/S pNg5XBYt5C4UiVEIKKez8kE8D0XsaxxM/Zlc9K63UleYhUG074ztDs7tw8WBVEJVnua5 8dBqnHnAEe89J4Lf8ELsg9NB34YQlMOvktMZgkuXClLW5mbbWc8EaxNPBscgNVh9EQGK cVXw== 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=TERqOuMzDcSX6pRz3xpvZX8Z6PpIvLLKKtMjLzdRCHw=; fh=CAnAdQnL3ihnB+hYxZaIHiPaZmjwyvF5RHKiALJ4T80=; b=bsyOx5Ukx8BVeW5SYHwVJq52iso2A1yqYAgWwp/hJk00lZME2Xyy0N3vqP+x2xHA3J U4d2rtvXu4h9csNImbWcwC3fRI0COt+7r40aduChp/f5CWdaIjD0FQDs1uq+BGYX9E+R RJN2frvRSuqsyM5cSNkIPi/irebnT3zKyTgDHRBxbuVB81habE1d53qM5AzAhaNouez5 fjLT9dr/P6IVh6YgSdcOBYkZ4a0tUG5wYK0ssfacQrG0ZiYCnZFQG0uu+ykW4xVPLN88 MSADRWz+nhJMTJh2ySKDtgMVorlPrMWJdrPCeexyviqEJCkmmwvroNLuTBs32y0unC3h AqyQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kpZJ8gWo; 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; dara=neutral header.i=@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 a640c23a62f3a-a7dc9d6f995si107922266b.363.2024.08.02.06.16.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 06:16:20 -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=kpZJ8gWo; 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; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9D97088BF5; Fri, 2 Aug 2024 15:15:10 +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="kpZJ8gWo"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BD9D388BEA; Fri, 2 Aug 2024 15:15:08 +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-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) (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 9419B88B9B for ; Fri, 2 Aug 2024 15:15:06 +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=jerome.forissier@linaro.org Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-427b1d4da32so18590945e9.0 for ; Fri, 02 Aug 2024 06:15:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722604506; x=1723209306; darn=lists.denx.de; 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=TERqOuMzDcSX6pRz3xpvZX8Z6PpIvLLKKtMjLzdRCHw=; b=kpZJ8gWoXLxl2b7HXRaPdHqO1Z41mmmMcx4m2EB5HTIfdchrg/yRLzufjLZTeQR/qj LZ/WoC+i678fxy0SeVQ5J2oYOg+L2RAlHVZ34gJbbL8+YbELARGeE2cJ9w/PyssJlWEq E52rEI35fNQOStJC0HvyDw6xvNkcHXRKh9JtbdDtll5FoMh5Jt1KwynhtG7tCVKrPIaC uDVfaPc+kx0Hr7jP2h1CeLeQtATwF6/tptbAlSQPdjrZw8YC1gB1G6lwwgeR7CUDGKJS VOldK4DZXiZoIWklnQXCJn5O7NDktyTOqLlBSCRYIEmiFQFiG1RQ3Fs+wOaBQ/pBGZAt PDXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722604506; x=1723209306; 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=TERqOuMzDcSX6pRz3xpvZX8Z6PpIvLLKKtMjLzdRCHw=; b=W9Teuyl/kMZGB2iV9Wg8TtwBMZysJi7EgLUHjy35t2BGebBt5t+sNFn5xyeVccTlBG osqsGT6XzfE/j/TIpYeViye772UDf9unNN4/ONAOaQyK4P3oJhnFaC/0vEFwqgcmJcGT B7OlNul7oUw3wId2d8bBv4lx0oycEA07/7/HKyaq0cv/v0jJMLDyyiUlgRvRZFoZUHTu 85cZZaKjwXfUyAjIlCj7vRrWzy1HuKOj1exPDOV2RVIuCCKUrn8JryW+P5MkQrRa8M5S kBMEFGb/6lk15OT2kb3bvC6aufbBqM3lDxPf7+ZjHLwGb3dM8yTrdqKkECEexT8vBypN KyOQ== X-Gm-Message-State: AOJu0Yx+46JssZYFzb46kqMw+F4/FejZ+iwLII3mqYPJlwL6eNdKegg+ WztcyrlFXLnw1DeEllcTBCWpJDY99k132QYy/Qgdp3ZHy8amxPv64oYlXWlvSGQflcbICKePmqr q/oCfzCJA X-Received: by 2002:a05:6000:144d:b0:367:895f:619e with SMTP id ffacd0b85a97d-36bb359d1camr4651249f8f.11.1722604505716; Fri, 02 Aug 2024 06:15:05 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:4fc9:a3ab:c134:cdf4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36bbcf0cc58sm1970304f8f.2.2024.08.02.06.15.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 06:15:05 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Javier Tia , Raymond Mao , Maxim Uvarov , Tim Harvey , Jerome Forissier , Tom Rini , Joe Hershberger , Ramon Fried , Simon Glass , Mattijs Korpershoek , AKASHI Takahiro , Heinrich Schuchardt , Francis Laniel , Jonathan Humphreys Subject: [PATCH v6 13/20] net-lwip: add wget command Date: Fri, 2 Aug 2024 15:14:00 +0200 Message-Id: <87b0eed7e9436840ef978745b5035c9952779d0b.1722601509.git.jerome.forissier@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: 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 Add support for the wget command with NET_LWIP. Based on code initially developed by Maxim U. Signed-off-by: Jerome Forissier Co-developed-by: Maxim Uvarov Cc: Maxim Uvarov Signed-off-by: Jerome Forissier --- cmd/Kconfig | 16 +-- cmd/net-lwip.c | 8 ++ include/net-lwip.h | 18 +++ net/lwip/Makefile | 1 + net/lwip/wget.c | 278 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 314 insertions(+), 7 deletions(-) create mode 100644 net/lwip/wget.c diff --git a/cmd/Kconfig b/cmd/Kconfig index c53ec2b2a14..81e4ae698bd 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1986,13 +1986,6 @@ config SYS_DISABLE_AUTOLOAD is complete. Enable this option to disable this behavior and instead require files to be loaded over the network by subsequent commands. -config CMD_WGET - bool "wget" - select PROT_TCP - help - wget is a simple command to download kernel, or other files, - from a http server over TCP. - config CMD_PING6 bool "ping6" depends on IPV6 @@ -2107,6 +2100,15 @@ config CMD_TFTPBOOT help tftpboot - load file via network using TFTP protocol +config CMD_WGET + bool "wget" + select PROT_TCP if NET + select PROT_TCP_LWIP if NET_LWIP + select PROT_DNS_LWIP if NET_LWIP + help + wget is a simple command to download kernel, or other files, + from a http server over TCP. + endif # if CMD_NET endif # if NET || NET_LWIP diff --git a/cmd/net-lwip.c b/cmd/net-lwip.c index c021da6a674..42f8bd6b259 100644 --- a/cmd/net-lwip.c +++ b/cmd/net-lwip.c @@ -35,3 +35,11 @@ U_BOOT_CMD( "hostname [envvar]" ); #endif + +#if defined(CONFIG_CMD_WGET) +U_BOOT_CMD( + wget, 3, 1, do_wget, + "boot image via network using HTTP protocol", + "[loadAddress] URL" +); +#endif diff --git a/include/net-lwip.h b/include/net-lwip.h index cb60537f9f3..f5d9255ccaf 100644 --- a/include/net-lwip.h +++ b/include/net-lwip.h @@ -12,8 +12,26 @@ void net_lwip_remove_netif(struct netif *netif); struct netif *net_lwip_get_netif(void); int net_lwip_rx(struct udevice *udev, struct netif *netif); +/** + * wget_with_dns() - runs dns host IP address resulution before wget + * + * @dst_addr: destination address to download the file + * @uri: uri string of target file of wget + * Return: downloaded file size, negative if failed + */ + +int wget_with_dns(ulong dst_addr, char *uri); +/** + * wget_validate_uri() - varidate the uri + * + * @uri: uri string of target file of wget + * Return: true if uri is valid, false if uri is invalid + */ +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_ping(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[]); #endif /* __NET_LWIP_H__ */ diff --git a/net/lwip/Makefile b/net/lwip/Makefile index 645d8a8c457..f2558f8763a 100644 --- a/net/lwip/Makefile +++ b/net/lwip/Makefile @@ -5,3 +5,4 @@ obj-$(CONFIG_CMD_DHCP) += dhcp.o obj-$(CONFIG_CMD_DNS) += dns.o obj-$(CONFIG_CMD_PING) += ping.o obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o +obj-$(CONFIG_CMD_WGET) += wget.o diff --git a/net/lwip/wget.c b/net/lwip/wget.c new file mode 100644 index 00000000000..65a69fab6e8 --- /dev/null +++ b/net/lwip/wget.c @@ -0,0 +1,278 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Copyright (C) 2024 Linaro Ltd. */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define SERVER_NAME_SIZE 200 +#define HTTP_PORT_DEFAULT 80 +#define PROGRESS_PRINT_STEP_BYTES (100 * 1024) + +enum done_state { + NOT_DONE = 0, + SUCCESS = 1, + FAILURE = 2 +}; + +struct wget_ctx { + ulong daddr; + ulong saved_daddr; + ulong size; + ulong prevsize; + ulong start_time; + enum done_state done; +}; + +static int parse_url(char *url, char *host, u16 *port, char **path) +{ + char *p, *pp; + long lport; + + p = strstr(url, "http://"); + if (!p) { + log_err("only http:// is supported\n"); + return -EINVAL; + } + + p += strlen("http://"); + + /* Parse hostname */ + pp = strchr(p, ':'); + if (!pp) + pp = strchr(p, '/'); + if (!pp) + return -EINVAL; + + if (p + SERVER_NAME_SIZE <= pp) + return -EINVAL; + + memcpy(host, p, pp - p); + host[pp - p] = '\0'; + + if (*pp == ':') { + /* Parse port number */ + p = pp + 1; + lport = simple_strtol(p, &pp, 10); + if (pp && *pp != '/') + return -EINVAL; + if (lport > 65535) + return -EINVAL; + *port = (u16)lport; + } else { + *port = HTTP_PORT_DEFAULT; + } + if (*pp != '/') + return -EINVAL; + *path = pp; + + return 0; +} + +static err_t httpc_recv_cb(void *arg, struct altcp_pcb *pcb, struct pbuf *pbuf, + err_t err) +{ + struct wget_ctx *ctx = arg; + struct pbuf *buf; + + if (!pbuf) + return ERR_BUF; + + for (buf = pbuf; buf; buf = buf->next) { + memcpy((void *)ctx->daddr, buf->payload, buf->len); + ctx->daddr += buf->len; + ctx->size += buf->len; + if (ctx->size - ctx->prevsize > PROGRESS_PRINT_STEP_BYTES) { + printf("#"); + ctx->prevsize = ctx->size; + } + } + + altcp_recved(pcb, pbuf->tot_len); + pbuf_free(pbuf); + return ERR_OK; +} + +static void httpc_result_cb(void *arg, httpc_result_t httpc_result, + u32_t rx_content_len, u32_t srv_res, err_t err) +{ + struct wget_ctx *ctx = arg; + ulong elapsed; + + if (httpc_result != HTTPC_RESULT_OK) { + log_err("\nHTTP client error %d\n", httpc_result); + ctx->done = FAILURE; + return; + } + + elapsed = get_timer(ctx->start_time); + if (rx_content_len > PROGRESS_PRINT_STEP_BYTES) + printf("\n"); + printf("%u bytes transferred in %lu ms (", rx_content_len, + get_timer(ctx->start_time)); + print_size(rx_content_len / elapsed * 1000, "/s)\n"); + + if (env_set_hex("filesize", rx_content_len) || + env_set_hex("fileaddr", ctx->saved_daddr)) { + log_err("Could not set filesize or fileaddr\n"); + ctx->done = FAILURE; + return; + } + + ctx->done = SUCCESS; +} + +static int wget_loop(struct udevice *udev, ulong dst_addr, char *uri) +{ + char server_name[SERVER_NAME_SIZE]; + httpc_connection_t conn; + httpc_state_t *state; + struct netif *netif; + struct wget_ctx ctx; + char *path; + u16 port; + + ctx.daddr = dst_addr; + ctx.saved_daddr = dst_addr; + ctx.done = NOT_DONE; + ctx.size = 0; + ctx.prevsize = 0; + + if (parse_url(uri, server_name, &port, &path)) + return CMD_RET_USAGE; + + netif = net_lwip_new_netif(udev); + if (!netif) + return -1; + + memset(&conn, 0, sizeof(conn)); + conn.result_fn = httpc_result_cb; + ctx.start_time = get_timer(0); + if (httpc_get_file_dns(server_name, port, path, &conn, httpc_recv_cb, + &ctx, &state)) { + net_lwip_remove_netif(netif); + return CMD_RET_FAILURE; + } + + while (!ctx.done) { + net_lwip_rx(udev, netif); + sys_check_timeouts(); + if (ctrlc()) + break; + } + + net_lwip_remove_netif(netif); + + if (ctx.done == SUCCESS) + return 0; + + return -1; +} + +int wget_with_dns(ulong dst_addr, char *uri) +{ + eth_set_current(); + + return wget_loop(eth_get_dev(), dst_addr, uri); +} + +int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) +{ + char *end; + char *url; + ulong dst_addr; + + if (argc < 2 || argc > 3) + return CMD_RET_USAGE; + + dst_addr = hextoul(argv[1], &end); + if (end == (argv[1] + strlen(argv[1]))) { + if (argc < 3) + return CMD_RET_USAGE; + url = argv[2]; + } else { + dst_addr = image_load_addr; + url = argv[1]; + } + + if (wget_with_dns(dst_addr, url)) + return CMD_RET_FAILURE; + + return CMD_RET_SUCCESS; +} + +/** + * wget_validate_uri() - validate the uri for wget + * + * @uri: uri string + * + * This function follows the current U-Boot wget implementation. + * scheme: only "http:" is supported + * authority: + * - user information: not supported + * - host: supported + * - port: not supported(always use the default port) + * + * Uri is expected to be correctly percent encoded. + * This is the minimum check, control codes(0x1-0x19, 0x7F, except '\0') + * and space character(0x20) are not allowed. + * + * TODO: stricter uri conformance check + * + * Return: true on success, false on failure + */ +bool wget_validate_uri(char *uri) +{ + char c; + bool ret = true; + char *str_copy, *s, *authority; + + for (c = 0x1; c < 0x21; c++) { + if (strchr(uri, c)) { + log_err("invalid character is used\n"); + return false; + } + } + if (strchr(uri, 0x7f)) { + log_err("invalid character is used\n"); + return false; + } + + if (strncmp(uri, "http://", 7)) { + log_err("only http:// is supported\n"); + return false; + } + str_copy = strdup(uri); + if (!str_copy) + return false; + + s = str_copy + strlen("http://"); + authority = strsep(&s, "/"); + if (!s) { + log_err("invalid uri, no file path\n"); + ret = false; + goto out; + } + s = strchr(authority, '@'); + if (s) { + log_err("user information is not supported\n"); + ret = false; + goto out; + } + s = strchr(authority, ':'); + if (s) { + log_err("user defined port is not supported\n"); + ret = false; + goto out; + } + +out: + free(str_copy); + + return ret; +}