blob: 05fb77302b3e10b04aa06082747f32d44708369f [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 */
Steve Rae593cbd92014-08-26 11:47:29 -070013#include <config.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050014#include <common.h>
15#include <errno.h>
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020016#include <fastboot.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050017#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>
Steve Raed1b5ed02014-08-26 11:47:28 -070024#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
25#include <fb_mmc.h>
26#endif
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050027
28#define FASTBOOT_VERSION "0.4"
29
30#define FASTBOOT_INTERFACE_CLASS 0xff
31#define FASTBOOT_INTERFACE_SUB_CLASS 0x42
32#define FASTBOOT_INTERFACE_PROTOCOL 0x03
33
34#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200)
35#define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040)
36#define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040)
37
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050038#define EP_BUFFER_SIZE 4096
39
40struct f_fastboot {
41 struct usb_function usb_function;
42
Steve Rae593cbd92014-08-26 11:47:29 -070043 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050044 struct usb_ep *in_ep, *out_ep;
45 struct usb_request *in_req, *out_req;
46};
47
48static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
49{
50 return container_of(f, struct f_fastboot, usb_function);
51}
52
53static struct f_fastboot *fastboot_func;
54static unsigned int download_size;
55static unsigned int download_bytes;
Dileep Katta9e4b5102015-02-17 02:02:36 +053056static bool is_high_speed;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050057
58static struct usb_endpoint_descriptor fs_ep_in = {
59 .bLength = USB_DT_ENDPOINT_SIZE,
60 .bDescriptorType = USB_DT_ENDPOINT,
61 .bEndpointAddress = USB_DIR_IN,
62 .bmAttributes = USB_ENDPOINT_XFER_BULK,
63 .wMaxPacketSize = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
64 .bInterval = 0x00,
65};
66
67static struct usb_endpoint_descriptor fs_ep_out = {
68 .bLength = USB_DT_ENDPOINT_SIZE,
69 .bDescriptorType = USB_DT_ENDPOINT,
70 .bEndpointAddress = USB_DIR_OUT,
71 .bmAttributes = USB_ENDPOINT_XFER_BULK,
72 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
73 .bInterval = 0x00,
74};
75
76static struct usb_endpoint_descriptor hs_ep_out = {
77 .bLength = USB_DT_ENDPOINT_SIZE,
78 .bDescriptorType = USB_DT_ENDPOINT,
79 .bEndpointAddress = USB_DIR_OUT,
80 .bmAttributes = USB_ENDPOINT_XFER_BULK,
81 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
82 .bInterval = 0x00,
83};
84
85static struct usb_interface_descriptor interface_desc = {
86 .bLength = USB_DT_INTERFACE_SIZE,
87 .bDescriptorType = USB_DT_INTERFACE,
88 .bInterfaceNumber = 0x00,
89 .bAlternateSetting = 0x00,
90 .bNumEndpoints = 0x02,
91 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
92 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
93 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
94};
95
96static struct usb_descriptor_header *fb_runtime_descs[] = {
97 (struct usb_descriptor_header *)&interface_desc,
98 (struct usb_descriptor_header *)&fs_ep_in,
99 (struct usb_descriptor_header *)&hs_ep_out,
100 NULL,
101};
102
103/*
104 * static strings, in UTF-8
105 */
106static const char fastboot_name[] = "Android Fastboot";
107
108static struct usb_string fastboot_string_defs[] = {
109 [0].s = fastboot_name,
110 { } /* end of list */
111};
112
113static struct usb_gadget_strings stringtab_fastboot = {
114 .language = 0x0409, /* en-us */
115 .strings = fastboot_string_defs,
116};
117
118static struct usb_gadget_strings *fastboot_strings[] = {
119 &stringtab_fastboot,
120 NULL,
121};
122
123static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300124static int strcmp_l1(const char *s1, const char *s2);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500125
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200126
127void fastboot_fail(char *response, const char *reason)
128{
129 strncpy(response, "FAIL\0", 5);
130 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
131}
132
133void fastboot_okay(char *response, const char *reason)
134{
135 strncpy(response, "OKAY\0", 5);
136 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
137}
138
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500139static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
140{
141 int status = req->status;
142 if (!status)
143 return;
144 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
145}
146
147static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
148{
149 int id;
150 struct usb_gadget *gadget = c->cdev->gadget;
151 struct f_fastboot *f_fb = func_to_fastboot(f);
Dileep Katta537cd072015-02-13 14:33:43 +0800152 const char *s;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500153
154 /* DYNAMIC interface numbers assignments */
155 id = usb_interface_id(c, f);
156 if (id < 0)
157 return id;
158 interface_desc.bInterfaceNumber = id;
159
160 id = usb_string_id(c->cdev);
161 if (id < 0)
162 return id;
163 fastboot_string_defs[0].id = id;
164 interface_desc.iInterface = id;
165
166 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
167 if (!f_fb->in_ep)
168 return -ENODEV;
169 f_fb->in_ep->driver_data = c->cdev;
170
171 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
172 if (!f_fb->out_ep)
173 return -ENODEV;
174 f_fb->out_ep->driver_data = c->cdev;
175
176 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
177
Dileep Katta537cd072015-02-13 14:33:43 +0800178 s = getenv("serial#");
179 if (s)
180 g_dnl_set_serialnumber((char *)s);
181
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500182 return 0;
183}
184
185static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
186{
187 memset(fastboot_func, 0, sizeof(*fastboot_func));
188}
189
190static void fastboot_disable(struct usb_function *f)
191{
192 struct f_fastboot *f_fb = func_to_fastboot(f);
193
194 usb_ep_disable(f_fb->out_ep);
195 usb_ep_disable(f_fb->in_ep);
196
197 if (f_fb->out_req) {
198 free(f_fb->out_req->buf);
199 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
200 f_fb->out_req = NULL;
201 }
202 if (f_fb->in_req) {
203 free(f_fb->in_req->buf);
204 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
205 f_fb->in_req = NULL;
206 }
207}
208
209static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
210{
211 struct usb_request *req;
212
213 req = usb_ep_alloc_request(ep, 0);
214 if (!req)
215 return NULL;
216
217 req->length = EP_BUFFER_SIZE;
218 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
219 if (!req->buf) {
220 usb_ep_free_request(ep, req);
221 return NULL;
222 }
223
224 memset(req->buf, 0, req->length);
225 return req;
226}
227
228static int fastboot_set_alt(struct usb_function *f,
229 unsigned interface, unsigned alt)
230{
231 int ret;
232 struct usb_composite_dev *cdev = f->config->cdev;
233 struct usb_gadget *gadget = cdev->gadget;
234 struct f_fastboot *f_fb = func_to_fastboot(f);
235
236 debug("%s: func: %s intf: %d alt: %d\n",
237 __func__, f->name, interface, alt);
238
239 /* make sure we don't enable the ep twice */
Dileep Katta9e4b5102015-02-17 02:02:36 +0530240 if (gadget->speed == USB_SPEED_HIGH) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500241 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
Dileep Katta9e4b5102015-02-17 02:02:36 +0530242 is_high_speed = true;
243 } else {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500244 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
Dileep Katta9e4b5102015-02-17 02:02:36 +0530245 is_high_speed = false;
246 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500247 if (ret) {
248 puts("failed to enable out ep\n");
249 return ret;
250 }
251
252 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
253 if (!f_fb->out_req) {
254 puts("failed to alloc out req\n");
255 ret = -EINVAL;
256 goto err;
257 }
258 f_fb->out_req->complete = rx_handler_command;
259
260 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
261 if (ret) {
262 puts("failed to enable in ep\n");
263 goto err;
264 }
265
266 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
267 if (!f_fb->in_req) {
268 puts("failed alloc req in\n");
269 ret = -EINVAL;
270 goto err;
271 }
272 f_fb->in_req->complete = fastboot_complete;
273
274 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
275 if (ret)
276 goto err;
277
278 return 0;
279err:
280 fastboot_disable(f);
281 return ret;
282}
283
284static int fastboot_add(struct usb_configuration *c)
285{
286 struct f_fastboot *f_fb = fastboot_func;
287 int status;
288
289 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
290
291 if (!f_fb) {
292 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
293 if (!f_fb)
294 return -ENOMEM;
295
296 fastboot_func = f_fb;
297 memset(f_fb, 0, sizeof(*f_fb));
298 }
299
300 f_fb->usb_function.name = "f_fastboot";
301 f_fb->usb_function.hs_descriptors = fb_runtime_descs;
302 f_fb->usb_function.bind = fastboot_bind;
303 f_fb->usb_function.unbind = fastboot_unbind;
304 f_fb->usb_function.set_alt = fastboot_set_alt;
305 f_fb->usb_function.disable = fastboot_disable;
306 f_fb->usb_function.strings = fastboot_strings;
307
308 status = usb_add_function(c, &f_fb->usb_function);
309 if (status) {
310 free(f_fb);
311 fastboot_func = f_fb;
312 }
313
314 return status;
315}
316DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
317
Steve Rae593cbd92014-08-26 11:47:29 -0700318static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500319{
320 struct usb_request *in_req = fastboot_func->in_req;
321 int ret;
322
323 memcpy(in_req->buf, buffer, buffer_size);
324 in_req->length = buffer_size;
Paul Kocialkowskibc9071c2015-07-04 16:46:16 +0200325
326 usb_ep_dequeue(fastboot_func->in_ep, in_req);
327
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500328 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
329 if (ret)
330 printf("Error %d on queue\n", ret);
331 return 0;
332}
333
334static int fastboot_tx_write_str(const char *buffer)
335{
336 return fastboot_tx_write(buffer, strlen(buffer));
337}
338
339static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
340{
341 do_reset(NULL, 0, 0, NULL);
342}
343
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300344int __weak fb_set_reboot_flag(void)
345{
346 return -ENOSYS;
347}
348
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500349static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
350{
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300351 char *cmd = req->buf;
352 if (!strcmp_l1("reboot-bootloader", cmd)) {
353 if (fb_set_reboot_flag()) {
354 fastboot_tx_write_str("FAILCannot set reboot flag");
355 return;
356 }
357 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500358 fastboot_func->in_req->complete = compl_do_reset;
359 fastboot_tx_write_str("OKAY");
360}
361
362static int strcmp_l1(const char *s1, const char *s2)
363{
364 if (!s1 || !s2)
365 return -1;
366 return strncmp(s1, s2, strlen(s1));
367}
368
369static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
370{
371 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200372 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500373 const char *s;
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200374 size_t chars_left;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500375
376 strcpy(response, "OKAY");
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200377 chars_left = sizeof(response) - strlen(response) - 1;
378
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500379 strsep(&cmd, ":");
380 if (!cmd) {
Steve Rae593cbd92014-08-26 11:47:29 -0700381 error("missing variable\n");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500382 fastboot_tx_write_str("FAILmissing var");
383 return;
384 }
385
386 if (!strcmp_l1("version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200387 strncat(response, FASTBOOT_VERSION, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500388 } else if (!strcmp_l1("bootloader-version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200389 strncat(response, U_BOOT_VERSION, chars_left);
Eric Nelsonc674a662014-09-30 12:05:40 -0700390 } else if (!strcmp_l1("downloadsize", cmd) ||
391 !strcmp_l1("max-download-size", cmd)) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500392 char str_num[12];
393
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200394 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200395 strncat(response, str_num, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500396 } else if (!strcmp_l1("serialno", cmd)) {
397 s = getenv("serial#");
398 if (s)
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200399 strncat(response, s, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500400 else
401 strcpy(response, "FAILValue not set");
402 } else {
Steve Rae593cbd92014-08-26 11:47:29 -0700403 error("unknown variable: %s\n", cmd);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500404 strcpy(response, "FAILVariable not implemented");
405 }
406 fastboot_tx_write_str(response);
407}
408
Dileep Katta9e4b5102015-02-17 02:02:36 +0530409static unsigned int rx_bytes_expected(unsigned int maxpacket)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500410{
411 int rx_remain = download_size - download_bytes;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530412 int rem = 0;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500413 if (rx_remain < 0)
414 return 0;
415 if (rx_remain > EP_BUFFER_SIZE)
416 return EP_BUFFER_SIZE;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530417 if (rx_remain < maxpacket) {
418 rx_remain = maxpacket;
419 } else if (rx_remain % maxpacket != 0) {
420 rem = rx_remain % maxpacket;
421 rx_remain = rx_remain + (maxpacket - rem);
422 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500423 return rx_remain;
424}
425
426#define BYTES_PER_DOT 0x20000
427static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
428{
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200429 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500430 unsigned int transfer_size = download_size - download_bytes;
431 const unsigned char *buffer = req->buf;
432 unsigned int buffer_size = req->actual;
Bo Shen23d1d102014-09-19 14:15:12 +0800433 unsigned int pre_dot_num, now_dot_num;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530434 unsigned int max;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500435
436 if (req->status != 0) {
437 printf("Bad status: %d\n", req->status);
438 return;
439 }
440
441 if (buffer_size < transfer_size)
442 transfer_size = buffer_size;
443
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200444 memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500445 buffer, transfer_size);
446
Bo Shen23d1d102014-09-19 14:15:12 +0800447 pre_dot_num = download_bytes / BYTES_PER_DOT;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500448 download_bytes += transfer_size;
Bo Shen23d1d102014-09-19 14:15:12 +0800449 now_dot_num = download_bytes / BYTES_PER_DOT;
450
451 if (pre_dot_num != now_dot_num) {
452 putc('.');
453 if (!(now_dot_num % 74))
454 putc('\n');
455 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500456
457 /* Check if transfer is done */
458 if (download_bytes >= download_size) {
459 /*
460 * Reset global transfer variable, keep download_bytes because
461 * it will be used in the next possible flashing command
462 */
463 download_size = 0;
464 req->complete = rx_handler_command;
465 req->length = EP_BUFFER_SIZE;
466
467 sprintf(response, "OKAY");
468 fastboot_tx_write_str(response);
469
470 printf("\ndownloading of %d bytes finished\n", download_bytes);
471 } else {
Dileep Katta9e4b5102015-02-17 02:02:36 +0530472 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
473 fs_ep_out.wMaxPacketSize;
474 req->length = rx_bytes_expected(max);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500475 if (req->length < ep->maxpacket)
476 req->length = ep->maxpacket;
477 }
478
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500479 req->actual = 0;
480 usb_ep_queue(ep, req, 0);
481}
482
483static void cb_download(struct usb_ep *ep, struct usb_request *req)
484{
485 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200486 char response[FASTBOOT_RESPONSE_LEN];
Dileep Katta9e4b5102015-02-17 02:02:36 +0530487 unsigned int max;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500488
489 strsep(&cmd, ":");
490 download_size = simple_strtoul(cmd, NULL, 16);
491 download_bytes = 0;
492
493 printf("Starting download of %d bytes\n", download_size);
494
495 if (0 == download_size) {
496 sprintf(response, "FAILdata invalid size");
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200497 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500498 download_size = 0;
499 sprintf(response, "FAILdata too large");
500 } else {
501 sprintf(response, "DATA%08x", download_size);
502 req->complete = rx_handler_dl_image;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530503 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
504 fs_ep_out.wMaxPacketSize;
505 req->length = rx_bytes_expected(max);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500506 if (req->length < ep->maxpacket)
507 req->length = ep->maxpacket;
508 }
509 fastboot_tx_write_str(response);
510}
511
512static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
513{
514 char boot_addr_start[12];
515 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
516
517 puts("Booting kernel..\n");
518
519 sprintf(boot_addr_start, "0x%lx", load_addr);
520 do_bootm(NULL, 0, 2, bootm_args);
521
522 /* This only happens if image is somehow faulty so we start over */
523 do_reset(NULL, 0, 0, NULL);
524}
525
526static void cb_boot(struct usb_ep *ep, struct usb_request *req)
527{
528 fastboot_func->in_req->complete = do_bootm_on_complete;
529 fastboot_tx_write_str("OKAY");
530}
531
Rob Herring267abc62014-12-10 14:43:04 -0600532static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
533{
534 g_dnl_trigger_detach();
535}
536
537static void cb_continue(struct usb_ep *ep, struct usb_request *req)
538{
539 fastboot_func->in_req->complete = do_exit_on_complete;
540 fastboot_tx_write_str("OKAY");
541}
542
Steve Raed1b5ed02014-08-26 11:47:28 -0700543#ifdef CONFIG_FASTBOOT_FLASH
544static void cb_flash(struct usb_ep *ep, struct usb_request *req)
545{
546 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200547 char response[FASTBOOT_RESPONSE_LEN];
Steve Raed1b5ed02014-08-26 11:47:28 -0700548
549 strsep(&cmd, ":");
550 if (!cmd) {
Steve Rae593cbd92014-08-26 11:47:29 -0700551 error("missing partition name\n");
Steve Raed1b5ed02014-08-26 11:47:28 -0700552 fastboot_tx_write_str("FAILmissing partition name");
553 return;
554 }
555
556 strcpy(response, "FAILno flash device defined");
557#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200558 fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
Steve Raed1b5ed02014-08-26 11:47:28 -0700559 download_bytes, response);
560#endif
561 fastboot_tx_write_str(response);
562}
563#endif
564
Michael Scottde195622015-01-26 15:49:00 -0600565static void cb_oem(struct usb_ep *ep, struct usb_request *req)
566{
567 char *cmd = req->buf;
Maxime Ripard4adef272015-10-15 22:04:04 +0200568#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
Rob Herring372d7de2015-01-26 15:49:01 -0600569 if (strncmp("format", cmd + 4, 6) == 0) {
570 char cmdbuf[32];
571 sprintf(cmdbuf, "gpt write mmc %x $partitions",
572 CONFIG_FASTBOOT_FLASH_MMC_DEV);
573 if (run_command(cmdbuf, 0))
574 fastboot_tx_write_str("FAIL");
575 else
576 fastboot_tx_write_str("OKAY");
577 } else
578#endif
Michael Scottde195622015-01-26 15:49:00 -0600579 if (strncmp("unlock", cmd + 4, 8) == 0) {
580 fastboot_tx_write_str("FAILnot implemented");
581 }
582 else {
583 fastboot_tx_write_str("FAILunknown oem command");
584 }
585}
586
Dileep Katta89792382015-02-17 18:48:23 +0530587#ifdef CONFIG_FASTBOOT_FLASH
588static void cb_erase(struct usb_ep *ep, struct usb_request *req)
589{
590 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200591 char response[FASTBOOT_RESPONSE_LEN];
Dileep Katta89792382015-02-17 18:48:23 +0530592
593 strsep(&cmd, ":");
594 if (!cmd) {
595 error("missing partition name");
596 fastboot_tx_write_str("FAILmissing partition name");
597 return;
598 }
599
600 strcpy(response, "FAILno flash device defined");
601
602#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
603 fb_mmc_erase(cmd, response);
604#endif
605 fastboot_tx_write_str(response);
606}
607#endif
608
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500609struct cmd_dispatch_info {
610 char *cmd;
611 void (*cb)(struct usb_ep *ep, struct usb_request *req);
612};
613
614static const struct cmd_dispatch_info cmd_dispatch_info[] = {
615 {
616 .cmd = "reboot",
617 .cb = cb_reboot,
618 }, {
619 .cmd = "getvar:",
620 .cb = cb_getvar,
621 }, {
622 .cmd = "download:",
623 .cb = cb_download,
624 }, {
625 .cmd = "boot",
626 .cb = cb_boot,
Rob Herring267abc62014-12-10 14:43:04 -0600627 }, {
628 .cmd = "continue",
629 .cb = cb_continue,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500630 },
Steve Raed1b5ed02014-08-26 11:47:28 -0700631#ifdef CONFIG_FASTBOOT_FLASH
632 {
633 .cmd = "flash",
634 .cb = cb_flash,
Dileep Katta89792382015-02-17 18:48:23 +0530635 }, {
636 .cmd = "erase",
637 .cb = cb_erase,
Steve Raed1b5ed02014-08-26 11:47:28 -0700638 },
639#endif
Michael Scottde195622015-01-26 15:49:00 -0600640 {
641 .cmd = "oem",
642 .cb = cb_oem,
643 },
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500644};
645
646static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
647{
648 char *cmdbuf = req->buf;
649 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
650 int i;
651
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200652 if (req->status != 0 || req->length == 0)
653 return;
654
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500655 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
656 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
657 func_cb = cmd_dispatch_info[i].cb;
658 break;
659 }
660 }
661
Steve Rae593cbd92014-08-26 11:47:29 -0700662 if (!func_cb) {
663 error("unknown command: %s\n", cmdbuf);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500664 fastboot_tx_write_str("FAILunknown command");
Steve Rae593cbd92014-08-26 11:47:29 -0700665 } else {
Eric Nelsone2140582014-10-01 14:30:56 -0700666 if (req->actual < req->length) {
667 u8 *buf = (u8 *)req->buf;
668 buf[req->actual] = 0;
669 func_cb(ep, req);
670 } else {
671 error("buffer overflow\n");
672 fastboot_tx_write_str("FAILbuffer overflow");
673 }
Steve Rae593cbd92014-08-26 11:47:29 -0700674 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500675
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200676 *cmdbuf = '\0';
677 req->actual = 0;
678 usb_ep_queue(ep, req, 0);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500679}