1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
10 #include <net/fastboot.h>
12 /* Fastboot port # defined in spec */
13 #define WELL_KNOWN_PORT 5554
19 FASTBOOT_FASTBOOT = 3,
22 struct __packed fastboot_header {
28 #define PACKET_SIZE 1024
29 #define DATA_SIZE (PACKET_SIZE - sizeof(struct fastboot_header))
31 /* Sequence number sent for every packet */
32 static unsigned short sequence_number = 1;
33 static const unsigned short packet_size = PACKET_SIZE;
34 static const unsigned short udp_version = 1;
36 /* Keep track of last packet for resubmission */
37 static uchar last_packet[PACKET_SIZE];
38 static unsigned int last_packet_len;
40 static struct in_addr fastboot_remote_ip;
41 /* The UDP port at their end */
42 static int fastboot_remote_port;
43 /* The UDP port at our end */
44 static int fastboot_our_port;
46 static void boot_downloaded_image(void);
48 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
50 * fastboot_udp_send_info() - Send an INFO packet during long commands.
52 * @msg: String describing the reason for waiting
54 static void fastboot_udp_send_info(const char *msg)
59 char response[FASTBOOT_RESPONSE_LEN] = {0};
61 struct fastboot_header response_header = {
62 .id = FASTBOOT_FASTBOOT,
64 .seq = htons(sequence_number)
67 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
71 memcpy(packet, &response_header, sizeof(response_header));
72 packet += sizeof(response_header);
74 fastboot_response("INFO", response, "%s", msg);
75 memcpy(packet, response, strlen(response));
76 packet += strlen(response);
78 len = packet - packet_base;
80 /* Save packet for retransmitting */
81 last_packet_len = len;
82 memcpy(last_packet, packet_base, last_packet_len);
84 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
85 fastboot_remote_port, fastboot_our_port, len);
89 * fastboot_timed_send_info() - Send INFO packet every 30 seconds
91 * @msg: String describing the reason for waiting
93 * Send an INFO packet during long commands based on timer. An INFO packet
94 * is sent if the time is 30 seconds after start. Else, noop.
96 static void fastboot_timed_send_info(const char *msg)
100 /* Initialize timer */
102 start = get_timer(0);
103 ulong time = get_timer(start);
104 /* Send INFO packet to host every 30 seconds */
106 start = get_timer(0);
107 fastboot_udp_send_info(msg);
113 * fastboot_send() - Sends a packet in response to received fastboot packet
115 * @header: Header for response packet
116 * @fastboot_data: Pointer to received fastboot data
117 * @fastboot_data_len: Length of received fastboot data
118 * @retransmit: Nonzero if sending last sent packet
120 static void fastboot_send(struct fastboot_header header, char *fastboot_data,
121 unsigned int fastboot_data_len, uchar retransmit)
126 const char *error_msg = "An error occurred.";
128 struct fastboot_header response_header = header;
129 static char command[FASTBOOT_COMMAND_LEN];
131 static bool pending_command;
132 char response[FASTBOOT_RESPONSE_LEN] = {0};
135 * We will always be sending some sort of packet, so
136 * cobble together the packet headers now.
138 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
139 packet_base = packet;
141 /* Resend last packet */
143 memcpy(packet, last_packet, last_packet_len);
144 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
145 fastboot_remote_port, fastboot_our_port,
150 response_header.seq = htons(response_header.seq);
151 memcpy(packet, &response_header, sizeof(response_header));
152 packet += sizeof(response_header);
156 tmp = htons(sequence_number);
157 memcpy(packet, &tmp, sizeof(tmp));
158 packet += sizeof(tmp);
161 tmp = htons(udp_version);
162 memcpy(packet, &tmp, sizeof(tmp));
163 packet += sizeof(tmp);
164 tmp = htons(packet_size);
165 memcpy(packet, &tmp, sizeof(tmp));
166 packet += sizeof(tmp);
169 memcpy(packet, error_msg, strlen(error_msg));
170 packet += strlen(error_msg);
172 case FASTBOOT_FASTBOOT:
173 if (cmd == FASTBOOT_COMMAND_DOWNLOAD) {
174 if (!fastboot_data_len && !fastboot_data_remaining()) {
175 fastboot_data_complete(response);
177 fastboot_data_download(fastboot_data,
181 } else if (!pending_command) {
182 strlcpy(command, fastboot_data,
183 min((size_t)fastboot_data_len + 1,
185 pending_command = true;
187 cmd = fastboot_handle_command(command, response);
188 pending_command = false;
191 * Sent some INFO packets, need to update sequence number in
194 if (header.seq != sequence_number) {
195 response_header.seq = htons(sequence_number);
196 memcpy(packet_base, &response_header,
197 sizeof(response_header));
199 /* Write response to packet */
200 memcpy(packet, response, strlen(response));
201 packet += strlen(response);
204 pr_err("ID %d not implemented.\n", header.id);
208 len = packet - packet_base;
210 /* Save packet for retransmitting */
211 last_packet_len = len;
212 memcpy(last_packet, packet_base, last_packet_len);
214 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
215 fastboot_remote_port, fastboot_our_port, len);
217 /* Continue boot process after sending response */
218 if (!strncmp("OKAY", response, 4)) {
220 case FASTBOOT_COMMAND_BOOT:
221 boot_downloaded_image();
224 case FASTBOOT_COMMAND_CONTINUE:
225 net_set_state(NETLOOP_SUCCESS);
228 case FASTBOOT_COMMAND_REBOOT:
229 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
230 case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
231 case FASTBOOT_COMMAND_REBOOT_RECOVERY:
232 do_reset(NULL, 0, 0, NULL);
237 if (!strncmp("OKAY", response, 4) || !strncmp("FAIL", response, 4))
242 * boot_downloaded_image() - Boots into downloaded image.
244 static void boot_downloaded_image(void)
247 net_set_state(NETLOOP_SUCCESS);
251 * fastboot_handler() - Incoming UDP packet handler.
253 * @packet: Pointer to incoming UDP packet
254 * @dport: Destination UDP port
255 * @sip: Source IP address
256 * @sport: Source UDP port
257 * @len: Packet length
259 static void fastboot_handler(uchar *packet, unsigned int dport,
260 struct in_addr sip, unsigned int sport,
263 struct fastboot_header header;
264 char fastboot_data[DATA_SIZE] = {0};
265 unsigned int fastboot_data_len = 0;
267 if (dport != fastboot_our_port)
270 fastboot_remote_ip = sip;
271 fastboot_remote_port = sport;
273 if (len < sizeof(struct fastboot_header) || len > PACKET_SIZE)
275 memcpy(&header, packet, sizeof(header));
277 header.seq = ntohs(header.seq);
278 packet += sizeof(header);
279 len -= sizeof(header);
283 fastboot_send(header, fastboot_data, 0, 0);
286 case FASTBOOT_FASTBOOT:
287 fastboot_data_len = len;
289 memcpy(fastboot_data, packet, len);
290 if (header.seq == sequence_number) {
291 fastboot_send(header, fastboot_data,
292 fastboot_data_len, 0);
294 } else if (header.seq == sequence_number - 1) {
295 /* Retransmit last sent packet */
296 fastboot_send(header, fastboot_data,
297 fastboot_data_len, 1);
301 pr_err("ID %d not implemented.\n", header.id);
302 header.id = FASTBOOT_ERROR;
303 fastboot_send(header, fastboot_data, 0, 0);
308 void fastboot_start_server(void)
310 printf("Using %s device\n", eth_get_name());
311 printf("Listening for fastboot command on %pI4\n", &net_ip);
313 fastboot_our_port = WELL_KNOWN_PORT;
315 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
316 fastboot_set_progress_callback(fastboot_timed_send_info);
318 net_set_udp_handler(fastboot_handler);
320 /* zero out server ether in case the server ip has changed */
321 memset(net_server_ethaddr, 0, 6);