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