- Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathfastboot.c
317 lines (278 loc) · 8.07 KB
/
fastboot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (C) 2016 The Android Open Source Project
*/
#include<common.h>
#include<command.h>
#include<fastboot.h>
#include<net.h>
#include<net/fastboot.h>
enum {
FASTBOOT_ERROR=0,
FASTBOOT_QUERY=1,
FASTBOOT_INIT=2,
FASTBOOT_FASTBOOT=3,
};
struct__packedfastboot_header {
ucharid;
ucharflags;
unsigned shortseq;
};
#definePACKET_SIZE 1024
#defineDATA_SIZE (PACKET_SIZE - sizeof(struct fastboot_header))
/* Sequence number sent for every packet */
staticunsigned shortsequence_number=1;
staticconstunsigned shortpacket_size=PACKET_SIZE;
staticconstunsigned shortudp_version=1;
/* Keep track of last packet for resubmission */
staticucharlast_packet[PACKET_SIZE];
staticunsigned intlast_packet_len;
staticstructin_addrfastboot_remote_ip;
/* The UDP port at their end */
staticintfastboot_remote_port;
/* The UDP port at our end */
staticintfastboot_our_port;
staticvoidboot_downloaded_image(void);
/**
* fastboot_udp_send_info() - Send an INFO packet during long commands.
*
* @msg: String describing the reason for waiting
*/
staticvoidfastboot_udp_send_info(constchar*msg)
{
uchar*packet;
uchar*packet_base;
intlen=0;
charresponse[FASTBOOT_RESPONSE_LEN] = {0};
structfastboot_headerresponse_header= {
.id=FASTBOOT_FASTBOOT,
.flags=0,
.seq=htons(sequence_number)
};
++sequence_number;
packet=net_tx_packet+net_eth_hdr_size() +IP_UDP_HDR_SIZE;
packet_base=packet;
/* Write headers */
memcpy(packet, &response_header, sizeof(response_header));
packet+=sizeof(response_header);
/* Write response */
fastboot_response("INFO", response, "%s", msg);
memcpy(packet, response, strlen(response));
packet+=strlen(response);
len=packet-packet_base;
/* Save packet for retransmitting */
last_packet_len=len;
memcpy(last_packet, packet_base, last_packet_len);
net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
fastboot_remote_port, fastboot_our_port, len);
}
/**
* fastboot_timed_send_info() - Send INFO packet every 30 seconds
*
* @msg: String describing the reason for waiting
*
* Send an INFO packet during long commands based on timer. An INFO packet
* is sent if the time is 30 seconds after start. Else, noop.
*/
staticvoidfastboot_timed_send_info(constchar*msg)
{
staticulongstart;
/* Initialize timer */
if (start==0)
start=get_timer(0);
ulongtime=get_timer(start);
/* Send INFO packet to host every 30 seconds */
if (time >= 30000) {
start=get_timer(0);
fastboot_udp_send_info(msg);
}
}
/**
* fastboot_send() - Sends a packet in response to received fastboot packet
*
* @header: Header for response packet
* @fastboot_data: Pointer to received fastboot data
* @fastboot_data_len: Length of received fastboot data
* @retransmit: Nonzero if sending last sent packet
*/
staticvoidfastboot_send(structfastboot_headerheader, char*fastboot_data,
unsigned intfastboot_data_len, ucharretransmit)
{
uchar*packet;
uchar*packet_base;
intlen=0;
constchar*error_msg="An error occurred.";
shorttmp;
structfastboot_headerresponse_header=header;
staticcharcommand[FASTBOOT_COMMAND_LEN];
staticintcmd=-1;
staticboolpending_command;
charresponse[FASTBOOT_RESPONSE_LEN] = {0};
/*
* We will always be sending some sort of packet, so
* cobble together the packet headers now.
*/
packet=net_tx_packet+net_eth_hdr_size() +IP_UDP_HDR_SIZE;
packet_base=packet;
/* Resend last packet */
if (retransmit) {
memcpy(packet, last_packet, last_packet_len);
net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
fastboot_remote_port, fastboot_our_port,
last_packet_len);
return;
}
response_header.seq=htons(response_header.seq);
memcpy(packet, &response_header, sizeof(response_header));
packet+=sizeof(response_header);
switch (header.id) {
caseFASTBOOT_QUERY:
tmp=htons(sequence_number);
memcpy(packet, &tmp, sizeof(tmp));
packet+=sizeof(tmp);
break;
caseFASTBOOT_INIT:
tmp=htons(udp_version);
memcpy(packet, &tmp, sizeof(tmp));
packet+=sizeof(tmp);
tmp=htons(packet_size);
memcpy(packet, &tmp, sizeof(tmp));
packet+=sizeof(tmp);
break;
caseFASTBOOT_ERROR:
memcpy(packet, error_msg, strlen(error_msg));
packet+=strlen(error_msg);
break;
caseFASTBOOT_FASTBOOT:
if (cmd==FASTBOOT_COMMAND_DOWNLOAD) {
if (!fastboot_data_len&& !fastboot_data_remaining()) {
fastboot_data_complete(response);
} else {
fastboot_data_download(fastboot_data,
fastboot_data_len,
response);
}
} elseif (!pending_command) {
strlcpy(command, fastboot_data,
min((size_t)fastboot_data_len+1,
sizeof(command)));
pending_command= true;
} else {
cmd=fastboot_handle_command(command, response);
pending_command= false;
}
/*
* Sent some INFO packets, need to update sequence number in
* header
*/
if (header.seq!=sequence_number) {
response_header.seq=htons(sequence_number);
memcpy(packet_base, &response_header,
sizeof(response_header));
}
/* Write response to packet */
memcpy(packet, response, strlen(response));
packet+=strlen(response);
break;
default:
pr_err("ID %d not implemented.\n", header.id);
return;
}
len=packet-packet_base;
/* Save packet for retransmitting */
last_packet_len=len;
memcpy(last_packet, packet_base, last_packet_len);
net_send_udp_packet(net_server_ethaddr, fastboot_remote_ip,
fastboot_remote_port, fastboot_our_port, len);
/* Continue boot process after sending response */
if (!strncmp("OKAY", response, 4)) {
switch (cmd) {
caseFASTBOOT_COMMAND_BOOT:
boot_downloaded_image();
break;
caseFASTBOOT_COMMAND_CONTINUE:
net_set_state(NETLOOP_SUCCESS);
break;
caseFASTBOOT_COMMAND_REBOOT:
caseFASTBOOT_COMMAND_REBOOT_BOOTLOADER:
caseFASTBOOT_COMMAND_REBOOT_FASTBOOTD:
caseFASTBOOT_COMMAND_REBOOT_RECOVERY:
do_reset(NULL, 0, 0, NULL);
break;
}
}
if (!strncmp("OKAY", response, 4) || !strncmp("FAIL", response, 4))
cmd=-1;
}
/**
* boot_downloaded_image() - Boots into downloaded image.
*/
staticvoidboot_downloaded_image(void)
{
fastboot_boot();
net_set_state(NETLOOP_SUCCESS);
}
/**
* fastboot_handler() - Incoming UDP packet handler.
*
* @packet: Pointer to incoming UDP packet
* @dport: Destination UDP port
* @sip: Source IP address
* @sport: Source UDP port
* @len: Packet length
*/
staticvoidfastboot_handler(uchar*packet, unsigned intdport,
structin_addrsip, unsigned intsport,
unsigned intlen)
{
structfastboot_headerheader;
charfastboot_data[DATA_SIZE] = {0};
unsigned intfastboot_data_len=0;
if (dport!=fastboot_our_port)
return;
fastboot_remote_ip=sip;
fastboot_remote_port=sport;
if (len<sizeof(structfastboot_header) ||len>PACKET_SIZE)
return;
memcpy(&header, packet, sizeof(header));
header.flags=0;
header.seq=ntohs(header.seq);
packet+=sizeof(header);
len-=sizeof(header);
switch (header.id) {
caseFASTBOOT_QUERY:
fastboot_send(header, fastboot_data, 0, 0);
break;
caseFASTBOOT_INIT:
caseFASTBOOT_FASTBOOT:
fastboot_data_len=len;
if (len>0)
memcpy(fastboot_data, packet, len);
if (header.seq==sequence_number) {
fastboot_send(header, fastboot_data,
fastboot_data_len, 0);
sequence_number++;
} elseif (header.seq==sequence_number-1) {
/* Retransmit last sent packet */
fastboot_send(header, fastboot_data,
fastboot_data_len, 1);
}
break;
default:
pr_err("ID %d not implemented.\n", header.id);
header.id=FASTBOOT_ERROR;
fastboot_send(header, fastboot_data, 0, 0);
break;
}
}
voidfastboot_start_server(void)
{
printf("Using %s device\n", eth_get_name());
printf("Listening for fastboot command on %pI4\n", &net_ip);
fastboot_our_port=CONFIG_UDP_FUNCTION_FASTBOOT_PORT;
if (IS_ENABLED(CONFIG_FASTBOOT_FLASH))
fastboot_set_progress_callback(fastboot_timed_send_info);
net_set_udp_handler(fastboot_handler);
/* zero out server ether in case the server ip has changed */
memset(net_server_ethaddr, 0, 6);
}