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 do_reset(NULL, 0, 0, NULL);
235 if (!strncmp("OKAY", response, 4) || !strncmp("FAIL", response, 4))
240 * boot_downloaded_image() - Boots into downloaded image.
242 static void boot_downloaded_image(void)
245 net_set_state(NETLOOP_SUCCESS);
249 * fastboot_handler() - Incoming UDP packet handler.
251 * @packet: Pointer to incoming UDP packet
252 * @dport: Destination UDP port
253 * @sip: Source IP address
254 * @sport: Source UDP port
255 * @len: Packet length
257 static void fastboot_handler(uchar *packet, unsigned int dport,
258 struct in_addr sip, unsigned int sport,
261 struct fastboot_header header;
262 char fastboot_data[DATA_SIZE] = {0};
263 unsigned int fastboot_data_len = 0;
265 if (dport != fastboot_our_port)
268 fastboot_remote_ip = sip;
269 fastboot_remote_port = sport;
271 if (len < sizeof(struct fastboot_header) || len > PACKET_SIZE)
273 memcpy(&header, packet, sizeof(header));
275 header.seq = ntohs(header.seq);
276 packet += sizeof(header);
277 len -= sizeof(header);
281 fastboot_send(header, fastboot_data, 0, 0);
284 case FASTBOOT_FASTBOOT:
285 fastboot_data_len = len;
287 memcpy(fastboot_data, packet, len);
288 if (header.seq == sequence_number) {
289 fastboot_send(header, fastboot_data,
290 fastboot_data_len, 0);
292 } else if (header.seq == sequence_number - 1) {
293 /* Retransmit last sent packet */
294 fastboot_send(header, fastboot_data,
295 fastboot_data_len, 1);
299 pr_err("ID %d not implemented.\n", header.id);
300 header.id = FASTBOOT_ERROR;
301 fastboot_send(header, fastboot_data, 0, 0);
306 void fastboot_start_server(void)
308 printf("Using %s device\n", eth_get_name());
309 printf("Listening for fastboot command on %pI4\n", &net_ip);
311 fastboot_our_port = WELL_KNOWN_PORT;
313 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
314 fastboot_set_progress_callback(fastboot_timed_send_info);
316 net_set_udp_handler(fastboot_handler);
318 /* zero out server ether in case the server ip has changed */
319 memset(net_server_ethaddr, 0, 6);