blob: d6907874787e3482e463066de170fe3d8c354712 [file] [log] [blame]
Alex Kiernand5aa57c2018-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 Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +00008#include <fastboot.h>
9#include <net.h>
Dmitrii Merkurev308252d2023-04-12 19:49:30 +010010#include <net/fastboot_udp.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060011#include <linux/printk.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000012
Alex Kiernand5aa57c2018-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 Kiernand5aa57c2018-05-29 15:30:53 +000044/**
45 * fastboot_udp_send_info() - Send an INFO packet during long commands.
46 *
47 * @msg: String describing the reason for waiting
48 */
49static void fastboot_udp_send_info(const char *msg)
50{
51 uchar *packet;
52 uchar *packet_base;
53 int len = 0;
54 char response[FASTBOOT_RESPONSE_LEN] = {0};
55
56 struct fastboot_header response_header = {
57 .id = FASTBOOT_FASTBOOT,
58 .flags = 0,
59 .seq = htons(sequence_number)
60 };
61 ++sequence_number;
62 packet = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
63 packet_base = packet;
64
65 /* Write headers */
66 memcpy(packet, &response_header, sizeof(response_header));
67 packet += sizeof(response_header);
68 /* Write response */
69 fastboot_response("INFO", response, "%s", msg);
70 memcpy(packet, response, strlen(response));
71 packet += strlen(response);
72
73 len = packet - packet_base;
74
75 /* Save packet for retransmitting */
76 last_packet_len = len;
77 memcpy(last_packet, packet_base, last_packet_len);
78
79 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
80 fastboot_remote_port, fastboot_our_port, len);
81}
82
83/**
84 * fastboot_timed_send_info() - Send INFO packet every 30 seconds
85 *
86 * @msg: String describing the reason for waiting
87 *
88 * Send an INFO packet during long commands based on timer. An INFO packet
89 * is sent if the time is 30 seconds after start. Else, noop.
90 */
91static void fastboot_timed_send_info(const char *msg)
92{
93 static ulong start;
94
95 /* Initialize timer */
96 if (start == 0)
97 start = get_timer(0);
98 ulong time = get_timer(start);
99 /* Send INFO packet to host every 30 seconds */
100 if (time >= 30000) {
101 start = get_timer(0);
102 fastboot_udp_send_info(msg);
103 }
104}
Alex Kiernand5aa57c2018-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;
183 }
184 /*
185 * Sent some INFO packets, need to update sequence number in
186 * header
187 */
188 if (header.seq != sequence_number) {
189 response_header.seq = htons(sequence_number);
190 memcpy(packet_base, &response_header,
191 sizeof(response_header));
192 }
193 /* Write response to packet */
194 memcpy(packet, response, strlen(response));
195 packet += strlen(response);
196 break;
197 default:
198 pr_err("ID %d not implemented.\n", header.id);
199 return;
200 }
201
202 len = packet - packet_base;
203
204 /* Save packet for retransmitting */
205 last_packet_len = len;
206 memcpy(last_packet, packet_base, last_packet_len);
207
208 net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
209 fastboot_remote_port, fastboot_our_port, len);
210
Dmitrii Merkurev07882902023-04-12 19:49:31 +0100211 fastboot_handle_boot(cmd, strncmp("OKAY", response, 4) == 0);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000212
213 if (!strncmp("OKAY", response, 4) || !strncmp("FAIL", response, 4))
214 cmd = -1;
215}
216
217/**
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000218 * fastboot_handler() - Incoming UDP packet handler.
219 *
220 * @packet: Pointer to incoming UDP packet
221 * @dport: Destination UDP port
222 * @sip: Source IP address
223 * @sport: Source UDP port
224 * @len: Packet length
225 */
226static void fastboot_handler(uchar *packet, unsigned int dport,
227 struct in_addr sip, unsigned int sport,
228 unsigned int len)
229{
230 struct fastboot_header header;
231 char fastboot_data[DATA_SIZE] = {0};
232 unsigned int fastboot_data_len = 0;
233
234 if (dport != fastboot_our_port)
235 return;
236
237 fastboot_remote_ip = sip;
238 fastboot_remote_port = sport;
239
240 if (len < sizeof(struct fastboot_header) || len > PACKET_SIZE)
241 return;
242 memcpy(&header, packet, sizeof(header));
243 header.flags = 0;
244 header.seq = ntohs(header.seq);
245 packet += sizeof(header);
246 len -= sizeof(header);
247
248 switch (header.id) {
249 case FASTBOOT_QUERY:
250 fastboot_send(header, fastboot_data, 0, 0);
251 break;
252 case FASTBOOT_INIT:
253 case FASTBOOT_FASTBOOT:
254 fastboot_data_len = len;
255 if (len > 0)
256 memcpy(fastboot_data, packet, len);
257 if (header.seq == sequence_number) {
258 fastboot_send(header, fastboot_data,
259 fastboot_data_len, 0);
260 sequence_number++;
261 } else if (header.seq == sequence_number - 1) {
262 /* Retransmit last sent packet */
263 fastboot_send(header, fastboot_data,
264 fastboot_data_len, 1);
265 }
266 break;
267 default:
268 pr_err("ID %d not implemented.\n", header.id);
269 header.id = FASTBOOT_ERROR;
270 fastboot_send(header, fastboot_data, 0, 0);
271 break;
272 }
273}
274
Dmitrii Merkurev308252d2023-04-12 19:49:30 +0100275void fastboot_udp_start_server(void)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000276{
277 printf("Using %s device\n", eth_get_name());
278 printf("Listening for fastboot command on %pI4\n", &net_ip);
279
Christian Gmeiner7fd97aa2022-01-13 08:40:06 +0100280 fastboot_our_port = CONFIG_UDP_FUNCTION_FASTBOOT_PORT;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000281
Simon Glass3eaba0792023-02-05 15:39:53 -0700282 if (IS_ENABLED(CONFIG_FASTBOOT_FLASH))
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100283 fastboot_set_progress_callback(fastboot_timed_send_info);
284
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000285 net_set_udp_handler(fastboot_handler);
286
287 /* zero out server ether in case the server ip has changed */
288 memset(net_server_ethaddr, 0, 6);
289}