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
28 #define FASTBOOT_VERSION "0.4"
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
41 struct usb_function usb_function;
43 /* IN/OUT EP's and corresponding requests */
44 struct usb_ep *in_ep, *out_ep;
45 struct usb_request *in_req, *out_req;
48 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
50 return container_of(f, struct f_fastboot, usb_function);
53 static struct f_fastboot *fastboot_func;
54 static unsigned int download_size;
55 static unsigned int download_bytes;
56 static bool is_high_speed;
58 static 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,
67 static 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,
76 static 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,
85 static 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,
96 static 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,
104 * static strings, in UTF-8
106 static const char fastboot_name[] = "Android Fastboot";
108 static struct usb_string fastboot_string_defs[] = {
109 [0].s = fastboot_name,
110 { } /* end of list */
113 static struct usb_gadget_strings stringtab_fastboot = {
114 .language = 0x0409, /* en-us */
115 .strings = fastboot_string_defs,
118 static struct usb_gadget_strings *fastboot_strings[] = {
123 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
124 static int strcmp_l1(const char *s1, const char *s2);
127 void fastboot_fail(char *response, const char *reason)
129 strncpy(response, "FAIL\0", 5);
130 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
133 void fastboot_okay(char *response, const char *reason)
135 strncpy(response, "OKAY\0", 5);
136 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
139 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
141 int status = req->status;
144 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
147 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
150 struct usb_gadget *gadget = c->cdev->gadget;
151 struct f_fastboot *f_fb = func_to_fastboot(f);
154 /* DYNAMIC interface numbers assignments */
155 id = usb_interface_id(c, f);
158 interface_desc.bInterfaceNumber = id;
160 id = usb_string_id(c->cdev);
163 fastboot_string_defs[0].id = id;
164 interface_desc.iInterface = id;
166 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
169 f_fb->in_ep->driver_data = c->cdev;
171 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
174 f_fb->out_ep->driver_data = c->cdev;
176 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
178 s = getenv("serial#");
180 g_dnl_set_serialnumber((char *)s);
185 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
187 memset(fastboot_func, 0, sizeof(*fastboot_func));
190 static void fastboot_disable(struct usb_function *f)
192 struct f_fastboot *f_fb = func_to_fastboot(f);
194 usb_ep_disable(f_fb->out_ep);
195 usb_ep_disable(f_fb->in_ep);
198 free(f_fb->out_req->buf);
199 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
200 f_fb->out_req = NULL;
203 free(f_fb->in_req->buf);
204 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
209 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
211 struct usb_request *req;
213 req = usb_ep_alloc_request(ep, 0);
217 req->length = EP_BUFFER_SIZE;
218 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
220 usb_ep_free_request(ep, req);
224 memset(req->buf, 0, req->length);
228 static int fastboot_set_alt(struct usb_function *f,
229 unsigned interface, unsigned alt)
232 struct usb_composite_dev *cdev = f->config->cdev;
233 struct usb_gadget *gadget = cdev->gadget;
234 struct f_fastboot *f_fb = func_to_fastboot(f);
236 debug("%s: func: %s intf: %d alt: %d\n",
237 __func__, f->name, interface, alt);
239 /* make sure we don't enable the ep twice */
240 if (gadget->speed == USB_SPEED_HIGH) {
241 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
242 is_high_speed = true;
244 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
245 is_high_speed = false;
248 puts("failed to enable out ep\n");
252 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
253 if (!f_fb->out_req) {
254 puts("failed to alloc out req\n");
258 f_fb->out_req->complete = rx_handler_command;
260 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
262 puts("failed to enable in ep\n");
266 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
268 puts("failed alloc req in\n");
272 f_fb->in_req->complete = fastboot_complete;
274 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
284 static int fastboot_add(struct usb_configuration *c)
286 struct f_fastboot *f_fb = fastboot_func;
289 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
292 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
296 fastboot_func = f_fb;
297 memset(f_fb, 0, sizeof(*f_fb));
300 f_fb->usb_function.name = "f_fastboot";
301 f_fb->usb_function.hs_descriptors = fb_runtime_descs;
302 f_fb->usb_function.bind = fastboot_bind;
303 f_fb->usb_function.unbind = fastboot_unbind;
304 f_fb->usb_function.set_alt = fastboot_set_alt;
305 f_fb->usb_function.disable = fastboot_disable;
306 f_fb->usb_function.strings = fastboot_strings;
308 status = usb_add_function(c, &f_fb->usb_function);
311 fastboot_func = f_fb;
316 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
318 static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
320 struct usb_request *in_req = fastboot_func->in_req;
323 memcpy(in_req->buf, buffer, buffer_size);
324 in_req->length = buffer_size;
326 usb_ep_dequeue(fastboot_func->in_ep, in_req);
328 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
330 printf("Error %d on queue\n", ret);
334 static int fastboot_tx_write_str(const char *buffer)
336 return fastboot_tx_write(buffer, strlen(buffer));
339 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
341 do_reset(NULL, 0, 0, NULL);
344 int __weak fb_set_reboot_flag(void)
349 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
351 char *cmd = req->buf;
352 if (!strcmp_l1("reboot-bootloader", cmd)) {
353 if (fb_set_reboot_flag()) {
354 fastboot_tx_write_str("FAILCannot set reboot flag");
358 fastboot_func->in_req->complete = compl_do_reset;
359 fastboot_tx_write_str("OKAY");
362 static int strcmp_l1(const char *s1, const char *s2)
366 return strncmp(s1, s2, strlen(s1));
369 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
371 char *cmd = req->buf;
372 char response[FASTBOOT_RESPONSE_LEN];
376 strcpy(response, "OKAY");
377 chars_left = sizeof(response) - strlen(response) - 1;
381 error("missing variable\n");
382 fastboot_tx_write_str("FAILmissing var");
386 if (!strcmp_l1("version", cmd)) {
387 strncat(response, FASTBOOT_VERSION, chars_left);
388 } else if (!strcmp_l1("bootloader-version", cmd)) {
389 strncat(response, U_BOOT_VERSION, chars_left);
390 } else if (!strcmp_l1("downloadsize", cmd) ||
391 !strcmp_l1("max-download-size", cmd)) {
394 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
395 strncat(response, str_num, chars_left);
396 } else if (!strcmp_l1("serialno", cmd)) {
397 s = getenv("serial#");
399 strncat(response, s, chars_left);
401 strcpy(response, "FAILValue not set");
403 error("unknown variable: %s\n", cmd);
404 strcpy(response, "FAILVariable not implemented");
406 fastboot_tx_write_str(response);
409 static unsigned int rx_bytes_expected(unsigned int maxpacket)
411 int rx_remain = download_size - download_bytes;
415 if (rx_remain > EP_BUFFER_SIZE)
416 return EP_BUFFER_SIZE;
417 if (rx_remain < maxpacket) {
418 rx_remain = maxpacket;
419 } else if (rx_remain % maxpacket != 0) {
420 rem = rx_remain % maxpacket;
421 rx_remain = rx_remain + (maxpacket - rem);
426 #define BYTES_PER_DOT 0x20000
427 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
429 char response[FASTBOOT_RESPONSE_LEN];
430 unsigned int transfer_size = download_size - download_bytes;
431 const unsigned char *buffer = req->buf;
432 unsigned int buffer_size = req->actual;
433 unsigned int pre_dot_num, now_dot_num;
436 if (req->status != 0) {
437 printf("Bad status: %d\n", req->status);
441 if (buffer_size < transfer_size)
442 transfer_size = buffer_size;
444 memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
445 buffer, transfer_size);
447 pre_dot_num = download_bytes / BYTES_PER_DOT;
448 download_bytes += transfer_size;
449 now_dot_num = download_bytes / BYTES_PER_DOT;
451 if (pre_dot_num != now_dot_num) {
453 if (!(now_dot_num % 74))
457 /* Check if transfer is done */
458 if (download_bytes >= download_size) {
460 * Reset global transfer variable, keep download_bytes because
461 * it will be used in the next possible flashing command
464 req->complete = rx_handler_command;
465 req->length = EP_BUFFER_SIZE;
467 sprintf(response, "OKAY");
468 fastboot_tx_write_str(response);
470 printf("\ndownloading of %d bytes finished\n", download_bytes);
472 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
473 fs_ep_out.wMaxPacketSize;
474 req->length = rx_bytes_expected(max);
475 if (req->length < ep->maxpacket)
476 req->length = ep->maxpacket;
480 usb_ep_queue(ep, req, 0);
483 static void cb_download(struct usb_ep *ep, struct usb_request *req)
485 char *cmd = req->buf;
486 char response[FASTBOOT_RESPONSE_LEN];
490 download_size = simple_strtoul(cmd, NULL, 16);
493 printf("Starting download of %d bytes\n", download_size);
495 if (0 == download_size) {
496 sprintf(response, "FAILdata invalid size");
497 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
499 sprintf(response, "FAILdata too large");
501 sprintf(response, "DATA%08x", download_size);
502 req->complete = rx_handler_dl_image;
503 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
504 fs_ep_out.wMaxPacketSize;
505 req->length = rx_bytes_expected(max);
506 if (req->length < ep->maxpacket)
507 req->length = ep->maxpacket;
509 fastboot_tx_write_str(response);
512 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
514 char boot_addr_start[12];
515 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
517 puts("Booting kernel..\n");
519 sprintf(boot_addr_start, "0x%lx", load_addr);
520 do_bootm(NULL, 0, 2, bootm_args);
522 /* This only happens if image is somehow faulty so we start over */
523 do_reset(NULL, 0, 0, NULL);
526 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
528 fastboot_func->in_req->complete = do_bootm_on_complete;
529 fastboot_tx_write_str("OKAY");
532 static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
534 g_dnl_trigger_detach();
537 static void cb_continue(struct usb_ep *ep, struct usb_request *req)
539 fastboot_func->in_req->complete = do_exit_on_complete;
540 fastboot_tx_write_str("OKAY");
543 #ifdef CONFIG_FASTBOOT_FLASH
544 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
546 char *cmd = req->buf;
547 char response[FASTBOOT_RESPONSE_LEN];
551 error("missing partition name\n");
552 fastboot_tx_write_str("FAILmissing partition name");
556 strcpy(response, "FAILno flash device defined");
557 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
558 fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
559 download_bytes, response);
561 fastboot_tx_write_str(response);
565 static void cb_oem(struct usb_ep *ep, struct usb_request *req)
567 char *cmd = req->buf;
568 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
569 if (strncmp("format", cmd + 4, 6) == 0) {
571 sprintf(cmdbuf, "gpt write mmc %x $partitions",
572 CONFIG_FASTBOOT_FLASH_MMC_DEV);
573 if (run_command(cmdbuf, 0))
574 fastboot_tx_write_str("FAIL");
576 fastboot_tx_write_str("OKAY");
579 if (strncmp("unlock", cmd + 4, 8) == 0) {
580 fastboot_tx_write_str("FAILnot implemented");
583 fastboot_tx_write_str("FAILunknown oem command");
587 #ifdef CONFIG_FASTBOOT_FLASH
588 static void cb_erase(struct usb_ep *ep, struct usb_request *req)
590 char *cmd = req->buf;
591 char response[FASTBOOT_RESPONSE_LEN];
595 error("missing partition name");
596 fastboot_tx_write_str("FAILmissing partition name");
600 strcpy(response, "FAILno flash device defined");
602 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
603 fb_mmc_erase(cmd, response);
605 fastboot_tx_write_str(response);
609 struct cmd_dispatch_info {
611 void (*cb)(struct usb_ep *ep, struct usb_request *req);
614 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
631 #ifdef CONFIG_FASTBOOT_FLASH
646 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
648 char *cmdbuf = req->buf;
649 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
652 if (req->status != 0 || req->length == 0)
655 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
656 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
657 func_cb = cmd_dispatch_info[i].cb;
663 error("unknown command: %s\n", cmdbuf);
664 fastboot_tx_write_str("FAILunknown command");
666 if (req->actual < req->length) {
667 u8 *buf = (u8 *)req->buf;
668 buf[req->actual] = 0;
671 error("buffer overflow\n");
672 fastboot_tx_write_str("FAILbuffer overflow");
678 usb_ep_queue(ep, req, 0);