From patchwork Wed Oct 9 14:49:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 833955 Delivered-To: patch@linaro.org Received: by 2002:adf:a1de:0:b0:37d:45d0:187 with SMTP id v30csp149957wrv; Wed, 9 Oct 2024 07:51:03 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXaBr+7zmr2eg+t6WybfwzfAYkbIQi1TfYFSQdfG74xAfnlmaEHuwcQXUkBsMeuz/arfYOf0A==@linaro.org X-Google-Smtp-Source: AGHT+IFN67xZqV86X0bMo09/AY8cgzp4olkBH/IryDFFa0I8KThJTQ1RW8AXpiNIkZBy8B+D/Joj X-Received: by 2002:a05:600c:4ec7:b0:42c:b7f9:4bbd with SMTP id 5b1f17b1804b1-430d59b780amr22843585e9.26.1728485463583; Wed, 09 Oct 2024 07:51:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1728485463; cv=none; d=google.com; s=arc-20240605; b=NWznSIULGF5m/9ejh6x5mnWM8CLOjSq0Yf8g4gpydyAiTSrNw08M+3b3GI8YuM9aZP VhtNwMS3SyKzsvrngJ0TaM7eTmvRVGQeHDp3AeaIIsVuXqDdI5ed7qlMZn2x1+1Whpum Jjm6WOCwvqWIgth/xtlf1q/OvKSsb7RvVz8hBE+Hu5kz2HPJiAqfmiro71+X6M/i7Gb5 Ks5NQh40Bhw+g9j4GR26e9XijztUq9NRzFUcGwSQA5tooG6+5NjQlfihU+8WZ3zJHxXr 9J34yFIK5Mbe2owYfSIIdhfqpApFAJgAf59mMqTsF3ABU+74RoTsPr3G9LlO8aQ7GPDl WX3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=SGLcIoQahP9n/OezrkoG88srol0tiav77UVhbXvA3KM=; fh=YSEEmLgwJ+aFbmJasvQUhoSq0gy6lRcaCWfpOo0Jz+0=; b=WPJUAX2OTxgTai736OIhpe2qjiwiTPf967lJNytbU6E6nc9zE6qgUmo6xcWMem/rUP pCmC800duNLDyhmq/1dkTkWCf9iJMDW4//U91cGDLlg4RGJOcjdThdbZjhpsaGWVS4Om dryqNocyT2mA8mNRqa+Lz6nFiAl5DLcm3I0U6yvJBbHfkMNkOoLXuQqBUL2fNLRpInZX rvOhTBWkJOpaq+XV4JVTmrlIWLgViMoFqYVYSQPKcUAh8ciK2y+CuwQbxFKbx3j6/b4L FNlg7JWf666/22wkMlhQiMEFY00C19pfaltK8x92iFsXrfGpXhMGQx362KSGGR/0xYiu 3NEw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Bzk8kktM; 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 ffacd0b85a97d-37d330c9ee5si1438732f8f.882.2024.10.09.07.51.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Oct 2024 07:51:03 -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=Bzk8kktM; 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 D8F0A88F5B; Wed, 9 Oct 2024 16:50:32 +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="Bzk8kktM"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6B4CA88EE2; Wed, 9 Oct 2024 16:50:31 +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,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) (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 0404B88DC1 for ; Wed, 9 Oct 2024 16:50:29 +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-x32d.google.com with SMTP id 5b1f17b1804b1-4311420b675so4596755e9.2 for ; Wed, 09 Oct 2024 07:50:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1728485428; x=1729090228; 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=SGLcIoQahP9n/OezrkoG88srol0tiav77UVhbXvA3KM=; b=Bzk8kktM8yUqnVF/rtHeST7B5KD669hODfSqvRe3lUvxNE4Agyz4bRw7GNcmllY2kQ thpeqEBNw+WEpFEzaKhcBoHNmJH3tJqy77Gug/JKkv/4jGVuY1m7rNSbCkaODD8M+vez Uu/BV1m63KyTQ5qGZ4vzfOyj7lLn8sD6aYqIGnTaiEUcYUpS6kg3BPneiXvl5zcLKr0v qEtJH1aTxcVQjxUSpzW8c8gxtzH/77aYVsKh5wK+QElM4rzi7kCaHtN5zs9IU3A3o+CP UG7QO1/5jaNaVBuynHaUF+fS99fpm8Nna2mI4L6wBbgLX15XvcT0pyDfk95aYR/NcfSJ lJzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728485428; x=1729090228; 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=SGLcIoQahP9n/OezrkoG88srol0tiav77UVhbXvA3KM=; b=bOW5gpY3RG8jLrd6PKDnPve8j4QCYYvWQgNbTTPKdFnWiCTQQR7YhcHuw8+fH15VDH qcnR8g9CPvBsB+1BWureDGFDTFTsy0RB0K2QL7S11cD9ITPRPxps0fPSP6Hf6laMpCxP 0Y8mqvvDwUyeOxy5xIhPCJD590AtL6IqQ2ShiIcBx7qhxVSD7z3FDIE2LcIPxq9+MDlk C2lRZFm+NQ+t8/934EL3B8HofO+aJmN6UYGTOUOoo04dmq3tOKvF4uRjlqrnEyI+hW8d zK+/Ac3HN5VtNqYL6+t+4t5cLlwr8L2n2HpAos9J0eyA6FTrKQuNbe6O37aTzfwEEHfX E+Ig== X-Gm-Message-State: AOJu0YyPN7dDRczgxBFXrGTtDaLTI8LZHYQfcSSbUw12xz/VtWAnIMgx Pe0wPJPt7M1TsyaPUoDriqa5+u8/14P3piZTtGI1z+sQN7UYud2xaejjD/vKOGvS0SUxJ0KWvsn 6fYbXZ/Fl X-Received: by 2002:a5d:538d:0:b0:37d:3a59:a579 with SMTP id ffacd0b85a97d-37d3aac08a9mr1620932f8f.49.1728485427896; Wed, 09 Oct 2024 07:50:27 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:9b46:2ad8:5153:1163]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d1691b5ddsm10700957f8f.47.2024.10.09.07.50.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Oct 2024 07:50:27 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Javier Tia , Raymond Mao , Maxim Uvarov , Tim Harvey , Anton Antonov , Jerome Forissier , Tom Rini , Joe Hershberger , Ramon Fried , Simon Glass , Mattijs Korpershoek , Heinrich Schuchardt , AKASHI Takahiro , Masahisa Kojima , Marek Vasut , Sean Anderson Subject: [PATCH v12 11/21] net: lwip: add TFTP support and tftpboot command Date: Wed, 9 Oct 2024 16:49:52 +0200 Message-Id: <4ba8f2a001fae5f81a9d5af6a9f99d82b90b4f9e.1728482473.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 Implement do_tftpb(). This implementation of the tftp command supports an optional port number. For example: tftp 192.168.0.30:9069:file.bin It also supports taking the server IP from ${tftpserverip} if defined, before falling back to ${serverip}. Signed-off-by: Jerome Forissier Acked-by: Ilias Apalodimas Tested-by: Ilias Apalodimas --- cmd/Kconfig | 3 +- cmd/net-lwip.c | 8 ++ include/net-common.h | 4 + include/net-lwip.h | 4 + lib/binman.c | 1 + net/lwip/dhcp.c | 11 +- net/lwip/net-lwip.c | 16 +++ net/lwip/tftp.c | 283 ++++++++++++++++++++++++++++++++++++++++++- 8 files changed, 324 insertions(+), 6 deletions(-) diff --git a/cmd/Kconfig b/cmd/Kconfig index fec679df402..01b884f6f59 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -2125,10 +2125,9 @@ config CMD_MDIO config CMD_TFTPBOOT bool "tftp" select PROT_UDP_LWIP if NET_LWIP - default n + default y help tftpboot - load file via network using TFTP protocol - Currently a placeholder (not implemented) when NET_LWIP=y. endif # if CMD_NET diff --git a/cmd/net-lwip.c b/cmd/net-lwip.c index 82edb5fd2e6..80f0872bb8f 100644 --- a/cmd/net-lwip.c +++ b/cmd/net-lwip.c @@ -11,3 +11,11 @@ U_BOOT_CMD( "[loadAddress] [[hostIPaddr:]bootfilename]" ); #endif + +#if defined(CONFIG_CMD_TFTPBOOT) +U_BOOT_CMD( + tftpboot, 3, 0, do_tftpb, + "boot image via network using TFTP protocol\n", + "[loadAddress] [[hostIPaddr:]bootfilename]" +); +#endif diff --git a/include/net-common.h b/include/net-common.h index cbcac178c82..fd7c5e7b488 100644 --- a/include/net-common.h +++ b/include/net-common.h @@ -459,6 +459,10 @@ static inline struct in_addr env_get_ip(char *var) int net_init(void); +/* NET compatibility */ +enum proto_t; +int net_loop(enum proto_t protocol); + /** * dhcp_run() - Run DHCP on the current ethernet device * diff --git a/include/net-lwip.h b/include/net-lwip.h index cfd06726577..37744b0e2cd 100644 --- a/include/net-lwip.h +++ b/include/net-lwip.h @@ -6,6 +6,10 @@ #include #include +enum proto_t { + TFTPGET +}; + struct netif *net_lwip_new_netif(struct udevice *udev); struct netif *net_lwip_new_netif_noip(struct udevice *udev); void net_lwip_remove_netif(struct netif *netif); diff --git a/lib/binman.c b/lib/binman.c index 9047f5275f3..93d85548116 100644 --- a/lib/binman.c +++ b/lib/binman.c @@ -137,6 +137,7 @@ int binman_init(void) { int ret; + return 0; binman = malloc(sizeof(struct binman_info)); if (!binman) return log_msg_ret("space for binman", -ENOMEM); diff --git a/net/lwip/dhcp.c b/net/lwip/dhcp.c index a2cc25d88f5..23b56226921 100644 --- a/net/lwip/dhcp.c +++ b/net/lwip/dhcp.c @@ -119,11 +119,20 @@ int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) int dhcp_run(ulong addr, const char *fname, bool autoload) { char *dhcp_argv[] = {"dhcp", NULL, }; +#ifdef CONFIG_CMD_TFTPBOOT + char *tftp_argv[] = {"tftpboot", boot_file_name, NULL, }; +#endif struct cmd_tbl cmdtp = {}; /* dummy */ if (autoload) { - /* Will be supported when TFTP is added */ +#ifdef CONFIG_CMD_TFTPBOOT + /* Assume DHCP was already performed */ + if (boot_file_name[0]) + return do_tftpb(&cmdtp, 0, 2, tftp_argv); + return 0; +#else return -EOPNOTSUPP; +#endif } return do_dhcp(&cmdtp, 0, 1, dhcp_argv); diff --git a/net/lwip/net-lwip.c b/net/lwip/net-lwip.c index 3b08ffe3b74..5c2bb2e0361 100644 --- a/net/lwip/net-lwip.c +++ b/net/lwip/net-lwip.c @@ -28,6 +28,7 @@ const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; char *pxelinux_configfile; /* Our IP addr (0 = unknown) */ struct in_addr net_ip; +char net_boot_file_name[1024]; static err_t linkoutput(struct netif *netif, struct pbuf *p) { @@ -283,6 +284,21 @@ void net_process_received_packet(uchar *in_packet, int len) #endif } +int net_loop(enum proto_t protocol) +{ + char *argv[1]; + + switch (protocol) { + case TFTPGET: + argv[0] = "tftpboot"; + return do_tftpb(NULL, 0, 1, argv); + default: + return -EINVAL; + } + + return -EINVAL; +} + u32_t sys_now(void) { return get_timer(0); diff --git a/net/lwip/tftp.c b/net/lwip/tftp.c index 1fa246f55d9..26978643313 100644 --- a/net/lwip/tftp.c +++ b/net/lwip/tftp.c @@ -2,10 +2,287 @@ /* Copyright (C) 2024 Linaro Ltd. */ #include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PROGRESS_PRINT_STEP_BYTES (10 * 1024) + +enum done_state { + NOT_DONE = 0, + SUCCESS, + FAILURE, + ABORTED +}; + +struct tftp_ctx { + ulong daddr; + ulong size; + ulong block_count; + ulong start_time; + enum done_state done; +}; + +static void *tftp_open(const char *fname, const char *mode, u8_t is_write) +{ + return NULL; +} + +static void tftp_close(void *handle) +{ + struct tftp_ctx *ctx = handle; + ulong elapsed; + + if (ctx->done == FAILURE || ctx->done == ABORTED) { + /* Closing after an error or Ctrl-C */ + return; + } + ctx->done = SUCCESS; + + elapsed = get_timer(ctx->start_time); + if (elapsed > 0) { + puts("\n\t "); /* Line up with "Loading: " */ + print_size(ctx->size / elapsed * 1000, "/s"); + } + puts("\ndone\n"); + printf("Bytes transferred = %lu (%lx hex)\n", ctx->size, ctx->size); + + if (env_set_hex("filesize", ctx->size)) { + log_err("filesize not updated\n"); + return; + } +} + +static int tftp_read(void *handle, void *buf, int bytes) +{ + return 0; +} + +static int tftp_write(void *handle, struct pbuf *p) +{ + struct tftp_ctx *ctx = handle; + struct pbuf *q; + + for (q = p; q != NULL; q = q->next) { + memcpy((void *)ctx->daddr, q->payload, q->len); + ctx->daddr += q->len; + ctx->size += q->len; + ctx->block_count++; + if (ctx->block_count % 10 == 0) { + putc('#'); + if (ctx->block_count % (65 * 10) == 0) + puts("\n\t "); + } + } + + return 0; +} + +static void tftp_error(void *handle, int err, const char *msg, int size) +{ + struct tftp_ctx *ctx = handle; + char message[100]; + + ctx->done = FAILURE; + memset(message, 0, sizeof(message)); + memcpy(message, msg, LWIP_MIN(sizeof(message) - 1, (size_t)size)); + + printf("\nTFTP error: %d (%s)\n", err, message); +} + +static const struct tftp_context tftp_context = { + tftp_open, + tftp_close, + tftp_read, + tftp_write, + tftp_error +}; + +static int tftp_loop(struct udevice *udev, ulong addr, char *fname, + ip_addr_t srvip, uint16_t srvport) +{ + struct netif *netif; + struct tftp_ctx ctx; + err_t err; + + if (!fname || addr == 0) + return -1; + + if (!srvport) + srvport = TFTP_PORT; + + netif = net_lwip_new_netif(udev); + if (!netif) + return -1; + + ctx.done = NOT_DONE; + ctx.size = 0; + ctx.block_count = 0; + ctx.daddr = addr; + + printf("Using %s device\n", udev->name); + printf("TFTP from server %s; our IP address is %s\n", + ip4addr_ntoa(&srvip), env_get("ipaddr")); + printf("Filename '%s'.\n", fname); + printf("Load address: 0x%lx\n", ctx.daddr); + printf("Loading: "); + + err = tftp_init_client(&tftp_context); + if (!(err == ERR_OK || err == ERR_USE)) + log_err("tftp_init_client err: %d\n", err); + + ctx.start_time = get_timer(0); + err = tftp_get(&ctx, &srvip, srvport, fname, TFTP_MODE_OCTET); + /* might return different errors, like routing problems */ + if (err != ERR_OK) { + printf("tftp_get() error %d\n", err); + net_lwip_remove_netif(netif); + return -1; + } + + while (!ctx.done) { + net_lwip_rx(udev, netif); + sys_check_timeouts(); + if (ctrlc()) { + printf("\nAbort\n"); + ctx.done = ABORTED; + break; + } + } + + tftp_cleanup(); + + net_lwip_remove_netif(netif); + + if (ctx.done == SUCCESS) { + if (env_set_hex("fileaddr", addr)) { + log_err("fileaddr not updated\n"); + return -1; + } + efi_set_bootdev("Net", "", fname, map_sysmem(addr, 0), + ctx.size); + return 0; + } + + return -1; +} int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { - /* Not implemented */ - return CMD_RET_FAILURE; + int ret = CMD_RET_SUCCESS; + char *arg = NULL; + char *words[3] = { }; + char *fname = NULL; + char *server_ip = NULL; + char *server_port = NULL; + char *end; + ip_addr_t srvip; + uint16_t port = TFTP_PORT; + ulong laddr; + ulong addr; + int i; + + laddr = env_get_ulong("loadaddr", 16, image_load_addr); + + switch (argc) { + case 1: + fname = 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]))) { + laddr = addr; + fname = env_get("bootfile"); + } else { + arg = strdup(argv[1]); + } + break; + case 3: + laddr = hextoul(argv[1], NULL); + arg = strdup(argv[2]); + break; + default: + ret = CMD_RET_USAGE; + goto out; + } + + if (!arg) + arg = net_boot_file_name; + + if (arg) { + /* Parse [ip:[port:]]fname */ + i = 0; + while ((*(words + i) = strsep(&arg,":"))) + i++; + + switch (i) { + case 3: + server_ip = words[0]; + server_port = words[1]; + fname = words[2]; + break; + case 2: + server_ip = words[0]; + fname = words[1]; + break; + case 1: + fname = words[0]; + break; + default: + break; + } + } + + if (!server_ip) + server_ip = env_get("tftpserverip"); + if (!server_ip) + server_ip = env_get("serverip"); + if (!server_ip) { + log_err("error: tftpserverip/serverip has to be set\n"); + ret = CMD_RET_FAILURE; + goto out; + } + + if (server_port) + port = dectoul(server_port, NULL); + + if (!ipaddr_aton(server_ip, &srvip)) { + log_err("error: ipaddr_aton\n"); + ret = CMD_RET_FAILURE; + goto out; + } + + if (!fname) { + log_err("error: no file name\n"); + ret = CMD_RET_FAILURE; + goto out; + } + + if (!laddr) { + log_err("error: no load address\n"); + ret = CMD_RET_FAILURE; + goto out; + } + + eth_set_current(); + + if (tftp_loop(eth_get_dev(), laddr, fname, srvip, port) < 0) + ret = CMD_RET_FAILURE; +out: + free(arg); + return ret; }