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+
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_DEV
27 #define FASTBOOT_VERSION "0.4"
29 #define FASTBOOT_INTERFACE_CLASS 0xff
30 #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
31 #define FASTBOOT_INTERFACE_PROTOCOL 0x03
33 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
34 #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
35 #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
37 /* The 64 defined bytes plus \0 */
38 #define RESPONSE_LEN (64 + 1)
40 #define EP_BUFFER_SIZE 4096
43 struct usb_function usb_function
;
45 /* IN/OUT EP's and corresponding requests */
46 struct usb_ep
*in_ep
, *out_ep
;
47 struct usb_request
*in_req
, *out_req
;
50 static inline struct f_fastboot
*func_to_fastboot(struct usb_function
*f
)
52 return container_of(f
, struct f_fastboot
, usb_function
);
55 static struct f_fastboot
*fastboot_func
;
56 static unsigned int download_size
;
57 static unsigned int download_bytes
;
59 static struct usb_endpoint_descriptor fs_ep_in
= {
60 .bLength
= USB_DT_ENDPOINT_SIZE
,
61 .bDescriptorType
= USB_DT_ENDPOINT
,
62 .bEndpointAddress
= USB_DIR_IN
,
63 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
64 .wMaxPacketSize
= TX_ENDPOINT_MAXIMUM_PACKET_SIZE
,
68 static struct usb_endpoint_descriptor fs_ep_out
= {
69 .bLength
= USB_DT_ENDPOINT_SIZE
,
70 .bDescriptorType
= USB_DT_ENDPOINT
,
71 .bEndpointAddress
= USB_DIR_OUT
,
72 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
73 .wMaxPacketSize
= RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1
,
77 static struct usb_endpoint_descriptor hs_ep_out
= {
78 .bLength
= USB_DT_ENDPOINT_SIZE
,
79 .bDescriptorType
= USB_DT_ENDPOINT
,
80 .bEndpointAddress
= USB_DIR_OUT
,
81 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
82 .wMaxPacketSize
= RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0
,
86 static struct usb_interface_descriptor interface_desc
= {
87 .bLength
= USB_DT_INTERFACE_SIZE
,
88 .bDescriptorType
= USB_DT_INTERFACE
,
89 .bInterfaceNumber
= 0x00,
90 .bAlternateSetting
= 0x00,
91 .bNumEndpoints
= 0x02,
92 .bInterfaceClass
= FASTBOOT_INTERFACE_CLASS
,
93 .bInterfaceSubClass
= FASTBOOT_INTERFACE_SUB_CLASS
,
94 .bInterfaceProtocol
= FASTBOOT_INTERFACE_PROTOCOL
,
97 static struct usb_descriptor_header
*fb_runtime_descs
[] = {
98 (struct usb_descriptor_header
*)&interface_desc
,
99 (struct usb_descriptor_header
*)&fs_ep_in
,
100 (struct usb_descriptor_header
*)&hs_ep_out
,
105 * static strings, in UTF-8
107 static const char fastboot_name
[] = "Android Fastboot";
109 static struct usb_string fastboot_string_defs
[] = {
110 [0].s
= fastboot_name
,
111 { } /* end of list */
114 static struct usb_gadget_strings stringtab_fastboot
= {
115 .language
= 0x0409, /* en-us */
116 .strings
= fastboot_string_defs
,
119 static struct usb_gadget_strings
*fastboot_strings
[] = {
124 static void rx_handler_command(struct usb_ep
*ep
, struct usb_request
*req
);
126 static void fastboot_complete(struct usb_ep
*ep
, struct usb_request
*req
)
128 int status
= req
->status
;
131 printf("status: %d ep '%s' trans: %d\n", status
, ep
->name
, req
->actual
);
134 static int fastboot_bind(struct usb_configuration
*c
, struct usb_function
*f
)
137 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
138 struct f_fastboot
*f_fb
= func_to_fastboot(f
);
140 /* DYNAMIC interface numbers assignments */
141 id
= usb_interface_id(c
, f
);
144 interface_desc
.bInterfaceNumber
= id
;
146 id
= usb_string_id(c
->cdev
);
149 fastboot_string_defs
[0].id
= id
;
150 interface_desc
.iInterface
= id
;
152 f_fb
->in_ep
= usb_ep_autoconfig(gadget
, &fs_ep_in
);
155 f_fb
->in_ep
->driver_data
= c
->cdev
;
157 f_fb
->out_ep
= usb_ep_autoconfig(gadget
, &fs_ep_out
);
160 f_fb
->out_ep
->driver_data
= c
->cdev
;
162 hs_ep_out
.bEndpointAddress
= fs_ep_out
.bEndpointAddress
;
167 static void fastboot_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
169 memset(fastboot_func
, 0, sizeof(*fastboot_func
));
172 static void fastboot_disable(struct usb_function
*f
)
174 struct f_fastboot
*f_fb
= func_to_fastboot(f
);
176 usb_ep_disable(f_fb
->out_ep
);
177 usb_ep_disable(f_fb
->in_ep
);
180 free(f_fb
->out_req
->buf
);
181 usb_ep_free_request(f_fb
->out_ep
, f_fb
->out_req
);
182 f_fb
->out_req
= NULL
;
185 free(f_fb
->in_req
->buf
);
186 usb_ep_free_request(f_fb
->in_ep
, f_fb
->in_req
);
191 static struct usb_request
*fastboot_start_ep(struct usb_ep
*ep
)
193 struct usb_request
*req
;
195 req
= usb_ep_alloc_request(ep
, 0);
199 req
->length
= EP_BUFFER_SIZE
;
200 req
->buf
= memalign(CONFIG_SYS_CACHELINE_SIZE
, EP_BUFFER_SIZE
);
202 usb_ep_free_request(ep
, req
);
206 memset(req
->buf
, 0, req
->length
);
210 static int fastboot_set_alt(struct usb_function
*f
,
211 unsigned interface
, unsigned alt
)
214 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
215 struct usb_gadget
*gadget
= cdev
->gadget
;
216 struct f_fastboot
*f_fb
= func_to_fastboot(f
);
218 debug("%s: func: %s intf: %d alt: %d\n",
219 __func__
, f
->name
, interface
, alt
);
221 /* make sure we don't enable the ep twice */
222 if (gadget
->speed
== USB_SPEED_HIGH
)
223 ret
= usb_ep_enable(f_fb
->out_ep
, &hs_ep_out
);
225 ret
= usb_ep_enable(f_fb
->out_ep
, &fs_ep_out
);
227 puts("failed to enable out ep\n");
231 f_fb
->out_req
= fastboot_start_ep(f_fb
->out_ep
);
232 if (!f_fb
->out_req
) {
233 puts("failed to alloc out req\n");
237 f_fb
->out_req
->complete
= rx_handler_command
;
239 ret
= usb_ep_enable(f_fb
->in_ep
, &fs_ep_in
);
241 puts("failed to enable in ep\n");
245 f_fb
->in_req
= fastboot_start_ep(f_fb
->in_ep
);
247 puts("failed alloc req in\n");
251 f_fb
->in_req
->complete
= fastboot_complete
;
253 ret
= usb_ep_queue(f_fb
->out_ep
, f_fb
->out_req
, 0);
263 static int fastboot_add(struct usb_configuration
*c
)
265 struct f_fastboot
*f_fb
= fastboot_func
;
268 debug("%s: cdev: 0x%p\n", __func__
, c
->cdev
);
271 f_fb
= memalign(CONFIG_SYS_CACHELINE_SIZE
, sizeof(*f_fb
));
275 fastboot_func
= f_fb
;
276 memset(f_fb
, 0, sizeof(*f_fb
));
279 f_fb
->usb_function
.name
= "f_fastboot";
280 f_fb
->usb_function
.hs_descriptors
= fb_runtime_descs
;
281 f_fb
->usb_function
.bind
= fastboot_bind
;
282 f_fb
->usb_function
.unbind
= fastboot_unbind
;
283 f_fb
->usb_function
.set_alt
= fastboot_set_alt
;
284 f_fb
->usb_function
.disable
= fastboot_disable
;
285 f_fb
->usb_function
.strings
= fastboot_strings
;
287 status
= usb_add_function(c
, &f_fb
->usb_function
);
290 fastboot_func
= f_fb
;
295 DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot
, fastboot_add
);
297 static int fastboot_tx_write(const char *buffer
, unsigned int buffer_size
)
299 struct usb_request
*in_req
= fastboot_func
->in_req
;
302 memcpy(in_req
->buf
, buffer
, buffer_size
);
303 in_req
->length
= buffer_size
;
304 ret
= usb_ep_queue(fastboot_func
->in_ep
, in_req
, 0);
306 printf("Error %d on queue\n", ret
);
310 static int fastboot_tx_write_str(const char *buffer
)
312 return fastboot_tx_write(buffer
, strlen(buffer
));
315 static void compl_do_reset(struct usb_ep
*ep
, struct usb_request
*req
)
317 do_reset(NULL
, 0, 0, NULL
);
320 static void cb_reboot(struct usb_ep
*ep
, struct usb_request
*req
)
322 fastboot_func
->in_req
->complete
= compl_do_reset
;
323 fastboot_tx_write_str("OKAY");
326 static int strcmp_l1(const char *s1
, const char *s2
)
330 return strncmp(s1
, s2
, strlen(s1
));
333 static void cb_getvar(struct usb_ep
*ep
, struct usb_request
*req
)
335 char *cmd
= req
->buf
;
336 char response
[RESPONSE_LEN
];
340 strcpy(response
, "OKAY");
341 chars_left
= sizeof(response
) - strlen(response
) - 1;
345 error("missing variable\n");
346 fastboot_tx_write_str("FAILmissing var");
350 if (!strcmp_l1("version", cmd
)) {
351 strncat(response
, FASTBOOT_VERSION
, chars_left
);
352 } else if (!strcmp_l1("bootloader-version", cmd
)) {
353 strncat(response
, U_BOOT_VERSION
, chars_left
);
354 } else if (!strcmp_l1("downloadsize", cmd
)) {
357 sprintf(str_num
, "%08x", CONFIG_USB_FASTBOOT_BUF_SIZE
);
358 strncat(response
, str_num
, chars_left
);
359 } else if (!strcmp_l1("serialno", cmd
)) {
360 s
= getenv("serial#");
362 strncat(response
, s
, chars_left
);
364 strcpy(response
, "FAILValue not set");
366 error("unknown variable: %s\n", cmd
);
367 strcpy(response
, "FAILVariable not implemented");
369 fastboot_tx_write_str(response
);
372 static unsigned int rx_bytes_expected(void)
374 int rx_remain
= download_size
- download_bytes
;
377 if (rx_remain
> EP_BUFFER_SIZE
)
378 return EP_BUFFER_SIZE
;
382 #define BYTES_PER_DOT 0x20000
383 static void rx_handler_dl_image(struct usb_ep
*ep
, struct usb_request
*req
)
385 char response
[RESPONSE_LEN
];
386 unsigned int transfer_size
= download_size
- download_bytes
;
387 const unsigned char *buffer
= req
->buf
;
388 unsigned int buffer_size
= req
->actual
;
390 if (req
->status
!= 0) {
391 printf("Bad status: %d\n", req
->status
);
395 if (buffer_size
< transfer_size
)
396 transfer_size
= buffer_size
;
398 memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR
+ download_bytes
,
399 buffer
, transfer_size
);
401 download_bytes
+= transfer_size
;
403 /* Check if transfer is done */
404 if (download_bytes
>= download_size
) {
406 * Reset global transfer variable, keep download_bytes because
407 * it will be used in the next possible flashing command
410 req
->complete
= rx_handler_command
;
411 req
->length
= EP_BUFFER_SIZE
;
413 sprintf(response
, "OKAY");
414 fastboot_tx_write_str(response
);
416 printf("\ndownloading of %d bytes finished\n", download_bytes
);
418 req
->length
= rx_bytes_expected();
419 if (req
->length
< ep
->maxpacket
)
420 req
->length
= ep
->maxpacket
;
423 if (download_bytes
&& !(download_bytes
% BYTES_PER_DOT
)) {
425 if (!(download_bytes
% (74 * BYTES_PER_DOT
)))
429 usb_ep_queue(ep
, req
, 0);
432 static void cb_download(struct usb_ep
*ep
, struct usb_request
*req
)
434 char *cmd
= req
->buf
;
435 char response
[RESPONSE_LEN
];
438 download_size
= simple_strtoul(cmd
, NULL
, 16);
441 printf("Starting download of %d bytes\n", download_size
);
443 if (0 == download_size
) {
444 sprintf(response
, "FAILdata invalid size");
445 } else if (download_size
> CONFIG_USB_FASTBOOT_BUF_SIZE
) {
447 sprintf(response
, "FAILdata too large");
449 sprintf(response
, "DATA%08x", download_size
);
450 req
->complete
= rx_handler_dl_image
;
451 req
->length
= rx_bytes_expected();
452 if (req
->length
< ep
->maxpacket
)
453 req
->length
= ep
->maxpacket
;
455 fastboot_tx_write_str(response
);
458 static void do_bootm_on_complete(struct usb_ep
*ep
, struct usb_request
*req
)
460 char boot_addr_start
[12];
461 char *bootm_args
[] = { "bootm", boot_addr_start
, NULL
};
463 puts("Booting kernel..\n");
465 sprintf(boot_addr_start
, "0x%lx", load_addr
);
466 do_bootm(NULL
, 0, 2, bootm_args
);
468 /* This only happens if image is somehow faulty so we start over */
469 do_reset(NULL
, 0, 0, NULL
);
472 static void cb_boot(struct usb_ep
*ep
, struct usb_request
*req
)
474 fastboot_func
->in_req
->complete
= do_bootm_on_complete
;
475 fastboot_tx_write_str("OKAY");
478 #ifdef CONFIG_FASTBOOT_FLASH
479 static void cb_flash(struct usb_ep
*ep
, struct usb_request
*req
)
481 char *cmd
= req
->buf
;
482 char response
[RESPONSE_LEN
];
486 error("missing partition name\n");
487 fastboot_tx_write_str("FAILmissing partition name");
491 strcpy(response
, "FAILno flash device defined");
492 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
493 fb_mmc_flash_write(cmd
, (void *)CONFIG_USB_FASTBOOT_BUF_ADDR
,
494 download_bytes
, response
);
496 fastboot_tx_write_str(response
);
500 struct cmd_dispatch_info
{
502 void (*cb
)(struct usb_ep
*ep
, struct usb_request
*req
);
505 static const struct cmd_dispatch_info cmd_dispatch_info
[] = {
519 #ifdef CONFIG_FASTBOOT_FLASH
527 static void rx_handler_command(struct usb_ep
*ep
, struct usb_request
*req
)
529 char *cmdbuf
= req
->buf
;
530 void (*func_cb
)(struct usb_ep
*ep
, struct usb_request
*req
) = NULL
;
533 for (i
= 0; i
< ARRAY_SIZE(cmd_dispatch_info
); i
++) {
534 if (!strcmp_l1(cmd_dispatch_info
[i
].cmd
, cmdbuf
)) {
535 func_cb
= cmd_dispatch_info
[i
].cb
;
541 error("unknown command: %s\n", cmdbuf
);
542 fastboot_tx_write_str("FAILunknown command");
547 if (req
->status
== 0) {
550 usb_ep_queue(ep
, req
, 0);