xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_fastboot.c (revision e2ec3e461250fc82781c8a83387c140c6fc30a5a)
13aab70afSSebastian Siewior /*
23aab70afSSebastian Siewior  * (C) Copyright 2008 - 2009
33aab70afSSebastian Siewior  * Windriver, <www.windriver.com>
43aab70afSSebastian Siewior  * Tom Rix <Tom.Rix@windriver.com>
53aab70afSSebastian Siewior  *
63aab70afSSebastian Siewior  * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
73aab70afSSebastian Siewior  *
83aab70afSSebastian Siewior  * Copyright 2014 Linaro, Ltd.
93aab70afSSebastian Siewior  * Rob Herring <robh@kernel.org>
103aab70afSSebastian Siewior  *
113aab70afSSebastian Siewior  * SPDX-License-Identifier:	GPL-2.0+
123aab70afSSebastian Siewior  */
13593cbd93SSteve Rae #include <config.h>
143aab70afSSebastian Siewior #include <common.h>
153aab70afSSebastian Siewior #include <errno.h>
163aab70afSSebastian Siewior #include <malloc.h>
173aab70afSSebastian Siewior #include <linux/usb/ch9.h>
183aab70afSSebastian Siewior #include <linux/usb/gadget.h>
193aab70afSSebastian Siewior #include <linux/usb/composite.h>
203aab70afSSebastian Siewior #include <linux/compiler.h>
213aab70afSSebastian Siewior #include <version.h>
223aab70afSSebastian Siewior #include <g_dnl.h>
23d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
24d1b5ed07SSteve Rae #include <fb_mmc.h>
25d1b5ed07SSteve Rae #endif
263aab70afSSebastian Siewior 
273aab70afSSebastian Siewior #define FASTBOOT_VERSION		"0.4"
283aab70afSSebastian Siewior 
293aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_CLASS	0xff
303aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_SUB_CLASS	0x42
313aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_PROTOCOL	0x03
323aab70afSSebastian Siewior 
333aab70afSSebastian Siewior #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0  (0x0200)
343aab70afSSebastian Siewior #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1  (0x0040)
353aab70afSSebastian Siewior #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE      (0x0040)
363aab70afSSebastian Siewior 
373aab70afSSebastian Siewior /* The 64 defined bytes plus \0 */
383aab70afSSebastian Siewior #define RESPONSE_LEN	(64 + 1)
393aab70afSSebastian Siewior 
403aab70afSSebastian Siewior #define EP_BUFFER_SIZE			4096
413aab70afSSebastian Siewior 
423aab70afSSebastian Siewior struct f_fastboot {
433aab70afSSebastian Siewior 	struct usb_function usb_function;
443aab70afSSebastian Siewior 
45593cbd93SSteve Rae 	/* IN/OUT EP's and corresponding requests */
463aab70afSSebastian Siewior 	struct usb_ep *in_ep, *out_ep;
473aab70afSSebastian Siewior 	struct usb_request *in_req, *out_req;
483aab70afSSebastian Siewior };
493aab70afSSebastian Siewior 
503aab70afSSebastian Siewior static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
513aab70afSSebastian Siewior {
523aab70afSSebastian Siewior 	return container_of(f, struct f_fastboot, usb_function);
533aab70afSSebastian Siewior }
543aab70afSSebastian Siewior 
553aab70afSSebastian Siewior static struct f_fastboot *fastboot_func;
563aab70afSSebastian Siewior static unsigned int download_size;
573aab70afSSebastian Siewior static unsigned int download_bytes;
589e4b510dSDileep Katta static bool is_high_speed;
593aab70afSSebastian Siewior 
603aab70afSSebastian Siewior static struct usb_endpoint_descriptor fs_ep_in = {
613aab70afSSebastian Siewior 	.bLength            = USB_DT_ENDPOINT_SIZE,
623aab70afSSebastian Siewior 	.bDescriptorType    = USB_DT_ENDPOINT,
633aab70afSSebastian Siewior 	.bEndpointAddress   = USB_DIR_IN,
643aab70afSSebastian Siewior 	.bmAttributes       = USB_ENDPOINT_XFER_BULK,
653aab70afSSebastian Siewior 	.wMaxPacketSize     = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
663aab70afSSebastian Siewior 	.bInterval          = 0x00,
673aab70afSSebastian Siewior };
683aab70afSSebastian Siewior 
693aab70afSSebastian Siewior static struct usb_endpoint_descriptor fs_ep_out = {
703aab70afSSebastian Siewior 	.bLength		= USB_DT_ENDPOINT_SIZE,
713aab70afSSebastian Siewior 	.bDescriptorType	= USB_DT_ENDPOINT,
723aab70afSSebastian Siewior 	.bEndpointAddress	= USB_DIR_OUT,
733aab70afSSebastian Siewior 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
743aab70afSSebastian Siewior 	.wMaxPacketSize		= RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
753aab70afSSebastian Siewior 	.bInterval		= 0x00,
763aab70afSSebastian Siewior };
773aab70afSSebastian Siewior 
783aab70afSSebastian Siewior static struct usb_endpoint_descriptor hs_ep_out = {
793aab70afSSebastian Siewior 	.bLength		= USB_DT_ENDPOINT_SIZE,
803aab70afSSebastian Siewior 	.bDescriptorType	= USB_DT_ENDPOINT,
813aab70afSSebastian Siewior 	.bEndpointAddress	= USB_DIR_OUT,
823aab70afSSebastian Siewior 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
833aab70afSSebastian Siewior 	.wMaxPacketSize		= RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
843aab70afSSebastian Siewior 	.bInterval		= 0x00,
853aab70afSSebastian Siewior };
863aab70afSSebastian Siewior 
873aab70afSSebastian Siewior static struct usb_interface_descriptor interface_desc = {
883aab70afSSebastian Siewior 	.bLength		= USB_DT_INTERFACE_SIZE,
893aab70afSSebastian Siewior 	.bDescriptorType	= USB_DT_INTERFACE,
903aab70afSSebastian Siewior 	.bInterfaceNumber	= 0x00,
913aab70afSSebastian Siewior 	.bAlternateSetting	= 0x00,
923aab70afSSebastian Siewior 	.bNumEndpoints		= 0x02,
933aab70afSSebastian Siewior 	.bInterfaceClass	= FASTBOOT_INTERFACE_CLASS,
943aab70afSSebastian Siewior 	.bInterfaceSubClass	= FASTBOOT_INTERFACE_SUB_CLASS,
953aab70afSSebastian Siewior 	.bInterfaceProtocol	= FASTBOOT_INTERFACE_PROTOCOL,
963aab70afSSebastian Siewior };
973aab70afSSebastian Siewior 
983aab70afSSebastian Siewior static struct usb_descriptor_header *fb_runtime_descs[] = {
993aab70afSSebastian Siewior 	(struct usb_descriptor_header *)&interface_desc,
1003aab70afSSebastian Siewior 	(struct usb_descriptor_header *)&fs_ep_in,
1013aab70afSSebastian Siewior 	(struct usb_descriptor_header *)&hs_ep_out,
1023aab70afSSebastian Siewior 	NULL,
1033aab70afSSebastian Siewior };
1043aab70afSSebastian Siewior 
1053aab70afSSebastian Siewior /*
1063aab70afSSebastian Siewior  * static strings, in UTF-8
1073aab70afSSebastian Siewior  */
1083aab70afSSebastian Siewior static const char fastboot_name[] = "Android Fastboot";
1093aab70afSSebastian Siewior 
1103aab70afSSebastian Siewior static struct usb_string fastboot_string_defs[] = {
1113aab70afSSebastian Siewior 	[0].s = fastboot_name,
1123aab70afSSebastian Siewior 	{  }			/* end of list */
1133aab70afSSebastian Siewior };
1143aab70afSSebastian Siewior 
1153aab70afSSebastian Siewior static struct usb_gadget_strings stringtab_fastboot = {
1163aab70afSSebastian Siewior 	.language	= 0x0409,	/* en-us */
1173aab70afSSebastian Siewior 	.strings	= fastboot_string_defs,
1183aab70afSSebastian Siewior };
1193aab70afSSebastian Siewior 
1203aab70afSSebastian Siewior static struct usb_gadget_strings *fastboot_strings[] = {
1213aab70afSSebastian Siewior 	&stringtab_fastboot,
1223aab70afSSebastian Siewior 	NULL,
1233aab70afSSebastian Siewior };
1243aab70afSSebastian Siewior 
1253aab70afSSebastian Siewior static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
126*e2ec3e46SAlexey Firago static int strcmp_l1(const char *s1, const char *s2);
1273aab70afSSebastian Siewior 
1283aab70afSSebastian Siewior static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
1293aab70afSSebastian Siewior {
1303aab70afSSebastian Siewior 	int status = req->status;
1313aab70afSSebastian Siewior 	if (!status)
1323aab70afSSebastian Siewior 		return;
1333aab70afSSebastian Siewior 	printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
1343aab70afSSebastian Siewior }
1353aab70afSSebastian Siewior 
1363aab70afSSebastian Siewior static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
1373aab70afSSebastian Siewior {
1383aab70afSSebastian Siewior 	int id;
1393aab70afSSebastian Siewior 	struct usb_gadget *gadget = c->cdev->gadget;
1403aab70afSSebastian Siewior 	struct f_fastboot *f_fb = func_to_fastboot(f);
141537cd072SDileep Katta 	const char *s;
1423aab70afSSebastian Siewior 
1433aab70afSSebastian Siewior 	/* DYNAMIC interface numbers assignments */
1443aab70afSSebastian Siewior 	id = usb_interface_id(c, f);
1453aab70afSSebastian Siewior 	if (id < 0)
1463aab70afSSebastian Siewior 		return id;
1473aab70afSSebastian Siewior 	interface_desc.bInterfaceNumber = id;
1483aab70afSSebastian Siewior 
1493aab70afSSebastian Siewior 	id = usb_string_id(c->cdev);
1503aab70afSSebastian Siewior 	if (id < 0)
1513aab70afSSebastian Siewior 		return id;
1523aab70afSSebastian Siewior 	fastboot_string_defs[0].id = id;
1533aab70afSSebastian Siewior 	interface_desc.iInterface = id;
1543aab70afSSebastian Siewior 
1553aab70afSSebastian Siewior 	f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
1563aab70afSSebastian Siewior 	if (!f_fb->in_ep)
1573aab70afSSebastian Siewior 		return -ENODEV;
1583aab70afSSebastian Siewior 	f_fb->in_ep->driver_data = c->cdev;
1593aab70afSSebastian Siewior 
1603aab70afSSebastian Siewior 	f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
1613aab70afSSebastian Siewior 	if (!f_fb->out_ep)
1623aab70afSSebastian Siewior 		return -ENODEV;
1633aab70afSSebastian Siewior 	f_fb->out_ep->driver_data = c->cdev;
1643aab70afSSebastian Siewior 
1653aab70afSSebastian Siewior 	hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
1663aab70afSSebastian Siewior 
167537cd072SDileep Katta 	s = getenv("serial#");
168537cd072SDileep Katta 	if (s)
169537cd072SDileep Katta 		g_dnl_set_serialnumber((char *)s);
170537cd072SDileep Katta 
1713aab70afSSebastian Siewior 	return 0;
1723aab70afSSebastian Siewior }
1733aab70afSSebastian Siewior 
1743aab70afSSebastian Siewior static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
1753aab70afSSebastian Siewior {
1763aab70afSSebastian Siewior 	memset(fastboot_func, 0, sizeof(*fastboot_func));
1773aab70afSSebastian Siewior }
1783aab70afSSebastian Siewior 
1793aab70afSSebastian Siewior static void fastboot_disable(struct usb_function *f)
1803aab70afSSebastian Siewior {
1813aab70afSSebastian Siewior 	struct f_fastboot *f_fb = func_to_fastboot(f);
1823aab70afSSebastian Siewior 
1833aab70afSSebastian Siewior 	usb_ep_disable(f_fb->out_ep);
1843aab70afSSebastian Siewior 	usb_ep_disable(f_fb->in_ep);
1853aab70afSSebastian Siewior 
1863aab70afSSebastian Siewior 	if (f_fb->out_req) {
1873aab70afSSebastian Siewior 		free(f_fb->out_req->buf);
1883aab70afSSebastian Siewior 		usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
1893aab70afSSebastian Siewior 		f_fb->out_req = NULL;
1903aab70afSSebastian Siewior 	}
1913aab70afSSebastian Siewior 	if (f_fb->in_req) {
1923aab70afSSebastian Siewior 		free(f_fb->in_req->buf);
1933aab70afSSebastian Siewior 		usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
1943aab70afSSebastian Siewior 		f_fb->in_req = NULL;
1953aab70afSSebastian Siewior 	}
1963aab70afSSebastian Siewior }
1973aab70afSSebastian Siewior 
1983aab70afSSebastian Siewior static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
1993aab70afSSebastian Siewior {
2003aab70afSSebastian Siewior 	struct usb_request *req;
2013aab70afSSebastian Siewior 
2023aab70afSSebastian Siewior 	req = usb_ep_alloc_request(ep, 0);
2033aab70afSSebastian Siewior 	if (!req)
2043aab70afSSebastian Siewior 		return NULL;
2053aab70afSSebastian Siewior 
2063aab70afSSebastian Siewior 	req->length = EP_BUFFER_SIZE;
2073aab70afSSebastian Siewior 	req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
2083aab70afSSebastian Siewior 	if (!req->buf) {
2093aab70afSSebastian Siewior 		usb_ep_free_request(ep, req);
2103aab70afSSebastian Siewior 		return NULL;
2113aab70afSSebastian Siewior 	}
2123aab70afSSebastian Siewior 
2133aab70afSSebastian Siewior 	memset(req->buf, 0, req->length);
2143aab70afSSebastian Siewior 	return req;
2153aab70afSSebastian Siewior }
2163aab70afSSebastian Siewior 
2173aab70afSSebastian Siewior static int fastboot_set_alt(struct usb_function *f,
2183aab70afSSebastian Siewior 			    unsigned interface, unsigned alt)
2193aab70afSSebastian Siewior {
2203aab70afSSebastian Siewior 	int ret;
2213aab70afSSebastian Siewior 	struct usb_composite_dev *cdev = f->config->cdev;
2223aab70afSSebastian Siewior 	struct usb_gadget *gadget = cdev->gadget;
2233aab70afSSebastian Siewior 	struct f_fastboot *f_fb = func_to_fastboot(f);
2243aab70afSSebastian Siewior 
2253aab70afSSebastian Siewior 	debug("%s: func: %s intf: %d alt: %d\n",
2263aab70afSSebastian Siewior 	      __func__, f->name, interface, alt);
2273aab70afSSebastian Siewior 
2283aab70afSSebastian Siewior 	/* make sure we don't enable the ep twice */
2299e4b510dSDileep Katta 	if (gadget->speed == USB_SPEED_HIGH) {
2303aab70afSSebastian Siewior 		ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
2319e4b510dSDileep Katta 		is_high_speed = true;
2329e4b510dSDileep Katta 	} else {
2333aab70afSSebastian Siewior 		ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
2349e4b510dSDileep Katta 		is_high_speed = false;
2359e4b510dSDileep Katta 	}
2363aab70afSSebastian Siewior 	if (ret) {
2373aab70afSSebastian Siewior 		puts("failed to enable out ep\n");
2383aab70afSSebastian Siewior 		return ret;
2393aab70afSSebastian Siewior 	}
2403aab70afSSebastian Siewior 
2413aab70afSSebastian Siewior 	f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
2423aab70afSSebastian Siewior 	if (!f_fb->out_req) {
2433aab70afSSebastian Siewior 		puts("failed to alloc out req\n");
2443aab70afSSebastian Siewior 		ret = -EINVAL;
2453aab70afSSebastian Siewior 		goto err;
2463aab70afSSebastian Siewior 	}
2473aab70afSSebastian Siewior 	f_fb->out_req->complete = rx_handler_command;
2483aab70afSSebastian Siewior 
2493aab70afSSebastian Siewior 	ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
2503aab70afSSebastian Siewior 	if (ret) {
2513aab70afSSebastian Siewior 		puts("failed to enable in ep\n");
2523aab70afSSebastian Siewior 		goto err;
2533aab70afSSebastian Siewior 	}
2543aab70afSSebastian Siewior 
2553aab70afSSebastian Siewior 	f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
2563aab70afSSebastian Siewior 	if (!f_fb->in_req) {
2573aab70afSSebastian Siewior 		puts("failed alloc req in\n");
2583aab70afSSebastian Siewior 		ret = -EINVAL;
2593aab70afSSebastian Siewior 		goto err;
2603aab70afSSebastian Siewior 	}
2613aab70afSSebastian Siewior 	f_fb->in_req->complete = fastboot_complete;
2623aab70afSSebastian Siewior 
2633aab70afSSebastian Siewior 	ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
2643aab70afSSebastian Siewior 	if (ret)
2653aab70afSSebastian Siewior 		goto err;
2663aab70afSSebastian Siewior 
2673aab70afSSebastian Siewior 	return 0;
2683aab70afSSebastian Siewior err:
2693aab70afSSebastian Siewior 	fastboot_disable(f);
2703aab70afSSebastian Siewior 	return ret;
2713aab70afSSebastian Siewior }
2723aab70afSSebastian Siewior 
2733aab70afSSebastian Siewior static int fastboot_add(struct usb_configuration *c)
2743aab70afSSebastian Siewior {
2753aab70afSSebastian Siewior 	struct f_fastboot *f_fb = fastboot_func;
2763aab70afSSebastian Siewior 	int status;
2773aab70afSSebastian Siewior 
2783aab70afSSebastian Siewior 	debug("%s: cdev: 0x%p\n", __func__, c->cdev);
2793aab70afSSebastian Siewior 
2803aab70afSSebastian Siewior 	if (!f_fb) {
2813aab70afSSebastian Siewior 		f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
2823aab70afSSebastian Siewior 		if (!f_fb)
2833aab70afSSebastian Siewior 			return -ENOMEM;
2843aab70afSSebastian Siewior 
2853aab70afSSebastian Siewior 		fastboot_func = f_fb;
2863aab70afSSebastian Siewior 		memset(f_fb, 0, sizeof(*f_fb));
2873aab70afSSebastian Siewior 	}
2883aab70afSSebastian Siewior 
2893aab70afSSebastian Siewior 	f_fb->usb_function.name = "f_fastboot";
2903aab70afSSebastian Siewior 	f_fb->usb_function.hs_descriptors = fb_runtime_descs;
2913aab70afSSebastian Siewior 	f_fb->usb_function.bind = fastboot_bind;
2923aab70afSSebastian Siewior 	f_fb->usb_function.unbind = fastboot_unbind;
2933aab70afSSebastian Siewior 	f_fb->usb_function.set_alt = fastboot_set_alt;
2943aab70afSSebastian Siewior 	f_fb->usb_function.disable = fastboot_disable;
2953aab70afSSebastian Siewior 	f_fb->usb_function.strings = fastboot_strings;
2963aab70afSSebastian Siewior 
2973aab70afSSebastian Siewior 	status = usb_add_function(c, &f_fb->usb_function);
2983aab70afSSebastian Siewior 	if (status) {
2993aab70afSSebastian Siewior 		free(f_fb);
3003aab70afSSebastian Siewior 		fastboot_func = f_fb;
3013aab70afSSebastian Siewior 	}
3023aab70afSSebastian Siewior 
3033aab70afSSebastian Siewior 	return status;
3043aab70afSSebastian Siewior }
3053aab70afSSebastian Siewior DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
3063aab70afSSebastian Siewior 
307593cbd93SSteve Rae static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
3083aab70afSSebastian Siewior {
3093aab70afSSebastian Siewior 	struct usb_request *in_req = fastboot_func->in_req;
3103aab70afSSebastian Siewior 	int ret;
3113aab70afSSebastian Siewior 
3123aab70afSSebastian Siewior 	memcpy(in_req->buf, buffer, buffer_size);
3133aab70afSSebastian Siewior 	in_req->length = buffer_size;
3143aab70afSSebastian Siewior 	ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
3153aab70afSSebastian Siewior 	if (ret)
3163aab70afSSebastian Siewior 		printf("Error %d on queue\n", ret);
3173aab70afSSebastian Siewior 	return 0;
3183aab70afSSebastian Siewior }
3193aab70afSSebastian Siewior 
3203aab70afSSebastian Siewior static int fastboot_tx_write_str(const char *buffer)
3213aab70afSSebastian Siewior {
3223aab70afSSebastian Siewior 	return fastboot_tx_write(buffer, strlen(buffer));
3233aab70afSSebastian Siewior }
3243aab70afSSebastian Siewior 
3253aab70afSSebastian Siewior static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
3263aab70afSSebastian Siewior {
3273aab70afSSebastian Siewior 	do_reset(NULL, 0, 0, NULL);
3283aab70afSSebastian Siewior }
3293aab70afSSebastian Siewior 
330*e2ec3e46SAlexey Firago int __weak fb_set_reboot_flag(void)
331*e2ec3e46SAlexey Firago {
332*e2ec3e46SAlexey Firago 	return -ENOSYS;
333*e2ec3e46SAlexey Firago }
334*e2ec3e46SAlexey Firago 
3353aab70afSSebastian Siewior static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
3363aab70afSSebastian Siewior {
337*e2ec3e46SAlexey Firago 	char *cmd = req->buf;
338*e2ec3e46SAlexey Firago 	if (!strcmp_l1("reboot-bootloader", cmd)) {
339*e2ec3e46SAlexey Firago 		if (fb_set_reboot_flag()) {
340*e2ec3e46SAlexey Firago 			fastboot_tx_write_str("FAILCannot set reboot flag");
341*e2ec3e46SAlexey Firago 			return;
342*e2ec3e46SAlexey Firago 		}
343*e2ec3e46SAlexey Firago 	}
3443aab70afSSebastian Siewior 	fastboot_func->in_req->complete = compl_do_reset;
3453aab70afSSebastian Siewior 	fastboot_tx_write_str("OKAY");
3463aab70afSSebastian Siewior }
3473aab70afSSebastian Siewior 
3483aab70afSSebastian Siewior static int strcmp_l1(const char *s1, const char *s2)
3493aab70afSSebastian Siewior {
3503aab70afSSebastian Siewior 	if (!s1 || !s2)
3513aab70afSSebastian Siewior 		return -1;
3523aab70afSSebastian Siewior 	return strncmp(s1, s2, strlen(s1));
3533aab70afSSebastian Siewior }
3543aab70afSSebastian Siewior 
3553aab70afSSebastian Siewior static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
3563aab70afSSebastian Siewior {
3573aab70afSSebastian Siewior 	char *cmd = req->buf;
3583aab70afSSebastian Siewior 	char response[RESPONSE_LEN];
3593aab70afSSebastian Siewior 	const char *s;
36029425be4SJeroen Hofstee 	size_t chars_left;
3613aab70afSSebastian Siewior 
3623aab70afSSebastian Siewior 	strcpy(response, "OKAY");
36329425be4SJeroen Hofstee 	chars_left = sizeof(response) - strlen(response) - 1;
36429425be4SJeroen Hofstee 
3653aab70afSSebastian Siewior 	strsep(&cmd, ":");
3663aab70afSSebastian Siewior 	if (!cmd) {
367593cbd93SSteve Rae 		error("missing variable\n");
3683aab70afSSebastian Siewior 		fastboot_tx_write_str("FAILmissing var");
3693aab70afSSebastian Siewior 		return;
3703aab70afSSebastian Siewior 	}
3713aab70afSSebastian Siewior 
3723aab70afSSebastian Siewior 	if (!strcmp_l1("version", cmd)) {
37329425be4SJeroen Hofstee 		strncat(response, FASTBOOT_VERSION, chars_left);
3743aab70afSSebastian Siewior 	} else if (!strcmp_l1("bootloader-version", cmd)) {
37529425be4SJeroen Hofstee 		strncat(response, U_BOOT_VERSION, chars_left);
376c674a666SEric Nelson 	} else if (!strcmp_l1("downloadsize", cmd) ||
377c674a666SEric Nelson 		!strcmp_l1("max-download-size", cmd)) {
3783aab70afSSebastian Siewior 		char str_num[12];
3793aab70afSSebastian Siewior 
38084c24f66SEric Nelson 		sprintf(str_num, "0x%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
38129425be4SJeroen Hofstee 		strncat(response, str_num, chars_left);
3823aab70afSSebastian Siewior 	} else if (!strcmp_l1("serialno", cmd)) {
3833aab70afSSebastian Siewior 		s = getenv("serial#");
3843aab70afSSebastian Siewior 		if (s)
38529425be4SJeroen Hofstee 			strncat(response, s, chars_left);
3863aab70afSSebastian Siewior 		else
3873aab70afSSebastian Siewior 			strcpy(response, "FAILValue not set");
3883aab70afSSebastian Siewior 	} else {
389593cbd93SSteve Rae 		error("unknown variable: %s\n", cmd);
3903aab70afSSebastian Siewior 		strcpy(response, "FAILVariable not implemented");
3913aab70afSSebastian Siewior 	}
3923aab70afSSebastian Siewior 	fastboot_tx_write_str(response);
3933aab70afSSebastian Siewior }
3943aab70afSSebastian Siewior 
3959e4b510dSDileep Katta static unsigned int rx_bytes_expected(unsigned int maxpacket)
3963aab70afSSebastian Siewior {
3973aab70afSSebastian Siewior 	int rx_remain = download_size - download_bytes;
3989e4b510dSDileep Katta 	int rem = 0;
3993aab70afSSebastian Siewior 	if (rx_remain < 0)
4003aab70afSSebastian Siewior 		return 0;
4013aab70afSSebastian Siewior 	if (rx_remain > EP_BUFFER_SIZE)
4023aab70afSSebastian Siewior 		return EP_BUFFER_SIZE;
4039e4b510dSDileep Katta 	if (rx_remain < maxpacket) {
4049e4b510dSDileep Katta 		rx_remain = maxpacket;
4059e4b510dSDileep Katta 	} else if (rx_remain % maxpacket != 0) {
4069e4b510dSDileep Katta 		rem = rx_remain % maxpacket;
4079e4b510dSDileep Katta 		rx_remain = rx_remain + (maxpacket - rem);
4089e4b510dSDileep Katta 	}
4093aab70afSSebastian Siewior 	return rx_remain;
4103aab70afSSebastian Siewior }
4113aab70afSSebastian Siewior 
4123aab70afSSebastian Siewior #define BYTES_PER_DOT	0x20000
4133aab70afSSebastian Siewior static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
4143aab70afSSebastian Siewior {
4153aab70afSSebastian Siewior 	char response[RESPONSE_LEN];
4163aab70afSSebastian Siewior 	unsigned int transfer_size = download_size - download_bytes;
4173aab70afSSebastian Siewior 	const unsigned char *buffer = req->buf;
4183aab70afSSebastian Siewior 	unsigned int buffer_size = req->actual;
41923d1d10cSBo Shen 	unsigned int pre_dot_num, now_dot_num;
4209e4b510dSDileep Katta 	unsigned int max;
4213aab70afSSebastian Siewior 
4223aab70afSSebastian Siewior 	if (req->status != 0) {
4233aab70afSSebastian Siewior 		printf("Bad status: %d\n", req->status);
4243aab70afSSebastian Siewior 		return;
4253aab70afSSebastian Siewior 	}
4263aab70afSSebastian Siewior 
4273aab70afSSebastian Siewior 	if (buffer_size < transfer_size)
4283aab70afSSebastian Siewior 		transfer_size = buffer_size;
4293aab70afSSebastian Siewior 
4303aab70afSSebastian Siewior 	memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
4313aab70afSSebastian Siewior 	       buffer, transfer_size);
4323aab70afSSebastian Siewior 
43323d1d10cSBo Shen 	pre_dot_num = download_bytes / BYTES_PER_DOT;
4343aab70afSSebastian Siewior 	download_bytes += transfer_size;
43523d1d10cSBo Shen 	now_dot_num = download_bytes / BYTES_PER_DOT;
43623d1d10cSBo Shen 
43723d1d10cSBo Shen 	if (pre_dot_num != now_dot_num) {
43823d1d10cSBo Shen 		putc('.');
43923d1d10cSBo Shen 		if (!(now_dot_num % 74))
44023d1d10cSBo Shen 			putc('\n');
44123d1d10cSBo Shen 	}
4423aab70afSSebastian Siewior 
4433aab70afSSebastian Siewior 	/* Check if transfer is done */
4443aab70afSSebastian Siewior 	if (download_bytes >= download_size) {
4453aab70afSSebastian Siewior 		/*
4463aab70afSSebastian Siewior 		 * Reset global transfer variable, keep download_bytes because
4473aab70afSSebastian Siewior 		 * it will be used in the next possible flashing command
4483aab70afSSebastian Siewior 		 */
4493aab70afSSebastian Siewior 		download_size = 0;
4503aab70afSSebastian Siewior 		req->complete = rx_handler_command;
4513aab70afSSebastian Siewior 		req->length = EP_BUFFER_SIZE;
4523aab70afSSebastian Siewior 
4533aab70afSSebastian Siewior 		sprintf(response, "OKAY");
4543aab70afSSebastian Siewior 		fastboot_tx_write_str(response);
4553aab70afSSebastian Siewior 
4563aab70afSSebastian Siewior 		printf("\ndownloading of %d bytes finished\n", download_bytes);
4573aab70afSSebastian Siewior 	} else {
4589e4b510dSDileep Katta 		max = is_high_speed ? hs_ep_out.wMaxPacketSize :
4599e4b510dSDileep Katta 				fs_ep_out.wMaxPacketSize;
4609e4b510dSDileep Katta 		req->length = rx_bytes_expected(max);
4613aab70afSSebastian Siewior 		if (req->length < ep->maxpacket)
4623aab70afSSebastian Siewior 			req->length = ep->maxpacket;
4633aab70afSSebastian Siewior 	}
4643aab70afSSebastian Siewior 
4653aab70afSSebastian Siewior 	req->actual = 0;
4663aab70afSSebastian Siewior 	usb_ep_queue(ep, req, 0);
4673aab70afSSebastian Siewior }
4683aab70afSSebastian Siewior 
4693aab70afSSebastian Siewior static void cb_download(struct usb_ep *ep, struct usb_request *req)
4703aab70afSSebastian Siewior {
4713aab70afSSebastian Siewior 	char *cmd = req->buf;
4723aab70afSSebastian Siewior 	char response[RESPONSE_LEN];
4739e4b510dSDileep Katta 	unsigned int max;
4743aab70afSSebastian Siewior 
4753aab70afSSebastian Siewior 	strsep(&cmd, ":");
4763aab70afSSebastian Siewior 	download_size = simple_strtoul(cmd, NULL, 16);
4773aab70afSSebastian Siewior 	download_bytes = 0;
4783aab70afSSebastian Siewior 
4793aab70afSSebastian Siewior 	printf("Starting download of %d bytes\n", download_size);
4803aab70afSSebastian Siewior 
4813aab70afSSebastian Siewior 	if (0 == download_size) {
4823aab70afSSebastian Siewior 		sprintf(response, "FAILdata invalid size");
4833aab70afSSebastian Siewior 	} else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
4843aab70afSSebastian Siewior 		download_size = 0;
4853aab70afSSebastian Siewior 		sprintf(response, "FAILdata too large");
4863aab70afSSebastian Siewior 	} else {
4873aab70afSSebastian Siewior 		sprintf(response, "DATA%08x", download_size);
4883aab70afSSebastian Siewior 		req->complete = rx_handler_dl_image;
4899e4b510dSDileep Katta 		max = is_high_speed ? hs_ep_out.wMaxPacketSize :
4909e4b510dSDileep Katta 			fs_ep_out.wMaxPacketSize;
4919e4b510dSDileep Katta 		req->length = rx_bytes_expected(max);
4923aab70afSSebastian Siewior 		if (req->length < ep->maxpacket)
4933aab70afSSebastian Siewior 			req->length = ep->maxpacket;
4943aab70afSSebastian Siewior 	}
4953aab70afSSebastian Siewior 	fastboot_tx_write_str(response);
4963aab70afSSebastian Siewior }
4973aab70afSSebastian Siewior 
4983aab70afSSebastian Siewior static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
4993aab70afSSebastian Siewior {
5003aab70afSSebastian Siewior 	char boot_addr_start[12];
5013aab70afSSebastian Siewior 	char *bootm_args[] = { "bootm", boot_addr_start, NULL };
5023aab70afSSebastian Siewior 
5033aab70afSSebastian Siewior 	puts("Booting kernel..\n");
5043aab70afSSebastian Siewior 
5053aab70afSSebastian Siewior 	sprintf(boot_addr_start, "0x%lx", load_addr);
5063aab70afSSebastian Siewior 	do_bootm(NULL, 0, 2, bootm_args);
5073aab70afSSebastian Siewior 
5083aab70afSSebastian Siewior 	/* This only happens if image is somehow faulty so we start over */
5093aab70afSSebastian Siewior 	do_reset(NULL, 0, 0, NULL);
5103aab70afSSebastian Siewior }
5113aab70afSSebastian Siewior 
5123aab70afSSebastian Siewior static void cb_boot(struct usb_ep *ep, struct usb_request *req)
5133aab70afSSebastian Siewior {
5143aab70afSSebastian Siewior 	fastboot_func->in_req->complete = do_bootm_on_complete;
5153aab70afSSebastian Siewior 	fastboot_tx_write_str("OKAY");
5163aab70afSSebastian Siewior }
5173aab70afSSebastian Siewior 
518267abc62SRob Herring static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
519267abc62SRob Herring {
520267abc62SRob Herring 	g_dnl_trigger_detach();
521267abc62SRob Herring }
522267abc62SRob Herring 
523267abc62SRob Herring static void cb_continue(struct usb_ep *ep, struct usb_request *req)
524267abc62SRob Herring {
525267abc62SRob Herring 	fastboot_func->in_req->complete = do_exit_on_complete;
526267abc62SRob Herring 	fastboot_tx_write_str("OKAY");
527267abc62SRob Herring }
528267abc62SRob Herring 
529d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH
530d1b5ed07SSteve Rae static void cb_flash(struct usb_ep *ep, struct usb_request *req)
531d1b5ed07SSteve Rae {
532d1b5ed07SSteve Rae 	char *cmd = req->buf;
533d1b5ed07SSteve Rae 	char response[RESPONSE_LEN];
534d1b5ed07SSteve Rae 
535d1b5ed07SSteve Rae 	strsep(&cmd, ":");
536d1b5ed07SSteve Rae 	if (!cmd) {
537593cbd93SSteve Rae 		error("missing partition name\n");
538d1b5ed07SSteve Rae 		fastboot_tx_write_str("FAILmissing partition name");
539d1b5ed07SSteve Rae 		return;
540d1b5ed07SSteve Rae 	}
541d1b5ed07SSteve Rae 
542d1b5ed07SSteve Rae 	strcpy(response, "FAILno flash device defined");
543d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
544d1b5ed07SSteve Rae 	fb_mmc_flash_write(cmd, (void *)CONFIG_USB_FASTBOOT_BUF_ADDR,
545d1b5ed07SSteve Rae 			   download_bytes, response);
546d1b5ed07SSteve Rae #endif
547d1b5ed07SSteve Rae 	fastboot_tx_write_str(response);
548d1b5ed07SSteve Rae }
549d1b5ed07SSteve Rae #endif
550d1b5ed07SSteve Rae 
551de195620SMichael Scott static void cb_oem(struct usb_ep *ep, struct usb_request *req)
552de195620SMichael Scott {
553de195620SMichael Scott 	char *cmd = req->buf;
554372d7decSRob Herring #ifdef CONFIG_FASTBOOT_FLASH
555372d7decSRob Herring 	if (strncmp("format", cmd + 4, 6) == 0) {
556372d7decSRob Herring 		char cmdbuf[32];
557372d7decSRob Herring                 sprintf(cmdbuf, "gpt write mmc %x $partitions",
558372d7decSRob Herring 			CONFIG_FASTBOOT_FLASH_MMC_DEV);
559372d7decSRob Herring                 if (run_command(cmdbuf, 0))
560372d7decSRob Herring 			fastboot_tx_write_str("FAIL");
561372d7decSRob Herring                 else
562372d7decSRob Herring 			fastboot_tx_write_str("OKAY");
563372d7decSRob Herring 	} else
564372d7decSRob Herring #endif
565de195620SMichael Scott 	if (strncmp("unlock", cmd + 4, 8) == 0) {
566de195620SMichael Scott 		fastboot_tx_write_str("FAILnot implemented");
567de195620SMichael Scott 	}
568de195620SMichael Scott 	else {
569de195620SMichael Scott 		fastboot_tx_write_str("FAILunknown oem command");
570de195620SMichael Scott 	}
571de195620SMichael Scott }
572de195620SMichael Scott 
57389792381SDileep Katta #ifdef CONFIG_FASTBOOT_FLASH
57489792381SDileep Katta static void cb_erase(struct usb_ep *ep, struct usb_request *req)
57589792381SDileep Katta {
57689792381SDileep Katta 	char *cmd = req->buf;
57789792381SDileep Katta 	char response[RESPONSE_LEN];
57889792381SDileep Katta 
57989792381SDileep Katta 	strsep(&cmd, ":");
58089792381SDileep Katta 	if (!cmd) {
58189792381SDileep Katta 		error("missing partition name");
58289792381SDileep Katta 		fastboot_tx_write_str("FAILmissing partition name");
58389792381SDileep Katta 		return;
58489792381SDileep Katta 	}
58589792381SDileep Katta 
58689792381SDileep Katta 	strcpy(response, "FAILno flash device defined");
58789792381SDileep Katta 
58889792381SDileep Katta #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
58989792381SDileep Katta 	fb_mmc_erase(cmd, response);
59089792381SDileep Katta #endif
59189792381SDileep Katta 	fastboot_tx_write_str(response);
59289792381SDileep Katta }
59389792381SDileep Katta #endif
59489792381SDileep Katta 
5953aab70afSSebastian Siewior struct cmd_dispatch_info {
5963aab70afSSebastian Siewior 	char *cmd;
5973aab70afSSebastian Siewior 	void (*cb)(struct usb_ep *ep, struct usb_request *req);
5983aab70afSSebastian Siewior };
5993aab70afSSebastian Siewior 
6003aab70afSSebastian Siewior static const struct cmd_dispatch_info cmd_dispatch_info[] = {
6013aab70afSSebastian Siewior 	{
6023aab70afSSebastian Siewior 		.cmd = "reboot",
6033aab70afSSebastian Siewior 		.cb = cb_reboot,
6043aab70afSSebastian Siewior 	}, {
6053aab70afSSebastian Siewior 		.cmd = "getvar:",
6063aab70afSSebastian Siewior 		.cb = cb_getvar,
6073aab70afSSebastian Siewior 	}, {
6083aab70afSSebastian Siewior 		.cmd = "download:",
6093aab70afSSebastian Siewior 		.cb = cb_download,
6103aab70afSSebastian Siewior 	}, {
6113aab70afSSebastian Siewior 		.cmd = "boot",
6123aab70afSSebastian Siewior 		.cb = cb_boot,
613267abc62SRob Herring 	}, {
614267abc62SRob Herring 		.cmd = "continue",
615267abc62SRob Herring 		.cb = cb_continue,
6163aab70afSSebastian Siewior 	},
617d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH
618d1b5ed07SSteve Rae 	{
619d1b5ed07SSteve Rae 		.cmd = "flash",
620d1b5ed07SSteve Rae 		.cb = cb_flash,
62189792381SDileep Katta 	}, {
62289792381SDileep Katta 		.cmd = "erase",
62389792381SDileep Katta 		.cb = cb_erase,
624d1b5ed07SSteve Rae 	},
625d1b5ed07SSteve Rae #endif
626de195620SMichael Scott 	{
627de195620SMichael Scott 		.cmd = "oem",
628de195620SMichael Scott 		.cb = cb_oem,
629de195620SMichael Scott 	},
6303aab70afSSebastian Siewior };
6313aab70afSSebastian Siewior 
6323aab70afSSebastian Siewior static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
6333aab70afSSebastian Siewior {
6343aab70afSSebastian Siewior 	char *cmdbuf = req->buf;
6353aab70afSSebastian Siewior 	void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
6363aab70afSSebastian Siewior 	int i;
6373aab70afSSebastian Siewior 
6383aab70afSSebastian Siewior 	for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
6393aab70afSSebastian Siewior 		if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
6403aab70afSSebastian Siewior 			func_cb = cmd_dispatch_info[i].cb;
6413aab70afSSebastian Siewior 			break;
6423aab70afSSebastian Siewior 		}
6433aab70afSSebastian Siewior 	}
6443aab70afSSebastian Siewior 
645593cbd93SSteve Rae 	if (!func_cb) {
646593cbd93SSteve Rae 		error("unknown command: %s\n", cmdbuf);
6473aab70afSSebastian Siewior 		fastboot_tx_write_str("FAILunknown command");
648593cbd93SSteve Rae 	} else {
649e2140588SEric Nelson 		if (req->actual < req->length) {
650e2140588SEric Nelson 			u8 *buf = (u8 *)req->buf;
651e2140588SEric Nelson 			buf[req->actual] = 0;
6523aab70afSSebastian Siewior 			func_cb(ep, req);
653e2140588SEric Nelson 		} else {
654e2140588SEric Nelson 			error("buffer overflow\n");
655e2140588SEric Nelson 			fastboot_tx_write_str("FAILbuffer overflow");
656e2140588SEric Nelson 		}
657593cbd93SSteve Rae 	}
6583aab70afSSebastian Siewior 
6593aab70afSSebastian Siewior 	if (req->status == 0) {
6603aab70afSSebastian Siewior 		*cmdbuf = '\0';
6613aab70afSSebastian Siewior 		req->actual = 0;
6623aab70afSSebastian Siewior 		usb_ep_queue(ep, req, 0);
6633aab70afSSebastian Siewior 	}
6643aab70afSSebastian Siewior }
665