8
8
#include <net/fastboot_tcp.h>
9
9
#include <net/tcp.h>
10
10
11
- static char command [FASTBOOT_COMMAND_LEN ] = {0 };
12
- static char response [FASTBOOT_RESPONSE_LEN ] = {0 };
11
+ #define FASTBOOT_TCP_PORT 5554
12
+
13
+ static char command [FASTBOOT_COMMAND_LEN ];
14
+ static char response [FASTBOOT_RESPONSE_LEN ];
13
15
14
16
static const unsigned short handshake_length = 4 ;
15
17
static const uchar * handshake = "FB01" ;
16
18
17
- static u16 curr_sport ;
18
- static u16 curr_dport ;
19
19
static u32 curr_tcp_seq_num ;
20
20
static u32 curr_tcp_ack_num ;
21
21
static unsigned int curr_request_len ;
@@ -25,34 +25,37 @@ static enum fastboot_tcp_state {
25
25
FASTBOOT_DISCONNECTING
26
26
} state = FASTBOOT_CLOSED ;
27
27
28
- static void fastboot_tcp_answer (u8 action , unsigned int len )
28
+ static void fastboot_tcp_answer (struct tcp_stream * tcp , u8 action ,
29
+ unsigned int len )
29
30
{
30
31
const u32 response_seq_num = curr_tcp_ack_num ;
31
32
const u32 response_ack_num = curr_tcp_seq_num +
32
33
(curr_request_len > 0 ? curr_request_len : 1 );
33
34
34
- net_send_tcp_packet (len , htons ( curr_sport ), htons ( curr_dport ) ,
35
+ net_send_tcp_packet (len , tcp -> rhost , tcp -> rport , tcp -> lport ,
35
36
action , response_seq_num , response_ack_num );
36
37
}
37
38
38
- static void fastboot_tcp_reset (void )
39
+ static void fastboot_tcp_reset (struct tcp_stream * tcp )
39
40
{
40
- fastboot_tcp_answer (TCP_RST , 0 );
41
+ fastboot_tcp_answer (tcp , TCP_RST , 0 );
41
42
state = FASTBOOT_CLOSED ;
42
43
}
43
44
44
- static void fastboot_tcp_send_packet (u8 action , const uchar * data , unsigned int len )
45
+ static void fastboot_tcp_send_packet (struct tcp_stream * tcp , u8 action ,
46
+ const uchar * data , unsigned int len )
45
47
{
46
48
uchar * pkt = net_get_async_tx_pkt_buf ();
47
49
48
50
memset (pkt , '\0' , PKTSIZE );
49
51
pkt += net_eth_hdr_size () + IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2 ;
50
52
memcpy (pkt , data , len );
51
- fastboot_tcp_answer (action , len );
53
+ fastboot_tcp_answer (tcp , action , len );
52
54
memset (pkt , '\0' , PKTSIZE );
53
55
}
54
56
55
- static void fastboot_tcp_send_message (const char * message , unsigned int len )
57
+ static void fastboot_tcp_send_message (struct tcp_stream * tcp ,
58
+ const char * message , unsigned int len )
56
59
{
57
60
__be64 len_be = __cpu_to_be64 (len );
58
61
uchar * pkt = net_get_async_tx_pkt_buf ();
@@ -63,12 +66,11 @@ static void fastboot_tcp_send_message(const char *message, unsigned int len)
63
66
memcpy (pkt , & len_be , 8 );
64
67
pkt += 8 ;
65
68
memcpy (pkt , message , len );
66
- fastboot_tcp_answer (TCP_ACK | TCP_PUSH , len + 8 );
69
+ fastboot_tcp_answer (tcp , TCP_ACK | TCP_PUSH , len + 8 );
67
70
memset (pkt , '\0' , PKTSIZE );
68
71
}
69
72
70
- static void fastboot_tcp_handler_ipv4 (uchar * pkt , u16 dport ,
71
- struct in_addr sip , u16 sport ,
73
+ static void fastboot_tcp_handler_ipv4 (struct tcp_stream * tcp , uchar * pkt ,
72
74
u32 tcp_seq_num , u32 tcp_ack_num ,
73
75
u8 action , unsigned int len )
74
76
{
@@ -77,8 +79,6 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport,
77
79
u8 tcp_fin = action & TCP_FIN ;
78
80
u8 tcp_push = action & TCP_PUSH ;
79
81
80
- curr_sport = sport ;
81
- curr_dport = dport ;
82
82
curr_tcp_seq_num = tcp_seq_num ;
83
83
curr_tcp_ack_num = tcp_ack_num ;
84
84
curr_request_len = len ;
@@ -89,17 +89,17 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport,
89
89
if (len != handshake_length ||
90
90
strlen (pkt ) != handshake_length ||
91
91
memcmp (pkt , handshake , handshake_length ) != 0 ) {
92
- fastboot_tcp_reset ();
92
+ fastboot_tcp_reset (tcp );
93
93
break ;
94
94
}
95
- fastboot_tcp_send_packet (TCP_ACK | TCP_PUSH ,
95
+ fastboot_tcp_send_packet (tcp , TCP_ACK | TCP_PUSH ,
96
96
handshake , handshake_length );
97
97
state = FASTBOOT_CONNECTED ;
98
98
}
99
99
break ;
100
100
case FASTBOOT_CONNECTED :
101
101
if (tcp_fin ) {
102
- fastboot_tcp_answer (TCP_FIN | TCP_ACK , 0 );
102
+ fastboot_tcp_answer (tcp , TCP_FIN | TCP_ACK , 0 );
103
103
state = FASTBOOT_DISCONNECTING ;
104
104
break ;
105
105
}
@@ -111,12 +111,12 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport,
111
111
112
112
// Only single packet messages are supported ATM
113
113
if (strlen (pkt ) != command_size ) {
114
- fastboot_tcp_reset ();
114
+ fastboot_tcp_reset (tcp );
115
115
break ;
116
116
}
117
117
strlcpy (command , pkt , len + 1 );
118
118
fastboot_command_id = fastboot_handle_command (command , response );
119
- fastboot_tcp_send_message (response , strlen (response ));
119
+ fastboot_tcp_send_message (tcp , response , strlen (response ));
120
120
fastboot_handle_boot (fastboot_command_id ,
121
121
strncmp ("OKAY" , response , 4 ) == 0 );
122
122
}
@@ -129,17 +129,21 @@ static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport,
129
129
130
130
memset (command , 0 , FASTBOOT_COMMAND_LEN );
131
131
memset (response , 0 , FASTBOOT_RESPONSE_LEN );
132
- curr_sport = 0 ;
133
- curr_dport = 0 ;
134
132
curr_tcp_seq_num = 0 ;
135
133
curr_tcp_ack_num = 0 ;
136
134
curr_request_len = 0 ;
137
135
}
138
136
137
+ static int incoming_filter (struct in_addr rhost , u16 rport , u16 lport )
138
+ {
139
+ return (lport == FASTBOOT_TCP_PORT );
140
+ }
141
+
139
142
void fastboot_tcp_start_server (void )
140
143
{
141
144
printf ("Using %s device\n" , eth_get_name ());
142
145
printf ("Listening for fastboot command on tcp %pI4\n" , & net_ip );
143
146
147
+ tcp_set_incoming_filter (incoming_filter );
144
148
tcp_set_tcp_handler (fastboot_tcp_handler_ipv4 );
145
149
}
0 commit comments