xref: /openbmc/u-boot/drivers/usb/gadget/f_fastboot.c (revision f73a7df984a9820d9beb829b32ccb5c3d55dc152)
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 <version.h>
22 #include <g_dnl.h>
23 #ifdef CONFIG_FASTBOOT_FLASH_MMC
24 #include <fb_mmc.h>
25 #endif
26 #ifdef CONFIG_FASTBOOT_FLASH_NAND
27 #include <fb_nand.h>
28 #endif
29 
30 #define FASTBOOT_INTERFACE_CLASS	0xff
31 #define FASTBOOT_INTERFACE_SUB_CLASS	0x42
32 #define FASTBOOT_INTERFACE_PROTOCOL	0x03
33 
34 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0  (0x0200)
35 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1  (0x0040)
36 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE      (0x0040)
37 
38 #define EP_BUFFER_SIZE			4096
39 /*
40  * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
41  * (64 or 512 or 1024), else we break on certain controllers like DWC3
42  * that expect bulk OUT requests to be divisible by maxpacket size.
43  */
44 
45 struct f_fastboot {
46 	struct usb_function usb_function;
47 
48 	/* IN/OUT EP's and corresponding requests */
49 	struct usb_ep *in_ep, *out_ep;
50 	struct usb_request *in_req, *out_req;
51 };
52 
53 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
54 {
55 	return container_of(f, struct f_fastboot, usb_function);
56 }
57 
58 static struct f_fastboot *fastboot_func;
59 static unsigned int download_size;
60 static unsigned int download_bytes;
61 
62 static struct usb_endpoint_descriptor fs_ep_in = {
63 	.bLength            = USB_DT_ENDPOINT_SIZE,
64 	.bDescriptorType    = USB_DT_ENDPOINT,
65 	.bEndpointAddress   = USB_DIR_IN,
66 	.bmAttributes       = USB_ENDPOINT_XFER_BULK,
67 	.wMaxPacketSize     = cpu_to_le16(64),
68 };
69 
70 static struct usb_endpoint_descriptor fs_ep_out = {
71 	.bLength		= USB_DT_ENDPOINT_SIZE,
72 	.bDescriptorType	= USB_DT_ENDPOINT,
73 	.bEndpointAddress	= USB_DIR_OUT,
74 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
75 	.wMaxPacketSize		= cpu_to_le16(64),
76 };
77 
78 static struct usb_endpoint_descriptor hs_ep_in = {
79 	.bLength		= USB_DT_ENDPOINT_SIZE,
80 	.bDescriptorType	= USB_DT_ENDPOINT,
81 	.bEndpointAddress	= USB_DIR_IN,
82 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
83 	.wMaxPacketSize		= cpu_to_le16(512),
84 };
85 
86 static struct usb_endpoint_descriptor hs_ep_out = {
87 	.bLength		= USB_DT_ENDPOINT_SIZE,
88 	.bDescriptorType	= USB_DT_ENDPOINT,
89 	.bEndpointAddress	= USB_DIR_OUT,
90 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
91 	.wMaxPacketSize		= cpu_to_le16(512),
92 };
93 
94 static struct usb_interface_descriptor interface_desc = {
95 	.bLength		= USB_DT_INTERFACE_SIZE,
96 	.bDescriptorType	= USB_DT_INTERFACE,
97 	.bInterfaceNumber	= 0x00,
98 	.bAlternateSetting	= 0x00,
99 	.bNumEndpoints		= 0x02,
100 	.bInterfaceClass	= FASTBOOT_INTERFACE_CLASS,
101 	.bInterfaceSubClass	= FASTBOOT_INTERFACE_SUB_CLASS,
102 	.bInterfaceProtocol	= FASTBOOT_INTERFACE_PROTOCOL,
103 };
104 
105 static struct usb_descriptor_header *fb_fs_function[] = {
106 	(struct usb_descriptor_header *)&interface_desc,
107 	(struct usb_descriptor_header *)&fs_ep_in,
108 	(struct usb_descriptor_header *)&fs_ep_out,
109 };
110 
111 static struct usb_descriptor_header *fb_hs_function[] = {
112 	(struct usb_descriptor_header *)&interface_desc,
113 	(struct usb_descriptor_header *)&hs_ep_in,
114 	(struct usb_descriptor_header *)&hs_ep_out,
115 	NULL,
116 };
117 
118 static struct usb_endpoint_descriptor *
119 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
120 	    struct usb_endpoint_descriptor *hs)
121 {
122 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
123 		return hs;
124 	return fs;
125 }
126 
127 /*
128  * static strings, in UTF-8
129  */
130 static const char fastboot_name[] = "Android Fastboot";
131 
132 static struct usb_string fastboot_string_defs[] = {
133 	[0].s = fastboot_name,
134 	{  }			/* end of list */
135 };
136 
137 static struct usb_gadget_strings stringtab_fastboot = {
138 	.language	= 0x0409,	/* en-us */
139 	.strings	= fastboot_string_defs,
140 };
141 
142 static struct usb_gadget_strings *fastboot_strings[] = {
143 	&stringtab_fastboot,
144 	NULL,
145 };
146 
147 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
148 static int strcmp_l1(const char *s1, const char *s2);
149 
150 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
151 {
152 	int status = req->status;
153 	if (!status)
154 		return;
155 	printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
156 }
157 
158 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
159 {
160 	int id;
161 	struct usb_gadget *gadget = c->cdev->gadget;
162 	struct f_fastboot *f_fb = func_to_fastboot(f);
163 	const char *s;
164 
165 	/* DYNAMIC interface numbers assignments */
166 	id = usb_interface_id(c, f);
167 	if (id < 0)
168 		return id;
169 	interface_desc.bInterfaceNumber = id;
170 
171 	id = usb_string_id(c->cdev);
172 	if (id < 0)
173 		return id;
174 	fastboot_string_defs[0].id = id;
175 	interface_desc.iInterface = id;
176 
177 	f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
178 	if (!f_fb->in_ep)
179 		return -ENODEV;
180 	f_fb->in_ep->driver_data = c->cdev;
181 
182 	f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
183 	if (!f_fb->out_ep)
184 		return -ENODEV;
185 	f_fb->out_ep->driver_data = c->cdev;
186 
187 	f->descriptors = fb_fs_function;
188 
189 	if (gadget_is_dualspeed(gadget)) {
190 		/* Assume endpoint addresses are the same for both speeds */
191 		hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
192 		hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
193 		/* copy HS descriptors */
194 		f->hs_descriptors = fb_hs_function;
195 	}
196 
197 	s = env_get("serial#");
198 	if (s)
199 		g_dnl_set_serialnumber((char *)s);
200 
201 	return 0;
202 }
203 
204 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
205 {
206 	memset(fastboot_func, 0, sizeof(*fastboot_func));
207 }
208 
209 static void fastboot_disable(struct usb_function *f)
210 {
211 	struct f_fastboot *f_fb = func_to_fastboot(f);
212 
213 	usb_ep_disable(f_fb->out_ep);
214 	usb_ep_disable(f_fb->in_ep);
215 
216 	if (f_fb->out_req) {
217 		free(f_fb->out_req->buf);
218 		usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
219 		f_fb->out_req = NULL;
220 	}
221 	if (f_fb->in_req) {
222 		free(f_fb->in_req->buf);
223 		usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
224 		f_fb->in_req = NULL;
225 	}
226 }
227 
228 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
229 {
230 	struct usb_request *req;
231 
232 	req = usb_ep_alloc_request(ep, 0);
233 	if (!req)
234 		return NULL;
235 
236 	req->length = EP_BUFFER_SIZE;
237 	req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
238 	if (!req->buf) {
239 		usb_ep_free_request(ep, req);
240 		return NULL;
241 	}
242 
243 	memset(req->buf, 0, req->length);
244 	return req;
245 }
246 
247 static int fastboot_set_alt(struct usb_function *f,
248 			    unsigned interface, unsigned alt)
249 {
250 	int ret;
251 	struct usb_composite_dev *cdev = f->config->cdev;
252 	struct usb_gadget *gadget = cdev->gadget;
253 	struct f_fastboot *f_fb = func_to_fastboot(f);
254 	const struct usb_endpoint_descriptor *d;
255 
256 	debug("%s: func: %s intf: %d alt: %d\n",
257 	      __func__, f->name, interface, alt);
258 
259 	d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
260 	ret = usb_ep_enable(f_fb->out_ep, d);
261 	if (ret) {
262 		puts("failed to enable out ep\n");
263 		return ret;
264 	}
265 
266 	f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
267 	if (!f_fb->out_req) {
268 		puts("failed to alloc out req\n");
269 		ret = -EINVAL;
270 		goto err;
271 	}
272 	f_fb->out_req->complete = rx_handler_command;
273 
274 	d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
275 	ret = usb_ep_enable(f_fb->in_ep, d);
276 	if (ret) {
277 		puts("failed to enable in ep\n");
278 		goto err;
279 	}
280 
281 	f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
282 	if (!f_fb->in_req) {
283 		puts("failed alloc req in\n");
284 		ret = -EINVAL;
285 		goto err;
286 	}
287 	f_fb->in_req->complete = fastboot_complete;
288 
289 	ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
290 	if (ret)
291 		goto err;
292 
293 	return 0;
294 err:
295 	fastboot_disable(f);
296 	return ret;
297 }
298 
299 static int fastboot_add(struct usb_configuration *c)
300 {
301 	struct f_fastboot *f_fb = fastboot_func;
302 	int status;
303 
304 	debug("%s: cdev: 0x%p\n", __func__, c->cdev);
305 
306 	if (!f_fb) {
307 		f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
308 		if (!f_fb)
309 			return -ENOMEM;
310 
311 		fastboot_func = f_fb;
312 		memset(f_fb, 0, sizeof(*f_fb));
313 	}
314 
315 	f_fb->usb_function.name = "f_fastboot";
316 	f_fb->usb_function.bind = fastboot_bind;
317 	f_fb->usb_function.unbind = fastboot_unbind;
318 	f_fb->usb_function.set_alt = fastboot_set_alt;
319 	f_fb->usb_function.disable = fastboot_disable;
320 	f_fb->usb_function.strings = fastboot_strings;
321 
322 	status = usb_add_function(c, &f_fb->usb_function);
323 	if (status) {
324 		free(f_fb);
325 		fastboot_func = f_fb;
326 	}
327 
328 	return status;
329 }
330 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
331 
332 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
333 {
334 	struct usb_request *in_req = fastboot_func->in_req;
335 	int ret;
336 
337 	memcpy(in_req->buf, buffer, buffer_size);
338 	in_req->length = buffer_size;
339 
340 	usb_ep_dequeue(fastboot_func->in_ep, in_req);
341 
342 	ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
343 	if (ret)
344 		printf("Error %d on queue\n", ret);
345 	return 0;
346 }
347 
348 static int fastboot_tx_write_str(const char *buffer)
349 {
350 	return fastboot_tx_write(buffer, strlen(buffer));
351 }
352 
353 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
354 {
355 	do_reset(NULL, 0, 0, NULL);
356 }
357 
358 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
359 {
360 	char *cmd = req->buf;
361 	if (!strcmp_l1("reboot-bootloader", cmd)) {
362 		if (fastboot_set_reboot_flag()) {
363 			fastboot_tx_write_str("FAILCannot set reboot flag");
364 			return;
365 		}
366 	}
367 	fastboot_func->in_req->complete = compl_do_reset;
368 	fastboot_tx_write_str("OKAY");
369 }
370 
371 static int strcmp_l1(const char *s1, const char *s2)
372 {
373 	if (!s1 || !s2)
374 		return -1;
375 	return strncmp(s1, s2, strlen(s1));
376 }
377 
378 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
379 {
380 	char *cmd = req->buf;
381 	char response[FASTBOOT_RESPONSE_LEN];
382 	const char *s;
383 	size_t chars_left;
384 
385 	strcpy(response, "OKAY");
386 	chars_left = sizeof(response) - strlen(response) - 1;
387 
388 	strsep(&cmd, ":");
389 	if (!cmd) {
390 		pr_err("missing variable");
391 		fastboot_tx_write_str("FAILmissing var");
392 		return;
393 	}
394 
395 	if (!strcmp_l1("version", cmd)) {
396 		strncat(response, FASTBOOT_VERSION, chars_left);
397 	} else if (!strcmp_l1("bootloader-version", cmd)) {
398 		strncat(response, U_BOOT_VERSION, chars_left);
399 	} else if (!strcmp_l1("downloadsize", cmd) ||
400 		!strcmp_l1("max-download-size", cmd)) {
401 		char str_num[12];
402 
403 		sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
404 		strncat(response, str_num, chars_left);
405 	} else if (!strcmp_l1("serialno", cmd)) {
406 		s = env_get("serial#");
407 		if (s)
408 			strncat(response, s, chars_left);
409 		else
410 			strcpy(response, "FAILValue not set");
411 	} else {
412 		char *envstr;
413 
414 		envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1);
415 		if (!envstr) {
416 			fastboot_tx_write_str("FAILmalloc error");
417 			return;
418 		}
419 
420 		sprintf(envstr, "fastboot.%s", cmd);
421 		s = env_get(envstr);
422 		if (s) {
423 			strncat(response, s, chars_left);
424 		} else {
425 			printf("WARNING: unknown variable: %s\n", cmd);
426 			strcpy(response, "FAILVariable not implemented");
427 		}
428 
429 		free(envstr);
430 	}
431 	fastboot_tx_write_str(response);
432 }
433 
434 static unsigned int rx_bytes_expected(struct usb_ep *ep)
435 {
436 	int rx_remain = download_size - download_bytes;
437 	unsigned int rem;
438 	unsigned int maxpacket = ep->maxpacket;
439 
440 	if (rx_remain <= 0)
441 		return 0;
442 	else if (rx_remain > EP_BUFFER_SIZE)
443 		return EP_BUFFER_SIZE;
444 
445 	/*
446 	 * Some controllers e.g. DWC3 don't like OUT transfers to be
447 	 * not ending in maxpacket boundary. So just make them happy by
448 	 * always requesting for integral multiple of maxpackets.
449 	 * This shouldn't bother controllers that don't care about it.
450 	 */
451 	rem = rx_remain % maxpacket;
452 	if (rem > 0)
453 		rx_remain = rx_remain + (maxpacket - rem);
454 
455 	return rx_remain;
456 }
457 
458 #define BYTES_PER_DOT	0x20000
459 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
460 {
461 	char response[FASTBOOT_RESPONSE_LEN];
462 	unsigned int transfer_size = download_size - download_bytes;
463 	const unsigned char *buffer = req->buf;
464 	unsigned int buffer_size = req->actual;
465 	unsigned int pre_dot_num, now_dot_num;
466 
467 	if (req->status != 0) {
468 		printf("Bad status: %d\n", req->status);
469 		return;
470 	}
471 
472 	if (buffer_size < transfer_size)
473 		transfer_size = buffer_size;
474 
475 	memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
476 	       buffer, transfer_size);
477 
478 	pre_dot_num = download_bytes / BYTES_PER_DOT;
479 	download_bytes += transfer_size;
480 	now_dot_num = download_bytes / BYTES_PER_DOT;
481 
482 	if (pre_dot_num != now_dot_num) {
483 		putc('.');
484 		if (!(now_dot_num % 74))
485 			putc('\n');
486 	}
487 
488 	/* Check if transfer is done */
489 	if (download_bytes >= download_size) {
490 		/*
491 		 * Reset global transfer variable, keep download_bytes because
492 		 * it will be used in the next possible flashing command
493 		 */
494 		download_size = 0;
495 		req->complete = rx_handler_command;
496 		req->length = EP_BUFFER_SIZE;
497 
498 		strcpy(response, "OKAY");
499 		fastboot_tx_write_str(response);
500 
501 		printf("\ndownloading of %d bytes finished\n", download_bytes);
502 	} else {
503 		req->length = rx_bytes_expected(ep);
504 	}
505 
506 	req->actual = 0;
507 	usb_ep_queue(ep, req, 0);
508 }
509 
510 static void cb_download(struct usb_ep *ep, struct usb_request *req)
511 {
512 	char *cmd = req->buf;
513 	char response[FASTBOOT_RESPONSE_LEN];
514 
515 	strsep(&cmd, ":");
516 	download_size = simple_strtoul(cmd, NULL, 16);
517 	download_bytes = 0;
518 
519 	printf("Starting download of %d bytes\n", download_size);
520 
521 	if (0 == download_size) {
522 		strcpy(response, "FAILdata invalid size");
523 	} else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
524 		download_size = 0;
525 		strcpy(response, "FAILdata too large");
526 	} else {
527 		sprintf(response, "DATA%08x", download_size);
528 		req->complete = rx_handler_dl_image;
529 		req->length = rx_bytes_expected(ep);
530 	}
531 	fastboot_tx_write_str(response);
532 }
533 
534 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
535 {
536 	char boot_addr_start[12];
537 	char *bootm_args[] = { "bootm", boot_addr_start, NULL };
538 
539 	puts("Booting kernel..\n");
540 
541 	sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR);
542 	do_bootm(NULL, 0, 2, bootm_args);
543 
544 	/* This only happens if image is somehow faulty so we start over */
545 	do_reset(NULL, 0, 0, NULL);
546 }
547 
548 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
549 {
550 	fastboot_func->in_req->complete = do_bootm_on_complete;
551 	fastboot_tx_write_str("OKAY");
552 }
553 
554 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
555 {
556 	g_dnl_trigger_detach();
557 }
558 
559 static void cb_continue(struct usb_ep *ep, struct usb_request *req)
560 {
561 	fastboot_func->in_req->complete = do_exit_on_complete;
562 	fastboot_tx_write_str("OKAY");
563 }
564 
565 #ifdef CONFIG_FASTBOOT_FLASH
566 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
567 {
568 	char *cmd = req->buf;
569 	char response[FASTBOOT_RESPONSE_LEN];
570 
571 	strsep(&cmd, ":");
572 	if (!cmd) {
573 		pr_err("missing partition name");
574 		fastboot_tx_write_str("FAILmissing partition name");
575 		return;
576 	}
577 
578 	fastboot_fail("no flash device defined", response);
579 #ifdef CONFIG_FASTBOOT_FLASH_MMC
580 	fastboot_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
581 				 download_bytes, response);
582 #endif
583 #ifdef CONFIG_FASTBOOT_FLASH_NAND
584 	fastboot_nand_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
585 				  download_bytes, response);
586 #endif
587 	fastboot_tx_write_str(response);
588 }
589 #endif
590 
591 static void cb_oem(struct usb_ep *ep, struct usb_request *req)
592 {
593 	char *cmd = req->buf;
594 #ifdef CONFIG_FASTBOOT_FLASH_MMC
595 	if (strncmp("format", cmd + 4, 6) == 0) {
596 		char cmdbuf[32];
597                 sprintf(cmdbuf, "gpt write mmc %x $partitions",
598 			CONFIG_FASTBOOT_FLASH_MMC_DEV);
599                 if (run_command(cmdbuf, 0))
600 			fastboot_tx_write_str("FAIL");
601                 else
602 			fastboot_tx_write_str("OKAY");
603 	} else
604 #endif
605 	if (strncmp("unlock", cmd + 4, 8) == 0) {
606 		fastboot_tx_write_str("FAILnot implemented");
607 	}
608 	else {
609 		fastboot_tx_write_str("FAILunknown oem command");
610 	}
611 }
612 
613 #ifdef CONFIG_FASTBOOT_FLASH
614 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
615 {
616 	char *cmd = req->buf;
617 	char response[FASTBOOT_RESPONSE_LEN];
618 
619 	strsep(&cmd, ":");
620 	if (!cmd) {
621 		pr_err("missing partition name");
622 		fastboot_tx_write_str("FAILmissing partition name");
623 		return;
624 	}
625 
626 	fastboot_fail("no flash device defined", response);
627 #ifdef CONFIG_FASTBOOT_FLASH_MMC
628 	fastboot_mmc_erase(cmd, response);
629 #endif
630 #ifdef CONFIG_FASTBOOT_FLASH_NAND
631 	fastboot_nand_erase(cmd, response);
632 #endif
633 	fastboot_tx_write_str(response);
634 }
635 #endif
636 
637 struct cmd_dispatch_info {
638 	char *cmd;
639 	void (*cb)(struct usb_ep *ep, struct usb_request *req);
640 };
641 
642 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
643 	{
644 		.cmd = "reboot",
645 		.cb = cb_reboot,
646 	}, {
647 		.cmd = "getvar:",
648 		.cb = cb_getvar,
649 	}, {
650 		.cmd = "download:",
651 		.cb = cb_download,
652 	}, {
653 		.cmd = "boot",
654 		.cb = cb_boot,
655 	}, {
656 		.cmd = "continue",
657 		.cb = cb_continue,
658 	},
659 #ifdef CONFIG_FASTBOOT_FLASH
660 	{
661 		.cmd = "flash",
662 		.cb = cb_flash,
663 	}, {
664 		.cmd = "erase",
665 		.cb = cb_erase,
666 	},
667 #endif
668 	{
669 		.cmd = "oem",
670 		.cb = cb_oem,
671 	},
672 };
673 
674 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
675 {
676 	char *cmdbuf = req->buf;
677 	void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
678 	int i;
679 
680 	if (req->status != 0 || req->length == 0)
681 		return;
682 
683 	for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
684 		if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
685 			func_cb = cmd_dispatch_info[i].cb;
686 			break;
687 		}
688 	}
689 
690 	if (!func_cb) {
691 		pr_err("unknown command: %.*s", req->actual, cmdbuf);
692 		fastboot_tx_write_str("FAILunknown command");
693 	} else {
694 		if (req->actual < req->length) {
695 			u8 *buf = (u8 *)req->buf;
696 			buf[req->actual] = 0;
697 			func_cb(ep, req);
698 		} else {
699 			pr_err("buffer overflow");
700 			fastboot_tx_write_str("FAILbuffer overflow");
701 		}
702 	}
703 
704 	*cmdbuf = '\0';
705 	req->actual = 0;
706 	usb_ep_queue(ep, req, 0);
707 }
708