2 * (C) Copyright 2008 - 2009
3 * Windriver, <www.windriver.com>
4 * Tom Rix <Tom.Rix@windriver.com>
6 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 * Copyright 2014 Linaro, Ltd.
9 * Rob Herring <robh@kernel.org>
11 * SPDX-License-Identifier: GPL-2.0+
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
19 #include <linux/usb/composite.h>
20 #include <linux/compiler.h>
23 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
27 #define FASTBOOT_VERSION "0.4"
29 #define FASTBOOT_INTERFACE_CLASS 0xff
30 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
31 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
33 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
34 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
35 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
37 /* The 64 defined bytes plus \0 */
38 #define RESPONSE_LEN (64 + 1)
40 #define EP_BUFFER_SIZE 4096
43 struct usb_function usb_function;
45 /* IN/OUT EP's and corresponding requests */
46 struct usb_ep *in_ep, *out_ep;
47 struct usb_request *in_req, *out_req;
50 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
52 return container_of(f, struct f_fastboot, usb_function);
55 static struct f_fastboot *fastboot_func;
56 static unsigned int download_size;
57 static unsigned int download_bytes;
58 static bool is_high_speed;
60 static struct usb_endpoint_descriptor fs_ep_in = {
61 .bLength = USB_DT_ENDPOINT_SIZE,
62 .bDescriptorType = USB_DT_ENDPOINT,
63 .bEndpointAddress = USB_DIR_IN,
64 .bmAttributes = USB_ENDPOINT_XFER_BULK,
65 .wMaxPacketSize = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
69 static struct usb_endpoint_descriptor fs_ep_out = {
70 .bLength = USB_DT_ENDPOINT_SIZE,
71 .bDescriptorType = USB_DT_ENDPOINT,
72 .bEndpointAddress = USB_DIR_OUT,
73 .bmAttributes = USB_ENDPOINT_XFER_BULK,
74 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
78 static 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,
83 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
87 static 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,
98 static struct usb_descriptor_header *fb_runtime_descs[] = {
99 (struct usb_descriptor_header *)&interface_desc,
100 (struct usb_descriptor_header *)&fs_ep_in,
101 (struct usb_descriptor_header *)&hs_ep_out,
106 * static strings, in UTF-8
108 static const char fastboot_name[] = "Android Fastboot";
110 static struct usb_string fastboot_string_defs[] = {
111 [0].s = fastboot_name,
112 { } /* end of list */
115 static struct usb_gadget_strings stringtab_fastboot = {
116 .language = 0x0409, /* en-us */
117 .strings = fastboot_string_defs,
120 static struct usb_gadget_strings *fastboot_strings[] = {
125 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
126 static int strcmp_l1(const char *s1, const char *s2);
128 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
130 int status = req->status;
133 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
136 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
139 struct usb_gadget *gadget = c->cdev->gadget;
140 struct f_fastboot *f_fb = func_to_fastboot(f);
143 /* DYNAMIC interface numbers assignments */
144 id = usb_interface_id(c, f);
147 interface_desc.bInterfaceNumber = id;
149 id = usb_string_id(c->cdev);
152 fastboot_string_defs[0].id = id;
153 interface_desc.iInterface = id;
155 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
158 f_fb->in_ep->driver_data = c->cdev;
160 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
163 f_fb->out_ep->driver_data = c->cdev;
165 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
167 s = getenv("serial#");
169 g_dnl_set_serialnumber((char *)s);
174 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
176 memset(fastboot_func, 0, sizeof(*fastboot_func));
179 static void fastboot_disable(struct usb_function *f)
181 struct f_fastboot *f_fb = func_to_fastboot(f);
183 usb_ep_disable(f_fb->out_ep);
184 usb_ep_disable(f_fb->in_ep);
187 free(f_fb->out_req->buf);
188 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
189 f_fb->out_req = NULL;
192 free(f_fb->in_req->buf);
193 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
198 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
200 struct usb_request *req;
202 req = usb_ep_alloc_request(ep, 0);
206 req->length = EP_BUFFER_SIZE;
207 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
209 usb_ep_free_request(ep, req);
213 memset(req->buf, 0, req->length);
217 static int fastboot_set_alt(struct usb_function *f,
218 unsigned interface, unsigned alt)
221 struct usb_composite_dev *cdev = f->config->cdev;
222 struct usb_gadget *gadget = cdev->gadget;
223 struct f_fastboot *f_fb = func_to_fastboot(f);
225 debug("%s: func: %s intf: %d alt: %d\n",
226 __func__, f->name, interface, alt);
228 /* make sure we don't enable the ep twice */
229 if (gadget->speed == USB_SPEED_HIGH) {
230 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
231 is_high_speed = true;
233 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
234 is_high_speed = false;
237 puts("failed to enable out ep\n");
241 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
242 if (!f_fb->out_req) {
243 puts("failed to alloc out req\n");
247 f_fb->out_req->complete = rx_handler_command;
249 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
251 puts("failed to enable in ep\n");
255 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
257 puts("failed alloc req in\n");
261 f_fb->in_req->complete = fastboot_complete;
263 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
273 static int fastboot_add(struct usb_configuration *c)
275 struct f_fastboot *f_fb = fastboot_func;
278 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
281 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
285 fastboot_func = f_fb;
286 memset(f_fb, 0, sizeof(*f_fb));
289 f_fb->usb_function.name = "f_fastboot";
290 f_fb->usb_function.hs_descriptors = fb_runtime_descs;
291 f_fb->usb_function.bind = fastboot_bind;
292 f_fb->usb_function.unbind = fastboot_unbind;
293 f_fb->usb_function.set_alt = fastboot_set_alt;
294 f_fb->usb_function.disable = fastboot_disable;
295 f_fb->usb_function.strings = fastboot_strings;
297 status = usb_add_function(c, &f_fb->usb_function);
300 fastboot_func = f_fb;
305 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
307 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
309 struct usb_request *in_req = fastboot_func->in_req;
312 memcpy(in_req->buf, buffer, buffer_size);
313 in_req->length = buffer_size;
314 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
316 printf("Error %d on queue\n", ret);
320 static int fastboot_tx_write_str(const char *buffer)
322 return fastboot_tx_write(buffer, strlen(buffer));
325 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
327 do_reset(NULL, 0, 0, NULL);
330 int __weak fb_set_reboot_flag(void)
335 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
337 char *cmd = req->buf;
338 if (!strcmp_l1("reboot-bootloader", cmd)) {
339 if (fb_set_reboot_flag()) {
340 fastboot_tx_write_str("FAILCannot set reboot flag");
344 fastboot_func->in_req->complete = compl_do_reset;
345 fastboot_tx_write_str("OKAY");
348 static int strcmp_l1(const char *s1, const char *s2)
352 return strncmp(s1, s2, strlen(s1));
355 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
357 char *cmd = req->buf;
358 char response[RESPONSE_LEN];
362 strcpy(response, "OKAY");
363 chars_left = sizeof(response) - strlen(response) - 1;
367 error("missing variable\n");
368 fastboot_tx_write_str("FAILmissing var");
372 if (!strcmp_l1("version", cmd)) {
373 strncat(response, FASTBOOT_VERSION, chars_left);
374 } else if (!strcmp_l1("bootloader-version", cmd)) {
375 strncat(response, U_BOOT_VERSION, chars_left);
376 } else if (!strcmp_l1("downloadsize", cmd) ||
377 !strcmp_l1("max-download-size", cmd)) {
380 sprintf(str_num, "0x%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
381 strncat(response, str_num, chars_left);
382 } else if (!strcmp_l1("serialno", cmd)) {
383 s = getenv("serial#");
385 strncat(response, s, chars_left);
387 strcpy(response, "FAILValue not set");
389 error("unknown variable: %s\n", cmd);
390 strcpy(response, "FAILVariable not implemented");
392 fastboot_tx_write_str(response);
395 static unsigned int rx_bytes_expected(unsigned int maxpacket)
397 int rx_remain = download_size - download_bytes;
401 if (rx_remain > EP_BUFFER_SIZE)
402 return EP_BUFFER_SIZE;
403 if (rx_remain < maxpacket) {
404 rx_remain = maxpacket;
405 } else if (rx_remain % maxpacket != 0) {
406 rem = rx_remain % maxpacket;
407 rx_remain = rx_remain + (maxpacket - rem);
412 #define BYTES_PER_DOT 0x20000
413 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
415 char response[RESPONSE_LEN];
416 unsigned int transfer_size = download_size - download_bytes;
417 const unsigned char *buffer = req->buf;
418 unsigned int buffer_size = req->actual;
419 unsigned int pre_dot_num, now_dot_num;
422 if (req->status != 0) {
423 printf("Bad status: %d\n", req->status);
427 if (buffer_size < transfer_size)
428 transfer_size = buffer_size;
430 memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
431 buffer, transfer_size);
433 pre_dot_num = download_bytes / BYTES_PER_DOT;
434 download_bytes += transfer_size;
435 now_dot_num = download_bytes / BYTES_PER_DOT;
437 if (pre_dot_num != now_dot_num) {
439 if (!(now_dot_num % 74))
443 /* Check if transfer is done */
444 if (download_bytes >= download_size) {
446 * Reset global transfer variable, keep download_bytes because
447 * it will be used in the next possible flashing command
450 req->complete = rx_handler_command;
451 req->length = EP_BUFFER_SIZE;
453 sprintf(response, "OKAY");
454 fastboot_tx_write_str(response);
456 printf("\ndownloading of %d bytes finished\n", download_bytes);
458 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
459 fs_ep_out.wMaxPacketSize;
460 req->length = rx_bytes_expected(max);
461 if (req->length < ep->maxpacket)
462 req->length = ep->maxpacket;
466 usb_ep_queue(ep, req, 0);
469 static void cb_download(struct usb_ep *ep, struct usb_request *req)
471 char *cmd = req->buf;
472 char response[RESPONSE_LEN];
476 download_size = simple_strtoul(cmd, NULL, 16);
479 printf("Starting download of %d bytes\n", download_size);
481 if (0 == download_size) {
482 sprintf(response, "FAILdata invalid size");
483 } else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
485 sprintf(response, "FAILdata too large");
487 sprintf(response, "DATA%08x", download_size);
488 req->complete = rx_handler_dl_image;
489 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
490 fs_ep_out.wMaxPacketSize;
491 req->length = rx_bytes_expected(max);
492 if (req->length < ep->maxpacket)
493 req->length = ep->maxpacket;
495 fastboot_tx_write_str(response);
498 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
500 char boot_addr_start[12];
501 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
503 puts("Booting kernel..\n");
505 sprintf(boot_addr_start, "0x%lx", load_addr);
506 do_bootm(NULL, 0, 2, bootm_args);
508 /* This only happens if image is somehow faulty so we start over */
509 do_reset(NULL, 0, 0, NULL);
512 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
514 fastboot_func->in_req->complete = do_bootm_on_complete;
515 fastboot_tx_write_str("OKAY");
518 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
520 g_dnl_trigger_detach();
523 static void cb_continue(struct usb_ep *ep, struct usb_request *req)
525 fastboot_func->in_req->complete = do_exit_on_complete;
526 fastboot_tx_write_str("OKAY");
529 #ifdef CONFIG_FASTBOOT_FLASH
530 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
532 char *cmd = req->buf;
533 char response[RESPONSE_LEN];
537 error("missing partition name\n");
538 fastboot_tx_write_str("FAILmissing partition name");
542 strcpy(response, "FAILno flash device defined");
543 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
544 fb_mmc_flash_write(cmd, (void *)CONFIG_USB_FASTBOOT_BUF_ADDR,
545 download_bytes, response);
547 fastboot_tx_write_str(response);
551 static void cb_oem(struct usb_ep *ep, struct usb_request *req)
553 char *cmd = req->buf;
554 #ifdef CONFIG_FASTBOOT_FLASH
555 if (strncmp("format", cmd + 4, 6) == 0) {
557 sprintf(cmdbuf, "gpt write mmc %x $partitions",
558 CONFIG_FASTBOOT_FLASH_MMC_DEV);
559 if (run_command(cmdbuf, 0))
560 fastboot_tx_write_str("FAIL");
562 fastboot_tx_write_str("OKAY");
565 if (strncmp("unlock", cmd + 4, 8) == 0) {
566 fastboot_tx_write_str("FAILnot implemented");
569 fastboot_tx_write_str("FAILunknown oem command");
573 #ifdef CONFIG_FASTBOOT_FLASH
574 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
576 char *cmd = req->buf;
577 char response[RESPONSE_LEN];
581 error("missing partition name");
582 fastboot_tx_write_str("FAILmissing partition name");
586 strcpy(response, "FAILno flash device defined");
588 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
589 fb_mmc_erase(cmd, response);
591 fastboot_tx_write_str(response);
595 struct cmd_dispatch_info {
597 void (*cb)(struct usb_ep *ep, struct usb_request *req);
600 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
617 #ifdef CONFIG_FASTBOOT_FLASH
632 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
634 char *cmdbuf = req->buf;
635 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
638 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
639 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
640 func_cb = cmd_dispatch_info[i].cb;
646 error("unknown command: %s\n", cmdbuf);
647 fastboot_tx_write_str("FAILunknown command");
649 if (req->actual < req->length) {
650 u8 *buf = (u8 *)req->buf;
651 buf[req->actual] = 0;
654 error("buffer overflow\n");
655 fastboot_tx_write_str("FAILbuffer overflow");
659 if (req->status == 0) {
662 usb_ep_queue(ep, req, 0);