blob: 3ad4346f2d099b77a243dc88394b24f409408473 [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>
14#include <errno.h>
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020015#include <fastboot.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050016#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 Siewior3aab70a2014-05-05 15:08:10 -050021#include <g_dnl.h>
22
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050023#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 Siewior3aab70a2014-05-05 15:08:10 -050031#define EP_BUFFER_SIZE 4096
Roger Quadrosac484c52016-04-19 10:16:59 +030032/*
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 Siewior3aab70a2014-05-05 15:08:10 -050037
38struct f_fastboot {
39 struct usb_function usb_function;
40
Steve Rae593cbd92014-08-26 11:47:29 -070041 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050042 struct usb_ep *in_ep, *out_ep;
43 struct usb_request *in_req, *out_req;
44};
45
46static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
47{
48 return container_of(f, struct f_fastboot, usb_function);
49}
50
51static struct f_fastboot *fastboot_func;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050052
53static 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 Quadros718156a2016-04-12 15:51:48 +030058 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050059};
60
61static 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 Quadros718156a2016-04-12 15:51:48 +030066 .wMaxPacketSize = cpu_to_le16(64),
67};
68
69static 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),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050075};
76
77static 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 Quadros718156a2016-04-12 15:51:48 +030082 .wMaxPacketSize = cpu_to_le16(512),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050083};
84
85static 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 Quadros718156a2016-04-12 15:51:48 +030096static struct usb_descriptor_header *fb_fs_function[] = {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050097 (struct usb_descriptor_header *)&interface_desc,
98 (struct usb_descriptor_header *)&fs_ep_in,
Roger Quadros718156a2016-04-12 15:51:48 +030099 (struct usb_descriptor_header *)&fs_ep_out,
100};
101
102static struct usb_descriptor_header *fb_hs_function[] = {
103 (struct usb_descriptor_header *)&interface_desc,
104 (struct usb_descriptor_header *)&hs_ep_in,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500105 (struct usb_descriptor_header *)&hs_ep_out,
106 NULL,
107};
108
Roger Quadros8b704a02016-04-13 11:30:00 +0300109static struct usb_endpoint_descriptor *
110fb_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 Siewior3aab70a2014-05-05 15:08:10 -0500118/*
119 * static strings, in UTF-8
120 */
121static const char fastboot_name[] = "Android Fastboot";
122
123static struct usb_string fastboot_string_defs[] = {
124 [0].s = fastboot_name,
125 { } /* end of list */
126};
127
128static struct usb_gadget_strings stringtab_fastboot = {
129 .language = 0x0409, /* en-us */
130 .strings = fastboot_string_defs,
131};
132
133static struct usb_gadget_strings *fastboot_strings[] = {
134 &stringtab_fastboot,
135 NULL,
136};
137
138static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
139
140static 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
148static 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 Katta537cd072015-02-13 14:33:43 +0800153 const char *s;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500154
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 Quadros718156a2016-04-12 15:51:48 +0300177 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 Siewior3aab70a2014-05-05 15:08:10 -0500186
Simon Glass00caae62017-08-03 12:22:12 -0600187 s = env_get("serial#");
Dileep Katta537cd072015-02-13 14:33:43 +0800188 if (s)
189 g_dnl_set_serialnumber((char *)s);
190
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500191 return 0;
192}
193
194static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
195{
196 memset(fastboot_func, 0, sizeof(*fastboot_func));
197}
198
199static 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
218static 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
237static 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 Quadros8b704a02016-04-13 11:30:00 +0300244 const struct usb_endpoint_descriptor *d;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500245
246 debug("%s: func: %s intf: %d alt: %d\n",
247 __func__, f->name, interface, alt);
248
Roger Quadros8b704a02016-04-13 11:30:00 +0300249 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
250 ret = usb_ep_enable(f_fb->out_ep, d);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500251 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 Quadros8b704a02016-04-13 11:30:00 +0300264 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
265 ret = usb_ep_enable(f_fb->in_ep, d);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500266 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;
284err:
285 fastboot_disable(f);
286 return ret;
287}
288
289static 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 Siewior3aab70a2014-05-05 15:08:10 -0500306 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}
320DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
321
Steve Rae593cbd92014-08-26 11:47:29 -0700322static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500323{
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 Kocialkowskibc9071c2015-07-04 16:46:16 +0200329
330 usb_ep_dequeue(fastboot_func->in_ep, in_req);
331
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500332 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
338static int fastboot_tx_write_str(const char *buffer)
339{
340 return fastboot_tx_write(buffer, strlen(buffer));
341}
342
343static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
344{
345 do_reset(NULL, 0, 0, NULL);
346}
347
Roger Quadrosac484c52016-04-19 10:16:59 +0300348static unsigned int rx_bytes_expected(struct usb_ep *ep)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500349{
Alex Kiernan65c96752018-05-29 15:30:55 +0000350 int rx_remain = fastboot_data_remaining();
Roger Quadrosac484c52016-04-19 10:16:59 +0300351 unsigned int rem;
352 unsigned int maxpacket = ep->maxpacket;
353
354 if (rx_remain <= 0)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500355 return 0;
Roger Quadrosac484c52016-04-19 10:16:59 +0300356 else if (rx_remain > EP_BUFFER_SIZE)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500357 return EP_BUFFER_SIZE;
Roger Quadrosac484c52016-04-19 10:16:59 +0300358
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 Katta9e4b5102015-02-17 02:02:36 +0530367 rx_remain = rx_remain + (maxpacket - rem);
Roger Quadrosac484c52016-04-19 10:16:59 +0300368
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500369 return rx_remain;
370}
371
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500372static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
373{
Alex Kiernan65c96752018-05-29 15:30:55 +0000374 char response[FASTBOOT_RESPONSE_LEN] = {0};
375 unsigned int transfer_size = fastboot_data_remaining();
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500376 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 Kiernan65c96752018-05-29 15:30:55 +0000387 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 Siewior3aab70a2014-05-05 15:08:10 -0500392
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500393 /*
Alex Kiernan65c96752018-05-29 15:30:55 +0000394 * Reset global transfer variable
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500395 */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500396 req->complete = rx_handler_command;
397 req->length = EP_BUFFER_SIZE;
398
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500399 fastboot_tx_write_str(response);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500400 } else {
Roger Quadrosac484c52016-04-19 10:16:59 +0300401 req->length = rx_bytes_expected(ep);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500402 }
403
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500404 req->actual = 0;
405 usb_ep_queue(ep, req, 0);
406}
407
Rob Herring267abc62014-12-10 14:43:04 -0600408static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
409{
410 g_dnl_trigger_detach();
411}
412
Alex Kiernan65c96752018-05-29 15:30:55 +0000413static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
Rob Herring267abc62014-12-10 14:43:04 -0600414{
Alex Kiernan65c96752018-05-29 15:30:55 +0000415 fastboot_boot();
416 do_exit_on_complete(ep, req);
Rob Herring267abc62014-12-10 14:43:04 -0600417}
418
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500419static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
420{
421 char *cmdbuf = req->buf;
Alex Kiernan65c96752018-05-29 15:30:55 +0000422 char response[FASTBOOT_RESPONSE_LEN] = {0};
423 int cmd = -1;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500424
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200425 if (req->status != 0 || req->length == 0)
426 return;
427
Alex Kiernan65c96752018-05-29 15:30:55 +0000428 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);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500434 }
435
Alex Kiernan65c96752018-05-29 15:30:55 +0000436 if (!strncmp("DATA", response, 4)) {
437 req->complete = rx_handler_dl_image;
438 req->length = rx_bytes_expected(ep);
439 }
440
441 fastboot_tx_write_str(response);
442
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;
448
449 case FASTBOOT_COMMAND_CONTINUE:
450 fastboot_func->in_req->complete = do_exit_on_complete;
451 break;
452
453 case FASTBOOT_COMMAND_REBOOT:
454 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
455 fastboot_func->in_req->complete = compl_do_reset;
456 break;
Eric Nelsone2140582014-10-01 14:30:56 -0700457 }
Steve Rae593cbd92014-08-26 11:47:29 -0700458 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500459
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200460 *cmdbuf = '\0';
461 req->actual = 0;
462 usb_ep_queue(ep, req, 0);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500463}