blob: e2659faf627c463ad3bc77001ed7eb12135b70a8 [file] [log] [blame]
Sebastian Siewior3aab70a2014-05-05 15:08:10 -05001/*
2 * (C) Copyright 2008 - 2009
3 * Windriver, <www.windriver.com>
4 * Tom Rix <Tom.Rix@windriver.com>
5 *
6 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7 *
8 * Copyright 2014 Linaro, Ltd.
9 * Rob Herring <robh@kernel.org>
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 */
13#include <common.h>
14#include <errno.h>
15#include <malloc.h>
16#include <linux/usb/ch9.h>
17#include <linux/usb/gadget.h>
18#include <linux/usb/composite.h>
19#include <linux/compiler.h>
20#include <version.h>
21#include <g_dnl.h>
Steve Raed1b5ed02014-08-26 11:47:28 -070022#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
23#include <fb_mmc.h>
24#endif
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050025
26#define FASTBOOT_VERSION "0.4"
27
28#define FASTBOOT_INTERFACE_CLASS 0xff
29#define FASTBOOT_INTERFACE_SUB_CLASS 0x42
30#define FASTBOOT_INTERFACE_PROTOCOL 0x03
31
32#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
33#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
34#define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
35
36/* The 64 defined bytes plus \0 */
37#define RESPONSE_LEN (64 + 1)
38
39#define EP_BUFFER_SIZE 4096
40
41struct f_fastboot {
42 struct usb_function usb_function;
43
44 /* IN/OUT EP's and correspoinding requests */
45 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;
55static unsigned int download_size;
56static unsigned int download_bytes;
57
58static struct usb_endpoint_descriptor fs_ep_in = {
59 .bLength = USB_DT_ENDPOINT_SIZE,
60 .bDescriptorType = USB_DT_ENDPOINT,
61 .bEndpointAddress = USB_DIR_IN,
62 .bmAttributes = USB_ENDPOINT_XFER_BULK,
63 .wMaxPacketSize = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
64 .bInterval = 0x00,
65};
66
67static struct usb_endpoint_descriptor fs_ep_out = {
68 .bLength = USB_DT_ENDPOINT_SIZE,
69 .bDescriptorType = USB_DT_ENDPOINT,
70 .bEndpointAddress = USB_DIR_OUT,
71 .bmAttributes = USB_ENDPOINT_XFER_BULK,
72 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
73 .bInterval = 0x00,
74};
75
76static struct usb_endpoint_descriptor hs_ep_out = {
77 .bLength = USB_DT_ENDPOINT_SIZE,
78 .bDescriptorType = USB_DT_ENDPOINT,
79 .bEndpointAddress = USB_DIR_OUT,
80 .bmAttributes = USB_ENDPOINT_XFER_BULK,
81 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
82 .bInterval = 0x00,
83};
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
96static struct usb_descriptor_header *fb_runtime_descs[] = {
97 (struct usb_descriptor_header *)&interface_desc,
98 (struct usb_descriptor_header *)&fs_ep_in,
99 (struct usb_descriptor_header *)&hs_ep_out,
100 NULL,
101};
102
103/*
104 * static strings, in UTF-8
105 */
106static const char fastboot_name[] = "Android Fastboot";
107
108static struct usb_string fastboot_string_defs[] = {
109 [0].s = fastboot_name,
110 { } /* end of list */
111};
112
113static struct usb_gadget_strings stringtab_fastboot = {
114 .language = 0x0409, /* en-us */
115 .strings = fastboot_string_defs,
116};
117
118static struct usb_gadget_strings *fastboot_strings[] = {
119 &stringtab_fastboot,
120 NULL,
121};
122
123static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
124
125static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
126{
127 int status = req->status;
128 if (!status)
129 return;
130 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
131}
132
133static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
134{
135 int id;
136 struct usb_gadget *gadget = c->cdev->gadget;
137 struct f_fastboot *f_fb = func_to_fastboot(f);
138
139 /* DYNAMIC interface numbers assignments */
140 id = usb_interface_id(c, f);
141 if (id < 0)
142 return id;
143 interface_desc.bInterfaceNumber = id;
144
145 id = usb_string_id(c->cdev);
146 if (id < 0)
147 return id;
148 fastboot_string_defs[0].id = id;
149 interface_desc.iInterface = id;
150
151 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
152 if (!f_fb->in_ep)
153 return -ENODEV;
154 f_fb->in_ep->driver_data = c->cdev;
155
156 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
157 if (!f_fb->out_ep)
158 return -ENODEV;
159 f_fb->out_ep->driver_data = c->cdev;
160
161 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
162
163 return 0;
164}
165
166static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
167{
168 memset(fastboot_func, 0, sizeof(*fastboot_func));
169}
170
171static void fastboot_disable(struct usb_function *f)
172{
173 struct f_fastboot *f_fb = func_to_fastboot(f);
174
175 usb_ep_disable(f_fb->out_ep);
176 usb_ep_disable(f_fb->in_ep);
177
178 if (f_fb->out_req) {
179 free(f_fb->out_req->buf);
180 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
181 f_fb->out_req = NULL;
182 }
183 if (f_fb->in_req) {
184 free(f_fb->in_req->buf);
185 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
186 f_fb->in_req = NULL;
187 }
188}
189
190static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
191{
192 struct usb_request *req;
193
194 req = usb_ep_alloc_request(ep, 0);
195 if (!req)
196 return NULL;
197
198 req->length = EP_BUFFER_SIZE;
199 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
200 if (!req->buf) {
201 usb_ep_free_request(ep, req);
202 return NULL;
203 }
204
205 memset(req->buf, 0, req->length);
206 return req;
207}
208
209static int fastboot_set_alt(struct usb_function *f,
210 unsigned interface, unsigned alt)
211{
212 int ret;
213 struct usb_composite_dev *cdev = f->config->cdev;
214 struct usb_gadget *gadget = cdev->gadget;
215 struct f_fastboot *f_fb = func_to_fastboot(f);
216
217 debug("%s: func: %s intf: %d alt: %d\n",
218 __func__, f->name, interface, alt);
219
220 /* make sure we don't enable the ep twice */
221 if (gadget->speed == USB_SPEED_HIGH)
222 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
223 else
224 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
225 if (ret) {
226 puts("failed to enable out ep\n");
227 return ret;
228 }
229
230 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
231 if (!f_fb->out_req) {
232 puts("failed to alloc out req\n");
233 ret = -EINVAL;
234 goto err;
235 }
236 f_fb->out_req->complete = rx_handler_command;
237
238 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
239 if (ret) {
240 puts("failed to enable in ep\n");
241 goto err;
242 }
243
244 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
245 if (!f_fb->in_req) {
246 puts("failed alloc req in\n");
247 ret = -EINVAL;
248 goto err;
249 }
250 f_fb->in_req->complete = fastboot_complete;
251
252 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
253 if (ret)
254 goto err;
255
256 return 0;
257err:
258 fastboot_disable(f);
259 return ret;
260}
261
262static int fastboot_add(struct usb_configuration *c)
263{
264 struct f_fastboot *f_fb = fastboot_func;
265 int status;
266
267 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
268
269 if (!f_fb) {
270 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
271 if (!f_fb)
272 return -ENOMEM;
273
274 fastboot_func = f_fb;
275 memset(f_fb, 0, sizeof(*f_fb));
276 }
277
278 f_fb->usb_function.name = "f_fastboot";
279 f_fb->usb_function.hs_descriptors = fb_runtime_descs;
280 f_fb->usb_function.bind = fastboot_bind;
281 f_fb->usb_function.unbind = fastboot_unbind;
282 f_fb->usb_function.set_alt = fastboot_set_alt;
283 f_fb->usb_function.disable = fastboot_disable;
284 f_fb->usb_function.strings = fastboot_strings;
285
286 status = usb_add_function(c, &f_fb->usb_function);
287 if (status) {
288 free(f_fb);
289 fastboot_func = f_fb;
290 }
291
292 return status;
293}
294DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
295
296int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
297{
298 struct usb_request *in_req = fastboot_func->in_req;
299 int ret;
300
301 memcpy(in_req->buf, buffer, buffer_size);
302 in_req->length = buffer_size;
303 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
304 if (ret)
305 printf("Error %d on queue\n", ret);
306 return 0;
307}
308
309static int fastboot_tx_write_str(const char *buffer)
310{
311 return fastboot_tx_write(buffer, strlen(buffer));
312}
313
314static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
315{
316 do_reset(NULL, 0, 0, NULL);
317}
318
319static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
320{
321 fastboot_func->in_req->complete = compl_do_reset;
322 fastboot_tx_write_str("OKAY");
323}
324
325static int strcmp_l1(const char *s1, const char *s2)
326{
327 if (!s1 || !s2)
328 return -1;
329 return strncmp(s1, s2, strlen(s1));
330}
331
332static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
333{
334 char *cmd = req->buf;
335 char response[RESPONSE_LEN];
336 const char *s;
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200337 size_t chars_left;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500338
339 strcpy(response, "OKAY");
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200340 chars_left = sizeof(response) - strlen(response) - 1;
341
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500342 strsep(&cmd, ":");
343 if (!cmd) {
344 fastboot_tx_write_str("FAILmissing var");
345 return;
346 }
347
348 if (!strcmp_l1("version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200349 strncat(response, FASTBOOT_VERSION, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500350 } else if (!strcmp_l1("bootloader-version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200351 strncat(response, U_BOOT_VERSION, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500352 } else if (!strcmp_l1("downloadsize", cmd)) {
353 char str_num[12];
354
355 sprintf(str_num, "%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200356 strncat(response, str_num, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500357 } else if (!strcmp_l1("serialno", cmd)) {
358 s = getenv("serial#");
359 if (s)
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200360 strncat(response, s, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500361 else
362 strcpy(response, "FAILValue not set");
363 } else {
364 strcpy(response, "FAILVariable not implemented");
365 }
366 fastboot_tx_write_str(response);
367}
368
369static unsigned int rx_bytes_expected(void)
370{
371 int rx_remain = download_size - download_bytes;
372 if (rx_remain < 0)
373 return 0;
374 if (rx_remain > EP_BUFFER_SIZE)
375 return EP_BUFFER_SIZE;
376 return rx_remain;
377}
378
379#define BYTES_PER_DOT 0x20000
380static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
381{
382 char response[RESPONSE_LEN];
383 unsigned int transfer_size = download_size - download_bytes;
384 const unsigned char *buffer = req->buf;
385 unsigned int buffer_size = req->actual;
386
387 if (req->status != 0) {
388 printf("Bad status: %d\n", req->status);
389 return;
390 }
391
392 if (buffer_size < transfer_size)
393 transfer_size = buffer_size;
394
395 memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
396 buffer, transfer_size);
397
398 download_bytes += transfer_size;
399
400 /* Check if transfer is done */
401 if (download_bytes >= download_size) {
402 /*
403 * Reset global transfer variable, keep download_bytes because
404 * it will be used in the next possible flashing command
405 */
406 download_size = 0;
407 req->complete = rx_handler_command;
408 req->length = EP_BUFFER_SIZE;
409
410 sprintf(response, "OKAY");
411 fastboot_tx_write_str(response);
412
413 printf("\ndownloading of %d bytes finished\n", download_bytes);
414 } else {
415 req->length = rx_bytes_expected();
416 if (req->length < ep->maxpacket)
417 req->length = ep->maxpacket;
418 }
419
420 if (download_bytes && !(download_bytes % BYTES_PER_DOT)) {
421 putc('.');
422 if (!(download_bytes % (74 * BYTES_PER_DOT)))
423 putc('\n');
424 }
425 req->actual = 0;
426 usb_ep_queue(ep, req, 0);
427}
428
429static void cb_download(struct usb_ep *ep, struct usb_request *req)
430{
431 char *cmd = req->buf;
432 char response[RESPONSE_LEN];
433
434 strsep(&cmd, ":");
435 download_size = simple_strtoul(cmd, NULL, 16);
436 download_bytes = 0;
437
438 printf("Starting download of %d bytes\n", download_size);
439
440 if (0 == download_size) {
441 sprintf(response, "FAILdata invalid size");
442 } else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
443 download_size = 0;
444 sprintf(response, "FAILdata too large");
445 } else {
446 sprintf(response, "DATA%08x", download_size);
447 req->complete = rx_handler_dl_image;
448 req->length = rx_bytes_expected();
449 if (req->length < ep->maxpacket)
450 req->length = ep->maxpacket;
451 }
452 fastboot_tx_write_str(response);
453}
454
455static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
456{
457 char boot_addr_start[12];
458 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
459
460 puts("Booting kernel..\n");
461
462 sprintf(boot_addr_start, "0x%lx", load_addr);
463 do_bootm(NULL, 0, 2, bootm_args);
464
465 /* This only happens if image is somehow faulty so we start over */
466 do_reset(NULL, 0, 0, NULL);
467}
468
469static void cb_boot(struct usb_ep *ep, struct usb_request *req)
470{
471 fastboot_func->in_req->complete = do_bootm_on_complete;
472 fastboot_tx_write_str("OKAY");
473}
474
Steve Raed1b5ed02014-08-26 11:47:28 -0700475#ifdef CONFIG_FASTBOOT_FLASH
476static void cb_flash(struct usb_ep *ep, struct usb_request *req)
477{
478 char *cmd = req->buf;
479 char response[RESPONSE_LEN];
480
481 strsep(&cmd, ":");
482 if (!cmd) {
483 printf("%s: missing partition name\n", __func__);
484 fastboot_tx_write_str("FAILmissing partition name");
485 return;
486 }
487
488 strcpy(response, "FAILno flash device defined");
489#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
490 fb_mmc_flash_write(cmd, (void *)CONFIG_USB_FASTBOOT_BUF_ADDR,
491 download_bytes, response);
492#endif
493 fastboot_tx_write_str(response);
494}
495#endif
496
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500497struct cmd_dispatch_info {
498 char *cmd;
499 void (*cb)(struct usb_ep *ep, struct usb_request *req);
500};
501
502static const struct cmd_dispatch_info cmd_dispatch_info[] = {
503 {
504 .cmd = "reboot",
505 .cb = cb_reboot,
506 }, {
507 .cmd = "getvar:",
508 .cb = cb_getvar,
509 }, {
510 .cmd = "download:",
511 .cb = cb_download,
512 }, {
513 .cmd = "boot",
514 .cb = cb_boot,
515 },
Steve Raed1b5ed02014-08-26 11:47:28 -0700516#ifdef CONFIG_FASTBOOT_FLASH
517 {
518 .cmd = "flash",
519 .cb = cb_flash,
520 },
521#endif
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500522};
523
524static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
525{
526 char *cmdbuf = req->buf;
527 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
528 int i;
529
530 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
531 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
532 func_cb = cmd_dispatch_info[i].cb;
533 break;
534 }
535 }
536
537 if (!func_cb)
538 fastboot_tx_write_str("FAILunknown command");
539 else
540 func_cb(ep, req);
541
542 if (req->status == 0) {
543 *cmdbuf = '\0';
544 req->actual = 0;
545 usb_ep_queue(ep, req, 0);
546 }
547}