Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2016 The Android Open Source Project |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 7 | #include <command.h> |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 8 | #include <fastboot.h> |
| 9 | #include <net.h> |
Dmitrii Merkurev | 308252d | 2023-04-12 19:49:30 +0100 | [diff] [blame] | 10 | #include <net/fastboot_udp.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 11 | #include <linux/printk.h> |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 12 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 13 | enum { |
| 14 | FASTBOOT_ERROR = 0, |
| 15 | FASTBOOT_QUERY = 1, |
| 16 | FASTBOOT_INIT = 2, |
| 17 | FASTBOOT_FASTBOOT = 3, |
| 18 | }; |
| 19 | |
| 20 | struct __packed fastboot_header { |
| 21 | uchar id; |
| 22 | uchar flags; |
| 23 | unsigned short seq; |
| 24 | }; |
| 25 | |
| 26 | #define PACKET_SIZE 1024 |
| 27 | #define DATA_SIZE (PACKET_SIZE - sizeof(struct fastboot_header)) |
| 28 | |
| 29 | /* Sequence number sent for every packet */ |
| 30 | static unsigned short sequence_number = 1; |
| 31 | static const unsigned short packet_size = PACKET_SIZE; |
| 32 | static const unsigned short udp_version = 1; |
| 33 | |
| 34 | /* Keep track of last packet for resubmission */ |
| 35 | static uchar last_packet[PACKET_SIZE]; |
| 36 | static unsigned int last_packet_len; |
| 37 | |
| 38 | static struct in_addr fastboot_remote_ip; |
| 39 | /* The UDP port at their end */ |
| 40 | static int fastboot_remote_port; |
| 41 | /* The UDP port at our end */ |
| 42 | static int fastboot_our_port; |
| 43 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 44 | /** |
| 45 | * fastboot_udp_send_info() - Send an INFO packet during long commands. |
| 46 | * |
| 47 | * @msg: String describing the reason for waiting |
| 48 | */ |
| 49 | static void fastboot_udp_send_info(const char *msg) |
| 50 | { |
| 51 | uchar *packet; |
| 52 | uchar *packet_base; |
| 53 | int len = 0; |
| 54 | char response[FASTBOOT_RESPONSE_LEN] = {0}; |
| 55 | |
| 56 | struct fastboot_header response_header = { |
| 57 | .id = FASTBOOT_FASTBOOT, |
| 58 | .flags = 0, |
| 59 | .seq = htons(sequence_number) |
| 60 | }; |
| 61 | ++sequence_number; |
| 62 | packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE; |
| 63 | packet_base = packet; |
| 64 | |
| 65 | /* Write headers */ |
| 66 | memcpy(packet, &response_header, sizeof(response_header)); |
| 67 | packet += sizeof(response_header); |
| 68 | /* Write response */ |
| 69 | fastboot_response("INFO", response, "%s", msg); |
| 70 | memcpy(packet, response, strlen(response)); |
| 71 | packet += strlen(response); |
| 72 | |
| 73 | len = packet - packet_base; |
| 74 | |
| 75 | /* Save packet for retransmitting */ |
| 76 | last_packet_len = len; |
| 77 | memcpy(last_packet, packet_base, last_packet_len); |
| 78 | |
| 79 | net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip, |
| 80 | fastboot_remote_port, fastboot_our_port, len); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * fastboot_timed_send_info() - Send INFO packet every 30 seconds |
| 85 | * |
| 86 | * @msg: String describing the reason for waiting |
| 87 | * |
| 88 | * Send an INFO packet during long commands based on timer. An INFO packet |
| 89 | * is sent if the time is 30 seconds after start. Else, noop. |
| 90 | */ |
| 91 | static void fastboot_timed_send_info(const char *msg) |
| 92 | { |
| 93 | static ulong start; |
| 94 | |
| 95 | /* Initialize timer */ |
| 96 | if (start == 0) |
| 97 | start = get_timer(0); |
| 98 | ulong time = get_timer(start); |
| 99 | /* Send INFO packet to host every 30 seconds */ |
| 100 | if (time >= 30000) { |
| 101 | start = get_timer(0); |
| 102 | fastboot_udp_send_info(msg); |
| 103 | } |
| 104 | } |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * fastboot_send() - Sends a packet in response to received fastboot packet |
| 108 | * |
| 109 | * @header: Header for response packet |
| 110 | * @fastboot_data: Pointer to received fastboot data |
| 111 | * @fastboot_data_len: Length of received fastboot data |
| 112 | * @retransmit: Nonzero if sending last sent packet |
| 113 | */ |
| 114 | static void fastboot_send(struct fastboot_header header, char *fastboot_data, |
| 115 | unsigned int fastboot_data_len, uchar retransmit) |
| 116 | { |
| 117 | uchar *packet; |
| 118 | uchar *packet_base; |
| 119 | int len = 0; |
| 120 | const char *error_msg = "An error occurred."; |
| 121 | short tmp; |
| 122 | struct fastboot_header response_header = header; |
| 123 | static char command[FASTBOOT_COMMAND_LEN]; |
| 124 | static int cmd = -1; |
| 125 | static bool pending_command; |
| 126 | char response[FASTBOOT_RESPONSE_LEN] = {0}; |
| 127 | |
| 128 | /* |
| 129 | * We will always be sending some sort of packet, so |
| 130 | * cobble together the packet headers now. |
| 131 | */ |
| 132 | packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE; |
| 133 | packet_base = packet; |
| 134 | |
| 135 | /* Resend last packet */ |
| 136 | if (retransmit) { |
| 137 | memcpy(packet, last_packet, last_packet_len); |
| 138 | net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip, |
| 139 | fastboot_remote_port, fastboot_our_port, |
| 140 | last_packet_len); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | response_header.seq = htons(response_header.seq); |
| 145 | memcpy(packet, &response_header, sizeof(response_header)); |
| 146 | packet += sizeof(response_header); |
| 147 | |
| 148 | switch (header.id) { |
| 149 | case FASTBOOT_QUERY: |
| 150 | tmp = htons(sequence_number); |
| 151 | memcpy(packet, &tmp, sizeof(tmp)); |
| 152 | packet += sizeof(tmp); |
| 153 | break; |
| 154 | case FASTBOOT_INIT: |
| 155 | tmp = htons(udp_version); |
| 156 | memcpy(packet, &tmp, sizeof(tmp)); |
| 157 | packet += sizeof(tmp); |
| 158 | tmp = htons(packet_size); |
| 159 | memcpy(packet, &tmp, sizeof(tmp)); |
| 160 | packet += sizeof(tmp); |
| 161 | break; |
| 162 | case FASTBOOT_ERROR: |
| 163 | memcpy(packet, error_msg, strlen(error_msg)); |
| 164 | packet += strlen(error_msg); |
| 165 | break; |
| 166 | case FASTBOOT_FASTBOOT: |
| 167 | if (cmd == FASTBOOT_COMMAND_DOWNLOAD) { |
| 168 | if (!fastboot_data_len && !fastboot_data_remaining()) { |
| 169 | fastboot_data_complete(response); |
| 170 | } else { |
| 171 | fastboot_data_download(fastboot_data, |
| 172 | fastboot_data_len, |
| 173 | response); |
| 174 | } |
| 175 | } else if (!pending_command) { |
| 176 | strlcpy(command, fastboot_data, |
| 177 | min((size_t)fastboot_data_len + 1, |
| 178 | sizeof(command))); |
| 179 | pending_command = true; |
| 180 | } else { |
| 181 | cmd = fastboot_handle_command(command, response); |
| 182 | pending_command = false; |
| 183 | } |
| 184 | /* |
| 185 | * Sent some INFO packets, need to update sequence number in |
| 186 | * header |
| 187 | */ |
| 188 | if (header.seq != sequence_number) { |
| 189 | response_header.seq = htons(sequence_number); |
| 190 | memcpy(packet_base, &response_header, |
| 191 | sizeof(response_header)); |
| 192 | } |
| 193 | /* Write response to packet */ |
| 194 | memcpy(packet, response, strlen(response)); |
| 195 | packet += strlen(response); |
| 196 | break; |
| 197 | default: |
| 198 | pr_err("ID %d not implemented.\n", header.id); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | len = packet - packet_base; |
| 203 | |
| 204 | /* Save packet for retransmitting */ |
| 205 | last_packet_len = len; |
| 206 | memcpy(last_packet, packet_base, last_packet_len); |
| 207 | |
| 208 | net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip, |
| 209 | fastboot_remote_port, fastboot_our_port, len); |
| 210 | |
Dmitrii Merkurev | 0788290 | 2023-04-12 19:49:31 +0100 | [diff] [blame] | 211 | fastboot_handle_boot(cmd, strncmp("OKAY", response, 4) == 0); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 212 | |
| 213 | if (!strncmp("OKAY", response, 4) || !strncmp("FAIL", response, 4)) |
| 214 | cmd = -1; |
| 215 | } |
| 216 | |
| 217 | /** |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 218 | * fastboot_handler() - Incoming UDP packet handler. |
| 219 | * |
| 220 | * @packet: Pointer to incoming UDP packet |
| 221 | * @dport: Destination UDP port |
| 222 | * @sip: Source IP address |
| 223 | * @sport: Source UDP port |
| 224 | * @len: Packet length |
| 225 | */ |
| 226 | static void fastboot_handler(uchar *packet, unsigned int dport, |
| 227 | struct in_addr sip, unsigned int sport, |
| 228 | unsigned int len) |
| 229 | { |
| 230 | struct fastboot_header header; |
| 231 | char fastboot_data[DATA_SIZE] = {0}; |
| 232 | unsigned int fastboot_data_len = 0; |
| 233 | |
| 234 | if (dport != fastboot_our_port) |
| 235 | return; |
| 236 | |
| 237 | fastboot_remote_ip = sip; |
| 238 | fastboot_remote_port = sport; |
| 239 | |
| 240 | if (len < sizeof(struct fastboot_header) || len > PACKET_SIZE) |
| 241 | return; |
| 242 | memcpy(&header, packet, sizeof(header)); |
| 243 | header.flags = 0; |
| 244 | header.seq = ntohs(header.seq); |
| 245 | packet += sizeof(header); |
| 246 | len -= sizeof(header); |
| 247 | |
| 248 | switch (header.id) { |
| 249 | case FASTBOOT_QUERY: |
| 250 | fastboot_send(header, fastboot_data, 0, 0); |
| 251 | break; |
| 252 | case FASTBOOT_INIT: |
| 253 | case FASTBOOT_FASTBOOT: |
| 254 | fastboot_data_len = len; |
| 255 | if (len > 0) |
| 256 | memcpy(fastboot_data, packet, len); |
| 257 | if (header.seq == sequence_number) { |
| 258 | fastboot_send(header, fastboot_data, |
| 259 | fastboot_data_len, 0); |
| 260 | sequence_number++; |
| 261 | } else if (header.seq == sequence_number - 1) { |
| 262 | /* Retransmit last sent packet */ |
| 263 | fastboot_send(header, fastboot_data, |
| 264 | fastboot_data_len, 1); |
| 265 | } |
| 266 | break; |
| 267 | default: |
| 268 | pr_err("ID %d not implemented.\n", header.id); |
| 269 | header.id = FASTBOOT_ERROR; |
| 270 | fastboot_send(header, fastboot_data, 0, 0); |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
Dmitrii Merkurev | 308252d | 2023-04-12 19:49:30 +0100 | [diff] [blame] | 275 | void fastboot_udp_start_server(void) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 276 | { |
| 277 | printf("Using %s device\n", eth_get_name()); |
| 278 | printf("Listening for fastboot command on %pI4\n", &net_ip); |
| 279 | |
Christian Gmeiner | 7fd97aa | 2022-01-13 08:40:06 +0100 | [diff] [blame] | 280 | fastboot_our_port = CONFIG_UDP_FUNCTION_FASTBOOT_PORT; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 281 | |
Simon Glass | 3eaba079 | 2023-02-05 15:39:53 -0700 | [diff] [blame] | 282 | if (IS_ENABLED(CONFIG_FASTBOOT_FLASH)) |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 283 | fastboot_set_progress_callback(fastboot_timed_send_info); |
| 284 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 285 | net_set_udp_handler(fastboot_handler); |
| 286 | |
| 287 | /* zero out server ether in case the server ip has changed */ |
| 288 | memset(net_server_ethaddr, 0, 6); |
| 289 | } |