Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3FB58C76196 for ; Tue, 28 Mar 2023 21:08:40 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5323785D4E; Tue, 28 Mar 2023 23:07:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com 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=google.com header.i=@google.com header.b="YuKp53Qp"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8379F85C2B; Tue, 28 Mar 2023 22:31:25 +0200 (CEST) Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) (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 3462785AEC for ; Tue, 28 Mar 2023 22:31:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3mU4jZAkKBuoPUYadUZZkSaaSXQ.OaYg-NaafXUefe.PQZj.PQ@flex--dimorinny.bounces.google.com Received: by mail-ed1-x54a.google.com with SMTP id x35-20020a50baa6000000b005021d1b1e9eso19203488ede.13 for ; Tue, 28 Mar 2023 13:31:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680035481; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=esn3UoZG0SB9D9AV/OpOFi5Hc+LlJs3kYnXOE5XZak8=; b=YuKp53QpTO/89yR1cGemmLSKBLk/maO4A/4Ps1nH/+IA+viQ09tjWzhV90WNKEII72 Y0qxSzH7tEaiBoeyC2nH66yiaVqkC1EyH3IXLMtnF5Cbkj/ORDKbOBoal5scjAQ+Imnq XRYRjVegnPiWXM86pvQ5pIf3SafW7UoXKjjhG/5+SWB5PQJGDIdEaVA9uA1gL6Xm03Em FOz0dR2kTOZ5KcoEWCycorBZ34Dhp9Cxm5QrM4QrMszfHfBQ7IrdOZFu6090LSDpNjzP q+VaL6emvCiRbAiHZQeHePjPeVkKrYXmAV6SemzIY2OzY2rBEKDAiOqHZ+vTrjR7FuEU Gpyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680035481; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=esn3UoZG0SB9D9AV/OpOFi5Hc+LlJs3kYnXOE5XZak8=; b=hflpZhSVx14HJcSoBTfX/h8dderxYmNfYviUanxmQ+RgWcnQie2CDFsiQcf/7fHLC5 CjABCUFuKMngsKb1wr32ODL3Nqk2Depud4RdOakx+3W3n5xYXatqNYdhZwpxgWhXoS2q Ud2PdU7QfuscngI8Z0hgOJUmQ3lZRplLkb3WxPjBBrwmpMT9cpJbCur1Q5i/26mM4zVs MSx4Q1auXNnAHZMM+Xrqb98iRbfsqzgF0KfZrI7LnPqtiiSohOG9untTuLMYrEsNTJPI 2lbRcgO3OtsAwH/Y8KBZm2vUQerRd14v0VWaMyXkml2xAISC3mmTn/Nb6v2Fma7k3JrE SKpw== X-Gm-Message-State: AAQBX9c7uLEkWMZFHX4KIYzXnQC+L3ekJt8kESCFwCjwT/ZEeBtwFn/3 BL0qvARblvQMgO51YqEwvOUO4Vz3JUhEAL3zGe+AzW++oIc1CeiQ6KX9VAPoB14Wyr8RxVFO8Xj ZmOQVlva07oCF4It9CuLSpGUNU6USc9TeCODhs+oK2LC4LxBhYaSTA5wcNF4BhtYwHBk= X-Google-Smtp-Source: AKy350aHby441h+eCT7YzIZweH5lWOrfxVaF38UGXKU1u8f62rdofARpstac98CL8eNQLijBaS5TM5eS1vnChNQ= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:dec6:15cf:125b:436b]) (user=dimorinny job=sendgmr) by 2002:a50:bae1:0:b0:4fb:3549:a708 with SMTP id x88-20020a50bae1000000b004fb3549a708mr8294766ede.6.1680035481774; Tue, 28 Mar 2023 13:31:21 -0700 (PDT) Date: Tue, 28 Mar 2023 21:31:10 +0100 In-Reply-To: <20230328203111.1032378-1-dimorinny@google.com> Mime-Version: 1.0 References: <20230328203111.1032378-1-dimorinny@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230328203111.1032378-2-dimorinny@google.com> Subject: [PATCH 2/3] net: add fastboot TCP support From: Dmitrii Merkurev To: u-boot@lists.denx.de Cc: Dmitrii Merkurev , Ying-Chun Liu , Simon Glass Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailman-Approved-At: Tue, 28 Mar 2023 23:07:15 +0200 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 Known limitations are 1. fastboot reboot doesn't work (answering OK but not rebooting) 2. flashing isn't supported (TCP transport only limitation) The command syntax is fastboot tcp Signed-off-by: Dmitrii Merkurev Cc: Ying-Chun Liu (PaulLiu) Cc: Simon Glass =D0=A1=D1=81: Joe Hershberger =D0=A1=D1=81: Ramon Fried --- MAINTAINERS | 6 +- cmd/fastboot.c | 25 +++- drivers/fastboot/Kconfig | 7 + drivers/fastboot/fb_common.c | 1 - include/net.h | 3 +- include/net/fastboot_tcp.h | 14 ++ include/net/{fastboot.h =3D> fastboot_udp.h} | 2 +- net/Makefile | 3 +- net/fastboot_tcp.c | 143 +++++++++++++++++++++ net/{fastboot.c =3D> fastboot_udp.c} | 4 +- net/net.c | 17 ++- 11 files changed, 210 insertions(+), 15 deletions(-) create mode 100644 include/net/fastboot_tcp.h rename include/net/{fastboot.h =3D> fastboot_udp.h} (91%) create mode 100644 net/fastboot_tcp.c rename net/{fastboot.c =3D> fastboot_udp.c} (99%) diff --git a/MAINTAINERS b/MAINTAINERS index 91d40ea4b6..501d1147d9 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -978,10 +978,12 @@ F: cmd/fastboot.c F: doc/android/fastboot*.rst F: include/fastboot.h F: include/fastboot-internal.h -F: include/net/fastboot.h +F: include/net/fastboot_tcp.h +F: include/net/fastboot_udp.h F: drivers/fastboot/ F: drivers/usb/gadget/f_fastboot.c -F: net/fastboot.c +F: net/fastboot_tcp.c +F: net/fastboot_udp.c F: test/dm/fastboot.c =20 FPGA diff --git a/cmd/fastboot.c b/cmd/fastboot.c index 97dc02ce74..3d5ff951eb 100644 --- a/cmd/fastboot.c +++ b/cmd/fastboot.c @@ -26,7 +26,7 @@ static int do_fastboot_udp(int argc, char *const argv[], return CMD_RET_FAILURE; } =20 - err =3D net_loop(FASTBOOT); + err =3D net_loop(FASTBOOT_UDP); =20 if (err < 0) { printf("fastboot udp error: %d\n", err); @@ -36,6 +36,26 @@ static int do_fastboot_udp(int argc, char *const argv[], return CMD_RET_SUCCESS; } =20 +static int do_fastboot_tcp(int argc, char *const argv[], + uintptr_t buf_addr, size_t buf_size) +{ + int err; + + if (!IS_ENABLED(CONFIG_TCP_FUNCTION_FASTBOOT)) { + pr_err("Fastboot TCP not enabled\n"); + return CMD_RET_FAILURE; + } + + err =3D net_loop(FASTBOOT_TCP); + + if (err < 0) { + printf("fastboot tcp error: %d\n", err); + return CMD_RET_FAILURE; + } + + return CMD_RET_SUCCESS; +} + static int do_fastboot_usb(int argc, char *const argv[], uintptr_t buf_addr, size_t buf_size) { @@ -141,7 +161,8 @@ NXTARG: =20 if (!strcmp(argv[1], "udp")) return do_fastboot_udp(argc, argv, buf_addr, buf_size); - + if (!strcmp(argv[1], "tcp")) + return do_fastboot_tcp(argc, argv, buf_addr, buf_size); if (!strcmp(argv[1], "usb")) { argv++; argc--; diff --git a/drivers/fastboot/Kconfig b/drivers/fastboot/Kconfig index eefa34779c..c07df8369e 100644 --- a/drivers/fastboot/Kconfig +++ b/drivers/fastboot/Kconfig @@ -28,6 +28,13 @@ config UDP_FUNCTION_FASTBOOT_PORT help The fastboot protocol requires a UDP port number. =20 +config TCP_FUNCTION_FASTBOOT + depends on NET + select FASTBOOT + bool "Enable fastboot protocol over TCP" + help + This enables the fastboot protocol over TCP. + if FASTBOOT =20 config FASTBOOT_BUF_ADDR diff --git a/drivers/fastboot/fb_common.c b/drivers/fastboot/fb_common.c index 57b6182c46..dde3cda78f 100644 --- a/drivers/fastboot/fb_common.c +++ b/drivers/fastboot/fb_common.c @@ -15,7 +15,6 @@ #include #include #include -#include =20 /** * fastboot_buf_addr - base address of the fastboot download buffer diff --git a/include/net.h b/include/net.h index 399af5e064..63daab3731 100644 --- a/include/net.h +++ b/include/net.h @@ -505,7 +505,8 @@ extern int net_restart_wrap; /* Tried all network devi= ces */ =20 enum proto_t { BOOTP, RARP, ARP, TFTPGET, DHCP, PING, PING6, DNS, NFS, CDP, NETCONS, - SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT, WOL, UDP, NCSI, WGET + SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT_UDP, FASTBOOT_TCP, WOL, + UDP, NCSI, WGET }; =20 extern char net_boot_file_name[1024];/* Boot File name */ diff --git a/include/net/fastboot_tcp.h b/include/net/fastboot_tcp.h new file mode 100644 index 0000000000..6cf29d52e9 --- /dev/null +++ b/include/net/fastboot_tcp.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (C) 2023 The Android Open Source Project + */ + +#ifndef __NET_FASTBOOT_TCP_H__ +#define __NET_FASTBOOT_TCP_H__ + +/** + * Wait for incoming tcp fastboot comands. + */ +void fastboot_tcp_start_server(void); + +#endif /* __NET_FASTBOOT_TCP_H__ */ diff --git a/include/net/fastboot.h b/include/net/fastboot_udp.h similarity index 91% rename from include/net/fastboot.h rename to include/net/fastboot_udp.h index 68602095d2..82145bcb9c 100644 --- a/include/net/fastboot.h +++ b/include/net/fastboot_udp.h @@ -14,7 +14,7 @@ /** * Wait for incoming fastboot comands. */ -void fastboot_start_server(void); +void fastboot_udp_start_server(void); =20 /**********************************************************************/ =20 diff --git a/net/Makefile b/net/Makefile index bea000b206..67fc77257d 100644 --- a/net/Makefile +++ b/net/Makefile @@ -26,7 +26,8 @@ obj-$(CONFIG_CMD_PCAP) +=3D pcap.o obj-$(CONFIG_CMD_RARP) +=3D rarp.o obj-$(CONFIG_CMD_SNTP) +=3D sntp.o obj-$(CONFIG_CMD_TFTPBOOT) +=3D tftp.o -obj-$(CONFIG_UDP_FUNCTION_FASTBOOT) +=3D fastboot.o +obj-$(CONFIG_UDP_FUNCTION_FASTBOOT) +=3D fastboot_udp.o +obj-$(CONFIG_TCP_FUNCTION_FASTBOOT) +=3D fastboot_tcp.o obj-$(CONFIG_CMD_WOL) +=3D wol.o obj-$(CONFIG_PROT_UDP) +=3D udp.o obj-$(CONFIG_PROT_TCP) +=3D tcp.o diff --git a/net/fastboot_tcp.c b/net/fastboot_tcp.c new file mode 100644 index 0000000000..b4bbd815c8 --- /dev/null +++ b/net/fastboot_tcp.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* + * Copyright (C) 2023 The Android Open Source Project + */ + +#include +#include +#include +#include +#include + +static char command[FASTBOOT_COMMAND_LEN] =3D {0}; +static char response[FASTBOOT_RESPONSE_LEN] =3D {0}; + +static const unsigned short handshake_length =3D 4; +static const uchar *handshake =3D "FB01"; + +static u16 curr_sport; +static u16 curr_dport; +static u32 curr_tcp_seq_num; +static u32 curr_tcp_ack_num; +static unsigned int curr_request_len; +static enum fastboot_tcp_state { + FASTBOOT_CLOSED, + FASTBOOT_CONNECTED, + FASTBOOT_DISCONNECTING +} state =3D FASTBOOT_CLOSED; + +static void fastboot_tcp_answer(u8 action, unsigned int len) +{ + const u32 response_seq_num =3D curr_tcp_ack_num; + const u32 response_ack_num =3D curr_tcp_seq_num + + (curr_request_len > 0 ? curr_request_len : 1); + + net_send_tcp_packet(len, htons(curr_sport), htons(curr_dport), + action, response_seq_num, response_ack_num); +} + +static void fastboot_tcp_reset(void) +{ + fastboot_tcp_answer(TCP_RST, 0); + state =3D FASTBOOT_CLOSED; +} + +static void fastboot_tcp_send_packet(u8 action, const uchar *data, unsigne= d int len) +{ + uchar *pkt =3D net_get_async_tx_pkt_buf(); + + memset((void *)pkt, 0, PKTSIZE); + pkt +=3D net_eth_hdr_size() + IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; + memcpy(pkt, data, len); + fastboot_tcp_answer(action, len); + memset((void *)pkt, 0, PKTSIZE); +} + +static void fastboot_tcp_send_message(const char *message, unsigned int le= n) +{ + __be64 len_be =3D __cpu_to_be64(len); + uchar *pkt =3D net_get_async_tx_pkt_buf(); + + memset((void *)pkt, 0, PKTSIZE); + pkt +=3D net_eth_hdr_size() + IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; + // Put first 8 bytes as a big endian message length + memcpy(pkt, &len_be, 8); + pkt +=3D 8; + memcpy(pkt, message, len); + fastboot_tcp_answer(TCP_ACK | TCP_PUSH, len + 8); + memset((void *)pkt, 0, PKTSIZE); +} + +static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport, + struct in_addr sip, u16 sport, + u32 tcp_seq_num, u32 tcp_ack_num, + u8 action, unsigned int len) +{ + u64 command_size; + u8 tcp_fin =3D action & TCP_FIN; + u8 tcp_push =3D action & TCP_PUSH; + + curr_sport =3D sport; + curr_dport =3D dport; + curr_tcp_seq_num =3D tcp_seq_num; + curr_tcp_ack_num =3D tcp_ack_num; + curr_request_len =3D len; + + switch (state) { + case FASTBOOT_CLOSED: + if (tcp_push) { + if (len !=3D handshake_length || + strlen(pkt) !=3D handshake_length || + memcmp(pkt, handshake, handshake_length) !=3D 0) { + fastboot_tcp_reset(); + break; + } + fastboot_tcp_send_packet(TCP_ACK | TCP_PUSH, + handshake, handshake_length); + state =3D FASTBOOT_CONNECTED; + } + break; + case FASTBOOT_CONNECTED: + if (tcp_fin) { + fastboot_tcp_answer(TCP_FIN | TCP_ACK, 0); + state =3D FASTBOOT_DISCONNECTING; + break; + } + if (tcp_push) { + // First 8 bytes is big endian message length + command_size =3D __be64_to_cpu(*(u64 *)pkt); + len -=3D 8; + pkt +=3D 8; + + // Only single packet messages are supported ATM + if (strlen(pkt) !=3D command_size) { + fastboot_tcp_reset(); + break; + } + strlcpy(command, pkt, len + 1); + fastboot_handle_command(command, response); + fastboot_tcp_send_message(response, strlen(response)); + } + break; + case FASTBOOT_DISCONNECTING: + if (tcp_push) + state =3D FASTBOOT_CLOSED; + break; + } + + memset(command, 0, FASTBOOT_COMMAND_LEN); + memset(response, 0, FASTBOOT_RESPONSE_LEN); + curr_sport =3D 0; + curr_dport =3D 0; + curr_tcp_seq_num =3D 0; + curr_tcp_ack_num =3D 0; + curr_request_len =3D 0; +} + +void fastboot_tcp_start_server(void) +{ + printf("Using %s device\n", eth_get_name()); + printf("Listening for fastboot command on tcp %pI4\n", &net_ip); + + tcp_set_tcp_handler(fastboot_tcp_handler_ipv4); +} diff --git a/net/fastboot.c b/net/fastboot_udp.c similarity index 99% rename from net/fastboot.c rename to net/fastboot_udp.c index e9569d88d2..27e779d8e0 100644 --- a/net/fastboot.c +++ b/net/fastboot_udp.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include =20 enum { FASTBOOT_ERROR =3D 0, @@ -300,7 +300,7 @@ static void fastboot_handler(uchar *packet, unsigned in= t dport, } } =20 -void fastboot_start_server(void) +void fastboot_udp_start_server(void) { printf("Using %s device\n", eth_get_name()); printf("Listening for fastboot command on %pI4\n", &net_ip); diff --git a/net/net.c b/net/net.c index c9a749f6cc..41c3cac26e 100644 --- a/net/net.c +++ b/net/net.c @@ -93,7 +93,8 @@ #include #include #include -#include +#include +#include #include #include #if defined(CONFIG_CMD_PCAP) @@ -498,9 +499,14 @@ restart: tftp_start_server(); break; #endif -#ifdef CONFIG_UDP_FUNCTION_FASTBOOT - case FASTBOOT: - fastboot_start_server(); +#if defined(CONFIG_UDP_FUNCTION_FASTBOOT) + case FASTBOOT_UDP: + fastboot_udp_start_server(); + break; +#endif +#if defined(CONFIG_TCP_FUNCTION_FASTBOOT) + case FASTBOOT_TCP: + fastboot_tcp_start_server(); break; #endif #if defined(CONFIG_CMD_DHCP) @@ -1491,7 +1497,8 @@ common: /* Fall through */ =20 case NETCONS: - case FASTBOOT: + case FASTBOOT_UDP: + case FASTBOOT_TCP: case TFTPSRV: if (IS_ENABLED(CONFIG_IPV6) && use_ip6) { if (!memcmp(&net_link_local_ip6, &net_null_addr_ip6, --=20 2.40.0.348.gf938b09366-goog