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+
16 #include <linux/usb/ch9.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/usb/composite.h>
19 #include <linux/compiler.h>
22 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
26 #define FASTBOOT_VERSION "0.4"
28 #define FASTBOOT_INTERFACE_CLASS 0xff
29 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
30 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
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)
36 /* The 64 defined bytes plus \0 */
37 #define RESPONSE_LEN (64 + 1)
39 #define EP_BUFFER_SIZE 4096
42 struct usb_function usb_function;
44 /* IN/OUT EP's and correspoinding requests */
45 struct usb_ep *in_ep, *out_ep;
46 struct usb_request *in_req, *out_req;
49 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
51 return container_of(f, struct f_fastboot, usb_function);
54 static struct f_fastboot *fastboot_func;
55 static unsigned int download_size;
56 static unsigned int download_bytes;
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);
125 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
127 int status = req->status;
130 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
133 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
136 struct usb_gadget *gadget = c->cdev->gadget;
137 struct f_fastboot *f_fb = func_to_fastboot(f);
139 /* DYNAMIC interface numbers assignments */
140 id = usb_interface_id(c, f);
143 interface_desc.bInterfaceNumber = id;
145 id = usb_string_id(c->cdev);
148 fastboot_string_defs[0].id = id;
149 interface_desc.iInterface = id;
151 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
154 f_fb->in_ep->driver_data = c->cdev;
156 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
159 f_fb->out_ep->driver_data = c->cdev;
161 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
166 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
168 memset(fastboot_func, 0, sizeof(*fastboot_func));
171 static void fastboot_disable(struct usb_function *f)
173 struct f_fastboot *f_fb = func_to_fastboot(f);
175 usb_ep_disable(f_fb->out_ep);
176 usb_ep_disable(f_fb->in_ep);
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;
184 free(f_fb->in_req->buf);
185 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
190 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
192 struct usb_request *req;
194 req = usb_ep_alloc_request(ep, 0);
198 req->length = EP_BUFFER_SIZE;
199 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
201 usb_ep_free_request(ep, req);
205 memset(req->buf, 0, req->length);
209 static int fastboot_set_alt(struct usb_function *f,
210 unsigned interface, unsigned alt)
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);
217 debug("%s: func: %s intf: %d alt: %d\n",
218 __func__, f->name, interface, alt);
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);
224 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
226 puts("failed to enable out ep\n");
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");
236 f_fb->out_req->complete = rx_handler_command;
238 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
240 puts("failed to enable in ep\n");
244 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
246 puts("failed alloc req in\n");
250 f_fb->in_req->complete = fastboot_complete;
252 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
262 static int fastboot_add(struct usb_configuration *c)
264 struct f_fastboot *f_fb = fastboot_func;
267 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
270 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
274 fastboot_func = f_fb;
275 memset(f_fb, 0, sizeof(*f_fb));
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;
286 status = usb_add_function(c, &f_fb->usb_function);
289 fastboot_func = f_fb;
294 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
296 int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
298 struct usb_request *in_req = fastboot_func->in_req;
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);
305 printf("Error %d on queue\n", ret);
309 static int fastboot_tx_write_str(const char *buffer)
311 return fastboot_tx_write(buffer, strlen(buffer));
314 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
316 do_reset(NULL, 0, 0, NULL);
319 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
321 fastboot_func->in_req->complete = compl_do_reset;
322 fastboot_tx_write_str("OKAY");
325 static int strcmp_l1(const char *s1, const char *s2)
329 return strncmp(s1, s2, strlen(s1));
332 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
334 char *cmd = req->buf;
335 char response[RESPONSE_LEN];
339 strcpy(response, "OKAY");
340 chars_left = sizeof(response) - strlen(response) - 1;
344 fastboot_tx_write_str("FAILmissing var");
348 if (!strcmp_l1("version", cmd)) {
349 strncat(response, FASTBOOT_VERSION, chars_left);
350 } else if (!strcmp_l1("bootloader-version", cmd)) {
351 strncat(response, U_BOOT_VERSION, chars_left);
352 } else if (!strcmp_l1("downloadsize", cmd)) {
355 sprintf(str_num, "%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
356 strncat(response, str_num, chars_left);
357 } else if (!strcmp_l1("serialno", cmd)) {
358 s = getenv("serial#");
360 strncat(response, s, chars_left);
362 strcpy(response, "FAILValue not set");
364 strcpy(response, "FAILVariable not implemented");
366 fastboot_tx_write_str(response);
369 static unsigned int rx_bytes_expected(void)
371 int rx_remain = download_size - download_bytes;
374 if (rx_remain > EP_BUFFER_SIZE)
375 return EP_BUFFER_SIZE;
379 #define BYTES_PER_DOT 0x20000
380 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
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;
387 if (req->status != 0) {
388 printf("Bad status: %d\n", req->status);
392 if (buffer_size < transfer_size)
393 transfer_size = buffer_size;
395 memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
396 buffer, transfer_size);
398 download_bytes += transfer_size;
400 /* Check if transfer is done */
401 if (download_bytes >= download_size) {
403 * Reset global transfer variable, keep download_bytes because
404 * it will be used in the next possible flashing command
407 req->complete = rx_handler_command;
408 req->length = EP_BUFFER_SIZE;
410 sprintf(response, "OKAY");
411 fastboot_tx_write_str(response);
413 printf("\ndownloading of %d bytes finished\n", download_bytes);
415 req->length = rx_bytes_expected();
416 if (req->length < ep->maxpacket)
417 req->length = ep->maxpacket;
420 if (download_bytes && !(download_bytes % BYTES_PER_DOT)) {
422 if (!(download_bytes % (74 * BYTES_PER_DOT)))
426 usb_ep_queue(ep, req, 0);
429 static void cb_download(struct usb_ep *ep, struct usb_request *req)
431 char *cmd = req->buf;
432 char response[RESPONSE_LEN];
435 download_size = simple_strtoul(cmd, NULL, 16);
438 printf("Starting download of %d bytes\n", download_size);
440 if (0 == download_size) {
441 sprintf(response, "FAILdata invalid size");
442 } else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
444 sprintf(response, "FAILdata too large");
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;
452 fastboot_tx_write_str(response);
455 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
457 char boot_addr_start[12];
458 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
460 puts("Booting kernel..\n");
462 sprintf(boot_addr_start, "0x%lx", load_addr);
463 do_bootm(NULL, 0, 2, bootm_args);
465 /* This only happens if image is somehow faulty so we start over */
466 do_reset(NULL, 0, 0, NULL);
469 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
471 fastboot_func->in_req->complete = do_bootm_on_complete;
472 fastboot_tx_write_str("OKAY");
475 #ifdef CONFIG_FASTBOOT_FLASH
476 static void cb_flash(struct usb_ep *ep, struct usb_request *req)
478 char *cmd = req->buf;
479 char response[RESPONSE_LEN];
483 printf("%s: missing partition name\n", __func__);
484 fastboot_tx_write_str("FAILmissing partition name");
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);
493 fastboot_tx_write_str(response);
497 struct cmd_dispatch_info {
499 void (*cb)(struct usb_ep *ep, struct usb_request *req);
502 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
516 #ifdef CONFIG_FASTBOOT_FLASH
524 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
526 char *cmdbuf = req->buf;
527 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
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;
538 fastboot_tx_write_str("FAILunknown command");
542 if (req->status == 0) {
545 usb_ep_queue(ep, req, 0);