blob: 6fee441ab3b6e6476911a210bf28edc64bdc55da [file] [log] [blame]
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * Copyright (C) 2016 The Android Open Source Project
4 */
5
6#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -06007#include <command.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +00008#include <fastboot.h>
9#include <net.h>
Dmitrii Merkurev443d3192023-04-12 19:49:30 +010010#include <net/fastboot_udp.h>
Simon Glass1e94b462023-09-14 18:21:46 -060011#include <linux/printk.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000012
Alex Kiernanf73a7df2018-05-29 15:30:53 +000013enum {
14 FASTBOOT_ERROR = 0,
15 FASTBOOT_QUERY = 1,
16 FASTBOOT_INIT = 2,
17 FASTBOOT_FASTBOOT = 3,
18};
19
20struct __packed fastboot_header {
21 uchar id;
22 uchar flags;
23 unsigned short seq;
24};
25
26#define PACKET_SIZE 1024
27#define DATA_SIZE (PACKET_SIZE - sizeof(struct fastboot_header))
28
29/* Sequence number sent for every packet */
30static unsigned short sequence_number = 1;
31static const unsigned short packet_size = PACKET_SIZE;
32static const unsigned short udp_version = 1;
33
34/* Keep track of last packet for resubmission */
35static uchar last_packet[PACKET_SIZE];
36static unsigned int last_packet_len;
37
38static struct in_addr fastboot_remote_ip;
39/* The UDP port at their end */
40static int fastboot_remote_port;
41/* The UDP port at our end */
42static int fastboot_our_port;
43
Alex Kiernanf73a7df2018-05-29 15:30:53 +000044/**
Ion Agorria85fcd692024-01-05 09:22:06 +020045 * fastboot_udp_send_response() - Send an response into UDP
Alex Kiernanf73a7df2018-05-29 15:30:53 +000046 *
Ion Agorria85fcd692024-01-05 09:22:06 +020047 * @response: Response to send
Alex Kiernanf73a7df2018-05-29 15:30:53 +000048 */
Ion Agorria85fcd692024-01-05 09:22:06 +020049static void fastboot_udp_send_response(const char *response)
Alex Kiernanf73a7df2018-05-29 15:30:53 +000050{
51 uchar *packet;
52 uchar *packet_base;
53 int len = 0;
Alex Kiernanf73a7df2018-05-29 15:30:53 +000054
55 struct fastboot_header response_header = {
56 .id = FASTBOOT_FASTBOOT,
57 .flags = 0,
58 .seq = htons(sequence_number)
59 };
60 ++sequence_number;
61 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
62 packet_base = packet;
63
64 /* Write headers */
65 memcpy(packet, &response_header, sizeof(response_header));
66 packet += sizeof(response_header);
67 /* Write response */
Alex Kiernanf73a7df2018-05-29 15:30:53 +000068 memcpy(packet, response, strlen(response));
69 packet += strlen(response);
70
71 len = packet - packet_base;
72
73 /* Save packet for retransmitting */
74 last_packet_len = len;
75 memcpy(last_packet, packet_base, last_packet_len);
76
77 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
78 fastboot_remote_port, fastboot_our_port, len);
79}
80
81/**
82 * fastboot_timed_send_info() - Send INFO packet every 30 seconds
83 *
84 * @msg: String describing the reason for waiting
85 *
86 * Send an INFO packet during long commands based on timer. An INFO packet
87 * is sent if the time is 30 seconds after start. Else, noop.
88 */
89static void fastboot_timed_send_info(const char *msg)
90{
91 static ulong start;
Ion Agorria85fcd692024-01-05 09:22:06 +020092 char response[FASTBOOT_RESPONSE_LEN] = {0};
Alex Kiernanf73a7df2018-05-29 15:30:53 +000093
94 /* Initialize timer */
95 if (start == 0)
96 start = get_timer(0);
97 ulong time = get_timer(start);
98 /* Send INFO packet to host every 30 seconds */
99 if (time >= 30000) {
100 start = get_timer(0);
Ion Agorria85fcd692024-01-05 09:22:06 +0200101 fastboot_response("INFO", response, "%s", msg);
102 fastboot_udp_send_response(response);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000103 }
104}
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000105
106/**
107 * fastboot_send() - Sends a packet in response to received fastboot packet
108 *
109 * @header: Header for response packet
110 * @fastboot_data: Pointer to received fastboot data
111 * @fastboot_data_len: Length of received fastboot data
112 * @retransmit: Nonzero if sending last sent packet
113 */
114static void fastboot_send(struct fastboot_header header, char *fastboot_data,
115 unsigned int fastboot_data_len, uchar retransmit)
116{
117 uchar *packet;
118 uchar *packet_base;
119 int len = 0;
120 const char *error_msg = "An error occurred.";
121 short tmp;
122 struct fastboot_header response_header = header;
123 static char command[FASTBOOT_COMMAND_LEN];
124 static int cmd = -1;
125 static bool pending_command;
126 char response[FASTBOOT_RESPONSE_LEN] = {0};
127
128 /*
129 * We will always be sending some sort of packet, so
130 * cobble together the packet headers now.
131 */
132 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
133 packet_base = packet;
134
135 /* Resend last packet */
136 if (retransmit) {
137 memcpy(packet, last_packet, last_packet_len);
138 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
139 fastboot_remote_port, fastboot_our_port,
140 last_packet_len);
141 return;
142 }
143
144 response_header.seq = htons(response_header.seq);
145 memcpy(packet, &response_header, sizeof(response_header));
146 packet += sizeof(response_header);
147
148 switch (header.id) {
149 case FASTBOOT_QUERY:
150 tmp = htons(sequence_number);
151 memcpy(packet, &tmp, sizeof(tmp));
152 packet += sizeof(tmp);
153 break;
154 case FASTBOOT_INIT:
155 tmp = htons(udp_version);
156 memcpy(packet, &tmp, sizeof(tmp));
157 packet += sizeof(tmp);
158 tmp = htons(packet_size);
159 memcpy(packet, &tmp, sizeof(tmp));
160 packet += sizeof(tmp);
161 break;
162 case FASTBOOT_ERROR:
163 memcpy(packet, error_msg, strlen(error_msg));
164 packet += strlen(error_msg);
165 break;
166 case FASTBOOT_FASTBOOT:
167 if (cmd == FASTBOOT_COMMAND_DOWNLOAD) {
168 if (!fastboot_data_len && !fastboot_data_remaining()) {
169 fastboot_data_complete(response);
170 } else {
171 fastboot_data_download(fastboot_data,
172 fastboot_data_len,
173 response);
174 }
175 } else if (!pending_command) {
176 strlcpy(command, fastboot_data,
177 min((size_t)fastboot_data_len + 1,
178 sizeof(command)));
179 pending_command = true;
180 } else {
181 cmd = fastboot_handle_command(command, response);
182 pending_command = false;
Ion Agorria85fcd692024-01-05 09:22:06 +0200183
184 if (!strncmp(FASTBOOT_MULTIRESPONSE_START, response, 4)) {
185 while (1) {
186 /* Call handler to obtain next response */
187 fastboot_multiresponse(cmd, response);
188
189 /*
190 * Send more responses or break to send
191 * final OKAY/FAIL response
192 */
193 if (strncmp("OKAY", response, 4) &&
194 strncmp("FAIL", response, 4))
195 fastboot_udp_send_response(response);
196 else
197 break;
198 }
199 }
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000200 }
201 /*
202 * Sent some INFO packets, need to update sequence number in
203 * header
204 */
205 if (header.seq != sequence_number) {
206 response_header.seq = htons(sequence_number);
207 memcpy(packet_base, &response_header,
208 sizeof(response_header));
209 }
210 /* Write response to packet */
211 memcpy(packet, response, strlen(response));
212 packet += strlen(response);
213 break;
214 default:
215 pr_err("ID %d not implemented.\n", header.id);
216 return;
217 }
218
219 len = packet - packet_base;
220
221 /* Save packet for retransmitting */
222 last_packet_len = len;
223 memcpy(last_packet, packet_base, last_packet_len);
224
225 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
226 fastboot_remote_port, fastboot_our_port, len);
227
Dmitrii Merkurevc8acbbb2023-04-12 19:49:31 +0100228 fastboot_handle_boot(cmd, strncmp("OKAY", response, 4) == 0);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000229
230 if (!strncmp("OKAY", response, 4) || !strncmp("FAIL", response, 4))
231 cmd = -1;
232}
233
234/**
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000235 * fastboot_handler() - Incoming UDP packet handler.
236 *
237 * @packet: Pointer to incoming UDP packet
238 * @dport: Destination UDP port
239 * @sip: Source IP address
240 * @sport: Source UDP port
241 * @len: Packet length
242 */
243static void fastboot_handler(uchar *packet, unsigned int dport,
244 struct in_addr sip, unsigned int sport,
245 unsigned int len)
246{
247 struct fastboot_header header;
248 char fastboot_data[DATA_SIZE] = {0};
249 unsigned int fastboot_data_len = 0;
250
251 if (dport != fastboot_our_port)
252 return;
253
254 fastboot_remote_ip = sip;
255 fastboot_remote_port = sport;
256
257 if (len < sizeof(struct fastboot_header) || len > PACKET_SIZE)
258 return;
259 memcpy(&header, packet, sizeof(header));
260 header.flags = 0;
261 header.seq = ntohs(header.seq);
262 packet += sizeof(header);
263 len -= sizeof(header);
264
265 switch (header.id) {
266 case FASTBOOT_QUERY:
267 fastboot_send(header, fastboot_data, 0, 0);
268 break;
269 case FASTBOOT_INIT:
270 case FASTBOOT_FASTBOOT:
271 fastboot_data_len = len;
272 if (len > 0)
273 memcpy(fastboot_data, packet, len);
274 if (header.seq == sequence_number) {
275 fastboot_send(header, fastboot_data,
276 fastboot_data_len, 0);
277 sequence_number++;
278 } else if (header.seq == sequence_number - 1) {
279 /* Retransmit last sent packet */
280 fastboot_send(header, fastboot_data,
281 fastboot_data_len, 1);
282 }
283 break;
284 default:
285 pr_err("ID %d not implemented.\n", header.id);
286 header.id = FASTBOOT_ERROR;
287 fastboot_send(header, fastboot_data, 0, 0);
288 break;
289 }
290}
291
Dmitrii Merkurev443d3192023-04-12 19:49:30 +0100292void fastboot_udp_start_server(void)
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000293{
294 printf("Using %s device\n", eth_get_name());
295 printf("Listening for fastboot command on %pI4\n", &net_ip);
296
Christian Gmeiner046bf8d2022-01-13 08:40:06 +0100297 fastboot_our_port = CONFIG_UDP_FUNCTION_FASTBOOT_PORT;
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000298
Simon Glass64d6bfb2023-02-05 15:39:53 -0700299 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH))
Patrick Delaunayd0379902022-12-15 10:15:50 +0100300 fastboot_set_progress_callback(fastboot_timed_send_info);
301
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000302 net_set_udp_handler(fastboot_handler);
303
304 /* zero out server ether in case the server ip has changed */
305 memset(net_server_ethaddr, 0, 6);
306}