Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 - 2009 |
| 4 | * Windriver, <www.windriver.com> |
| 5 | * Tom Rix <Tom.Rix@windriver.com> |
| 6 | * |
| 7 | * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 8 | * |
| 9 | * Copyright 2014 Linaro, Ltd. |
| 10 | * Rob Herring <robh@kernel.org> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 11 | */ |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 12 | #include <config.h> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 13 | #include <common.h> |
| 14 | #include <errno.h> |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 15 | #include <fastboot.h> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <linux/usb/ch9.h> |
| 18 | #include <linux/usb/gadget.h> |
| 19 | #include <linux/usb/composite.h> |
| 20 | #include <linux/compiler.h> |
| 21 | #include <version.h> |
| 22 | #include <g_dnl.h> |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 23 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
| 24 | #include <fb_mmc.h> |
| 25 | #endif |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 26 | #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV |
| 27 | #include <fb_nand.h> |
| 28 | #endif |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 29 | |
| 30 | #define FASTBOOT_VERSION "0.4" |
| 31 | |
| 32 | #define FASTBOOT_INTERFACE_CLASS 0xff |
| 33 | #define FASTBOOT_INTERFACE_SUB_CLASS 0x42 |
| 34 | #define FASTBOOT_INTERFACE_PROTOCOL 0x03 |
| 35 | |
| 36 | #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200) |
| 37 | #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040) |
| 38 | #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040) |
| 39 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 40 | #define EP_BUFFER_SIZE 4096 |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 41 | /* |
| 42 | * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size |
| 43 | * (64 or 512 or 1024), else we break on certain controllers like DWC3 |
| 44 | * that expect bulk OUT requests to be divisible by maxpacket size. |
| 45 | */ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 46 | |
| 47 | struct f_fastboot { |
| 48 | struct usb_function usb_function; |
| 49 | |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 50 | /* IN/OUT EP's and corresponding requests */ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 51 | struct usb_ep *in_ep, *out_ep; |
| 52 | struct usb_request *in_req, *out_req; |
| 53 | }; |
| 54 | |
| 55 | static inline struct f_fastboot *func_to_fastboot(struct usb_function *f) |
| 56 | { |
| 57 | return container_of(f, struct f_fastboot, usb_function); |
| 58 | } |
| 59 | |
| 60 | static struct f_fastboot *fastboot_func; |
| 61 | static unsigned int download_size; |
| 62 | static unsigned int download_bytes; |
| 63 | |
| 64 | static struct usb_endpoint_descriptor fs_ep_in = { |
| 65 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 66 | .bDescriptorType = USB_DT_ENDPOINT, |
| 67 | .bEndpointAddress = USB_DIR_IN, |
| 68 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 69 | .wMaxPacketSize = cpu_to_le16(64), |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static struct usb_endpoint_descriptor fs_ep_out = { |
| 73 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 74 | .bDescriptorType = USB_DT_ENDPOINT, |
| 75 | .bEndpointAddress = USB_DIR_OUT, |
| 76 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 77 | .wMaxPacketSize = cpu_to_le16(64), |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 80 | static struct usb_endpoint_descriptor hs_ep_in = { |
| 81 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 82 | .bDescriptorType = USB_DT_ENDPOINT, |
| 83 | .bEndpointAddress = USB_DIR_IN, |
| 84 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 85 | .wMaxPacketSize = cpu_to_le16(512), |
| 86 | }; |
| 87 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 88 | static struct usb_endpoint_descriptor hs_ep_out = { |
| 89 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 90 | .bDescriptorType = USB_DT_ENDPOINT, |
| 91 | .bEndpointAddress = USB_DIR_OUT, |
| 92 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 93 | .wMaxPacketSize = cpu_to_le16(512), |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | static struct usb_interface_descriptor interface_desc = { |
| 97 | .bLength = USB_DT_INTERFACE_SIZE, |
| 98 | .bDescriptorType = USB_DT_INTERFACE, |
| 99 | .bInterfaceNumber = 0x00, |
| 100 | .bAlternateSetting = 0x00, |
| 101 | .bNumEndpoints = 0x02, |
| 102 | .bInterfaceClass = FASTBOOT_INTERFACE_CLASS, |
| 103 | .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS, |
| 104 | .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL, |
| 105 | }; |
| 106 | |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 107 | static struct usb_descriptor_header *fb_fs_function[] = { |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 108 | (struct usb_descriptor_header *)&interface_desc, |
| 109 | (struct usb_descriptor_header *)&fs_ep_in, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 110 | (struct usb_descriptor_header *)&fs_ep_out, |
| 111 | }; |
| 112 | |
| 113 | static struct usb_descriptor_header *fb_hs_function[] = { |
| 114 | (struct usb_descriptor_header *)&interface_desc, |
| 115 | (struct usb_descriptor_header *)&hs_ep_in, |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 116 | (struct usb_descriptor_header *)&hs_ep_out, |
| 117 | NULL, |
| 118 | }; |
| 119 | |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 120 | static struct usb_endpoint_descriptor * |
| 121 | fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, |
| 122 | struct usb_endpoint_descriptor *hs) |
| 123 | { |
| 124 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 125 | return hs; |
| 126 | return fs; |
| 127 | } |
| 128 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 129 | /* |
| 130 | * static strings, in UTF-8 |
| 131 | */ |
| 132 | static const char fastboot_name[] = "Android Fastboot"; |
| 133 | |
| 134 | static struct usb_string fastboot_string_defs[] = { |
| 135 | [0].s = fastboot_name, |
| 136 | { } /* end of list */ |
| 137 | }; |
| 138 | |
| 139 | static struct usb_gadget_strings stringtab_fastboot = { |
| 140 | .language = 0x0409, /* en-us */ |
| 141 | .strings = fastboot_string_defs, |
| 142 | }; |
| 143 | |
| 144 | static struct usb_gadget_strings *fastboot_strings[] = { |
| 145 | &stringtab_fastboot, |
| 146 | NULL, |
| 147 | }; |
| 148 | |
| 149 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); |
Alexey Firago | 96da306 | 2015-02-25 17:10:47 +0300 | [diff] [blame] | 150 | static int strcmp_l1(const char *s1, const char *s2); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 151 | |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 152 | |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 153 | static char *fb_response_str; |
| 154 | |
| 155 | void fastboot_fail(const char *reason) |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 156 | { |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 157 | strncpy(fb_response_str, "FAIL\0", 5); |
| 158 | strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1); |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 161 | void fastboot_okay(const char *reason) |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 162 | { |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 163 | strncpy(fb_response_str, "OKAY\0", 5); |
| 164 | strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1); |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 167 | static void fastboot_complete(struct usb_ep *ep, struct usb_request *req) |
| 168 | { |
| 169 | int status = req->status; |
| 170 | if (!status) |
| 171 | return; |
| 172 | printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual); |
| 173 | } |
| 174 | |
| 175 | static int fastboot_bind(struct usb_configuration *c, struct usb_function *f) |
| 176 | { |
| 177 | int id; |
| 178 | struct usb_gadget *gadget = c->cdev->gadget; |
| 179 | struct f_fastboot *f_fb = func_to_fastboot(f); |
Dileep Katta | 447c975 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 180 | const char *s; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 181 | |
| 182 | /* DYNAMIC interface numbers assignments */ |
| 183 | id = usb_interface_id(c, f); |
| 184 | if (id < 0) |
| 185 | return id; |
| 186 | interface_desc.bInterfaceNumber = id; |
| 187 | |
| 188 | id = usb_string_id(c->cdev); |
| 189 | if (id < 0) |
| 190 | return id; |
| 191 | fastboot_string_defs[0].id = id; |
| 192 | interface_desc.iInterface = id; |
| 193 | |
| 194 | f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in); |
| 195 | if (!f_fb->in_ep) |
| 196 | return -ENODEV; |
| 197 | f_fb->in_ep->driver_data = c->cdev; |
| 198 | |
| 199 | f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out); |
| 200 | if (!f_fb->out_ep) |
| 201 | return -ENODEV; |
| 202 | f_fb->out_ep->driver_data = c->cdev; |
| 203 | |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 204 | f->descriptors = fb_fs_function; |
| 205 | |
| 206 | if (gadget_is_dualspeed(gadget)) { |
| 207 | /* Assume endpoint addresses are the same for both speeds */ |
| 208 | hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; |
| 209 | hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; |
| 210 | /* copy HS descriptors */ |
| 211 | f->hs_descriptors = fb_hs_function; |
| 212 | } |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 213 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 214 | s = env_get("serial#"); |
Dileep Katta | 447c975 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 215 | if (s) |
| 216 | g_dnl_set_serialnumber((char *)s); |
| 217 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f) |
| 222 | { |
| 223 | memset(fastboot_func, 0, sizeof(*fastboot_func)); |
| 224 | } |
| 225 | |
| 226 | static void fastboot_disable(struct usb_function *f) |
| 227 | { |
| 228 | struct f_fastboot *f_fb = func_to_fastboot(f); |
| 229 | |
| 230 | usb_ep_disable(f_fb->out_ep); |
| 231 | usb_ep_disable(f_fb->in_ep); |
| 232 | |
| 233 | if (f_fb->out_req) { |
| 234 | free(f_fb->out_req->buf); |
| 235 | usb_ep_free_request(f_fb->out_ep, f_fb->out_req); |
| 236 | f_fb->out_req = NULL; |
| 237 | } |
| 238 | if (f_fb->in_req) { |
| 239 | free(f_fb->in_req->buf); |
| 240 | usb_ep_free_request(f_fb->in_ep, f_fb->in_req); |
| 241 | f_fb->in_req = NULL; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | static struct usb_request *fastboot_start_ep(struct usb_ep *ep) |
| 246 | { |
| 247 | struct usb_request *req; |
| 248 | |
| 249 | req = usb_ep_alloc_request(ep, 0); |
| 250 | if (!req) |
| 251 | return NULL; |
| 252 | |
| 253 | req->length = EP_BUFFER_SIZE; |
| 254 | req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE); |
| 255 | if (!req->buf) { |
| 256 | usb_ep_free_request(ep, req); |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | memset(req->buf, 0, req->length); |
| 261 | return req; |
| 262 | } |
| 263 | |
| 264 | static int fastboot_set_alt(struct usb_function *f, |
| 265 | unsigned interface, unsigned alt) |
| 266 | { |
| 267 | int ret; |
| 268 | struct usb_composite_dev *cdev = f->config->cdev; |
| 269 | struct usb_gadget *gadget = cdev->gadget; |
| 270 | struct f_fastboot *f_fb = func_to_fastboot(f); |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 271 | const struct usb_endpoint_descriptor *d; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 272 | |
| 273 | debug("%s: func: %s intf: %d alt: %d\n", |
| 274 | __func__, f->name, interface, alt); |
| 275 | |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 276 | d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out); |
| 277 | ret = usb_ep_enable(f_fb->out_ep, d); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 278 | if (ret) { |
| 279 | puts("failed to enable out ep\n"); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | f_fb->out_req = fastboot_start_ep(f_fb->out_ep); |
| 284 | if (!f_fb->out_req) { |
| 285 | puts("failed to alloc out req\n"); |
| 286 | ret = -EINVAL; |
| 287 | goto err; |
| 288 | } |
| 289 | f_fb->out_req->complete = rx_handler_command; |
| 290 | |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 291 | d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in); |
| 292 | ret = usb_ep_enable(f_fb->in_ep, d); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 293 | if (ret) { |
| 294 | puts("failed to enable in ep\n"); |
| 295 | goto err; |
| 296 | } |
| 297 | |
| 298 | f_fb->in_req = fastboot_start_ep(f_fb->in_ep); |
| 299 | if (!f_fb->in_req) { |
| 300 | puts("failed alloc req in\n"); |
| 301 | ret = -EINVAL; |
| 302 | goto err; |
| 303 | } |
| 304 | f_fb->in_req->complete = fastboot_complete; |
| 305 | |
| 306 | ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0); |
| 307 | if (ret) |
| 308 | goto err; |
| 309 | |
| 310 | return 0; |
| 311 | err: |
| 312 | fastboot_disable(f); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static int fastboot_add(struct usb_configuration *c) |
| 317 | { |
| 318 | struct f_fastboot *f_fb = fastboot_func; |
| 319 | int status; |
| 320 | |
| 321 | debug("%s: cdev: 0x%p\n", __func__, c->cdev); |
| 322 | |
| 323 | if (!f_fb) { |
| 324 | f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb)); |
| 325 | if (!f_fb) |
| 326 | return -ENOMEM; |
| 327 | |
| 328 | fastboot_func = f_fb; |
| 329 | memset(f_fb, 0, sizeof(*f_fb)); |
| 330 | } |
| 331 | |
| 332 | f_fb->usb_function.name = "f_fastboot"; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 333 | f_fb->usb_function.bind = fastboot_bind; |
| 334 | f_fb->usb_function.unbind = fastboot_unbind; |
| 335 | f_fb->usb_function.set_alt = fastboot_set_alt; |
| 336 | f_fb->usb_function.disable = fastboot_disable; |
| 337 | f_fb->usb_function.strings = fastboot_strings; |
| 338 | |
| 339 | status = usb_add_function(c, &f_fb->usb_function); |
| 340 | if (status) { |
| 341 | free(f_fb); |
| 342 | fastboot_func = f_fb; |
| 343 | } |
| 344 | |
| 345 | return status; |
| 346 | } |
| 347 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add); |
| 348 | |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 349 | static int fastboot_tx_write(const char *buffer, unsigned int buffer_size) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 350 | { |
| 351 | struct usb_request *in_req = fastboot_func->in_req; |
| 352 | int ret; |
| 353 | |
| 354 | memcpy(in_req->buf, buffer, buffer_size); |
| 355 | in_req->length = buffer_size; |
Paul Kocialkowski | 2032bfd | 2015-07-04 16:46:16 +0200 | [diff] [blame] | 356 | |
| 357 | usb_ep_dequeue(fastboot_func->in_ep, in_req); |
| 358 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 359 | ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0); |
| 360 | if (ret) |
| 361 | printf("Error %d on queue\n", ret); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static int fastboot_tx_write_str(const char *buffer) |
| 366 | { |
| 367 | return fastboot_tx_write(buffer, strlen(buffer)); |
| 368 | } |
| 369 | |
| 370 | static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) |
| 371 | { |
| 372 | do_reset(NULL, 0, 0, NULL); |
| 373 | } |
| 374 | |
Alexey Firago | 96da306 | 2015-02-25 17:10:47 +0300 | [diff] [blame] | 375 | int __weak fb_set_reboot_flag(void) |
| 376 | { |
| 377 | return -ENOSYS; |
| 378 | } |
| 379 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 380 | static void cb_reboot(struct usb_ep *ep, struct usb_request *req) |
| 381 | { |
Alexey Firago | 96da306 | 2015-02-25 17:10:47 +0300 | [diff] [blame] | 382 | char *cmd = req->buf; |
| 383 | if (!strcmp_l1("reboot-bootloader", cmd)) { |
| 384 | if (fb_set_reboot_flag()) { |
| 385 | fastboot_tx_write_str("FAILCannot set reboot flag"); |
| 386 | return; |
| 387 | } |
| 388 | } |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 389 | fastboot_func->in_req->complete = compl_do_reset; |
| 390 | fastboot_tx_write_str("OKAY"); |
| 391 | } |
| 392 | |
| 393 | static int strcmp_l1(const char *s1, const char *s2) |
| 394 | { |
| 395 | if (!s1 || !s2) |
| 396 | return -1; |
| 397 | return strncmp(s1, s2, strlen(s1)); |
| 398 | } |
| 399 | |
| 400 | static void cb_getvar(struct usb_ep *ep, struct usb_request *req) |
| 401 | { |
| 402 | char *cmd = req->buf; |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 403 | char response[FASTBOOT_RESPONSE_LEN]; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 404 | const char *s; |
Jeroen Hofstee | 36a6b40 | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 405 | size_t chars_left; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 406 | |
| 407 | strcpy(response, "OKAY"); |
Jeroen Hofstee | 36a6b40 | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 408 | chars_left = sizeof(response) - strlen(response) - 1; |
| 409 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 410 | strsep(&cmd, ":"); |
| 411 | if (!cmd) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 412 | pr_err("missing variable"); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 413 | fastboot_tx_write_str("FAILmissing var"); |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | if (!strcmp_l1("version", cmd)) { |
Jeroen Hofstee | 36a6b40 | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 418 | strncat(response, FASTBOOT_VERSION, chars_left); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 419 | } else if (!strcmp_l1("bootloader-version", cmd)) { |
Jeroen Hofstee | 36a6b40 | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 420 | strncat(response, U_BOOT_VERSION, chars_left); |
Eric Nelson | 70567d9a | 2014-09-30 12:05:40 -0700 | [diff] [blame] | 421 | } else if (!strcmp_l1("downloadsize", cmd) || |
| 422 | !strcmp_l1("max-download-size", cmd)) { |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 423 | char str_num[12]; |
| 424 | |
Paul Kocialkowski | f84f091 | 2015-07-20 12:38:22 +0200 | [diff] [blame] | 425 | sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE); |
Jeroen Hofstee | 36a6b40 | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 426 | strncat(response, str_num, chars_left); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 427 | } else if (!strcmp_l1("serialno", cmd)) { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 428 | s = env_get("serial#"); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 429 | if (s) |
Jeroen Hofstee | 36a6b40 | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 430 | strncat(response, s, chars_left); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 431 | else |
| 432 | strcpy(response, "FAILValue not set"); |
| 433 | } else { |
nicolas.le.bayon@st.com | 6c24f09 | 2017-05-09 15:58:36 +0000 | [diff] [blame] | 434 | char *envstr; |
Rob Herring | ec00a0d | 2016-03-17 17:21:23 +0100 | [diff] [blame] | 435 | |
nicolas.le.bayon@st.com | 6c24f09 | 2017-05-09 15:58:36 +0000 | [diff] [blame] | 436 | envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1); |
| 437 | if (!envstr) { |
| 438 | fastboot_tx_write_str("FAILmalloc error"); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | sprintf(envstr, "fastboot.%s", cmd); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 443 | s = env_get(envstr); |
Rob Herring | ec00a0d | 2016-03-17 17:21:23 +0100 | [diff] [blame] | 444 | if (s) { |
| 445 | strncat(response, s, chars_left); |
| 446 | } else { |
| 447 | printf("WARNING: unknown variable: %s\n", cmd); |
| 448 | strcpy(response, "FAILVariable not implemented"); |
| 449 | } |
nicolas.le.bayon@st.com | 6c24f09 | 2017-05-09 15:58:36 +0000 | [diff] [blame] | 450 | |
| 451 | free(envstr); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 452 | } |
| 453 | fastboot_tx_write_str(response); |
| 454 | } |
| 455 | |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 456 | static unsigned int rx_bytes_expected(struct usb_ep *ep) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 457 | { |
| 458 | int rx_remain = download_size - download_bytes; |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 459 | unsigned int rem; |
| 460 | unsigned int maxpacket = ep->maxpacket; |
| 461 | |
| 462 | if (rx_remain <= 0) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 463 | return 0; |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 464 | else if (rx_remain > EP_BUFFER_SIZE) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 465 | return EP_BUFFER_SIZE; |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 466 | |
| 467 | /* |
| 468 | * Some controllers e.g. DWC3 don't like OUT transfers to be |
| 469 | * not ending in maxpacket boundary. So just make them happy by |
| 470 | * always requesting for integral multiple of maxpackets. |
| 471 | * This shouldn't bother controllers that don't care about it. |
| 472 | */ |
| 473 | rem = rx_remain % maxpacket; |
| 474 | if (rem > 0) |
Dileep Katta | 50b4c17 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 475 | rx_remain = rx_remain + (maxpacket - rem); |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 476 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 477 | return rx_remain; |
| 478 | } |
| 479 | |
| 480 | #define BYTES_PER_DOT 0x20000 |
| 481 | static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req) |
| 482 | { |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 483 | char response[FASTBOOT_RESPONSE_LEN]; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 484 | unsigned int transfer_size = download_size - download_bytes; |
| 485 | const unsigned char *buffer = req->buf; |
| 486 | unsigned int buffer_size = req->actual; |
Bo Shen | c65503a | 2014-09-19 14:15:12 +0800 | [diff] [blame] | 487 | unsigned int pre_dot_num, now_dot_num; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 488 | |
| 489 | if (req->status != 0) { |
| 490 | printf("Bad status: %d\n", req->status); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | if (buffer_size < transfer_size) |
| 495 | transfer_size = buffer_size; |
| 496 | |
Paul Kocialkowski | f84f091 | 2015-07-20 12:38:22 +0200 | [diff] [blame] | 497 | memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes, |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 498 | buffer, transfer_size); |
| 499 | |
Bo Shen | c65503a | 2014-09-19 14:15:12 +0800 | [diff] [blame] | 500 | pre_dot_num = download_bytes / BYTES_PER_DOT; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 501 | download_bytes += transfer_size; |
Bo Shen | c65503a | 2014-09-19 14:15:12 +0800 | [diff] [blame] | 502 | now_dot_num = download_bytes / BYTES_PER_DOT; |
| 503 | |
| 504 | if (pre_dot_num != now_dot_num) { |
| 505 | putc('.'); |
| 506 | if (!(now_dot_num % 74)) |
| 507 | putc('\n'); |
| 508 | } |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 509 | |
| 510 | /* Check if transfer is done */ |
| 511 | if (download_bytes >= download_size) { |
| 512 | /* |
| 513 | * Reset global transfer variable, keep download_bytes because |
| 514 | * it will be used in the next possible flashing command |
| 515 | */ |
| 516 | download_size = 0; |
| 517 | req->complete = rx_handler_command; |
| 518 | req->length = EP_BUFFER_SIZE; |
| 519 | |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 520 | strcpy(response, "OKAY"); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 521 | fastboot_tx_write_str(response); |
| 522 | |
| 523 | printf("\ndownloading of %d bytes finished\n", download_bytes); |
| 524 | } else { |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 525 | req->length = rx_bytes_expected(ep); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 526 | } |
| 527 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 528 | req->actual = 0; |
| 529 | usb_ep_queue(ep, req, 0); |
| 530 | } |
| 531 | |
| 532 | static void cb_download(struct usb_ep *ep, struct usb_request *req) |
| 533 | { |
| 534 | char *cmd = req->buf; |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 535 | char response[FASTBOOT_RESPONSE_LEN]; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 536 | |
| 537 | strsep(&cmd, ":"); |
| 538 | download_size = simple_strtoul(cmd, NULL, 16); |
| 539 | download_bytes = 0; |
| 540 | |
| 541 | printf("Starting download of %d bytes\n", download_size); |
| 542 | |
| 543 | if (0 == download_size) { |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 544 | strcpy(response, "FAILdata invalid size"); |
Paul Kocialkowski | f84f091 | 2015-07-20 12:38:22 +0200 | [diff] [blame] | 545 | } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) { |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 546 | download_size = 0; |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 547 | strcpy(response, "FAILdata too large"); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 548 | } else { |
| 549 | sprintf(response, "DATA%08x", download_size); |
| 550 | req->complete = rx_handler_dl_image; |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 551 | req->length = rx_bytes_expected(ep); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 552 | } |
| 553 | fastboot_tx_write_str(response); |
| 554 | } |
| 555 | |
| 556 | static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req) |
| 557 | { |
| 558 | char boot_addr_start[12]; |
| 559 | char *bootm_args[] = { "bootm", boot_addr_start, NULL }; |
| 560 | |
| 561 | puts("Booting kernel..\n"); |
| 562 | |
Tom Rini | 4c753a2 | 2017-08-22 08:20:02 -0400 | [diff] [blame] | 563 | sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 564 | do_bootm(NULL, 0, 2, bootm_args); |
| 565 | |
| 566 | /* This only happens if image is somehow faulty so we start over */ |
| 567 | do_reset(NULL, 0, 0, NULL); |
| 568 | } |
| 569 | |
| 570 | static void cb_boot(struct usb_ep *ep, struct usb_request *req) |
| 571 | { |
| 572 | fastboot_func->in_req->complete = do_bootm_on_complete; |
| 573 | fastboot_tx_write_str("OKAY"); |
| 574 | } |
| 575 | |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 576 | static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req) |
| 577 | { |
| 578 | g_dnl_trigger_detach(); |
| 579 | } |
| 580 | |
| 581 | static void cb_continue(struct usb_ep *ep, struct usb_request *req) |
| 582 | { |
| 583 | fastboot_func->in_req->complete = do_exit_on_complete; |
| 584 | fastboot_tx_write_str("OKAY"); |
| 585 | } |
| 586 | |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 587 | #ifdef CONFIG_FASTBOOT_FLASH |
| 588 | static void cb_flash(struct usb_ep *ep, struct usb_request *req) |
| 589 | { |
| 590 | char *cmd = req->buf; |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 591 | char response[FASTBOOT_RESPONSE_LEN]; |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 592 | |
| 593 | strsep(&cmd, ":"); |
| 594 | if (!cmd) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 595 | pr_err("missing partition name"); |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 596 | fastboot_tx_write_str("FAILmissing partition name"); |
| 597 | return; |
| 598 | } |
| 599 | |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 600 | /* initialize the response buffer */ |
| 601 | fb_response_str = response; |
| 602 | |
| 603 | fastboot_fail("no flash device defined"); |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 604 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
Steve Rae | f1f112f | 2016-06-07 11:19:35 -0700 | [diff] [blame] | 605 | fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR, |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 606 | download_bytes); |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 607 | #endif |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 608 | #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV |
Steve Rae | f1f112f | 2016-06-07 11:19:35 -0700 | [diff] [blame] | 609 | fb_nand_flash_write(cmd, |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 610 | (void *)CONFIG_FASTBOOT_BUF_ADDR, |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 611 | download_bytes); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 612 | #endif |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 613 | fastboot_tx_write_str(response); |
| 614 | } |
| 615 | #endif |
| 616 | |
Michael Scott | 7e23abf | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 617 | static void cb_oem(struct usb_ep *ep, struct usb_request *req) |
| 618 | { |
| 619 | char *cmd = req->buf; |
Maxime Ripard | 78a52ca | 2015-10-15 22:04:04 +0200 | [diff] [blame] | 620 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
Rob Herring | 13047e5 | 2015-01-26 15:49:01 -0600 | [diff] [blame] | 621 | if (strncmp("format", cmd + 4, 6) == 0) { |
| 622 | char cmdbuf[32]; |
| 623 | sprintf(cmdbuf, "gpt write mmc %x $partitions", |
| 624 | CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 625 | if (run_command(cmdbuf, 0)) |
| 626 | fastboot_tx_write_str("FAIL"); |
| 627 | else |
| 628 | fastboot_tx_write_str("OKAY"); |
| 629 | } else |
| 630 | #endif |
Michael Scott | 7e23abf | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 631 | if (strncmp("unlock", cmd + 4, 8) == 0) { |
| 632 | fastboot_tx_write_str("FAILnot implemented"); |
| 633 | } |
| 634 | else { |
| 635 | fastboot_tx_write_str("FAILunknown oem command"); |
| 636 | } |
| 637 | } |
| 638 | |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 639 | #ifdef CONFIG_FASTBOOT_FLASH |
| 640 | static void cb_erase(struct usb_ep *ep, struct usb_request *req) |
| 641 | { |
| 642 | char *cmd = req->buf; |
Maxime Ripard | 00e8eb7 | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 643 | char response[FASTBOOT_RESPONSE_LEN]; |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 644 | |
| 645 | strsep(&cmd, ":"); |
| 646 | if (!cmd) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 647 | pr_err("missing partition name"); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 648 | fastboot_tx_write_str("FAILmissing partition name"); |
| 649 | return; |
| 650 | } |
| 651 | |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 652 | /* initialize the response buffer */ |
| 653 | fb_response_str = response; |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 654 | |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 655 | fastboot_fail("no flash device defined"); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 656 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 657 | fb_mmc_erase(cmd); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 658 | #endif |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 659 | #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV |
Steve Rae | 76336b2 | 2016-06-07 11:19:37 -0700 | [diff] [blame] | 660 | fb_nand_erase(cmd); |
Maxime Ripard | 33602a9 | 2015-10-15 14:34:17 +0200 | [diff] [blame] | 661 | #endif |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 662 | fastboot_tx_write_str(response); |
| 663 | } |
| 664 | #endif |
| 665 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 666 | struct cmd_dispatch_info { |
| 667 | char *cmd; |
| 668 | void (*cb)(struct usb_ep *ep, struct usb_request *req); |
| 669 | }; |
| 670 | |
| 671 | static const struct cmd_dispatch_info cmd_dispatch_info[] = { |
| 672 | { |
| 673 | .cmd = "reboot", |
| 674 | .cb = cb_reboot, |
| 675 | }, { |
| 676 | .cmd = "getvar:", |
| 677 | .cb = cb_getvar, |
| 678 | }, { |
| 679 | .cmd = "download:", |
| 680 | .cb = cb_download, |
| 681 | }, { |
| 682 | .cmd = "boot", |
| 683 | .cb = cb_boot, |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 684 | }, { |
| 685 | .cmd = "continue", |
| 686 | .cb = cb_continue, |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 687 | }, |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 688 | #ifdef CONFIG_FASTBOOT_FLASH |
| 689 | { |
| 690 | .cmd = "flash", |
| 691 | .cb = cb_flash, |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 692 | }, { |
| 693 | .cmd = "erase", |
| 694 | .cb = cb_erase, |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 695 | }, |
| 696 | #endif |
Michael Scott | 7e23abf | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 697 | { |
| 698 | .cmd = "oem", |
| 699 | .cb = cb_oem, |
| 700 | }, |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 701 | }; |
| 702 | |
| 703 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) |
| 704 | { |
| 705 | char *cmdbuf = req->buf; |
| 706 | void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL; |
| 707 | int i; |
| 708 | |
Paul Kocialkowski | 4a126b6 | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 709 | if (req->status != 0 || req->length == 0) |
| 710 | return; |
| 711 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 712 | for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) { |
| 713 | if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) { |
| 714 | func_cb = cmd_dispatch_info[i].cb; |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 | |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 719 | if (!func_cb) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 720 | pr_err("unknown command: %.*s", req->actual, cmdbuf); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 721 | fastboot_tx_write_str("FAILunknown command"); |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 722 | } else { |
Eric Nelson | 3d626cd | 2014-10-01 14:30:56 -0700 | [diff] [blame] | 723 | if (req->actual < req->length) { |
| 724 | u8 *buf = (u8 *)req->buf; |
| 725 | buf[req->actual] = 0; |
| 726 | func_cb(ep, req); |
| 727 | } else { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 728 | pr_err("buffer overflow"); |
Eric Nelson | 3d626cd | 2014-10-01 14:30:56 -0700 | [diff] [blame] | 729 | fastboot_tx_write_str("FAILbuffer overflow"); |
| 730 | } |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 731 | } |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 732 | |
Paul Kocialkowski | 4a126b6 | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 733 | *cmdbuf = '\0'; |
| 734 | req->actual = 0; |
| 735 | usb_ep_queue(ep, req, 0); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 736 | } |