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