From patchwork Wed Apr 12 18:49:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Dmitrii Merkurev X-Patchwork-Id: 1768516 X-Patchwork-Delegate: rfried.dev@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20221208 header.b=rzCNffoI; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Py2jQ3NZ6z1yZZ for ; Fri, 14 Apr 2023 00:56:58 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B3DA785BB5; Thu, 13 Apr 2023 16:56:41 +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="rzCNffoI"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D4D3285FDD; Wed, 12 Apr 2023 20:49:49 +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=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) (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 502D783741 for ; Wed, 12 Apr 2023 20:49:45 +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=3SP02ZAkKBkcmrvx0rww7pxxpun.lxv3-kxx2ur121.mnw6.mn@flex--dimorinny.bounces.google.com Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54f9df9ebc5so23431957b3.13 for ; Wed, 12 Apr 2023 11:49:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681325384; x=1683917384; 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=Bvl6dEmVELIMFPh99U1L4tk9j1R+YBAV5RVNEsDEOVk=; b=rzCNffoIjV57iTd7su5LluQ6WmYd2p7ltzWNnlCj9x7NfZoaeDMnQSRBe8fC7QTPmO nXtjKlVpBhpzCI5A403BjABdBjNXyT8UKDROE2/uul23Bh2v38oFUdhqOWXFDENisqQo hputyVML+/wQWpYnUH/e8kGhE9ZAJNq08jFQBnLnXF4Ufini6Nkr1SP6l1xTS36UoOMv gej2b9rWovgF26k9haxgodg9PYDcw3Gww3YCtmYWgTYKOR+axRwc/P5/v2aKdd3QjcUR xjjUibk1osPCxy9D8hM1zIMA1ITAq85Vevs43qx3/qEH6GcG/IL5mXJ/KG2ZFzFCIhQk oGuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681325384; x=1683917384; 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=Bvl6dEmVELIMFPh99U1L4tk9j1R+YBAV5RVNEsDEOVk=; b=i/Q7vY75BS7QcIVPP34b+tjleA7tUrL+KbV3zvEGbsTR/cyvOyox9s5L4BcFmQaMiB p4KQl7CKmzdNMgvNNH5kxqJM1D9SqK5fBWhhaYf4jARt86Fah4CVgavpW5Ks0hIghlPo QlPrB/mzAX6P0G0qeg/GATM0U8HFH9OB0kYw5xcaJWRj1lYHiSwbX/kjdZlIRNI8J958 8Xgc6L0pJvq5rjznmeQ+7BTsqC5tskZY9fvZK8/E9Yt+qrpcp7qU5vQHiMY5oFkH9c2Y VQ5E7YNC1A7vt3FWnDteMzOurWk2mVo159MsQKxRah0M4PyrUDpZcLkok/LayKcKAj8w 4tjw== X-Gm-Message-State: AAQBX9d94cjTEuBc5t5+iuqHL4tfcK1nHV5O7HFrS3h4FsgR87Kt55tg 8oE6Ni+7ezQ8sEcDzQBAn4EiQzlkZ0Co71xoTczNxd/lIue/Y5SHg6HTtHiWgAL2KGhovlun2BC b0IkTqzd4IRq6rw9Pif7S01e2Th8u1ESXyDAicyLaWvBCqFauvbcEvqjMRr3rHUnO0bw= X-Google-Smtp-Source: AKy350ZBWxj4c474WwLajxwue7+nTlQW2Dg5lumBKYtQ1kst0+XF41Xa1Fzpb2gQcQOzEpae3B9pt98zWEu29Zo= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:7766:76f9:f832:5b94]) (user=dimorinny job=sendgmr) by 2002:a5b:647:0:b0:b8f:5474:2f33 with SMTP id o7-20020a5b0647000000b00b8f54742f33mr317416ybq.5.1681325384059; Wed, 12 Apr 2023 11:49:44 -0700 (PDT) Date: Wed, 12 Apr 2023 19:49:30 +0100 In-Reply-To: <20230412184931.1422789-1-dimorinny@google.com> Mime-Version: 1.0 References: <20230412184931.1422789-1-dimorinny@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230412184931.1422789-2-dimorinny@google.com> Subject: [PATCH v2 2/3] net: add fastboot TCP support From: Dmitrii Merkurev To: u-boot@lists.denx.de Cc: Dmitrii Merkurev , Ying-Chun Liu , Simon Glass X-Mailman-Approved-At: Thu, 13 Apr 2023 16:56:35 +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 Сс: Joe Hershberger Сс: Ramon Fried Reviewed-by: Simon Glass --- MAINTAINERS | 6 +- cmd/fastboot.c | 25 ++++- drivers/fastboot/Kconfig | 14 +++ drivers/fastboot/fb_common.c | 1 - include/net.h | 3 +- include/net/fastboot.h | 21 ----- include/net/fastboot_tcp.h | 14 +++ include/net/fastboot_udp.h | 14 +++ net/Makefile | 3 +- net/fastboot_tcp.c | 143 +++++++++++++++++++++++++++++ net/{fastboot.c => fastboot_udp.c} | 4 +- net/net.c | 17 +++- 12 files changed, 230 insertions(+), 35 deletions(-) delete mode 100644 include/net/fastboot.h create mode 100644 include/net/fastboot_tcp.h create mode 100644 include/net/fastboot_udp.h create mode 100644 net/fastboot_tcp.c rename net/{fastboot.c => fastboot_udp.c} (99%) diff --git a/MAINTAINERS b/MAINTAINERS index 4c17c6cb9f..e28d1f7ae0 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -987,10 +987,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 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; } - err = net_loop(FASTBOOT); + err = net_loop(FASTBOOT_UDP); 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; } +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 = 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: 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..a3df9aa3d0 100644 --- a/drivers/fastboot/Kconfig +++ b/drivers/fastboot/Kconfig @@ -4,6 +4,13 @@ config FASTBOOT bool imply ANDROID_BOOT_IMAGE imply CMD_FASTBOOT + help + Fastboot is a protocol used in Android devices for + communicating between the device and a computer during + the bootloader stage. It allows the user to flash the + device firmware and unlock the bootloader. + More information about the protocol and usecases: + https://android.googlesource.com/platform/system/core/+/refs/heads/master/fastboot/ config USB_FUNCTION_FASTBOOT bool "Enable USB fastboot gadget" @@ -28,6 +35,13 @@ config UDP_FUNCTION_FASTBOOT_PORT help The fastboot protocol requires a UDP port number. +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 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 /** * 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 devices */ 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 }; extern char net_boot_file_name[1024];/* Boot File name */ diff --git a/include/net/fastboot.h b/include/net/fastboot.h deleted file mode 100644 index 68602095d2..0000000000 --- a/include/net/fastboot.h +++ /dev/null @@ -1,21 +0,0 @@ -/* SPDX-License-Identifier: BSD-2-Clause - * - * Copyright (C) 2016 The Android Open Source Project - */ - -#ifndef __NET_FASTBOOT_H__ -#define __NET_FASTBOOT_H__ - -/**********************************************************************/ -/* - * Global functions and variables. - */ - -/** - * Wait for incoming fastboot comands. - */ -void fastboot_start_server(void); - -/**********************************************************************/ - -#endif /* __NET_FASTBOOT_H__ */ 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_udp.h b/include/net/fastboot_udp.h new file mode 100644 index 0000000000..d4382c0a0e --- /dev/null +++ b/include/net/fastboot_udp.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (C) 2016 The Android Open Source Project + */ + +#ifndef __NET_FASTBOOT_H__ +#define __NET_FASTBOOT_H__ + +/** + * Wait for incoming UDP fastboot comands. + */ +void fastboot_udp_start_server(void); + +#endif /* __NET_FASTBOOT_H__ */ 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) += pcap.o obj-$(CONFIG_CMD_RARP) += rarp.o obj-$(CONFIG_CMD_SNTP) += sntp.o obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o -obj-$(CONFIG_UDP_FUNCTION_FASTBOOT) += fastboot.o +obj-$(CONFIG_UDP_FUNCTION_FASTBOOT) += fastboot_udp.o +obj-$(CONFIG_TCP_FUNCTION_FASTBOOT) += fastboot_tcp.o obj-$(CONFIG_CMD_WOL) += wol.o obj-$(CONFIG_PROT_UDP) += udp.o obj-$(CONFIG_PROT_TCP) += tcp.o diff --git a/net/fastboot_tcp.c b/net/fastboot_tcp.c new file mode 100644 index 0000000000..b5613b6aa2 --- /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] = {0}; +static char response[FASTBOOT_RESPONSE_LEN] = {0}; + +static const unsigned short handshake_length = 4; +static const uchar *handshake = "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 = FASTBOOT_CLOSED; + +static void fastboot_tcp_answer(u8 action, unsigned int len) +{ + const u32 response_seq_num = curr_tcp_ack_num; + const u32 response_ack_num = 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 = FASTBOOT_CLOSED; +} + +static void fastboot_tcp_send_packet(u8 action, const uchar *data, unsigned int len) +{ + uchar *pkt = net_get_async_tx_pkt_buf(); + + memset(pkt, '\0', PKTSIZE); + pkt += net_eth_hdr_size() + IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; + memcpy(pkt, data, len); + fastboot_tcp_answer(action, len); + memset(pkt, '\0', PKTSIZE); +} + +static void fastboot_tcp_send_message(const char *message, unsigned int len) +{ + __be64 len_be = __cpu_to_be64(len); + uchar *pkt = net_get_async_tx_pkt_buf(); + + memset(pkt, '\0', PKTSIZE); + pkt += 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 += 8; + memcpy(pkt, message, len); + fastboot_tcp_answer(TCP_ACK | TCP_PUSH, len + 8); + memset(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 = action & TCP_FIN; + u8 tcp_push = action & TCP_PUSH; + + curr_sport = sport; + curr_dport = dport; + curr_tcp_seq_num = tcp_seq_num; + curr_tcp_ack_num = tcp_ack_num; + curr_request_len = len; + + switch (state) { + case FASTBOOT_CLOSED: + if (tcp_push) { + if (len != handshake_length || + strlen(pkt) != handshake_length || + memcmp(pkt, handshake, handshake_length) != 0) { + fastboot_tcp_reset(); + break; + } + fastboot_tcp_send_packet(TCP_ACK | TCP_PUSH, + handshake, handshake_length); + state = FASTBOOT_CONNECTED; + } + break; + case FASTBOOT_CONNECTED: + if (tcp_fin) { + fastboot_tcp_answer(TCP_FIN | TCP_ACK, 0); + state = FASTBOOT_DISCONNECTING; + break; + } + if (tcp_push) { + // First 8 bytes is big endian message length + command_size = __be64_to_cpu(*(u64 *)pkt); + len -= 8; + pkt += 8; + + // Only single packet messages are supported ATM + if (strlen(pkt) != 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 = FASTBOOT_CLOSED; + break; + } + + memset(command, 0, FASTBOOT_COMMAND_LEN); + memset(response, 0, FASTBOOT_RESPONSE_LEN); + curr_sport = 0; + curr_dport = 0; + curr_tcp_seq_num = 0; + curr_tcp_ack_num = 0; + curr_request_len = 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 enum { FASTBOOT_ERROR = 0, @@ -300,7 +300,7 @@ static void fastboot_handler(uchar *packet, unsigned int dport, } } -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 */ 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,