Dmitrii Merkurev | 443d319 | 2023-04-12 19:49:30 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2023 The Android Open Source Project |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <fastboot.h> |
| 8 | #include <net.h> |
| 9 | #include <net/fastboot_tcp.h> |
| 10 | #include <net/tcp.h> |
| 11 | |
| 12 | static char command[FASTBOOT_COMMAND_LEN] = {0}; |
| 13 | static char response[FASTBOOT_RESPONSE_LEN] = {0}; |
| 14 | |
| 15 | static const unsigned short handshake_length = 4; |
| 16 | static const uchar *handshake = "FB01"; |
| 17 | |
| 18 | static u16 curr_sport; |
| 19 | static u16 curr_dport; |
| 20 | static u32 curr_tcp_seq_num; |
| 21 | static u32 curr_tcp_ack_num; |
| 22 | static unsigned int curr_request_len; |
| 23 | static enum fastboot_tcp_state { |
| 24 | FASTBOOT_CLOSED, |
| 25 | FASTBOOT_CONNECTED, |
| 26 | FASTBOOT_DISCONNECTING |
| 27 | } state = FASTBOOT_CLOSED; |
| 28 | |
| 29 | static void fastboot_tcp_answer(u8 action, unsigned int len) |
| 30 | { |
| 31 | const u32 response_seq_num = curr_tcp_ack_num; |
| 32 | const u32 response_ack_num = curr_tcp_seq_num + |
| 33 | (curr_request_len > 0 ? curr_request_len : 1); |
| 34 | |
| 35 | net_send_tcp_packet(len, htons(curr_sport), htons(curr_dport), |
| 36 | action, response_seq_num, response_ack_num); |
| 37 | } |
| 38 | |
| 39 | static void fastboot_tcp_reset(void) |
| 40 | { |
| 41 | fastboot_tcp_answer(TCP_RST, 0); |
| 42 | state = FASTBOOT_CLOSED; |
| 43 | } |
| 44 | |
| 45 | static void fastboot_tcp_send_packet(u8 action, const uchar *data, unsigned int len) |
| 46 | { |
| 47 | uchar *pkt = net_get_async_tx_pkt_buf(); |
| 48 | |
| 49 | memset(pkt, '\0', PKTSIZE); |
| 50 | pkt += net_eth_hdr_size() + IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; |
| 51 | memcpy(pkt, data, len); |
| 52 | fastboot_tcp_answer(action, len); |
| 53 | memset(pkt, '\0', PKTSIZE); |
| 54 | } |
| 55 | |
| 56 | static void fastboot_tcp_send_message(const char *message, unsigned int len) |
| 57 | { |
| 58 | __be64 len_be = __cpu_to_be64(len); |
| 59 | uchar *pkt = net_get_async_tx_pkt_buf(); |
| 60 | |
| 61 | memset(pkt, '\0', PKTSIZE); |
| 62 | pkt += net_eth_hdr_size() + IP_TCP_HDR_SIZE + TCP_TSOPT_SIZE + 2; |
| 63 | // Put first 8 bytes as a big endian message length |
| 64 | memcpy(pkt, &len_be, 8); |
| 65 | pkt += 8; |
| 66 | memcpy(pkt, message, len); |
| 67 | fastboot_tcp_answer(TCP_ACK | TCP_PUSH, len + 8); |
| 68 | memset(pkt, '\0', PKTSIZE); |
| 69 | } |
| 70 | |
| 71 | static void fastboot_tcp_handler_ipv4(uchar *pkt, u16 dport, |
| 72 | struct in_addr sip, u16 sport, |
| 73 | u32 tcp_seq_num, u32 tcp_ack_num, |
| 74 | u8 action, unsigned int len) |
| 75 | { |
| 76 | u64 command_size; |
| 77 | u8 tcp_fin = action & TCP_FIN; |
| 78 | u8 tcp_push = action & TCP_PUSH; |
| 79 | |
| 80 | curr_sport = sport; |
| 81 | curr_dport = dport; |
| 82 | curr_tcp_seq_num = tcp_seq_num; |
| 83 | curr_tcp_ack_num = tcp_ack_num; |
| 84 | curr_request_len = len; |
| 85 | |
| 86 | switch (state) { |
| 87 | case FASTBOOT_CLOSED: |
| 88 | if (tcp_push) { |
| 89 | if (len != handshake_length || |
| 90 | strlen(pkt) != handshake_length || |
| 91 | memcmp(pkt, handshake, handshake_length) != 0) { |
| 92 | fastboot_tcp_reset(); |
| 93 | break; |
| 94 | } |
| 95 | fastboot_tcp_send_packet(TCP_ACK | TCP_PUSH, |
| 96 | handshake, handshake_length); |
| 97 | state = FASTBOOT_CONNECTED; |
| 98 | } |
| 99 | break; |
| 100 | case FASTBOOT_CONNECTED: |
| 101 | if (tcp_fin) { |
| 102 | fastboot_tcp_answer(TCP_FIN | TCP_ACK, 0); |
| 103 | state = FASTBOOT_DISCONNECTING; |
| 104 | break; |
| 105 | } |
| 106 | if (tcp_push) { |
| 107 | // First 8 bytes is big endian message length |
| 108 | command_size = __be64_to_cpu(*(u64 *)pkt); |
| 109 | len -= 8; |
| 110 | pkt += 8; |
| 111 | |
| 112 | // Only single packet messages are supported ATM |
| 113 | if (strlen(pkt) != command_size) { |
| 114 | fastboot_tcp_reset(); |
| 115 | break; |
| 116 | } |
| 117 | strlcpy(command, pkt, len + 1); |
| 118 | fastboot_handle_command(command, response); |
| 119 | fastboot_tcp_send_message(response, strlen(response)); |
| 120 | } |
| 121 | break; |
| 122 | case FASTBOOT_DISCONNECTING: |
| 123 | if (tcp_push) |
| 124 | state = FASTBOOT_CLOSED; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | memset(command, 0, FASTBOOT_COMMAND_LEN); |
| 129 | memset(response, 0, FASTBOOT_RESPONSE_LEN); |
| 130 | curr_sport = 0; |
| 131 | curr_dport = 0; |
| 132 | curr_tcp_seq_num = 0; |
| 133 | curr_tcp_ack_num = 0; |
| 134 | curr_request_len = 0; |
| 135 | } |
| 136 | |
| 137 | void fastboot_tcp_start_server(void) |
| 138 | { |
| 139 | printf("Using %s device\n", eth_get_name()); |
| 140 | printf("Listening for fastboot command on tcp %pI4\n", &net_ip); |
| 141 | |
| 142 | tcp_set_tcp_handler(fastboot_tcp_handler_ipv4); |
| 143 | } |