net: support being a TCP server to unblock TCP fastboot
Subsystems (2)
PATCH 1/3 net: support being a TCP server to unblock TCP fastboot
Make following changes to unblock TCP fastboot support:
1. Implement being a TCP server support
2. Introduce dedicated TCP traffic handler (get rid of UDP signature)
3. Ensure seq_num and ack_num are respected in net_send_tcp_packet function (make sure existing wget_cmd code is reflected with the fix)
Сс: Joe Hershberger <[email protected]>
Сс: Ramon Fried <[email protected]>
---
include/net/tcp.h | 16 +++++-- net/tcp.c | 115 +++++++++++++++++++++++----------------------- net/wget.c | 43 ++++++++--------- 3 files changed, 90 insertions(+), 84 deletions(-)
include/net/tcp.h +11 -5
@@ -259,6 +259,7 @@ union tcp_build_pkt {* enum tcp_state - TCP State machine states for connection * @TCP_CLOSED: Need to send SYN to connect * @TCP_SYN_SENT: Trying to connect, waiting for SYN ACK +* @TCP_SYN_RECEIVED: Initial SYN received, waiting for ACK * @TCP_ESTABLISHED: both server & client have a connection * @TCP_CLOSE_WAIT: Rec FIN, passed to app for FIN, ACK rsp * @TCP_CLOSING: Rec FIN, sent FIN, ACK waiting for ACK
@@ -268,6 +269,7 @@ union tcp_build_pkt {enum tcp_state {TCP_CLOSED ,TCP_SYN_SENT , +TCP_SYN_RECEIVED ,TCP_ESTABLISHED ,TCP_CLOSE_WAIT ,TCP_CLOSING ,
@@ -283,14 +285,18 @@ int tcp_set_tcp_header(uchar *pkt, int dport, int sport, int payload_len,/** * rxhand_tcp() - An incoming packet handler. * @pkt: pointer to the application packet -* @dport: destination UDP port +* @dport: destination TCP port * @sip: source IP address -* @sport: source UDP port +* @sport: source TCP port +* @tcp_seq_num: TCP sequential number +* @tcp_ack_num: TCP acknowledgment number +* @action: TCP action (SYN, ACK, FIN, etc) * @len: packet length */ -typedef void rxhand_tcp (uchar *pkt,unsigned int dport, -struct in_addr sip,unsigned int sport, -unsigned int len); +typedef void rxhand_tcp (uchar *pkt,u16 dport, +struct in_addr sip,u16 sport, +u32 tcp_seq_num,u32 tcp_ack_num, +u8 action,unsigned int len);void tcp_set_tcp_handler (rxhand_tcp *f);void rxhand_tcp_f (union tcp_build_pkt *b,unsigned int len);
net/tcp.c +57 -58
248 lines not on this page · raw message
net/wget.c +22 -21
97 lines not on this page · raw message
Signature · 1 line
2.40.0.348.gf938b09366-goog
PATCH 2/3 net: add fastboot TCP support
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
Сс: Joe Hershberger <[email protected]>
Сс: Ramon Fried <[email protected]>
---
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 => fastboot_udp.h} | 2 +-
net/Makefile | 3 +-
net/fastboot_tcp.c | 143 +++++++++++++++++++++
net/{fastboot.c => 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 => fastboot_udp.h} (91%)
create mode 100644 net/fastboot_tcp.c
rename net/{fastboot.c => fastboot_udp.c} (99%)MAINTAINERS +4 -2
@@ -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 FPGA
cmd/fastboot.c +23 -2
@@ -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--;
drivers/fastboot/Kconfig +7 -0
@@ -28,6 +28,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
drivers/fastboot/fb_common.c +0 -1
@@ -15,7 +15,6 @@#include <command.h> #include <env.h> #include <fastboot.h> -#include <net/fastboot.h> /** * fastboot_buf_addr - base address of the fastboot download buffer
include/net.h +2 -1
@@ -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 */
include/net/fastboot_tcp.h new file +14 -0
@@ -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__ */
include/net/fastboot_udp.h renamed from include/net/fastboot.h +1 -1
@@ -14,7 +14,7 @@/** * Wait for incoming fastboot comands. */ -void fastboot_start_server (void ); +void fastboot_udp_start_server (void );/**********************************************************************/
net/Makefile +2 -1
@@ -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
net/fastboot_tcp.c new file +143 -0
143 lines not on this page · raw message
net/fastboot_udp.c renamed from net/fastboot.c +2 -2
@@ -7,7 +7,7 @@#include <command.h> #include <fastboot.h> #include <net.h> -#include <net/fastboot.h> +#include <net/fastboot_udp.h> 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);
net/net.c +12 -5
@@ -93,7 +93,8 @@#include <net.h> #include <net6.h> #include <ndisc.h> -#include <net/fastboot.h> +#include <net/fastboot_udp.h> +#include <net/fastboot_tcp.h> #include <net/tftp.h> #include <net/ncsi.h> #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,
Signature · 1 line
2.40.0.348.gf938b09366-goog
Re: [PATCH 2/3] net: add fastboot TCP support
Hi Dmitrii,
Quoted 15 lines · Dmitrii Merkurev
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 tcpSigned-off-by Dmitrii Merkurev <[email protected]>Cc Ying-Chun Liu (PaulLiu) <[email protected]>Cc Simon Glass <[email protected]>Сс: Joe Hershberger <[email protected]>
Сс: Ramon Fried <[email protected]>
---
Quoted 16 lines
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 => fastboot_udp.h} | 2 +-
net/Makefile | 3 +-
net/fastboot_tcp.c | 143 +++++++++++++++++++++
net/{fastboot.c => 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 => fastboot_udp.h} (91%)
create mode 100644 net/fastboot_tcp.c
rename net/{fastboot.c => fastboot_udp.c} (99%)nits and a question below
Quoted 75 lines
MAINTAINERS +4
-2@@ -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 FPGAcmd/fastboot.c +23
-2@@ -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--;drivers/fastboot/Kconfig +0
-0
drivers/fastboot/Kconfig config UDP_FUNCTION_FASTBOOT_PORT · line 36
Quoted 9 lines
drivers/fastboot/Kconfig +6
-0@@ -28,6 +28,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.
Please can you add some more help, like a link to the protocol and what it is used for?
Quoted 58 lines
drivers/fastboot/Kconfig +1
-0+ if FASTBOOT config FASTBOOT_BUF_ADDRdrivers/fastboot/fb_common.c +0
-1@@ -15,7 +15,6 @@#include <command.h> #include <env.h> #include <fastboot.h> -#include <net/fastboot.h> /** * fastboot_buf_addr - base address of the fastboot download buffer include/net.h +2
-1@@ -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 */ include/net/fastboot_tcp.h new file +14
-0@@ -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__ */ include/net/fastboot_udp.h renamed from include/net/fastboot.h +0
-0
include/net/fastboot_udp.h line 17
Quoted 5 lines
include/net/fastboot_udp.h renamed from include/net/fastboot.h +1
-1@@ -14,7 +14,7 @@/** * Wait for incoming fastboot comands. */ -void fastboot_start_server (void ); +void fastboot_udp_start_server (void );
Please add a comment
Quoted 62 lines
include/net/fastboot_udp.h renamed from include/net/fastboot.h +0
-0/**********************************************************************/ net/Makefile +2
-1@@ -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.onet/fastboot_tcp.c new file +39
-0@@ -0,0 +1,143 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* +* Copyright (C) 2023 The Android Open Source Project +*/ + +#include <common.h> +#include <fastboot.h> +#include <net.h> +#include <net/fastboot_tcp.h> +#include <net/tcp.h> + +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 )
net/fastboot_tcp.c line 49
Quoted 10 lines
net/fastboot_tcp.c new file +10
-0+{ +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 ((void *)pkt,0 ,PKTSIZE );
'\0'
Can you drop cast on these?
Quoted 17 lines
net/fastboot_tcp.c new file +17
-0+ pkt +=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 len) +{ +__be64 len_be =__cpu_to_be64 (len); +uchar *pkt =net_get_async_tx_pkt_buf (); + +memset ((void *)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);
net/fastboot_tcp.c line 76
Quoted 10 lines
net/fastboot_tcp.c new file +10
-0+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;
Wow, that can really be that big? Or are you using u64 just because that is the size of the field?
Quoted 135 lines
net/fastboot_tcp.c new file +67
-0+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); +}net/fastboot_udp.c renamed from net/fastboot.c +2
-2@@ -7,7 +7,7 @@#include <command.h> #include <fastboot.h> #include <net.h> -#include <net/fastboot.h> +#include <net/fastboot_udp.h> 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);net/net.c +12
-6@@ -93,7 +93,8 @@#include <net.h> #include <net6.h> #include <ndisc.h> -#include <net/fastboot.h> +#include <net/fastboot_udp.h> +#include <net/fastboot_tcp.h> #include <net/tftp.h> #include <net/ncsi.h> #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, --
Quoted 2 lines
2.40.0.348.gf938b09366-goog
Regards,
Simon
Re: [PATCH 2/3] net: add fastboot TCP support
Thank you for the comments. Fixed everything you mentioned in uploaded v2.
Quoted 2 lines
Wow, that can really be that big? Or are you using u64 just because that is the size of the field?
I don't think any fastboot message can be that big. You're right,
using u64 to fit the field. Here is more information about packet
format https://chromium.googlesource.com/aosp/platform/system/core/+/refs/heads/upstream/fastboot/#fastboot-data
Quoted 442 lines · Simon Glass
Hi Dmitrii,
Quoted 15 lines · Dmitrii Merkurev
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 tcpSigned-off-by Dmitrii Merkurev <[email protected]>Cc Ying-Chun Liu (PaulLiu) <[email protected]>Cc Simon Glass <[email protected]>Сс: Joe Hershberger <[email protected]>
Сс: Ramon Fried <[email protected]>
---Quoted 16 lines
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 => fastboot_udp.h} | 2 +- net/Makefile | 3 +- net/fastboot_tcp.c | 143 +++++++++++++++++++++ net/{fastboot.c => 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 => fastboot_udp.h} (91%) create mode 100644 net/fastboot_tcp.c rename net/{fastboot.c => fastboot_udp.c} (99%)Reviewed-by Simon Glass <[email protected]>nits and a question below
Quoted 75 lines
MAINTAINERS +4
-2@@ -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 FPGAcmd/fastboot.c +23
-2@@ -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--;drivers/fastboot/Kconfig +0
-0drivers/fastboot/Kconfig config UDP_FUNCTION_FASTBOOT_PORT · line 36
Quoted 9 lines
drivers/fastboot/Kconfig +6
-0@@ -28,6 +28,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.Please can you add some more help, like a link to the protocol and what it is used for?
Quoted 58 lines
drivers/fastboot/Kconfig +1
-0+ if FASTBOOT config FASTBOOT_BUF_ADDRdrivers/fastboot/fb_common.c +0
-1@@ -15,7 +15,6 @@#include <command.h> #include <env.h> #include <fastboot.h> -#include <net/fastboot.h> /** * fastboot_buf_addr - base address of the fastboot download buffer include/net.h +2
-1@@ -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 */ include/net/fastboot_tcp.h new file +14
-0@@ -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__ */ include/net/fastboot_udp.h renamed from include/net/fastboot.h +0
-0include/net/fastboot_udp.h line 17
Quoted 5 lines
include/net/fastboot_udp.h renamed from include/net/fastboot.h +1
-1@@ -14,7 +14,7 @@/** * Wait for incoming fastboot comands. */ -void fastboot_start_server (void ); +void fastboot_udp_start_server (void );Please add a comment
Quoted 62 lines
include/net/fastboot_udp.h renamed from include/net/fastboot.h +0
-0/**********************************************************************/ net/Makefile +2
-1@@ -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.onet/fastboot_tcp.c new file +39
-0@@ -0,0 +1,143 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* +* Copyright (C) 2023 The Android Open Source Project +*/ + +#include <common.h> +#include <fastboot.h> +#include <net.h> +#include <net/fastboot_tcp.h> +#include <net/tcp.h> + +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 )net/fastboot_tcp.c line 49
Quoted 10 lines
net/fastboot_tcp.c new file +10
-0+{ +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 ((void *)pkt,0 ,PKTSIZE );'\0'
Can you drop cast on these?
Quoted 17 lines
net/fastboot_tcp.c new file +17
-0+ pkt +=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 len) +{ +__be64 len_be =__cpu_to_be64 (len); +uchar *pkt =net_get_async_tx_pkt_buf (); + +memset ((void *)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);net/fastboot_tcp.c line 76
Quoted 10 lines
net/fastboot_tcp.c new file +10
-0+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;Wow, that can really be that big? Or are you using u64 just because that is the size of the field?
Quoted 135 lines
net/fastboot_tcp.c new file +67
-0+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); +}net/fastboot_udp.c renamed from net/fastboot.c +2
-2@@ -7,7 +7,7 @@#include <command.h> #include <fastboot.h> #include <net.h> -#include <net/fastboot.h> +#include <net/fastboot_udp.h> 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);net/net.c +12
-6@@ -93,7 +93,8 @@#include <net.h> #include <net6.h> #include <ndisc.h> -#include <net/fastboot.h> +#include <net/fastboot_udp.h> +#include <net/fastboot_tcp.h> #include <net/tftp.h> #include <net/ncsi.h> #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, --Quoted 2 lines
2.40.0.348.gf938b09366-goog
Regards,
Simon
PATCH 3/3 net: share fastboot boot handle logic between transports
In reply to Dmitrii MerkurevIntroduce reboot, boot and continue commands support to TCP fastboot by moving existing UDP logic into the common module.
Cc: Ying-Chun Liu (PaulLiu) <[email protected]>
Cc: Simon Glass <[email protected]>
Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
---
drivers/fastboot/fb_common.c | 32 ++++++++++++++++++++++++++++++++ include/fastboot.h | 9 +++++++++ net/fastboot_tcp.c | 5 ++++- net/fastboot_udp.c | 29 +---------------------------- 4 files changed, 46 insertions(+), 29 deletions(-)
drivers/fastboot/fb_common.c +32 -0
@@ -15,6 +15,7 @@#include <command.h> #include <env.h> #include <fastboot.h> +#include <net.h> /** * fastboot_buf_addr - base address of the fastboot download buffer
@@ -155,6 +156,37 @@ void fastboot_boot(void) } } +/** +* fastboot_handle_boot() - Shared implementation of system reaction to +* fastboot commands +* +* Making desceisions about device boot state (stay in fastboot, reboot +* to bootloader, reboot to OS, etc). +*/ +void fastboot_handle_boot (int command,bool success) +{ +if (!success) +return ; + +switch (command) { +case FASTBOOT_COMMAND_BOOT : +fastboot_boot (); +net_set_state (NETLOOP_SUCCESS ); +break ; + +case FASTBOOT_COMMAND_CONTINUE : +net_set_state (NETLOOP_SUCCESS ); +break ; + +case FASTBOOT_COMMAND_REBOOT : +case FASTBOOT_COMMAND_REBOOT_BOOTLOADER : +case FASTBOOT_COMMAND_REBOOT_FASTBOOTD : +case FASTBOOT_COMMAND_REBOOT_RECOVERY : +do_reset (NULL ,0 ,0 ,NULL ); +break ; + } +} +/** * fastboot_set_progress_callback() - set progress callback *
include/fastboot.h +9 -0
@@ -123,6 +123,15 @@ void fastboot_init(void *buf_addr, u32 buf_size);*/ void fastboot_boot (void ); +/** +* fastboot_handle_boot() - Shared implementation of system reaction to +* fastboot commands +* +* Making desceisions about device boot state (stay in fastboot, reboot +* to bootloader, reboot to OS, etc). +*/ +void fastboot_handle_boot (int command,bool success); +/** * fastboot_handle_command() - Handle fastboot command *
net/fastboot_tcp.c +4 -1
@@ -73,6 +73,7 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport, u32 tcp_seq_num, u32 tcp_ack_num, u8 action, unsigned int len) { + int fastboot_command_id;u64 command_size;u8 tcp_fin = action &TCP_FIN ;u8 tcp_push = action &TCP_PUSH ;
@@ -115,8 +116,10 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport,break ; }strlcpy (command, pkt, len +1 ); -fastboot_handle_command (command, response); + fastboot_command_id =fastboot_handle_command (command, response);fastboot_tcp_send_message (response,strlen (response)); +fastboot_handle_boot (fastboot_command_id, +strncmp ("OKAY" , response,4 ) ==0 ); }break ;case FASTBOOT_DISCONNECTING :
net/fastboot_udp.c +1 -28
@@ -209,39 +209,12 @@ static void fastboot_send(struct fastboot_header header, char *fastboot_data,net_send_udp_packet (net_server_ethaddr, fastboot_remote_ip, fastboot_remote_port, fastboot_our_port, len); -/* Continue boot process after sending response */ -if (!strncmp ("OKAY" , response,4 )) { -switch (cmd) { -case FASTBOOT_COMMAND_BOOT : -boot_downloaded_image (); -break ; - -case FASTBOOT_COMMAND_CONTINUE : -net_set_state (NETLOOP_SUCCESS ); -break ; - -case FASTBOOT_COMMAND_REBOOT : -case FASTBOOT_COMMAND_REBOOT_BOOTLOADER : -case FASTBOOT_COMMAND_REBOOT_FASTBOOTD : -case FASTBOOT_COMMAND_REBOOT_RECOVERY : -do_reset (NULL ,0 ,0 ,NULL ); -break ; - } - } +fastboot_handle_boot (cmd,strncmp ("OKAY" , response,4 ) ==0 );if (!strncmp ("OKAY" , response,4 ) || !strncmp ("FAIL" , response,4 )) cmd =-1 ; } -/** -* boot_downloaded_image() - Boots into downloaded image. -*/ -static void boot_downloaded_image (void ) -{ -fastboot_boot (); -net_set_state (NETLOOP_SUCCESS ); -} -/** * fastboot_handler() - Incoming UDP packet handler. *
Signature · 1 line
2.40.0.348.gf938b09366-goog
Re: [PATCH 3/3] net: share fastboot boot handle logic between transports
Quoted 8 lines · Dmitrii Merkurev
Introduce reboot, boot and continue commands support to TCP fastboot by moving existing UDP logic into the common module.
Cc: Ying-Chun Liu (PaulLiu) <[email protected]>
Cc: Simon Glass <[email protected]>
Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>
Quoted 8 lines
Signed-off-by Dmitrii Merkurev <[email protected]>---
drivers/fastboot/fb_common.c | 32 ++++++++++++++++++++++++++++++++ include/fastboot.h | 9 +++++++++ net/fastboot_tcp.c | 5 ++++- net/fastboot_udp.c | 29 +---------------------------- 4 files changed, 46 insertions(+), 29 deletions(-)
Ramon Fried
Reviewed-byRe: [PATCH 3/3] net: share fastboot boot handle logic between transports
Quoted 13 lines · Simon Glass
Quoted 11 lines · Dmitrii Merkurev
Introduce reboot, boot and continue commands support to TCP fastboot by moving existing UDP logic into the common module.
Cc: Ying-Chun Liu (PaulLiu) <[email protected]>
Cc: Simon Glass <[email protected]>
Сс: Joe Hershberger <[email protected]> Сс: Ramon Fried <[email protected]>Signed-off-by Dmitrii Merkurev <[email protected]>---
Quoted 7 lines
Quoted 5 lines
drivers/fastboot/fb_common.c | 32 ++++++++++++++++++++++++++++++++ include/fastboot.h | 9 +++++++++ net/fastboot_tcp.c | 5 ++++- net/fastboot_udp.c | 29 +---------------------------- 4 files changed, 46 insertions(+), 29 deletions(-)Reviewed-by Simon Glass <[email protected]>
Quoted 7 lines · Dmitrii Merkurev
Make following changes to unblock TCP fastboot support:
1. Implement being a TCP server support
2. Introduce dedicated TCP traffic handler (get rid of UDP signature)
3. Ensureseq_numandack_numare respected innet_send_tcp_packetfunction (make sure existingwget_cmdcode is reflected with the fix)
Quoted 6 lines
Signed-off-by Dmitrii Merkurev <[email protected]>Cc Ying-Chun Liu (PaulLiu) <[email protected]>Cc Simon Glass <[email protected]>Сс: Joe Hershberger <[email protected]>
Сс: Ramon Fried <[email protected]>
---
I've built it and ran the unit test of the TCP stack. The code looks good to me.
Quoted 11 lines · Dmitrii Merkurev
Make following changes to unblock TCP fastboot support:
1. Implement being a TCP server support
2. Introduce dedicated TCP traffic handler (get rid of UDP signature)
3. Ensureseq_numandack_numare respected innet_send_tcp_packetfunction (make sure existingwget_cmdcode is reflected with the fix)Signed-off-by Dmitrii Merkurev <[email protected]>Cc Ying-Chun Liu (PaulLiu) <[email protected]>Cc Simon Glass <[email protected]>
Quoted 9 lines
Сс: Joe Hershberger <[email protected]>
Сс: Ramon Fried <[email protected]>
---include/net/tcp.h | 16 +++++-- net/tcp.c | 115 +++++++++++++++++++++++----------------------- net/wget.c | 43 ++++++++--------- 3 files changed, 90 insertions(+), 84 deletions(-)
Ramon Fried
Reviewed-byQuoted 17 lines · Simon Glass
Quoted 15 lines · Dmitrii Merkurev
Make following changes to unblock TCP fastboot support:
1. Implement being a TCP server support
2. Introduce dedicated TCP traffic handler (get rid of UDP signature)
3. Ensureseq_numandack_numare respected innet_send_tcp_packetfunction (make sure existingwget_cmdcode is reflected with the fix)Signed-off-by Dmitrii Merkurev <[email protected]>Cc Ying-Chun Liu (PaulLiu) <[email protected]>Cc Simon Glass <[email protected]>Сс: Joe Hershberger <[email protected]>
Сс: Ramon Fried <[email protected]>
---
Quoted 6 lines
Quoted 5 lines
include/net/tcp.h | 16 +++++-- net/tcp.c | 115 +++++++++++++++++++++++----------------------- net/wget.c | 43 ++++++++--------- 3 files changed, 90 insertions(+), 84 deletions(-)Reviewed-by Simon Glass <[email protected]>