1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 - 2009
4 * Windriver, <www.windriver.com>
5 * Tom Rix <Tom.Rix@windriver.com>
7 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 * Copyright 2014 Linaro, Ltd.
10 * Rob Herring <robh@kernel.org>
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
26 #ifdef CONFIG_FASTBOOT_FLASH_NAND
30 #define FASTBOOT_INTERFACE_CLASS 0xff
31 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
32 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
34 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
35 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
36 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
38 #define EP_BUFFER_SIZE 4096
40 * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
41 * (64 or 512 or 1024), else we break on certain controllers like DWC3
42 * that expect bulk OUT requests to be divisible by maxpacket size.
46 struct usb_function usb_function;
48 /* IN/OUT EP's and corresponding requests */
49 struct usb_ep *in_ep, *out_ep;
50 struct usb_request *in_req, *out_req;
53 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
55 return container_of(f, struct f_fastboot, usb_function);
58 static struct f_fastboot *fastboot_func;
59 static unsigned int download_size;
60 static unsigned int download_bytes;
62 static struct usb_endpoint_descriptor fs_ep_in = {
63 .bLength = USB_DT_ENDPOINT_SIZE,
64 .bDescriptorType = USB_DT_ENDPOINT,
65 .bEndpointAddress = USB_DIR_IN,
66 .bmAttributes = USB_ENDPOINT_XFER_BULK,
67 .wMaxPacketSize = cpu_to_le16(64),
70 static struct usb_endpoint_descriptor fs_ep_out = {
71 .bLength = USB_DT_ENDPOINT_SIZE,
72 .bDescriptorType = USB_DT_ENDPOINT,
73 .bEndpointAddress = USB_DIR_OUT,
74 .bmAttributes = USB_ENDPOINT_XFER_BULK,
75 .wMaxPacketSize = cpu_to_le16(64),
78 static struct usb_endpoint_descriptor hs_ep_in = {
79 .bLength = USB_DT_ENDPOINT_SIZE,
80 .bDescriptorType = USB_DT_ENDPOINT,
81 .bEndpointAddress = USB_DIR_IN,
82 .bmAttributes = USB_ENDPOINT_XFER_BULK,
83 .wMaxPacketSize = cpu_to_le16(512),
86 static struct usb_endpoint_descriptor hs_ep_out = {
87 .bLength = USB_DT_ENDPOINT_SIZE,
88 .bDescriptorType = USB_DT_ENDPOINT,
89 .bEndpointAddress = USB_DIR_OUT,
90 .bmAttributes = USB_ENDPOINT_XFER_BULK,
91 .wMaxPacketSize = cpu_to_le16(512),
94 static struct usb_interface_descriptor interface_desc = {
95 .bLength = USB_DT_INTERFACE_SIZE,
96 .bDescriptorType = USB_DT_INTERFACE,
97 .bInterfaceNumber = 0x00,
98 .bAlternateSetting = 0x00,
99 .bNumEndpoints = 0x02,
100 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
101 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
102 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
105 static struct usb_descriptor_header *fb_fs_function[] = {
106 (struct usb_descriptor_header *)&interface_desc,
107 (struct usb_descriptor_header *)&fs_ep_in,
108 (struct usb_descriptor_header *)&fs_ep_out,
111 static struct usb_descriptor_header *fb_hs_function[] = {
112 (struct usb_descriptor_header *)&interface_desc,
113 (struct usb_descriptor_header *)&hs_ep_in,
114 (struct usb_descriptor_header *)&hs_ep_out,
118 static struct usb_endpoint_descriptor *
119 fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
120 struct usb_endpoint_descriptor *hs)
122 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
128 * static strings, in UTF-8
130 static const char fastboot_name[] = "Android Fastboot";
132 static struct usb_string fastboot_string_defs[] = {
133 [0].s = fastboot_name,
134 { } /* end of list */
137 static struct usb_gadget_strings stringtab_fastboot = {
138 .language = 0x0409, /* en-us */
139 .strings = fastboot_string_defs,
142 static struct usb_gadget_strings *fastboot_strings[] = {
147 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
148 static int strcmp_l1(const char *s1, const char *s2);
150 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
152 int status = req->status;
155 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
158 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
161 struct usb_gadget *gadget = c->cdev->gadget;
162 struct f_fastboot *f_fb = func_to_fastboot(f);
165 /* DYNAMIC interface numbers assignments */
166 id = usb_interface_id(c, f);
169 interface_desc.bInterfaceNumber = id;
171 id = usb_string_id(c->cdev);
174 fastboot_string_defs[0].id = id;
175 interface_desc.iInterface = id;
177 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
180 f_fb->in_ep->driver_data = c->cdev;
182 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
185 f_fb->out_ep->driver_data = c->cdev;
187 f->descriptors = fb_fs_function;
189 if (gadget_is_dualspeed(gadget)) {
190 /* Assume endpoint addresses are the same for both speeds */
191 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
192 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
193 /* copy HS descriptors */
194 f->hs_descriptors = fb_hs_function;
197 s = env_get("serial#");
199 g_dnl_set_serialnumber((char *)s);
204 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
206 memset(fastboot_func, 0, sizeof(*fastboot_func));
209 static void fastboot_disable(struct usb_function *f)
211 struct f_fastboot *f_fb = func_to_fastboot(f);
213 usb_ep_disable(f_fb->out_ep);
214 usb_ep_disable(f_fb->in_ep);
217 free(f_fb->out_req->buf);
218 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
219 f_fb->out_req = NULL;
222 free(f_fb->in_req->buf);
223 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
228 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
230 struct usb_request *req;
232 req = usb_ep_alloc_request(ep, 0);
236 req->length = EP_BUFFER_SIZE;
237 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
239 usb_ep_free_request(ep, req);
243 memset(req->buf, 0, req->length);
247 static int fastboot_set_alt(struct usb_function *f,
248 unsigned interface, unsigned alt)
251 struct usb_composite_dev *cdev = f->config->cdev;
252 struct usb_gadget *gadget = cdev->gadget;
253 struct f_fastboot *f_fb = func_to_fastboot(f);
254 const struct usb_endpoint_descriptor *d;
256 debug("%s: func: %s intf: %d alt: %d\n",
257 __func__, f->name, interface, alt);
259 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
260 ret = usb_ep_enable(f_fb->out_ep, d);
262 puts("failed to enable out ep\n");
266 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
267 if (!f_fb->out_req) {
268 puts("failed to alloc out req\n");
272 f_fb->out_req->complete = rx_handler_command;
274 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
275 ret = usb_ep_enable(f_fb->in_ep, d);
277 puts("failed to enable in ep\n");
281 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
283 puts("failed alloc req in\n");
287 f_fb->in_req->complete = fastboot_complete;
289 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
299 static int fastboot_add(struct usb_configuration *c)
301 struct f_fastboot *f_fb = fastboot_func;
304 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
307 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
311 fastboot_func = f_fb;
312 memset(f_fb, 0, sizeof(*f_fb));
315 f_fb->usb_function.name = "f_fastboot";
316 f_fb->usb_function.bind = fastboot_bind;
317 f_fb->usb_function.unbind = fastboot_unbind;
318 f_fb->usb_function.set_alt = fastboot_set_alt;
319 f_fb->usb_function.disable = fastboot_disable;
320 f_fb->usb_function.strings = fastboot_strings;
322 status = usb_add_function(c, &f_fb->usb_function);
325 fastboot_func = f_fb;
330 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
332 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
334 struct usb_request *in_req = fastboot_func->in_req;
337 memcpy(in_req->buf, buffer, buffer_size);
338 in_req->length = buffer_size;
340 usb_ep_dequeue(fastboot_func->in_ep, in_req);
342 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
344 printf("Error %d on queue\n", ret);
348 static int fastboot_tx_write_str(const char *buffer)
350 return fastboot_tx_write(buffer, strlen(buffer));
353 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
355 do_reset(NULL, 0, 0, NULL);
358 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
360 char *cmd = req->buf;
361 if (!strcmp_l1("reboot-bootloader", cmd)) {
362 if (fastboot_set_reboot_flag()) {
363 fastboot_tx_write_str("FAILCannot set reboot flag");
367 fastboot_func->in_req->complete = compl_do_reset;
368 fastboot_tx_write_str("OKAY");
371 static int strcmp_l1(const char *s1, const char *s2)
375 return strncmp(s1, s2, strlen(s1));
378 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
380 char *cmd = req->buf;
381 char response[FASTBOOT_RESPONSE_LEN];
385 strcpy(response, "OKAY");
386 chars_left = sizeof(response) - strlen(response) - 1;
390 pr_err("missing variable");
391 fastboot_tx_write_str("FAILmissing var");
395 if (!strcmp_l1("version", cmd)) {
396 strncat(response, FASTBOOT_VERSION, chars_left);
397 } else if (!strcmp_l1("bootloader-version", cmd)) {
398 strncat(response, U_BOOT_VERSION, chars_left);
399 } else if (!strcmp_l1("downloadsize", cmd) ||
400 !strcmp_l1("max-download-size", cmd)) {
403 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
404 strncat(response, str_num, chars_left);
405 } else if (!strcmp_l1("serialno", cmd)) {
406 s = env_get("serial#");
408 strncat(response, s, chars_left);
410 strcpy(response, "FAILValue not set");
414 envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1);
416 fastboot_tx_write_str("FAILmalloc error");
420 sprintf(envstr, "fastboot.%s", cmd);
423 strncat(response, s, chars_left);
425 printf("WARNING: unknown variable: %s\n", cmd);
426 strcpy(response, "FAILVariable not implemented");
431 fastboot_tx_write_str(response);
434 static unsigned int rx_bytes_expected(struct usb_ep *ep)
436 int rx_remain = download_size - download_bytes;
438 unsigned int maxpacket = ep->maxpacket;
442 else if (rx_remain > EP_BUFFER_SIZE)
443 return EP_BUFFER_SIZE;
446 * Some controllers e.g. DWC3 don't like OUT transfers to be
447 * not ending in maxpacket boundary. So just make them happy by
448 * always requesting for integral multiple of maxpackets.
449 * This shouldn't bother controllers that don't care about it.
451 rem = rx_remain % maxpacket;
453 rx_remain = rx_remain + (maxpacket - rem);
458 #define BYTES_PER_DOT 0x20000
459 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
461 char response[FASTBOOT_RESPONSE_LEN];
462 unsigned int transfer_size = download_size - download_bytes;
463 const unsigned char *buffer = req->buf;
464 unsigned int buffer_size = req->actual;
465 unsigned int pre_dot_num, now_dot_num;
467 if (req->status != 0) {
468 printf("Bad status: %d\n", req->status);
472 if (buffer_size < transfer_size)
473 transfer_size = buffer_size;
475 memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
476 buffer, transfer_size);
478 pre_dot_num = download_bytes / BYTES_PER_DOT;
479 download_bytes += transfer_size;
480 now_dot_num = download_bytes / BYTES_PER_DOT;
482 if (pre_dot_num != now_dot_num) {
484 if (!(now_dot_num % 74))
488 /* Check if transfer is done */
489 if (download_bytes >= download_size) {
491 * Reset global transfer variable, keep download_bytes because
492 * it will be used in the next possible flashing command
495 req->complete = rx_handler_command;
496 req->length = EP_BUFFER_SIZE;
498 strcpy(response, "OKAY");
499 fastboot_tx_write_str(response);
501 printf("\ndownloading of %d bytes finished\n", download_bytes);
503 req->length = rx_bytes_expected(ep);
507 usb_ep_queue(ep, req, 0);
510 static void cb_download(struct usb_ep *ep, struct usb_request *req)
512 char *cmd = req->buf;
513 char response[FASTBOOT_RESPONSE_LEN];
516 download_size = simple_strtoul(cmd, NULL, 16);
519 printf("Starting download of %d bytes\n", download_size);
521 if (0 == download_size) {
522 strcpy(response, "FAILdata invalid size");
523 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
525 strcpy(response, "FAILdata too large");
527 sprintf(response, "DATA%08x", download_size);
528 req->complete = rx_handler_dl_image;
529 req->length = rx_bytes_expected(ep);
531 fastboot_tx_write_str(response);
534 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
536 char boot_addr_start[12];
537 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
539 puts("Booting kernel..\n");
541 sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR);
542 do_bootm(NULL, 0, 2, bootm_args);
544 /* This only happens if image is somehow faulty so we start over */
545 do_reset(NULL, 0, 0, NULL);
548 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
550 fastboot_func->in_req->complete = do_bootm_on_complete;
551 fastboot_tx_write_str("OKAY");
554 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
556 g_dnl_trigger_detach();
559 static void cb_continue(struct usb_ep *ep, struct usb_request *req)
561 fastboot_func->in_req->complete = do_exit_on_complete;
562 fastboot_tx_write_str("OKAY");
565 #ifdef CONFIG_FASTBOOT_FLASH
566 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
568 char *cmd = req->buf;
569 char response[FASTBOOT_RESPONSE_LEN];
573 pr_err("missing partition name");
574 fastboot_tx_write_str("FAILmissing partition name");
578 fastboot_fail("no flash device defined", response);
579 #ifdef CONFIG_FASTBOOT_FLASH_MMC
580 fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
581 download_bytes, response);
583 #ifdef CONFIG_FASTBOOT_FLASH_NAND
584 fb_nand_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
585 download_bytes, response);
587 fastboot_tx_write_str(response);
591 static void cb_oem(struct usb_ep *ep, struct usb_request *req)
593 char *cmd = req->buf;
594 #ifdef CONFIG_FASTBOOT_FLASH_MMC
595 if (strncmp("format", cmd + 4, 6) == 0) {
597 sprintf(cmdbuf, "gpt write mmc %x $partitions",
598 CONFIG_FASTBOOT_FLASH_MMC_DEV);
599 if (run_command(cmdbuf, 0))
600 fastboot_tx_write_str("FAIL");
602 fastboot_tx_write_str("OKAY");
605 if (strncmp("unlock", cmd + 4, 8) == 0) {
606 fastboot_tx_write_str("FAILnot implemented");
609 fastboot_tx_write_str("FAILunknown oem command");
613 #ifdef CONFIG_FASTBOOT_FLASH
614 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
616 char *cmd = req->buf;
617 char response[FASTBOOT_RESPONSE_LEN];
621 pr_err("missing partition name");
622 fastboot_tx_write_str("FAILmissing partition name");
626 fastboot_fail("no flash device defined", response);
627 #ifdef CONFIG_FASTBOOT_FLASH_MMC
628 fb_mmc_erase(cmd, response);
630 #ifdef CONFIG_FASTBOOT_FLASH_NAND
631 fb_nand_erase(cmd, response);
633 fastboot_tx_write_str(response);
637 struct cmd_dispatch_info {
639 void (*cb)(struct usb_ep *ep, struct usb_request *req);
642 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
659 #ifdef CONFIG_FASTBOOT_FLASH
674 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
676 char *cmdbuf = req->buf;
677 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
680 if (req->status != 0 || req->length == 0)
683 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
684 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
685 func_cb = cmd_dispatch_info[i].cb;
691 pr_err("unknown command: %.*s", req->actual, cmdbuf);
692 fastboot_tx_write_str("FAILunknown command");
694 if (req->actual < req->length) {
695 u8 *buf = (u8 *)req->buf;
696 buf[req->actual] = 0;
699 pr_err("buffer overflow");
700 fastboot_tx_write_str("FAILbuffer overflow");
706 usb_ep_queue(ep, req, 0);