blob: fc27dbe8de4996e6488adf0b436cf2d036317cc1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior3aab70a2014-05-05 15:08:10 -05002/*
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 Siewior3aab70a2014-05-05 15:08:10 -050011 */
Steve Rae593cbd92014-08-26 11:47:29 -070012#include <config.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050013#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -060014#include <env.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050015#include <errno.h>
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020016#include <fastboot.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050017#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 Siewior3aab70a2014-05-05 15:08:10 -050022#include <g_dnl.h>
23
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050024#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 Siewior3aab70a2014-05-05 15:08:10 -050032#define EP_BUFFER_SIZE 4096
Roger Quadrosac484c52016-04-19 10:16:59 +030033/*
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 Siewior3aab70a2014-05-05 15:08:10 -050038
39struct f_fastboot {
40 struct usb_function usb_function;
41
Steve Rae593cbd92014-08-26 11:47:29 -070042 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050043 struct usb_ep *in_ep, *out_ep;
44 struct usb_request *in_req, *out_req;
45};
46
47static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
48{
49 return container_of(f, struct f_fastboot, usb_function);
50}
51
52static struct f_fastboot *fastboot_func;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050053
54static 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 Quadros718156a2016-04-12 15:51:48 +030059 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050060};
61
62static 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 Quadros718156a2016-04-12 15:51:48 +030067 .wMaxPacketSize = cpu_to_le16(64),
68};
69
70static 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 Siewior3aab70a2014-05-05 15:08:10 -050076};
77
78static 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 Quadros718156a2016-04-12 15:51:48 +030083 .wMaxPacketSize = cpu_to_le16(512),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050084};
85
86static 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 Quadros718156a2016-04-12 15:51:48 +030097static struct usb_descriptor_header *fb_fs_function[] = {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050098 (struct usb_descriptor_header *)&interface_desc,
99 (struct usb_descriptor_header *)&fs_ep_in,
Roger Quadros718156a2016-04-12 15:51:48 +0300100 (struct usb_descriptor_header *)&fs_ep_out,
101};
102
103static struct usb_descriptor_header *fb_hs_function[] = {
104 (struct usb_descriptor_header *)&interface_desc,
105 (struct usb_descriptor_header *)&hs_ep_in,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500106 (struct usb_descriptor_header *)&hs_ep_out,
107 NULL,
108};
109
Roger Quadros8b704a02016-04-13 11:30:00 +0300110static struct usb_endpoint_descriptor *
111fb_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 Siewior3aab70a2014-05-05 15:08:10 -0500119/*
120 * static strings, in UTF-8
121 */
122static const char fastboot_name[] = "Android Fastboot";
123
124static struct usb_string fastboot_string_defs[] = {
125 [0].s = fastboot_name,
126 { } /* end of list */
127};
128
129static struct usb_gadget_strings stringtab_fastboot = {
130 .language = 0x0409, /* en-us */
131 .strings = fastboot_string_defs,
132};
133
134static struct usb_gadget_strings *fastboot_strings[] = {
135 &stringtab_fastboot,
136 NULL,
137};
138
139static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
140
141static 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
149static 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 Katta537cd072015-02-13 14:33:43 +0800154 const char *s;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500155
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 Quadros718156a2016-04-12 15:51:48 +0300178 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 Siewior3aab70a2014-05-05 15:08:10 -0500187
Simon Glass00caae62017-08-03 12:22:12 -0600188 s = env_get("serial#");
Dileep Katta537cd072015-02-13 14:33:43 +0800189 if (s)
190 g_dnl_set_serialnumber((char *)s);
191
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500192 return 0;
193}
194
195static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
196{
197 memset(fastboot_func, 0, sizeof(*fastboot_func));
198}
199
200static 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
219static 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
238static 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 Quadros8b704a02016-04-13 11:30:00 +0300245 const struct usb_endpoint_descriptor *d;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500246
247 debug("%s: func: %s intf: %d alt: %d\n",
248 __func__, f->name, interface, alt);
249
Roger Quadros8b704a02016-04-13 11:30:00 +0300250 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
251 ret = usb_ep_enable(f_fb->out_ep, d);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500252 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 Quadros8b704a02016-04-13 11:30:00 +0300265 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
266 ret = usb_ep_enable(f_fb->in_ep, d);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500267 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;
285err:
286 fastboot_disable(f);
287 return ret;
288}
289
290static 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 Siewior3aab70a2014-05-05 15:08:10 -0500307 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}
321DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
322
Steve Rae593cbd92014-08-26 11:47:29 -0700323static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500324{
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 Kocialkowskibc9071c2015-07-04 16:46:16 +0200330
331 usb_ep_dequeue(fastboot_func->in_ep, in_req);
332
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500333 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
339static int fastboot_tx_write_str(const char *buffer)
340{
341 return fastboot_tx_write(buffer, strlen(buffer));
342}
343
344static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
345{
346 do_reset(NULL, 0, 0, NULL);
347}
348
Roger Quadrosac484c52016-04-19 10:16:59 +0300349static unsigned int rx_bytes_expected(struct usb_ep *ep)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500350{
Alex Kiernan65c96752018-05-29 15:30:55 +0000351 int rx_remain = fastboot_data_remaining();
Roger Quadrosac484c52016-04-19 10:16:59 +0300352 unsigned int rem;
353 unsigned int maxpacket = ep->maxpacket;
354
355 if (rx_remain <= 0)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500356 return 0;
Roger Quadrosac484c52016-04-19 10:16:59 +0300357 else if (rx_remain > EP_BUFFER_SIZE)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500358 return EP_BUFFER_SIZE;
Roger Quadrosac484c52016-04-19 10:16:59 +0300359
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 Katta9e4b5102015-02-17 02:02:36 +0530368 rx_remain = rx_remain + (maxpacket - rem);
Roger Quadrosac484c52016-04-19 10:16:59 +0300369
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500370 return rx_remain;
371}
372
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500373static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
374{
Alex Kiernan65c96752018-05-29 15:30:55 +0000375 char response[FASTBOOT_RESPONSE_LEN] = {0};
376 unsigned int transfer_size = fastboot_data_remaining();
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500377 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 Kiernan65c96752018-05-29 15:30:55 +0000388 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 Siewior3aab70a2014-05-05 15:08:10 -0500393
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500394 /*
Alex Kiernan65c96752018-05-29 15:30:55 +0000395 * Reset global transfer variable
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500396 */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500397 req->complete = rx_handler_command;
398 req->length = EP_BUFFER_SIZE;
399
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500400 fastboot_tx_write_str(response);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500401 } else {
Roger Quadrosac484c52016-04-19 10:16:59 +0300402 req->length = rx_bytes_expected(ep);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500403 }
404
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500405 req->actual = 0;
406 usb_ep_queue(ep, req, 0);
407}
408
Rob Herring267abc62014-12-10 14:43:04 -0600409static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
410{
411 g_dnl_trigger_detach();
412}
413
Alex Kiernan65c96752018-05-29 15:30:55 +0000414static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
Rob Herring267abc62014-12-10 14:43:04 -0600415{
Alex Kiernan65c96752018-05-29 15:30:55 +0000416 fastboot_boot();
417 do_exit_on_complete(ep, req);
Rob Herring267abc62014-12-10 14:43:04 -0600418}
419
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500420static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
421{
422 char *cmdbuf = req->buf;
Alex Kiernan65c96752018-05-29 15:30:55 +0000423 char response[FASTBOOT_RESPONSE_LEN] = {0};
424 int cmd = -1;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500425
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200426 if (req->status != 0 || req->length == 0)
427 return;
428
Alex Kiernan65c96752018-05-29 15:30:55 +0000429 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 Siewior3aab70a2014-05-05 15:08:10 -0500435 }
436
Alex Kiernan65c96752018-05-29 15:30:55 +0000437 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 Nelsone2140582014-10-01 14:30:56 -0700458 }
Steve Rae593cbd92014-08-26 11:47:29 -0700459 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500460
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200461 *cmdbuf = '\0';
462 req->actual = 0;
463 usb_ep_queue(ep, req, 0);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500464}