1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2016 The Android Open Source Project
9 #include <net/fastboot.h>
11 /* Fastboot port # defined in spec */
12 #define WELL_KNOWN_PORT 5554
18 FASTBOOT_FASTBOOT = 3,
21 struct __packed fastboot_header {
27 #define PACKET_SIZE 1024
28 #define DATA_SIZE (PACKET_SIZE - sizeof(struct fastboot_header))
30 /* Sequence number sent for every packet */
31 static unsigned short sequence_number = 1;
32 static const unsigned short packet_size = PACKET_SIZE;
33 static const unsigned short udp_version = 1;
35 /* Keep track of last packet for resubmission */
36 static uchar last_packet[PACKET_SIZE];
37 static unsigned int last_packet_len;
39 static struct in_addr fastboot_remote_ip;
40 /* The UDP port at their end */
41 static int fastboot_remote_port;
42 /* The UDP port at our end */
43 static int fastboot_our_port;
45 static void boot_downloaded_image(void);
47 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
49 * fastboot_udp_send_info() - Send an INFO packet during long commands.
51 * @msg: String describing the reason for waiting
53 static void fastboot_udp_send_info(const char *msg)
58 char response[FASTBOOT_RESPONSE_LEN] = {0};
60 struct fastboot_header response_header = {
61 .id = FASTBOOT_FASTBOOT,
63 .seq = htons(sequence_number)
66 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
70 memcpy(packet, &response_header, sizeof(response_header));
71 packet += sizeof(response_header);
73 fastboot_response("INFO", response, "%s", msg);
74 memcpy(packet, response, strlen(response));
75 packet += strlen(response);
77 len = packet - packet_base;
79 /* Save packet for retransmitting */
80 last_packet_len = len;
81 memcpy(last_packet, packet_base, last_packet_len);
83 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
84 fastboot_remote_port, fastboot_our_port, len);
88 * fastboot_timed_send_info() - Send INFO packet every 30 seconds
90 * @msg: String describing the reason for waiting
92 * Send an INFO packet during long commands based on timer. An INFO packet
93 * is sent if the time is 30 seconds after start. Else, noop.
95 static void fastboot_timed_send_info(const char *msg)
99 /* Initialize timer */
101 start = get_timer(0);
102 ulong time = get_timer(start);
103 /* Send INFO packet to host every 30 seconds */
105 start = get_timer(0);
106 fastboot_udp_send_info(msg);
112 * fastboot_send() - Sends a packet in response to received fastboot packet
114 * @header: Header for response packet
115 * @fastboot_data: Pointer to received fastboot data
116 * @fastboot_data_len: Length of received fastboot data
117 * @retransmit: Nonzero if sending last sent packet
119 static void fastboot_send(struct fastboot_header header, char *fastboot_data,
120 unsigned int fastboot_data_len, uchar retransmit)
125 const char *error_msg = "An error occurred.";
127 struct fastboot_header response_header = header;
128 static char command[FASTBOOT_COMMAND_LEN];
130 static bool pending_command;
131 char response[FASTBOOT_RESPONSE_LEN] = {0};
134 * We will always be sending some sort of packet, so
135 * cobble together the packet headers now.
137 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
138 packet_base = packet;
140 /* Resend last packet */
142 memcpy(packet, last_packet, last_packet_len);
143 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
144 fastboot_remote_port, fastboot_our_port,
149 response_header.seq = htons(response_header.seq);
150 memcpy(packet, &response_header, sizeof(response_header));
151 packet += sizeof(response_header);
155 tmp = htons(sequence_number);
156 memcpy(packet, &tmp, sizeof(tmp));
157 packet += sizeof(tmp);
160 tmp = htons(udp_version);
161 memcpy(packet, &tmp, sizeof(tmp));
162 packet += sizeof(tmp);
163 tmp = htons(packet_size);
164 memcpy(packet, &tmp, sizeof(tmp));
165 packet += sizeof(tmp);
168 memcpy(packet, error_msg, strlen(error_msg));
169 packet += strlen(error_msg);
171 case FASTBOOT_FASTBOOT:
172 if (cmd == FASTBOOT_COMMAND_DOWNLOAD) {
173 if (!fastboot_data_len && !fastboot_data_remaining()) {
174 fastboot_data_complete(response);
176 fastboot_data_download(fastboot_data,
180 } else if (!pending_command) {
181 strlcpy(command, fastboot_data,
182 min((size_t)fastboot_data_len + 1,
184 pending_command = true;
186 cmd = fastboot_handle_command(command, response);
187 pending_command = false;
190 * Sent some INFO packets, need to update sequence number in
193 if (header.seq != sequence_number) {
194 response_header.seq = htons(sequence_number);
195 memcpy(packet_base, &response_header,
196 sizeof(response_header));
198 /* Write response to packet */
199 memcpy(packet, response, strlen(response));
200 packet += strlen(response);
203 pr_err("ID %d not implemented.\n", header.id);
207 len = packet - packet_base;
209 /* Save packet for retransmitting */
210 last_packet_len = len;
211 memcpy(last_packet, packet_base, last_packet_len);
213 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
214 fastboot_remote_port, fastboot_our_port, len);
216 /* Continue boot process after sending response */
217 if (!strncmp("OKAY", response, 4)) {
219 case FASTBOOT_COMMAND_BOOT:
220 boot_downloaded_image();
223 case FASTBOOT_COMMAND_CONTINUE:
224 net_set_state(NETLOOP_SUCCESS);
227 case FASTBOOT_COMMAND_REBOOT:
228 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
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 = WELL_KNOWN_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);