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>
23 #define FASTBOOT_VERSION "0.4"
25 #define FASTBOOT_INTERFACE_CLASS 0xff
26 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
27 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
29 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
30 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
31 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
33 /* The 64 defined bytes plus \0 */
34 #define RESPONSE_LEN (64 + 1)
36 #define EP_BUFFER_SIZE 4096
39 struct usb_function usb_function;
41 /* IN/OUT EP's and correspoinding requests */
42 struct usb_ep *in_ep, *out_ep;
43 struct usb_request *in_req, *out_req;
46 static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
48 return container_of(f, struct f_fastboot, usb_function);
51 static struct f_fastboot *fastboot_func;
52 static unsigned int download_size;
53 static unsigned int download_bytes;
55 static struct usb_endpoint_descriptor fs_ep_in = {
56 .bLength = USB_DT_ENDPOINT_SIZE,
57 .bDescriptorType = USB_DT_ENDPOINT,
58 .bEndpointAddress = USB_DIR_IN,
59 .bmAttributes = USB_ENDPOINT_XFER_BULK,
60 .wMaxPacketSize = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
64 static struct usb_endpoint_descriptor fs_ep_out = {
65 .bLength = USB_DT_ENDPOINT_SIZE,
66 .bDescriptorType = USB_DT_ENDPOINT,
67 .bEndpointAddress = USB_DIR_OUT,
68 .bmAttributes = USB_ENDPOINT_XFER_BULK,
69 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
73 static struct usb_endpoint_descriptor hs_ep_out = {
74 .bLength = USB_DT_ENDPOINT_SIZE,
75 .bDescriptorType = USB_DT_ENDPOINT,
76 .bEndpointAddress = USB_DIR_OUT,
77 .bmAttributes = USB_ENDPOINT_XFER_BULK,
78 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
82 static struct usb_interface_descriptor interface_desc = {
83 .bLength = USB_DT_INTERFACE_SIZE,
84 .bDescriptorType = USB_DT_INTERFACE,
85 .bInterfaceNumber = 0x00,
86 .bAlternateSetting = 0x00,
87 .bNumEndpoints = 0x02,
88 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
89 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
90 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
93 static struct usb_descriptor_header *fb_runtime_descs[] = {
94 (struct usb_descriptor_header *)&interface_desc,
95 (struct usb_descriptor_header *)&fs_ep_in,
96 (struct usb_descriptor_header *)&hs_ep_out,
101 * static strings, in UTF-8
103 static const char fastboot_name[] = "Android Fastboot";
105 static struct usb_string fastboot_string_defs[] = {
106 [0].s = fastboot_name,
107 { } /* end of list */
110 static struct usb_gadget_strings stringtab_fastboot = {
111 .language = 0x0409, /* en-us */
112 .strings = fastboot_string_defs,
115 static struct usb_gadget_strings *fastboot_strings[] = {
120 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
122 static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
124 int status = req->status;
127 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
130 static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
133 struct usb_gadget *gadget = c->cdev->gadget;
134 struct f_fastboot *f_fb = func_to_fastboot(f);
136 /* DYNAMIC interface numbers assignments */
137 id = usb_interface_id(c, f);
140 interface_desc.bInterfaceNumber = id;
142 id = usb_string_id(c->cdev);
145 fastboot_string_defs[0].id = id;
146 interface_desc.iInterface = id;
148 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
151 f_fb->in_ep->driver_data = c->cdev;
153 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
156 f_fb->out_ep->driver_data = c->cdev;
158 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
163 static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
165 memset(fastboot_func, 0, sizeof(*fastboot_func));
168 static void fastboot_disable(struct usb_function *f)
170 struct f_fastboot *f_fb = func_to_fastboot(f);
172 usb_ep_disable(f_fb->out_ep);
173 usb_ep_disable(f_fb->in_ep);
176 free(f_fb->out_req->buf);
177 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
178 f_fb->out_req = NULL;
181 free(f_fb->in_req->buf);
182 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
187 static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
189 struct usb_request *req;
191 req = usb_ep_alloc_request(ep, 0);
195 req->length = EP_BUFFER_SIZE;
196 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
198 usb_ep_free_request(ep, req);
202 memset(req->buf, 0, req->length);
206 static int fastboot_set_alt(struct usb_function *f,
207 unsigned interface, unsigned alt)
210 struct usb_composite_dev *cdev = f->config->cdev;
211 struct usb_gadget *gadget = cdev->gadget;
212 struct f_fastboot *f_fb = func_to_fastboot(f);
214 debug("%s: func: %s intf: %d alt: %d\n",
215 __func__, f->name, interface, alt);
217 /* make sure we don't enable the ep twice */
218 if (gadget->speed == USB_SPEED_HIGH)
219 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
221 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
223 puts("failed to enable out ep\n");
227 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
228 if (!f_fb->out_req) {
229 puts("failed to alloc out req\n");
233 f_fb->out_req->complete = rx_handler_command;
235 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
237 puts("failed to enable in ep\n");
241 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
243 puts("failed alloc req in\n");
247 f_fb->in_req->complete = fastboot_complete;
249 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
259 static int fastboot_add(struct usb_configuration *c)
261 struct f_fastboot *f_fb = fastboot_func;
264 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
267 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
271 fastboot_func = f_fb;
272 memset(f_fb, 0, sizeof(*f_fb));
275 f_fb->usb_function.name = "f_fastboot";
276 f_fb->usb_function.hs_descriptors = fb_runtime_descs;
277 f_fb->usb_function.bind = fastboot_bind;
278 f_fb->usb_function.unbind = fastboot_unbind;
279 f_fb->usb_function.set_alt = fastboot_set_alt;
280 f_fb->usb_function.disable = fastboot_disable;
281 f_fb->usb_function.strings = fastboot_strings;
283 status = usb_add_function(c, &f_fb->usb_function);
286 fastboot_func = f_fb;
291 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
293 int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
295 struct usb_request *in_req = fastboot_func->in_req;
298 memcpy(in_req->buf, buffer, buffer_size);
299 in_req->length = buffer_size;
300 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
302 printf("Error %d on queue\n", ret);
306 static int fastboot_tx_write_str(const char *buffer)
308 return fastboot_tx_write(buffer, strlen(buffer));
311 static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
313 do_reset(NULL, 0, 0, NULL);
316 static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
318 fastboot_func->in_req->complete = compl_do_reset;
319 fastboot_tx_write_str("OKAY");
322 static int strcmp_l1(const char *s1, const char *s2)
326 return strncmp(s1, s2, strlen(s1));
329 static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
331 char *cmd = req->buf;
332 char response[RESPONSE_LEN];
335 strcpy(response, "OKAY");
338 fastboot_tx_write_str("FAILmissing var");
342 if (!strcmp_l1("version", cmd)) {
343 strncat(response, FASTBOOT_VERSION, sizeof(response));
344 } else if (!strcmp_l1("bootloader-version", cmd)) {
345 strncat(response, U_BOOT_VERSION, sizeof(response));
346 } else if (!strcmp_l1("downloadsize", cmd)) {
349 sprintf(str_num, "%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
350 strncat(response, str_num, sizeof(response));
351 } else if (!strcmp_l1("serialno", cmd)) {
352 s = getenv("serial#");
354 strncat(response, s, sizeof(response));
356 strcpy(response, "FAILValue not set");
358 strcpy(response, "FAILVariable not implemented");
360 fastboot_tx_write_str(response);
363 static unsigned int rx_bytes_expected(void)
365 int rx_remain = download_size - download_bytes;
368 if (rx_remain > EP_BUFFER_SIZE)
369 return EP_BUFFER_SIZE;
373 #define BYTES_PER_DOT 0x20000
374 static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
376 char response[RESPONSE_LEN];
377 unsigned int transfer_size = download_size - download_bytes;
378 const unsigned char *buffer = req->buf;
379 unsigned int buffer_size = req->actual;
381 if (req->status != 0) {
382 printf("Bad status: %d\n", req->status);
386 if (buffer_size < transfer_size)
387 transfer_size = buffer_size;
389 memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
390 buffer, transfer_size);
392 download_bytes += transfer_size;
394 /* Check if transfer is done */
395 if (download_bytes >= download_size) {
397 * Reset global transfer variable, keep download_bytes because
398 * it will be used in the next possible flashing command
401 req->complete = rx_handler_command;
402 req->length = EP_BUFFER_SIZE;
404 sprintf(response, "OKAY");
405 fastboot_tx_write_str(response);
407 printf("\ndownloading of %d bytes finished\n", download_bytes);
409 req->length = rx_bytes_expected();
410 if (req->length < ep->maxpacket)
411 req->length = ep->maxpacket;
414 if (download_bytes && !(download_bytes % BYTES_PER_DOT)) {
416 if (!(download_bytes % (74 * BYTES_PER_DOT)))
420 usb_ep_queue(ep, req, 0);
423 static void cb_download(struct usb_ep *ep, struct usb_request *req)
425 char *cmd = req->buf;
426 char response[RESPONSE_LEN];
429 download_size = simple_strtoul(cmd, NULL, 16);
432 printf("Starting download of %d bytes\n", download_size);
434 if (0 == download_size) {
435 sprintf(response, "FAILdata invalid size");
436 } else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
438 sprintf(response, "FAILdata too large");
440 sprintf(response, "DATA%08x", download_size);
441 req->complete = rx_handler_dl_image;
442 req->length = rx_bytes_expected();
443 if (req->length < ep->maxpacket)
444 req->length = ep->maxpacket;
446 fastboot_tx_write_str(response);
449 static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
451 char boot_addr_start[12];
452 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
454 puts("Booting kernel..\n");
456 sprintf(boot_addr_start, "0x%lx", load_addr);
457 do_bootm(NULL, 0, 2, bootm_args);
459 /* This only happens if image is somehow faulty so we start over */
460 do_reset(NULL, 0, 0, NULL);
463 static void cb_boot(struct usb_ep *ep, struct usb_request *req)
465 fastboot_func->in_req->complete = do_bootm_on_complete;
466 fastboot_tx_write_str("OKAY");
469 struct cmd_dispatch_info {
471 void (*cb)(struct usb_ep *ep, struct usb_request *req);
474 static const struct cmd_dispatch_info cmd_dispatch_info[] = {
490 static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
492 char *cmdbuf = req->buf;
493 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
496 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
497 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
498 func_cb = cmd_dispatch_info[i].cb;
504 fastboot_tx_write_str("FAILunknown command");
508 if (req->status == 0) {
511 usb_ep_queue(ep, req, 0);