blob: 7a1acb9df02be0d47a7fd492eaa1004d9c45232a [file] [log] [blame]
Sebastian Siewior3aab70a2014-05-05 15:08:10 -05001/*
2 * (C) Copyright 2008 - 2009
3 * Windriver, <www.windriver.com>
4 * Tom Rix <Tom.Rix@windriver.com>
5 *
6 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7 *
8 * Copyright 2014 Linaro, Ltd.
9 * Rob Herring <robh@kernel.org>
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 */
13#include <common.h>
14#include <errno.h>
15#include <malloc.h>
16#include <linux/usb/ch9.h>
17#include <linux/usb/gadget.h>
18#include <linux/usb/composite.h>
19#include <linux/compiler.h>
20#include <version.h>
21#include <g_dnl.h>
22
23#define FASTBOOT_VERSION "0.4"
24
25#define FASTBOOT_INTERFACE_CLASS 0xff
26#define FASTBOOT_INTERFACE_SUB_CLASS 0x42
27#define FASTBOOT_INTERFACE_PROTOCOL 0x03
28
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)
32
33/* The 64 defined bytes plus \0 */
34#define RESPONSE_LEN (64 + 1)
35
36#define EP_BUFFER_SIZE 4096
37
38struct f_fastboot {
39 struct usb_function usb_function;
40
41 /* IN/OUT EP's and correspoinding requests */
42 struct usb_ep *in_ep, *out_ep;
43 struct usb_request *in_req, *out_req;
44};
45
46static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
47{
48 return container_of(f, struct f_fastboot, usb_function);
49}
50
51static struct f_fastboot *fastboot_func;
52static unsigned int download_size;
53static unsigned int download_bytes;
54
55static 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,
61 .bInterval = 0x00,
62};
63
64static 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,
70 .bInterval = 0x00,
71};
72
73static 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,
79 .bInterval = 0x00,
80};
81
82static 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,
91};
92
93static 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,
97 NULL,
98};
99
100/*
101 * static strings, in UTF-8
102 */
103static const char fastboot_name[] = "Android Fastboot";
104
105static struct usb_string fastboot_string_defs[] = {
106 [0].s = fastboot_name,
107 { } /* end of list */
108};
109
110static struct usb_gadget_strings stringtab_fastboot = {
111 .language = 0x0409, /* en-us */
112 .strings = fastboot_string_defs,
113};
114
115static struct usb_gadget_strings *fastboot_strings[] = {
116 &stringtab_fastboot,
117 NULL,
118};
119
120static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
121
122static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
123{
124 int status = req->status;
125 if (!status)
126 return;
127 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
128}
129
130static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
131{
132 int id;
133 struct usb_gadget *gadget = c->cdev->gadget;
134 struct f_fastboot *f_fb = func_to_fastboot(f);
135
136 /* DYNAMIC interface numbers assignments */
137 id = usb_interface_id(c, f);
138 if (id < 0)
139 return id;
140 interface_desc.bInterfaceNumber = id;
141
142 id = usb_string_id(c->cdev);
143 if (id < 0)
144 return id;
145 fastboot_string_defs[0].id = id;
146 interface_desc.iInterface = id;
147
148 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
149 if (!f_fb->in_ep)
150 return -ENODEV;
151 f_fb->in_ep->driver_data = c->cdev;
152
153 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
154 if (!f_fb->out_ep)
155 return -ENODEV;
156 f_fb->out_ep->driver_data = c->cdev;
157
158 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
159
160 return 0;
161}
162
163static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
164{
165 memset(fastboot_func, 0, sizeof(*fastboot_func));
166}
167
168static void fastboot_disable(struct usb_function *f)
169{
170 struct f_fastboot *f_fb = func_to_fastboot(f);
171
172 usb_ep_disable(f_fb->out_ep);
173 usb_ep_disable(f_fb->in_ep);
174
175 if (f_fb->out_req) {
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;
179 }
180 if (f_fb->in_req) {
181 free(f_fb->in_req->buf);
182 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
183 f_fb->in_req = NULL;
184 }
185}
186
187static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
188{
189 struct usb_request *req;
190
191 req = usb_ep_alloc_request(ep, 0);
192 if (!req)
193 return NULL;
194
195 req->length = EP_BUFFER_SIZE;
196 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
197 if (!req->buf) {
198 usb_ep_free_request(ep, req);
199 return NULL;
200 }
201
202 memset(req->buf, 0, req->length);
203 return req;
204}
205
206static int fastboot_set_alt(struct usb_function *f,
207 unsigned interface, unsigned alt)
208{
209 int ret;
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);
213
214 debug("%s: func: %s intf: %d alt: %d\n",
215 __func__, f->name, interface, alt);
216
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);
220 else
221 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
222 if (ret) {
223 puts("failed to enable out ep\n");
224 return ret;
225 }
226
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");
230 ret = -EINVAL;
231 goto err;
232 }
233 f_fb->out_req->complete = rx_handler_command;
234
235 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
236 if (ret) {
237 puts("failed to enable in ep\n");
238 goto err;
239 }
240
241 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
242 if (!f_fb->in_req) {
243 puts("failed alloc req in\n");
244 ret = -EINVAL;
245 goto err;
246 }
247 f_fb->in_req->complete = fastboot_complete;
248
249 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
250 if (ret)
251 goto err;
252
253 return 0;
254err:
255 fastboot_disable(f);
256 return ret;
257}
258
259static int fastboot_add(struct usb_configuration *c)
260{
261 struct f_fastboot *f_fb = fastboot_func;
262 int status;
263
264 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
265
266 if (!f_fb) {
267 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
268 if (!f_fb)
269 return -ENOMEM;
270
271 fastboot_func = f_fb;
272 memset(f_fb, 0, sizeof(*f_fb));
273 }
274
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;
282
283 status = usb_add_function(c, &f_fb->usb_function);
284 if (status) {
285 free(f_fb);
286 fastboot_func = f_fb;
287 }
288
289 return status;
290}
291DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
292
293int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
294{
295 struct usb_request *in_req = fastboot_func->in_req;
296 int ret;
297
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);
301 if (ret)
302 printf("Error %d on queue\n", ret);
303 return 0;
304}
305
306static int fastboot_tx_write_str(const char *buffer)
307{
308 return fastboot_tx_write(buffer, strlen(buffer));
309}
310
311static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
312{
313 do_reset(NULL, 0, 0, NULL);
314}
315
316static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
317{
318 fastboot_func->in_req->complete = compl_do_reset;
319 fastboot_tx_write_str("OKAY");
320}
321
322static int strcmp_l1(const char *s1, const char *s2)
323{
324 if (!s1 || !s2)
325 return -1;
326 return strncmp(s1, s2, strlen(s1));
327}
328
329static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
330{
331 char *cmd = req->buf;
332 char response[RESPONSE_LEN];
333 const char *s;
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200334 size_t chars_left;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500335
336 strcpy(response, "OKAY");
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200337 chars_left = sizeof(response) - strlen(response) - 1;
338
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500339 strsep(&cmd, ":");
340 if (!cmd) {
341 fastboot_tx_write_str("FAILmissing var");
342 return;
343 }
344
345 if (!strcmp_l1("version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200346 strncat(response, FASTBOOT_VERSION, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500347 } else if (!strcmp_l1("bootloader-version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200348 strncat(response, U_BOOT_VERSION, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500349 } else if (!strcmp_l1("downloadsize", cmd)) {
350 char str_num[12];
351
352 sprintf(str_num, "%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200353 strncat(response, str_num, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500354 } else if (!strcmp_l1("serialno", cmd)) {
355 s = getenv("serial#");
356 if (s)
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200357 strncat(response, s, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500358 else
359 strcpy(response, "FAILValue not set");
360 } else {
361 strcpy(response, "FAILVariable not implemented");
362 }
363 fastboot_tx_write_str(response);
364}
365
366static unsigned int rx_bytes_expected(void)
367{
368 int rx_remain = download_size - download_bytes;
369 if (rx_remain < 0)
370 return 0;
371 if (rx_remain > EP_BUFFER_SIZE)
372 return EP_BUFFER_SIZE;
373 return rx_remain;
374}
375
376#define BYTES_PER_DOT 0x20000
377static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
378{
379 char response[RESPONSE_LEN];
380 unsigned int transfer_size = download_size - download_bytes;
381 const unsigned char *buffer = req->buf;
382 unsigned int buffer_size = req->actual;
383
384 if (req->status != 0) {
385 printf("Bad status: %d\n", req->status);
386 return;
387 }
388
389 if (buffer_size < transfer_size)
390 transfer_size = buffer_size;
391
392 memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
393 buffer, transfer_size);
394
395 download_bytes += transfer_size;
396
397 /* Check if transfer is done */
398 if (download_bytes >= download_size) {
399 /*
400 * Reset global transfer variable, keep download_bytes because
401 * it will be used in the next possible flashing command
402 */
403 download_size = 0;
404 req->complete = rx_handler_command;
405 req->length = EP_BUFFER_SIZE;
406
407 sprintf(response, "OKAY");
408 fastboot_tx_write_str(response);
409
410 printf("\ndownloading of %d bytes finished\n", download_bytes);
411 } else {
412 req->length = rx_bytes_expected();
413 if (req->length < ep->maxpacket)
414 req->length = ep->maxpacket;
415 }
416
417 if (download_bytes && !(download_bytes % BYTES_PER_DOT)) {
418 putc('.');
419 if (!(download_bytes % (74 * BYTES_PER_DOT)))
420 putc('\n');
421 }
422 req->actual = 0;
423 usb_ep_queue(ep, req, 0);
424}
425
426static void cb_download(struct usb_ep *ep, struct usb_request *req)
427{
428 char *cmd = req->buf;
429 char response[RESPONSE_LEN];
430
431 strsep(&cmd, ":");
432 download_size = simple_strtoul(cmd, NULL, 16);
433 download_bytes = 0;
434
435 printf("Starting download of %d bytes\n", download_size);
436
437 if (0 == download_size) {
438 sprintf(response, "FAILdata invalid size");
439 } else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
440 download_size = 0;
441 sprintf(response, "FAILdata too large");
442 } else {
443 sprintf(response, "DATA%08x", download_size);
444 req->complete = rx_handler_dl_image;
445 req->length = rx_bytes_expected();
446 if (req->length < ep->maxpacket)
447 req->length = ep->maxpacket;
448 }
449 fastboot_tx_write_str(response);
450}
451
452static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
453{
454 char boot_addr_start[12];
455 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
456
457 puts("Booting kernel..\n");
458
459 sprintf(boot_addr_start, "0x%lx", load_addr);
460 do_bootm(NULL, 0, 2, bootm_args);
461
462 /* This only happens if image is somehow faulty so we start over */
463 do_reset(NULL, 0, 0, NULL);
464}
465
466static void cb_boot(struct usb_ep *ep, struct usb_request *req)
467{
468 fastboot_func->in_req->complete = do_bootm_on_complete;
469 fastboot_tx_write_str("OKAY");
470}
471
472struct cmd_dispatch_info {
473 char *cmd;
474 void (*cb)(struct usb_ep *ep, struct usb_request *req);
475};
476
477static const struct cmd_dispatch_info cmd_dispatch_info[] = {
478 {
479 .cmd = "reboot",
480 .cb = cb_reboot,
481 }, {
482 .cmd = "getvar:",
483 .cb = cb_getvar,
484 }, {
485 .cmd = "download:",
486 .cb = cb_download,
487 }, {
488 .cmd = "boot",
489 .cb = cb_boot,
490 },
491};
492
493static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
494{
495 char *cmdbuf = req->buf;
496 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
497 int i;
498
499 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
500 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
501 func_cb = cmd_dispatch_info[i].cb;
502 break;
503 }
504 }
505
506 if (!func_cb)
507 fastboot_tx_write_str("FAILunknown command");
508 else
509 func_cb(ep, req);
510
511 if (req->status == 0) {
512 *cmdbuf = '\0';
513 req->actual = 0;
514 usb_ep_queue(ep, req, 0);
515 }
516}