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 314d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT 324d0fc665SAndy Ye #include <optee_include/OpteeClientInterface.h> 334d0fc665SAndy Ye #endif 343aab70afSSebastian Siewior 353aab70afSSebastian Siewior #define FASTBOOT_VERSION "0.4" 363aab70afSSebastian Siewior 373aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_CLASS 0xff 383aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_SUB_CLASS 0x42 393aab70afSSebastian Siewior #define FASTBOOT_INTERFACE_PROTOCOL 0x03 403aab70afSSebastian Siewior 413aab70afSSebastian Siewior #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200) 423aab70afSSebastian Siewior #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040) 433aab70afSSebastian Siewior #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040) 443aab70afSSebastian Siewior 453aab70afSSebastian Siewior #define EP_BUFFER_SIZE 4096 46ac484c5aSRoger Quadros /* 47ac484c5aSRoger Quadros * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size 48ac484c5aSRoger Quadros * (64 or 512 or 1024), else we break on certain controllers like DWC3 49ac484c5aSRoger Quadros * that expect bulk OUT requests to be divisible by maxpacket size. 50ac484c5aSRoger Quadros */ 513aab70afSSebastian Siewior 523aab70afSSebastian Siewior struct f_fastboot { 533aab70afSSebastian Siewior struct usb_function usb_function; 543aab70afSSebastian Siewior 55593cbd93SSteve Rae /* IN/OUT EP's and corresponding requests */ 563aab70afSSebastian Siewior struct usb_ep *in_ep, *out_ep; 573aab70afSSebastian Siewior struct usb_request *in_req, *out_req; 583aab70afSSebastian Siewior }; 593aab70afSSebastian Siewior 603aab70afSSebastian Siewior static inline struct f_fastboot *func_to_fastboot(struct usb_function *f) 613aab70afSSebastian Siewior { 623aab70afSSebastian Siewior return container_of(f, struct f_fastboot, usb_function); 633aab70afSSebastian Siewior } 643aab70afSSebastian Siewior 653aab70afSSebastian Siewior static struct f_fastboot *fastboot_func; 663aab70afSSebastian Siewior static unsigned int download_size; 673aab70afSSebastian Siewior static unsigned int download_bytes; 68367cce4dSXu Hongfei static unsigned int upload_size; 69367cce4dSXu Hongfei static unsigned int upload_bytes; 70367cce4dSXu Hongfei static bool start_upload; 713aab70afSSebastian Siewior 723aab70afSSebastian Siewior static struct usb_endpoint_descriptor fs_ep_in = { 733aab70afSSebastian Siewior .bLength = USB_DT_ENDPOINT_SIZE, 743aab70afSSebastian Siewior .bDescriptorType = USB_DT_ENDPOINT, 753aab70afSSebastian Siewior .bEndpointAddress = USB_DIR_IN, 763aab70afSSebastian Siewior .bmAttributes = USB_ENDPOINT_XFER_BULK, 77718156adSRoger Quadros .wMaxPacketSize = cpu_to_le16(64), 783aab70afSSebastian Siewior }; 793aab70afSSebastian Siewior 803aab70afSSebastian Siewior static struct usb_endpoint_descriptor fs_ep_out = { 813aab70afSSebastian Siewior .bLength = USB_DT_ENDPOINT_SIZE, 823aab70afSSebastian Siewior .bDescriptorType = USB_DT_ENDPOINT, 833aab70afSSebastian Siewior .bEndpointAddress = USB_DIR_OUT, 843aab70afSSebastian Siewior .bmAttributes = USB_ENDPOINT_XFER_BULK, 85718156adSRoger Quadros .wMaxPacketSize = cpu_to_le16(64), 86718156adSRoger Quadros }; 87718156adSRoger Quadros 88718156adSRoger Quadros static struct usb_endpoint_descriptor hs_ep_in = { 89718156adSRoger Quadros .bLength = USB_DT_ENDPOINT_SIZE, 90718156adSRoger Quadros .bDescriptorType = USB_DT_ENDPOINT, 91718156adSRoger Quadros .bEndpointAddress = USB_DIR_IN, 92718156adSRoger Quadros .bmAttributes = USB_ENDPOINT_XFER_BULK, 93718156adSRoger Quadros .wMaxPacketSize = cpu_to_le16(512), 943aab70afSSebastian Siewior }; 953aab70afSSebastian Siewior 963aab70afSSebastian Siewior static struct usb_endpoint_descriptor hs_ep_out = { 973aab70afSSebastian Siewior .bLength = USB_DT_ENDPOINT_SIZE, 983aab70afSSebastian Siewior .bDescriptorType = USB_DT_ENDPOINT, 993aab70afSSebastian Siewior .bEndpointAddress = USB_DIR_OUT, 1003aab70afSSebastian Siewior .bmAttributes = USB_ENDPOINT_XFER_BULK, 101718156adSRoger Quadros .wMaxPacketSize = cpu_to_le16(512), 1023aab70afSSebastian Siewior }; 1033aab70afSSebastian Siewior 1043aab70afSSebastian Siewior static struct usb_interface_descriptor interface_desc = { 1053aab70afSSebastian Siewior .bLength = USB_DT_INTERFACE_SIZE, 1063aab70afSSebastian Siewior .bDescriptorType = USB_DT_INTERFACE, 1073aab70afSSebastian Siewior .bInterfaceNumber = 0x00, 1083aab70afSSebastian Siewior .bAlternateSetting = 0x00, 1093aab70afSSebastian Siewior .bNumEndpoints = 0x02, 1103aab70afSSebastian Siewior .bInterfaceClass = FASTBOOT_INTERFACE_CLASS, 1113aab70afSSebastian Siewior .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS, 1123aab70afSSebastian Siewior .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL, 1133aab70afSSebastian Siewior }; 1143aab70afSSebastian Siewior 115718156adSRoger Quadros static struct usb_descriptor_header *fb_fs_function[] = { 1163aab70afSSebastian Siewior (struct usb_descriptor_header *)&interface_desc, 1173aab70afSSebastian Siewior (struct usb_descriptor_header *)&fs_ep_in, 118718156adSRoger Quadros (struct usb_descriptor_header *)&fs_ep_out, 119718156adSRoger Quadros }; 120718156adSRoger Quadros 121718156adSRoger Quadros static struct usb_descriptor_header *fb_hs_function[] = { 122718156adSRoger Quadros (struct usb_descriptor_header *)&interface_desc, 123718156adSRoger Quadros (struct usb_descriptor_header *)&hs_ep_in, 1243aab70afSSebastian Siewior (struct usb_descriptor_header *)&hs_ep_out, 1253aab70afSSebastian Siewior NULL, 1263aab70afSSebastian Siewior }; 1273aab70afSSebastian Siewior 1288b704a0eSRoger Quadros static struct usb_endpoint_descriptor * 1298b704a0eSRoger Quadros fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, 1308b704a0eSRoger Quadros struct usb_endpoint_descriptor *hs) 1318b704a0eSRoger Quadros { 1328b704a0eSRoger Quadros if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) 1338b704a0eSRoger Quadros return hs; 1348b704a0eSRoger Quadros return fs; 1358b704a0eSRoger Quadros } 1368b704a0eSRoger Quadros 1373aab70afSSebastian Siewior /* 1383aab70afSSebastian Siewior * static strings, in UTF-8 1393aab70afSSebastian Siewior */ 1403aab70afSSebastian Siewior static const char fastboot_name[] = "Android Fastboot"; 1413aab70afSSebastian Siewior 1423aab70afSSebastian Siewior static struct usb_string fastboot_string_defs[] = { 1433aab70afSSebastian Siewior [0].s = fastboot_name, 1443aab70afSSebastian Siewior { } /* end of list */ 1453aab70afSSebastian Siewior }; 1463aab70afSSebastian Siewior 1473aab70afSSebastian Siewior static struct usb_gadget_strings stringtab_fastboot = { 1483aab70afSSebastian Siewior .language = 0x0409, /* en-us */ 1493aab70afSSebastian Siewior .strings = fastboot_string_defs, 1503aab70afSSebastian Siewior }; 1513aab70afSSebastian Siewior 1523aab70afSSebastian Siewior static struct usb_gadget_strings *fastboot_strings[] = { 1533aab70afSSebastian Siewior &stringtab_fastboot, 1543aab70afSSebastian Siewior NULL, 1553aab70afSSebastian Siewior }; 1563aab70afSSebastian Siewior 1573aab70afSSebastian Siewior static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); 158e2ec3e46SAlexey Firago static int strcmp_l1(const char *s1, const char *s2); 1593aab70afSSebastian Siewior 1603aab70afSSebastian Siewior static void fastboot_complete(struct usb_ep *ep, struct usb_request *req) 1613aab70afSSebastian Siewior { 1623aab70afSSebastian Siewior int status = req->status; 1633aab70afSSebastian Siewior if (!status) 1643aab70afSSebastian Siewior return; 1653aab70afSSebastian Siewior printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual); 1663aab70afSSebastian Siewior } 1673aab70afSSebastian Siewior 1683aab70afSSebastian Siewior static int fastboot_bind(struct usb_configuration *c, struct usb_function *f) 1693aab70afSSebastian Siewior { 1703aab70afSSebastian Siewior int id; 1713aab70afSSebastian Siewior struct usb_gadget *gadget = c->cdev->gadget; 1723aab70afSSebastian Siewior struct f_fastboot *f_fb = func_to_fastboot(f); 173537cd072SDileep Katta const char *s; 1743aab70afSSebastian Siewior 1753aab70afSSebastian Siewior /* DYNAMIC interface numbers assignments */ 1763aab70afSSebastian Siewior id = usb_interface_id(c, f); 1773aab70afSSebastian Siewior if (id < 0) 1783aab70afSSebastian Siewior return id; 1793aab70afSSebastian Siewior interface_desc.bInterfaceNumber = id; 1803aab70afSSebastian Siewior 1813aab70afSSebastian Siewior id = usb_string_id(c->cdev); 1823aab70afSSebastian Siewior if (id < 0) 1833aab70afSSebastian Siewior return id; 1843aab70afSSebastian Siewior fastboot_string_defs[0].id = id; 1853aab70afSSebastian Siewior interface_desc.iInterface = id; 1863aab70afSSebastian Siewior 1873aab70afSSebastian Siewior f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in); 1883aab70afSSebastian Siewior if (!f_fb->in_ep) 1893aab70afSSebastian Siewior return -ENODEV; 1903aab70afSSebastian Siewior f_fb->in_ep->driver_data = c->cdev; 1913aab70afSSebastian Siewior 1923aab70afSSebastian Siewior f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out); 1933aab70afSSebastian Siewior if (!f_fb->out_ep) 1943aab70afSSebastian Siewior return -ENODEV; 1953aab70afSSebastian Siewior f_fb->out_ep->driver_data = c->cdev; 1963aab70afSSebastian Siewior 197718156adSRoger Quadros f->descriptors = fb_fs_function; 198718156adSRoger Quadros 199718156adSRoger Quadros if (gadget_is_dualspeed(gadget)) { 200718156adSRoger Quadros /* Assume endpoint addresses are the same for both speeds */ 201718156adSRoger Quadros hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; 2023aab70afSSebastian Siewior hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; 203718156adSRoger Quadros /* copy HS descriptors */ 204718156adSRoger Quadros f->hs_descriptors = fb_hs_function; 205718156adSRoger Quadros } 2063aab70afSSebastian Siewior 20700caae6dSSimon Glass s = env_get("serial#"); 208537cd072SDileep Katta if (s) 209537cd072SDileep Katta g_dnl_set_serialnumber((char *)s); 210537cd072SDileep Katta 2113aab70afSSebastian Siewior return 0; 2123aab70afSSebastian Siewior } 2133aab70afSSebastian Siewior 2143aab70afSSebastian Siewior static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f) 2153aab70afSSebastian Siewior { 2163aab70afSSebastian Siewior memset(fastboot_func, 0, sizeof(*fastboot_func)); 2173aab70afSSebastian Siewior } 2183aab70afSSebastian Siewior 2193aab70afSSebastian Siewior static void fastboot_disable(struct usb_function *f) 2203aab70afSSebastian Siewior { 2213aab70afSSebastian Siewior struct f_fastboot *f_fb = func_to_fastboot(f); 2223aab70afSSebastian Siewior 2233aab70afSSebastian Siewior usb_ep_disable(f_fb->out_ep); 2243aab70afSSebastian Siewior usb_ep_disable(f_fb->in_ep); 2253aab70afSSebastian Siewior 2263aab70afSSebastian Siewior if (f_fb->out_req) { 2273aab70afSSebastian Siewior free(f_fb->out_req->buf); 2283aab70afSSebastian Siewior usb_ep_free_request(f_fb->out_ep, f_fb->out_req); 2293aab70afSSebastian Siewior f_fb->out_req = NULL; 2303aab70afSSebastian Siewior } 2313aab70afSSebastian Siewior if (f_fb->in_req) { 2323aab70afSSebastian Siewior free(f_fb->in_req->buf); 2333aab70afSSebastian Siewior usb_ep_free_request(f_fb->in_ep, f_fb->in_req); 2343aab70afSSebastian Siewior f_fb->in_req = NULL; 2353aab70afSSebastian Siewior } 2363aab70afSSebastian Siewior } 2373aab70afSSebastian Siewior 2383aab70afSSebastian Siewior static struct usb_request *fastboot_start_ep(struct usb_ep *ep) 2393aab70afSSebastian Siewior { 2403aab70afSSebastian Siewior struct usb_request *req; 2413aab70afSSebastian Siewior 2423aab70afSSebastian Siewior req = usb_ep_alloc_request(ep, 0); 2433aab70afSSebastian Siewior if (!req) 2443aab70afSSebastian Siewior return NULL; 2453aab70afSSebastian Siewior 2463aab70afSSebastian Siewior req->length = EP_BUFFER_SIZE; 2473aab70afSSebastian Siewior req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE); 2483aab70afSSebastian Siewior if (!req->buf) { 2493aab70afSSebastian Siewior usb_ep_free_request(ep, req); 2503aab70afSSebastian Siewior return NULL; 2513aab70afSSebastian Siewior } 2523aab70afSSebastian Siewior 2533aab70afSSebastian Siewior memset(req->buf, 0, req->length); 2543aab70afSSebastian Siewior return req; 2553aab70afSSebastian Siewior } 2563aab70afSSebastian Siewior 2573aab70afSSebastian Siewior static int fastboot_set_alt(struct usb_function *f, 2583aab70afSSebastian Siewior unsigned interface, unsigned alt) 2593aab70afSSebastian Siewior { 2603aab70afSSebastian Siewior int ret; 2613aab70afSSebastian Siewior struct usb_composite_dev *cdev = f->config->cdev; 2623aab70afSSebastian Siewior struct usb_gadget *gadget = cdev->gadget; 2633aab70afSSebastian Siewior struct f_fastboot *f_fb = func_to_fastboot(f); 2648b704a0eSRoger Quadros const struct usb_endpoint_descriptor *d; 2653aab70afSSebastian Siewior 2663aab70afSSebastian Siewior debug("%s: func: %s intf: %d alt: %d\n", 2673aab70afSSebastian Siewior __func__, f->name, interface, alt); 2683aab70afSSebastian Siewior 2698b704a0eSRoger Quadros d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out); 2708b704a0eSRoger Quadros ret = usb_ep_enable(f_fb->out_ep, d); 2713aab70afSSebastian Siewior if (ret) { 2723aab70afSSebastian Siewior puts("failed to enable out ep\n"); 2733aab70afSSebastian Siewior return ret; 2743aab70afSSebastian Siewior } 2753aab70afSSebastian Siewior 2763aab70afSSebastian Siewior f_fb->out_req = fastboot_start_ep(f_fb->out_ep); 2773aab70afSSebastian Siewior if (!f_fb->out_req) { 2783aab70afSSebastian Siewior puts("failed to alloc out req\n"); 2793aab70afSSebastian Siewior ret = -EINVAL; 2803aab70afSSebastian Siewior goto err; 2813aab70afSSebastian Siewior } 2823aab70afSSebastian Siewior f_fb->out_req->complete = rx_handler_command; 2833aab70afSSebastian Siewior 2848b704a0eSRoger Quadros d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in); 2858b704a0eSRoger Quadros ret = usb_ep_enable(f_fb->in_ep, d); 2863aab70afSSebastian Siewior if (ret) { 2873aab70afSSebastian Siewior puts("failed to enable in ep\n"); 2883aab70afSSebastian Siewior goto err; 2893aab70afSSebastian Siewior } 2903aab70afSSebastian Siewior 2913aab70afSSebastian Siewior f_fb->in_req = fastboot_start_ep(f_fb->in_ep); 2923aab70afSSebastian Siewior if (!f_fb->in_req) { 2933aab70afSSebastian Siewior puts("failed alloc req in\n"); 2943aab70afSSebastian Siewior ret = -EINVAL; 2953aab70afSSebastian Siewior goto err; 2963aab70afSSebastian Siewior } 2973aab70afSSebastian Siewior f_fb->in_req->complete = fastboot_complete; 2983aab70afSSebastian Siewior 2993aab70afSSebastian Siewior ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0); 3003aab70afSSebastian Siewior if (ret) 3013aab70afSSebastian Siewior goto err; 3023aab70afSSebastian Siewior 3033aab70afSSebastian Siewior return 0; 3043aab70afSSebastian Siewior err: 3053aab70afSSebastian Siewior fastboot_disable(f); 3063aab70afSSebastian Siewior return ret; 3073aab70afSSebastian Siewior } 3083aab70afSSebastian Siewior 3093aab70afSSebastian Siewior static int fastboot_add(struct usb_configuration *c) 3103aab70afSSebastian Siewior { 3113aab70afSSebastian Siewior struct f_fastboot *f_fb = fastboot_func; 3123aab70afSSebastian Siewior int status; 3133aab70afSSebastian Siewior 3143aab70afSSebastian Siewior debug("%s: cdev: 0x%p\n", __func__, c->cdev); 3153aab70afSSebastian Siewior 3163aab70afSSebastian Siewior if (!f_fb) { 3173aab70afSSebastian Siewior f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb)); 3183aab70afSSebastian Siewior if (!f_fb) 3193aab70afSSebastian Siewior return -ENOMEM; 3203aab70afSSebastian Siewior 3213aab70afSSebastian Siewior fastboot_func = f_fb; 3223aab70afSSebastian Siewior memset(f_fb, 0, sizeof(*f_fb)); 3233aab70afSSebastian Siewior } 3243aab70afSSebastian Siewior 3253aab70afSSebastian Siewior f_fb->usb_function.name = "f_fastboot"; 3263aab70afSSebastian Siewior f_fb->usb_function.bind = fastboot_bind; 3273aab70afSSebastian Siewior f_fb->usb_function.unbind = fastboot_unbind; 3283aab70afSSebastian Siewior f_fb->usb_function.set_alt = fastboot_set_alt; 3293aab70afSSebastian Siewior f_fb->usb_function.disable = fastboot_disable; 3303aab70afSSebastian Siewior f_fb->usb_function.strings = fastboot_strings; 3313aab70afSSebastian Siewior 3323aab70afSSebastian Siewior status = usb_add_function(c, &f_fb->usb_function); 3333aab70afSSebastian Siewior if (status) { 3343aab70afSSebastian Siewior free(f_fb); 3353aab70afSSebastian Siewior fastboot_func = f_fb; 3363aab70afSSebastian Siewior } 3373aab70afSSebastian Siewior 3383aab70afSSebastian Siewior return status; 3393aab70afSSebastian Siewior } 3403aab70afSSebastian Siewior DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add); 3413aab70afSSebastian Siewior 342593cbd93SSteve Rae static int fastboot_tx_write(const char *buffer, unsigned int buffer_size) 3433aab70afSSebastian Siewior { 3443aab70afSSebastian Siewior struct usb_request *in_req = fastboot_func->in_req; 3453aab70afSSebastian Siewior int ret; 3463aab70afSSebastian Siewior 3473aab70afSSebastian Siewior memcpy(in_req->buf, buffer, buffer_size); 3483aab70afSSebastian Siewior in_req->length = buffer_size; 349bc9071c9SPaul Kocialkowski 350bc9071c9SPaul Kocialkowski usb_ep_dequeue(fastboot_func->in_ep, in_req); 351bc9071c9SPaul Kocialkowski 3523aab70afSSebastian Siewior ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0); 3533aab70afSSebastian Siewior if (ret) 3543aab70afSSebastian Siewior printf("Error %d on queue\n", ret); 3553aab70afSSebastian Siewior return 0; 3563aab70afSSebastian Siewior } 3573aab70afSSebastian Siewior 3583aab70afSSebastian Siewior static int fastboot_tx_write_str(const char *buffer) 3593aab70afSSebastian Siewior { 3603aab70afSSebastian Siewior return fastboot_tx_write(buffer, strlen(buffer)); 3613aab70afSSebastian Siewior } 3623aab70afSSebastian Siewior 3633aab70afSSebastian Siewior static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) 3643aab70afSSebastian Siewior { 3653aab70afSSebastian Siewior do_reset(NULL, 0, 0, NULL); 3663aab70afSSebastian Siewior } 3673aab70afSSebastian Siewior 368e2ec3e46SAlexey Firago int __weak fb_set_reboot_flag(void) 369e2ec3e46SAlexey Firago { 370e2ec3e46SAlexey Firago return -ENOSYS; 371e2ec3e46SAlexey Firago } 372e2ec3e46SAlexey Firago 3733aab70afSSebastian Siewior static void cb_reboot(struct usb_ep *ep, struct usb_request *req) 3743aab70afSSebastian Siewior { 375e2ec3e46SAlexey Firago char *cmd = req->buf; 376e2ec3e46SAlexey Firago if (!strcmp_l1("reboot-bootloader", cmd)) { 377e2ec3e46SAlexey Firago if (fb_set_reboot_flag()) { 378e2ec3e46SAlexey Firago fastboot_tx_write_str("FAILCannot set reboot flag"); 379e2ec3e46SAlexey Firago return; 380e2ec3e46SAlexey Firago } 381e2ec3e46SAlexey Firago } 3823aab70afSSebastian Siewior fastboot_func->in_req->complete = compl_do_reset; 3833aab70afSSebastian Siewior fastboot_tx_write_str("OKAY"); 3843aab70afSSebastian Siewior } 3853aab70afSSebastian Siewior 3863aab70afSSebastian Siewior static int strcmp_l1(const char *s1, const char *s2) 3873aab70afSSebastian Siewior { 3883aab70afSSebastian Siewior if (!s1 || !s2) 3893aab70afSSebastian Siewior return -1; 3903aab70afSSebastian Siewior return strncmp(s1, s2, strlen(s1)); 3913aab70afSSebastian Siewior } 3923aab70afSSebastian Siewior 3933aab70afSSebastian Siewior static void cb_getvar(struct usb_ep *ep, struct usb_request *req) 3943aab70afSSebastian Siewior { 3953aab70afSSebastian Siewior char *cmd = req->buf; 3963c8f98f5SMaxime Ripard char response[FASTBOOT_RESPONSE_LEN]; 3973aab70afSSebastian Siewior const char *s; 39829425be4SJeroen Hofstee size_t chars_left; 3993aab70afSSebastian Siewior 4003aab70afSSebastian Siewior strcpy(response, "OKAY"); 40129425be4SJeroen Hofstee chars_left = sizeof(response) - strlen(response) - 1; 40229425be4SJeroen Hofstee 4033aab70afSSebastian Siewior strsep(&cmd, ":"); 4043aab70afSSebastian Siewior if (!cmd) { 405*90aa625cSMasahiro Yamada pr_err("missing variable"); 4063aab70afSSebastian Siewior fastboot_tx_write_str("FAILmissing var"); 4073aab70afSSebastian Siewior return; 4083aab70afSSebastian Siewior } 4093aab70afSSebastian Siewior 4103aab70afSSebastian Siewior if (!strcmp_l1("version", cmd)) { 41129425be4SJeroen Hofstee strncat(response, FASTBOOT_VERSION, chars_left); 4123aab70afSSebastian Siewior } else if (!strcmp_l1("bootloader-version", cmd)) { 41329425be4SJeroen Hofstee strncat(response, U_BOOT_VERSION, chars_left); 414374a9995SCody Xie } else if (!strcmp_l1("product", cmd)) { 415374a9995SCody Xie strncat(response, CONFIG_SYS_BOARD, chars_left); 416374a9995SCody Xie } else if (!strcmp_l1("variant", cmd)) { 417374a9995SCody Xie strncat(response, "userdebug", chars_left); 418374a9995SCody Xie } else if (!strcmp_l1("secure", cmd)) { 419374a9995SCody Xie strncat(response, "no", chars_left); 420374a9995SCody Xie } else if (!strcmp_l1("unlocked", cmd)) { 421374a9995SCody Xie strncat(response, "yes", chars_left); 422374a9995SCody Xie } else if (!strcmp_l1("off-mode-charge", cmd)) { 423374a9995SCody Xie strncat(response, "0", chars_left); 424374a9995SCody Xie } else if (!strcmp_l1("battery-voltage", cmd)) { 425374a9995SCody Xie strncat(response, "7.4", chars_left); 426374a9995SCody Xie } else if (!strcmp_l1("battery-soc-ok", cmd)) { 427374a9995SCody Xie strncat(response, "yes", chars_left); 428c674a666SEric Nelson } else if (!strcmp_l1("downloadsize", cmd) || 429c674a666SEric Nelson !strcmp_l1("max-download-size", cmd)) { 4303aab70afSSebastian Siewior char str_num[12]; 4313aab70afSSebastian Siewior 432a588d99aSPaul Kocialkowski sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE); 43329425be4SJeroen Hofstee strncat(response, str_num, chars_left); 4343aab70afSSebastian Siewior } else if (!strcmp_l1("serialno", cmd)) { 43500caae6dSSimon Glass s = env_get("serial#"); 4363aab70afSSebastian Siewior if (s) 43729425be4SJeroen Hofstee strncat(response, s, chars_left); 4383aab70afSSebastian Siewior else 4393aab70afSSebastian Siewior strcpy(response, "FAILValue not set"); 440367cce4dSXu Hongfei } else if (strncmp("at-attest-dh", cmd, 12) == 0) { 4414d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT 4424d0fc665SAndy Ye char dhbuf[8]; 4434d0fc665SAndy Ye uint32_t dh_len = 8; 4444d0fc665SAndy Ye uint32_t res = trusty_attest_dh((uint8_t *)dhbuf, &dh_len); 4454d0fc665SAndy Ye if (res) 4464d0fc665SAndy Ye strcpy(response, "FAILdh not set"); 4474d0fc665SAndy Ye else 4484d0fc665SAndy Ye strncat(response, dhbuf, chars_left); 4494d0fc665SAndy Ye #else 4504d0fc665SAndy Ye fastboot_tx_write_str("FAILnot implemented"); 4514d0fc665SAndy Ye return; 4524d0fc665SAndy Ye #endif 453367cce4dSXu Hongfei } else if (strncmp("at-attest-uuid", cmd, 14) == 0) { 4544d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT 455367cce4dSXu Hongfei char uuid[32] = {0}; 4564d0fc665SAndy Ye uint32_t uuid_len = 32; 4574d0fc665SAndy Ye uint32_t res = trusty_attest_uuid((uint8_t *)uuid, &uuid_len); 4584d0fc665SAndy Ye if (res) 4594d0fc665SAndy Ye strcpy(response, "FAILuuid not set"); 4604d0fc665SAndy Ye else 461367cce4dSXu Hongfei strncat(response, uuid, chars_left); 4624d0fc665SAndy Ye #else 4634d0fc665SAndy Ye fastboot_tx_write_str("FAILnot implemented"); 4644d0fc665SAndy Ye return; 4654d0fc665SAndy Ye #endif 466367cce4dSXu Hongfei } else if (strncmp("at-vboot-state", cmd, 14) == 0) { 467367cce4dSXu Hongfei char uuid[32] = {0}; 468367cce4dSXu Hongfei 469367cce4dSXu Hongfei strncat(response, uuid, chars_left); 470367cce4dSXu Hongfei } else if (!strcmp_l1("slot-count", cmd)) { 471367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER 472367cce4dSXu Hongfei char slot_count[2]; 473367cce4dSXu Hongfei char temp; 474367cce4dSXu Hongfei 475367cce4dSXu Hongfei slot_count[1] = '\0'; 4765b090159SJason Zhu avb_read_slot_count(&temp); 477367cce4dSXu Hongfei slot_count[0] = temp + 0x30; 478367cce4dSXu Hongfei strncat(response, slot_count, chars_left); 479367cce4dSXu Hongfei #else 480367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 481367cce4dSXu Hongfei return; 482367cce4dSXu Hongfei #endif 483367cce4dSXu Hongfei } else if (!strcmp_l1("current-slot", cmd)) { 484367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER 485367cce4dSXu Hongfei char slot_surrent[8] = {0}; 486367cce4dSXu Hongfei 4875b090159SJason Zhu if (!avb_get_current_slot(slot_surrent)) 488374a9995SCody Xie strncat(response, slot_surrent+1, chars_left); 489367cce4dSXu Hongfei else 490367cce4dSXu Hongfei strcpy(response, "FAILgeterror"); 491367cce4dSXu Hongfei #else 492367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 493367cce4dSXu Hongfei return; 494367cce4dSXu Hongfei #endif 495367cce4dSXu Hongfei } else if (!strcmp_l1("slot-suffixes", cmd)) { 496367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER 497367cce4dSXu Hongfei char slot_suffixes_temp[4]; 498367cce4dSXu Hongfei char slot_suffixes[9]; 499367cce4dSXu Hongfei int slot_cnt = 0; 500367cce4dSXu Hongfei 501367cce4dSXu Hongfei memset(slot_suffixes_temp, 0, 4); 502367cce4dSXu Hongfei memset(slot_suffixes, 0, 9); 5035b090159SJason Zhu avb_read_slot_suffixes(slot_suffixes_temp); 504367cce4dSXu Hongfei while (slot_suffixes_temp[slot_cnt] != '\0') { 505367cce4dSXu Hongfei slot_suffixes[slot_cnt * 2] 506367cce4dSXu Hongfei = slot_suffixes_temp[slot_cnt]; 507367cce4dSXu Hongfei slot_suffixes[slot_cnt * 2 + 1] = ','; 508367cce4dSXu Hongfei slot_cnt++; 509367cce4dSXu Hongfei } 510367cce4dSXu Hongfei strncat(response, slot_suffixes, chars_left); 511367cce4dSXu Hongfei #else 512367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 513367cce4dSXu Hongfei return; 514367cce4dSXu Hongfei #endif 515367cce4dSXu Hongfei } else if (!strncmp("has-slot", cmd, 8)) { 516367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER 517367cce4dSXu Hongfei char *part_name = cmd; 518367cce4dSXu Hongfei 519367cce4dSXu Hongfei cmd = strsep(&part_name, ":"); 520367cce4dSXu Hongfei if (!strcmp(part_name, "boot") || 521367cce4dSXu Hongfei !strcmp(part_name, "system") || 522374a9995SCody Xie !strcmp(part_name, "vendor") || 523374a9995SCody Xie !strcmp(part_name, "vbmeta") || 524374a9995SCody Xie !strcmp(part_name, "oem")) { 525367cce4dSXu Hongfei strncat(response, "yes", chars_left); 526367cce4dSXu Hongfei } else { 527367cce4dSXu Hongfei strcpy(response, "FAILno"); 528367cce4dSXu Hongfei } 529367cce4dSXu Hongfei #else 530367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 531367cce4dSXu Hongfei return; 532367cce4dSXu Hongfei #endif 533374a9995SCody Xie } else if (!strncmp("slot-unbootable", cmd, 15)) { 534374a9995SCody Xie #ifdef CONFIG_AVB_LIBAVB_USER 535374a9995SCody Xie char *slot_name = cmd; 536374a9995SCody Xie 537374a9995SCody Xie cmd = strsep(&slot_name, ":"); 538374a9995SCody Xie if (!strcmp(slot_name, "a") || 539374a9995SCody Xie !strcmp(slot_name, "b")) { 540374a9995SCody Xie strncat(response, "no", chars_left); 541374a9995SCody Xie } else { 542374a9995SCody Xie strcpy(response, "FAILno"); 543374a9995SCody Xie } 544374a9995SCody Xie #else 545374a9995SCody Xie fastboot_tx_write_str("FAILnot implemented"); 546374a9995SCody Xie return; 547374a9995SCody Xie #endif 548374a9995SCody Xie } else if (!strncmp("slot-successful", cmd, 15)) { 549374a9995SCody Xie #ifdef CONFIG_AVB_LIBAVB_USER 550374a9995SCody Xie char *slot_name = cmd; 551374a9995SCody Xie 552374a9995SCody Xie cmd = strsep(&slot_name, ":"); 553374a9995SCody Xie if (!strcmp(slot_name, "a") || 554374a9995SCody Xie !strcmp(slot_name, "b")) { 555374a9995SCody Xie strncat(response, "no", chars_left); 556374a9995SCody Xie } else { 557374a9995SCody Xie strcpy(response, "FAILno"); 558374a9995SCody Xie } 559374a9995SCody Xie #else 560374a9995SCody Xie fastboot_tx_write_str("FAILnot implemented"); 561374a9995SCody Xie return; 562374a9995SCody Xie #endif 563374a9995SCody Xie } else if (!strncmp("slot-retry-count", cmd, 16)) { 564374a9995SCody Xie #ifdef CONFIG_AVB_LIBAVB_USER 565374a9995SCody Xie char *slot_name = cmd; 566374a9995SCody Xie char count[10] = {0}; 567374a9995SCody Xie static int cnt[2] = {0}; 568374a9995SCody Xie 569374a9995SCody Xie cmd = strsep(&slot_name, ":"); 570374a9995SCody Xie if (!strcmp(slot_name, "a")) { 571374a9995SCody Xie sprintf(count, "%c", 0x30+cnt[0]); 572374a9995SCody Xie strncat(response, count, chars_left); 573374a9995SCody Xie if (cnt[0] > 0) 574374a9995SCody Xie cnt[0]--; 575374a9995SCody Xie } else if (!strcmp(slot_name, "b")) { 576374a9995SCody Xie sprintf(count, "%c", 0x30+cnt[1]); 577374a9995SCody Xie strncat(response, count, chars_left); 578374a9995SCody Xie if (cnt[1] > 0) 579374a9995SCody Xie cnt[1]--; 580374a9995SCody Xie } else { 581374a9995SCody Xie strcpy(response, "FAILno"); 582374a9995SCody Xie } 583374a9995SCody Xie #else 584374a9995SCody Xie fastboot_tx_write_str("FAILnot implemented"); 585374a9995SCody Xie return; 586374a9995SCody Xie #endif 587367cce4dSXu Hongfei } else if (!strncmp("partition-type", cmd, 14) || 588367cce4dSXu Hongfei !strncmp("partition-size", cmd, 14)) { 589367cce4dSXu Hongfei disk_partition_t part_info; 590367cce4dSXu Hongfei struct blk_desc *dev_desc; 591367cce4dSXu Hongfei char *part_name = cmd; 592367cce4dSXu Hongfei char part_size_str[20]; 593367cce4dSXu Hongfei 594367cce4dSXu Hongfei cmd = strsep(&part_name, ":"); 595367cce4dSXu Hongfei dev_desc = blk_get_dev("mmc", 0); 596367cce4dSXu Hongfei if (!dev_desc) { 597367cce4dSXu Hongfei strcpy(response, "FAILblock device not found"); 598367cce4dSXu Hongfei } else if (part_get_info_by_name(dev_desc, part_name, &part_info) < 0) { 599367cce4dSXu Hongfei strcpy(response, "FAILpartition not found"); 600367cce4dSXu Hongfei } else if (!strncmp("partition-type", cmd, 14)) { 601367cce4dSXu Hongfei strncat(response, (char *)part_info.type, chars_left); 602367cce4dSXu Hongfei } else if (!strncmp("partition-size", cmd, 14)) { 603367cce4dSXu Hongfei sprintf(part_size_str, "0x%016x", (int)part_info.size); 604367cce4dSXu Hongfei strncat(response, part_size_str, chars_left); 605367cce4dSXu Hongfei } 6063aab70afSSebastian Siewior } else { 607b1f2a17cSnicolas.le.bayon@st.com char *envstr; 60874322201SRob Herring 609b1f2a17cSnicolas.le.bayon@st.com envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1); 610b1f2a17cSnicolas.le.bayon@st.com if (!envstr) { 611b1f2a17cSnicolas.le.bayon@st.com fastboot_tx_write_str("FAILmalloc error"); 612b1f2a17cSnicolas.le.bayon@st.com return; 613b1f2a17cSnicolas.le.bayon@st.com } 614b1f2a17cSnicolas.le.bayon@st.com 615b1f2a17cSnicolas.le.bayon@st.com sprintf(envstr, "fastboot.%s", cmd); 61600caae6dSSimon Glass s = env_get(envstr); 61774322201SRob Herring if (s) { 61874322201SRob Herring strncat(response, s, chars_left); 61974322201SRob Herring } else { 620a18c2706SSteve Rae printf("WARNING: unknown variable: %s\n", cmd); 6213aab70afSSebastian Siewior strcpy(response, "FAILVariable not implemented"); 6223aab70afSSebastian Siewior } 623b1f2a17cSnicolas.le.bayon@st.com 624b1f2a17cSnicolas.le.bayon@st.com free(envstr); 62574322201SRob Herring } 6263aab70afSSebastian Siewior fastboot_tx_write_str(response); 6273aab70afSSebastian Siewior } 6283aab70afSSebastian Siewior 629ac484c5aSRoger Quadros static unsigned int rx_bytes_expected(struct usb_ep *ep) 6303aab70afSSebastian Siewior { 6313aab70afSSebastian Siewior int rx_remain = download_size - download_bytes; 632ac484c5aSRoger Quadros unsigned int rem; 633ac484c5aSRoger Quadros unsigned int maxpacket = ep->maxpacket; 634ac484c5aSRoger Quadros 635ac484c5aSRoger Quadros if (rx_remain <= 0) 6363aab70afSSebastian Siewior return 0; 637ac484c5aSRoger Quadros else if (rx_remain > EP_BUFFER_SIZE) 6383aab70afSSebastian Siewior return EP_BUFFER_SIZE; 639ac484c5aSRoger Quadros 640ac484c5aSRoger Quadros /* 641ac484c5aSRoger Quadros * Some controllers e.g. DWC3 don't like OUT transfers to be 642ac484c5aSRoger Quadros * not ending in maxpacket boundary. So just make them happy by 643ac484c5aSRoger Quadros * always requesting for integral multiple of maxpackets. 644ac484c5aSRoger Quadros * This shouldn't bother controllers that don't care about it. 645ac484c5aSRoger Quadros */ 6469e4b510dSDileep Katta rem = rx_remain % maxpacket; 647ac484c5aSRoger Quadros if (rem > 0) 6489e4b510dSDileep Katta rx_remain = rx_remain + (maxpacket - rem); 649ac484c5aSRoger Quadros 6503aab70afSSebastian Siewior return rx_remain; 6513aab70afSSebastian Siewior } 6523aab70afSSebastian Siewior 6533aab70afSSebastian Siewior #define BYTES_PER_DOT 0x20000 6543aab70afSSebastian Siewior static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req) 6553aab70afSSebastian Siewior { 6563c8f98f5SMaxime Ripard char response[FASTBOOT_RESPONSE_LEN]; 6573aab70afSSebastian Siewior unsigned int transfer_size = download_size - download_bytes; 6583aab70afSSebastian Siewior const unsigned char *buffer = req->buf; 6593aab70afSSebastian Siewior unsigned int buffer_size = req->actual; 66023d1d10cSBo Shen unsigned int pre_dot_num, now_dot_num; 6613aab70afSSebastian Siewior 6623aab70afSSebastian Siewior if (req->status != 0) { 6633aab70afSSebastian Siewior printf("Bad status: %d\n", req->status); 6643aab70afSSebastian Siewior return; 6653aab70afSSebastian Siewior } 6663aab70afSSebastian Siewior 6673aab70afSSebastian Siewior if (buffer_size < transfer_size) 6683aab70afSSebastian Siewior transfer_size = buffer_size; 6693aab70afSSebastian Siewior 670a588d99aSPaul Kocialkowski memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes, 6713aab70afSSebastian Siewior buffer, transfer_size); 6723aab70afSSebastian Siewior 67323d1d10cSBo Shen pre_dot_num = download_bytes / BYTES_PER_DOT; 6743aab70afSSebastian Siewior download_bytes += transfer_size; 67523d1d10cSBo Shen now_dot_num = download_bytes / BYTES_PER_DOT; 67623d1d10cSBo Shen 67723d1d10cSBo Shen if (pre_dot_num != now_dot_num) { 67823d1d10cSBo Shen putc('.'); 67923d1d10cSBo Shen if (!(now_dot_num % 74)) 68023d1d10cSBo Shen putc('\n'); 68123d1d10cSBo Shen } 6823aab70afSSebastian Siewior 6833aab70afSSebastian Siewior /* Check if transfer is done */ 6843aab70afSSebastian Siewior if (download_bytes >= download_size) { 6853aab70afSSebastian Siewior /* 6863aab70afSSebastian Siewior * Reset global transfer variable, keep download_bytes because 6873aab70afSSebastian Siewior * it will be used in the next possible flashing command 6883aab70afSSebastian Siewior */ 6893aab70afSSebastian Siewior download_size = 0; 6903aab70afSSebastian Siewior req->complete = rx_handler_command; 6913aab70afSSebastian Siewior req->length = EP_BUFFER_SIZE; 6923aab70afSSebastian Siewior 693192bc694SBen Whitten strcpy(response, "OKAY"); 6943aab70afSSebastian Siewior fastboot_tx_write_str(response); 6953aab70afSSebastian Siewior 6963aab70afSSebastian Siewior printf("\ndownloading of %d bytes finished\n", download_bytes); 6973aab70afSSebastian Siewior } else { 698ac484c5aSRoger Quadros req->length = rx_bytes_expected(ep); 6993aab70afSSebastian Siewior } 7003aab70afSSebastian Siewior 7013aab70afSSebastian Siewior req->actual = 0; 7023aab70afSSebastian Siewior usb_ep_queue(ep, req, 0); 7033aab70afSSebastian Siewior } 7043aab70afSSebastian Siewior 7053aab70afSSebastian Siewior static void cb_download(struct usb_ep *ep, struct usb_request *req) 7063aab70afSSebastian Siewior { 7073aab70afSSebastian Siewior char *cmd = req->buf; 7083c8f98f5SMaxime Ripard char response[FASTBOOT_RESPONSE_LEN]; 7093aab70afSSebastian Siewior 7103aab70afSSebastian Siewior strsep(&cmd, ":"); 7113aab70afSSebastian Siewior download_size = simple_strtoul(cmd, NULL, 16); 7123aab70afSSebastian Siewior download_bytes = 0; 7133aab70afSSebastian Siewior 7143aab70afSSebastian Siewior printf("Starting download of %d bytes\n", download_size); 7153aab70afSSebastian Siewior 7163aab70afSSebastian Siewior if (0 == download_size) { 717192bc694SBen Whitten strcpy(response, "FAILdata invalid size"); 718a588d99aSPaul Kocialkowski } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) { 7193aab70afSSebastian Siewior download_size = 0; 720192bc694SBen Whitten strcpy(response, "FAILdata too large"); 7213aab70afSSebastian Siewior } else { 7223aab70afSSebastian Siewior sprintf(response, "DATA%08x", download_size); 7233aab70afSSebastian Siewior req->complete = rx_handler_dl_image; 724ac484c5aSRoger Quadros req->length = rx_bytes_expected(ep); 7253aab70afSSebastian Siewior } 726367cce4dSXu Hongfei 727367cce4dSXu Hongfei fastboot_tx_write_str(response); 728367cce4dSXu Hongfei } 729367cce4dSXu Hongfei 730367cce4dSXu Hongfei static void tx_handler_ul(struct usb_ep *ep, struct usb_request *req) 731367cce4dSXu Hongfei { 732367cce4dSXu Hongfei unsigned int xfer_size = 0; 733367cce4dSXu Hongfei unsigned int pre_dot_num, now_dot_num; 734367cce4dSXu Hongfei unsigned int remain_size = 0; 735367cce4dSXu Hongfei unsigned int transferred_size = req->actual; 736367cce4dSXu Hongfei 737367cce4dSXu Hongfei if (req->status != 0) { 738367cce4dSXu Hongfei printf("Bad status: %d\n", req->status); 739367cce4dSXu Hongfei return; 740367cce4dSXu Hongfei } 741367cce4dSXu Hongfei 742367cce4dSXu Hongfei if (start_upload) { 743367cce4dSXu Hongfei pre_dot_num = upload_bytes / BYTES_PER_DOT; 744367cce4dSXu Hongfei upload_bytes += transferred_size; 745367cce4dSXu Hongfei now_dot_num = upload_bytes / BYTES_PER_DOT; 746367cce4dSXu Hongfei 747367cce4dSXu Hongfei if (pre_dot_num != now_dot_num) { 748367cce4dSXu Hongfei putc('.'); 749367cce4dSXu Hongfei if (!(now_dot_num % 74)) 750367cce4dSXu Hongfei putc('\n'); 751367cce4dSXu Hongfei } 752367cce4dSXu Hongfei } 753367cce4dSXu Hongfei 754367cce4dSXu Hongfei remain_size = upload_size - upload_bytes; 755367cce4dSXu Hongfei xfer_size = (remain_size > EP_BUFFER_SIZE) ? 756367cce4dSXu Hongfei EP_BUFFER_SIZE : remain_size; 757367cce4dSXu Hongfei 758367cce4dSXu Hongfei debug("%s: remain_size=%d, transferred_size=%d", 759367cce4dSXu Hongfei __func__, remain_size, transferred_size); 760367cce4dSXu Hongfei debug("xfer_size=%d, upload_bytes=%d, upload_size=%d!\n", 761367cce4dSXu Hongfei xfer_size, upload_bytes, upload_size); 762367cce4dSXu Hongfei 763367cce4dSXu Hongfei if (remain_size <= 0) { 764367cce4dSXu Hongfei fastboot_func->in_req->complete = fastboot_complete; 765367cce4dSXu Hongfei fastboot_tx_write_str("OKAY"); 766367cce4dSXu Hongfei printf("\nuploading of %d bytes finished\n", upload_bytes); 767367cce4dSXu Hongfei upload_bytes = 0; 768367cce4dSXu Hongfei upload_size = 0; 769367cce4dSXu Hongfei start_upload = false; 770367cce4dSXu Hongfei return; 771367cce4dSXu Hongfei } 772367cce4dSXu Hongfei 773367cce4dSXu Hongfei /* Remove the transfer callback which response the upload */ 774367cce4dSXu Hongfei /* request from host */ 775367cce4dSXu Hongfei if (!upload_bytes) 776367cce4dSXu Hongfei start_upload = true; 777367cce4dSXu Hongfei 778367cce4dSXu Hongfei fastboot_tx_write((char *)(CONFIG_FASTBOOT_BUF_ADDR + upload_bytes), 779367cce4dSXu Hongfei xfer_size); 780367cce4dSXu Hongfei } 781367cce4dSXu Hongfei 782367cce4dSXu Hongfei static void cb_upload(struct usb_ep *ep, struct usb_request *req) 783367cce4dSXu Hongfei { 784367cce4dSXu Hongfei char response[FASTBOOT_RESPONSE_LEN]; 785367cce4dSXu Hongfei 7864d0fc665SAndy Ye 787367cce4dSXu Hongfei 788367cce4dSXu Hongfei printf("Starting upload of %d bytes\n", upload_size); 789367cce4dSXu Hongfei 790367cce4dSXu Hongfei if (0 == upload_size) { 791367cce4dSXu Hongfei strcpy(response, "FAILdata invalid size"); 792367cce4dSXu Hongfei } else { 793367cce4dSXu Hongfei start_upload = false; 794367cce4dSXu Hongfei sprintf(response, "DATA%08x", upload_size); 795367cce4dSXu Hongfei fastboot_func->in_req->complete = tx_handler_ul; 796367cce4dSXu Hongfei } 797367cce4dSXu Hongfei 7983aab70afSSebastian Siewior fastboot_tx_write_str(response); 7993aab70afSSebastian Siewior } 8003aab70afSSebastian Siewior 8013aab70afSSebastian Siewior static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req) 8023aab70afSSebastian Siewior { 8033aab70afSSebastian Siewior char boot_addr_start[12]; 8043aab70afSSebastian Siewior char *bootm_args[] = { "bootm", boot_addr_start, NULL }; 8053aab70afSSebastian Siewior 8063aab70afSSebastian Siewior puts("Booting kernel..\n"); 8073aab70afSSebastian Siewior 80890ae53ceSTom Rini sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR); 8093aab70afSSebastian Siewior do_bootm(NULL, 0, 2, bootm_args); 8103aab70afSSebastian Siewior 8113aab70afSSebastian Siewior /* This only happens if image is somehow faulty so we start over */ 8123aab70afSSebastian Siewior do_reset(NULL, 0, 0, NULL); 8133aab70afSSebastian Siewior } 8143aab70afSSebastian Siewior 8153aab70afSSebastian Siewior static void cb_boot(struct usb_ep *ep, struct usb_request *req) 8163aab70afSSebastian Siewior { 8173aab70afSSebastian Siewior fastboot_func->in_req->complete = do_bootm_on_complete; 8183aab70afSSebastian Siewior fastboot_tx_write_str("OKAY"); 8193aab70afSSebastian Siewior } 8203aab70afSSebastian Siewior 821267abc62SRob Herring static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req) 822267abc62SRob Herring { 823267abc62SRob Herring g_dnl_trigger_detach(); 824267abc62SRob Herring } 825267abc62SRob Herring 826267abc62SRob Herring static void cb_continue(struct usb_ep *ep, struct usb_request *req) 827267abc62SRob Herring { 828267abc62SRob Herring fastboot_func->in_req->complete = do_exit_on_complete; 829267abc62SRob Herring fastboot_tx_write_str("OKAY"); 830267abc62SRob Herring } 831267abc62SRob Herring 832367cce4dSXu Hongfei static void cb_set_active(struct usb_ep *ep, struct usb_request *req) 833367cce4dSXu Hongfei { 834367cce4dSXu Hongfei char *cmd = req->buf; 835367cce4dSXu Hongfei 836367cce4dSXu Hongfei debug("%s: %s\n", __func__, cmd); 837367cce4dSXu Hongfei 838367cce4dSXu Hongfei strsep(&cmd, ":"); 839367cce4dSXu Hongfei if (!cmd) { 840*90aa625cSMasahiro Yamada pr_err("missing slot name"); 841367cce4dSXu Hongfei fastboot_tx_write_str("FAIL: missing slot name"); 842367cce4dSXu Hongfei return; 843367cce4dSXu Hongfei } 844367cce4dSXu Hongfei #ifdef CONFIG_AVB_LIBAVB_USER 845367cce4dSXu Hongfei unsigned int slot_number; 846367cce4dSXu Hongfei if (strncmp("a", cmd, 1) == 0) { 847367cce4dSXu Hongfei slot_number = 0; 8485b090159SJason Zhu avb_set_slot_active(&slot_number); 849367cce4dSXu Hongfei } else if (strncmp("b", cmd, 1) == 0) { 850367cce4dSXu Hongfei slot_number = 1; 8515b090159SJason Zhu avb_set_slot_active(&slot_number); 852367cce4dSXu Hongfei } else { 853367cce4dSXu Hongfei fastboot_tx_write_str("FAIL: unkown slot name"); 854367cce4dSXu Hongfei return; 855367cce4dSXu Hongfei } 856367cce4dSXu Hongfei 857367cce4dSXu Hongfei fastboot_tx_write_str("OKAY"); 858367cce4dSXu Hongfei return; 859367cce4dSXu Hongfei #else 860367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 861367cce4dSXu Hongfei return; 862367cce4dSXu Hongfei #endif 863367cce4dSXu Hongfei } 864367cce4dSXu Hongfei 865d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH 866d1b5ed07SSteve Rae static void cb_flash(struct usb_ep *ep, struct usb_request *req) 867d1b5ed07SSteve Rae { 868d1b5ed07SSteve Rae char *cmd = req->buf; 869374a9995SCody Xie char response[FASTBOOT_RESPONSE_LEN] = {0}; 8707bc1707dSJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER 8717bc1707dSJason Zhu uint8_t flash_lock_state; 872d1b5ed07SSteve Rae 873ef52a073SJason Zhu if (avb_read_flash_lock_state(&flash_lock_state)) { 8747bc1707dSJason Zhu fastboot_tx_write_str("FAIL"); 8757bc1707dSJason Zhu return; 876ef52a073SJason Zhu } 877ef52a073SJason Zhu 8787bc1707dSJason Zhu if (flash_lock_state == 0) { 8797bc1707dSJason Zhu fastboot_tx_write_str("FAILThe device is locked, can not flash!"); 8807bc1707dSJason Zhu printf("The device is locked, can not flash!\n"); 8817bc1707dSJason Zhu return; 8827bc1707dSJason Zhu } 8837bc1707dSJason Zhu #endif 884d1b5ed07SSteve Rae strsep(&cmd, ":"); 885d1b5ed07SSteve Rae if (!cmd) { 886*90aa625cSMasahiro Yamada pr_err("missing partition name"); 887d1b5ed07SSteve Rae fastboot_tx_write_str("FAILmissing partition name"); 888d1b5ed07SSteve Rae return; 889d1b5ed07SSteve Rae } 890d1b5ed07SSteve Rae 8918b464fa9SJocelyn Bohr fastboot_fail("no flash device defined", response); 892d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV 89364ece848SSteve Rae fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR, 8948b464fa9SJocelyn Bohr download_bytes, response); 895d1b5ed07SSteve Rae #endif 896bf8940d3SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV 8978b464fa9SJocelyn Bohr fb_nand_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR, 8988b464fa9SJocelyn Bohr download_bytes, response); 899bf8940d3SMaxime Ripard #endif 900d1b5ed07SSteve Rae fastboot_tx_write_str(response); 901d1b5ed07SSteve Rae } 902d1b5ed07SSteve Rae #endif 903d1b5ed07SSteve Rae 904367cce4dSXu Hongfei static void cb_flashing(struct usb_ep *ep, struct usb_request *req) 905367cce4dSXu Hongfei { 906367cce4dSXu Hongfei char *cmd = req->buf; 907367cce4dSXu Hongfei 908367cce4dSXu Hongfei if (strncmp("lock", cmd + 9, 4) == 0) { 9097bc1707dSJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER 9107bc1707dSJason Zhu uint8_t flash_lock_state; 9117bc1707dSJason Zhu flash_lock_state = 0; 9125b090159SJason Zhu if (avb_write_flash_lock_state(flash_lock_state)) 9137bc1707dSJason Zhu fastboot_tx_write_str("FAIL"); 9147bc1707dSJason Zhu else 9157bc1707dSJason Zhu fastboot_tx_write_str("OKAY"); 9167bc1707dSJason Zhu #else 917367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 9187bc1707dSJason Zhu #endif 919367cce4dSXu Hongfei } else if (strncmp("unlock", cmd + 9, 6) == 0) { 9207bc1707dSJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER 9217bc1707dSJason Zhu uint8_t flash_lock_state; 9227bc1707dSJason Zhu flash_lock_state = 1; 9235b090159SJason Zhu if (avb_write_flash_lock_state(flash_lock_state)) 9247bc1707dSJason Zhu fastboot_tx_write_str("FAIL"); 9257bc1707dSJason Zhu else 9267bc1707dSJason Zhu fastboot_tx_write_str("OKAY"); 9277bc1707dSJason Zhu #else 928367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 9297bc1707dSJason Zhu #endif 930367cce4dSXu Hongfei } else if (strncmp("lock_critical", cmd + 9, 12) == 0) { 931367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 932367cce4dSXu Hongfei } else if (strncmp("unlock_critical", cmd + 9, 14) == 0) { 933367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 934367cce4dSXu Hongfei } else if (strncmp("get_unlock_ability", cmd + 9, 17) == 0) { 935367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 936367cce4dSXu Hongfei } else if (strncmp("get_unlock_bootloader_nonce", cmd + 4, 27) == 0) { 937367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 938367cce4dSXu Hongfei } else if (strncmp("unlock_bootloader", cmd + 9, 17) == 0) { 939367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 940367cce4dSXu Hongfei } else if (strncmp("lock_bootloader", cmd + 9, 15) == 0) { 941367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 942367cce4dSXu Hongfei } else { 943367cce4dSXu Hongfei fastboot_tx_write_str("FAILunknown flashing command"); 944367cce4dSXu Hongfei } 945367cce4dSXu Hongfei } 946367cce4dSXu Hongfei 947de195620SMichael Scott static void cb_oem(struct usb_ep *ep, struct usb_request *req) 948de195620SMichael Scott { 949de195620SMichael Scott char *cmd = req->buf; 950367cce4dSXu Hongfei 9514adef270SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV 952372d7decSRob Herring if (strncmp("format", cmd + 4, 6) == 0) { 953372d7decSRob Herring char cmdbuf[32]; 954372d7decSRob Herring sprintf(cmdbuf, "gpt write mmc %x $partitions", 955372d7decSRob Herring CONFIG_FASTBOOT_FLASH_MMC_DEV); 956372d7decSRob Herring if (run_command(cmdbuf, 0)) 957372d7decSRob Herring fastboot_tx_write_str("FAIL"); 958372d7decSRob Herring else 959372d7decSRob Herring fastboot_tx_write_str("OKAY"); 960372d7decSRob Herring } else 961372d7decSRob Herring #endif 962de195620SMichael Scott if (strncmp("unlock", cmd + 4, 8) == 0) { 963de195620SMichael Scott fastboot_tx_write_str("FAILnot implemented"); 964367cce4dSXu Hongfei } else if (strncmp("at-get-ca-request", cmd + 4, 17) == 0) { 9654d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT 9664d0fc665SAndy Ye uint8_t operation_start[128]; 9674d0fc665SAndy Ye uint8_t out[256]; 9684d0fc665SAndy Ye uint32_t operation_size = download_bytes; 9694d0fc665SAndy Ye uint32_t out_len = 256; 9704d0fc665SAndy Ye uint32_t res = 0; 9714d0fc665SAndy Ye memcpy(operation_start, (void *)CONFIG_FASTBOOT_BUF_ADDR, download_bytes); 9724d0fc665SAndy Ye res = trusty_attest_get_ca(operation_start, &operation_size, out, &out_len); 9734d0fc665SAndy Ye if (res) { 9744d0fc665SAndy Ye fastboot_tx_write_str("FAILtrusty_attest_get_ca failed"); 9754d0fc665SAndy Ye return; 9764d0fc665SAndy Ye } 9774d0fc665SAndy Ye upload_size = out_len; 9784d0fc665SAndy Ye memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR, out, out_len); 979367cce4dSXu Hongfei fastboot_tx_write_str("OKAY"); 9804d0fc665SAndy Ye #else 9814d0fc665SAndy Ye fastboot_tx_write_str("FAILnot implemented"); 9824d0fc665SAndy Ye return; 9834d0fc665SAndy Ye #endif 984367cce4dSXu Hongfei } else if (strncmp("at-set-ca-response", cmd + 4, 18) == 0) { 9854d0fc665SAndy Ye #ifdef CONFIG_OPTEE_CLIENT 9864d0fc665SAndy Ye uint8_t ca_response[8*1024]; 9874d0fc665SAndy Ye uint32_t ca_response_size = download_bytes; 9884d0fc665SAndy Ye uint32_t res = 0; 9894d0fc665SAndy Ye memcpy(ca_response, (void *)CONFIG_FASTBOOT_BUF_ADDR, download_bytes); 9904d0fc665SAndy Ye res = trusty_attest_set_ca(ca_response, &ca_response_size); 9914d0fc665SAndy Ye if (res) { 9924d0fc665SAndy Ye fastboot_tx_write_str("FAILtrusty_attest_set_ca failed"); 9934d0fc665SAndy Ye } else { 994367cce4dSXu Hongfei fastboot_tx_write_str("OKAY"); 9954d0fc665SAndy Ye } 9964d0fc665SAndy Ye #else 9974d0fc665SAndy Ye fastboot_tx_write_str("FAILnot implemented"); 9984d0fc665SAndy Ye return; 9994d0fc665SAndy Ye #endif 1000367cce4dSXu Hongfei } else if (strncmp("at-lock-vboot", cmd + 4, 13) == 0) { 1001d8bd6e97SJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER 1002d8bd6e97SJason Zhu uint8_t lock_state; 1003d8bd6e97SJason Zhu lock_state = 0; 10045b090159SJason Zhu if (avb_write_lock_state(lock_state)) 1005d8bd6e97SJason Zhu fastboot_tx_write_str("FAIL"); 1006d8bd6e97SJason Zhu else 1007d8bd6e97SJason Zhu fastboot_tx_write_str("OKAY"); 1008d8bd6e97SJason Zhu #else 1009367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 1010d8bd6e97SJason Zhu #endif 1011367cce4dSXu Hongfei } else if (strncmp("at-unlock-vboot", cmd + 4, 15) == 0) { 1012d8bd6e97SJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER 1013d8bd6e97SJason Zhu uint8_t lock_state; 10145b090159SJason Zhu if (avb_read_lock_state(&lock_state)) 1015d8bd6e97SJason Zhu fastboot_tx_write_str("FAIL"); 1016d8bd6e97SJason Zhu if (lock_state >> 1 == 1) { 1017d8bd6e97SJason Zhu fastboot_tx_write_str("FAILThe vboot is disable!"); 1018d8bd6e97SJason Zhu } else { 1019d8bd6e97SJason Zhu lock_state = 1; 10205b090159SJason Zhu if (avb_write_lock_state(lock_state)) 1021d8bd6e97SJason Zhu fastboot_tx_write_str("FAIL"); 1022d8bd6e97SJason Zhu else 1023d8bd6e97SJason Zhu fastboot_tx_write_str("OKAY"); 1024d8bd6e97SJason Zhu } 1025d8bd6e97SJason Zhu #else 1026367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 1027d8bd6e97SJason Zhu #endif 1028367cce4dSXu Hongfei } else if (strncmp("at-disable-unlock-vboot", cmd + 4, 23) == 0) { 1029d8bd6e97SJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER 1030d8bd6e97SJason Zhu uint8_t lock_state; 1031d8bd6e97SJason Zhu lock_state = 2; 10325b090159SJason Zhu if (avb_write_lock_state(lock_state)) 1033d8bd6e97SJason Zhu fastboot_tx_write_str("FAIL"); 1034d8bd6e97SJason Zhu else 1035d8bd6e97SJason Zhu fastboot_tx_write_str("OKAY"); 1036d8bd6e97SJason Zhu #else 1037367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 1038d8bd6e97SJason Zhu #endif 1039367cce4dSXu Hongfei } else if (strncmp("fuse at-perm-attr", cmd + 4, 16) == 0) { 10404f3cd37cSJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER 10410916e43bSJason Zhu if (PERM_ATTR_TOTAL_SIZE != download_bytes) { 10420916e43bSJason Zhu printf("Permanent attribute size is not equal!\n"); 10430916e43bSJason Zhu fastboot_tx_write_str("FAIL"); 10440916e43bSJason Zhu return; 10450916e43bSJason Zhu } 10460916e43bSJason Zhu 10475b090159SJason Zhu if (avb_write_permanent_attributes((uint8_t *) 10484f3cd37cSJason Zhu CONFIG_FASTBOOT_BUF_ADDR, 10490916e43bSJason Zhu download_bytes 10500916e43bSJason Zhu - PERM_ATTR_DIGEST_SIZE)) { 10514f3cd37cSJason Zhu fastboot_tx_write_str("FAIL"); 10520916e43bSJason Zhu return; 10530916e43bSJason Zhu } 10540916e43bSJason Zhu 10550916e43bSJason Zhu if (avb_write_attribute_hash((uint8_t *) 10560916e43bSJason Zhu (CONFIG_FASTBOOT_BUF_ADDR 10570916e43bSJason Zhu + download_bytes 10580916e43bSJason Zhu - PERM_ATTR_DIGEST_SIZE), 10590916e43bSJason Zhu PERM_ATTR_DIGEST_SIZE)) { 10600916e43bSJason Zhu fastboot_tx_write_str("FAIL"); 10610916e43bSJason Zhu return; 10620916e43bSJason Zhu } 10630916e43bSJason Zhu 10640916e43bSJason Zhu if (avb_write_perm_attr_flag(1)) { 10650916e43bSJason Zhu fastboot_tx_write_str("FAIL"); 10660916e43bSJason Zhu return; 10670916e43bSJason Zhu } 10680916e43bSJason Zhu 10694f3cd37cSJason Zhu fastboot_tx_write_str("OKAY"); 10704f3cd37cSJason Zhu #else 1071367cce4dSXu Hongfei fastboot_tx_write_str("FAILnot implemented"); 10724f3cd37cSJason Zhu #endif 10734e1bbe84SJason Zhu } else if (strncmp("fuse at-bootloader-vboot-key", cmd + 4, 27) == 0) { 10744e1bbe84SJason Zhu #ifdef CONFIG_AVB_LIBAVB_USER 10754e1bbe84SJason Zhu if (download_bytes != VBOOT_KEY_HASH_SIZE) { 10764e1bbe84SJason Zhu fastboot_tx_write_str("FAIL"); 10774e1bbe84SJason Zhu printf("The vboot key size error!\n"); 10784e1bbe84SJason Zhu } 10794e1bbe84SJason Zhu 10804e1bbe84SJason Zhu if (avb_write_vbootkey_hash((uint8_t *) 10814e1bbe84SJason Zhu CONFIG_FASTBOOT_BUF_ADDR, 10824e1bbe84SJason Zhu VBOOT_KEY_HASH_SIZE)) { 10834e1bbe84SJason Zhu fastboot_tx_write_str("FAIL"); 10844e1bbe84SJason Zhu return; 10854e1bbe84SJason Zhu } 10864e1bbe84SJason Zhu fastboot_tx_write_str("OKAY"); 10874e1bbe84SJason Zhu #else 10884e1bbe84SJason Zhu fastboot_tx_write_str("FAILnot implemented"); 10894e1bbe84SJason Zhu #endif 1090367cce4dSXu Hongfei } else { 1091de195620SMichael Scott fastboot_tx_write_str("FAILunknown oem command"); 1092de195620SMichael Scott } 1093de195620SMichael Scott } 1094de195620SMichael Scott 109589792381SDileep Katta #ifdef CONFIG_FASTBOOT_FLASH 109689792381SDileep Katta static void cb_erase(struct usb_ep *ep, struct usb_request *req) 109789792381SDileep Katta { 109889792381SDileep Katta char *cmd = req->buf; 10993c8f98f5SMaxime Ripard char response[FASTBOOT_RESPONSE_LEN]; 110089792381SDileep Katta 110189792381SDileep Katta strsep(&cmd, ":"); 110289792381SDileep Katta if (!cmd) { 1103*90aa625cSMasahiro Yamada pr_err("missing partition name"); 110489792381SDileep Katta fastboot_tx_write_str("FAILmissing partition name"); 110589792381SDileep Katta return; 110689792381SDileep Katta } 110789792381SDileep Katta 11088b464fa9SJocelyn Bohr fastboot_fail("no flash device defined", response); 110989792381SDileep Katta #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV 11108b464fa9SJocelyn Bohr fb_mmc_erase(cmd, response); 111189792381SDileep Katta #endif 1112bf8940d3SMaxime Ripard #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV 11138b464fa9SJocelyn Bohr fb_nand_erase(cmd, response); 1114bf8940d3SMaxime Ripard #endif 111589792381SDileep Katta fastboot_tx_write_str(response); 111689792381SDileep Katta } 111789792381SDileep Katta #endif 111889792381SDileep Katta 11193aab70afSSebastian Siewior struct cmd_dispatch_info { 11203aab70afSSebastian Siewior char *cmd; 11213aab70afSSebastian Siewior void (*cb)(struct usb_ep *ep, struct usb_request *req); 11223aab70afSSebastian Siewior }; 11233aab70afSSebastian Siewior 11243aab70afSSebastian Siewior static const struct cmd_dispatch_info cmd_dispatch_info[] = { 11253aab70afSSebastian Siewior { 11263aab70afSSebastian Siewior .cmd = "reboot", 11273aab70afSSebastian Siewior .cb = cb_reboot, 11283aab70afSSebastian Siewior }, { 11293aab70afSSebastian Siewior .cmd = "getvar:", 11303aab70afSSebastian Siewior .cb = cb_getvar, 11313aab70afSSebastian Siewior }, { 11323aab70afSSebastian Siewior .cmd = "download:", 11333aab70afSSebastian Siewior .cb = cb_download, 11343aab70afSSebastian Siewior }, { 1135367cce4dSXu Hongfei .cmd = "upload", 1136367cce4dSXu Hongfei .cb = cb_upload, 1137367cce4dSXu Hongfei }, { 11383aab70afSSebastian Siewior .cmd = "boot", 11393aab70afSSebastian Siewior .cb = cb_boot, 1140267abc62SRob Herring }, { 1141267abc62SRob Herring .cmd = "continue", 1142267abc62SRob Herring .cb = cb_continue, 1143367cce4dSXu Hongfei }, { 1144367cce4dSXu Hongfei .cmd = "set_active", 1145367cce4dSXu Hongfei .cb = cb_set_active, 11463aab70afSSebastian Siewior }, 1147d1b5ed07SSteve Rae #ifdef CONFIG_FASTBOOT_FLASH 1148d1b5ed07SSteve Rae { 1149367cce4dSXu Hongfei .cmd = "flashing", 1150367cce4dSXu Hongfei .cb = cb_flashing, 1151367cce4dSXu Hongfei }, 1152367cce4dSXu Hongfei { 1153d1b5ed07SSteve Rae .cmd = "flash", 1154d1b5ed07SSteve Rae .cb = cb_flash, 115589792381SDileep Katta }, { 115689792381SDileep Katta .cmd = "erase", 115789792381SDileep Katta .cb = cb_erase, 1158d1b5ed07SSteve Rae }, 1159d1b5ed07SSteve Rae #endif 1160de195620SMichael Scott { 1161de195620SMichael Scott .cmd = "oem", 1162de195620SMichael Scott .cb = cb_oem, 1163de195620SMichael Scott }, 11643aab70afSSebastian Siewior }; 11653aab70afSSebastian Siewior 11663aab70afSSebastian Siewior static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) 11673aab70afSSebastian Siewior { 11683aab70afSSebastian Siewior char *cmdbuf = req->buf; 11693aab70afSSebastian Siewior void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL; 11703aab70afSSebastian Siewior int i; 11713aab70afSSebastian Siewior 117294b385faSPaul Kocialkowski if (req->status != 0 || req->length == 0) 117394b385faSPaul Kocialkowski return; 117494b385faSPaul Kocialkowski 11753aab70afSSebastian Siewior for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) { 11763aab70afSSebastian Siewior if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) { 11773aab70afSSebastian Siewior func_cb = cmd_dispatch_info[i].cb; 11783aab70afSSebastian Siewior break; 11793aab70afSSebastian Siewior } 11803aab70afSSebastian Siewior } 11813aab70afSSebastian Siewior 1182593cbd93SSteve Rae if (!func_cb) { 1183*90aa625cSMasahiro Yamada pr_err("unknown command: %.*s", req->actual, cmdbuf); 11843aab70afSSebastian Siewior fastboot_tx_write_str("FAILunknown command"); 1185593cbd93SSteve Rae } else { 1186e2140588SEric Nelson if (req->actual < req->length) { 1187e2140588SEric Nelson u8 *buf = (u8 *)req->buf; 1188e2140588SEric Nelson buf[req->actual] = 0; 11893aab70afSSebastian Siewior func_cb(ep, req); 1190e2140588SEric Nelson } else { 1191*90aa625cSMasahiro Yamada pr_err("buffer overflow"); 1192e2140588SEric Nelson fastboot_tx_write_str("FAILbuffer overflow"); 1193e2140588SEric Nelson } 1194593cbd93SSteve Rae } 11953aab70afSSebastian Siewior 11963aab70afSSebastian Siewior *cmdbuf = '\0'; 11973aab70afSSebastian Siewior req->actual = 0; 11983aab70afSSebastian Siewior usb_ep_queue(ep, req, 0); 11993aab70afSSebastian Siewior } 1200