blob: 751ec9e010795079a0d8c601026b0c7071f7a2fa [file] [log] [blame]
Sebastian Siewior9d4471e2014-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 */
Steve Rae56ad7922014-08-26 11:47:29 -070013#include <config.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050014#include <common.h>
15#include <errno.h>
16#include <malloc.h>
17#include <linux/usb/ch9.h>
18#include <linux/usb/gadget.h>
19#include <linux/usb/composite.h>
20#include <linux/compiler.h>
21#include <version.h>
22#include <g_dnl.h>
Steve Raebfb9ba42014-08-26 11:47:28 -070023#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
24#include <fb_mmc.h>
25#endif
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050026
27#define FASTBOOT_VERSION "0.4"
28
29#define FASTBOOT_INTERFACE_CLASS 0xff
30#define FASTBOOT_INTERFACE_SUB_CLASS 0x42
31#define FASTBOOT_INTERFACE_PROTOCOL 0x03
32
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)
36
37/* The 64 defined bytes plus \0 */
38#define RESPONSE_LEN (64 + 1)
39
40#define EP_BUFFER_SIZE 4096
41
42struct f_fastboot {
43 struct usb_function usb_function;
44
Steve Rae56ad7922014-08-26 11:47:29 -070045 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050046 struct usb_ep *in_ep, *out_ep;
47 struct usb_request *in_req, *out_req;
48};
49
50static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
51{
52 return container_of(f, struct f_fastboot, usb_function);
53}
54
55static struct f_fastboot *fastboot_func;
56static unsigned int download_size;
57static unsigned int download_bytes;
Dileep Katta50b4c172015-02-17 02:02:36 +053058static bool is_high_speed;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050059
60static struct usb_endpoint_descriptor fs_ep_in = {
61 .bLength = USB_DT_ENDPOINT_SIZE,
62 .bDescriptorType = USB_DT_ENDPOINT,
63 .bEndpointAddress = USB_DIR_IN,
64 .bmAttributes = USB_ENDPOINT_XFER_BULK,
65 .wMaxPacketSize = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
66 .bInterval = 0x00,
67};
68
69static struct usb_endpoint_descriptor fs_ep_out = {
70 .bLength = USB_DT_ENDPOINT_SIZE,
71 .bDescriptorType = USB_DT_ENDPOINT,
72 .bEndpointAddress = USB_DIR_OUT,
73 .bmAttributes = USB_ENDPOINT_XFER_BULK,
74 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
75 .bInterval = 0x00,
76};
77
78static struct usb_endpoint_descriptor hs_ep_out = {
79 .bLength = USB_DT_ENDPOINT_SIZE,
80 .bDescriptorType = USB_DT_ENDPOINT,
81 .bEndpointAddress = USB_DIR_OUT,
82 .bmAttributes = USB_ENDPOINT_XFER_BULK,
83 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
84 .bInterval = 0x00,
85};
86
87static struct usb_interface_descriptor interface_desc = {
88 .bLength = USB_DT_INTERFACE_SIZE,
89 .bDescriptorType = USB_DT_INTERFACE,
90 .bInterfaceNumber = 0x00,
91 .bAlternateSetting = 0x00,
92 .bNumEndpoints = 0x02,
93 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
94 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
95 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
96};
97
98static struct usb_descriptor_header *fb_runtime_descs[] = {
99 (struct usb_descriptor_header *)&interface_desc,
100 (struct usb_descriptor_header *)&fs_ep_in,
101 (struct usb_descriptor_header *)&hs_ep_out,
102 NULL,
103};
104
105/*
106 * static strings, in UTF-8
107 */
108static const char fastboot_name[] = "Android Fastboot";
109
110static struct usb_string fastboot_string_defs[] = {
111 [0].s = fastboot_name,
112 { } /* end of list */
113};
114
115static struct usb_gadget_strings stringtab_fastboot = {
116 .language = 0x0409, /* en-us */
117 .strings = fastboot_string_defs,
118};
119
120static struct usb_gadget_strings *fastboot_strings[] = {
121 &stringtab_fastboot,
122 NULL,
123};
124
125static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
126
127static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
128{
129 int status = req->status;
130 if (!status)
131 return;
132 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
133}
134
135static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
136{
137 int id;
138 struct usb_gadget *gadget = c->cdev->gadget;
139 struct f_fastboot *f_fb = func_to_fastboot(f);
Dileep Katta447c9752015-02-13 14:33:43 +0800140 const char *s;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500141
142 /* DYNAMIC interface numbers assignments */
143 id = usb_interface_id(c, f);
144 if (id < 0)
145 return id;
146 interface_desc.bInterfaceNumber = id;
147
148 id = usb_string_id(c->cdev);
149 if (id < 0)
150 return id;
151 fastboot_string_defs[0].id = id;
152 interface_desc.iInterface = id;
153
154 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
155 if (!f_fb->in_ep)
156 return -ENODEV;
157 f_fb->in_ep->driver_data = c->cdev;
158
159 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
160 if (!f_fb->out_ep)
161 return -ENODEV;
162 f_fb->out_ep->driver_data = c->cdev;
163
164 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
165
Dileep Katta447c9752015-02-13 14:33:43 +0800166 s = getenv("serial#");
167 if (s)
168 g_dnl_set_serialnumber((char *)s);
169
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500170 return 0;
171}
172
173static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
174{
175 memset(fastboot_func, 0, sizeof(*fastboot_func));
176}
177
178static void fastboot_disable(struct usb_function *f)
179{
180 struct f_fastboot *f_fb = func_to_fastboot(f);
181
182 usb_ep_disable(f_fb->out_ep);
183 usb_ep_disable(f_fb->in_ep);
184
185 if (f_fb->out_req) {
186 free(f_fb->out_req->buf);
187 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
188 f_fb->out_req = NULL;
189 }
190 if (f_fb->in_req) {
191 free(f_fb->in_req->buf);
192 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
193 f_fb->in_req = NULL;
194 }
195}
196
197static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
198{
199 struct usb_request *req;
200
201 req = usb_ep_alloc_request(ep, 0);
202 if (!req)
203 return NULL;
204
205 req->length = EP_BUFFER_SIZE;
206 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
207 if (!req->buf) {
208 usb_ep_free_request(ep, req);
209 return NULL;
210 }
211
212 memset(req->buf, 0, req->length);
213 return req;
214}
215
216static int fastboot_set_alt(struct usb_function *f,
217 unsigned interface, unsigned alt)
218{
219 int ret;
220 struct usb_composite_dev *cdev = f->config->cdev;
221 struct usb_gadget *gadget = cdev->gadget;
222 struct f_fastboot *f_fb = func_to_fastboot(f);
223
224 debug("%s: func: %s intf: %d alt: %d\n",
225 __func__, f->name, interface, alt);
226
227 /* make sure we don't enable the ep twice */
Dileep Katta50b4c172015-02-17 02:02:36 +0530228 if (gadget->speed == USB_SPEED_HIGH) {
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500229 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
Dileep Katta50b4c172015-02-17 02:02:36 +0530230 is_high_speed = true;
231 } else {
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500232 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
Dileep Katta50b4c172015-02-17 02:02:36 +0530233 is_high_speed = false;
234 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500235 if (ret) {
236 puts("failed to enable out ep\n");
237 return ret;
238 }
239
240 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
241 if (!f_fb->out_req) {
242 puts("failed to alloc out req\n");
243 ret = -EINVAL;
244 goto err;
245 }
246 f_fb->out_req->complete = rx_handler_command;
247
248 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
249 if (ret) {
250 puts("failed to enable in ep\n");
251 goto err;
252 }
253
254 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
255 if (!f_fb->in_req) {
256 puts("failed alloc req in\n");
257 ret = -EINVAL;
258 goto err;
259 }
260 f_fb->in_req->complete = fastboot_complete;
261
262 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
263 if (ret)
264 goto err;
265
266 return 0;
267err:
268 fastboot_disable(f);
269 return ret;
270}
271
272static int fastboot_add(struct usb_configuration *c)
273{
274 struct f_fastboot *f_fb = fastboot_func;
275 int status;
276
277 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
278
279 if (!f_fb) {
280 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
281 if (!f_fb)
282 return -ENOMEM;
283
284 fastboot_func = f_fb;
285 memset(f_fb, 0, sizeof(*f_fb));
286 }
287
288 f_fb->usb_function.name = "f_fastboot";
289 f_fb->usb_function.hs_descriptors = fb_runtime_descs;
290 f_fb->usb_function.bind = fastboot_bind;
291 f_fb->usb_function.unbind = fastboot_unbind;
292 f_fb->usb_function.set_alt = fastboot_set_alt;
293 f_fb->usb_function.disable = fastboot_disable;
294 f_fb->usb_function.strings = fastboot_strings;
295
296 status = usb_add_function(c, &f_fb->usb_function);
297 if (status) {
298 free(f_fb);
299 fastboot_func = f_fb;
300 }
301
302 return status;
303}
304DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
305
Steve Rae56ad7922014-08-26 11:47:29 -0700306static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500307{
308 struct usb_request *in_req = fastboot_func->in_req;
309 int ret;
310
311 memcpy(in_req->buf, buffer, buffer_size);
312 in_req->length = buffer_size;
313 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
314 if (ret)
315 printf("Error %d on queue\n", ret);
316 return 0;
317}
318
319static int fastboot_tx_write_str(const char *buffer)
320{
321 return fastboot_tx_write(buffer, strlen(buffer));
322}
323
324static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
325{
326 do_reset(NULL, 0, 0, NULL);
327}
328
329static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
330{
331 fastboot_func->in_req->complete = compl_do_reset;
332 fastboot_tx_write_str("OKAY");
333}
334
335static int strcmp_l1(const char *s1, const char *s2)
336{
337 if (!s1 || !s2)
338 return -1;
339 return strncmp(s1, s2, strlen(s1));
340}
341
342static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
343{
344 char *cmd = req->buf;
345 char response[RESPONSE_LEN];
346 const char *s;
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200347 size_t chars_left;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500348
349 strcpy(response, "OKAY");
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200350 chars_left = sizeof(response) - strlen(response) - 1;
351
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500352 strsep(&cmd, ":");
353 if (!cmd) {
Steve Rae56ad7922014-08-26 11:47:29 -0700354 error("missing variable\n");
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500355 fastboot_tx_write_str("FAILmissing var");
356 return;
357 }
358
359 if (!strcmp_l1("version", cmd)) {
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200360 strncat(response, FASTBOOT_VERSION, chars_left);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500361 } else if (!strcmp_l1("bootloader-version", cmd)) {
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200362 strncat(response, U_BOOT_VERSION, chars_left);
Eric Nelson70567d9a2014-09-30 12:05:40 -0700363 } else if (!strcmp_l1("downloadsize", cmd) ||
364 !strcmp_l1("max-download-size", cmd)) {
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500365 char str_num[12];
366
Eric Nelson33004ab2014-09-30 12:05:41 -0700367 sprintf(str_num, "0x%08x", CONFIG_USB_FASTBOOT_BUF_SIZE);
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200368 strncat(response, str_num, chars_left);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500369 } else if (!strcmp_l1("serialno", cmd)) {
370 s = getenv("serial#");
371 if (s)
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200372 strncat(response, s, chars_left);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500373 else
374 strcpy(response, "FAILValue not set");
375 } else {
Steve Rae56ad7922014-08-26 11:47:29 -0700376 error("unknown variable: %s\n", cmd);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500377 strcpy(response, "FAILVariable not implemented");
378 }
379 fastboot_tx_write_str(response);
380}
381
Dileep Katta50b4c172015-02-17 02:02:36 +0530382static unsigned int rx_bytes_expected(unsigned int maxpacket)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500383{
384 int rx_remain = download_size - download_bytes;
Dileep Katta50b4c172015-02-17 02:02:36 +0530385 int rem = 0;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500386 if (rx_remain < 0)
387 return 0;
388 if (rx_remain > EP_BUFFER_SIZE)
389 return EP_BUFFER_SIZE;
Dileep Katta50b4c172015-02-17 02:02:36 +0530390 if (rx_remain < maxpacket) {
391 rx_remain = maxpacket;
392 } else if (rx_remain % maxpacket != 0) {
393 rem = rx_remain % maxpacket;
394 rx_remain = rx_remain + (maxpacket - rem);
395 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500396 return rx_remain;
397}
398
399#define BYTES_PER_DOT 0x20000
400static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
401{
402 char response[RESPONSE_LEN];
403 unsigned int transfer_size = download_size - download_bytes;
404 const unsigned char *buffer = req->buf;
405 unsigned int buffer_size = req->actual;
Bo Shenc65503a2014-09-19 14:15:12 +0800406 unsigned int pre_dot_num, now_dot_num;
Dileep Katta50b4c172015-02-17 02:02:36 +0530407 unsigned int max;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500408
409 if (req->status != 0) {
410 printf("Bad status: %d\n", req->status);
411 return;
412 }
413
414 if (buffer_size < transfer_size)
415 transfer_size = buffer_size;
416
417 memcpy((void *)CONFIG_USB_FASTBOOT_BUF_ADDR + download_bytes,
418 buffer, transfer_size);
419
Bo Shenc65503a2014-09-19 14:15:12 +0800420 pre_dot_num = download_bytes / BYTES_PER_DOT;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500421 download_bytes += transfer_size;
Bo Shenc65503a2014-09-19 14:15:12 +0800422 now_dot_num = download_bytes / BYTES_PER_DOT;
423
424 if (pre_dot_num != now_dot_num) {
425 putc('.');
426 if (!(now_dot_num % 74))
427 putc('\n');
428 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500429
430 /* Check if transfer is done */
431 if (download_bytes >= download_size) {
432 /*
433 * Reset global transfer variable, keep download_bytes because
434 * it will be used in the next possible flashing command
435 */
436 download_size = 0;
437 req->complete = rx_handler_command;
438 req->length = EP_BUFFER_SIZE;
439
440 sprintf(response, "OKAY");
441 fastboot_tx_write_str(response);
442
443 printf("\ndownloading of %d bytes finished\n", download_bytes);
444 } else {
Dileep Katta50b4c172015-02-17 02:02:36 +0530445 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
446 fs_ep_out.wMaxPacketSize;
447 req->length = rx_bytes_expected(max);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500448 if (req->length < ep->maxpacket)
449 req->length = ep->maxpacket;
450 }
451
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500452 req->actual = 0;
453 usb_ep_queue(ep, req, 0);
454}
455
456static void cb_download(struct usb_ep *ep, struct usb_request *req)
457{
458 char *cmd = req->buf;
459 char response[RESPONSE_LEN];
Dileep Katta50b4c172015-02-17 02:02:36 +0530460 unsigned int max;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500461
462 strsep(&cmd, ":");
463 download_size = simple_strtoul(cmd, NULL, 16);
464 download_bytes = 0;
465
466 printf("Starting download of %d bytes\n", download_size);
467
468 if (0 == download_size) {
469 sprintf(response, "FAILdata invalid size");
470 } else if (download_size > CONFIG_USB_FASTBOOT_BUF_SIZE) {
471 download_size = 0;
472 sprintf(response, "FAILdata too large");
473 } else {
474 sprintf(response, "DATA%08x", download_size);
475 req->complete = rx_handler_dl_image;
Dileep Katta50b4c172015-02-17 02:02:36 +0530476 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
477 fs_ep_out.wMaxPacketSize;
478 req->length = rx_bytes_expected(max);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500479 if (req->length < ep->maxpacket)
480 req->length = ep->maxpacket;
481 }
482 fastboot_tx_write_str(response);
483}
484
485static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
486{
487 char boot_addr_start[12];
488 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
489
490 puts("Booting kernel..\n");
491
492 sprintf(boot_addr_start, "0x%lx", load_addr);
493 do_bootm(NULL, 0, 2, bootm_args);
494
495 /* This only happens if image is somehow faulty so we start over */
496 do_reset(NULL, 0, 0, NULL);
497}
498
499static void cb_boot(struct usb_ep *ep, struct usb_request *req)
500{
501 fastboot_func->in_req->complete = do_bootm_on_complete;
502 fastboot_tx_write_str("OKAY");
503}
504
Rob Herring5aba3982014-12-10 14:43:04 -0600505static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
506{
507 g_dnl_trigger_detach();
508}
509
510static void cb_continue(struct usb_ep *ep, struct usb_request *req)
511{
512 fastboot_func->in_req->complete = do_exit_on_complete;
513 fastboot_tx_write_str("OKAY");
514}
515
Steve Raebfb9ba42014-08-26 11:47:28 -0700516#ifdef CONFIG_FASTBOOT_FLASH
517static void cb_flash(struct usb_ep *ep, struct usb_request *req)
518{
519 char *cmd = req->buf;
520 char response[RESPONSE_LEN];
521
522 strsep(&cmd, ":");
523 if (!cmd) {
Steve Rae56ad7922014-08-26 11:47:29 -0700524 error("missing partition name\n");
Steve Raebfb9ba42014-08-26 11:47:28 -0700525 fastboot_tx_write_str("FAILmissing partition name");
526 return;
527 }
528
529 strcpy(response, "FAILno flash device defined");
530#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
531 fb_mmc_flash_write(cmd, (void *)CONFIG_USB_FASTBOOT_BUF_ADDR,
532 download_bytes, response);
533#endif
534 fastboot_tx_write_str(response);
535}
536#endif
537
Michael Scott7e23abf2015-01-26 15:49:00 -0600538static void cb_oem(struct usb_ep *ep, struct usb_request *req)
539{
540 char *cmd = req->buf;
Rob Herring13047e52015-01-26 15:49:01 -0600541#ifdef CONFIG_FASTBOOT_FLASH
542 if (strncmp("format", cmd + 4, 6) == 0) {
543 char cmdbuf[32];
544 sprintf(cmdbuf, "gpt write mmc %x $partitions",
545 CONFIG_FASTBOOT_FLASH_MMC_DEV);
546 if (run_command(cmdbuf, 0))
547 fastboot_tx_write_str("FAIL");
548 else
549 fastboot_tx_write_str("OKAY");
550 } else
551#endif
Michael Scott7e23abf2015-01-26 15:49:00 -0600552 if (strncmp("unlock", cmd + 4, 8) == 0) {
553 fastboot_tx_write_str("FAILnot implemented");
554 }
555 else {
556 fastboot_tx_write_str("FAILunknown oem command");
557 }
558}
559
Dileep Kattab1228202015-02-17 18:48:23 +0530560#ifdef CONFIG_FASTBOOT_FLASH
561static void cb_erase(struct usb_ep *ep, struct usb_request *req)
562{
563 char *cmd = req->buf;
564 char response[RESPONSE_LEN];
565
566 strsep(&cmd, ":");
567 if (!cmd) {
568 error("missing partition name");
569 fastboot_tx_write_str("FAILmissing partition name");
570 return;
571 }
572
573 strcpy(response, "FAILno flash device defined");
574
575#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
576 fb_mmc_erase(cmd, response);
577#endif
578 fastboot_tx_write_str(response);
579}
580#endif
581
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500582struct cmd_dispatch_info {
583 char *cmd;
584 void (*cb)(struct usb_ep *ep, struct usb_request *req);
585};
586
587static const struct cmd_dispatch_info cmd_dispatch_info[] = {
588 {
589 .cmd = "reboot",
590 .cb = cb_reboot,
591 }, {
592 .cmd = "getvar:",
593 .cb = cb_getvar,
594 }, {
595 .cmd = "download:",
596 .cb = cb_download,
597 }, {
598 .cmd = "boot",
599 .cb = cb_boot,
Rob Herring5aba3982014-12-10 14:43:04 -0600600 }, {
601 .cmd = "continue",
602 .cb = cb_continue,
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500603 },
Steve Raebfb9ba42014-08-26 11:47:28 -0700604#ifdef CONFIG_FASTBOOT_FLASH
605 {
606 .cmd = "flash",
607 .cb = cb_flash,
Dileep Kattab1228202015-02-17 18:48:23 +0530608 }, {
609 .cmd = "erase",
610 .cb = cb_erase,
Steve Raebfb9ba42014-08-26 11:47:28 -0700611 },
612#endif
Michael Scott7e23abf2015-01-26 15:49:00 -0600613 {
614 .cmd = "oem",
615 .cb = cb_oem,
616 },
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500617};
618
619static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
620{
621 char *cmdbuf = req->buf;
622 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
623 int i;
624
625 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
626 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
627 func_cb = cmd_dispatch_info[i].cb;
628 break;
629 }
630 }
631
Steve Rae56ad7922014-08-26 11:47:29 -0700632 if (!func_cb) {
633 error("unknown command: %s\n", cmdbuf);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500634 fastboot_tx_write_str("FAILunknown command");
Steve Rae56ad7922014-08-26 11:47:29 -0700635 } else {
Eric Nelson3d626cd2014-10-01 14:30:56 -0700636 if (req->actual < req->length) {
637 u8 *buf = (u8 *)req->buf;
638 buf[req->actual] = 0;
639 func_cb(ep, req);
640 } else {
641 error("buffer overflow\n");
642 fastboot_tx_write_str("FAILbuffer overflow");
643 }
Steve Rae56ad7922014-08-26 11:47:29 -0700644 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500645
646 if (req->status == 0) {
647 *cmdbuf = '\0';
648 req->actual = 0;
649 usb_ep_queue(ep, req, 0);
650 }
651}