blob: d1be3f5e1370b8d98800053c885b159f8facf895 [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 */
Simon Glass09140112020-05-10 11:40:03 -060012#include <command.h>
Steve Rae593cbd92014-08-26 11:47:29 -070013#include <config.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050014#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -060015#include <env.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050016#include <errno.h>
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020017#include <fastboot.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050018#include <malloc.h>
19#include <linux/usb/ch9.h>
20#include <linux/usb/gadget.h>
21#include <linux/usb/composite.h>
22#include <linux/compiler.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050023#include <g_dnl.h>
24
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050025#define FASTBOOT_INTERFACE_CLASS 0xff
26#define FASTBOOT_INTERFACE_SUB_CLASS 0x42
27#define FASTBOOT_INTERFACE_PROTOCOL 0x03
28
29#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
30#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
31#define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
32
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050033#define EP_BUFFER_SIZE 4096
Roger Quadrosac484c52016-04-19 10:16:59 +030034/*
35 * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
36 * (64 or 512 or 1024), else we break on certain controllers like DWC3
37 * that expect bulk OUT requests to be divisible by maxpacket size.
38 */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050039
40struct f_fastboot {
41 struct usb_function usb_function;
42
Steve Rae593cbd92014-08-26 11:47:29 -070043 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050044 struct usb_ep *in_ep, *out_ep;
45 struct usb_request *in_req, *out_req;
46};
47
48static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
49{
50 return container_of(f, struct f_fastboot, usb_function);
51}
52
53static struct f_fastboot *fastboot_func;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050054
55static struct usb_endpoint_descriptor fs_ep_in = {
56 .bLength = USB_DT_ENDPOINT_SIZE,
57 .bDescriptorType = USB_DT_ENDPOINT,
58 .bEndpointAddress = USB_DIR_IN,
59 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadros718156a2016-04-12 15:51:48 +030060 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050061};
62
63static struct usb_endpoint_descriptor fs_ep_out = {
64 .bLength = USB_DT_ENDPOINT_SIZE,
65 .bDescriptorType = USB_DT_ENDPOINT,
66 .bEndpointAddress = USB_DIR_OUT,
67 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadros718156a2016-04-12 15:51:48 +030068 .wMaxPacketSize = cpu_to_le16(64),
69};
70
71static struct usb_endpoint_descriptor hs_ep_in = {
72 .bLength = USB_DT_ENDPOINT_SIZE,
73 .bDescriptorType = USB_DT_ENDPOINT,
74 .bEndpointAddress = USB_DIR_IN,
75 .bmAttributes = USB_ENDPOINT_XFER_BULK,
76 .wMaxPacketSize = cpu_to_le16(512),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050077};
78
79static struct usb_endpoint_descriptor hs_ep_out = {
80 .bLength = USB_DT_ENDPOINT_SIZE,
81 .bDescriptorType = USB_DT_ENDPOINT,
82 .bEndpointAddress = USB_DIR_OUT,
83 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadros718156a2016-04-12 15:51:48 +030084 .wMaxPacketSize = cpu_to_le16(512),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050085};
86
87static struct usb_interface_descriptor interface_desc = {
88 .bLength = USB_DT_INTERFACE_SIZE,
89 .bDescriptorType = USB_DT_INTERFACE,
90 .bInterfaceNumber = 0x00,
91 .bAlternateSetting = 0x00,
92 .bNumEndpoints = 0x02,
93 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
94 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
95 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
96};
97
Roger Quadros718156a2016-04-12 15:51:48 +030098static struct usb_descriptor_header *fb_fs_function[] = {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050099 (struct usb_descriptor_header *)&interface_desc,
100 (struct usb_descriptor_header *)&fs_ep_in,
Roger Quadros718156a2016-04-12 15:51:48 +0300101 (struct usb_descriptor_header *)&fs_ep_out,
102};
103
104static struct usb_descriptor_header *fb_hs_function[] = {
105 (struct usb_descriptor_header *)&interface_desc,
106 (struct usb_descriptor_header *)&hs_ep_in,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500107 (struct usb_descriptor_header *)&hs_ep_out,
108 NULL,
109};
110
Roger Quadros8b704a02016-04-13 11:30:00 +0300111static struct usb_endpoint_descriptor *
112fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
113 struct usb_endpoint_descriptor *hs)
114{
115 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
116 return hs;
117 return fs;
118}
119
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500120/*
121 * static strings, in UTF-8
122 */
123static const char fastboot_name[] = "Android Fastboot";
124
125static struct usb_string fastboot_string_defs[] = {
126 [0].s = fastboot_name,
127 { } /* end of list */
128};
129
130static struct usb_gadget_strings stringtab_fastboot = {
131 .language = 0x0409, /* en-us */
132 .strings = fastboot_string_defs,
133};
134
135static struct usb_gadget_strings *fastboot_strings[] = {
136 &stringtab_fastboot,
137 NULL,
138};
139
140static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
141
142static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
143{
144 int status = req->status;
145 if (!status)
146 return;
147 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
148}
149
150static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
151{
152 int id;
153 struct usb_gadget *gadget = c->cdev->gadget;
154 struct f_fastboot *f_fb = func_to_fastboot(f);
Dileep Katta537cd072015-02-13 14:33:43 +0800155 const char *s;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500156
157 /* DYNAMIC interface numbers assignments */
158 id = usb_interface_id(c, f);
159 if (id < 0)
160 return id;
161 interface_desc.bInterfaceNumber = id;
162
163 id = usb_string_id(c->cdev);
164 if (id < 0)
165 return id;
166 fastboot_string_defs[0].id = id;
167 interface_desc.iInterface = id;
168
169 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
170 if (!f_fb->in_ep)
171 return -ENODEV;
172 f_fb->in_ep->driver_data = c->cdev;
173
174 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
175 if (!f_fb->out_ep)
176 return -ENODEV;
177 f_fb->out_ep->driver_data = c->cdev;
178
Roger Quadros718156a2016-04-12 15:51:48 +0300179 f->descriptors = fb_fs_function;
180
181 if (gadget_is_dualspeed(gadget)) {
182 /* Assume endpoint addresses are the same for both speeds */
183 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
184 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
185 /* copy HS descriptors */
186 f->hs_descriptors = fb_hs_function;
187 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500188
Simon Glass00caae62017-08-03 12:22:12 -0600189 s = env_get("serial#");
Dileep Katta537cd072015-02-13 14:33:43 +0800190 if (s)
191 g_dnl_set_serialnumber((char *)s);
192
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500193 return 0;
194}
195
196static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
197{
198 memset(fastboot_func, 0, sizeof(*fastboot_func));
199}
200
201static void fastboot_disable(struct usb_function *f)
202{
203 struct f_fastboot *f_fb = func_to_fastboot(f);
204
205 usb_ep_disable(f_fb->out_ep);
206 usb_ep_disable(f_fb->in_ep);
207
208 if (f_fb->out_req) {
209 free(f_fb->out_req->buf);
210 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
211 f_fb->out_req = NULL;
212 }
213 if (f_fb->in_req) {
214 free(f_fb->in_req->buf);
215 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
216 f_fb->in_req = NULL;
217 }
218}
219
220static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
221{
222 struct usb_request *req;
223
224 req = usb_ep_alloc_request(ep, 0);
225 if (!req)
226 return NULL;
227
228 req->length = EP_BUFFER_SIZE;
229 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
230 if (!req->buf) {
231 usb_ep_free_request(ep, req);
232 return NULL;
233 }
234
235 memset(req->buf, 0, req->length);
236 return req;
237}
238
239static int fastboot_set_alt(struct usb_function *f,
240 unsigned interface, unsigned alt)
241{
242 int ret;
243 struct usb_composite_dev *cdev = f->config->cdev;
244 struct usb_gadget *gadget = cdev->gadget;
245 struct f_fastboot *f_fb = func_to_fastboot(f);
Roger Quadros8b704a02016-04-13 11:30:00 +0300246 const struct usb_endpoint_descriptor *d;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500247
248 debug("%s: func: %s intf: %d alt: %d\n",
249 __func__, f->name, interface, alt);
250
Roger Quadros8b704a02016-04-13 11:30:00 +0300251 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
252 ret = usb_ep_enable(f_fb->out_ep, d);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500253 if (ret) {
254 puts("failed to enable out ep\n");
255 return ret;
256 }
257
258 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
259 if (!f_fb->out_req) {
260 puts("failed to alloc out req\n");
261 ret = -EINVAL;
262 goto err;
263 }
264 f_fb->out_req->complete = rx_handler_command;
265
Roger Quadros8b704a02016-04-13 11:30:00 +0300266 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
267 ret = usb_ep_enable(f_fb->in_ep, d);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500268 if (ret) {
269 puts("failed to enable in ep\n");
270 goto err;
271 }
272
273 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
274 if (!f_fb->in_req) {
275 puts("failed alloc req in\n");
276 ret = -EINVAL;
277 goto err;
278 }
279 f_fb->in_req->complete = fastboot_complete;
280
281 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
282 if (ret)
283 goto err;
284
285 return 0;
286err:
287 fastboot_disable(f);
288 return ret;
289}
290
291static int fastboot_add(struct usb_configuration *c)
292{
293 struct f_fastboot *f_fb = fastboot_func;
294 int status;
295
296 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
297
298 if (!f_fb) {
299 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
300 if (!f_fb)
301 return -ENOMEM;
302
303 fastboot_func = f_fb;
304 memset(f_fb, 0, sizeof(*f_fb));
305 }
306
307 f_fb->usb_function.name = "f_fastboot";
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500308 f_fb->usb_function.bind = fastboot_bind;
309 f_fb->usb_function.unbind = fastboot_unbind;
310 f_fb->usb_function.set_alt = fastboot_set_alt;
311 f_fb->usb_function.disable = fastboot_disable;
312 f_fb->usb_function.strings = fastboot_strings;
313
314 status = usb_add_function(c, &f_fb->usb_function);
315 if (status) {
316 free(f_fb);
317 fastboot_func = f_fb;
318 }
319
320 return status;
321}
322DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
323
Steve Rae593cbd92014-08-26 11:47:29 -0700324static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500325{
326 struct usb_request *in_req = fastboot_func->in_req;
327 int ret;
328
329 memcpy(in_req->buf, buffer, buffer_size);
330 in_req->length = buffer_size;
Paul Kocialkowskibc9071c2015-07-04 16:46:16 +0200331
332 usb_ep_dequeue(fastboot_func->in_ep, in_req);
333
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500334 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
335 if (ret)
336 printf("Error %d on queue\n", ret);
337 return 0;
338}
339
340static int fastboot_tx_write_str(const char *buffer)
341{
342 return fastboot_tx_write(buffer, strlen(buffer));
343}
344
345static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
346{
347 do_reset(NULL, 0, 0, NULL);
348}
349
Roger Quadrosac484c52016-04-19 10:16:59 +0300350static unsigned int rx_bytes_expected(struct usb_ep *ep)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500351{
Alex Kiernan65c96752018-05-29 15:30:55 +0000352 int rx_remain = fastboot_data_remaining();
Roger Quadrosac484c52016-04-19 10:16:59 +0300353 unsigned int rem;
354 unsigned int maxpacket = ep->maxpacket;
355
356 if (rx_remain <= 0)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500357 return 0;
Roger Quadrosac484c52016-04-19 10:16:59 +0300358 else if (rx_remain > EP_BUFFER_SIZE)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500359 return EP_BUFFER_SIZE;
Roger Quadrosac484c52016-04-19 10:16:59 +0300360
361 /*
362 * Some controllers e.g. DWC3 don't like OUT transfers to be
363 * not ending in maxpacket boundary. So just make them happy by
364 * always requesting for integral multiple of maxpackets.
365 * This shouldn't bother controllers that don't care about it.
366 */
367 rem = rx_remain % maxpacket;
368 if (rem > 0)
Dileep Katta9e4b5102015-02-17 02:02:36 +0530369 rx_remain = rx_remain + (maxpacket - rem);
Roger Quadrosac484c52016-04-19 10:16:59 +0300370
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500371 return rx_remain;
372}
373
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500374static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
375{
Alex Kiernan65c96752018-05-29 15:30:55 +0000376 char response[FASTBOOT_RESPONSE_LEN] = {0};
377 unsigned int transfer_size = fastboot_data_remaining();
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500378 const unsigned char *buffer = req->buf;
379 unsigned int buffer_size = req->actual;
380
381 if (req->status != 0) {
382 printf("Bad status: %d\n", req->status);
383 return;
384 }
385
386 if (buffer_size < transfer_size)
387 transfer_size = buffer_size;
388
Alex Kiernan65c96752018-05-29 15:30:55 +0000389 fastboot_data_download(buffer, transfer_size, response);
390 if (response[0]) {
391 fastboot_tx_write_str(response);
392 } else if (!fastboot_data_remaining()) {
393 fastboot_data_complete(response);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500394
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500395 /*
Alex Kiernan65c96752018-05-29 15:30:55 +0000396 * Reset global transfer variable
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500397 */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500398 req->complete = rx_handler_command;
399 req->length = EP_BUFFER_SIZE;
400
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500401 fastboot_tx_write_str(response);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500402 } else {
Roger Quadrosac484c52016-04-19 10:16:59 +0300403 req->length = rx_bytes_expected(ep);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500404 }
405
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500406 req->actual = 0;
407 usb_ep_queue(ep, req, 0);
408}
409
Rob Herring267abc62014-12-10 14:43:04 -0600410static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
411{
412 g_dnl_trigger_detach();
413}
414
Alex Kiernan65c96752018-05-29 15:30:55 +0000415static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
Rob Herring267abc62014-12-10 14:43:04 -0600416{
Alex Kiernan65c96752018-05-29 15:30:55 +0000417 fastboot_boot();
418 do_exit_on_complete(ep, req);
Rob Herring267abc62014-12-10 14:43:04 -0600419}
420
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500421static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
422{
423 char *cmdbuf = req->buf;
Alex Kiernan65c96752018-05-29 15:30:55 +0000424 char response[FASTBOOT_RESPONSE_LEN] = {0};
425 int cmd = -1;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500426
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200427 if (req->status != 0 || req->length == 0)
428 return;
429
Alex Kiernan65c96752018-05-29 15:30:55 +0000430 if (req->actual < req->length) {
431 cmdbuf[req->actual] = '\0';
432 cmd = fastboot_handle_command(cmdbuf, response);
433 } else {
434 pr_err("buffer overflow");
435 fastboot_fail("buffer overflow", response);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500436 }
437
Alex Kiernan65c96752018-05-29 15:30:55 +0000438 if (!strncmp("DATA", response, 4)) {
439 req->complete = rx_handler_dl_image;
440 req->length = rx_bytes_expected(ep);
441 }
442
443 fastboot_tx_write_str(response);
444
445 if (!strncmp("OKAY", response, 4)) {
446 switch (cmd) {
447 case FASTBOOT_COMMAND_BOOT:
448 fastboot_func->in_req->complete = do_bootm_on_complete;
449 break;
450
451 case FASTBOOT_COMMAND_CONTINUE:
452 fastboot_func->in_req->complete = do_exit_on_complete;
453 break;
454
455 case FASTBOOT_COMMAND_REBOOT:
456 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
457 fastboot_func->in_req->complete = compl_do_reset;
458 break;
Eric Nelsone2140582014-10-01 14:30:56 -0700459 }
Steve Rae593cbd92014-08-26 11:47:29 -0700460 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500461
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200462 *cmdbuf = '\0';
463 req->actual = 0;
464 usb_ep_queue(ep, req, 0);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500465}