1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008 - 2009
4  * Windriver, <www.windriver.com>
5  * Tom Rix <Tom.Rix@windriver.com>
6  *
7  * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8  *
9  * Copyright 2014 Linaro, Ltd.
10  * Rob Herring <robh@kernel.org>
11  */
12 #include <command.h>
13 #include <config.h>
14 #include <common.h>
15 #include <env.h>
16 #include <errno.h>
17 #include <fastboot.h>
18 #include <log.h>
19 #include <malloc.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 #include <linux/usb/composite.h>
23 #include <linux/compiler.h>
24 #include <g_dnl.h>
25 
26 #define FASTBOOT_INTERFACE_CLASS	0xff
27 #define FASTBOOT_INTERFACE_SUB_CLASS	0x42
28 #define FASTBOOT_INTERFACE_PROTOCOL	0x03
29 
30 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0  (0x0200)
31 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1  (0x0040)
32 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE      (0x0040)
33 
34 #define EP_BUFFER_SIZE			4096
35 /*
36  * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
37  * (64 or 512 or 1024), else we break on certain controllers like DWC3
38  * that expect bulk OUT requests to be divisible by maxpacket size.
39  */
40 
41 struct f_fastboot {
42 	struct usb_function usb_function;
43 
44 	/* IN/OUT EP's and corresponding requests */
45 	struct usb_ep *in_ep, *out_ep;
46 	struct usb_request *in_req, *out_req;
47 };
48 
func_to_fastboot(struct usb_function * f)49 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
50 {
51 	return container_of(f, struct f_fastboot, usb_function);
52 }
53 
54 static struct f_fastboot *fastboot_func;
55 
56 static struct usb_endpoint_descriptor fs_ep_in = {
57 	.bLength            = USB_DT_ENDPOINT_SIZE,
58 	.bDescriptorType    = USB_DT_ENDPOINT,
59 	.bEndpointAddress   = USB_DIR_IN,
60 	.bmAttributes       = USB_ENDPOINT_XFER_BULK,
61 	.wMaxPacketSize     = cpu_to_le16(64),
62 };
63 
64 static struct usb_endpoint_descriptor fs_ep_out = {
65 	.bLength		= USB_DT_ENDPOINT_SIZE,
66 	.bDescriptorType	= USB_DT_ENDPOINT,
67 	.bEndpointAddress	= USB_DIR_OUT,
68 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
69 	.wMaxPacketSize		= cpu_to_le16(64),
70 };
71 
72 static struct usb_endpoint_descriptor hs_ep_in = {
73 	.bLength		= USB_DT_ENDPOINT_SIZE,
74 	.bDescriptorType	= USB_DT_ENDPOINT,
75 	.bEndpointAddress	= USB_DIR_IN,
76 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
77 	.wMaxPacketSize		= cpu_to_le16(512),
78 };
79 
80 static struct usb_endpoint_descriptor hs_ep_out = {
81 	.bLength		= USB_DT_ENDPOINT_SIZE,
82 	.bDescriptorType	= USB_DT_ENDPOINT,
83 	.bEndpointAddress	= USB_DIR_OUT,
84 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
85 	.wMaxPacketSize		= cpu_to_le16(512),
86 };
87 
88 static struct usb_interface_descriptor interface_desc = {
89 	.bLength		= USB_DT_INTERFACE_SIZE,
90 	.bDescriptorType	= USB_DT_INTERFACE,
91 	.bInterfaceNumber	= 0x00,
92 	.bAlternateSetting	= 0x00,
93 	.bNumEndpoints		= 0x02,
94 	.bInterfaceClass	= FASTBOOT_INTERFACE_CLASS,
95 	.bInterfaceSubClass	= FASTBOOT_INTERFACE_SUB_CLASS,
96 	.bInterfaceProtocol	= FASTBOOT_INTERFACE_PROTOCOL,
97 };
98 
99 static struct usb_descriptor_header *fb_fs_function[] = {
100 	(struct usb_descriptor_header *)&interface_desc,
101 	(struct usb_descriptor_header *)&fs_ep_in,
102 	(struct usb_descriptor_header *)&fs_ep_out,
103 };
104 
105 static struct usb_descriptor_header *fb_hs_function[] = {
106 	(struct usb_descriptor_header *)&interface_desc,
107 	(struct usb_descriptor_header *)&hs_ep_in,
108 	(struct usb_descriptor_header *)&hs_ep_out,
109 	NULL,
110 };
111 
112 static struct usb_endpoint_descriptor *
fb_ep_desc(struct usb_gadget * g,struct usb_endpoint_descriptor * fs,struct usb_endpoint_descriptor * hs)113 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
114 	    struct usb_endpoint_descriptor *hs)
115 {
116 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
117 		return hs;
118 	return fs;
119 }
120 
121 /*
122  * static strings, in UTF-8
123  */
124 static const char fastboot_name[] = "Android Fastboot";
125 
126 static struct usb_string fastboot_string_defs[] = {
127 	[0].s = fastboot_name,
128 	{  }			/* end of list */
129 };
130 
131 static struct usb_gadget_strings stringtab_fastboot = {
132 	.language	= 0x0409,	/* en-us */
133 	.strings	= fastboot_string_defs,
134 };
135 
136 static struct usb_gadget_strings *fastboot_strings[] = {
137 	&stringtab_fastboot,
138 	NULL,
139 };
140 
141 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
142 
fastboot_complete(struct usb_ep * ep,struct usb_request * req)143 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
144 {
145 	int status = req->status;
146 	if (!status)
147 		return;
148 	printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
149 }
150 
fastboot_bind(struct usb_configuration * c,struct usb_function * f)151 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
152 {
153 	int id;
154 	struct usb_gadget *gadget = c->cdev->gadget;
155 	struct f_fastboot *f_fb = func_to_fastboot(f);
156 	const char *s;
157 
158 	/* DYNAMIC interface numbers assignments */
159 	id = usb_interface_id(c, f);
160 	if (id < 0)
161 		return id;
162 	interface_desc.bInterfaceNumber = id;
163 
164 	id = usb_string_id(c->cdev);
165 	if (id < 0)
166 		return id;
167 	fastboot_string_defs[0].id = id;
168 	interface_desc.iInterface = id;
169 
170 	f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
171 	if (!f_fb->in_ep)
172 		return -ENODEV;
173 	f_fb->in_ep->driver_data = c->cdev;
174 
175 	f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
176 	if (!f_fb->out_ep)
177 		return -ENODEV;
178 	f_fb->out_ep->driver_data = c->cdev;
179 
180 	f->descriptors = fb_fs_function;
181 
182 	if (gadget_is_dualspeed(gadget)) {
183 		/* Assume endpoint addresses are the same for both speeds */
184 		hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
185 		hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
186 		/* copy HS descriptors */
187 		f->hs_descriptors = fb_hs_function;
188 	}
189 
190 	s = env_get("serial#");
191 	if (s)
192 		g_dnl_set_serialnumber((char *)s);
193 
194 	return 0;
195 }
196 
fastboot_unbind(struct usb_configuration * c,struct usb_function * f)197 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
198 {
199 	memset(fastboot_func, 0, sizeof(*fastboot_func));
200 }
201 
fastboot_disable(struct usb_function * f)202 static void fastboot_disable(struct usb_function *f)
203 {
204 	struct f_fastboot *f_fb = func_to_fastboot(f);
205 
206 	usb_ep_disable(f_fb->out_ep);
207 	usb_ep_disable(f_fb->in_ep);
208 
209 	if (f_fb->out_req) {
210 		free(f_fb->out_req->buf);
211 		usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
212 		f_fb->out_req = NULL;
213 	}
214 	if (f_fb->in_req) {
215 		free(f_fb->in_req->buf);
216 		usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
217 		f_fb->in_req = NULL;
218 	}
219 }
220 
fastboot_start_ep(struct usb_ep * ep)221 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
222 {
223 	struct usb_request *req;
224 
225 	req = usb_ep_alloc_request(ep, 0);
226 	if (!req)
227 		return NULL;
228 
229 	req->length = EP_BUFFER_SIZE;
230 	req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
231 	if (!req->buf) {
232 		usb_ep_free_request(ep, req);
233 		return NULL;
234 	}
235 
236 	memset(req->buf, 0, req->length);
237 	return req;
238 }
239 
fastboot_set_alt(struct usb_function * f,unsigned interface,unsigned alt)240 static int fastboot_set_alt(struct usb_function *f,
241 			    unsigned interface, unsigned alt)
242 {
243 	int ret;
244 	struct usb_composite_dev *cdev = f->config->cdev;
245 	struct usb_gadget *gadget = cdev->gadget;
246 	struct f_fastboot *f_fb = func_to_fastboot(f);
247 	const struct usb_endpoint_descriptor *d;
248 
249 	debug("%s: func: %s intf: %d alt: %d\n",
250 	      __func__, f->name, interface, alt);
251 
252 	d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
253 	ret = usb_ep_enable(f_fb->out_ep, d);
254 	if (ret) {
255 		puts("failed to enable out ep\n");
256 		return ret;
257 	}
258 
259 	f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
260 	if (!f_fb->out_req) {
261 		puts("failed to alloc out req\n");
262 		ret = -EINVAL;
263 		goto err;
264 	}
265 	f_fb->out_req->complete = rx_handler_command;
266 
267 	d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
268 	ret = usb_ep_enable(f_fb->in_ep, d);
269 	if (ret) {
270 		puts("failed to enable in ep\n");
271 		goto err;
272 	}
273 
274 	f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
275 	if (!f_fb->in_req) {
276 		puts("failed alloc req in\n");
277 		ret = -EINVAL;
278 		goto err;
279 	}
280 	f_fb->in_req->complete = fastboot_complete;
281 
282 	ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
283 	if (ret)
284 		goto err;
285 
286 	return 0;
287 err:
288 	fastboot_disable(f);
289 	return ret;
290 }
291 
fastboot_add(struct usb_configuration * c)292 static int fastboot_add(struct usb_configuration *c)
293 {
294 	struct f_fastboot *f_fb = fastboot_func;
295 	int status;
296 
297 	debug("%s: cdev: 0x%p\n", __func__, c->cdev);
298 
299 	if (!f_fb) {
300 		f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
301 		if (!f_fb)
302 			return -ENOMEM;
303 
304 		fastboot_func = f_fb;
305 		memset(f_fb, 0, sizeof(*f_fb));
306 	}
307 
308 	f_fb->usb_function.name = "f_fastboot";
309 	f_fb->usb_function.bind = fastboot_bind;
310 	f_fb->usb_function.unbind = fastboot_unbind;
311 	f_fb->usb_function.set_alt = fastboot_set_alt;
312 	f_fb->usb_function.disable = fastboot_disable;
313 	f_fb->usb_function.strings = fastboot_strings;
314 
315 	status = usb_add_function(c, &f_fb->usb_function);
316 	if (status) {
317 		free(f_fb);
318 		fastboot_func = f_fb;
319 	}
320 
321 	return status;
322 }
323 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
324 
fastboot_tx_write(const char * buffer,unsigned int buffer_size)325 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
326 {
327 	struct usb_request *in_req = fastboot_func->in_req;
328 	int ret;
329 
330 	memcpy(in_req->buf, buffer, buffer_size);
331 	in_req->length = buffer_size;
332 
333 	usb_ep_dequeue(fastboot_func->in_ep, in_req);
334 
335 	ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
336 	if (ret)
337 		printf("Error %d on queue\n", ret);
338 	return 0;
339 }
340 
fastboot_tx_write_str(const char * buffer)341 static int fastboot_tx_write_str(const char *buffer)
342 {
343 	return fastboot_tx_write(buffer, strlen(buffer));
344 }
345 
compl_do_reset(struct usb_ep * ep,struct usb_request * req)346 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
347 {
348 	do_reset(NULL, 0, 0, NULL);
349 }
350 
rx_bytes_expected(struct usb_ep * ep)351 static unsigned int rx_bytes_expected(struct usb_ep *ep)
352 {
353 	int rx_remain = fastboot_data_remaining();
354 	unsigned int rem;
355 	unsigned int maxpacket = ep->maxpacket;
356 
357 	if (rx_remain <= 0)
358 		return 0;
359 	else if (rx_remain > EP_BUFFER_SIZE)
360 		return EP_BUFFER_SIZE;
361 
362 	/*
363 	 * Some controllers e.g. DWC3 don't like OUT transfers to be
364 	 * not ending in maxpacket boundary. So just make them happy by
365 	 * always requesting for integral multiple of maxpackets.
366 	 * This shouldn't bother controllers that don't care about it.
367 	 */
368 	rem = rx_remain % maxpacket;
369 	if (rem > 0)
370 		rx_remain = rx_remain + (maxpacket - rem);
371 
372 	return rx_remain;
373 }
374 
rx_handler_dl_image(struct usb_ep * ep,struct usb_request * req)375 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
376 {
377 	char response[FASTBOOT_RESPONSE_LEN] = {0};
378 	unsigned int transfer_size = fastboot_data_remaining();
379 	const unsigned char *buffer = req->buf;
380 	unsigned int buffer_size = req->actual;
381 
382 	if (req->status != 0) {
383 		printf("Bad status: %d\n", req->status);
384 		return;
385 	}
386 
387 	if (buffer_size < transfer_size)
388 		transfer_size = buffer_size;
389 
390 	fastboot_data_download(buffer, transfer_size, response);
391 	if (response[0]) {
392 		fastboot_tx_write_str(response);
393 	} else if (!fastboot_data_remaining()) {
394 		fastboot_data_complete(response);
395 
396 		/*
397 		 * Reset global transfer variable
398 		 */
399 		req->complete = rx_handler_command;
400 		req->length = EP_BUFFER_SIZE;
401 
402 		fastboot_tx_write_str(response);
403 	} else {
404 		req->length = rx_bytes_expected(ep);
405 	}
406 
407 	req->actual = 0;
408 	usb_ep_queue(ep, req, 0);
409 }
410 
do_exit_on_complete(struct usb_ep * ep,struct usb_request * req)411 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
412 {
413 	g_dnl_trigger_detach();
414 }
415 
do_bootm_on_complete(struct usb_ep * ep,struct usb_request * req)416 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
417 {
418 	fastboot_boot();
419 	do_exit_on_complete(ep, req);
420 }
421 
rx_handler_command(struct usb_ep * ep,struct usb_request * req)422 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
423 {
424 	char *cmdbuf = req->buf;
425 	char response[FASTBOOT_RESPONSE_LEN] = {0};
426 	int cmd = -1;
427 
428 	if (req->status != 0 || req->length == 0)
429 		return;
430 
431 	if (req->actual < req->length) {
432 		cmdbuf[req->actual] = '\0';
433 		cmd = fastboot_handle_command(cmdbuf, response);
434 	} else {
435 		pr_err("buffer overflow");
436 		fastboot_fail("buffer overflow", response);
437 	}
438 
439 	if (!strncmp("DATA", response, 4)) {
440 		req->complete = rx_handler_dl_image;
441 		req->length = rx_bytes_expected(ep);
442 	}
443 
444 	fastboot_tx_write_str(response);
445 
446 	if (!strncmp("OKAY", response, 4)) {
447 		switch (cmd) {
448 		case FASTBOOT_COMMAND_BOOT:
449 			fastboot_func->in_req->complete = do_bootm_on_complete;
450 			break;
451 
452 		case FASTBOOT_COMMAND_CONTINUE:
453 			fastboot_func->in_req->complete = do_exit_on_complete;
454 			break;
455 
456 		case FASTBOOT_COMMAND_REBOOT:
457 		case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
458 			fastboot_func->in_req->complete = compl_do_reset;
459 			break;
460 		}
461 	}
462 
463 	*cmdbuf = '\0';
464 	req->actual = 0;
465 	usb_ep_queue(ep, req, 0);
466 }
467