xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_fastboot.c (revision 64ece84854ae49f40e9b9d4d88502247774f9d2f)
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 
154 void fastboot_fail(char *response, const char *reason)
155 {
156 	strncpy(response, "FAIL\0", 5);
157 	strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
158 }
159 
160 void fastboot_okay(char *response, const char *reason)
161 {
162 	strncpy(response, "OKAY\0", 5);
163 	strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
164 }
165 
166 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
167 {
168 	int status = req->status;
169 	if (!status)
170 		return;
171 	printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
172 }
173 
174 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
175 {
176 	int id;
177 	struct usb_gadget *gadget = c->cdev->gadget;
178 	struct f_fastboot *f_fb = func_to_fastboot(f);
179 	const char *s;
180 
181 	/* DYNAMIC interface numbers assignments */
182 	id = usb_interface_id(c, f);
183 	if (id < 0)
184 		return id;
185 	interface_desc.bInterfaceNumber = id;
186 
187 	id = usb_string_id(c->cdev);
188 	if (id < 0)
189 		return id;
190 	fastboot_string_defs[0].id = id;
191 	interface_desc.iInterface = id;
192 
193 	f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
194 	if (!f_fb->in_ep)
195 		return -ENODEV;
196 	f_fb->in_ep->driver_data = c->cdev;
197 
198 	f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
199 	if (!f_fb->out_ep)
200 		return -ENODEV;
201 	f_fb->out_ep->driver_data = c->cdev;
202 
203 	f->descriptors = fb_fs_function;
204 
205 	if (gadget_is_dualspeed(gadget)) {
206 		/* Assume endpoint addresses are the same for both speeds */
207 		hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
208 		hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
209 		/* copy HS descriptors */
210 		f->hs_descriptors = fb_hs_function;
211 	}
212 
213 	s = getenv("serial#");
214 	if (s)
215 		g_dnl_set_serialnumber((char *)s);
216 
217 	return 0;
218 }
219 
220 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
221 {
222 	memset(fastboot_func, 0, sizeof(*fastboot_func));
223 }
224 
225 static void fastboot_disable(struct usb_function *f)
226 {
227 	struct f_fastboot *f_fb = func_to_fastboot(f);
228 
229 	usb_ep_disable(f_fb->out_ep);
230 	usb_ep_disable(f_fb->in_ep);
231 
232 	if (f_fb->out_req) {
233 		free(f_fb->out_req->buf);
234 		usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
235 		f_fb->out_req = NULL;
236 	}
237 	if (f_fb->in_req) {
238 		free(f_fb->in_req->buf);
239 		usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
240 		f_fb->in_req = NULL;
241 	}
242 }
243 
244 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
245 {
246 	struct usb_request *req;
247 
248 	req = usb_ep_alloc_request(ep, 0);
249 	if (!req)
250 		return NULL;
251 
252 	req->length = EP_BUFFER_SIZE;
253 	req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
254 	if (!req->buf) {
255 		usb_ep_free_request(ep, req);
256 		return NULL;
257 	}
258 
259 	memset(req->buf, 0, req->length);
260 	return req;
261 }
262 
263 static int fastboot_set_alt(struct usb_function *f,
264 			    unsigned interface, unsigned alt)
265 {
266 	int ret;
267 	struct usb_composite_dev *cdev = f->config->cdev;
268 	struct usb_gadget *gadget = cdev->gadget;
269 	struct f_fastboot *f_fb = func_to_fastboot(f);
270 	const struct usb_endpoint_descriptor *d;
271 
272 	debug("%s: func: %s intf: %d alt: %d\n",
273 	      __func__, f->name, interface, alt);
274 
275 	d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
276 	ret = usb_ep_enable(f_fb->out_ep, d);
277 	if (ret) {
278 		puts("failed to enable out ep\n");
279 		return ret;
280 	}
281 
282 	f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
283 	if (!f_fb->out_req) {
284 		puts("failed to alloc out req\n");
285 		ret = -EINVAL;
286 		goto err;
287 	}
288 	f_fb->out_req->complete = rx_handler_command;
289 
290 	d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
291 	ret = usb_ep_enable(f_fb->in_ep, d);
292 	if (ret) {
293 		puts("failed to enable in ep\n");
294 		goto err;
295 	}
296 
297 	f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
298 	if (!f_fb->in_req) {
299 		puts("failed alloc req in\n");
300 		ret = -EINVAL;
301 		goto err;
302 	}
303 	f_fb->in_req->complete = fastboot_complete;
304 
305 	ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
306 	if (ret)
307 		goto err;
308 
309 	return 0;
310 err:
311 	fastboot_disable(f);
312 	return ret;
313 }
314 
315 static int fastboot_add(struct usb_configuration *c)
316 {
317 	struct f_fastboot *f_fb = fastboot_func;
318 	int status;
319 
320 	debug("%s: cdev: 0x%p\n", __func__, c->cdev);
321 
322 	if (!f_fb) {
323 		f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
324 		if (!f_fb)
325 			return -ENOMEM;
326 
327 		fastboot_func = f_fb;
328 		memset(f_fb, 0, sizeof(*f_fb));
329 	}
330 
331 	f_fb->usb_function.name = "f_fastboot";
332 	f_fb->usb_function.bind = fastboot_bind;
333 	f_fb->usb_function.unbind = fastboot_unbind;
334 	f_fb->usb_function.set_alt = fastboot_set_alt;
335 	f_fb->usb_function.disable = fastboot_disable;
336 	f_fb->usb_function.strings = fastboot_strings;
337 
338 	status = usb_add_function(c, &f_fb->usb_function);
339 	if (status) {
340 		free(f_fb);
341 		fastboot_func = f_fb;
342 	}
343 
344 	return status;
345 }
346 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
347 
348 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
349 {
350 	struct usb_request *in_req = fastboot_func->in_req;
351 	int ret;
352 
353 	memcpy(in_req->buf, buffer, buffer_size);
354 	in_req->length = buffer_size;
355 
356 	usb_ep_dequeue(fastboot_func->in_ep, in_req);
357 
358 	ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
359 	if (ret)
360 		printf("Error %d on queue\n", ret);
361 	return 0;
362 }
363 
364 static int fastboot_tx_write_str(const char *buffer)
365 {
366 	return fastboot_tx_write(buffer, strlen(buffer));
367 }
368 
369 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
370 {
371 	do_reset(NULL, 0, 0, NULL);
372 }
373 
374 int __weak fb_set_reboot_flag(void)
375 {
376 	return -ENOSYS;
377 }
378 
379 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
380 {
381 	char *cmd = req->buf;
382 	if (!strcmp_l1("reboot-bootloader", cmd)) {
383 		if (fb_set_reboot_flag()) {
384 			fastboot_tx_write_str("FAILCannot set reboot flag");
385 			return;
386 		}
387 	}
388 	fastboot_func->in_req->complete = compl_do_reset;
389 	fastboot_tx_write_str("OKAY");
390 }
391 
392 static int strcmp_l1(const char *s1, const char *s2)
393 {
394 	if (!s1 || !s2)
395 		return -1;
396 	return strncmp(s1, s2, strlen(s1));
397 }
398 
399 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
400 {
401 	char *cmd = req->buf;
402 	char response[FASTBOOT_RESPONSE_LEN];
403 	const char *s;
404 	size_t chars_left;
405 
406 	strcpy(response, "OKAY");
407 	chars_left = sizeof(response) - strlen(response) - 1;
408 
409 	strsep(&cmd, ":");
410 	if (!cmd) {
411 		error("missing variable");
412 		fastboot_tx_write_str("FAILmissing var");
413 		return;
414 	}
415 
416 	if (!strcmp_l1("version", cmd)) {
417 		strncat(response, FASTBOOT_VERSION, chars_left);
418 	} else if (!strcmp_l1("bootloader-version", cmd)) {
419 		strncat(response, U_BOOT_VERSION, chars_left);
420 	} else if (!strcmp_l1("downloadsize", cmd) ||
421 		!strcmp_l1("max-download-size", cmd)) {
422 		char str_num[12];
423 
424 		sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
425 		strncat(response, str_num, chars_left);
426 	} else if (!strcmp_l1("serialno", cmd)) {
427 		s = getenv("serial#");
428 		if (s)
429 			strncat(response, s, chars_left);
430 		else
431 			strcpy(response, "FAILValue not set");
432 	} else {
433 		char envstr[32];
434 
435 		snprintf(envstr, sizeof(envstr) - 1, "fastboot.%s", cmd);
436 		s = getenv(envstr);
437 		if (s) {
438 			strncat(response, s, chars_left);
439 		} else {
440 			printf("WARNING: unknown variable: %s\n", cmd);
441 			strcpy(response, "FAILVariable not implemented");
442 		}
443 	}
444 	fastboot_tx_write_str(response);
445 }
446 
447 static unsigned int rx_bytes_expected(struct usb_ep *ep)
448 {
449 	int rx_remain = download_size - download_bytes;
450 	unsigned int rem;
451 	unsigned int maxpacket = ep->maxpacket;
452 
453 	if (rx_remain <= 0)
454 		return 0;
455 	else if (rx_remain > EP_BUFFER_SIZE)
456 		return EP_BUFFER_SIZE;
457 
458 	/*
459 	 * Some controllers e.g. DWC3 don't like OUT transfers to be
460 	 * not ending in maxpacket boundary. So just make them happy by
461 	 * always requesting for integral multiple of maxpackets.
462 	 * This shouldn't bother controllers that don't care about it.
463 	 */
464 	rem = rx_remain % maxpacket;
465 	if (rem > 0)
466 		rx_remain = rx_remain + (maxpacket - rem);
467 
468 	return rx_remain;
469 }
470 
471 #define BYTES_PER_DOT	0x20000
472 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
473 {
474 	char response[FASTBOOT_RESPONSE_LEN];
475 	unsigned int transfer_size = download_size - download_bytes;
476 	const unsigned char *buffer = req->buf;
477 	unsigned int buffer_size = req->actual;
478 	unsigned int pre_dot_num, now_dot_num;
479 
480 	if (req->status != 0) {
481 		printf("Bad status: %d\n", req->status);
482 		return;
483 	}
484 
485 	if (buffer_size < transfer_size)
486 		transfer_size = buffer_size;
487 
488 	memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
489 	       buffer, transfer_size);
490 
491 	pre_dot_num = download_bytes / BYTES_PER_DOT;
492 	download_bytes += transfer_size;
493 	now_dot_num = download_bytes / BYTES_PER_DOT;
494 
495 	if (pre_dot_num != now_dot_num) {
496 		putc('.');
497 		if (!(now_dot_num % 74))
498 			putc('\n');
499 	}
500 
501 	/* Check if transfer is done */
502 	if (download_bytes >= download_size) {
503 		/*
504 		 * Reset global transfer variable, keep download_bytes because
505 		 * it will be used in the next possible flashing command
506 		 */
507 		download_size = 0;
508 		req->complete = rx_handler_command;
509 		req->length = EP_BUFFER_SIZE;
510 
511 		strcpy(response, "OKAY");
512 		fastboot_tx_write_str(response);
513 
514 		printf("\ndownloading of %d bytes finished\n", download_bytes);
515 	} else {
516 		req->length = rx_bytes_expected(ep);
517 	}
518 
519 	req->actual = 0;
520 	usb_ep_queue(ep, req, 0);
521 }
522 
523 static void cb_download(struct usb_ep *ep, struct usb_request *req)
524 {
525 	char *cmd = req->buf;
526 	char response[FASTBOOT_RESPONSE_LEN];
527 
528 	strsep(&cmd, ":");
529 	download_size = simple_strtoul(cmd, NULL, 16);
530 	download_bytes = 0;
531 
532 	printf("Starting download of %d bytes\n", download_size);
533 
534 	if (0 == download_size) {
535 		strcpy(response, "FAILdata invalid size");
536 	} else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
537 		download_size = 0;
538 		strcpy(response, "FAILdata too large");
539 	} else {
540 		sprintf(response, "DATA%08x", download_size);
541 		req->complete = rx_handler_dl_image;
542 		req->length = rx_bytes_expected(ep);
543 	}
544 	fastboot_tx_write_str(response);
545 }
546 
547 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
548 {
549 	char boot_addr_start[12];
550 	char *bootm_args[] = { "bootm", boot_addr_start, NULL };
551 
552 	puts("Booting kernel..\n");
553 
554 	sprintf(boot_addr_start, "0x%lx", load_addr);
555 	do_bootm(NULL, 0, 2, bootm_args);
556 
557 	/* This only happens if image is somehow faulty so we start over */
558 	do_reset(NULL, 0, 0, NULL);
559 }
560 
561 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
562 {
563 	fastboot_func->in_req->complete = do_bootm_on_complete;
564 	fastboot_tx_write_str("OKAY");
565 }
566 
567 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
568 {
569 	g_dnl_trigger_detach();
570 }
571 
572 static void cb_continue(struct usb_ep *ep, struct usb_request *req)
573 {
574 	fastboot_func->in_req->complete = do_exit_on_complete;
575 	fastboot_tx_write_str("OKAY");
576 }
577 
578 #ifdef CONFIG_FASTBOOT_FLASH
579 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
580 {
581 	char *cmd = req->buf;
582 	char response[FASTBOOT_RESPONSE_LEN];
583 
584 	strsep(&cmd, ":");
585 	if (!cmd) {
586 		error("missing partition name");
587 		fastboot_tx_write_str("FAILmissing partition name");
588 		return;
589 	}
590 
591 	strcpy(response, "FAILno flash device defined");
592 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
593 	fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
594 			   download_bytes, response);
595 #endif
596 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
597 	fb_nand_flash_write(cmd,
598 			    (void *)CONFIG_FASTBOOT_BUF_ADDR,
599 			    download_bytes, response);
600 #endif
601 	fastboot_tx_write_str(response);
602 }
603 #endif
604 
605 static void cb_oem(struct usb_ep *ep, struct usb_request *req)
606 {
607 	char *cmd = req->buf;
608 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
609 	if (strncmp("format", cmd + 4, 6) == 0) {
610 		char cmdbuf[32];
611                 sprintf(cmdbuf, "gpt write mmc %x $partitions",
612 			CONFIG_FASTBOOT_FLASH_MMC_DEV);
613                 if (run_command(cmdbuf, 0))
614 			fastboot_tx_write_str("FAIL");
615                 else
616 			fastboot_tx_write_str("OKAY");
617 	} else
618 #endif
619 	if (strncmp("unlock", cmd + 4, 8) == 0) {
620 		fastboot_tx_write_str("FAILnot implemented");
621 	}
622 	else {
623 		fastboot_tx_write_str("FAILunknown oem command");
624 	}
625 }
626 
627 #ifdef CONFIG_FASTBOOT_FLASH
628 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
629 {
630 	char *cmd = req->buf;
631 	char response[FASTBOOT_RESPONSE_LEN];
632 
633 	strsep(&cmd, ":");
634 	if (!cmd) {
635 		error("missing partition name");
636 		fastboot_tx_write_str("FAILmissing partition name");
637 		return;
638 	}
639 
640 	strcpy(response, "FAILno flash device defined");
641 
642 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
643 	fb_mmc_erase(cmd, response);
644 #endif
645 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
646 	fb_nand_erase(cmd, response);
647 #endif
648 	fastboot_tx_write_str(response);
649 }
650 #endif
651 
652 struct cmd_dispatch_info {
653 	char *cmd;
654 	void (*cb)(struct usb_ep *ep, struct usb_request *req);
655 };
656 
657 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
658 	{
659 		.cmd = "reboot",
660 		.cb = cb_reboot,
661 	}, {
662 		.cmd = "getvar:",
663 		.cb = cb_getvar,
664 	}, {
665 		.cmd = "download:",
666 		.cb = cb_download,
667 	}, {
668 		.cmd = "boot",
669 		.cb = cb_boot,
670 	}, {
671 		.cmd = "continue",
672 		.cb = cb_continue,
673 	},
674 #ifdef CONFIG_FASTBOOT_FLASH
675 	{
676 		.cmd = "flash",
677 		.cb = cb_flash,
678 	}, {
679 		.cmd = "erase",
680 		.cb = cb_erase,
681 	},
682 #endif
683 	{
684 		.cmd = "oem",
685 		.cb = cb_oem,
686 	},
687 };
688 
689 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
690 {
691 	char *cmdbuf = req->buf;
692 	void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
693 	int i;
694 
695 	if (req->status != 0 || req->length == 0)
696 		return;
697 
698 	for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
699 		if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
700 			func_cb = cmd_dispatch_info[i].cb;
701 			break;
702 		}
703 	}
704 
705 	if (!func_cb) {
706 		error("unknown command: %s", cmdbuf);
707 		fastboot_tx_write_str("FAILunknown command");
708 	} else {
709 		if (req->actual < req->length) {
710 			u8 *buf = (u8 *)req->buf;
711 			buf[req->actual] = 0;
712 			func_cb(ep, req);
713 		} else {
714 			error("buffer overflow");
715 			fastboot_tx_write_str("FAILbuffer overflow");
716 		}
717 	}
718 
719 	*cmdbuf = '\0';
720 	req->actual = 0;
721 	usb_ep_queue(ep, req, 0);
722 }
723