blob: d1d087e12b2b1b32e22916e27451c9ef11357247 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior9d4471e2014-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 Siewior9d4471e2014-05-05 15:08:10 -050011 */
Simon Glassed38aef2020-05-10 11:40:03 -060012#include <command.h>
Steve Rae56ad7922014-08-26 11:47:29 -070013#include <config.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050014#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060015#include <env.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050016#include <errno.h>
Maxime Ripard00e8eb72015-10-15 14:34:13 +020017#include <fastboot.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050019#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 Siewior9d4471e2014-05-05 15:08:10 -050024#include <g_dnl.h>
25
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050026#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 Siewior9d4471e2014-05-05 15:08:10 -050034#define EP_BUFFER_SIZE 4096
Roger Quadrosaefb0212016-04-19 10:16:59 +030035/*
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 Siewior9d4471e2014-05-05 15:08:10 -050040
41struct f_fastboot {
42 struct usb_function usb_function;
43
Steve Rae56ad7922014-08-26 11:47:29 -070044 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050045 struct usb_ep *in_ep, *out_ep;
46 struct usb_request *in_req, *out_req;
47};
48
49static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
50{
51 return container_of(f, struct f_fastboot, usb_function);
52}
53
54static struct f_fastboot *fastboot_func;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050055
56static 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 Quadrosbf4b41a2016-04-12 15:51:48 +030061 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050062};
63
64static 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 Quadrosbf4b41a2016-04-12 15:51:48 +030069 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050070};
71
Roger Quadrosbf4b41a2016-04-12 15:51:48 +030072static 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 Siewior9d4471e2014-05-05 15:08:10 -050080static 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 Quadrosbf4b41a2016-04-12 15:51:48 +030085 .wMaxPacketSize = cpu_to_le16(512),
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050086};
87
88static 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 Quadrosbf4b41a2016-04-12 15:51:48 +030099static struct usb_descriptor_header *fb_fs_function[] = {
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500100 (struct usb_descriptor_header *)&interface_desc,
101 (struct usb_descriptor_header *)&fs_ep_in,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +0300102 (struct usb_descriptor_header *)&fs_ep_out,
103};
104
105static struct usb_descriptor_header *fb_hs_function[] = {
106 (struct usb_descriptor_header *)&interface_desc,
107 (struct usb_descriptor_header *)&hs_ep_in,
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500108 (struct usb_descriptor_header *)&hs_ep_out,
109 NULL,
110};
111
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300112static struct usb_endpoint_descriptor *
113fb_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 Siewior9d4471e2014-05-05 15:08:10 -0500121/*
122 * static strings, in UTF-8
123 */
124static const char fastboot_name[] = "Android Fastboot";
125
126static struct usb_string fastboot_string_defs[] = {
127 [0].s = fastboot_name,
128 { } /* end of list */
129};
130
131static struct usb_gadget_strings stringtab_fastboot = {
132 .language = 0x0409, /* en-us */
133 .strings = fastboot_string_defs,
134};
135
136static struct usb_gadget_strings *fastboot_strings[] = {
137 &stringtab_fastboot,
138 NULL,
139};
140
141static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
142
143static 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
151static 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 Katta447c9752015-02-13 14:33:43 +0800156 const char *s;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500157
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 Quadrosbf4b41a2016-04-12 15:51:48 +0300180 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 Siewior9d4471e2014-05-05 15:08:10 -0500189
Simon Glass64b723f2017-08-03 12:22:12 -0600190 s = env_get("serial#");
Dileep Katta447c9752015-02-13 14:33:43 +0800191 if (s)
192 g_dnl_set_serialnumber((char *)s);
193
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500194 return 0;
195}
196
197static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
198{
199 memset(fastboot_func, 0, sizeof(*fastboot_func));
200}
201
202static 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
221static 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
240static 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 Quadros9ab3fdf2016-04-13 11:30:00 +0300247 const struct usb_endpoint_descriptor *d;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500248
249 debug("%s: func: %s intf: %d alt: %d\n",
250 __func__, f->name, interface, alt);
251
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300252 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
253 ret = usb_ep_enable(f_fb->out_ep, d);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500254 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 Quadros9ab3fdf2016-04-13 11:30:00 +0300267 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
268 ret = usb_ep_enable(f_fb->in_ep, d);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500269 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;
287err:
288 fastboot_disable(f);
289 return ret;
290}
291
292static 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 Siewior9d4471e2014-05-05 15:08:10 -0500309 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}
323DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
324
Steve Rae56ad7922014-08-26 11:47:29 -0700325static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500326{
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 Kocialkowski2032bfd2015-07-04 16:46:16 +0200332
333 usb_ep_dequeue(fastboot_func->in_ep, in_req);
334
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500335 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
341static int fastboot_tx_write_str(const char *buffer)
342{
343 return fastboot_tx_write(buffer, strlen(buffer));
344}
345
346static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
347{
348 do_reset(NULL, 0, 0, NULL);
349}
350
Roger Quadrosaefb0212016-04-19 10:16:59 +0300351static unsigned int rx_bytes_expected(struct usb_ep *ep)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500352{
Alex Kiernane80d2942018-05-29 15:30:55 +0000353 int rx_remain = fastboot_data_remaining();
Roger Quadrosaefb0212016-04-19 10:16:59 +0300354 unsigned int rem;
355 unsigned int maxpacket = ep->maxpacket;
356
357 if (rx_remain <= 0)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500358 return 0;
Roger Quadrosaefb0212016-04-19 10:16:59 +0300359 else if (rx_remain > EP_BUFFER_SIZE)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500360 return EP_BUFFER_SIZE;
Roger Quadrosaefb0212016-04-19 10:16:59 +0300361
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 Katta50b4c172015-02-17 02:02:36 +0530370 rx_remain = rx_remain + (maxpacket - rem);
Roger Quadrosaefb0212016-04-19 10:16:59 +0300371
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500372 return rx_remain;
373}
374
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500375static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
376{
Alex Kiernane80d2942018-05-29 15:30:55 +0000377 char response[FASTBOOT_RESPONSE_LEN] = {0};
378 unsigned int transfer_size = fastboot_data_remaining();
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500379 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 Kiernane80d2942018-05-29 15:30:55 +0000390 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 Siewior9d4471e2014-05-05 15:08:10 -0500395
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500396 /*
Alex Kiernane80d2942018-05-29 15:30:55 +0000397 * Reset global transfer variable
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500398 */
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500399 req->complete = rx_handler_command;
400 req->length = EP_BUFFER_SIZE;
401
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500402 fastboot_tx_write_str(response);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500403 } else {
Roger Quadrosaefb0212016-04-19 10:16:59 +0300404 req->length = rx_bytes_expected(ep);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500405 }
406
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500407 req->actual = 0;
408 usb_ep_queue(ep, req, 0);
409}
410
Rob Herring5aba3982014-12-10 14:43:04 -0600411static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
412{
413 g_dnl_trigger_detach();
414}
415
Alex Kiernane80d2942018-05-29 15:30:55 +0000416static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
Rob Herring5aba3982014-12-10 14:43:04 -0600417{
Alex Kiernane80d2942018-05-29 15:30:55 +0000418 fastboot_boot();
419 do_exit_on_complete(ep, req);
Rob Herring5aba3982014-12-10 14:43:04 -0600420}
421
Alex Kiernane80d2942018-05-29 15:30:55 +0000422static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
Steve Raebfb9ba42014-08-26 11:47:28 -0700423{
Alex Kiernane80d2942018-05-29 15:30:55 +0000424 char *cmdbuf = req->buf;
425 char response[FASTBOOT_RESPONSE_LEN] = {0};
426 int cmd = -1;
Steve Raebfb9ba42014-08-26 11:47:28 -0700427
Alex Kiernane80d2942018-05-29 15:30:55 +0000428 if (req->status != 0 || req->length == 0)
Steve Raebfb9ba42014-08-26 11:47:28 -0700429 return;
Steve Raebfb9ba42014-08-26 11:47:28 -0700430
Alex Kiernane80d2942018-05-29 15:30:55 +0000431 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 Scott7e23abf2015-01-26 15:49:00 -0600437 }
Michael Scott7e23abf2015-01-26 15:49:00 -0600438
Alex Kiernane80d2942018-05-29 15:30:55 +0000439 if (!strncmp("DATA", response, 4)) {
440 req->complete = rx_handler_dl_image;
441 req->length = rx_bytes_expected(ep);
Dileep Kattab1228202015-02-17 18:48:23 +0530442 }
443
Alex Kiernane80d2942018-05-29 15:30:55 +0000444 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;
Paul Kocialkowski4a126b62015-07-04 16:46:15 +0200449
Alex Kiernane80d2942018-05-29 15:30:55 +0000450 case FASTBOOT_COMMAND_CONTINUE:
451 fastboot_func->in_req->complete = do_exit_on_complete;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500452 break;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500453
Alex Kiernane80d2942018-05-29 15:30:55 +0000454 case FASTBOOT_COMMAND_REBOOT:
455 case FASTBOOT_COMMAND_REBOOT_BOOTLOADER:
Roman Kovalivskyib30b97b2020-07-28 23:35:33 +0300456 case FASTBOOT_COMMAND_REBOOT_FASTBOOTD:
457 case FASTBOOT_COMMAND_REBOOT_RECOVERY:
Alex Kiernane80d2942018-05-29 15:30:55 +0000458 fastboot_func->in_req->complete = compl_do_reset;
459 break;
Eric Nelson3d626cd2014-10-01 14:30:56 -0700460 }
Steve Rae56ad7922014-08-26 11:47:29 -0700461 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500462
yurii.pidhornyi5953ffa2020-08-20 18:41:18 +0300463 fastboot_tx_write_str(response);
464
Paul Kocialkowski4a126b62015-07-04 16:46:15 +0200465 *cmdbuf = '\0';
466 req->actual = 0;
467 usb_ep_queue(ep, req, 0);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500468}