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