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> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 21 | #include <g_dnl.h> |
| 22 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 23 | #define FASTBOOT_INTERFACE_CLASS 0xff |
| 24 | #define FASTBOOT_INTERFACE_SUB_CLASS 0x42 |
| 25 | #define FASTBOOT_INTERFACE_PROTOCOL 0x03 |
| 26 | |
| 27 | #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200) |
| 28 | #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040) |
| 29 | #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040) |
| 30 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 31 | #define EP_BUFFER_SIZE 4096 |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 32 | /* |
| 33 | * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size |
| 34 | * (64 or 512 or 1024), else we break on certain controllers like DWC3 |
| 35 | * that expect bulk OUT requests to be divisible by maxpacket size. |
| 36 | */ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 37 | |
| 38 | struct f_fastboot { |
| 39 | struct usb_function usb_function; |
| 40 | |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 41 | /* IN/OUT EP's and corresponding requests */ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 42 | struct usb_ep *in_ep, *out_ep; |
| 43 | struct usb_request *in_req, *out_req; |
| 44 | }; |
| 45 | |
| 46 | static inline struct f_fastboot *func_to_fastboot(struct usb_function *f) |
| 47 | { |
| 48 | return container_of(f, struct f_fastboot, usb_function); |
| 49 | } |
| 50 | |
| 51 | static struct f_fastboot *fastboot_func; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 52 | |
| 53 | static struct usb_endpoint_descriptor fs_ep_in = { |
| 54 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 55 | .bDescriptorType = USB_DT_ENDPOINT, |
| 56 | .bEndpointAddress = USB_DIR_IN, |
| 57 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 58 | .wMaxPacketSize = cpu_to_le16(64), |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static struct usb_endpoint_descriptor fs_ep_out = { |
| 62 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 63 | .bDescriptorType = USB_DT_ENDPOINT, |
| 64 | .bEndpointAddress = USB_DIR_OUT, |
| 65 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 66 | .wMaxPacketSize = cpu_to_le16(64), |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 69 | static struct usb_endpoint_descriptor hs_ep_in = { |
| 70 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 71 | .bDescriptorType = USB_DT_ENDPOINT, |
| 72 | .bEndpointAddress = USB_DIR_IN, |
| 73 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 74 | .wMaxPacketSize = cpu_to_le16(512), |
| 75 | }; |
| 76 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 77 | static struct usb_endpoint_descriptor hs_ep_out = { |
| 78 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 79 | .bDescriptorType = USB_DT_ENDPOINT, |
| 80 | .bEndpointAddress = USB_DIR_OUT, |
| 81 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 82 | .wMaxPacketSize = cpu_to_le16(512), |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static struct usb_interface_descriptor interface_desc = { |
| 86 | .bLength = USB_DT_INTERFACE_SIZE, |
| 87 | .bDescriptorType = USB_DT_INTERFACE, |
| 88 | .bInterfaceNumber = 0x00, |
| 89 | .bAlternateSetting = 0x00, |
| 90 | .bNumEndpoints = 0x02, |
| 91 | .bInterfaceClass = FASTBOOT_INTERFACE_CLASS, |
| 92 | .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS, |
| 93 | .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL, |
| 94 | }; |
| 95 | |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 96 | static struct usb_descriptor_header *fb_fs_function[] = { |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 97 | (struct usb_descriptor_header *)&interface_desc, |
| 98 | (struct usb_descriptor_header *)&fs_ep_in, |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 99 | (struct usb_descriptor_header *)&fs_ep_out, |
| 100 | }; |
| 101 | |
| 102 | static struct usb_descriptor_header *fb_hs_function[] = { |
| 103 | (struct usb_descriptor_header *)&interface_desc, |
| 104 | (struct usb_descriptor_header *)&hs_ep_in, |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 105 | (struct usb_descriptor_header *)&hs_ep_out, |
| 106 | NULL, |
| 107 | }; |
| 108 | |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 109 | static struct usb_endpoint_descriptor * |
| 110 | fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, |
| 111 | struct usb_endpoint_descriptor *hs) |
| 112 | { |
| 113 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 114 | return hs; |
| 115 | return fs; |
| 116 | } |
| 117 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 118 | /* |
| 119 | * static strings, in UTF-8 |
| 120 | */ |
| 121 | static const char fastboot_name[] = "Android Fastboot"; |
| 122 | |
| 123 | static struct usb_string fastboot_string_defs[] = { |
| 124 | [0].s = fastboot_name, |
| 125 | { } /* end of list */ |
| 126 | }; |
| 127 | |
| 128 | static struct usb_gadget_strings stringtab_fastboot = { |
| 129 | .language = 0x0409, /* en-us */ |
| 130 | .strings = fastboot_string_defs, |
| 131 | }; |
| 132 | |
| 133 | static struct usb_gadget_strings *fastboot_strings[] = { |
| 134 | &stringtab_fastboot, |
| 135 | NULL, |
| 136 | }; |
| 137 | |
| 138 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); |
| 139 | |
| 140 | static void fastboot_complete(struct usb_ep *ep, struct usb_request *req) |
| 141 | { |
| 142 | int status = req->status; |
| 143 | if (!status) |
| 144 | return; |
| 145 | printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual); |
| 146 | } |
| 147 | |
| 148 | static int fastboot_bind(struct usb_configuration *c, struct usb_function *f) |
| 149 | { |
| 150 | int id; |
| 151 | struct usb_gadget *gadget = c->cdev->gadget; |
| 152 | struct f_fastboot *f_fb = func_to_fastboot(f); |
Dileep Katta | 447c975 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 153 | const char *s; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 154 | |
| 155 | /* DYNAMIC interface numbers assignments */ |
| 156 | id = usb_interface_id(c, f); |
| 157 | if (id < 0) |
| 158 | return id; |
| 159 | interface_desc.bInterfaceNumber = id; |
| 160 | |
| 161 | id = usb_string_id(c->cdev); |
| 162 | if (id < 0) |
| 163 | return id; |
| 164 | fastboot_string_defs[0].id = id; |
| 165 | interface_desc.iInterface = id; |
| 166 | |
| 167 | f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in); |
| 168 | if (!f_fb->in_ep) |
| 169 | return -ENODEV; |
| 170 | f_fb->in_ep->driver_data = c->cdev; |
| 171 | |
| 172 | f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out); |
| 173 | if (!f_fb->out_ep) |
| 174 | return -ENODEV; |
| 175 | f_fb->out_ep->driver_data = c->cdev; |
| 176 | |
Roger Quadros | bf4b41a | 2016-04-12 15:51:48 +0300 | [diff] [blame] | 177 | f->descriptors = fb_fs_function; |
| 178 | |
| 179 | if (gadget_is_dualspeed(gadget)) { |
| 180 | /* Assume endpoint addresses are the same for both speeds */ |
| 181 | hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress; |
| 182 | hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; |
| 183 | /* copy HS descriptors */ |
| 184 | f->hs_descriptors = fb_hs_function; |
| 185 | } |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 186 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 187 | s = env_get("serial#"); |
Dileep Katta | 447c975 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 188 | if (s) |
| 189 | g_dnl_set_serialnumber((char *)s); |
| 190 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f) |
| 195 | { |
| 196 | memset(fastboot_func, 0, sizeof(*fastboot_func)); |
| 197 | } |
| 198 | |
| 199 | static void fastboot_disable(struct usb_function *f) |
| 200 | { |
| 201 | struct f_fastboot *f_fb = func_to_fastboot(f); |
| 202 | |
| 203 | usb_ep_disable(f_fb->out_ep); |
| 204 | usb_ep_disable(f_fb->in_ep); |
| 205 | |
| 206 | if (f_fb->out_req) { |
| 207 | free(f_fb->out_req->buf); |
| 208 | usb_ep_free_request(f_fb->out_ep, f_fb->out_req); |
| 209 | f_fb->out_req = NULL; |
| 210 | } |
| 211 | if (f_fb->in_req) { |
| 212 | free(f_fb->in_req->buf); |
| 213 | usb_ep_free_request(f_fb->in_ep, f_fb->in_req); |
| 214 | f_fb->in_req = NULL; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static struct usb_request *fastboot_start_ep(struct usb_ep *ep) |
| 219 | { |
| 220 | struct usb_request *req; |
| 221 | |
| 222 | req = usb_ep_alloc_request(ep, 0); |
| 223 | if (!req) |
| 224 | return NULL; |
| 225 | |
| 226 | req->length = EP_BUFFER_SIZE; |
| 227 | req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE); |
| 228 | if (!req->buf) { |
| 229 | usb_ep_free_request(ep, req); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | memset(req->buf, 0, req->length); |
| 234 | return req; |
| 235 | } |
| 236 | |
| 237 | static int fastboot_set_alt(struct usb_function *f, |
| 238 | unsigned interface, unsigned alt) |
| 239 | { |
| 240 | int ret; |
| 241 | struct usb_composite_dev *cdev = f->config->cdev; |
| 242 | struct usb_gadget *gadget = cdev->gadget; |
| 243 | struct f_fastboot *f_fb = func_to_fastboot(f); |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 244 | const struct usb_endpoint_descriptor *d; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 245 | |
| 246 | debug("%s: func: %s intf: %d alt: %d\n", |
| 247 | __func__, f->name, interface, alt); |
| 248 | |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 249 | d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out); |
| 250 | ret = usb_ep_enable(f_fb->out_ep, d); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 251 | if (ret) { |
| 252 | puts("failed to enable out ep\n"); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | f_fb->out_req = fastboot_start_ep(f_fb->out_ep); |
| 257 | if (!f_fb->out_req) { |
| 258 | puts("failed to alloc out req\n"); |
| 259 | ret = -EINVAL; |
| 260 | goto err; |
| 261 | } |
| 262 | f_fb->out_req->complete = rx_handler_command; |
| 263 | |
Roger Quadros | 9ab3fdf | 2016-04-13 11:30:00 +0300 | [diff] [blame] | 264 | d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in); |
| 265 | ret = usb_ep_enable(f_fb->in_ep, d); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 266 | if (ret) { |
| 267 | puts("failed to enable in ep\n"); |
| 268 | goto err; |
| 269 | } |
| 270 | |
| 271 | f_fb->in_req = fastboot_start_ep(f_fb->in_ep); |
| 272 | if (!f_fb->in_req) { |
| 273 | puts("failed alloc req in\n"); |
| 274 | ret = -EINVAL; |
| 275 | goto err; |
| 276 | } |
| 277 | f_fb->in_req->complete = fastboot_complete; |
| 278 | |
| 279 | ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0); |
| 280 | if (ret) |
| 281 | goto err; |
| 282 | |
| 283 | return 0; |
| 284 | err: |
| 285 | fastboot_disable(f); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static int fastboot_add(struct usb_configuration *c) |
| 290 | { |
| 291 | struct f_fastboot *f_fb = fastboot_func; |
| 292 | int status; |
| 293 | |
| 294 | debug("%s: cdev: 0x%p\n", __func__, c->cdev); |
| 295 | |
| 296 | if (!f_fb) { |
| 297 | f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb)); |
| 298 | if (!f_fb) |
| 299 | return -ENOMEM; |
| 300 | |
| 301 | fastboot_func = f_fb; |
| 302 | memset(f_fb, 0, sizeof(*f_fb)); |
| 303 | } |
| 304 | |
| 305 | f_fb->usb_function.name = "f_fastboot"; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 306 | f_fb->usb_function.bind = fastboot_bind; |
| 307 | f_fb->usb_function.unbind = fastboot_unbind; |
| 308 | f_fb->usb_function.set_alt = fastboot_set_alt; |
| 309 | f_fb->usb_function.disable = fastboot_disable; |
| 310 | f_fb->usb_function.strings = fastboot_strings; |
| 311 | |
| 312 | status = usb_add_function(c, &f_fb->usb_function); |
| 313 | if (status) { |
| 314 | free(f_fb); |
| 315 | fastboot_func = f_fb; |
| 316 | } |
| 317 | |
| 318 | return status; |
| 319 | } |
| 320 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add); |
| 321 | |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 322 | static int fastboot_tx_write(const char *buffer, unsigned int buffer_size) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 323 | { |
| 324 | struct usb_request *in_req = fastboot_func->in_req; |
| 325 | int ret; |
| 326 | |
| 327 | memcpy(in_req->buf, buffer, buffer_size); |
| 328 | in_req->length = buffer_size; |
Paul Kocialkowski | 2032bfd | 2015-07-04 16:46:16 +0200 | [diff] [blame] | 329 | |
| 330 | usb_ep_dequeue(fastboot_func->in_ep, in_req); |
| 331 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 332 | ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0); |
| 333 | if (ret) |
| 334 | printf("Error %d on queue\n", ret); |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int fastboot_tx_write_str(const char *buffer) |
| 339 | { |
| 340 | return fastboot_tx_write(buffer, strlen(buffer)); |
| 341 | } |
| 342 | |
| 343 | static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) |
| 344 | { |
| 345 | do_reset(NULL, 0, 0, NULL); |
| 346 | } |
| 347 | |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 348 | static unsigned int rx_bytes_expected(struct usb_ep *ep) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 349 | { |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 350 | int rx_remain = fastboot_data_remaining(); |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 351 | unsigned int rem; |
| 352 | unsigned int maxpacket = ep->maxpacket; |
| 353 | |
| 354 | if (rx_remain <= 0) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 355 | return 0; |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 356 | else if (rx_remain > EP_BUFFER_SIZE) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 357 | return EP_BUFFER_SIZE; |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 358 | |
| 359 | /* |
| 360 | * Some controllers e.g. DWC3 don't like OUT transfers to be |
| 361 | * not ending in maxpacket boundary. So just make them happy by |
| 362 | * always requesting for integral multiple of maxpackets. |
| 363 | * This shouldn't bother controllers that don't care about it. |
| 364 | */ |
| 365 | rem = rx_remain % maxpacket; |
| 366 | if (rem > 0) |
Dileep Katta | 50b4c17 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 367 | rx_remain = rx_remain + (maxpacket - rem); |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 368 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 369 | return rx_remain; |
| 370 | } |
| 371 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 372 | static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req) |
| 373 | { |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 374 | char response[FASTBOOT_RESPONSE_LEN] = {0}; |
| 375 | unsigned int transfer_size = fastboot_data_remaining(); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 376 | const unsigned char *buffer = req->buf; |
| 377 | unsigned int buffer_size = req->actual; |
| 378 | |
| 379 | if (req->status != 0) { |
| 380 | printf("Bad status: %d\n", req->status); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | if (buffer_size < transfer_size) |
| 385 | transfer_size = buffer_size; |
| 386 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 387 | fastboot_data_download(buffer, transfer_size, response); |
| 388 | if (response[0]) { |
| 389 | fastboot_tx_write_str(response); |
| 390 | } else if (!fastboot_data_remaining()) { |
| 391 | fastboot_data_complete(response); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 392 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 393 | /* |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 394 | * Reset global transfer variable |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 395 | */ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 396 | req->complete = rx_handler_command; |
| 397 | req->length = EP_BUFFER_SIZE; |
| 398 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 399 | fastboot_tx_write_str(response); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 400 | } else { |
Roger Quadros | aefb021 | 2016-04-19 10:16:59 +0300 | [diff] [blame] | 401 | req->length = rx_bytes_expected(ep); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 404 | req->actual = 0; |
| 405 | usb_ep_queue(ep, req, 0); |
| 406 | } |
| 407 | |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 408 | static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req) |
| 409 | { |
| 410 | g_dnl_trigger_detach(); |
| 411 | } |
| 412 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 413 | static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req) |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 414 | { |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 415 | fastboot_boot(); |
| 416 | do_exit_on_complete(ep, req); |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 417 | } |
| 418 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 419 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 420 | { |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 421 | char *cmdbuf = req->buf; |
| 422 | char response[FASTBOOT_RESPONSE_LEN] = {0}; |
| 423 | int cmd = -1; |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 424 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 425 | if (req->status != 0 || req->length == 0) |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 426 | return; |
Steve Rae | bfb9ba4 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 427 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 428 | if (req->actual < req->length) { |
| 429 | cmdbuf[req->actual] = '\0'; |
| 430 | cmd = fastboot_handle_command(cmdbuf, response); |
| 431 | } else { |
| 432 | pr_err("buffer overflow"); |
| 433 | fastboot_fail("buffer overflow", response); |
Michael Scott | 7e23abf | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 434 | } |
Michael Scott | 7e23abf | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 435 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 436 | if (!strncmp("DATA", response, 4)) { |
| 437 | req->complete = rx_handler_dl_image; |
| 438 | req->length = rx_bytes_expected(ep); |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 439 | } |
| 440 | |
Dileep Katta | b122820 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 441 | fastboot_tx_write_str(response); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 442 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 443 | if (!strncmp("OKAY", response, 4)) { |
| 444 | switch (cmd) { |
| 445 | case FASTBOOT_COMMAND_BOOT: |
| 446 | fastboot_func->in_req->complete = do_bootm_on_complete; |
| 447 | break; |
Paul Kocialkowski | 4a126b6 | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 448 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 449 | case FASTBOOT_COMMAND_CONTINUE: |
| 450 | fastboot_func->in_req->complete = do_exit_on_complete; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 451 | break; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 452 | |
Alex Kiernan | e80d294 | 2018-05-29 15:30:55 +0000 | [diff] [blame] | 453 | case FASTBOOT_COMMAND_REBOOT: |
| 454 | case FASTBOOT_COMMAND_REBOOT_BOOTLOADER: |
| 455 | fastboot_func->in_req->complete = compl_do_reset; |
| 456 | break; |
Eric Nelson | 3d626cd | 2014-10-01 14:30:56 -0700 | [diff] [blame] | 457 | } |
Steve Rae | 56ad792 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 458 | } |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 459 | |
Paul Kocialkowski | 4a126b6 | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 460 | *cmdbuf = '\0'; |
| 461 | req->actual = 0; |
| 462 | usb_ep_queue(ep, req, 0); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 463 | } |