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