xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_fastboot.c (revision 4e1bbe849d3f26647783cb425fa4e8010e31ac90)
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>
163c8f98f5SMaxime Ripard #include <fastboot.h>
173aab70afSSebastian Siewior #include <malloc.h>
183aab70afSSebastian Siewior #include <linux/usb/ch9.h>
193aab70afSSebastian Siewior #include <linux/usb/gadget.h>
203aab70afSSebastian Siewior #include <linux/usb/composite.h>
213aab70afSSebastian Siewior #include <linux/compiler.h>
223aab70afSSebastian Siewior #include <version.h>
233aab70afSSebastian Siewior #include <g_dnl.h>
24367cce4dSXu Hongfei #include <android_avb/avb_ops_user.h>
25d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
26d1b5ed07SSteve Rae #include <fb_mmc.h>
27d1b5ed07SSteve Rae #endif
28bf8940d3SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
29bf8940d3SMaxime Ripard #include <fb_nand.h>
30bf8940d3SMaxime Ripard #endif
313aab70afSSebastian Siewior 
323aab70afSSebastian Siewior #define FASTBOOT_VERSION		"0.4"
333aab70afSSebastian Siewior 
343aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_CLASS	0xff
353aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_SUB_CLASS	0x42
363aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_PROTOCOL	0x03
373aab70afSSebastian Siewior 
383aab70afSSebastian Siewior #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0  (0x0200)
393aab70afSSebastian Siewior #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1  (0x0040)
403aab70afSSebastian Siewior #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE      (0x0040)
413aab70afSSebastian Siewior 
423aab70afSSebastian Siewior #define EP_BUFFER_SIZE			4096
43ac484c5aSRoger Quadros /*
44ac484c5aSRoger Quadros  * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
45ac484c5aSRoger Quadros  * (64 or 512 or 1024), else we break on certain controllers like DWC3
46ac484c5aSRoger Quadros  * that expect bulk OUT requests to be divisible by maxpacket size.
47ac484c5aSRoger Quadros  */
483aab70afSSebastian Siewior 
493aab70afSSebastian Siewior struct f_fastboot {
503aab70afSSebastian Siewior 	struct usb_function usb_function;
513aab70afSSebastian Siewior 
52593cbd93SSteve Rae 	/* IN/OUT EP's and corresponding requests */
533aab70afSSebastian Siewior 	struct usb_ep *in_ep, *out_ep;
543aab70afSSebastian Siewior 	struct usb_request *in_req, *out_req;
553aab70afSSebastian Siewior };
563aab70afSSebastian Siewior 
573aab70afSSebastian Siewior static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
583aab70afSSebastian Siewior {
593aab70afSSebastian Siewior 	return container_of(f, struct f_fastboot, usb_function);
603aab70afSSebastian Siewior }
613aab70afSSebastian Siewior 
623aab70afSSebastian Siewior static struct f_fastboot *fastboot_func;
633aab70afSSebastian Siewior static unsigned int download_size;
643aab70afSSebastian Siewior static unsigned int download_bytes;
65367cce4dSXu Hongfei static unsigned int upload_size;
66367cce4dSXu Hongfei static unsigned int upload_bytes;
67367cce4dSXu Hongfei static bool start_upload;
683aab70afSSebastian Siewior 
693aab70afSSebastian Siewior static struct usb_endpoint_descriptor fs_ep_in = {
703aab70afSSebastian Siewior 	.bLength            = USB_DT_ENDPOINT_SIZE,
713aab70afSSebastian Siewior 	.bDescriptorType    = USB_DT_ENDPOINT,
723aab70afSSebastian Siewior 	.bEndpointAddress   = USB_DIR_IN,
733aab70afSSebastian Siewior 	.bmAttributes       = USB_ENDPOINT_XFER_BULK,
74718156adSRoger Quadros 	.wMaxPacketSize     = cpu_to_le16(64),
753aab70afSSebastian Siewior };
763aab70afSSebastian Siewior 
773aab70afSSebastian Siewior static struct usb_endpoint_descriptor fs_ep_out = {
783aab70afSSebastian Siewior 	.bLength		= USB_DT_ENDPOINT_SIZE,
793aab70afSSebastian Siewior 	.bDescriptorType	= USB_DT_ENDPOINT,
803aab70afSSebastian Siewior 	.bEndpointAddress	= USB_DIR_OUT,
813aab70afSSebastian Siewior 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
82718156adSRoger Quadros 	.wMaxPacketSize		= cpu_to_le16(64),
83718156adSRoger Quadros };
84718156adSRoger Quadros 
85718156adSRoger Quadros static struct usb_endpoint_descriptor hs_ep_in = {
86718156adSRoger Quadros 	.bLength		= USB_DT_ENDPOINT_SIZE,
87718156adSRoger Quadros 	.bDescriptorType	= USB_DT_ENDPOINT,
88718156adSRoger Quadros 	.bEndpointAddress	= USB_DIR_IN,
89718156adSRoger Quadros 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
90718156adSRoger Quadros 	.wMaxPacketSize		= cpu_to_le16(512),
913aab70afSSebastian Siewior };
923aab70afSSebastian Siewior 
933aab70afSSebastian Siewior static struct usb_endpoint_descriptor hs_ep_out = {
943aab70afSSebastian Siewior 	.bLength		= USB_DT_ENDPOINT_SIZE,
953aab70afSSebastian Siewior 	.bDescriptorType	= USB_DT_ENDPOINT,
963aab70afSSebastian Siewior 	.bEndpointAddress	= USB_DIR_OUT,
973aab70afSSebastian Siewior 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
98718156adSRoger Quadros 	.wMaxPacketSize		= cpu_to_le16(512),
993aab70afSSebastian Siewior };
1003aab70afSSebastian Siewior 
1013aab70afSSebastian Siewior static struct usb_interface_descriptor interface_desc = {
1023aab70afSSebastian Siewior 	.bLength		= USB_DT_INTERFACE_SIZE,
1033aab70afSSebastian Siewior 	.bDescriptorType	= USB_DT_INTERFACE,
1043aab70afSSebastian Siewior 	.bInterfaceNumber	= 0x00,
1053aab70afSSebastian Siewior 	.bAlternateSetting	= 0x00,
1063aab70afSSebastian Siewior 	.bNumEndpoints		= 0x02,
1073aab70afSSebastian Siewior 	.bInterfaceClass	= FASTBOOT_INTERFACE_CLASS,
1083aab70afSSebastian Siewior 	.bInterfaceSubClass	= FASTBOOT_INTERFACE_SUB_CLASS,
1093aab70afSSebastian Siewior 	.bInterfaceProtocol	= FASTBOOT_INTERFACE_PROTOCOL,
1103aab70afSSebastian Siewior };
1113aab70afSSebastian Siewior 
112718156adSRoger Quadros static struct usb_descriptor_header *fb_fs_function[] = {
1133aab70afSSebastian Siewior 	(struct usb_descriptor_header *)&interface_desc,
1143aab70afSSebastian Siewior 	(struct usb_descriptor_header *)&fs_ep_in,
115718156adSRoger Quadros 	(struct usb_descriptor_header *)&fs_ep_out,
116718156adSRoger Quadros };
117718156adSRoger Quadros 
118718156adSRoger Quadros static struct usb_descriptor_header *fb_hs_function[] = {
119718156adSRoger Quadros 	(struct usb_descriptor_header *)&interface_desc,
120718156adSRoger Quadros 	(struct usb_descriptor_header *)&hs_ep_in,
1213aab70afSSebastian Siewior 	(struct usb_descriptor_header *)&hs_ep_out,
1223aab70afSSebastian Siewior 	NULL,
1233aab70afSSebastian Siewior };
1243aab70afSSebastian Siewior 
1258b704a0eSRoger Quadros static struct usb_endpoint_descriptor *
1268b704a0eSRoger Quadros fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
1278b704a0eSRoger Quadros 	    struct usb_endpoint_descriptor *hs)
1288b704a0eSRoger Quadros {
1298b704a0eSRoger Quadros 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
1308b704a0eSRoger Quadros 		return hs;
1318b704a0eSRoger Quadros 	return fs;
1328b704a0eSRoger Quadros }
1338b704a0eSRoger Quadros 
1343aab70afSSebastian Siewior /*
1353aab70afSSebastian Siewior  * static strings, in UTF-8
1363aab70afSSebastian Siewior  */
1373aab70afSSebastian Siewior static const char fastboot_name[] = "Android Fastboot";
1383aab70afSSebastian Siewior 
1393aab70afSSebastian Siewior static struct usb_string fastboot_string_defs[] = {
1403aab70afSSebastian Siewior 	[0].s = fastboot_name,
1413aab70afSSebastian Siewior 	{  }			/* end of list */
1423aab70afSSebastian Siewior };
1433aab70afSSebastian Siewior 
1443aab70afSSebastian Siewior static struct usb_gadget_strings stringtab_fastboot = {
1453aab70afSSebastian Siewior 	.language	= 0x0409,	/* en-us */
1463aab70afSSebastian Siewior 	.strings	= fastboot_string_defs,
1473aab70afSSebastian Siewior };
1483aab70afSSebastian Siewior 
1493aab70afSSebastian Siewior static struct usb_gadget_strings *fastboot_strings[] = {
1503aab70afSSebastian Siewior 	&stringtab_fastboot,
1513aab70afSSebastian Siewior 	NULL,
1523aab70afSSebastian Siewior };
1533aab70afSSebastian Siewior 
1543aab70afSSebastian Siewior static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
155e2ec3e46SAlexey Firago static int strcmp_l1(const char *s1, const char *s2);
1563aab70afSSebastian Siewior 
1573aab70afSSebastian Siewior static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
1583aab70afSSebastian Siewior {
1593aab70afSSebastian Siewior 	int status = req->status;
1603aab70afSSebastian Siewior 	if (!status)
1613aab70afSSebastian Siewior 		return;
1623aab70afSSebastian Siewior 	printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
1633aab70afSSebastian Siewior }
1643aab70afSSebastian Siewior 
1653aab70afSSebastian Siewior static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
1663aab70afSSebastian Siewior {
1673aab70afSSebastian Siewior 	int id;
1683aab70afSSebastian Siewior 	struct usb_gadget *gadget = c->cdev->gadget;
1693aab70afSSebastian Siewior 	struct f_fastboot *f_fb = func_to_fastboot(f);
170537cd072SDileep Katta 	const char *s;
1713aab70afSSebastian Siewior 
1723aab70afSSebastian Siewior 	/* DYNAMIC interface numbers assignments */
1733aab70afSSebastian Siewior 	id = usb_interface_id(c, f);
1743aab70afSSebastian Siewior 	if (id < 0)
1753aab70afSSebastian Siewior 		return id;
1763aab70afSSebastian Siewior 	interface_desc.bInterfaceNumber = id;
1773aab70afSSebastian Siewior 
1783aab70afSSebastian Siewior 	id = usb_string_id(c->cdev);
1793aab70afSSebastian Siewior 	if (id < 0)
1803aab70afSSebastian Siewior 		return id;
1813aab70afSSebastian Siewior 	fastboot_string_defs[0].id = id;
1823aab70afSSebastian Siewior 	interface_desc.iInterface = id;
1833aab70afSSebastian Siewior 
1843aab70afSSebastian Siewior 	f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
1853aab70afSSebastian Siewior 	if (!f_fb->in_ep)
1863aab70afSSebastian Siewior 		return -ENODEV;
1873aab70afSSebastian Siewior 	f_fb->in_ep->driver_data = c->cdev;
1883aab70afSSebastian Siewior 
1893aab70afSSebastian Siewior 	f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
1903aab70afSSebastian Siewior 	if (!f_fb->out_ep)
1913aab70afSSebastian Siewior 		return -ENODEV;
1923aab70afSSebastian Siewior 	f_fb->out_ep->driver_data = c->cdev;
1933aab70afSSebastian Siewior 
194718156adSRoger Quadros 	f->descriptors = fb_fs_function;
195718156adSRoger Quadros 
196718156adSRoger Quadros 	if (gadget_is_dualspeed(gadget)) {
197718156adSRoger Quadros 		/* Assume endpoint addresses are the same for both speeds */
198718156adSRoger Quadros 		hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
1993aab70afSSebastian Siewior 		hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
200718156adSRoger Quadros 		/* copy HS descriptors */
201718156adSRoger Quadros 		f->hs_descriptors = fb_hs_function;
202718156adSRoger Quadros 	}
2033aab70afSSebastian Siewior 
20400caae6dSSimon Glass 	s = env_get("serial#");
205537cd072SDileep Katta 	if (s)
206537cd072SDileep Katta 		g_dnl_set_serialnumber((char *)s);
207537cd072SDileep Katta 
2083aab70afSSebastian Siewior 	return 0;
2093aab70afSSebastian Siewior }
2103aab70afSSebastian Siewior 
2113aab70afSSebastian Siewior static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
2123aab70afSSebastian Siewior {
2133aab70afSSebastian Siewior 	memset(fastboot_func, 0, sizeof(*fastboot_func));
2143aab70afSSebastian Siewior }
2153aab70afSSebastian Siewior 
2163aab70afSSebastian Siewior static void fastboot_disable(struct usb_function *f)
2173aab70afSSebastian Siewior {
2183aab70afSSebastian Siewior 	struct f_fastboot *f_fb = func_to_fastboot(f);
2193aab70afSSebastian Siewior 
2203aab70afSSebastian Siewior 	usb_ep_disable(f_fb->out_ep);
2213aab70afSSebastian Siewior 	usb_ep_disable(f_fb->in_ep);
2223aab70afSSebastian Siewior 
2233aab70afSSebastian Siewior 	if (f_fb->out_req) {
2243aab70afSSebastian Siewior 		free(f_fb->out_req->buf);
2253aab70afSSebastian Siewior 		usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
2263aab70afSSebastian Siewior 		f_fb->out_req = NULL;
2273aab70afSSebastian Siewior 	}
2283aab70afSSebastian Siewior 	if (f_fb->in_req) {
2293aab70afSSebastian Siewior 		free(f_fb->in_req->buf);
2303aab70afSSebastian Siewior 		usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
2313aab70afSSebastian Siewior 		f_fb->in_req = NULL;
2323aab70afSSebastian Siewior 	}
2333aab70afSSebastian Siewior }
2343aab70afSSebastian Siewior 
2353aab70afSSebastian Siewior static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
2363aab70afSSebastian Siewior {
2373aab70afSSebastian Siewior 	struct usb_request *req;
2383aab70afSSebastian Siewior 
2393aab70afSSebastian Siewior 	req = usb_ep_alloc_request(ep, 0);
2403aab70afSSebastian Siewior 	if (!req)
2413aab70afSSebastian Siewior 		return NULL;
2423aab70afSSebastian Siewior 
2433aab70afSSebastian Siewior 	req->length = EP_BUFFER_SIZE;
2443aab70afSSebastian Siewior 	req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
2453aab70afSSebastian Siewior 	if (!req->buf) {
2463aab70afSSebastian Siewior 		usb_ep_free_request(ep, req);
2473aab70afSSebastian Siewior 		return NULL;
2483aab70afSSebastian Siewior 	}
2493aab70afSSebastian Siewior 
2503aab70afSSebastian Siewior 	memset(req->buf, 0, req->length);
2513aab70afSSebastian Siewior 	return req;
2523aab70afSSebastian Siewior }
2533aab70afSSebastian Siewior 
2543aab70afSSebastian Siewior static int fastboot_set_alt(struct usb_function *f,
2553aab70afSSebastian Siewior 			    unsigned interface, unsigned alt)
2563aab70afSSebastian Siewior {
2573aab70afSSebastian Siewior 	int ret;
2583aab70afSSebastian Siewior 	struct usb_composite_dev *cdev = f->config->cdev;
2593aab70afSSebastian Siewior 	struct usb_gadget *gadget = cdev->gadget;
2603aab70afSSebastian Siewior 	struct f_fastboot *f_fb = func_to_fastboot(f);
2618b704a0eSRoger Quadros 	const struct usb_endpoint_descriptor *d;
2623aab70afSSebastian Siewior 
2633aab70afSSebastian Siewior 	debug("%s: func: %s intf: %d alt: %d\n",
2643aab70afSSebastian Siewior 	      __func__, f->name, interface, alt);
2653aab70afSSebastian Siewior 
2668b704a0eSRoger Quadros 	d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
2678b704a0eSRoger Quadros 	ret = usb_ep_enable(f_fb->out_ep, d);
2683aab70afSSebastian Siewior 	if (ret) {
2693aab70afSSebastian Siewior 		puts("failed to enable out ep\n");
2703aab70afSSebastian Siewior 		return ret;
2713aab70afSSebastian Siewior 	}
2723aab70afSSebastian Siewior 
2733aab70afSSebastian Siewior 	f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
2743aab70afSSebastian Siewior 	if (!f_fb->out_req) {
2753aab70afSSebastian Siewior 		puts("failed to alloc out req\n");
2763aab70afSSebastian Siewior 		ret = -EINVAL;
2773aab70afSSebastian Siewior 		goto err;
2783aab70afSSebastian Siewior 	}
2793aab70afSSebastian Siewior 	f_fb->out_req->complete = rx_handler_command;
2803aab70afSSebastian Siewior 
2818b704a0eSRoger Quadros 	d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
2828b704a0eSRoger Quadros 	ret = usb_ep_enable(f_fb->in_ep, d);
2833aab70afSSebastian Siewior 	if (ret) {
2843aab70afSSebastian Siewior 		puts("failed to enable in ep\n");
2853aab70afSSebastian Siewior 		goto err;
2863aab70afSSebastian Siewior 	}
2873aab70afSSebastian Siewior 
2883aab70afSSebastian Siewior 	f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
2893aab70afSSebastian Siewior 	if (!f_fb->in_req) {
2903aab70afSSebastian Siewior 		puts("failed alloc req in\n");
2913aab70afSSebastian Siewior 		ret = -EINVAL;
2923aab70afSSebastian Siewior 		goto err;
2933aab70afSSebastian Siewior 	}
2943aab70afSSebastian Siewior 	f_fb->in_req->complete = fastboot_complete;
2953aab70afSSebastian Siewior 
2963aab70afSSebastian Siewior 	ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
2973aab70afSSebastian Siewior 	if (ret)
2983aab70afSSebastian Siewior 		goto err;
2993aab70afSSebastian Siewior 
3003aab70afSSebastian Siewior 	return 0;
3013aab70afSSebastian Siewior err:
3023aab70afSSebastian Siewior 	fastboot_disable(f);
3033aab70afSSebastian Siewior 	return ret;
3043aab70afSSebastian Siewior }
3053aab70afSSebastian Siewior 
3063aab70afSSebastian Siewior static int fastboot_add(struct usb_configuration *c)
3073aab70afSSebastian Siewior {
3083aab70afSSebastian Siewior 	struct f_fastboot *f_fb = fastboot_func;
3093aab70afSSebastian Siewior 	int status;
3103aab70afSSebastian Siewior 
3113aab70afSSebastian Siewior 	debug("%s: cdev: 0x%p\n", __func__, c->cdev);
3123aab70afSSebastian Siewior 
3133aab70afSSebastian Siewior 	if (!f_fb) {
3143aab70afSSebastian Siewior 		f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
3153aab70afSSebastian Siewior 		if (!f_fb)
3163aab70afSSebastian Siewior 			return -ENOMEM;
3173aab70afSSebastian Siewior 
3183aab70afSSebastian Siewior 		fastboot_func = f_fb;
3193aab70afSSebastian Siewior 		memset(f_fb, 0, sizeof(*f_fb));
3203aab70afSSebastian Siewior 	}
3213aab70afSSebastian Siewior 
3223aab70afSSebastian Siewior 	f_fb->usb_function.name = "f_fastboot";
3233aab70afSSebastian Siewior 	f_fb->usb_function.bind = fastboot_bind;
3243aab70afSSebastian Siewior 	f_fb->usb_function.unbind = fastboot_unbind;
3253aab70afSSebastian Siewior 	f_fb->usb_function.set_alt = fastboot_set_alt;
3263aab70afSSebastian Siewior 	f_fb->usb_function.disable = fastboot_disable;
3273aab70afSSebastian Siewior 	f_fb->usb_function.strings = fastboot_strings;
3283aab70afSSebastian Siewior 
3293aab70afSSebastian Siewior 	status = usb_add_function(c, &f_fb->usb_function);
3303aab70afSSebastian Siewior 	if (status) {
3313aab70afSSebastian Siewior 		free(f_fb);
3323aab70afSSebastian Siewior 		fastboot_func = f_fb;
3333aab70afSSebastian Siewior 	}
3343aab70afSSebastian Siewior 
3353aab70afSSebastian Siewior 	return status;
3363aab70afSSebastian Siewior }
3373aab70afSSebastian Siewior DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
3383aab70afSSebastian Siewior 
339593cbd93SSteve Rae static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
3403aab70afSSebastian Siewior {
3413aab70afSSebastian Siewior 	struct usb_request *in_req = fastboot_func->in_req;
3423aab70afSSebastian Siewior 	int ret;
3433aab70afSSebastian Siewior 
3443aab70afSSebastian Siewior 	memcpy(in_req->buf, buffer, buffer_size);
3453aab70afSSebastian Siewior 	in_req->length = buffer_size;
346bc9071c9SPaul Kocialkowski 
347bc9071c9SPaul Kocialkowski 	usb_ep_dequeue(fastboot_func->in_ep, in_req);
348bc9071c9SPaul Kocialkowski 
3493aab70afSSebastian Siewior 	ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
3503aab70afSSebastian Siewior 	if (ret)
3513aab70afSSebastian Siewior 		printf("Error %d on queue\n", ret);
3523aab70afSSebastian Siewior 	return 0;
3533aab70afSSebastian Siewior }
3543aab70afSSebastian Siewior 
3553aab70afSSebastian Siewior static int fastboot_tx_write_str(const char *buffer)
3563aab70afSSebastian Siewior {
3573aab70afSSebastian Siewior 	return fastboot_tx_write(buffer, strlen(buffer));
3583aab70afSSebastian Siewior }
3593aab70afSSebastian Siewior 
3603aab70afSSebastian Siewior static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
3613aab70afSSebastian Siewior {
3623aab70afSSebastian Siewior 	do_reset(NULL, 0, 0, NULL);
3633aab70afSSebastian Siewior }
3643aab70afSSebastian Siewior 
365e2ec3e46SAlexey Firago int __weak fb_set_reboot_flag(void)
366e2ec3e46SAlexey Firago {
367e2ec3e46SAlexey Firago 	return -ENOSYS;
368e2ec3e46SAlexey Firago }
369e2ec3e46SAlexey Firago 
3703aab70afSSebastian Siewior static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
3713aab70afSSebastian Siewior {
372e2ec3e46SAlexey Firago 	char *cmd = req->buf;
373e2ec3e46SAlexey Firago 	if (!strcmp_l1("reboot-bootloader", cmd)) {
374e2ec3e46SAlexey Firago 		if (fb_set_reboot_flag()) {
375e2ec3e46SAlexey Firago 			fastboot_tx_write_str("FAILCannot set reboot flag");
376e2ec3e46SAlexey Firago 			return;
377e2ec3e46SAlexey Firago 		}
378e2ec3e46SAlexey Firago 	}
3793aab70afSSebastian Siewior 	fastboot_func->in_req->complete = compl_do_reset;
3803aab70afSSebastian Siewior 	fastboot_tx_write_str("OKAY");
3813aab70afSSebastian Siewior }
3823aab70afSSebastian Siewior 
3833aab70afSSebastian Siewior static int strcmp_l1(const char *s1, const char *s2)
3843aab70afSSebastian Siewior {
3853aab70afSSebastian Siewior 	if (!s1 || !s2)
3863aab70afSSebastian Siewior 		return -1;
3873aab70afSSebastian Siewior 	return strncmp(s1, s2, strlen(s1));
3883aab70afSSebastian Siewior }
3893aab70afSSebastian Siewior 
3903aab70afSSebastian Siewior static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
3913aab70afSSebastian Siewior {
3923aab70afSSebastian Siewior 	char *cmd = req->buf;
3933c8f98f5SMaxime Ripard 	char response[FASTBOOT_RESPONSE_LEN];
3943aab70afSSebastian Siewior 	const char *s;
39529425be4SJeroen Hofstee 	size_t chars_left;
3963aab70afSSebastian Siewior 
3973aab70afSSebastian Siewior 	strcpy(response, "OKAY");
39829425be4SJeroen Hofstee 	chars_left = sizeof(response) - strlen(response) - 1;
39929425be4SJeroen Hofstee 
4003aab70afSSebastian Siewior 	strsep(&cmd, ":");
4013aab70afSSebastian Siewior 	if (!cmd) {
402a18c2706SSteve Rae 		error("missing variable");
4033aab70afSSebastian Siewior 		fastboot_tx_write_str("FAILmissing var");
4043aab70afSSebastian Siewior 		return;
4053aab70afSSebastian Siewior 	}
4063aab70afSSebastian Siewior 
4073aab70afSSebastian Siewior 	if (!strcmp_l1("version", cmd)) {
40829425be4SJeroen Hofstee 		strncat(response, FASTBOOT_VERSION, chars_left);
4093aab70afSSebastian Siewior 	} else if (!strcmp_l1("bootloader-version", cmd)) {
41029425be4SJeroen Hofstee 		strncat(response, U_BOOT_VERSION, chars_left);
411c674a666SEric Nelson 	} else if (!strcmp_l1("downloadsize", cmd) ||
412c674a666SEric Nelson 		!strcmp_l1("max-download-size", cmd)) {
4133aab70afSSebastian Siewior 		char str_num[12];
4143aab70afSSebastian Siewior 
415a588d99aSPaul Kocialkowski 		sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
41629425be4SJeroen Hofstee 		strncat(response, str_num, chars_left);
4173aab70afSSebastian Siewior 	} else if (!strcmp_l1("serialno", cmd)) {
41800caae6dSSimon Glass 		s = env_get("serial#");
4193aab70afSSebastian Siewior 		if (s)
42029425be4SJeroen Hofstee 			strncat(response, s, chars_left);
4213aab70afSSebastian Siewior 		else
4223aab70afSSebastian Siewior 			strcpy(response, "FAILValue not set");
423367cce4dSXu Hongfei 	} else if (strncmp("at-attest-dh", cmd, 12) == 0) {
424367cce4dSXu Hongfei 		char dh[32] = {0};
425367cce4dSXu Hongfei 
426367cce4dSXu Hongfei 		strncat(response, dh, chars_left);
427367cce4dSXu Hongfei 	} else if (strncmp("at-attest-uuid", cmd, 14) == 0) {
428367cce4dSXu Hongfei 		char uuid[32] = {0};
429367cce4dSXu Hongfei 
430367cce4dSXu Hongfei 		strncat(response, uuid, chars_left);
431367cce4dSXu Hongfei 	} else if (strncmp("at-vboot-state", cmd, 14) == 0) {
432367cce4dSXu Hongfei 		char uuid[32] = {0};
433367cce4dSXu Hongfei 
434367cce4dSXu Hongfei 		strncat(response, uuid, chars_left);
435367cce4dSXu Hongfei 	} else if (!strcmp_l1("slot-count", cmd)) {
436367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER
437367cce4dSXu Hongfei 		char slot_count[2];
438367cce4dSXu Hongfei 		char temp;
439367cce4dSXu Hongfei 
440367cce4dSXu Hongfei 		slot_count[1] = '\0';
4415b090159SJason Zhu 		avb_read_slot_count(&temp);
442367cce4dSXu Hongfei 		slot_count[0] = temp + 0x30;
443367cce4dSXu Hongfei 		strncat(response, slot_count, chars_left);
444367cce4dSXu Hongfei #else
445367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
446367cce4dSXu Hongfei 		return;
447367cce4dSXu Hongfei #endif
448367cce4dSXu Hongfei 	} else if (!strcmp_l1("current-slot", cmd)) {
449367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER
450367cce4dSXu Hongfei 		char slot_surrent[8] = {0};
451367cce4dSXu Hongfei 
4525b090159SJason Zhu 		if (!avb_get_current_slot(slot_surrent))
453367cce4dSXu Hongfei 			strncat(response, slot_surrent, chars_left);
454367cce4dSXu Hongfei 		else
455367cce4dSXu Hongfei 			strcpy(response, "FAILgeterror");
456367cce4dSXu Hongfei #else
457367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
458367cce4dSXu Hongfei 		return;
459367cce4dSXu Hongfei #endif
460367cce4dSXu Hongfei 	} else if (!strcmp_l1("slot-suffixes", cmd)) {
461367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER
462367cce4dSXu Hongfei 		char slot_suffixes_temp[4];
463367cce4dSXu Hongfei 		char slot_suffixes[9];
464367cce4dSXu Hongfei 		int slot_cnt = 0;
465367cce4dSXu Hongfei 
466367cce4dSXu Hongfei 		memset(slot_suffixes_temp, 0, 4);
467367cce4dSXu Hongfei 		memset(slot_suffixes, 0, 9);
4685b090159SJason Zhu 		avb_read_slot_suffixes(slot_suffixes_temp);
469367cce4dSXu Hongfei 		while (slot_suffixes_temp[slot_cnt] != '\0') {
470367cce4dSXu Hongfei 			slot_suffixes[slot_cnt * 2]
471367cce4dSXu Hongfei 				= slot_suffixes_temp[slot_cnt];
472367cce4dSXu Hongfei 			slot_suffixes[slot_cnt * 2 + 1] = ',';
473367cce4dSXu Hongfei 			slot_cnt++;
474367cce4dSXu Hongfei 		}
475367cce4dSXu Hongfei 		strncat(response, slot_suffixes, chars_left);
476367cce4dSXu Hongfei #else
477367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
478367cce4dSXu Hongfei 		return;
479367cce4dSXu Hongfei #endif
480367cce4dSXu Hongfei 	} else if (!strncmp("has-slot", cmd, 8)) {
481367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER
482367cce4dSXu Hongfei 		char *part_name = cmd;
483367cce4dSXu Hongfei 
484367cce4dSXu Hongfei 		cmd = strsep(&part_name, ":");
485367cce4dSXu Hongfei 		if (!strcmp(part_name, "boot") ||
486367cce4dSXu Hongfei 		    !strcmp(part_name, "system") ||
487367cce4dSXu Hongfei 		    !strcmp(part_name, "boot")) {
488367cce4dSXu Hongfei 			strncat(response, "yes", chars_left);
489367cce4dSXu Hongfei 		} else {
490367cce4dSXu Hongfei 			strcpy(response, "FAILno");
491367cce4dSXu Hongfei 		}
492367cce4dSXu Hongfei #else
493367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
494367cce4dSXu Hongfei 		return;
495367cce4dSXu Hongfei #endif
496367cce4dSXu Hongfei 	} else if (!strncmp("partition-type", cmd, 14) ||
497367cce4dSXu Hongfei 		   !strncmp("partition-size", cmd, 14)) {
498367cce4dSXu Hongfei 		disk_partition_t part_info;
499367cce4dSXu Hongfei 		struct blk_desc *dev_desc;
500367cce4dSXu Hongfei 		char *part_name = cmd;
501367cce4dSXu Hongfei 		char part_size_str[20];
502367cce4dSXu Hongfei 
503367cce4dSXu Hongfei 		cmd = strsep(&part_name, ":");
504367cce4dSXu Hongfei 		dev_desc = blk_get_dev("mmc", 0);
505367cce4dSXu Hongfei 		if (!dev_desc) {
506367cce4dSXu Hongfei 			strcpy(response, "FAILblock device not found");
507367cce4dSXu Hongfei 		} else if (part_get_info_by_name(dev_desc, part_name, &part_info) < 0) {
508367cce4dSXu Hongfei 			strcpy(response, "FAILpartition not found");
509367cce4dSXu Hongfei 		} else if (!strncmp("partition-type", cmd, 14)) {
510367cce4dSXu Hongfei 			strncat(response, (char *)part_info.type, chars_left);
511367cce4dSXu Hongfei 		} else if (!strncmp("partition-size", cmd, 14)) {
512367cce4dSXu Hongfei 			sprintf(part_size_str, "0x%016x", (int)part_info.size);
513367cce4dSXu Hongfei 			strncat(response, part_size_str, chars_left);
514367cce4dSXu Hongfei 		}
5153aab70afSSebastian Siewior 	} else {
516b1f2a17cSnicolas.le.bayon@st.com 		char *envstr;
51774322201SRob Herring 
518b1f2a17cSnicolas.le.bayon@st.com 		envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1);
519b1f2a17cSnicolas.le.bayon@st.com 		if (!envstr) {
520b1f2a17cSnicolas.le.bayon@st.com 			fastboot_tx_write_str("FAILmalloc error");
521b1f2a17cSnicolas.le.bayon@st.com 			return;
522b1f2a17cSnicolas.le.bayon@st.com 		}
523b1f2a17cSnicolas.le.bayon@st.com 
524b1f2a17cSnicolas.le.bayon@st.com 		sprintf(envstr, "fastboot.%s", cmd);
52500caae6dSSimon Glass 		s = env_get(envstr);
52674322201SRob Herring 		if (s) {
52774322201SRob Herring 			strncat(response, s, chars_left);
52874322201SRob Herring 		} else {
529a18c2706SSteve Rae 			printf("WARNING: unknown variable: %s\n", cmd);
5303aab70afSSebastian Siewior 			strcpy(response, "FAILVariable not implemented");
5313aab70afSSebastian Siewior 		}
532b1f2a17cSnicolas.le.bayon@st.com 
533b1f2a17cSnicolas.le.bayon@st.com 		free(envstr);
53474322201SRob Herring 	}
5353aab70afSSebastian Siewior 	fastboot_tx_write_str(response);
5363aab70afSSebastian Siewior }
5373aab70afSSebastian Siewior 
538ac484c5aSRoger Quadros static unsigned int rx_bytes_expected(struct usb_ep *ep)
5393aab70afSSebastian Siewior {
5403aab70afSSebastian Siewior 	int rx_remain = download_size - download_bytes;
541ac484c5aSRoger Quadros 	unsigned int rem;
542ac484c5aSRoger Quadros 	unsigned int maxpacket = ep->maxpacket;
543ac484c5aSRoger Quadros 
544ac484c5aSRoger Quadros 	if (rx_remain <= 0)
5453aab70afSSebastian Siewior 		return 0;
546ac484c5aSRoger Quadros 	else if (rx_remain > EP_BUFFER_SIZE)
5473aab70afSSebastian Siewior 		return EP_BUFFER_SIZE;
548ac484c5aSRoger Quadros 
549ac484c5aSRoger Quadros 	/*
550ac484c5aSRoger Quadros 	 * Some controllers e.g. DWC3 don't like OUT transfers to be
551ac484c5aSRoger Quadros 	 * not ending in maxpacket boundary. So just make them happy by
552ac484c5aSRoger Quadros 	 * always requesting for integral multiple of maxpackets.
553ac484c5aSRoger Quadros 	 * This shouldn't bother controllers that don't care about it.
554ac484c5aSRoger Quadros 	 */
5559e4b510dSDileep Katta 	rem = rx_remain % maxpacket;
556ac484c5aSRoger Quadros 	if (rem > 0)
5579e4b510dSDileep Katta 		rx_remain = rx_remain + (maxpacket - rem);
558ac484c5aSRoger Quadros 
5593aab70afSSebastian Siewior 	return rx_remain;
5603aab70afSSebastian Siewior }
5613aab70afSSebastian Siewior 
5623aab70afSSebastian Siewior #define BYTES_PER_DOT	0x20000
5633aab70afSSebastian Siewior static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
5643aab70afSSebastian Siewior {
5653c8f98f5SMaxime Ripard 	char response[FASTBOOT_RESPONSE_LEN];
5663aab70afSSebastian Siewior 	unsigned int transfer_size = download_size - download_bytes;
5673aab70afSSebastian Siewior 	const unsigned char *buffer = req->buf;
5683aab70afSSebastian Siewior 	unsigned int buffer_size = req->actual;
56923d1d10cSBo Shen 	unsigned int pre_dot_num, now_dot_num;
5703aab70afSSebastian Siewior 
5713aab70afSSebastian Siewior 	if (req->status != 0) {
5723aab70afSSebastian Siewior 		printf("Bad status: %d\n", req->status);
5733aab70afSSebastian Siewior 		return;
5743aab70afSSebastian Siewior 	}
5753aab70afSSebastian Siewior 
5763aab70afSSebastian Siewior 	if (buffer_size < transfer_size)
5773aab70afSSebastian Siewior 		transfer_size = buffer_size;
5783aab70afSSebastian Siewior 
579a588d99aSPaul Kocialkowski 	memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
5803aab70afSSebastian Siewior 	       buffer, transfer_size);
5813aab70afSSebastian Siewior 
58223d1d10cSBo Shen 	pre_dot_num = download_bytes / BYTES_PER_DOT;
5833aab70afSSebastian Siewior 	download_bytes += transfer_size;
58423d1d10cSBo Shen 	now_dot_num = download_bytes / BYTES_PER_DOT;
58523d1d10cSBo Shen 
58623d1d10cSBo Shen 	if (pre_dot_num != now_dot_num) {
58723d1d10cSBo Shen 		putc('.');
58823d1d10cSBo Shen 		if (!(now_dot_num % 74))
58923d1d10cSBo Shen 			putc('\n');
59023d1d10cSBo Shen 	}
5913aab70afSSebastian Siewior 
5923aab70afSSebastian Siewior 	/* Check if transfer is done */
5933aab70afSSebastian Siewior 	if (download_bytes >= download_size) {
5943aab70afSSebastian Siewior 		/*
5953aab70afSSebastian Siewior 		 * Reset global transfer variable, keep download_bytes because
5963aab70afSSebastian Siewior 		 * it will be used in the next possible flashing command
5973aab70afSSebastian Siewior 		 */
5983aab70afSSebastian Siewior 		download_size = 0;
5993aab70afSSebastian Siewior 		req->complete = rx_handler_command;
6003aab70afSSebastian Siewior 		req->length = EP_BUFFER_SIZE;
6013aab70afSSebastian Siewior 
602192bc694SBen Whitten 		strcpy(response, "OKAY");
6033aab70afSSebastian Siewior 		fastboot_tx_write_str(response);
6043aab70afSSebastian Siewior 
6053aab70afSSebastian Siewior 		printf("\ndownloading of %d bytes finished\n", download_bytes);
6063aab70afSSebastian Siewior 	} else {
607ac484c5aSRoger Quadros 		req->length = rx_bytes_expected(ep);
6083aab70afSSebastian Siewior 	}
6093aab70afSSebastian Siewior 
6103aab70afSSebastian Siewior 	req->actual = 0;
6113aab70afSSebastian Siewior 	usb_ep_queue(ep, req, 0);
6123aab70afSSebastian Siewior }
6133aab70afSSebastian Siewior 
6143aab70afSSebastian Siewior static void cb_download(struct usb_ep *ep, struct usb_request *req)
6153aab70afSSebastian Siewior {
6163aab70afSSebastian Siewior 	char *cmd = req->buf;
6173c8f98f5SMaxime Ripard 	char response[FASTBOOT_RESPONSE_LEN];
6183aab70afSSebastian Siewior 
6193aab70afSSebastian Siewior 	strsep(&cmd, ":");
6203aab70afSSebastian Siewior 	download_size = simple_strtoul(cmd, NULL, 16);
6213aab70afSSebastian Siewior 	download_bytes = 0;
6223aab70afSSebastian Siewior 
6233aab70afSSebastian Siewior 	printf("Starting download of %d bytes\n", download_size);
6243aab70afSSebastian Siewior 
6253aab70afSSebastian Siewior 	if (0 == download_size) {
626192bc694SBen Whitten 		strcpy(response, "FAILdata invalid size");
627a588d99aSPaul Kocialkowski 	} else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
6283aab70afSSebastian Siewior 		download_size = 0;
629192bc694SBen Whitten 		strcpy(response, "FAILdata too large");
6303aab70afSSebastian Siewior 	} else {
6313aab70afSSebastian Siewior 		sprintf(response, "DATA%08x", download_size);
6323aab70afSSebastian Siewior 		req->complete = rx_handler_dl_image;
633ac484c5aSRoger Quadros 		req->length = rx_bytes_expected(ep);
6343aab70afSSebastian Siewior 	}
635367cce4dSXu Hongfei 
636367cce4dSXu Hongfei 	fastboot_tx_write_str(response);
637367cce4dSXu Hongfei }
638367cce4dSXu Hongfei 
639367cce4dSXu Hongfei static void tx_handler_ul(struct usb_ep *ep, struct usb_request *req)
640367cce4dSXu Hongfei {
641367cce4dSXu Hongfei 	unsigned int xfer_size = 0;
642367cce4dSXu Hongfei 	unsigned int pre_dot_num, now_dot_num;
643367cce4dSXu Hongfei 	unsigned int remain_size = 0;
644367cce4dSXu Hongfei 	unsigned int transferred_size = req->actual;
645367cce4dSXu Hongfei 
646367cce4dSXu Hongfei 	if (req->status != 0) {
647367cce4dSXu Hongfei 		printf("Bad status: %d\n", req->status);
648367cce4dSXu Hongfei 		return;
649367cce4dSXu Hongfei 	}
650367cce4dSXu Hongfei 
651367cce4dSXu Hongfei 	if (start_upload) {
652367cce4dSXu Hongfei 		pre_dot_num = upload_bytes / BYTES_PER_DOT;
653367cce4dSXu Hongfei 		upload_bytes += transferred_size;
654367cce4dSXu Hongfei 		now_dot_num = upload_bytes / BYTES_PER_DOT;
655367cce4dSXu Hongfei 
656367cce4dSXu Hongfei 		if (pre_dot_num != now_dot_num) {
657367cce4dSXu Hongfei 			putc('.');
658367cce4dSXu Hongfei 			if (!(now_dot_num % 74))
659367cce4dSXu Hongfei 				putc('\n');
660367cce4dSXu Hongfei 		}
661367cce4dSXu Hongfei 	}
662367cce4dSXu Hongfei 
663367cce4dSXu Hongfei 	remain_size = upload_size - upload_bytes;
664367cce4dSXu Hongfei 	xfer_size = (remain_size > EP_BUFFER_SIZE) ?
665367cce4dSXu Hongfei 		    EP_BUFFER_SIZE : remain_size;
666367cce4dSXu Hongfei 
667367cce4dSXu Hongfei 	debug("%s: remain_size=%d, transferred_size=%d",
668367cce4dSXu Hongfei 	      __func__, remain_size, transferred_size);
669367cce4dSXu Hongfei 	debug("xfer_size=%d, upload_bytes=%d, upload_size=%d!\n",
670367cce4dSXu Hongfei 	      xfer_size, upload_bytes, upload_size);
671367cce4dSXu Hongfei 
672367cce4dSXu Hongfei 	if (remain_size <= 0) {
673367cce4dSXu Hongfei 		fastboot_func->in_req->complete = fastboot_complete;
674367cce4dSXu Hongfei 		fastboot_tx_write_str("OKAY");
675367cce4dSXu Hongfei 		printf("\nuploading of %d bytes finished\n", upload_bytes);
676367cce4dSXu Hongfei 		upload_bytes = 0;
677367cce4dSXu Hongfei 		upload_size = 0;
678367cce4dSXu Hongfei 		start_upload = false;
679367cce4dSXu Hongfei 		return;
680367cce4dSXu Hongfei 	}
681367cce4dSXu Hongfei 
682367cce4dSXu Hongfei 	/* Remove the transfer callback which response the upload */
683367cce4dSXu Hongfei 	/* request from host */
684367cce4dSXu Hongfei 	if (!upload_bytes)
685367cce4dSXu Hongfei 		start_upload = true;
686367cce4dSXu Hongfei 
687367cce4dSXu Hongfei 	fastboot_tx_write((char *)(CONFIG_FASTBOOT_BUF_ADDR + upload_bytes),
688367cce4dSXu Hongfei 			  xfer_size);
689367cce4dSXu Hongfei }
690367cce4dSXu Hongfei 
691367cce4dSXu Hongfei static void cb_upload(struct usb_ep *ep, struct usb_request *req)
692367cce4dSXu Hongfei {
693367cce4dSXu Hongfei 	char response[FASTBOOT_RESPONSE_LEN];
694367cce4dSXu Hongfei 
695367cce4dSXu Hongfei 	upload_size = download_bytes;
696367cce4dSXu Hongfei 
697367cce4dSXu Hongfei 	printf("Starting upload of %d bytes\n", upload_size);
698367cce4dSXu Hongfei 
699367cce4dSXu Hongfei 	if (0 == upload_size) {
700367cce4dSXu Hongfei 		strcpy(response, "FAILdata invalid size");
701367cce4dSXu Hongfei 	} else {
702367cce4dSXu Hongfei 		start_upload = false;
703367cce4dSXu Hongfei 		sprintf(response, "DATA%08x", upload_size);
704367cce4dSXu Hongfei 		fastboot_func->in_req->complete = tx_handler_ul;
705367cce4dSXu Hongfei 	}
706367cce4dSXu Hongfei 
7073aab70afSSebastian Siewior 	fastboot_tx_write_str(response);
7083aab70afSSebastian Siewior }
7093aab70afSSebastian Siewior 
7103aab70afSSebastian Siewior static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
7113aab70afSSebastian Siewior {
7123aab70afSSebastian Siewior 	char boot_addr_start[12];
7133aab70afSSebastian Siewior 	char *bootm_args[] = { "bootm", boot_addr_start, NULL };
7143aab70afSSebastian Siewior 
7153aab70afSSebastian Siewior 	puts("Booting kernel..\n");
7163aab70afSSebastian Siewior 
71790ae53ceSTom Rini 	sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR);
7183aab70afSSebastian Siewior 	do_bootm(NULL, 0, 2, bootm_args);
7193aab70afSSebastian Siewior 
7203aab70afSSebastian Siewior 	/* This only happens if image is somehow faulty so we start over */
7213aab70afSSebastian Siewior 	do_reset(NULL, 0, 0, NULL);
7223aab70afSSebastian Siewior }
7233aab70afSSebastian Siewior 
7243aab70afSSebastian Siewior static void cb_boot(struct usb_ep *ep, struct usb_request *req)
7253aab70afSSebastian Siewior {
7263aab70afSSebastian Siewior 	fastboot_func->in_req->complete = do_bootm_on_complete;
7273aab70afSSebastian Siewior 	fastboot_tx_write_str("OKAY");
7283aab70afSSebastian Siewior }
7293aab70afSSebastian Siewior 
730267abc62SRob Herring static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
731267abc62SRob Herring {
732267abc62SRob Herring 	g_dnl_trigger_detach();
733267abc62SRob Herring }
734267abc62SRob Herring 
735267abc62SRob Herring static void cb_continue(struct usb_ep *ep, struct usb_request *req)
736267abc62SRob Herring {
737267abc62SRob Herring 	fastboot_func->in_req->complete = do_exit_on_complete;
738267abc62SRob Herring 	fastboot_tx_write_str("OKAY");
739267abc62SRob Herring }
740267abc62SRob Herring 
741367cce4dSXu Hongfei static void cb_set_active(struct usb_ep *ep, struct usb_request *req)
742367cce4dSXu Hongfei {
743367cce4dSXu Hongfei 	char *cmd = req->buf;
744367cce4dSXu Hongfei 
745367cce4dSXu Hongfei 	debug("%s: %s\n", __func__, cmd);
746367cce4dSXu Hongfei 
747367cce4dSXu Hongfei 	strsep(&cmd, ":");
748367cce4dSXu Hongfei 	if (!cmd) {
749367cce4dSXu Hongfei 		error("missing slot name");
750367cce4dSXu Hongfei 		fastboot_tx_write_str("FAIL: missing slot name");
751367cce4dSXu Hongfei 		return;
752367cce4dSXu Hongfei 	}
753367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER
754367cce4dSXu Hongfei 	unsigned int slot_number;
755367cce4dSXu Hongfei 	if (strncmp("a", cmd, 1) == 0) {
756367cce4dSXu Hongfei 		slot_number = 0;
7575b090159SJason Zhu 		avb_set_slot_active(&slot_number);
758367cce4dSXu Hongfei 	} else if (strncmp("b", cmd, 1) == 0) {
759367cce4dSXu Hongfei 		slot_number = 1;
7605b090159SJason Zhu 		avb_set_slot_active(&slot_number);
761367cce4dSXu Hongfei 	} else {
762367cce4dSXu Hongfei 		fastboot_tx_write_str("FAIL: unkown slot name");
763367cce4dSXu Hongfei 		return;
764367cce4dSXu Hongfei 	}
765367cce4dSXu Hongfei 
766367cce4dSXu Hongfei 	fastboot_tx_write_str("OKAY");
767367cce4dSXu Hongfei 	return;
768367cce4dSXu Hongfei #else
769367cce4dSXu Hongfei 	fastboot_tx_write_str("FAILnot implemented");
770367cce4dSXu Hongfei 	return;
771367cce4dSXu Hongfei #endif
772367cce4dSXu Hongfei }
773367cce4dSXu Hongfei 
774d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH
775d1b5ed07SSteve Rae static void cb_flash(struct usb_ep *ep, struct usb_request *req)
776d1b5ed07SSteve Rae {
777d1b5ed07SSteve Rae 	char *cmd = req->buf;
7783c8f98f5SMaxime Ripard 	char response[FASTBOOT_RESPONSE_LEN];
7797bc1707dSJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER
7807bc1707dSJason Zhu 	uint8_t flash_lock_state;
781d1b5ed07SSteve Rae 
7825b090159SJason Zhu 	if (avb_read_flash_lock_state(&flash_lock_state))
7837bc1707dSJason Zhu 		fastboot_tx_write_str("FAIL");
7847bc1707dSJason Zhu 		return;
7857bc1707dSJason Zhu 	if (flash_lock_state == 0) {
7867bc1707dSJason Zhu 		fastboot_tx_write_str("FAILThe device is locked, can not flash!");
7877bc1707dSJason Zhu 		printf("The device is locked, can not flash!\n");
7887bc1707dSJason Zhu 		return;
7897bc1707dSJason Zhu 	}
7907bc1707dSJason Zhu #endif
791d1b5ed07SSteve Rae 	strsep(&cmd, ":");
792d1b5ed07SSteve Rae 	if (!cmd) {
793a18c2706SSteve Rae 		error("missing partition name");
794d1b5ed07SSteve Rae 		fastboot_tx_write_str("FAILmissing partition name");
795d1b5ed07SSteve Rae 		return;
796d1b5ed07SSteve Rae 	}
797d1b5ed07SSteve Rae 
7988b464fa9SJocelyn Bohr 	fastboot_fail("no flash device defined", response);
799d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
80064ece848SSteve Rae 	fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
8018b464fa9SJocelyn Bohr 				download_bytes, response);
802d1b5ed07SSteve Rae #endif
803bf8940d3SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
8048b464fa9SJocelyn Bohr 	fb_nand_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
8058b464fa9SJocelyn Bohr 				download_bytes, response);
806bf8940d3SMaxime Ripard #endif
807d1b5ed07SSteve Rae 	fastboot_tx_write_str(response);
808d1b5ed07SSteve Rae }
809d1b5ed07SSteve Rae #endif
810d1b5ed07SSteve Rae 
811367cce4dSXu Hongfei static void cb_flashing(struct usb_ep *ep, struct usb_request *req)
812367cce4dSXu Hongfei {
813367cce4dSXu Hongfei 	char *cmd = req->buf;
814367cce4dSXu Hongfei 
815367cce4dSXu Hongfei 	if (strncmp("lock", cmd + 9, 4) == 0) {
8167bc1707dSJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER
8177bc1707dSJason Zhu 		uint8_t flash_lock_state;
8187bc1707dSJason Zhu 		flash_lock_state = 0;
8195b090159SJason Zhu 		if (avb_write_flash_lock_state(flash_lock_state))
8207bc1707dSJason Zhu 			fastboot_tx_write_str("FAIL");
8217bc1707dSJason Zhu 		else
8227bc1707dSJason Zhu 			fastboot_tx_write_str("OKAY");
8237bc1707dSJason Zhu #else
824367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
8257bc1707dSJason Zhu #endif
826367cce4dSXu Hongfei 	} else if (strncmp("unlock", cmd + 9, 6) == 0) {
8277bc1707dSJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER
8287bc1707dSJason Zhu 		uint8_t flash_lock_state;
8297bc1707dSJason Zhu 		flash_lock_state = 1;
8305b090159SJason Zhu 		if (avb_write_flash_lock_state(flash_lock_state))
8317bc1707dSJason Zhu 			fastboot_tx_write_str("FAIL");
8327bc1707dSJason Zhu 		else
8337bc1707dSJason Zhu 			fastboot_tx_write_str("OKAY");
8347bc1707dSJason Zhu #else
835367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
8367bc1707dSJason Zhu #endif
837367cce4dSXu Hongfei 	} else if (strncmp("lock_critical", cmd + 9, 12) == 0) {
838367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
839367cce4dSXu Hongfei 	} else if (strncmp("unlock_critical", cmd + 9, 14) == 0) {
840367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
841367cce4dSXu Hongfei 	} else if (strncmp("get_unlock_ability", cmd + 9, 17) == 0) {
842367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
843367cce4dSXu Hongfei 	} else if (strncmp("get_unlock_bootloader_nonce", cmd + 4, 27) == 0) {
844367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
845367cce4dSXu Hongfei 	} else if (strncmp("unlock_bootloader", cmd + 9, 17) == 0) {
846367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
847367cce4dSXu Hongfei 	} else if (strncmp("lock_bootloader", cmd + 9, 15) == 0) {
848367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
849367cce4dSXu Hongfei 	} else {
850367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILunknown flashing command");
851367cce4dSXu Hongfei 	}
852367cce4dSXu Hongfei }
853367cce4dSXu Hongfei 
854de195620SMichael Scott static void cb_oem(struct usb_ep *ep, struct usb_request *req)
855de195620SMichael Scott {
856de195620SMichael Scott 	char *cmd = req->buf;
857367cce4dSXu Hongfei 
8584adef270SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
859372d7decSRob Herring 	if (strncmp("format", cmd + 4, 6) == 0) {
860372d7decSRob Herring 		char cmdbuf[32];
861372d7decSRob Herring                 sprintf(cmdbuf, "gpt write mmc %x $partitions",
862372d7decSRob Herring 			CONFIG_FASTBOOT_FLASH_MMC_DEV);
863372d7decSRob Herring                 if (run_command(cmdbuf, 0))
864372d7decSRob Herring 			fastboot_tx_write_str("FAIL");
865372d7decSRob Herring                 else
866372d7decSRob Herring 			fastboot_tx_write_str("OKAY");
867372d7decSRob Herring 	} else
868372d7decSRob Herring #endif
869de195620SMichael Scott 	if (strncmp("unlock", cmd + 4, 8) == 0) {
870de195620SMichael Scott 		fastboot_tx_write_str("FAILnot implemented");
871367cce4dSXu Hongfei 	} else if (strncmp("at-get-ca-request", cmd + 4, 17) == 0) {
872367cce4dSXu Hongfei 		fastboot_tx_write_str("OKAY");
873367cce4dSXu Hongfei 	} else if (strncmp("at-set-ca-response", cmd + 4, 18) == 0) {
874367cce4dSXu Hongfei 		fastboot_tx_write_str("OKAY");
875367cce4dSXu Hongfei 	} else if (strncmp("at-lock-vboot", cmd + 4, 13) == 0) {
876d8bd6e97SJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER
877d8bd6e97SJason Zhu 		uint8_t lock_state;
878d8bd6e97SJason Zhu 		lock_state = 0;
8795b090159SJason Zhu 		if (avb_write_lock_state(lock_state))
880d8bd6e97SJason Zhu 			fastboot_tx_write_str("FAIL");
881d8bd6e97SJason Zhu 		else
882d8bd6e97SJason Zhu 			fastboot_tx_write_str("OKAY");
883d8bd6e97SJason Zhu #else
884367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
885d8bd6e97SJason Zhu #endif
886367cce4dSXu Hongfei 	} else if (strncmp("at-unlock-vboot", cmd + 4, 15) == 0) {
887d8bd6e97SJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER
888d8bd6e97SJason Zhu 		uint8_t lock_state;
8895b090159SJason Zhu 		if (avb_read_lock_state(&lock_state))
890d8bd6e97SJason Zhu 			fastboot_tx_write_str("FAIL");
891d8bd6e97SJason Zhu 		if (lock_state >> 1 == 1) {
892d8bd6e97SJason Zhu 			fastboot_tx_write_str("FAILThe vboot is disable!");
893d8bd6e97SJason Zhu 		} else {
894d8bd6e97SJason Zhu 			lock_state = 1;
8955b090159SJason Zhu 			if (avb_write_lock_state(lock_state))
896d8bd6e97SJason Zhu 				fastboot_tx_write_str("FAIL");
897d8bd6e97SJason Zhu 			else
898d8bd6e97SJason Zhu 				fastboot_tx_write_str("OKAY");
899d8bd6e97SJason Zhu 		}
900d8bd6e97SJason Zhu #else
901367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
902d8bd6e97SJason Zhu #endif
903367cce4dSXu Hongfei 	} else if (strncmp("at-disable-unlock-vboot", cmd + 4, 23) == 0) {
904d8bd6e97SJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER
905d8bd6e97SJason Zhu 		uint8_t lock_state;
906d8bd6e97SJason Zhu 		lock_state = 2;
9075b090159SJason Zhu 		if (avb_write_lock_state(lock_state))
908d8bd6e97SJason Zhu 			fastboot_tx_write_str("FAIL");
909d8bd6e97SJason Zhu 		else
910d8bd6e97SJason Zhu 			fastboot_tx_write_str("OKAY");
911d8bd6e97SJason Zhu #else
912367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
913d8bd6e97SJason Zhu #endif
914367cce4dSXu Hongfei 	} else if (strncmp("fuse at-perm-attr", cmd + 4, 16) == 0) {
9154f3cd37cSJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER
9165b090159SJason Zhu 		if (avb_write_permanent_attributes((uint8_t *)
9174f3cd37cSJason Zhu 						   CONFIG_FASTBOOT_BUF_ADDR,
9184f3cd37cSJason Zhu 						   download_bytes))
9194f3cd37cSJason Zhu 			fastboot_tx_write_str("FAIL");
9204f3cd37cSJason Zhu 		else
9214f3cd37cSJason Zhu 			fastboot_tx_write_str("OKAY");
9224f3cd37cSJason Zhu #else
923367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
9244f3cd37cSJason Zhu #endif
925*4e1bbe84SJason Zhu 	} else if (strncmp("fuse at-bootloader-vboot-key", cmd + 4, 27) == 0) {
926*4e1bbe84SJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER
927*4e1bbe84SJason Zhu 		if (download_bytes != VBOOT_KEY_HASH_SIZE) {
928*4e1bbe84SJason Zhu 			fastboot_tx_write_str("FAIL");
929*4e1bbe84SJason Zhu 			printf("The vboot key size error!\n");
930*4e1bbe84SJason Zhu 		}
931*4e1bbe84SJason Zhu 
932*4e1bbe84SJason Zhu 		if (avb_write_vbootkey_hash((uint8_t *)
933*4e1bbe84SJason Zhu 					    CONFIG_FASTBOOT_BUF_ADDR,
934*4e1bbe84SJason Zhu 					    VBOOT_KEY_HASH_SIZE)) {
935*4e1bbe84SJason Zhu 			fastboot_tx_write_str("FAIL");
936*4e1bbe84SJason Zhu 			return;
937*4e1bbe84SJason Zhu 		}
938*4e1bbe84SJason Zhu 		fastboot_tx_write_str("OKAY");
939*4e1bbe84SJason Zhu #else
940*4e1bbe84SJason Zhu 		fastboot_tx_write_str("FAILnot implemented");
941*4e1bbe84SJason Zhu #endif
942367cce4dSXu Hongfei 	} else {
943de195620SMichael Scott 		fastboot_tx_write_str("FAILunknown oem command");
944de195620SMichael Scott 	}
945de195620SMichael Scott }
946de195620SMichael Scott 
94789792381SDileep Katta #ifdef CONFIG_FASTBOOT_FLASH
94889792381SDileep Katta static void cb_erase(struct usb_ep *ep, struct usb_request *req)
94989792381SDileep Katta {
95089792381SDileep Katta 	char *cmd = req->buf;
9513c8f98f5SMaxime Ripard 	char response[FASTBOOT_RESPONSE_LEN];
95289792381SDileep Katta 
95389792381SDileep Katta 	strsep(&cmd, ":");
95489792381SDileep Katta 	if (!cmd) {
95589792381SDileep Katta 		error("missing partition name");
95689792381SDileep Katta 		fastboot_tx_write_str("FAILmissing partition name");
95789792381SDileep Katta 		return;
95889792381SDileep Katta 	}
95989792381SDileep Katta 
9608b464fa9SJocelyn Bohr 	fastboot_fail("no flash device defined", response);
96189792381SDileep Katta #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
9628b464fa9SJocelyn Bohr 	fb_mmc_erase(cmd, response);
96389792381SDileep Katta #endif
964bf8940d3SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
9658b464fa9SJocelyn Bohr 	fb_nand_erase(cmd, response);
966bf8940d3SMaxime Ripard #endif
96789792381SDileep Katta 	fastboot_tx_write_str(response);
96889792381SDileep Katta }
96989792381SDileep Katta #endif
97089792381SDileep Katta 
9713aab70afSSebastian Siewior struct cmd_dispatch_info {
9723aab70afSSebastian Siewior 	char *cmd;
9733aab70afSSebastian Siewior 	void (*cb)(struct usb_ep *ep, struct usb_request *req);
9743aab70afSSebastian Siewior };
9753aab70afSSebastian Siewior 
9763aab70afSSebastian Siewior static const struct cmd_dispatch_info cmd_dispatch_info[] = {
9773aab70afSSebastian Siewior 	{
9783aab70afSSebastian Siewior 		.cmd = "reboot",
9793aab70afSSebastian Siewior 		.cb = cb_reboot,
9803aab70afSSebastian Siewior 	}, {
9813aab70afSSebastian Siewior 		.cmd = "getvar:",
9823aab70afSSebastian Siewior 		.cb = cb_getvar,
9833aab70afSSebastian Siewior 	}, {
9843aab70afSSebastian Siewior 		.cmd = "download:",
9853aab70afSSebastian Siewior 		.cb = cb_download,
9863aab70afSSebastian Siewior 	}, {
987367cce4dSXu Hongfei 		.cmd = "upload",
988367cce4dSXu Hongfei 		.cb = cb_upload,
989367cce4dSXu Hongfei 	}, {
9903aab70afSSebastian Siewior 		.cmd = "boot",
9913aab70afSSebastian Siewior 		.cb = cb_boot,
992267abc62SRob Herring 	}, {
993267abc62SRob Herring 		.cmd = "continue",
994267abc62SRob Herring 		.cb = cb_continue,
995367cce4dSXu Hongfei 	}, {
996367cce4dSXu Hongfei 		.cmd = "set_active",
997367cce4dSXu Hongfei 		.cb = cb_set_active,
9983aab70afSSebastian Siewior 	},
999d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH
1000d1b5ed07SSteve Rae 	{
1001367cce4dSXu Hongfei 		.cmd = "flashing",
1002367cce4dSXu Hongfei 		.cb = cb_flashing,
1003367cce4dSXu Hongfei 	},
1004367cce4dSXu Hongfei 	{
1005d1b5ed07SSteve Rae 		.cmd = "flash",
1006d1b5ed07SSteve Rae 		.cb = cb_flash,
100789792381SDileep Katta 	}, {
100889792381SDileep Katta 		.cmd = "erase",
100989792381SDileep Katta 		.cb = cb_erase,
1010d1b5ed07SSteve Rae 	},
1011d1b5ed07SSteve Rae #endif
1012de195620SMichael Scott 	{
1013de195620SMichael Scott 		.cmd = "oem",
1014de195620SMichael Scott 		.cb = cb_oem,
1015de195620SMichael Scott 	},
10163aab70afSSebastian Siewior };
10173aab70afSSebastian Siewior 
10183aab70afSSebastian Siewior static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
10193aab70afSSebastian Siewior {
10203aab70afSSebastian Siewior 	char *cmdbuf = req->buf;
10213aab70afSSebastian Siewior 	void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
10223aab70afSSebastian Siewior 	int i;
10233aab70afSSebastian Siewior 
102494b385faSPaul Kocialkowski 	if (req->status != 0 || req->length == 0)
102594b385faSPaul Kocialkowski 		return;
102694b385faSPaul Kocialkowski 
10273aab70afSSebastian Siewior 	for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
10283aab70afSSebastian Siewior 		if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
10293aab70afSSebastian Siewior 			func_cb = cmd_dispatch_info[i].cb;
10303aab70afSSebastian Siewior 			break;
10313aab70afSSebastian Siewior 		}
10323aab70afSSebastian Siewior 	}
10333aab70afSSebastian Siewior 
1034593cbd93SSteve Rae 	if (!func_cb) {
10357715dea4SJohn Keeping 		error("unknown command: %.*s", req->actual, cmdbuf);
10363aab70afSSebastian Siewior 		fastboot_tx_write_str("FAILunknown command");
1037593cbd93SSteve Rae 	} else {
1038e2140588SEric Nelson 		if (req->actual < req->length) {
1039e2140588SEric Nelson 			u8 *buf = (u8 *)req->buf;
1040e2140588SEric Nelson 			buf[req->actual] = 0;
10413aab70afSSebastian Siewior 			func_cb(ep, req);
1042e2140588SEric Nelson 		} else {
1043a18c2706SSteve Rae 			error("buffer overflow");
1044e2140588SEric Nelson 			fastboot_tx_write_str("FAILbuffer overflow");
1045e2140588SEric Nelson 		}
1046593cbd93SSteve Rae 	}
10473aab70afSSebastian Siewior 
10483aab70afSSebastian Siewior 	*cmdbuf = '\0';
10493aab70afSSebastian Siewior 	req->actual = 0;
10503aab70afSSebastian Siewior 	usb_ep_queue(ep, req, 0);
10513aab70afSSebastian Siewior }
1052