xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_fastboot.c (revision 174b45448e334397fdadf104f3fe82d5e5158e4e)
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>
2537a7bc39SJason Zhu #include <android_avb/rk_avb_ops_user.h>
26d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
27d1b5ed07SSteve Rae #include <fb_mmc.h>
28d1b5ed07SSteve Rae #endif
29bf8940d3SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
30bf8940d3SMaxime Ripard #include <fb_nand.h>
31bf8940d3SMaxime Ripard #endif
324d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT
334d0fc665SAndy Ye #include <optee_include/OpteeClientInterface.h>
344d0fc665SAndy Ye #endif
353aab70afSSebastian Siewior 
363aab70afSSebastian Siewior #define FASTBOOT_VERSION		"0.4"
373aab70afSSebastian Siewior 
383aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_CLASS	0xff
393aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_SUB_CLASS	0x42
403aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_PROTOCOL	0x03
413aab70afSSebastian Siewior 
423aab70afSSebastian Siewior #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0  (0x0200)
433aab70afSSebastian Siewior #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1  (0x0040)
443aab70afSSebastian Siewior #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE      (0x0040)
453aab70afSSebastian Siewior 
463aab70afSSebastian Siewior #define EP_BUFFER_SIZE			4096
47ac484c5aSRoger Quadros /*
48ac484c5aSRoger Quadros  * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
49ac484c5aSRoger Quadros  * (64 or 512 or 1024), else we break on certain controllers like DWC3
50ac484c5aSRoger Quadros  * that expect bulk OUT requests to be divisible by maxpacket size.
51ac484c5aSRoger Quadros  */
523aab70afSSebastian Siewior 
533aab70afSSebastian Siewior struct f_fastboot {
543aab70afSSebastian Siewior 	struct usb_function usb_function;
553aab70afSSebastian Siewior 
56593cbd93SSteve Rae 	/* IN/OUT EP's and corresponding requests */
573aab70afSSebastian Siewior 	struct usb_ep *in_ep, *out_ep;
583aab70afSSebastian Siewior 	struct usb_request *in_req, *out_req;
593aab70afSSebastian Siewior };
603aab70afSSebastian Siewior 
613aab70afSSebastian Siewior static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
623aab70afSSebastian Siewior {
633aab70afSSebastian Siewior 	return container_of(f, struct f_fastboot, usb_function);
643aab70afSSebastian Siewior }
653aab70afSSebastian Siewior 
663aab70afSSebastian Siewior static struct f_fastboot *fastboot_func;
673aab70afSSebastian Siewior static unsigned int download_size;
683aab70afSSebastian Siewior static unsigned int download_bytes;
69367cce4dSXu Hongfei static unsigned int upload_size;
70367cce4dSXu Hongfei static unsigned int upload_bytes;
71367cce4dSXu Hongfei static bool start_upload;
723aab70afSSebastian Siewior 
733aab70afSSebastian Siewior static struct usb_endpoint_descriptor fs_ep_in = {
743aab70afSSebastian Siewior 	.bLength            = USB_DT_ENDPOINT_SIZE,
753aab70afSSebastian Siewior 	.bDescriptorType    = USB_DT_ENDPOINT,
763aab70afSSebastian Siewior 	.bEndpointAddress   = USB_DIR_IN,
773aab70afSSebastian Siewior 	.bmAttributes       = USB_ENDPOINT_XFER_BULK,
78718156adSRoger Quadros 	.wMaxPacketSize     = cpu_to_le16(64),
793aab70afSSebastian Siewior };
803aab70afSSebastian Siewior 
813aab70afSSebastian Siewior static struct usb_endpoint_descriptor fs_ep_out = {
823aab70afSSebastian Siewior 	.bLength		= USB_DT_ENDPOINT_SIZE,
833aab70afSSebastian Siewior 	.bDescriptorType	= USB_DT_ENDPOINT,
843aab70afSSebastian Siewior 	.bEndpointAddress	= USB_DIR_OUT,
853aab70afSSebastian Siewior 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
86718156adSRoger Quadros 	.wMaxPacketSize		= cpu_to_le16(64),
87718156adSRoger Quadros };
88718156adSRoger Quadros 
89718156adSRoger Quadros static struct usb_endpoint_descriptor hs_ep_in = {
90718156adSRoger Quadros 	.bLength		= USB_DT_ENDPOINT_SIZE,
91718156adSRoger Quadros 	.bDescriptorType	= USB_DT_ENDPOINT,
92718156adSRoger Quadros 	.bEndpointAddress	= USB_DIR_IN,
93718156adSRoger Quadros 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
94718156adSRoger Quadros 	.wMaxPacketSize		= cpu_to_le16(512),
953aab70afSSebastian Siewior };
963aab70afSSebastian Siewior 
973aab70afSSebastian Siewior static struct usb_endpoint_descriptor hs_ep_out = {
983aab70afSSebastian Siewior 	.bLength		= USB_DT_ENDPOINT_SIZE,
993aab70afSSebastian Siewior 	.bDescriptorType	= USB_DT_ENDPOINT,
1003aab70afSSebastian Siewior 	.bEndpointAddress	= USB_DIR_OUT,
1013aab70afSSebastian Siewior 	.bmAttributes		= USB_ENDPOINT_XFER_BULK,
102718156adSRoger Quadros 	.wMaxPacketSize		= cpu_to_le16(512),
1033aab70afSSebastian Siewior };
1043aab70afSSebastian Siewior 
1053aab70afSSebastian Siewior static struct usb_interface_descriptor interface_desc = {
1063aab70afSSebastian Siewior 	.bLength		= USB_DT_INTERFACE_SIZE,
1073aab70afSSebastian Siewior 	.bDescriptorType	= USB_DT_INTERFACE,
1083aab70afSSebastian Siewior 	.bInterfaceNumber	= 0x00,
1093aab70afSSebastian Siewior 	.bAlternateSetting	= 0x00,
1103aab70afSSebastian Siewior 	.bNumEndpoints		= 0x02,
1113aab70afSSebastian Siewior 	.bInterfaceClass	= FASTBOOT_INTERFACE_CLASS,
1123aab70afSSebastian Siewior 	.bInterfaceSubClass	= FASTBOOT_INTERFACE_SUB_CLASS,
1133aab70afSSebastian Siewior 	.bInterfaceProtocol	= FASTBOOT_INTERFACE_PROTOCOL,
1143aab70afSSebastian Siewior };
1153aab70afSSebastian Siewior 
116718156adSRoger Quadros static struct usb_descriptor_header *fb_fs_function[] = {
1173aab70afSSebastian Siewior 	(struct usb_descriptor_header *)&interface_desc,
1183aab70afSSebastian Siewior 	(struct usb_descriptor_header *)&fs_ep_in,
119718156adSRoger Quadros 	(struct usb_descriptor_header *)&fs_ep_out,
120718156adSRoger Quadros };
121718156adSRoger Quadros 
122718156adSRoger Quadros static struct usb_descriptor_header *fb_hs_function[] = {
123718156adSRoger Quadros 	(struct usb_descriptor_header *)&interface_desc,
124718156adSRoger Quadros 	(struct usb_descriptor_header *)&hs_ep_in,
1253aab70afSSebastian Siewior 	(struct usb_descriptor_header *)&hs_ep_out,
1263aab70afSSebastian Siewior 	NULL,
1273aab70afSSebastian Siewior };
1283aab70afSSebastian Siewior 
1298b704a0eSRoger Quadros static struct usb_endpoint_descriptor *
1308b704a0eSRoger Quadros fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
1318b704a0eSRoger Quadros 	    struct usb_endpoint_descriptor *hs)
1328b704a0eSRoger Quadros {
1338b704a0eSRoger Quadros 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
1348b704a0eSRoger Quadros 		return hs;
1358b704a0eSRoger Quadros 	return fs;
1368b704a0eSRoger Quadros }
1378b704a0eSRoger Quadros 
1383aab70afSSebastian Siewior /*
1393aab70afSSebastian Siewior  * static strings, in UTF-8
1403aab70afSSebastian Siewior  */
1413aab70afSSebastian Siewior static const char fastboot_name[] = "Android Fastboot";
1423aab70afSSebastian Siewior 
1433aab70afSSebastian Siewior static struct usb_string fastboot_string_defs[] = {
1443aab70afSSebastian Siewior 	[0].s = fastboot_name,
1453aab70afSSebastian Siewior 	{  }			/* end of list */
1463aab70afSSebastian Siewior };
1473aab70afSSebastian Siewior 
1483aab70afSSebastian Siewior static struct usb_gadget_strings stringtab_fastboot = {
1493aab70afSSebastian Siewior 	.language	= 0x0409,	/* en-us */
1503aab70afSSebastian Siewior 	.strings	= fastboot_string_defs,
1513aab70afSSebastian Siewior };
1523aab70afSSebastian Siewior 
1533aab70afSSebastian Siewior static struct usb_gadget_strings *fastboot_strings[] = {
1543aab70afSSebastian Siewior 	&stringtab_fastboot,
1553aab70afSSebastian Siewior 	NULL,
1563aab70afSSebastian Siewior };
1573aab70afSSebastian Siewior 
1583aab70afSSebastian Siewior static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
159e2ec3e46SAlexey Firago static int strcmp_l1(const char *s1, const char *s2);
1603aab70afSSebastian Siewior 
1613aab70afSSebastian Siewior static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
1623aab70afSSebastian Siewior {
1633aab70afSSebastian Siewior 	int status = req->status;
1643aab70afSSebastian Siewior 	if (!status)
1653aab70afSSebastian Siewior 		return;
1663aab70afSSebastian Siewior 	printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
1673aab70afSSebastian Siewior }
1683aab70afSSebastian Siewior 
1693aab70afSSebastian Siewior static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
1703aab70afSSebastian Siewior {
1713aab70afSSebastian Siewior 	int id;
1723aab70afSSebastian Siewior 	struct usb_gadget *gadget = c->cdev->gadget;
1733aab70afSSebastian Siewior 	struct f_fastboot *f_fb = func_to_fastboot(f);
174537cd072SDileep Katta 	const char *s;
1753aab70afSSebastian Siewior 
1763aab70afSSebastian Siewior 	/* DYNAMIC interface numbers assignments */
1773aab70afSSebastian Siewior 	id = usb_interface_id(c, f);
1783aab70afSSebastian Siewior 	if (id < 0)
1793aab70afSSebastian Siewior 		return id;
1803aab70afSSebastian Siewior 	interface_desc.bInterfaceNumber = id;
1813aab70afSSebastian Siewior 
1823aab70afSSebastian Siewior 	id = usb_string_id(c->cdev);
1833aab70afSSebastian Siewior 	if (id < 0)
1843aab70afSSebastian Siewior 		return id;
1853aab70afSSebastian Siewior 	fastboot_string_defs[0].id = id;
1863aab70afSSebastian Siewior 	interface_desc.iInterface = id;
1873aab70afSSebastian Siewior 
1883aab70afSSebastian Siewior 	f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
1893aab70afSSebastian Siewior 	if (!f_fb->in_ep)
1903aab70afSSebastian Siewior 		return -ENODEV;
1913aab70afSSebastian Siewior 	f_fb->in_ep->driver_data = c->cdev;
1923aab70afSSebastian Siewior 
1933aab70afSSebastian Siewior 	f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
1943aab70afSSebastian Siewior 	if (!f_fb->out_ep)
1953aab70afSSebastian Siewior 		return -ENODEV;
1963aab70afSSebastian Siewior 	f_fb->out_ep->driver_data = c->cdev;
1973aab70afSSebastian Siewior 
198718156adSRoger Quadros 	f->descriptors = fb_fs_function;
199718156adSRoger Quadros 
200718156adSRoger Quadros 	if (gadget_is_dualspeed(gadget)) {
201718156adSRoger Quadros 		/* Assume endpoint addresses are the same for both speeds */
202718156adSRoger Quadros 		hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
2033aab70afSSebastian Siewior 		hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
204718156adSRoger Quadros 		/* copy HS descriptors */
205718156adSRoger Quadros 		f->hs_descriptors = fb_hs_function;
206718156adSRoger Quadros 	}
2073aab70afSSebastian Siewior 
20800caae6dSSimon Glass 	s = env_get("serial#");
209537cd072SDileep Katta 	if (s)
210537cd072SDileep Katta 		g_dnl_set_serialnumber((char *)s);
211537cd072SDileep Katta 
2123aab70afSSebastian Siewior 	return 0;
2133aab70afSSebastian Siewior }
2143aab70afSSebastian Siewior 
2153aab70afSSebastian Siewior static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
2163aab70afSSebastian Siewior {
2173aab70afSSebastian Siewior 	memset(fastboot_func, 0, sizeof(*fastboot_func));
2183aab70afSSebastian Siewior }
2193aab70afSSebastian Siewior 
2203aab70afSSebastian Siewior static void fastboot_disable(struct usb_function *f)
2213aab70afSSebastian Siewior {
2223aab70afSSebastian Siewior 	struct f_fastboot *f_fb = func_to_fastboot(f);
2233aab70afSSebastian Siewior 
2243aab70afSSebastian Siewior 	usb_ep_disable(f_fb->out_ep);
2253aab70afSSebastian Siewior 	usb_ep_disable(f_fb->in_ep);
2263aab70afSSebastian Siewior 
2273aab70afSSebastian Siewior 	if (f_fb->out_req) {
2283aab70afSSebastian Siewior 		free(f_fb->out_req->buf);
2293aab70afSSebastian Siewior 		usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
2303aab70afSSebastian Siewior 		f_fb->out_req = NULL;
2313aab70afSSebastian Siewior 	}
2323aab70afSSebastian Siewior 	if (f_fb->in_req) {
2333aab70afSSebastian Siewior 		free(f_fb->in_req->buf);
2343aab70afSSebastian Siewior 		usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
2353aab70afSSebastian Siewior 		f_fb->in_req = NULL;
2363aab70afSSebastian Siewior 	}
2373aab70afSSebastian Siewior }
2383aab70afSSebastian Siewior 
2393aab70afSSebastian Siewior static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
2403aab70afSSebastian Siewior {
2413aab70afSSebastian Siewior 	struct usb_request *req;
2423aab70afSSebastian Siewior 
2433aab70afSSebastian Siewior 	req = usb_ep_alloc_request(ep, 0);
2443aab70afSSebastian Siewior 	if (!req)
2453aab70afSSebastian Siewior 		return NULL;
2463aab70afSSebastian Siewior 
2473aab70afSSebastian Siewior 	req->length = EP_BUFFER_SIZE;
2483aab70afSSebastian Siewior 	req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
2493aab70afSSebastian Siewior 	if (!req->buf) {
2503aab70afSSebastian Siewior 		usb_ep_free_request(ep, req);
2513aab70afSSebastian Siewior 		return NULL;
2523aab70afSSebastian Siewior 	}
2533aab70afSSebastian Siewior 
2543aab70afSSebastian Siewior 	memset(req->buf, 0, req->length);
2553aab70afSSebastian Siewior 	return req;
2563aab70afSSebastian Siewior }
2573aab70afSSebastian Siewior 
2583aab70afSSebastian Siewior static int fastboot_set_alt(struct usb_function *f,
2593aab70afSSebastian Siewior 			    unsigned interface, unsigned alt)
2603aab70afSSebastian Siewior {
2613aab70afSSebastian Siewior 	int ret;
2623aab70afSSebastian Siewior 	struct usb_composite_dev *cdev = f->config->cdev;
2633aab70afSSebastian Siewior 	struct usb_gadget *gadget = cdev->gadget;
2643aab70afSSebastian Siewior 	struct f_fastboot *f_fb = func_to_fastboot(f);
2658b704a0eSRoger Quadros 	const struct usb_endpoint_descriptor *d;
2663aab70afSSebastian Siewior 
2673aab70afSSebastian Siewior 	debug("%s: func: %s intf: %d alt: %d\n",
2683aab70afSSebastian Siewior 	      __func__, f->name, interface, alt);
2693aab70afSSebastian Siewior 
2708b704a0eSRoger Quadros 	d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
2718b704a0eSRoger Quadros 	ret = usb_ep_enable(f_fb->out_ep, d);
2723aab70afSSebastian Siewior 	if (ret) {
2733aab70afSSebastian Siewior 		puts("failed to enable out ep\n");
2743aab70afSSebastian Siewior 		return ret;
2753aab70afSSebastian Siewior 	}
2763aab70afSSebastian Siewior 
2773aab70afSSebastian Siewior 	f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
2783aab70afSSebastian Siewior 	if (!f_fb->out_req) {
2793aab70afSSebastian Siewior 		puts("failed to alloc out req\n");
2803aab70afSSebastian Siewior 		ret = -EINVAL;
2813aab70afSSebastian Siewior 		goto err;
2823aab70afSSebastian Siewior 	}
2833aab70afSSebastian Siewior 	f_fb->out_req->complete = rx_handler_command;
2843aab70afSSebastian Siewior 
2858b704a0eSRoger Quadros 	d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
2868b704a0eSRoger Quadros 	ret = usb_ep_enable(f_fb->in_ep, d);
2873aab70afSSebastian Siewior 	if (ret) {
2883aab70afSSebastian Siewior 		puts("failed to enable in ep\n");
2893aab70afSSebastian Siewior 		goto err;
2903aab70afSSebastian Siewior 	}
2913aab70afSSebastian Siewior 
2923aab70afSSebastian Siewior 	f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
2933aab70afSSebastian Siewior 	if (!f_fb->in_req) {
2943aab70afSSebastian Siewior 		puts("failed alloc req in\n");
2953aab70afSSebastian Siewior 		ret = -EINVAL;
2963aab70afSSebastian Siewior 		goto err;
2973aab70afSSebastian Siewior 	}
2983aab70afSSebastian Siewior 	f_fb->in_req->complete = fastboot_complete;
2993aab70afSSebastian Siewior 
3003aab70afSSebastian Siewior 	ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
3013aab70afSSebastian Siewior 	if (ret)
3023aab70afSSebastian Siewior 		goto err;
3033aab70afSSebastian Siewior 
3043aab70afSSebastian Siewior 	return 0;
3053aab70afSSebastian Siewior err:
3063aab70afSSebastian Siewior 	fastboot_disable(f);
3073aab70afSSebastian Siewior 	return ret;
3083aab70afSSebastian Siewior }
3093aab70afSSebastian Siewior 
3103aab70afSSebastian Siewior static int fastboot_add(struct usb_configuration *c)
3113aab70afSSebastian Siewior {
3123aab70afSSebastian Siewior 	struct f_fastboot *f_fb = fastboot_func;
3133aab70afSSebastian Siewior 	int status;
3143aab70afSSebastian Siewior 
3153aab70afSSebastian Siewior 	debug("%s: cdev: 0x%p\n", __func__, c->cdev);
3163aab70afSSebastian Siewior 
3173aab70afSSebastian Siewior 	if (!f_fb) {
3183aab70afSSebastian Siewior 		f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
3193aab70afSSebastian Siewior 		if (!f_fb)
3203aab70afSSebastian Siewior 			return -ENOMEM;
3213aab70afSSebastian Siewior 
3223aab70afSSebastian Siewior 		fastboot_func = f_fb;
3233aab70afSSebastian Siewior 		memset(f_fb, 0, sizeof(*f_fb));
3243aab70afSSebastian Siewior 	}
3253aab70afSSebastian Siewior 
3263aab70afSSebastian Siewior 	f_fb->usb_function.name = "f_fastboot";
3273aab70afSSebastian Siewior 	f_fb->usb_function.bind = fastboot_bind;
3283aab70afSSebastian Siewior 	f_fb->usb_function.unbind = fastboot_unbind;
3293aab70afSSebastian Siewior 	f_fb->usb_function.set_alt = fastboot_set_alt;
3303aab70afSSebastian Siewior 	f_fb->usb_function.disable = fastboot_disable;
3313aab70afSSebastian Siewior 	f_fb->usb_function.strings = fastboot_strings;
3323aab70afSSebastian Siewior 
3333aab70afSSebastian Siewior 	status = usb_add_function(c, &f_fb->usb_function);
3343aab70afSSebastian Siewior 	if (status) {
3353aab70afSSebastian Siewior 		free(f_fb);
3363aab70afSSebastian Siewior 		fastboot_func = f_fb;
3373aab70afSSebastian Siewior 	}
3383aab70afSSebastian Siewior 
3393aab70afSSebastian Siewior 	return status;
3403aab70afSSebastian Siewior }
3413aab70afSSebastian Siewior DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
3423aab70afSSebastian Siewior 
343593cbd93SSteve Rae static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
3443aab70afSSebastian Siewior {
3453aab70afSSebastian Siewior 	struct usb_request *in_req = fastboot_func->in_req;
3463aab70afSSebastian Siewior 	int ret;
3473aab70afSSebastian Siewior 
3483aab70afSSebastian Siewior 	memcpy(in_req->buf, buffer, buffer_size);
3493aab70afSSebastian Siewior 	in_req->length = buffer_size;
350bc9071c9SPaul Kocialkowski 
351bc9071c9SPaul Kocialkowski 	usb_ep_dequeue(fastboot_func->in_ep, in_req);
352bc9071c9SPaul Kocialkowski 
3533aab70afSSebastian Siewior 	ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
3543aab70afSSebastian Siewior 	if (ret)
3553aab70afSSebastian Siewior 		printf("Error %d on queue\n", ret);
3563aab70afSSebastian Siewior 	return 0;
3573aab70afSSebastian Siewior }
3583aab70afSSebastian Siewior 
3593aab70afSSebastian Siewior static int fastboot_tx_write_str(const char *buffer)
3603aab70afSSebastian Siewior {
3613aab70afSSebastian Siewior 	return fastboot_tx_write(buffer, strlen(buffer));
3623aab70afSSebastian Siewior }
3633aab70afSSebastian Siewior 
3643aab70afSSebastian Siewior static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
3653aab70afSSebastian Siewior {
3663aab70afSSebastian Siewior 	do_reset(NULL, 0, 0, NULL);
3673aab70afSSebastian Siewior }
3683aab70afSSebastian Siewior 
369e2ec3e46SAlexey Firago int __weak fb_set_reboot_flag(void)
370e2ec3e46SAlexey Firago {
371e2ec3e46SAlexey Firago 	return -ENOSYS;
372e2ec3e46SAlexey Firago }
373e2ec3e46SAlexey Firago 
3743aab70afSSebastian Siewior static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
3753aab70afSSebastian Siewior {
376e2ec3e46SAlexey Firago 	char *cmd = req->buf;
377e2ec3e46SAlexey Firago 	if (!strcmp_l1("reboot-bootloader", cmd)) {
378e2ec3e46SAlexey Firago 		if (fb_set_reboot_flag()) {
379e2ec3e46SAlexey Firago 			fastboot_tx_write_str("FAILCannot set reboot flag");
380e2ec3e46SAlexey Firago 			return;
381e2ec3e46SAlexey Firago 		}
382e2ec3e46SAlexey Firago 	}
3833aab70afSSebastian Siewior 	fastboot_func->in_req->complete = compl_do_reset;
3843aab70afSSebastian Siewior 	fastboot_tx_write_str("OKAY");
3853aab70afSSebastian Siewior }
3863aab70afSSebastian Siewior 
3873aab70afSSebastian Siewior static int strcmp_l1(const char *s1, const char *s2)
3883aab70afSSebastian Siewior {
3893aab70afSSebastian Siewior 	if (!s1 || !s2)
3903aab70afSSebastian Siewior 		return -1;
3913aab70afSSebastian Siewior 	return strncmp(s1, s2, strlen(s1));
3923aab70afSSebastian Siewior }
3933aab70afSSebastian Siewior 
3943aab70afSSebastian Siewior static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
3953aab70afSSebastian Siewior {
3963aab70afSSebastian Siewior 	char *cmd = req->buf;
3973c8f98f5SMaxime Ripard 	char response[FASTBOOT_RESPONSE_LEN];
3983aab70afSSebastian Siewior 	const char *s;
39929425be4SJeroen Hofstee 	size_t chars_left;
4003aab70afSSebastian Siewior 
4013aab70afSSebastian Siewior 	strcpy(response, "OKAY");
40229425be4SJeroen Hofstee 	chars_left = sizeof(response) - strlen(response) - 1;
40329425be4SJeroen Hofstee 
4043aab70afSSebastian Siewior 	strsep(&cmd, ":");
4053aab70afSSebastian Siewior 	if (!cmd) {
40690aa625cSMasahiro Yamada 		pr_err("missing variable");
4073aab70afSSebastian Siewior 		fastboot_tx_write_str("FAILmissing var");
4083aab70afSSebastian Siewior 		return;
4093aab70afSSebastian Siewior 	}
4103aab70afSSebastian Siewior 
4113aab70afSSebastian Siewior 	if (!strcmp_l1("version", cmd)) {
41229425be4SJeroen Hofstee 		strncat(response, FASTBOOT_VERSION, chars_left);
4133aab70afSSebastian Siewior 	} else if (!strcmp_l1("bootloader-version", cmd)) {
41429425be4SJeroen Hofstee 		strncat(response, U_BOOT_VERSION, chars_left);
415374a9995SCody Xie 	} else if (!strcmp_l1("product", cmd)) {
416374a9995SCody Xie 		strncat(response, CONFIG_SYS_BOARD, chars_left);
417374a9995SCody Xie 	} else if (!strcmp_l1("variant", cmd)) {
418374a9995SCody Xie 		strncat(response, "userdebug", chars_left);
419374a9995SCody Xie 	} else if (!strcmp_l1("secure", cmd)) {
420374a9995SCody Xie 		strncat(response, "no", chars_left);
421374a9995SCody Xie 	} else if (!strcmp_l1("unlocked", cmd)) {
422374a9995SCody Xie 		strncat(response, "yes", chars_left);
423374a9995SCody Xie 	} else if (!strcmp_l1("off-mode-charge", cmd)) {
424374a9995SCody Xie 		strncat(response, "0", chars_left);
425374a9995SCody Xie 	} else if (!strcmp_l1("battery-voltage", cmd)) {
426374a9995SCody Xie 		strncat(response, "7.4", chars_left);
427374a9995SCody Xie 	} else if (!strcmp_l1("battery-soc-ok", cmd)) {
428374a9995SCody Xie 		strncat(response, "yes", chars_left);
429c674a666SEric Nelson 	} else if (!strcmp_l1("downloadsize", cmd) ||
430c674a666SEric Nelson 		!strcmp_l1("max-download-size", cmd)) {
4313aab70afSSebastian Siewior 		char str_num[12];
4323aab70afSSebastian Siewior 
433a588d99aSPaul Kocialkowski 		sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
43429425be4SJeroen Hofstee 		strncat(response, str_num, chars_left);
4353aab70afSSebastian Siewior 	} else if (!strcmp_l1("serialno", cmd)) {
43600caae6dSSimon Glass 		s = env_get("serial#");
4373aab70afSSebastian Siewior 		if (s)
43829425be4SJeroen Hofstee 			strncat(response, s, chars_left);
4393aab70afSSebastian Siewior 		else
4403aab70afSSebastian Siewior 			strcpy(response, "FAILValue not set");
441367cce4dSXu Hongfei 	} else if (strncmp("at-attest-dh", cmd, 12) == 0) {
4424d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT
4434d0fc665SAndy Ye 		char dhbuf[8];
4444d0fc665SAndy Ye 		uint32_t dh_len = 8;
4454d0fc665SAndy Ye 		uint32_t res = trusty_attest_dh((uint8_t *)dhbuf, &dh_len);
4464d0fc665SAndy Ye 		if (res)
4474d0fc665SAndy Ye 			strcpy(response, "FAILdh not set");
4484d0fc665SAndy Ye 		else
4494d0fc665SAndy Ye 			strncat(response, dhbuf, chars_left);
4504d0fc665SAndy Ye #else
4514d0fc665SAndy Ye 		fastboot_tx_write_str("FAILnot implemented");
4524d0fc665SAndy Ye 		return;
4534d0fc665SAndy Ye #endif
454367cce4dSXu Hongfei 	} else if (strncmp("at-attest-uuid", cmd, 14) == 0) {
4554d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT
456367cce4dSXu Hongfei 		char uuid[32] = {0};
4574d0fc665SAndy Ye 		uint32_t uuid_len = 32;
4584d0fc665SAndy Ye 		uint32_t res = trusty_attest_uuid((uint8_t *)uuid, &uuid_len);
4594d0fc665SAndy Ye 		if (res)
4604d0fc665SAndy Ye 			strcpy(response, "FAILuuid not set");
4614d0fc665SAndy Ye 		else
462367cce4dSXu Hongfei 			strncat(response, uuid, chars_left);
4634d0fc665SAndy Ye #else
4644d0fc665SAndy Ye 		fastboot_tx_write_str("FAILnot implemented");
4654d0fc665SAndy Ye 		return;
4664d0fc665SAndy Ye #endif
467367cce4dSXu Hongfei 	} else if (strncmp("at-vboot-state", cmd, 14) == 0) {
468367cce4dSXu Hongfei 		char uuid[32] = {0};
469367cce4dSXu Hongfei 
470367cce4dSXu Hongfei 		strncat(response, uuid, chars_left);
471367cce4dSXu Hongfei 	} else if (!strcmp_l1("slot-count", cmd)) {
47237a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
473367cce4dSXu Hongfei 		char slot_count[2];
474367cce4dSXu Hongfei 		char temp;
475367cce4dSXu Hongfei 
476367cce4dSXu Hongfei 		slot_count[1] = '\0';
47737a7bc39SJason Zhu 		rk_avb_read_slot_count(&temp);
478367cce4dSXu Hongfei 		slot_count[0] = temp + 0x30;
479367cce4dSXu Hongfei 		strncat(response, slot_count, chars_left);
480367cce4dSXu Hongfei #else
481367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
482367cce4dSXu Hongfei 		return;
483367cce4dSXu Hongfei #endif
484367cce4dSXu Hongfei 	} else if (!strcmp_l1("current-slot", cmd)) {
48537a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
486367cce4dSXu Hongfei 		char slot_surrent[8] = {0};
487367cce4dSXu Hongfei 
48837a7bc39SJason Zhu 		if (!rk_avb_get_current_slot(slot_surrent))
489374a9995SCody Xie 			strncat(response, slot_surrent+1, chars_left);
490367cce4dSXu Hongfei 		else
491367cce4dSXu Hongfei 			strcpy(response, "FAILgeterror");
492367cce4dSXu Hongfei #else
493367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
494367cce4dSXu Hongfei 		return;
495367cce4dSXu Hongfei #endif
496367cce4dSXu Hongfei 	} else if (!strcmp_l1("slot-suffixes", cmd)) {
49737a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
498367cce4dSXu Hongfei 		char slot_suffixes_temp[4];
499367cce4dSXu Hongfei 		char slot_suffixes[9];
500367cce4dSXu Hongfei 		int slot_cnt = 0;
501367cce4dSXu Hongfei 
502367cce4dSXu Hongfei 		memset(slot_suffixes_temp, 0, 4);
503367cce4dSXu Hongfei 		memset(slot_suffixes, 0, 9);
50437a7bc39SJason Zhu 		rk_avb_read_slot_suffixes(slot_suffixes_temp);
505367cce4dSXu Hongfei 		while (slot_suffixes_temp[slot_cnt] != '\0') {
506367cce4dSXu Hongfei 			slot_suffixes[slot_cnt * 2]
507367cce4dSXu Hongfei 				= slot_suffixes_temp[slot_cnt];
508367cce4dSXu Hongfei 			slot_suffixes[slot_cnt * 2 + 1] = ',';
509367cce4dSXu Hongfei 			slot_cnt++;
510367cce4dSXu Hongfei 		}
511367cce4dSXu Hongfei 		strncat(response, slot_suffixes, chars_left);
512367cce4dSXu Hongfei #else
513367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
514367cce4dSXu Hongfei 		return;
515367cce4dSXu Hongfei #endif
516367cce4dSXu Hongfei 	} else if (!strncmp("has-slot", cmd, 8)) {
51737a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
518367cce4dSXu Hongfei 		char *part_name = cmd;
519367cce4dSXu Hongfei 
520367cce4dSXu Hongfei 		cmd = strsep(&part_name, ":");
521367cce4dSXu Hongfei 		if (!strcmp(part_name, "boot") ||
522367cce4dSXu Hongfei 		    !strcmp(part_name, "system") ||
523374a9995SCody Xie 		    !strcmp(part_name, "vendor") ||
524374a9995SCody Xie 		    !strcmp(part_name, "vbmeta") ||
525374a9995SCody Xie 		    !strcmp(part_name, "oem")) {
526367cce4dSXu Hongfei 			strncat(response, "yes", chars_left);
527367cce4dSXu Hongfei 		} else {
528367cce4dSXu Hongfei 			strcpy(response, "FAILno");
529367cce4dSXu Hongfei 		}
530367cce4dSXu Hongfei #else
531367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
532367cce4dSXu Hongfei 		return;
533367cce4dSXu Hongfei #endif
534374a9995SCody Xie 	} else if (!strncmp("slot-unbootable", cmd, 15)) {
53537a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
536374a9995SCody Xie 		char *slot_name = cmd;
537374a9995SCody Xie 
538374a9995SCody Xie 		cmd = strsep(&slot_name, ":");
539374a9995SCody Xie 		if (!strcmp(slot_name, "a") ||
540374a9995SCody Xie 		    !strcmp(slot_name, "b")) {
541374a9995SCody Xie 			strncat(response, "no", chars_left);
542374a9995SCody Xie 		} else {
543374a9995SCody Xie 			strcpy(response, "FAILno");
544374a9995SCody Xie 		}
545374a9995SCody Xie #else
546374a9995SCody Xie 		fastboot_tx_write_str("FAILnot implemented");
547374a9995SCody Xie 		return;
548374a9995SCody Xie #endif
549374a9995SCody Xie 	} else if (!strncmp("slot-successful", cmd, 15)) {
55037a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
551374a9995SCody Xie 		char *slot_name = cmd;
552374a9995SCody Xie 
553374a9995SCody Xie 		cmd = strsep(&slot_name, ":");
554374a9995SCody Xie 		if (!strcmp(slot_name, "a") ||
555374a9995SCody Xie 		    !strcmp(slot_name, "b")) {
556374a9995SCody Xie 			strncat(response, "no", chars_left);
557374a9995SCody Xie 		} else {
558374a9995SCody Xie 			strcpy(response, "FAILno");
559374a9995SCody Xie 		}
560374a9995SCody Xie #else
561374a9995SCody Xie 		fastboot_tx_write_str("FAILnot implemented");
562374a9995SCody Xie 		return;
563374a9995SCody Xie #endif
564374a9995SCody Xie 	} else if (!strncmp("slot-retry-count", cmd, 16)) {
56537a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
566374a9995SCody Xie 		char *slot_name = cmd;
567374a9995SCody Xie 		char count[10] = {0};
568374a9995SCody Xie 		static int cnt[2] = {0};
569374a9995SCody Xie 
570374a9995SCody Xie 		cmd = strsep(&slot_name, ":");
571374a9995SCody Xie 		if (!strcmp(slot_name, "a")) {
572374a9995SCody Xie 			sprintf(count, "%c", 0x30+cnt[0]);
573374a9995SCody Xie 			strncat(response, count, chars_left);
574374a9995SCody Xie 			if (cnt[0] > 0)
575374a9995SCody Xie 				cnt[0]--;
576374a9995SCody Xie 		} else if (!strcmp(slot_name, "b")) {
577374a9995SCody Xie 			sprintf(count, "%c", 0x30+cnt[1]);
578374a9995SCody Xie 			strncat(response, count, chars_left);
579374a9995SCody Xie 			if (cnt[1] > 0)
580374a9995SCody Xie 				cnt[1]--;
581374a9995SCody Xie 		} else {
582374a9995SCody Xie 			strcpy(response, "FAILno");
583374a9995SCody Xie 		}
584374a9995SCody Xie #else
585374a9995SCody Xie 		fastboot_tx_write_str("FAILnot implemented");
586374a9995SCody Xie 		return;
587374a9995SCody Xie #endif
588367cce4dSXu Hongfei 	} else if (!strncmp("partition-type", cmd, 14) ||
589367cce4dSXu Hongfei 		   !strncmp("partition-size", cmd, 14)) {
590367cce4dSXu Hongfei 		disk_partition_t part_info;
591367cce4dSXu Hongfei 		struct blk_desc *dev_desc;
592367cce4dSXu Hongfei 		char *part_name = cmd;
593367cce4dSXu Hongfei 		char part_size_str[20];
594367cce4dSXu Hongfei 
595367cce4dSXu Hongfei 		cmd = strsep(&part_name, ":");
596367cce4dSXu Hongfei 		dev_desc = blk_get_dev("mmc", 0);
597367cce4dSXu Hongfei 		if (!dev_desc) {
598367cce4dSXu Hongfei 			strcpy(response, "FAILblock device not found");
599367cce4dSXu Hongfei 		} else if (part_get_info_by_name(dev_desc, part_name, &part_info) < 0) {
600367cce4dSXu Hongfei 			strcpy(response, "FAILpartition not found");
601367cce4dSXu Hongfei 		} else if (!strncmp("partition-type", cmd, 14)) {
602367cce4dSXu Hongfei 			strncat(response, (char *)part_info.type, chars_left);
603367cce4dSXu Hongfei 		} else if (!strncmp("partition-size", cmd, 14)) {
604367cce4dSXu Hongfei 			sprintf(part_size_str, "0x%016x", (int)part_info.size);
605367cce4dSXu Hongfei 			strncat(response, part_size_str, chars_left);
606367cce4dSXu Hongfei 		}
6073aab70afSSebastian Siewior 	} else {
608b1f2a17cSnicolas.le.bayon@st.com 		char *envstr;
60974322201SRob Herring 
610b1f2a17cSnicolas.le.bayon@st.com 		envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1);
611b1f2a17cSnicolas.le.bayon@st.com 		if (!envstr) {
612b1f2a17cSnicolas.le.bayon@st.com 			fastboot_tx_write_str("FAILmalloc error");
613b1f2a17cSnicolas.le.bayon@st.com 			return;
614b1f2a17cSnicolas.le.bayon@st.com 		}
615b1f2a17cSnicolas.le.bayon@st.com 
616b1f2a17cSnicolas.le.bayon@st.com 		sprintf(envstr, "fastboot.%s", cmd);
61700caae6dSSimon Glass 		s = env_get(envstr);
61874322201SRob Herring 		if (s) {
61974322201SRob Herring 			strncat(response, s, chars_left);
62074322201SRob Herring 		} else {
621a18c2706SSteve Rae 			printf("WARNING: unknown variable: %s\n", cmd);
6223aab70afSSebastian Siewior 			strcpy(response, "FAILVariable not implemented");
6233aab70afSSebastian Siewior 		}
624b1f2a17cSnicolas.le.bayon@st.com 
625b1f2a17cSnicolas.le.bayon@st.com 		free(envstr);
62674322201SRob Herring 	}
6273aab70afSSebastian Siewior 	fastboot_tx_write_str(response);
6283aab70afSSebastian Siewior }
6293aab70afSSebastian Siewior 
630ac484c5aSRoger Quadros static unsigned int rx_bytes_expected(struct usb_ep *ep)
6313aab70afSSebastian Siewior {
6323aab70afSSebastian Siewior 	int rx_remain = download_size - download_bytes;
633ac484c5aSRoger Quadros 	unsigned int rem;
634ac484c5aSRoger Quadros 	unsigned int maxpacket = ep->maxpacket;
635ac484c5aSRoger Quadros 
636ac484c5aSRoger Quadros 	if (rx_remain <= 0)
6373aab70afSSebastian Siewior 		return 0;
638ac484c5aSRoger Quadros 	else if (rx_remain > EP_BUFFER_SIZE)
6393aab70afSSebastian Siewior 		return EP_BUFFER_SIZE;
640ac484c5aSRoger Quadros 
641ac484c5aSRoger Quadros 	/*
642ac484c5aSRoger Quadros 	 * Some controllers e.g. DWC3 don't like OUT transfers to be
643ac484c5aSRoger Quadros 	 * not ending in maxpacket boundary. So just make them happy by
644ac484c5aSRoger Quadros 	 * always requesting for integral multiple of maxpackets.
645ac484c5aSRoger Quadros 	 * This shouldn't bother controllers that don't care about it.
646ac484c5aSRoger Quadros 	 */
6479e4b510dSDileep Katta 	rem = rx_remain % maxpacket;
648ac484c5aSRoger Quadros 	if (rem > 0)
6499e4b510dSDileep Katta 		rx_remain = rx_remain + (maxpacket - rem);
650ac484c5aSRoger Quadros 
6513aab70afSSebastian Siewior 	return rx_remain;
6523aab70afSSebastian Siewior }
6533aab70afSSebastian Siewior 
6543aab70afSSebastian Siewior #define BYTES_PER_DOT	0x20000
6553aab70afSSebastian Siewior static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
6563aab70afSSebastian Siewior {
6573c8f98f5SMaxime Ripard 	char response[FASTBOOT_RESPONSE_LEN];
6583aab70afSSebastian Siewior 	unsigned int transfer_size = download_size - download_bytes;
6593aab70afSSebastian Siewior 	const unsigned char *buffer = req->buf;
6603aab70afSSebastian Siewior 	unsigned int buffer_size = req->actual;
66123d1d10cSBo Shen 	unsigned int pre_dot_num, now_dot_num;
6623aab70afSSebastian Siewior 
6633aab70afSSebastian Siewior 	if (req->status != 0) {
6643aab70afSSebastian Siewior 		printf("Bad status: %d\n", req->status);
6653aab70afSSebastian Siewior 		return;
6663aab70afSSebastian Siewior 	}
6673aab70afSSebastian Siewior 
6683aab70afSSebastian Siewior 	if (buffer_size < transfer_size)
6693aab70afSSebastian Siewior 		transfer_size = buffer_size;
6703aab70afSSebastian Siewior 
671a588d99aSPaul Kocialkowski 	memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
6723aab70afSSebastian Siewior 	       buffer, transfer_size);
6733aab70afSSebastian Siewior 
67423d1d10cSBo Shen 	pre_dot_num = download_bytes / BYTES_PER_DOT;
6753aab70afSSebastian Siewior 	download_bytes += transfer_size;
67623d1d10cSBo Shen 	now_dot_num = download_bytes / BYTES_PER_DOT;
67723d1d10cSBo Shen 
67823d1d10cSBo Shen 	if (pre_dot_num != now_dot_num) {
67923d1d10cSBo Shen 		putc('.');
68023d1d10cSBo Shen 		if (!(now_dot_num % 74))
68123d1d10cSBo Shen 			putc('\n');
68223d1d10cSBo Shen 	}
6833aab70afSSebastian Siewior 
6843aab70afSSebastian Siewior 	/* Check if transfer is done */
6853aab70afSSebastian Siewior 	if (download_bytes >= download_size) {
6863aab70afSSebastian Siewior 		/*
6873aab70afSSebastian Siewior 		 * Reset global transfer variable, keep download_bytes because
6883aab70afSSebastian Siewior 		 * it will be used in the next possible flashing command
6893aab70afSSebastian Siewior 		 */
6903aab70afSSebastian Siewior 		download_size = 0;
6913aab70afSSebastian Siewior 		req->complete = rx_handler_command;
6923aab70afSSebastian Siewior 		req->length = EP_BUFFER_SIZE;
6933aab70afSSebastian Siewior 
694192bc694SBen Whitten 		strcpy(response, "OKAY");
6953aab70afSSebastian Siewior 		fastboot_tx_write_str(response);
6963aab70afSSebastian Siewior 
6973aab70afSSebastian Siewior 		printf("\ndownloading of %d bytes finished\n", download_bytes);
6983aab70afSSebastian Siewior 	} else {
699ac484c5aSRoger Quadros 		req->length = rx_bytes_expected(ep);
7003aab70afSSebastian Siewior 	}
7013aab70afSSebastian Siewior 
7023aab70afSSebastian Siewior 	req->actual = 0;
7033aab70afSSebastian Siewior 	usb_ep_queue(ep, req, 0);
7043aab70afSSebastian Siewior }
7053aab70afSSebastian Siewior 
7063aab70afSSebastian Siewior static void cb_download(struct usb_ep *ep, struct usb_request *req)
7073aab70afSSebastian Siewior {
7083aab70afSSebastian Siewior 	char *cmd = req->buf;
7093c8f98f5SMaxime Ripard 	char response[FASTBOOT_RESPONSE_LEN];
7103aab70afSSebastian Siewior 
7113aab70afSSebastian Siewior 	strsep(&cmd, ":");
7123aab70afSSebastian Siewior 	download_size = simple_strtoul(cmd, NULL, 16);
7133aab70afSSebastian Siewior 	download_bytes = 0;
7143aab70afSSebastian Siewior 
7153aab70afSSebastian Siewior 	printf("Starting download of %d bytes\n", download_size);
7163aab70afSSebastian Siewior 
7173aab70afSSebastian Siewior 	if (0 == download_size) {
718192bc694SBen Whitten 		strcpy(response, "FAILdata invalid size");
719a588d99aSPaul Kocialkowski 	} else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
7203aab70afSSebastian Siewior 		download_size = 0;
721192bc694SBen Whitten 		strcpy(response, "FAILdata too large");
7223aab70afSSebastian Siewior 	} else {
7233aab70afSSebastian Siewior 		sprintf(response, "DATA%08x", download_size);
7243aab70afSSebastian Siewior 		req->complete = rx_handler_dl_image;
725ac484c5aSRoger Quadros 		req->length = rx_bytes_expected(ep);
7263aab70afSSebastian Siewior 	}
727367cce4dSXu Hongfei 
728367cce4dSXu Hongfei 	fastboot_tx_write_str(response);
729367cce4dSXu Hongfei }
730367cce4dSXu Hongfei 
731367cce4dSXu Hongfei static void tx_handler_ul(struct usb_ep *ep, struct usb_request *req)
732367cce4dSXu Hongfei {
733367cce4dSXu Hongfei 	unsigned int xfer_size = 0;
734367cce4dSXu Hongfei 	unsigned int pre_dot_num, now_dot_num;
735367cce4dSXu Hongfei 	unsigned int remain_size = 0;
736367cce4dSXu Hongfei 	unsigned int transferred_size = req->actual;
737367cce4dSXu Hongfei 
738367cce4dSXu Hongfei 	if (req->status != 0) {
739367cce4dSXu Hongfei 		printf("Bad status: %d\n", req->status);
740367cce4dSXu Hongfei 		return;
741367cce4dSXu Hongfei 	}
742367cce4dSXu Hongfei 
743367cce4dSXu Hongfei 	if (start_upload) {
744367cce4dSXu Hongfei 		pre_dot_num = upload_bytes / BYTES_PER_DOT;
745367cce4dSXu Hongfei 		upload_bytes += transferred_size;
746367cce4dSXu Hongfei 		now_dot_num = upload_bytes / BYTES_PER_DOT;
747367cce4dSXu Hongfei 
748367cce4dSXu Hongfei 		if (pre_dot_num != now_dot_num) {
749367cce4dSXu Hongfei 			putc('.');
750367cce4dSXu Hongfei 			if (!(now_dot_num % 74))
751367cce4dSXu Hongfei 				putc('\n');
752367cce4dSXu Hongfei 		}
753367cce4dSXu Hongfei 	}
754367cce4dSXu Hongfei 
755367cce4dSXu Hongfei 	remain_size = upload_size - upload_bytes;
756367cce4dSXu Hongfei 	xfer_size = (remain_size > EP_BUFFER_SIZE) ?
757367cce4dSXu Hongfei 		    EP_BUFFER_SIZE : remain_size;
758367cce4dSXu Hongfei 
759367cce4dSXu Hongfei 	debug("%s: remain_size=%d, transferred_size=%d",
760367cce4dSXu Hongfei 	      __func__, remain_size, transferred_size);
761367cce4dSXu Hongfei 	debug("xfer_size=%d, upload_bytes=%d, upload_size=%d!\n",
762367cce4dSXu Hongfei 	      xfer_size, upload_bytes, upload_size);
763367cce4dSXu Hongfei 
764367cce4dSXu Hongfei 	if (remain_size <= 0) {
765367cce4dSXu Hongfei 		fastboot_func->in_req->complete = fastboot_complete;
766367cce4dSXu Hongfei 		fastboot_tx_write_str("OKAY");
767367cce4dSXu Hongfei 		printf("\nuploading of %d bytes finished\n", upload_bytes);
768367cce4dSXu Hongfei 		upload_bytes = 0;
769367cce4dSXu Hongfei 		upload_size = 0;
770367cce4dSXu Hongfei 		start_upload = false;
771367cce4dSXu Hongfei 		return;
772367cce4dSXu Hongfei 	}
773367cce4dSXu Hongfei 
774367cce4dSXu Hongfei 	/* Remove the transfer callback which response the upload */
775367cce4dSXu Hongfei 	/* request from host */
776367cce4dSXu Hongfei 	if (!upload_bytes)
777367cce4dSXu Hongfei 		start_upload = true;
778367cce4dSXu Hongfei 
779*174b4544SKever Yang 	fastboot_tx_write((char *)((phys_addr_t)CONFIG_FASTBOOT_BUF_ADDR + \
780*174b4544SKever Yang 			  upload_bytes),
781367cce4dSXu Hongfei 			  xfer_size);
782367cce4dSXu Hongfei }
783367cce4dSXu Hongfei 
784367cce4dSXu Hongfei static void cb_upload(struct usb_ep *ep, struct usb_request *req)
785367cce4dSXu Hongfei {
786367cce4dSXu Hongfei 	char response[FASTBOOT_RESPONSE_LEN];
787367cce4dSXu Hongfei 
7884d0fc665SAndy Ye 
789367cce4dSXu Hongfei 
790367cce4dSXu Hongfei 	printf("Starting upload of %d bytes\n", upload_size);
791367cce4dSXu Hongfei 
792367cce4dSXu Hongfei 	if (0 == upload_size) {
793367cce4dSXu Hongfei 		strcpy(response, "FAILdata invalid size");
794367cce4dSXu Hongfei 	} else {
795367cce4dSXu Hongfei 		start_upload = false;
796367cce4dSXu Hongfei 		sprintf(response, "DATA%08x", upload_size);
797367cce4dSXu Hongfei 		fastboot_func->in_req->complete = tx_handler_ul;
798367cce4dSXu Hongfei 	}
799367cce4dSXu Hongfei 
8003aab70afSSebastian Siewior 	fastboot_tx_write_str(response);
8013aab70afSSebastian Siewior }
8023aab70afSSebastian Siewior 
8033aab70afSSebastian Siewior static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
8043aab70afSSebastian Siewior {
8053aab70afSSebastian Siewior 	char boot_addr_start[12];
8063aab70afSSebastian Siewior 	char *bootm_args[] = { "bootm", boot_addr_start, NULL };
8073aab70afSSebastian Siewior 
8083aab70afSSebastian Siewior 	puts("Booting kernel..\n");
8093aab70afSSebastian Siewior 
81090ae53ceSTom Rini 	sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR);
8113aab70afSSebastian Siewior 	do_bootm(NULL, 0, 2, bootm_args);
8123aab70afSSebastian Siewior 
8133aab70afSSebastian Siewior 	/* This only happens if image is somehow faulty so we start over */
8143aab70afSSebastian Siewior 	do_reset(NULL, 0, 0, NULL);
8153aab70afSSebastian Siewior }
8163aab70afSSebastian Siewior 
8173aab70afSSebastian Siewior static void cb_boot(struct usb_ep *ep, struct usb_request *req)
8183aab70afSSebastian Siewior {
8193aab70afSSebastian Siewior 	fastboot_func->in_req->complete = do_bootm_on_complete;
8203aab70afSSebastian Siewior 	fastboot_tx_write_str("OKAY");
8213aab70afSSebastian Siewior }
8223aab70afSSebastian Siewior 
823267abc62SRob Herring static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
824267abc62SRob Herring {
825267abc62SRob Herring 	g_dnl_trigger_detach();
826267abc62SRob Herring }
827267abc62SRob Herring 
828267abc62SRob Herring static void cb_continue(struct usb_ep *ep, struct usb_request *req)
829267abc62SRob Herring {
830267abc62SRob Herring 	fastboot_func->in_req->complete = do_exit_on_complete;
831267abc62SRob Herring 	fastboot_tx_write_str("OKAY");
832267abc62SRob Herring }
833267abc62SRob Herring 
834367cce4dSXu Hongfei static void cb_set_active(struct usb_ep *ep, struct usb_request *req)
835367cce4dSXu Hongfei {
836367cce4dSXu Hongfei 	char *cmd = req->buf;
837367cce4dSXu Hongfei 
838367cce4dSXu Hongfei 	debug("%s: %s\n", __func__, cmd);
839367cce4dSXu Hongfei 
840367cce4dSXu Hongfei 	strsep(&cmd, ":");
841367cce4dSXu Hongfei 	if (!cmd) {
84290aa625cSMasahiro Yamada 		pr_err("missing slot name");
843367cce4dSXu Hongfei 		fastboot_tx_write_str("FAIL: missing slot name");
844367cce4dSXu Hongfei 		return;
845367cce4dSXu Hongfei 	}
84637a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
847367cce4dSXu Hongfei 	unsigned int slot_number;
848367cce4dSXu Hongfei 	if (strncmp("a", cmd, 1) == 0) {
849367cce4dSXu Hongfei 		slot_number = 0;
85037a7bc39SJason Zhu 		rk_avb_set_slot_active(&slot_number);
851367cce4dSXu Hongfei 	} else if (strncmp("b", cmd, 1) == 0) {
852367cce4dSXu Hongfei 		slot_number = 1;
85337a7bc39SJason Zhu 		rk_avb_set_slot_active(&slot_number);
854367cce4dSXu Hongfei 	} else {
855367cce4dSXu Hongfei 		fastboot_tx_write_str("FAIL: unkown slot name");
856367cce4dSXu Hongfei 		return;
857367cce4dSXu Hongfei 	}
858367cce4dSXu Hongfei 
859367cce4dSXu Hongfei 	fastboot_tx_write_str("OKAY");
860367cce4dSXu Hongfei 	return;
861367cce4dSXu Hongfei #else
862367cce4dSXu Hongfei 	fastboot_tx_write_str("FAILnot implemented");
863367cce4dSXu Hongfei 	return;
864367cce4dSXu Hongfei #endif
865367cce4dSXu Hongfei }
866367cce4dSXu Hongfei 
867d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH
868d1b5ed07SSteve Rae static void cb_flash(struct usb_ep *ep, struct usb_request *req)
869d1b5ed07SSteve Rae {
870d1b5ed07SSteve Rae 	char *cmd = req->buf;
871374a9995SCody Xie 	char response[FASTBOOT_RESPONSE_LEN] = {0};
87237a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
8737bc1707dSJason Zhu 	uint8_t flash_lock_state;
874d1b5ed07SSteve Rae 
87537a7bc39SJason Zhu 	if (rk_avb_read_flash_lock_state(&flash_lock_state)) {
8767bc1707dSJason Zhu 		fastboot_tx_write_str("FAIL");
8777bc1707dSJason Zhu 		return;
878ef52a073SJason Zhu 	}
879ef52a073SJason Zhu 
8807bc1707dSJason Zhu 	if (flash_lock_state == 0) {
8817bc1707dSJason Zhu 		fastboot_tx_write_str("FAILThe device is locked, can not flash!");
8827bc1707dSJason Zhu 		printf("The device is locked, can not flash!\n");
8837bc1707dSJason Zhu 		return;
8847bc1707dSJason Zhu 	}
8857bc1707dSJason Zhu #endif
886d1b5ed07SSteve Rae 	strsep(&cmd, ":");
887d1b5ed07SSteve Rae 	if (!cmd) {
88890aa625cSMasahiro Yamada 		pr_err("missing partition name");
889d1b5ed07SSteve Rae 		fastboot_tx_write_str("FAILmissing partition name");
890d1b5ed07SSteve Rae 		return;
891d1b5ed07SSteve Rae 	}
892d1b5ed07SSteve Rae 
8938b464fa9SJocelyn Bohr 	fastboot_fail("no flash device defined", response);
894d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
89564ece848SSteve Rae 	fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
8968b464fa9SJocelyn Bohr 				download_bytes, response);
897d1b5ed07SSteve Rae #endif
898bf8940d3SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
8998b464fa9SJocelyn Bohr 	fb_nand_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
9008b464fa9SJocelyn Bohr 				download_bytes, response);
901bf8940d3SMaxime Ripard #endif
902d1b5ed07SSteve Rae 	fastboot_tx_write_str(response);
903d1b5ed07SSteve Rae }
904d1b5ed07SSteve Rae 
905367cce4dSXu Hongfei static void cb_flashing(struct usb_ep *ep, struct usb_request *req)
906367cce4dSXu Hongfei {
907367cce4dSXu Hongfei 	char *cmd = req->buf;
908367cce4dSXu Hongfei 
909367cce4dSXu Hongfei 	if (strncmp("lock", cmd + 9, 4) == 0) {
91037a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
9117bc1707dSJason Zhu 		uint8_t flash_lock_state;
9127bc1707dSJason Zhu 		flash_lock_state = 0;
91337a7bc39SJason Zhu 		if (rk_avb_write_flash_lock_state(flash_lock_state))
9147bc1707dSJason Zhu 			fastboot_tx_write_str("FAIL");
9157bc1707dSJason Zhu 		else
9167bc1707dSJason Zhu 			fastboot_tx_write_str("OKAY");
9177bc1707dSJason Zhu #else
918367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
9197bc1707dSJason Zhu #endif
920367cce4dSXu Hongfei 	} else if (strncmp("unlock", cmd + 9, 6) == 0) {
92137a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
9227bc1707dSJason Zhu 		uint8_t flash_lock_state;
9237bc1707dSJason Zhu 		flash_lock_state = 1;
92437a7bc39SJason Zhu 		if (rk_avb_write_flash_lock_state(flash_lock_state))
9257bc1707dSJason Zhu 			fastboot_tx_write_str("FAIL");
9267bc1707dSJason Zhu 		else
9277bc1707dSJason Zhu 			fastboot_tx_write_str("OKAY");
9287bc1707dSJason Zhu #else
929367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
9307bc1707dSJason Zhu #endif
931367cce4dSXu Hongfei 	} else if (strncmp("lock_critical", cmd + 9, 12) == 0) {
932367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
933367cce4dSXu Hongfei 	} else if (strncmp("unlock_critical", cmd + 9, 14) == 0) {
934367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
935367cce4dSXu Hongfei 	} else if (strncmp("get_unlock_ability", cmd + 9, 17) == 0) {
936367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
937367cce4dSXu Hongfei 	} else if (strncmp("get_unlock_bootloader_nonce", cmd + 4, 27) == 0) {
938367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
939367cce4dSXu Hongfei 	} else if (strncmp("unlock_bootloader", cmd + 9, 17) == 0) {
940367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
941367cce4dSXu Hongfei 	} else if (strncmp("lock_bootloader", cmd + 9, 15) == 0) {
942367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
943367cce4dSXu Hongfei 	} else {
944367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILunknown flashing command");
945367cce4dSXu Hongfei 	}
946367cce4dSXu Hongfei }
9476f3eb474SKever Yang #endif
948367cce4dSXu Hongfei 
949de195620SMichael Scott static void cb_oem(struct usb_ep *ep, struct usb_request *req)
950de195620SMichael Scott {
951de195620SMichael Scott 	char *cmd = req->buf;
952367cce4dSXu Hongfei 
9534adef270SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
954372d7decSRob Herring 	if (strncmp("format", cmd + 4, 6) == 0) {
955372d7decSRob Herring 		char cmdbuf[32];
956372d7decSRob Herring                 sprintf(cmdbuf, "gpt write mmc %x $partitions",
957372d7decSRob Herring 			CONFIG_FASTBOOT_FLASH_MMC_DEV);
958372d7decSRob Herring                 if (run_command(cmdbuf, 0))
959372d7decSRob Herring 			fastboot_tx_write_str("FAIL");
960372d7decSRob Herring                 else
961372d7decSRob Herring 			fastboot_tx_write_str("OKAY");
962372d7decSRob Herring 	} else
963372d7decSRob Herring #endif
964de195620SMichael Scott 	if (strncmp("unlock", cmd + 4, 8) == 0) {
965de195620SMichael Scott 		fastboot_tx_write_str("FAILnot implemented");
966367cce4dSXu Hongfei 	} else if (strncmp("at-get-ca-request", cmd + 4, 17) == 0) {
9674d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT
9684d0fc665SAndy Ye 		uint8_t operation_start[128];
9694d0fc665SAndy Ye 		uint8_t out[256];
9704d0fc665SAndy Ye 		uint32_t operation_size = download_bytes;
9714d0fc665SAndy Ye 		uint32_t out_len = 256;
9724d0fc665SAndy Ye 		uint32_t res = 0;
9734d0fc665SAndy Ye 		memcpy(operation_start, (void *)CONFIG_FASTBOOT_BUF_ADDR, download_bytes);
9744d0fc665SAndy Ye 		res = trusty_attest_get_ca(operation_start, &operation_size, out, &out_len);
9754d0fc665SAndy Ye 		if (res) {
9764d0fc665SAndy Ye 			fastboot_tx_write_str("FAILtrusty_attest_get_ca failed");
9774d0fc665SAndy Ye 			return;
9784d0fc665SAndy Ye 		}
9794d0fc665SAndy Ye 		upload_size = out_len;
9804d0fc665SAndy Ye 		memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR, out, out_len);
981367cce4dSXu Hongfei 		fastboot_tx_write_str("OKAY");
9824d0fc665SAndy Ye #else
9834d0fc665SAndy Ye 		fastboot_tx_write_str("FAILnot implemented");
9844d0fc665SAndy Ye 		return;
9854d0fc665SAndy Ye #endif
986367cce4dSXu Hongfei 	} else if (strncmp("at-set-ca-response", cmd + 4, 18) == 0) {
9874d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT
9884d0fc665SAndy Ye 		uint8_t ca_response[8*1024];
9894d0fc665SAndy Ye 		uint32_t ca_response_size = download_bytes;
9904d0fc665SAndy Ye 		uint32_t res = 0;
9914d0fc665SAndy Ye 		memcpy(ca_response, (void *)CONFIG_FASTBOOT_BUF_ADDR, download_bytes);
9924d0fc665SAndy Ye 		res = trusty_attest_set_ca(ca_response, &ca_response_size);
9934d0fc665SAndy Ye 		if (res) {
9944d0fc665SAndy Ye 			fastboot_tx_write_str("FAILtrusty_attest_set_ca failed");
9954d0fc665SAndy Ye 		} else {
996367cce4dSXu Hongfei 			fastboot_tx_write_str("OKAY");
9974d0fc665SAndy Ye 		}
9984d0fc665SAndy Ye #else
9994d0fc665SAndy Ye 		fastboot_tx_write_str("FAILnot implemented");
10004d0fc665SAndy Ye 		return;
10014d0fc665SAndy Ye #endif
1002367cce4dSXu Hongfei 	} else if (strncmp("at-lock-vboot", cmd + 4, 13) == 0) {
100337a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
1004d8bd6e97SJason Zhu 		uint8_t lock_state;
1005d8bd6e97SJason Zhu 		lock_state = 0;
100637a7bc39SJason Zhu 		if (rk_avb_write_lock_state(lock_state))
1007d8bd6e97SJason Zhu 			fastboot_tx_write_str("FAIL");
1008d8bd6e97SJason Zhu 		else
1009d8bd6e97SJason Zhu 			fastboot_tx_write_str("OKAY");
1010d8bd6e97SJason Zhu #else
1011367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
1012d8bd6e97SJason Zhu #endif
1013367cce4dSXu Hongfei 	} else if (strncmp("at-unlock-vboot", cmd + 4, 15) == 0) {
101437a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
1015d8bd6e97SJason Zhu 		uint8_t lock_state;
101637a7bc39SJason Zhu 		if (rk_avb_read_lock_state(&lock_state))
1017d8bd6e97SJason Zhu 			fastboot_tx_write_str("FAIL");
1018d8bd6e97SJason Zhu 		if (lock_state >> 1 == 1) {
1019d8bd6e97SJason Zhu 			fastboot_tx_write_str("FAILThe vboot is disable!");
1020d8bd6e97SJason Zhu 		} else {
1021d8bd6e97SJason Zhu 			lock_state = 1;
102237a7bc39SJason Zhu 			if (rk_avb_write_lock_state(lock_state))
1023d8bd6e97SJason Zhu 				fastboot_tx_write_str("FAIL");
1024d8bd6e97SJason Zhu 			else
1025d8bd6e97SJason Zhu 				fastboot_tx_write_str("OKAY");
1026d8bd6e97SJason Zhu 		}
1027d8bd6e97SJason Zhu #else
1028367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
1029d8bd6e97SJason Zhu #endif
1030367cce4dSXu Hongfei 	} else if (strncmp("at-disable-unlock-vboot", cmd + 4, 23) == 0) {
103137a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
1032d8bd6e97SJason Zhu 		uint8_t lock_state;
1033d8bd6e97SJason Zhu 		lock_state = 2;
103437a7bc39SJason Zhu 		if (rk_avb_write_lock_state(lock_state))
1035d8bd6e97SJason Zhu 			fastboot_tx_write_str("FAIL");
1036d8bd6e97SJason Zhu 		else
1037d8bd6e97SJason Zhu 			fastboot_tx_write_str("OKAY");
1038d8bd6e97SJason Zhu #else
1039367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
1040d8bd6e97SJason Zhu #endif
1041367cce4dSXu Hongfei 	} else if (strncmp("fuse at-perm-attr", cmd + 4, 16) == 0) {
104237a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
10430916e43bSJason Zhu 		if (PERM_ATTR_TOTAL_SIZE != download_bytes) {
10440916e43bSJason Zhu 			printf("Permanent attribute size is not equal!\n");
10450916e43bSJason Zhu 			fastboot_tx_write_str("FAIL");
10460916e43bSJason Zhu 			return;
10470916e43bSJason Zhu 		}
10480916e43bSJason Zhu 
104937a7bc39SJason Zhu 		if (rk_avb_write_permanent_attributes((uint8_t *)
10504f3cd37cSJason Zhu 					       CONFIG_FASTBOOT_BUF_ADDR,
10510916e43bSJason Zhu 					       download_bytes
10520916e43bSJason Zhu 					       - PERM_ATTR_DIGEST_SIZE)) {
10534f3cd37cSJason Zhu 			fastboot_tx_write_str("FAIL");
10540916e43bSJason Zhu 			return;
10550916e43bSJason Zhu 		}
10560916e43bSJason Zhu 
105737a7bc39SJason Zhu 		if (rk_avb_write_attribute_hash((uint8_t *)
10580916e43bSJason Zhu 					     (CONFIG_FASTBOOT_BUF_ADDR
10590916e43bSJason Zhu 					     + download_bytes
10600916e43bSJason Zhu 					     - PERM_ATTR_DIGEST_SIZE),
10610916e43bSJason Zhu 					     PERM_ATTR_DIGEST_SIZE)) {
10620916e43bSJason Zhu 			fastboot_tx_write_str("FAIL");
10630916e43bSJason Zhu 			return;
10640916e43bSJason Zhu 		}
10650916e43bSJason Zhu 
106637a7bc39SJason Zhu 		if (rk_avb_write_perm_attr_flag(1)) {
10670916e43bSJason Zhu 			fastboot_tx_write_str("FAIL");
10680916e43bSJason Zhu 			return;
10690916e43bSJason Zhu 		}
10700916e43bSJason Zhu 
10714f3cd37cSJason Zhu 		fastboot_tx_write_str("OKAY");
10724f3cd37cSJason Zhu #else
1073367cce4dSXu Hongfei 		fastboot_tx_write_str("FAILnot implemented");
10744f3cd37cSJason Zhu #endif
10754e1bbe84SJason Zhu 	} else if (strncmp("fuse at-bootloader-vboot-key", cmd + 4, 27) == 0) {
107637a7bc39SJason Zhu #ifdef CONFIG_RK_AVB_LIBAVB_USER
10774e1bbe84SJason Zhu 		if (download_bytes != VBOOT_KEY_HASH_SIZE) {
10784e1bbe84SJason Zhu 			fastboot_tx_write_str("FAIL");
10794e1bbe84SJason Zhu 			printf("The vboot key size error!\n");
10804e1bbe84SJason Zhu 		}
10814e1bbe84SJason Zhu 
108237a7bc39SJason Zhu 		if (rk_avb_write_vbootkey_hash((uint8_t *)
10834e1bbe84SJason Zhu 					    CONFIG_FASTBOOT_BUF_ADDR,
10844e1bbe84SJason Zhu 					    VBOOT_KEY_HASH_SIZE)) {
10854e1bbe84SJason Zhu 			fastboot_tx_write_str("FAIL");
10864e1bbe84SJason Zhu 			return;
10874e1bbe84SJason Zhu 		}
10884e1bbe84SJason Zhu 		fastboot_tx_write_str("OKAY");
10894e1bbe84SJason Zhu #else
10904e1bbe84SJason Zhu 		fastboot_tx_write_str("FAILnot implemented");
10914e1bbe84SJason Zhu #endif
1092367cce4dSXu Hongfei 	} else {
1093de195620SMichael Scott 		fastboot_tx_write_str("FAILunknown oem command");
1094de195620SMichael Scott 	}
1095de195620SMichael Scott }
1096de195620SMichael Scott 
109789792381SDileep Katta #ifdef CONFIG_FASTBOOT_FLASH
109889792381SDileep Katta static void cb_erase(struct usb_ep *ep, struct usb_request *req)
109989792381SDileep Katta {
110089792381SDileep Katta 	char *cmd = req->buf;
11013c8f98f5SMaxime Ripard 	char response[FASTBOOT_RESPONSE_LEN];
110289792381SDileep Katta 
110389792381SDileep Katta 	strsep(&cmd, ":");
110489792381SDileep Katta 	if (!cmd) {
110590aa625cSMasahiro Yamada 		pr_err("missing partition name");
110689792381SDileep Katta 		fastboot_tx_write_str("FAILmissing partition name");
110789792381SDileep Katta 		return;
110889792381SDileep Katta 	}
110989792381SDileep Katta 
11108b464fa9SJocelyn Bohr 	fastboot_fail("no flash device defined", response);
111189792381SDileep Katta #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
11128b464fa9SJocelyn Bohr 	fb_mmc_erase(cmd, response);
111389792381SDileep Katta #endif
1114bf8940d3SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
11158b464fa9SJocelyn Bohr 	fb_nand_erase(cmd, response);
1116bf8940d3SMaxime Ripard #endif
111789792381SDileep Katta 	fastboot_tx_write_str(response);
111889792381SDileep Katta }
111989792381SDileep Katta #endif
112089792381SDileep Katta 
11213aab70afSSebastian Siewior struct cmd_dispatch_info {
11223aab70afSSebastian Siewior 	char *cmd;
11233aab70afSSebastian Siewior 	void (*cb)(struct usb_ep *ep, struct usb_request *req);
11243aab70afSSebastian Siewior };
11253aab70afSSebastian Siewior 
11263aab70afSSebastian Siewior static const struct cmd_dispatch_info cmd_dispatch_info[] = {
11273aab70afSSebastian Siewior 	{
11283aab70afSSebastian Siewior 		.cmd = "reboot",
11293aab70afSSebastian Siewior 		.cb = cb_reboot,
11303aab70afSSebastian Siewior 	}, {
11313aab70afSSebastian Siewior 		.cmd = "getvar:",
11323aab70afSSebastian Siewior 		.cb = cb_getvar,
11333aab70afSSebastian Siewior 	}, {
11343aab70afSSebastian Siewior 		.cmd = "download:",
11353aab70afSSebastian Siewior 		.cb = cb_download,
11363aab70afSSebastian Siewior 	}, {
1137367cce4dSXu Hongfei 		.cmd = "upload",
1138367cce4dSXu Hongfei 		.cb = cb_upload,
1139367cce4dSXu Hongfei 	}, {
11403aab70afSSebastian Siewior 		.cmd = "boot",
11413aab70afSSebastian Siewior 		.cb = cb_boot,
1142267abc62SRob Herring 	}, {
1143267abc62SRob Herring 		.cmd = "continue",
1144267abc62SRob Herring 		.cb = cb_continue,
1145367cce4dSXu Hongfei 	}, {
1146367cce4dSXu Hongfei 		.cmd = "set_active",
1147367cce4dSXu Hongfei 		.cb = cb_set_active,
11483aab70afSSebastian Siewior 	},
1149d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH
1150d1b5ed07SSteve Rae 	{
1151367cce4dSXu Hongfei 		.cmd = "flashing",
1152367cce4dSXu Hongfei 		.cb = cb_flashing,
1153367cce4dSXu Hongfei 	},
1154367cce4dSXu Hongfei 	{
1155d1b5ed07SSteve Rae 		.cmd = "flash",
1156d1b5ed07SSteve Rae 		.cb = cb_flash,
115789792381SDileep Katta 	}, {
115889792381SDileep Katta 		.cmd = "erase",
115989792381SDileep Katta 		.cb = cb_erase,
1160d1b5ed07SSteve Rae 	},
1161d1b5ed07SSteve Rae #endif
1162de195620SMichael Scott 	{
1163de195620SMichael Scott 		.cmd = "oem",
1164de195620SMichael Scott 		.cb = cb_oem,
1165de195620SMichael Scott 	},
11663aab70afSSebastian Siewior };
11673aab70afSSebastian Siewior 
11683aab70afSSebastian Siewior static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
11693aab70afSSebastian Siewior {
11703aab70afSSebastian Siewior 	char *cmdbuf = req->buf;
11713aab70afSSebastian Siewior 	void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
11723aab70afSSebastian Siewior 	int i;
11733aab70afSSebastian Siewior 
117494b385faSPaul Kocialkowski 	if (req->status != 0 || req->length == 0)
117594b385faSPaul Kocialkowski 		return;
117694b385faSPaul Kocialkowski 
11773aab70afSSebastian Siewior 	for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
11783aab70afSSebastian Siewior 		if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
11793aab70afSSebastian Siewior 			func_cb = cmd_dispatch_info[i].cb;
11803aab70afSSebastian Siewior 			break;
11813aab70afSSebastian Siewior 		}
11823aab70afSSebastian Siewior 	}
11833aab70afSSebastian Siewior 
1184593cbd93SSteve Rae 	if (!func_cb) {
118590aa625cSMasahiro Yamada 		pr_err("unknown command: %.*s", req->actual, cmdbuf);
11863aab70afSSebastian Siewior 		fastboot_tx_write_str("FAILunknown command");
1187593cbd93SSteve Rae 	} else {
1188e2140588SEric Nelson 		if (req->actual < req->length) {
1189e2140588SEric Nelson 			u8 *buf = (u8 *)req->buf;
1190e2140588SEric Nelson 			buf[req->actual] = 0;
11913aab70afSSebastian Siewior 			func_cb(ep, req);
1192e2140588SEric Nelson 		} else {
119390aa625cSMasahiro Yamada 			pr_err("buffer overflow");
1194e2140588SEric Nelson 			fastboot_tx_write_str("FAILbuffer overflow");
1195e2140588SEric Nelson 		}
1196593cbd93SSteve Rae 	}
11973aab70afSSebastian Siewior 
11983aab70afSSebastian Siewior 	*cmdbuf = '\0';
11993aab70afSSebastian Siewior 	req->actual = 0;
12003aab70afSSebastian Siewior 	usb_ep_queue(ep, req, 0);
12013aab70afSSebastian Siewior }
1202