1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
10 #include <net/fastboot.h>
16 FASTBOOT_FASTBOOT = 3,
19 struct __packed fastboot_header {
25 #define PACKET_SIZE 1024
26 #define DATA_SIZE (PACKET_SIZE - sizeof(struct fastboot_header))
28 /* Sequence number sent for every packet */
29 static unsigned short sequence_number = 1;
30 static const unsigned short packet_size = PACKET_SIZE;
31 static const unsigned short udp_version = 1;
33 /* Keep track of last packet for resubmission */
34 static uchar last_packet[PACKET_SIZE];
35 static unsigned int last_packet_len;
37 static struct in_addr fastboot_remote_ip;
38 /* The UDP port at their end */
39 static int fastboot_remote_port;
40 /* The UDP port at our end */
41 static int fastboot_our_port;
43 static void boot_downloaded_image(void);
46 * fastboot_udp_send_info() - Send an INFO packet during long commands.
48 * @msg: String describing the reason for waiting
50 static void fastboot_udp_send_info(const char *msg)
55 char response[FASTBOOT_RESPONSE_LEN] = {0};
57 struct fastboot_header response_header = {
58 .id = FASTBOOT_FASTBOOT,
60 .seq = htons(sequence_number)
63 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
67 memcpy(packet, &response_header, sizeof(response_header));
68 packet += sizeof(response_header);
70 fastboot_response("INFO", response, "%s", msg);
71 memcpy(packet, response, strlen(response));
72 packet += strlen(response);
74 len = packet - packet_base;
76 /* Save packet for retransmitting */
77 last_packet_len = len;
78 memcpy(last_packet, packet_base, last_packet_len);
80 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
81 fastboot_remote_port, fastboot_our_port, len);
85 * fastboot_timed_send_info() - Send INFO packet every 30 seconds
87 * @msg: String describing the reason for waiting
89 * Send an INFO packet during long commands based on timer. An INFO packet
90 * is sent if the time is 30 seconds after start. Else, noop.
92 static void fastboot_timed_send_info(const char *msg)
96 /* Initialize timer */
99 ulong time = get_timer(start);
100 /* Send INFO packet to host every 30 seconds */
102 start = get_timer(0);
103 fastboot_udp_send_info(msg);
108 * fastboot_send() - Sends a packet in response to received fastboot packet
110 * @header: Header for response packet
111 * @fastboot_data: Pointer to received fastboot data
112 * @fastboot_data_len: Length of received fastboot data
113 * @retransmit: Nonzero if sending last sent packet
115 static void fastboot_send(struct fastboot_header header, char *fastboot_data,
116 unsigned int fastboot_data_len, uchar retransmit)
121 const char *error_msg = "An error occurred.";
123 struct fastboot_header response_header = header;
124 static char command[FASTBOOT_COMMAND_LEN];
126 static bool pending_command;
127 char response[FASTBOOT_RESPONSE_LEN] = {0};
130 * We will always be sending some sort of packet, so
131 * cobble together the packet headers now.
133 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
134 packet_base = packet;
136 /* Resend last packet */
138 memcpy(packet, last_packet, last_packet_len);
139 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
140 fastboot_remote_port, fastboot_our_port,
145 response_header.seq = htons(response_header.seq);
146 memcpy(packet, &response_header, sizeof(response_header));
147 packet += sizeof(response_header);
151 tmp = htons(sequence_number);
152 memcpy(packet, &tmp, sizeof(tmp));
153 packet += sizeof(tmp);
156 tmp = htons(udp_version);
157 memcpy(packet, &tmp, sizeof(tmp));
158 packet += sizeof(tmp);
159 tmp = htons(packet_size);
160 memcpy(packet, &tmp, sizeof(tmp));
161 packet += sizeof(tmp);
164 memcpy(packet, error_msg, strlen(error_msg));
165 packet += strlen(error_msg);
167 case FASTBOOT_FASTBOOT:
168 if (cmd == FASTBOOT_COMMAND_DOWNLOAD) {
169 if (!fastboot_data_len && !fastboot_data_remaining()) {
170 fastboot_data_complete(response);
172 fastboot_data_download(fastboot_data,
176 } else if (!pending_command) {
177 strlcpy(command, fastboot_data,
178 min((size_t)fastboot_data_len + 1,
180 pending_command = true;
182 cmd = fastboot_handle_command(command, response);
183 pending_command = false;
186 * Sent some INFO packets, need to update sequence number in
189 if (header.seq != sequence_number) {
190 response_header.seq = htons(sequence_number);
191 memcpy(packet_base, &response_header,
192 sizeof(response_header));
194 /* Write response to packet */
195 memcpy(packet, response, strlen(response));
196 packet += strlen(response);
199 pr_err("ID %d not implemented.\n", header.id);
203 len = packet - packet_base;
205 /* Save packet for retransmitting */
206 last_packet_len = len;
207 memcpy(last_packet, packet_base, last_packet_len);
209 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
210 fastboot_remote_port, fastboot_our_port, len);
212 /* Continue boot process after sending response */
213 if (!strncmp("OKAY", response, 4)) {
215 case FASTBOOT_COMMAND_BOOT:
216 boot_downloaded_image();
219 case FASTBOOT_COMMAND_CONTINUE:
220 net_set_state(NETLOOP_SUCCESS);
223 case FASTBOOT_COMMAND_REBOOT:
224 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
225 case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
226 case FASTBOOT_COMMAND_REBOOT_RECOVERY:
227 do_reset(NULL, 0, 0, NULL);
232 if (!strncmp("OKAY", response, 4) || !strncmp("FAIL", response, 4))
237 * boot_downloaded_image() - Boots into downloaded image.
239 static void boot_downloaded_image(void)
242 net_set_state(NETLOOP_SUCCESS);
246 * fastboot_handler() - Incoming UDP packet handler.
248 * @packet: Pointer to incoming UDP packet
249 * @dport: Destination UDP port
250 * @sip: Source IP address
251 * @sport: Source UDP port
252 * @len: Packet length
254 static void fastboot_handler(uchar *packet, unsigned int dport,
255 struct in_addr sip, unsigned int sport,
258 struct fastboot_header header;
259 char fastboot_data[DATA_SIZE] = {0};
260 unsigned int fastboot_data_len = 0;
262 if (dport != fastboot_our_port)
265 fastboot_remote_ip = sip;
266 fastboot_remote_port = sport;
268 if (len < sizeof(struct fastboot_header) || len > PACKET_SIZE)
270 memcpy(&header, packet, sizeof(header));
272 header.seq = ntohs(header.seq);
273 packet += sizeof(header);
274 len -= sizeof(header);
278 fastboot_send(header, fastboot_data, 0, 0);
281 case FASTBOOT_FASTBOOT:
282 fastboot_data_len = len;
284 memcpy(fastboot_data, packet, len);
285 if (header.seq == sequence_number) {
286 fastboot_send(header, fastboot_data,
287 fastboot_data_len, 0);
289 } else if (header.seq == sequence_number - 1) {
290 /* Retransmit last sent packet */
291 fastboot_send(header, fastboot_data,
292 fastboot_data_len, 1);
296 pr_err("ID %d not implemented.\n", header.id);
297 header.id = FASTBOOT_ERROR;
298 fastboot_send(header, fastboot_data, 0, 0);
303 void fastboot_start_server(void)
305 printf("Using %s device\n", eth_get_name());
306 printf("Listening for fastboot command on %pI4\n", &net_ip);
308 fastboot_our_port = CONFIG_UDP_FUNCTION_FASTBOOT_PORT;
310 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH))
311 fastboot_set_progress_callback(fastboot_timed_send_info);
313 net_set_udp_handler(fastboot_handler);
315 /* zero out server ether in case the server ip has changed */
316 memset(net_server_ethaddr, 0, 6);