]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/usb/gadget/f_fastboot.c
fastboot: Ensure we treat CONFIG_FASTBOOT_BUF_ADDR as long
[people/ms/u-boot.git] / drivers / usb / gadget / f_fastboot.c
CommitLineData
3aab70af
SS
1/*
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 */
593cbd93 13#include <config.h>
3aab70af
SS
14#include <common.h>
15#include <errno.h>
3c8f98f5 16#include <fastboot.h>
3aab70af
SS
17#include <malloc.h>
18#include <linux/usb/ch9.h>
19#include <linux/usb/gadget.h>
20#include <linux/usb/composite.h>
21#include <linux/compiler.h>
22#include <version.h>
23#include <g_dnl.h>
d1b5ed07
SR
24#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
25#include <fb_mmc.h>
26#endif
bf8940d3
MR
27#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
28#include <fb_nand.h>
29#endif
3aab70af
SS
30
31#define FASTBOOT_VERSION "0.4"
32
33#define FASTBOOT_INTERFACE_CLASS 0xff
34#define FASTBOOT_INTERFACE_SUB_CLASS 0x42
35#define FASTBOOT_INTERFACE_PROTOCOL 0x03
36
37#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
38#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
39#define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
40
3aab70af 41#define EP_BUFFER_SIZE 4096
ac484c5a
RQ
42/*
43 * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
44 * (64 or 512 or 1024), else we break on certain controllers like DWC3
45 * that expect bulk OUT requests to be divisible by maxpacket size.
46 */
3aab70af
SS
47
48struct f_fastboot {
49 struct usb_function usb_function;
50
593cbd93 51 /* IN/OUT EP's and corresponding requests */
3aab70af
SS
52 struct usb_ep *in_ep, *out_ep;
53 struct usb_request *in_req, *out_req;
54};
55
56static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
57{
58 return container_of(f, struct f_fastboot, usb_function);
59}
60
61static struct f_fastboot *fastboot_func;
62static unsigned int download_size;
63static unsigned int download_bytes;
64
65static struct usb_endpoint_descriptor fs_ep_in = {
66 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT,
68 .bEndpointAddress = USB_DIR_IN,
69 .bmAttributes = USB_ENDPOINT_XFER_BULK,
718156ad 70 .wMaxPacketSize = cpu_to_le16(64),
3aab70af
SS
71};
72
73static struct usb_endpoint_descriptor fs_ep_out = {
74 .bLength = USB_DT_ENDPOINT_SIZE,
75 .bDescriptorType = USB_DT_ENDPOINT,
76 .bEndpointAddress = USB_DIR_OUT,
77 .bmAttributes = USB_ENDPOINT_XFER_BULK,
718156ad
RQ
78 .wMaxPacketSize = cpu_to_le16(64),
79};
80
81static struct usb_endpoint_descriptor hs_ep_in = {
82 .bLength = USB_DT_ENDPOINT_SIZE,
83 .bDescriptorType = USB_DT_ENDPOINT,
84 .bEndpointAddress = USB_DIR_IN,
85 .bmAttributes = USB_ENDPOINT_XFER_BULK,
86 .wMaxPacketSize = cpu_to_le16(512),
3aab70af
SS
87};
88
89static struct usb_endpoint_descriptor hs_ep_out = {
90 .bLength = USB_DT_ENDPOINT_SIZE,
91 .bDescriptorType = USB_DT_ENDPOINT,
92 .bEndpointAddress = USB_DIR_OUT,
93 .bmAttributes = USB_ENDPOINT_XFER_BULK,
718156ad 94 .wMaxPacketSize = cpu_to_le16(512),
3aab70af
SS
95};
96
97static struct usb_interface_descriptor interface_desc = {
98 .bLength = USB_DT_INTERFACE_SIZE,
99 .bDescriptorType = USB_DT_INTERFACE,
100 .bInterfaceNumber = 0x00,
101 .bAlternateSetting = 0x00,
102 .bNumEndpoints = 0x02,
103 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
104 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
105 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
106};
107
718156ad 108static struct usb_descriptor_header *fb_fs_function[] = {
3aab70af
SS
109 (struct usb_descriptor_header *)&interface_desc,
110 (struct usb_descriptor_header *)&fs_ep_in,
718156ad
RQ
111 (struct usb_descriptor_header *)&fs_ep_out,
112};
113
114static struct usb_descriptor_header *fb_hs_function[] = {
115 (struct usb_descriptor_header *)&interface_desc,
116 (struct usb_descriptor_header *)&hs_ep_in,
3aab70af
SS
117 (struct usb_descriptor_header *)&hs_ep_out,
118 NULL,
119};
120
8b704a0e
RQ
121static struct usb_endpoint_descriptor *
122fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
123 struct usb_endpoint_descriptor *hs)
124{
125 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
126 return hs;
127 return fs;
128}
129
3aab70af
SS
130/*
131 * static strings, in UTF-8
132 */
133static const char fastboot_name[] = "Android Fastboot";
134
135static struct usb_string fastboot_string_defs[] = {
136 [0].s = fastboot_name,
137 { } /* end of list */
138};
139
140static struct usb_gadget_strings stringtab_fastboot = {
141 .language = 0x0409, /* en-us */
142 .strings = fastboot_string_defs,
143};
144
145static struct usb_gadget_strings *fastboot_strings[] = {
146 &stringtab_fastboot,
147 NULL,
148};
149
150static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
e2ec3e46 151static int strcmp_l1(const char *s1, const char *s2);
3aab70af 152
3c8f98f5 153
9bc34799
SR
154static char *fb_response_str;
155
156void fastboot_fail(const char *reason)
3c8f98f5 157{
9bc34799
SR
158 strncpy(fb_response_str, "FAIL\0", 5);
159 strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
3c8f98f5
MR
160}
161
9bc34799 162void fastboot_okay(const char *reason)
3c8f98f5 163{
9bc34799
SR
164 strncpy(fb_response_str, "OKAY\0", 5);
165 strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
3c8f98f5
MR
166}
167
3aab70af
SS
168static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
169{
170 int status = req->status;
171 if (!status)
172 return;
173 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
174}
175
176static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
177{
178 int id;
179 struct usb_gadget *gadget = c->cdev->gadget;
180 struct f_fastboot *f_fb = func_to_fastboot(f);
537cd072 181 const char *s;
3aab70af
SS
182
183 /* DYNAMIC interface numbers assignments */
184 id = usb_interface_id(c, f);
185 if (id < 0)
186 return id;
187 interface_desc.bInterfaceNumber = id;
188
189 id = usb_string_id(c->cdev);
190 if (id < 0)
191 return id;
192 fastboot_string_defs[0].id = id;
193 interface_desc.iInterface = id;
194
195 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
196 if (!f_fb->in_ep)
197 return -ENODEV;
198 f_fb->in_ep->driver_data = c->cdev;
199
200 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
201 if (!f_fb->out_ep)
202 return -ENODEV;
203 f_fb->out_ep->driver_data = c->cdev;
204
718156ad
RQ
205 f->descriptors = fb_fs_function;
206
207 if (gadget_is_dualspeed(gadget)) {
208 /* Assume endpoint addresses are the same for both speeds */
209 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
210 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
211 /* copy HS descriptors */
212 f->hs_descriptors = fb_hs_function;
213 }
3aab70af 214
00caae6d 215 s = env_get("serial#");
537cd072
DK
216 if (s)
217 g_dnl_set_serialnumber((char *)s);
218
3aab70af
SS
219 return 0;
220}
221
222static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
223{
224 memset(fastboot_func, 0, sizeof(*fastboot_func));
225}
226
227static void fastboot_disable(struct usb_function *f)
228{
229 struct f_fastboot *f_fb = func_to_fastboot(f);
230
231 usb_ep_disable(f_fb->out_ep);
232 usb_ep_disable(f_fb->in_ep);
233
234 if (f_fb->out_req) {
235 free(f_fb->out_req->buf);
236 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
237 f_fb->out_req = NULL;
238 }
239 if (f_fb->in_req) {
240 free(f_fb->in_req->buf);
241 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
242 f_fb->in_req = NULL;
243 }
244}
245
246static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
247{
248 struct usb_request *req;
249
250 req = usb_ep_alloc_request(ep, 0);
251 if (!req)
252 return NULL;
253
254 req->length = EP_BUFFER_SIZE;
255 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
256 if (!req->buf) {
257 usb_ep_free_request(ep, req);
258 return NULL;
259 }
260
261 memset(req->buf, 0, req->length);
262 return req;
263}
264
265static int fastboot_set_alt(struct usb_function *f,
266 unsigned interface, unsigned alt)
267{
268 int ret;
269 struct usb_composite_dev *cdev = f->config->cdev;
270 struct usb_gadget *gadget = cdev->gadget;
271 struct f_fastboot *f_fb = func_to_fastboot(f);
8b704a0e 272 const struct usb_endpoint_descriptor *d;
3aab70af
SS
273
274 debug("%s: func: %s intf: %d alt: %d\n",
275 __func__, f->name, interface, alt);
276
8b704a0e
RQ
277 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
278 ret = usb_ep_enable(f_fb->out_ep, d);
3aab70af
SS
279 if (ret) {
280 puts("failed to enable out ep\n");
281 return ret;
282 }
283
284 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
285 if (!f_fb->out_req) {
286 puts("failed to alloc out req\n");
287 ret = -EINVAL;
288 goto err;
289 }
290 f_fb->out_req->complete = rx_handler_command;
291
8b704a0e
RQ
292 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
293 ret = usb_ep_enable(f_fb->in_ep, d);
3aab70af
SS
294 if (ret) {
295 puts("failed to enable in ep\n");
296 goto err;
297 }
298
299 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
300 if (!f_fb->in_req) {
301 puts("failed alloc req in\n");
302 ret = -EINVAL;
303 goto err;
304 }
305 f_fb->in_req->complete = fastboot_complete;
306
307 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
308 if (ret)
309 goto err;
310
311 return 0;
312err:
313 fastboot_disable(f);
314 return ret;
315}
316
317static int fastboot_add(struct usb_configuration *c)
318{
319 struct f_fastboot *f_fb = fastboot_func;
320 int status;
321
322 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
323
324 if (!f_fb) {
325 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
326 if (!f_fb)
327 return -ENOMEM;
328
329 fastboot_func = f_fb;
330 memset(f_fb, 0, sizeof(*f_fb));
331 }
332
333 f_fb->usb_function.name = "f_fastboot";
3aab70af
SS
334 f_fb->usb_function.bind = fastboot_bind;
335 f_fb->usb_function.unbind = fastboot_unbind;
336 f_fb->usb_function.set_alt = fastboot_set_alt;
337 f_fb->usb_function.disable = fastboot_disable;
338 f_fb->usb_function.strings = fastboot_strings;
339
340 status = usb_add_function(c, &f_fb->usb_function);
341 if (status) {
342 free(f_fb);
343 fastboot_func = f_fb;
344 }
345
346 return status;
347}
348DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
349
593cbd93 350static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
3aab70af
SS
351{
352 struct usb_request *in_req = fastboot_func->in_req;
353 int ret;
354
355 memcpy(in_req->buf, buffer, buffer_size);
356 in_req->length = buffer_size;
bc9071c9
PK
357
358 usb_ep_dequeue(fastboot_func->in_ep, in_req);
359
3aab70af
SS
360 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
361 if (ret)
362 printf("Error %d on queue\n", ret);
363 return 0;
364}
365
366static int fastboot_tx_write_str(const char *buffer)
367{
368 return fastboot_tx_write(buffer, strlen(buffer));
369}
370
371static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
372{
373 do_reset(NULL, 0, 0, NULL);
374}
375
e2ec3e46
AF
376int __weak fb_set_reboot_flag(void)
377{
378 return -ENOSYS;
379}
380
3aab70af
SS
381static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
382{
e2ec3e46
AF
383 char *cmd = req->buf;
384 if (!strcmp_l1("reboot-bootloader", cmd)) {
385 if (fb_set_reboot_flag()) {
386 fastboot_tx_write_str("FAILCannot set reboot flag");
387 return;
388 }
389 }
3aab70af
SS
390 fastboot_func->in_req->complete = compl_do_reset;
391 fastboot_tx_write_str("OKAY");
392}
393
394static int strcmp_l1(const char *s1, const char *s2)
395{
396 if (!s1 || !s2)
397 return -1;
398 return strncmp(s1, s2, strlen(s1));
399}
400
401static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
402{
403 char *cmd = req->buf;
3c8f98f5 404 char response[FASTBOOT_RESPONSE_LEN];
3aab70af 405 const char *s;
29425be4 406 size_t chars_left;
3aab70af
SS
407
408 strcpy(response, "OKAY");
29425be4
JH
409 chars_left = sizeof(response) - strlen(response) - 1;
410
3aab70af
SS
411 strsep(&cmd, ":");
412 if (!cmd) {
a18c2706 413 error("missing variable");
3aab70af
SS
414 fastboot_tx_write_str("FAILmissing var");
415 return;
416 }
417
418 if (!strcmp_l1("version", cmd)) {
29425be4 419 strncat(response, FASTBOOT_VERSION, chars_left);
3aab70af 420 } else if (!strcmp_l1("bootloader-version", cmd)) {
29425be4 421 strncat(response, U_BOOT_VERSION, chars_left);
c674a666
EN
422 } else if (!strcmp_l1("downloadsize", cmd) ||
423 !strcmp_l1("max-download-size", cmd)) {
3aab70af
SS
424 char str_num[12];
425
a588d99a 426 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
29425be4 427 strncat(response, str_num, chars_left);
3aab70af 428 } else if (!strcmp_l1("serialno", cmd)) {
00caae6d 429 s = env_get("serial#");
3aab70af 430 if (s)
29425be4 431 strncat(response, s, chars_left);
3aab70af
SS
432 else
433 strcpy(response, "FAILValue not set");
434 } else {
b1f2a17c 435 char *envstr;
74322201 436
b1f2a17c 437 envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1);
438 if (!envstr) {
439 fastboot_tx_write_str("FAILmalloc error");
440 return;
441 }
442
443 sprintf(envstr, "fastboot.%s", cmd);
00caae6d 444 s = env_get(envstr);
74322201
RH
445 if (s) {
446 strncat(response, s, chars_left);
447 } else {
448 printf("WARNING: unknown variable: %s\n", cmd);
449 strcpy(response, "FAILVariable not implemented");
450 }
b1f2a17c 451
452 free(envstr);
3aab70af
SS
453 }
454 fastboot_tx_write_str(response);
455}
456
ac484c5a 457static unsigned int rx_bytes_expected(struct usb_ep *ep)
3aab70af
SS
458{
459 int rx_remain = download_size - download_bytes;
ac484c5a
RQ
460 unsigned int rem;
461 unsigned int maxpacket = ep->maxpacket;
462
463 if (rx_remain <= 0)
3aab70af 464 return 0;
ac484c5a 465 else if (rx_remain > EP_BUFFER_SIZE)
3aab70af 466 return EP_BUFFER_SIZE;
ac484c5a
RQ
467
468 /*
469 * Some controllers e.g. DWC3 don't like OUT transfers to be
470 * not ending in maxpacket boundary. So just make them happy by
471 * always requesting for integral multiple of maxpackets.
472 * This shouldn't bother controllers that don't care about it.
473 */
474 rem = rx_remain % maxpacket;
475 if (rem > 0)
9e4b510d 476 rx_remain = rx_remain + (maxpacket - rem);
ac484c5a 477
3aab70af
SS
478 return rx_remain;
479}
480
481#define BYTES_PER_DOT 0x20000
482static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
483{
3c8f98f5 484 char response[FASTBOOT_RESPONSE_LEN];
3aab70af
SS
485 unsigned int transfer_size = download_size - download_bytes;
486 const unsigned char *buffer = req->buf;
487 unsigned int buffer_size = req->actual;
23d1d10c 488 unsigned int pre_dot_num, now_dot_num;
3aab70af
SS
489
490 if (req->status != 0) {
491 printf("Bad status: %d\n", req->status);
492 return;
493 }
494
495 if (buffer_size < transfer_size)
496 transfer_size = buffer_size;
497
a588d99a 498 memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
3aab70af
SS
499 buffer, transfer_size);
500
23d1d10c 501 pre_dot_num = download_bytes / BYTES_PER_DOT;
3aab70af 502 download_bytes += transfer_size;
23d1d10c
BS
503 now_dot_num = download_bytes / BYTES_PER_DOT;
504
505 if (pre_dot_num != now_dot_num) {
506 putc('.');
507 if (!(now_dot_num % 74))
508 putc('\n');
509 }
3aab70af
SS
510
511 /* Check if transfer is done */
512 if (download_bytes >= download_size) {
513 /*
514 * Reset global transfer variable, keep download_bytes because
515 * it will be used in the next possible flashing command
516 */
517 download_size = 0;
518 req->complete = rx_handler_command;
519 req->length = EP_BUFFER_SIZE;
520
192bc694 521 strcpy(response, "OKAY");
3aab70af
SS
522 fastboot_tx_write_str(response);
523
524 printf("\ndownloading of %d bytes finished\n", download_bytes);
525 } else {
ac484c5a 526 req->length = rx_bytes_expected(ep);
3aab70af
SS
527 }
528
3aab70af
SS
529 req->actual = 0;
530 usb_ep_queue(ep, req, 0);
531}
532
533static void cb_download(struct usb_ep *ep, struct usb_request *req)
534{
535 char *cmd = req->buf;
3c8f98f5 536 char response[FASTBOOT_RESPONSE_LEN];
3aab70af
SS
537
538 strsep(&cmd, ":");
539 download_size = simple_strtoul(cmd, NULL, 16);
540 download_bytes = 0;
541
542 printf("Starting download of %d bytes\n", download_size);
543
544 if (0 == download_size) {
192bc694 545 strcpy(response, "FAILdata invalid size");
a588d99a 546 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
3aab70af 547 download_size = 0;
192bc694 548 strcpy(response, "FAILdata too large");
3aab70af
SS
549 } else {
550 sprintf(response, "DATA%08x", download_size);
551 req->complete = rx_handler_dl_image;
ac484c5a 552 req->length = rx_bytes_expected(ep);
3aab70af
SS
553 }
554 fastboot_tx_write_str(response);
555}
556
557static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
558{
559 char boot_addr_start[12];
560 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
561
562 puts("Booting kernel..\n");
563
90ae53ce 564 sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR);
3aab70af
SS
565 do_bootm(NULL, 0, 2, bootm_args);
566
567 /* This only happens if image is somehow faulty so we start over */
568 do_reset(NULL, 0, 0, NULL);
569}
570
571static void cb_boot(struct usb_ep *ep, struct usb_request *req)
572{
573 fastboot_func->in_req->complete = do_bootm_on_complete;
574 fastboot_tx_write_str("OKAY");
575}
576
267abc62
RH
577static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
578{
579 g_dnl_trigger_detach();
580}
581
582static void cb_continue(struct usb_ep *ep, struct usb_request *req)
583{
584 fastboot_func->in_req->complete = do_exit_on_complete;
585 fastboot_tx_write_str("OKAY");
586}
587
d1b5ed07
SR
588#ifdef CONFIG_FASTBOOT_FLASH
589static void cb_flash(struct usb_ep *ep, struct usb_request *req)
590{
591 char *cmd = req->buf;
3c8f98f5 592 char response[FASTBOOT_RESPONSE_LEN];
d1b5ed07
SR
593
594 strsep(&cmd, ":");
595 if (!cmd) {
a18c2706 596 error("missing partition name");
d1b5ed07
SR
597 fastboot_tx_write_str("FAILmissing partition name");
598 return;
599 }
600
9bc34799
SR
601 /* initialize the response buffer */
602 fb_response_str = response;
603
604 fastboot_fail("no flash device defined");
d1b5ed07 605#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
64ece848 606 fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
9bc34799 607 download_bytes);
bf8940d3
MR
608#endif
609#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
64ece848 610 fb_nand_flash_write(cmd,
bf8940d3 611 (void *)CONFIG_FASTBOOT_BUF_ADDR,
9bc34799 612 download_bytes);
d1b5ed07
SR
613#endif
614 fastboot_tx_write_str(response);
615}
616#endif
617
de195620
MS
618static void cb_oem(struct usb_ep *ep, struct usb_request *req)
619{
620 char *cmd = req->buf;
4adef270 621#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
372d7dec
RH
622 if (strncmp("format", cmd + 4, 6) == 0) {
623 char cmdbuf[32];
624 sprintf(cmdbuf, "gpt write mmc %x $partitions",
625 CONFIG_FASTBOOT_FLASH_MMC_DEV);
626 if (run_command(cmdbuf, 0))
627 fastboot_tx_write_str("FAIL");
628 else
629 fastboot_tx_write_str("OKAY");
630 } else
631#endif
de195620
MS
632 if (strncmp("unlock", cmd + 4, 8) == 0) {
633 fastboot_tx_write_str("FAILnot implemented");
634 }
635 else {
636 fastboot_tx_write_str("FAILunknown oem command");
637 }
638}
639
89792381
DK
640#ifdef CONFIG_FASTBOOT_FLASH
641static void cb_erase(struct usb_ep *ep, struct usb_request *req)
642{
643 char *cmd = req->buf;
3c8f98f5 644 char response[FASTBOOT_RESPONSE_LEN];
89792381
DK
645
646 strsep(&cmd, ":");
647 if (!cmd) {
648 error("missing partition name");
649 fastboot_tx_write_str("FAILmissing partition name");
650 return;
651 }
652
9bc34799
SR
653 /* initialize the response buffer */
654 fb_response_str = response;
89792381 655
9bc34799 656 fastboot_fail("no flash device defined");
89792381 657#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
9bc34799 658 fb_mmc_erase(cmd);
bf8940d3
MR
659#endif
660#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
9bc34799 661 fb_nand_erase(cmd);
89792381
DK
662#endif
663 fastboot_tx_write_str(response);
664}
665#endif
666
3aab70af
SS
667struct cmd_dispatch_info {
668 char *cmd;
669 void (*cb)(struct usb_ep *ep, struct usb_request *req);
670};
671
672static const struct cmd_dispatch_info cmd_dispatch_info[] = {
673 {
674 .cmd = "reboot",
675 .cb = cb_reboot,
676 }, {
677 .cmd = "getvar:",
678 .cb = cb_getvar,
679 }, {
680 .cmd = "download:",
681 .cb = cb_download,
682 }, {
683 .cmd = "boot",
684 .cb = cb_boot,
267abc62
RH
685 }, {
686 .cmd = "continue",
687 .cb = cb_continue,
3aab70af 688 },
d1b5ed07
SR
689#ifdef CONFIG_FASTBOOT_FLASH
690 {
691 .cmd = "flash",
692 .cb = cb_flash,
89792381
DK
693 }, {
694 .cmd = "erase",
695 .cb = cb_erase,
d1b5ed07
SR
696 },
697#endif
de195620
MS
698 {
699 .cmd = "oem",
700 .cb = cb_oem,
701 },
3aab70af
SS
702};
703
704static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
705{
706 char *cmdbuf = req->buf;
707 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
708 int i;
709
94b385fa
PK
710 if (req->status != 0 || req->length == 0)
711 return;
712
3aab70af
SS
713 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
714 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
715 func_cb = cmd_dispatch_info[i].cb;
716 break;
717 }
718 }
719
593cbd93 720 if (!func_cb) {
7715dea4 721 error("unknown command: %.*s", req->actual, cmdbuf);
3aab70af 722 fastboot_tx_write_str("FAILunknown command");
593cbd93 723 } else {
e2140588
EN
724 if (req->actual < req->length) {
725 u8 *buf = (u8 *)req->buf;
726 buf[req->actual] = 0;
727 func_cb(ep, req);
728 } else {
a18c2706 729 error("buffer overflow");
e2140588
EN
730 fastboot_tx_write_str("FAILbuffer overflow");
731 }
593cbd93 732 }
3aab70af 733
94b385fa
PK
734 *cmdbuf = '\0';
735 req->actual = 0;
736 usb_ep_queue(ep, req, 0);
3aab70af 737}