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);
45 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
47 * fastboot_udp_send_info() - Send an INFO packet during long commands.
49 * @msg: String describing the reason for waiting
51 static void fastboot_udp_send_info(const char *msg)
56 char response[FASTBOOT_RESPONSE_LEN] = {0};
58 struct fastboot_header response_header = {
59 .id = FASTBOOT_FASTBOOT,
61 .seq = htons(sequence_number)
64 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
68 memcpy(packet, &response_header, sizeof(response_header));
69 packet += sizeof(response_header);
71 fastboot_response("INFO", response, "%s", msg);
72 memcpy(packet, response, strlen(response));
73 packet += strlen(response);
75 len = packet - packet_base;
77 /* Save packet for retransmitting */
78 last_packet_len = len;
79 memcpy(last_packet, packet_base, last_packet_len);
81 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
82 fastboot_remote_port, fastboot_our_port, len);
86 * fastboot_timed_send_info() - Send INFO packet every 30 seconds
88 * @msg: String describing the reason for waiting
90 * Send an INFO packet during long commands based on timer. An INFO packet
91 * is sent if the time is 30 seconds after start. Else, noop.
93 static void fastboot_timed_send_info(const char *msg)
97 /* Initialize timer */
100 ulong time = get_timer(start);
101 /* Send INFO packet to host every 30 seconds */
103 start = get_timer(0);
104 fastboot_udp_send_info(msg);
110 * fastboot_send() - Sends a packet in response to received fastboot packet
112 * @header: Header for response packet
113 * @fastboot_data: Pointer to received fastboot data
114 * @fastboot_data_len: Length of received fastboot data
115 * @retransmit: Nonzero if sending last sent packet
117 static void fastboot_send(struct fastboot_header header, char *fastboot_data,
118 unsigned int fastboot_data_len, uchar retransmit)
123 const char *error_msg = "An error occurred.";
125 struct fastboot_header response_header = header;
126 static char command[FASTBOOT_COMMAND_LEN];
128 static bool pending_command;
129 char response[FASTBOOT_RESPONSE_LEN] = {0};
132 * We will always be sending some sort of packet, so
133 * cobble together the packet headers now.
135 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
136 packet_base = packet;
138 /* Resend last packet */
140 memcpy(packet, last_packet, last_packet_len);
141 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
142 fastboot_remote_port, fastboot_our_port,
147 response_header.seq = htons(response_header.seq);
148 memcpy(packet, &response_header, sizeof(response_header));
149 packet += sizeof(response_header);
153 tmp = htons(sequence_number);
154 memcpy(packet, &tmp, sizeof(tmp));
155 packet += sizeof(tmp);
158 tmp = htons(udp_version);
159 memcpy(packet, &tmp, sizeof(tmp));
160 packet += sizeof(tmp);
161 tmp = htons(packet_size);
162 memcpy(packet, &tmp, sizeof(tmp));
163 packet += sizeof(tmp);
166 memcpy(packet, error_msg, strlen(error_msg));
167 packet += strlen(error_msg);
169 case FASTBOOT_FASTBOOT:
170 if (cmd == FASTBOOT_COMMAND_DOWNLOAD) {
171 if (!fastboot_data_len && !fastboot_data_remaining()) {
172 fastboot_data_complete(response);
174 fastboot_data_download(fastboot_data,
178 } else if (!pending_command) {
179 strlcpy(command, fastboot_data,
180 min((size_t)fastboot_data_len + 1,
182 pending_command = true;
184 cmd = fastboot_handle_command(command, response);
185 pending_command = false;
188 * Sent some INFO packets, need to update sequence number in
191 if (header.seq != sequence_number) {
192 response_header.seq = htons(sequence_number);
193 memcpy(packet_base, &response_header,
194 sizeof(response_header));
196 /* Write response to packet */
197 memcpy(packet, response, strlen(response));
198 packet += strlen(response);
201 pr_err("ID %d not implemented.\n", header.id);
205 len = packet - packet_base;
207 /* Save packet for retransmitting */
208 last_packet_len = len;
209 memcpy(last_packet, packet_base, last_packet_len);
211 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
212 fastboot_remote_port, fastboot_our_port, len);
214 /* Continue boot process after sending response */
215 if (!strncmp("OKAY", response, 4)) {
217 case FASTBOOT_COMMAND_BOOT:
218 boot_downloaded_image();
221 case FASTBOOT_COMMAND_CONTINUE:
222 net_set_state(NETLOOP_SUCCESS);
225 case FASTBOOT_COMMAND_REBOOT:
226 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
227 case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
228 case FASTBOOT_COMMAND_REBOOT_RECOVERY:
229 do_reset(NULL, 0, 0, NULL);
234 if (!strncmp("OKAY", response, 4) || !strncmp("FAIL", response, 4))
239 * boot_downloaded_image() - Boots into downloaded image.
241 static void boot_downloaded_image(void)
244 net_set_state(NETLOOP_SUCCESS);
248 * fastboot_handler() - Incoming UDP packet handler.
250 * @packet: Pointer to incoming UDP packet
251 * @dport: Destination UDP port
252 * @sip: Source IP address
253 * @sport: Source UDP port
254 * @len: Packet length
256 static void fastboot_handler(uchar *packet, unsigned int dport,
257 struct in_addr sip, unsigned int sport,
260 struct fastboot_header header;
261 char fastboot_data[DATA_SIZE] = {0};
262 unsigned int fastboot_data_len = 0;
264 if (dport != fastboot_our_port)
267 fastboot_remote_ip = sip;
268 fastboot_remote_port = sport;
270 if (len < sizeof(struct fastboot_header) || len > PACKET_SIZE)
272 memcpy(&header, packet, sizeof(header));
274 header.seq = ntohs(header.seq);
275 packet += sizeof(header);
276 len -= sizeof(header);
280 fastboot_send(header, fastboot_data, 0, 0);
283 case FASTBOOT_FASTBOOT:
284 fastboot_data_len = len;
286 memcpy(fastboot_data, packet, len);
287 if (header.seq == sequence_number) {
288 fastboot_send(header, fastboot_data,
289 fastboot_data_len, 0);
291 } else if (header.seq == sequence_number - 1) {
292 /* Retransmit last sent packet */
293 fastboot_send(header, fastboot_data,
294 fastboot_data_len, 1);
298 pr_err("ID %d not implemented.\n", header.id);
299 header.id = FASTBOOT_ERROR;
300 fastboot_send(header, fastboot_data, 0, 0);
305 void fastboot_start_server(void)
307 printf("Using %s device\n", eth_get_name());
308 printf("Listening for fastboot command on %pI4\n", &net_ip);
310 fastboot_our_port = CONFIG_UDP_FUNCTION_FASTBOOT_PORT;
312 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
313 fastboot_set_progress_callback(fastboot_timed_send_info);
315 net_set_udp_handler(fastboot_handler);
317 /* zero out server ether in case the server ip has changed */
318 memset(net_server_ethaddr, 0, 6);