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+
18 #include <linux/usb/ch9.h>
19 #include <linux/usb/gadget.h>
20 #include <linux/usb/composite.h>
21 #include <linux/compiler.h>
24 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
27 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
31 #define FASTBOOT_VERSION "0.4"
33 #define FASTBOOT_INTERFACE_CLASS 0xff
34 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
35 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
37 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
38 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
39 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
41 #define EP_BUFFER_SIZE 4096
44 struct usb_function usb_function;
46 /* IN/OUT EP's and corresponding requests */
47 struct usb_ep *in_ep, *out_ep;
48 struct usb_request *in_req, *out_req;
51 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
53 return container_of(f, struct f_fastboot, usb_function);
56 static struct f_fastboot *fastboot_func;
57 static unsigned int fastboot_flash_session_id;
58 static unsigned int download_size;
59 static unsigned int download_bytes;
60 static bool is_high_speed;
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,
119 * static strings, in UTF-8
121 static const char fastboot_name[] = "Android Fastboot";
123 static struct usb_string fastboot_string_defs[] = {
124 [0].s = fastboot_name,
125 { } /* end of list */
128 static struct usb_gadget_strings stringtab_fastboot = {
129 .language = 0x0409, /* en-us */
130 .strings = fastboot_string_defs,
133 static struct usb_gadget_strings *fastboot_strings[] = {
138 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
139 static int strcmp_l1(const char *s1, const char *s2);
142 void fastboot_fail(char *response, const char *reason)
144 strncpy(response, "FAIL\0", 5);
145 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
148 void fastboot_okay(char *response, const char *reason)
150 strncpy(response, "OKAY\0", 5);
151 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
154 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
156 int status = req->status;
159 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
162 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
165 struct usb_gadget *gadget = c->cdev->gadget;
166 struct f_fastboot *f_fb = func_to_fastboot(f);
169 /* DYNAMIC interface numbers assignments */
170 id = usb_interface_id(c, f);
173 interface_desc.bInterfaceNumber = id;
175 id = usb_string_id(c->cdev);
178 fastboot_string_defs[0].id = id;
179 interface_desc.iInterface = id;
181 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
184 f_fb->in_ep->driver_data = c->cdev;
186 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
189 f_fb->out_ep->driver_data = c->cdev;
191 f->descriptors = fb_fs_function;
193 if (gadget_is_dualspeed(gadget)) {
194 /* Assume endpoint addresses are the same for both speeds */
195 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
196 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
197 /* copy HS descriptors */
198 f->hs_descriptors = fb_hs_function;
201 s = getenv("serial#");
203 g_dnl_set_serialnumber((char *)s);
208 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
210 memset(fastboot_func, 0, sizeof(*fastboot_func));
213 static void fastboot_disable(struct usb_function *f)
215 struct f_fastboot *f_fb = func_to_fastboot(f);
217 usb_ep_disable(f_fb->out_ep);
218 usb_ep_disable(f_fb->in_ep);
221 free(f_fb->out_req->buf);
222 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
223 f_fb->out_req = NULL;
226 free(f_fb->in_req->buf);
227 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
232 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
234 struct usb_request *req;
236 req = usb_ep_alloc_request(ep, 0);
240 req->length = EP_BUFFER_SIZE;
241 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
243 usb_ep_free_request(ep, req);
247 memset(req->buf, 0, req->length);
251 static int fastboot_set_alt(struct usb_function *f,
252 unsigned interface, unsigned alt)
255 struct usb_composite_dev *cdev = f->config->cdev;
256 struct usb_gadget *gadget = cdev->gadget;
257 struct f_fastboot *f_fb = func_to_fastboot(f);
259 debug("%s: func: %s intf: %d alt: %d\n",
260 __func__, f->name, interface, alt);
262 /* make sure we don't enable the ep twice */
263 if (gadget->speed == USB_SPEED_HIGH) {
264 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
265 is_high_speed = true;
267 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
268 is_high_speed = false;
271 puts("failed to enable out ep\n");
275 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
276 if (!f_fb->out_req) {
277 puts("failed to alloc out req\n");
281 f_fb->out_req->complete = rx_handler_command;
283 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
285 puts("failed to enable in ep\n");
289 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
291 puts("failed alloc req in\n");
295 f_fb->in_req->complete = fastboot_complete;
297 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
307 static int fastboot_add(struct usb_configuration *c)
309 struct f_fastboot *f_fb = fastboot_func;
312 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
315 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
319 fastboot_func = f_fb;
320 memset(f_fb, 0, sizeof(*f_fb));
323 f_fb->usb_function.name = "f_fastboot";
324 f_fb->usb_function.bind = fastboot_bind;
325 f_fb->usb_function.unbind = fastboot_unbind;
326 f_fb->usb_function.set_alt = fastboot_set_alt;
327 f_fb->usb_function.disable = fastboot_disable;
328 f_fb->usb_function.strings = fastboot_strings;
330 status = usb_add_function(c, &f_fb->usb_function);
333 fastboot_func = f_fb;
338 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
340 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
342 struct usb_request *in_req = fastboot_func->in_req;
345 memcpy(in_req->buf, buffer, buffer_size);
346 in_req->length = buffer_size;
348 usb_ep_dequeue(fastboot_func->in_ep, in_req);
350 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
352 printf("Error %d on queue\n", ret);
356 static int fastboot_tx_write_str(const char *buffer)
358 return fastboot_tx_write(buffer, strlen(buffer));
361 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
363 do_reset(NULL, 0, 0, NULL);
366 int __weak fb_set_reboot_flag(void)
371 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
373 char *cmd = req->buf;
374 if (!strcmp_l1("reboot-bootloader", cmd)) {
375 if (fb_set_reboot_flag()) {
376 fastboot_tx_write_str("FAILCannot set reboot flag");
380 fastboot_func->in_req->complete = compl_do_reset;
381 fastboot_tx_write_str("OKAY");
384 static int strcmp_l1(const char *s1, const char *s2)
388 return strncmp(s1, s2, strlen(s1));
391 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
393 char *cmd = req->buf;
394 char response[FASTBOOT_RESPONSE_LEN];
398 strcpy(response, "OKAY");
399 chars_left = sizeof(response) - strlen(response) - 1;
403 error("missing variable");
404 fastboot_tx_write_str("FAILmissing var");
408 if (!strcmp_l1("version", cmd)) {
409 strncat(response, FASTBOOT_VERSION, chars_left);
410 } else if (!strcmp_l1("bootloader-version", cmd)) {
411 strncat(response, U_BOOT_VERSION, chars_left);
412 } else if (!strcmp_l1("downloadsize", cmd) ||
413 !strcmp_l1("max-download-size", cmd)) {
416 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
417 strncat(response, str_num, chars_left);
420 * This also indicates the start of a new flashing
421 * "session", in which we could have 1-N buffers to
422 * write to a partition.
424 * Reset our session counter.
426 fastboot_flash_session_id = 0;
427 } else if (!strcmp_l1("serialno", cmd)) {
428 s = getenv("serial#");
430 strncat(response, s, chars_left);
432 strcpy(response, "FAILValue not set");
436 snprintf(envstr, sizeof(envstr) - 1, "fastboot.%s", cmd);
439 strncat(response, s, chars_left);
441 printf("WARNING: unknown variable: %s\n", cmd);
442 strcpy(response, "FAILVariable not implemented");
445 fastboot_tx_write_str(response);
448 static unsigned int rx_bytes_expected(unsigned int maxpacket)
450 int rx_remain = download_size - download_bytes;
454 if (rx_remain > EP_BUFFER_SIZE)
455 return EP_BUFFER_SIZE;
456 if (rx_remain < maxpacket) {
457 rx_remain = maxpacket;
458 } else if (rx_remain % maxpacket != 0) {
459 rem = rx_remain % maxpacket;
460 rx_remain = rx_remain + (maxpacket - rem);
465 #define BYTES_PER_DOT 0x20000
466 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
468 char response[FASTBOOT_RESPONSE_LEN];
469 unsigned int transfer_size = download_size - download_bytes;
470 const unsigned char *buffer = req->buf;
471 unsigned int buffer_size = req->actual;
472 unsigned int pre_dot_num, now_dot_num;
475 if (req->status != 0) {
476 printf("Bad status: %d\n", req->status);
480 if (buffer_size < transfer_size)
481 transfer_size = buffer_size;
483 memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
484 buffer, transfer_size);
486 pre_dot_num = download_bytes / BYTES_PER_DOT;
487 download_bytes += transfer_size;
488 now_dot_num = download_bytes / BYTES_PER_DOT;
490 if (pre_dot_num != now_dot_num) {
492 if (!(now_dot_num % 74))
496 /* Check if transfer is done */
497 if (download_bytes >= download_size) {
499 * Reset global transfer variable, keep download_bytes because
500 * it will be used in the next possible flashing command
503 req->complete = rx_handler_command;
504 req->length = EP_BUFFER_SIZE;
506 strcpy(response, "OKAY");
507 fastboot_tx_write_str(response);
509 printf("\ndownloading of %d bytes finished\n", download_bytes);
511 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
512 fs_ep_out.wMaxPacketSize;
513 req->length = rx_bytes_expected(max);
514 if (req->length < ep->maxpacket)
515 req->length = ep->maxpacket;
519 usb_ep_queue(ep, req, 0);
522 static void cb_download(struct usb_ep *ep, struct usb_request *req)
524 char *cmd = req->buf;
525 char response[FASTBOOT_RESPONSE_LEN];
529 download_size = simple_strtoul(cmd, NULL, 16);
532 printf("Starting download of %d bytes\n", download_size);
534 if (0 == download_size) {
535 strcpy(response, "FAILdata invalid size");
536 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
538 strcpy(response, "FAILdata too large");
540 sprintf(response, "DATA%08x", download_size);
541 req->complete = rx_handler_dl_image;
542 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
543 fs_ep_out.wMaxPacketSize;
544 req->length = rx_bytes_expected(max);
545 if (req->length < ep->maxpacket)
546 req->length = ep->maxpacket;
548 fastboot_tx_write_str(response);
551 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
553 char boot_addr_start[12];
554 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
556 puts("Booting kernel..\n");
558 sprintf(boot_addr_start, "0x%lx", load_addr);
559 do_bootm(NULL, 0, 2, bootm_args);
561 /* This only happens if image is somehow faulty so we start over */
562 do_reset(NULL, 0, 0, NULL);
565 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
567 fastboot_func->in_req->complete = do_bootm_on_complete;
568 fastboot_tx_write_str("OKAY");
571 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
573 g_dnl_trigger_detach();
576 static void cb_continue(struct usb_ep *ep, struct usb_request *req)
578 fastboot_func->in_req->complete = do_exit_on_complete;
579 fastboot_tx_write_str("OKAY");
582 #ifdef CONFIG_FASTBOOT_FLASH
583 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
585 char *cmd = req->buf;
586 char response[FASTBOOT_RESPONSE_LEN];
590 error("missing partition name");
591 fastboot_tx_write_str("FAILmissing partition name");
595 strcpy(response, "FAILno flash device defined");
596 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
597 fb_mmc_flash_write(cmd, fastboot_flash_session_id,
598 (void *)CONFIG_FASTBOOT_BUF_ADDR,
599 download_bytes, response);
601 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
602 fb_nand_flash_write(cmd, fastboot_flash_session_id,
603 (void *)CONFIG_FASTBOOT_BUF_ADDR,
604 download_bytes, response);
606 fastboot_flash_session_id++;
607 fastboot_tx_write_str(response);
611 static void cb_oem(struct usb_ep *ep, struct usb_request *req)
613 char *cmd = req->buf;
614 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
615 if (strncmp("format", cmd + 4, 6) == 0) {
617 sprintf(cmdbuf, "gpt write mmc %x $partitions",
618 CONFIG_FASTBOOT_FLASH_MMC_DEV);
619 if (run_command(cmdbuf, 0))
620 fastboot_tx_write_str("FAIL");
622 fastboot_tx_write_str("OKAY");
625 if (strncmp("unlock", cmd + 4, 8) == 0) {
626 fastboot_tx_write_str("FAILnot implemented");
629 fastboot_tx_write_str("FAILunknown oem command");
633 #ifdef CONFIG_FASTBOOT_FLASH
634 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
636 char *cmd = req->buf;
637 char response[FASTBOOT_RESPONSE_LEN];
641 error("missing partition name");
642 fastboot_tx_write_str("FAILmissing partition name");
646 strcpy(response, "FAILno flash device defined");
648 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
649 fb_mmc_erase(cmd, response);
651 #ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
652 fb_nand_erase(cmd, response);
654 fastboot_tx_write_str(response);
658 struct cmd_dispatch_info {
660 void (*cb)(struct usb_ep *ep, struct usb_request *req);
663 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
680 #ifdef CONFIG_FASTBOOT_FLASH
695 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
697 char *cmdbuf = req->buf;
698 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
701 if (req->status != 0 || req->length == 0)
704 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
705 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
706 func_cb = cmd_dispatch_info[i].cb;
712 error("unknown command: %s", cmdbuf);
713 fastboot_tx_write_str("FAILunknown command");
715 if (req->actual < req->length) {
716 u8 *buf = (u8 *)req->buf;
717 buf[req->actual] = 0;
720 error("buffer overflow");
721 fastboot_tx_write_str("FAILbuffer overflow");
727 usb_ep_queue(ep, req, 0);