blob: e1038ead4128aeda085d5f53b1283ee998cafe16 [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
Maxime Ripardbf8940d2015-10-15 14:34:17 +020027#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
28#include <fb_nand.h>
29#endif
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050030
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
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050041#define EP_BUFFER_SIZE 4096
42
43struct f_fastboot {
44 struct usb_function usb_function;
45
Steve Rae593cbd92014-08-26 11:47:29 -070046 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050047 struct usb_ep *in_ep, *out_ep;
48 struct usb_request *in_req, *out_req;
49};
50
51static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
52{
53 return container_of(f, struct f_fastboot, usb_function);
54}
55
56static struct f_fastboot *fastboot_func;
Maxime Ripard6c9e00e2015-10-15 14:34:15 +020057static unsigned int fastboot_flash_session_id;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050058static unsigned int download_size;
59static unsigned int download_bytes;
Dileep Katta9e4b5102015-02-17 02:02:36 +053060static bool is_high_speed;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050061
62static struct usb_endpoint_descriptor fs_ep_in = {
63 .bLength = USB_DT_ENDPOINT_SIZE,
64 .bDescriptorType = USB_DT_ENDPOINT,
65 .bEndpointAddress = USB_DIR_IN,
66 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadros718156a2016-04-12 15:51:48 +030067 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050068};
69
70static struct usb_endpoint_descriptor fs_ep_out = {
71 .bLength = USB_DT_ENDPOINT_SIZE,
72 .bDescriptorType = USB_DT_ENDPOINT,
73 .bEndpointAddress = USB_DIR_OUT,
74 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadros718156a2016-04-12 15:51:48 +030075 .wMaxPacketSize = cpu_to_le16(64),
76};
77
78static struct usb_endpoint_descriptor hs_ep_in = {
79 .bLength = USB_DT_ENDPOINT_SIZE,
80 .bDescriptorType = USB_DT_ENDPOINT,
81 .bEndpointAddress = USB_DIR_IN,
82 .bmAttributes = USB_ENDPOINT_XFER_BULK,
83 .wMaxPacketSize = cpu_to_le16(512),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050084};
85
86static struct usb_endpoint_descriptor hs_ep_out = {
87 .bLength = USB_DT_ENDPOINT_SIZE,
88 .bDescriptorType = USB_DT_ENDPOINT,
89 .bEndpointAddress = USB_DIR_OUT,
90 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadros718156a2016-04-12 15:51:48 +030091 .wMaxPacketSize = cpu_to_le16(512),
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050092};
93
94static struct usb_interface_descriptor interface_desc = {
95 .bLength = USB_DT_INTERFACE_SIZE,
96 .bDescriptorType = USB_DT_INTERFACE,
97 .bInterfaceNumber = 0x00,
98 .bAlternateSetting = 0x00,
99 .bNumEndpoints = 0x02,
100 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
101 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
102 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
103};
104
Roger Quadros718156a2016-04-12 15:51:48 +0300105static struct usb_descriptor_header *fb_fs_function[] = {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500106 (struct usb_descriptor_header *)&interface_desc,
107 (struct usb_descriptor_header *)&fs_ep_in,
Roger Quadros718156a2016-04-12 15:51:48 +0300108 (struct usb_descriptor_header *)&fs_ep_out,
109};
110
111static struct usb_descriptor_header *fb_hs_function[] = {
112 (struct usb_descriptor_header *)&interface_desc,
113 (struct usb_descriptor_header *)&hs_ep_in,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500114 (struct usb_descriptor_header *)&hs_ep_out,
115 NULL,
116};
117
118/*
119 * static strings, in UTF-8
120 */
121static const char fastboot_name[] = "Android Fastboot";
122
123static struct usb_string fastboot_string_defs[] = {
124 [0].s = fastboot_name,
125 { } /* end of list */
126};
127
128static struct usb_gadget_strings stringtab_fastboot = {
129 .language = 0x0409, /* en-us */
130 .strings = fastboot_string_defs,
131};
132
133static struct usb_gadget_strings *fastboot_strings[] = {
134 &stringtab_fastboot,
135 NULL,
136};
137
138static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300139static int strcmp_l1(const char *s1, const char *s2);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500140
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200141
142void fastboot_fail(char *response, const char *reason)
143{
144 strncpy(response, "FAIL\0", 5);
145 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
146}
147
148void fastboot_okay(char *response, const char *reason)
149{
150 strncpy(response, "OKAY\0", 5);
151 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
152}
153
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500154static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
155{
156 int status = req->status;
157 if (!status)
158 return;
159 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
160}
161
162static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
163{
164 int id;
165 struct usb_gadget *gadget = c->cdev->gadget;
166 struct f_fastboot *f_fb = func_to_fastboot(f);
Dileep Katta537cd072015-02-13 14:33:43 +0800167 const char *s;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500168
169 /* DYNAMIC interface numbers assignments */
170 id = usb_interface_id(c, f);
171 if (id < 0)
172 return id;
173 interface_desc.bInterfaceNumber = id;
174
175 id = usb_string_id(c->cdev);
176 if (id < 0)
177 return id;
178 fastboot_string_defs[0].id = id;
179 interface_desc.iInterface = id;
180
181 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
182 if (!f_fb->in_ep)
183 return -ENODEV;
184 f_fb->in_ep->driver_data = c->cdev;
185
186 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
187 if (!f_fb->out_ep)
188 return -ENODEV;
189 f_fb->out_ep->driver_data = c->cdev;
190
Roger Quadros718156a2016-04-12 15:51:48 +0300191 f->descriptors = fb_fs_function;
192
193 if (gadget_is_dualspeed(gadget)) {
194 /* Assume endpoint addresses are the same for both speeds */
195 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
196 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
197 /* copy HS descriptors */
198 f->hs_descriptors = fb_hs_function;
199 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500200
Dileep Katta537cd072015-02-13 14:33:43 +0800201 s = getenv("serial#");
202 if (s)
203 g_dnl_set_serialnumber((char *)s);
204
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500205 return 0;
206}
207
208static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
209{
210 memset(fastboot_func, 0, sizeof(*fastboot_func));
211}
212
213static void fastboot_disable(struct usb_function *f)
214{
215 struct f_fastboot *f_fb = func_to_fastboot(f);
216
217 usb_ep_disable(f_fb->out_ep);
218 usb_ep_disable(f_fb->in_ep);
219
220 if (f_fb->out_req) {
221 free(f_fb->out_req->buf);
222 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
223 f_fb->out_req = NULL;
224 }
225 if (f_fb->in_req) {
226 free(f_fb->in_req->buf);
227 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
228 f_fb->in_req = NULL;
229 }
230}
231
232static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
233{
234 struct usb_request *req;
235
236 req = usb_ep_alloc_request(ep, 0);
237 if (!req)
238 return NULL;
239
240 req->length = EP_BUFFER_SIZE;
241 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
242 if (!req->buf) {
243 usb_ep_free_request(ep, req);
244 return NULL;
245 }
246
247 memset(req->buf, 0, req->length);
248 return req;
249}
250
251static int fastboot_set_alt(struct usb_function *f,
252 unsigned interface, unsigned alt)
253{
254 int ret;
255 struct usb_composite_dev *cdev = f->config->cdev;
256 struct usb_gadget *gadget = cdev->gadget;
257 struct f_fastboot *f_fb = func_to_fastboot(f);
258
259 debug("%s: func: %s intf: %d alt: %d\n",
260 __func__, f->name, interface, alt);
261
262 /* make sure we don't enable the ep twice */
Dileep Katta9e4b5102015-02-17 02:02:36 +0530263 if (gadget->speed == USB_SPEED_HIGH) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500264 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
Dileep Katta9e4b5102015-02-17 02:02:36 +0530265 is_high_speed = true;
266 } else {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500267 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
Dileep Katta9e4b5102015-02-17 02:02:36 +0530268 is_high_speed = false;
269 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500270 if (ret) {
271 puts("failed to enable out ep\n");
272 return ret;
273 }
274
275 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
276 if (!f_fb->out_req) {
277 puts("failed to alloc out req\n");
278 ret = -EINVAL;
279 goto err;
280 }
281 f_fb->out_req->complete = rx_handler_command;
282
283 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
284 if (ret) {
285 puts("failed to enable in ep\n");
286 goto err;
287 }
288
289 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
290 if (!f_fb->in_req) {
291 puts("failed alloc req in\n");
292 ret = -EINVAL;
293 goto err;
294 }
295 f_fb->in_req->complete = fastboot_complete;
296
297 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
298 if (ret)
299 goto err;
300
301 return 0;
302err:
303 fastboot_disable(f);
304 return ret;
305}
306
307static int fastboot_add(struct usb_configuration *c)
308{
309 struct f_fastboot *f_fb = fastboot_func;
310 int status;
311
312 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
313
314 if (!f_fb) {
315 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
316 if (!f_fb)
317 return -ENOMEM;
318
319 fastboot_func = f_fb;
320 memset(f_fb, 0, sizeof(*f_fb));
321 }
322
323 f_fb->usb_function.name = "f_fastboot";
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500324 f_fb->usb_function.bind = fastboot_bind;
325 f_fb->usb_function.unbind = fastboot_unbind;
326 f_fb->usb_function.set_alt = fastboot_set_alt;
327 f_fb->usb_function.disable = fastboot_disable;
328 f_fb->usb_function.strings = fastboot_strings;
329
330 status = usb_add_function(c, &f_fb->usb_function);
331 if (status) {
332 free(f_fb);
333 fastboot_func = f_fb;
334 }
335
336 return status;
337}
338DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
339
Steve Rae593cbd92014-08-26 11:47:29 -0700340static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500341{
342 struct usb_request *in_req = fastboot_func->in_req;
343 int ret;
344
345 memcpy(in_req->buf, buffer, buffer_size);
346 in_req->length = buffer_size;
Paul Kocialkowskibc9071c2015-07-04 16:46:16 +0200347
348 usb_ep_dequeue(fastboot_func->in_ep, in_req);
349
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500350 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
351 if (ret)
352 printf("Error %d on queue\n", ret);
353 return 0;
354}
355
356static int fastboot_tx_write_str(const char *buffer)
357{
358 return fastboot_tx_write(buffer, strlen(buffer));
359}
360
361static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
362{
363 do_reset(NULL, 0, 0, NULL);
364}
365
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300366int __weak fb_set_reboot_flag(void)
367{
368 return -ENOSYS;
369}
370
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500371static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
372{
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300373 char *cmd = req->buf;
374 if (!strcmp_l1("reboot-bootloader", cmd)) {
375 if (fb_set_reboot_flag()) {
376 fastboot_tx_write_str("FAILCannot set reboot flag");
377 return;
378 }
379 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500380 fastboot_func->in_req->complete = compl_do_reset;
381 fastboot_tx_write_str("OKAY");
382}
383
384static int strcmp_l1(const char *s1, const char *s2)
385{
386 if (!s1 || !s2)
387 return -1;
388 return strncmp(s1, s2, strlen(s1));
389}
390
391static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
392{
393 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200394 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500395 const char *s;
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200396 size_t chars_left;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500397
398 strcpy(response, "OKAY");
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200399 chars_left = sizeof(response) - strlen(response) - 1;
400
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500401 strsep(&cmd, ":");
402 if (!cmd) {
Steve Raea18c2702016-01-27 15:02:41 -0800403 error("missing variable");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500404 fastboot_tx_write_str("FAILmissing var");
405 return;
406 }
407
408 if (!strcmp_l1("version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200409 strncat(response, FASTBOOT_VERSION, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500410 } else if (!strcmp_l1("bootloader-version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200411 strncat(response, U_BOOT_VERSION, chars_left);
Eric Nelsonc674a662014-09-30 12:05:40 -0700412 } else if (!strcmp_l1("downloadsize", cmd) ||
413 !strcmp_l1("max-download-size", cmd)) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500414 char str_num[12];
415
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200416 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200417 strncat(response, str_num, chars_left);
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200418
419 /*
420 * This also indicates the start of a new flashing
421 * "session", in which we could have 1-N buffers to
422 * write to a partition.
423 *
424 * Reset our session counter.
425 */
426 fastboot_flash_session_id = 0;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500427 } else if (!strcmp_l1("serialno", cmd)) {
428 s = getenv("serial#");
429 if (s)
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200430 strncat(response, s, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500431 else
432 strcpy(response, "FAILValue not set");
433 } else {
Rob Herring74322202016-03-17 17:21:23 +0100434 char envstr[32];
435
436 snprintf(envstr, sizeof(envstr) - 1, "fastboot.%s", cmd);
437 s = getenv(envstr);
438 if (s) {
439 strncat(response, s, chars_left);
440 } else {
441 printf("WARNING: unknown variable: %s\n", cmd);
442 strcpy(response, "FAILVariable not implemented");
443 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500444 }
445 fastboot_tx_write_str(response);
446}
447
Dileep Katta9e4b5102015-02-17 02:02:36 +0530448static unsigned int rx_bytes_expected(unsigned int maxpacket)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500449{
450 int rx_remain = download_size - download_bytes;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530451 int rem = 0;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500452 if (rx_remain < 0)
453 return 0;
454 if (rx_remain > EP_BUFFER_SIZE)
455 return EP_BUFFER_SIZE;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530456 if (rx_remain < maxpacket) {
457 rx_remain = maxpacket;
458 } else if (rx_remain % maxpacket != 0) {
459 rem = rx_remain % maxpacket;
460 rx_remain = rx_remain + (maxpacket - rem);
461 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500462 return rx_remain;
463}
464
465#define BYTES_PER_DOT 0x20000
466static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
467{
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200468 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500469 unsigned int transfer_size = download_size - download_bytes;
470 const unsigned char *buffer = req->buf;
471 unsigned int buffer_size = req->actual;
Bo Shen23d1d102014-09-19 14:15:12 +0800472 unsigned int pre_dot_num, now_dot_num;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530473 unsigned int max;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500474
475 if (req->status != 0) {
476 printf("Bad status: %d\n", req->status);
477 return;
478 }
479
480 if (buffer_size < transfer_size)
481 transfer_size = buffer_size;
482
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200483 memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500484 buffer, transfer_size);
485
Bo Shen23d1d102014-09-19 14:15:12 +0800486 pre_dot_num = download_bytes / BYTES_PER_DOT;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500487 download_bytes += transfer_size;
Bo Shen23d1d102014-09-19 14:15:12 +0800488 now_dot_num = download_bytes / BYTES_PER_DOT;
489
490 if (pre_dot_num != now_dot_num) {
491 putc('.');
492 if (!(now_dot_num % 74))
493 putc('\n');
494 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500495
496 /* Check if transfer is done */
497 if (download_bytes >= download_size) {
498 /*
499 * Reset global transfer variable, keep download_bytes because
500 * it will be used in the next possible flashing command
501 */
502 download_size = 0;
503 req->complete = rx_handler_command;
504 req->length = EP_BUFFER_SIZE;
505
Ben Whitten192bc692015-12-30 13:05:58 +0000506 strcpy(response, "OKAY");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500507 fastboot_tx_write_str(response);
508
509 printf("\ndownloading of %d bytes finished\n", download_bytes);
510 } else {
Dileep Katta9e4b5102015-02-17 02:02:36 +0530511 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
512 fs_ep_out.wMaxPacketSize;
513 req->length = rx_bytes_expected(max);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500514 if (req->length < ep->maxpacket)
515 req->length = ep->maxpacket;
516 }
517
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500518 req->actual = 0;
519 usb_ep_queue(ep, req, 0);
520}
521
522static void cb_download(struct usb_ep *ep, struct usb_request *req)
523{
524 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200525 char response[FASTBOOT_RESPONSE_LEN];
Dileep Katta9e4b5102015-02-17 02:02:36 +0530526 unsigned int max;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500527
528 strsep(&cmd, ":");
529 download_size = simple_strtoul(cmd, NULL, 16);
530 download_bytes = 0;
531
532 printf("Starting download of %d bytes\n", download_size);
533
534 if (0 == download_size) {
Ben Whitten192bc692015-12-30 13:05:58 +0000535 strcpy(response, "FAILdata invalid size");
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200536 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500537 download_size = 0;
Ben Whitten192bc692015-12-30 13:05:58 +0000538 strcpy(response, "FAILdata too large");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500539 } else {
540 sprintf(response, "DATA%08x", download_size);
541 req->complete = rx_handler_dl_image;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530542 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
543 fs_ep_out.wMaxPacketSize;
544 req->length = rx_bytes_expected(max);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500545 if (req->length < ep->maxpacket)
546 req->length = ep->maxpacket;
547 }
548 fastboot_tx_write_str(response);
549}
550
551static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
552{
553 char boot_addr_start[12];
554 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
555
556 puts("Booting kernel..\n");
557
558 sprintf(boot_addr_start, "0x%lx", load_addr);
559 do_bootm(NULL, 0, 2, bootm_args);
560
561 /* This only happens if image is somehow faulty so we start over */
562 do_reset(NULL, 0, 0, NULL);
563}
564
565static void cb_boot(struct usb_ep *ep, struct usb_request *req)
566{
567 fastboot_func->in_req->complete = do_bootm_on_complete;
568 fastboot_tx_write_str("OKAY");
569}
570
Rob Herring267abc62014-12-10 14:43:04 -0600571static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
572{
573 g_dnl_trigger_detach();
574}
575
576static void cb_continue(struct usb_ep *ep, struct usb_request *req)
577{
578 fastboot_func->in_req->complete = do_exit_on_complete;
579 fastboot_tx_write_str("OKAY");
580}
581
Steve Raed1b5ed02014-08-26 11:47:28 -0700582#ifdef CONFIG_FASTBOOT_FLASH
583static void cb_flash(struct usb_ep *ep, struct usb_request *req)
584{
585 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200586 char response[FASTBOOT_RESPONSE_LEN];
Steve Raed1b5ed02014-08-26 11:47:28 -0700587
588 strsep(&cmd, ":");
589 if (!cmd) {
Steve Raea18c2702016-01-27 15:02:41 -0800590 error("missing partition name");
Steve Raed1b5ed02014-08-26 11:47:28 -0700591 fastboot_tx_write_str("FAILmissing partition name");
592 return;
593 }
594
595 strcpy(response, "FAILno flash device defined");
596#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200597 fb_mmc_flash_write(cmd, fastboot_flash_session_id,
598 (void *)CONFIG_FASTBOOT_BUF_ADDR,
Steve Raed1b5ed02014-08-26 11:47:28 -0700599 download_bytes, response);
600#endif
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200601#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
602 fb_nand_flash_write(cmd, fastboot_flash_session_id,
603 (void *)CONFIG_FASTBOOT_BUF_ADDR,
604 download_bytes, response);
605#endif
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200606 fastboot_flash_session_id++;
Steve Raed1b5ed02014-08-26 11:47:28 -0700607 fastboot_tx_write_str(response);
608}
609#endif
610
Michael Scottde195622015-01-26 15:49:00 -0600611static void cb_oem(struct usb_ep *ep, struct usb_request *req)
612{
613 char *cmd = req->buf;
Maxime Ripard4adef272015-10-15 22:04:04 +0200614#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
Rob Herring372d7de2015-01-26 15:49:01 -0600615 if (strncmp("format", cmd + 4, 6) == 0) {
616 char cmdbuf[32];
617 sprintf(cmdbuf, "gpt write mmc %x $partitions",
618 CONFIG_FASTBOOT_FLASH_MMC_DEV);
619 if (run_command(cmdbuf, 0))
620 fastboot_tx_write_str("FAIL");
621 else
622 fastboot_tx_write_str("OKAY");
623 } else
624#endif
Michael Scottde195622015-01-26 15:49:00 -0600625 if (strncmp("unlock", cmd + 4, 8) == 0) {
626 fastboot_tx_write_str("FAILnot implemented");
627 }
628 else {
629 fastboot_tx_write_str("FAILunknown oem command");
630 }
631}
632
Dileep Katta89792382015-02-17 18:48:23 +0530633#ifdef CONFIG_FASTBOOT_FLASH
634static void cb_erase(struct usb_ep *ep, struct usb_request *req)
635{
636 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200637 char response[FASTBOOT_RESPONSE_LEN];
Dileep Katta89792382015-02-17 18:48:23 +0530638
639 strsep(&cmd, ":");
640 if (!cmd) {
641 error("missing partition name");
642 fastboot_tx_write_str("FAILmissing partition name");
643 return;
644 }
645
646 strcpy(response, "FAILno flash device defined");
647
648#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
649 fb_mmc_erase(cmd, response);
650#endif
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200651#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
652 fb_nand_erase(cmd, response);
653#endif
Dileep Katta89792382015-02-17 18:48:23 +0530654 fastboot_tx_write_str(response);
655}
656#endif
657
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500658struct cmd_dispatch_info {
659 char *cmd;
660 void (*cb)(struct usb_ep *ep, struct usb_request *req);
661};
662
663static const struct cmd_dispatch_info cmd_dispatch_info[] = {
664 {
665 .cmd = "reboot",
666 .cb = cb_reboot,
667 }, {
668 .cmd = "getvar:",
669 .cb = cb_getvar,
670 }, {
671 .cmd = "download:",
672 .cb = cb_download,
673 }, {
674 .cmd = "boot",
675 .cb = cb_boot,
Rob Herring267abc62014-12-10 14:43:04 -0600676 }, {
677 .cmd = "continue",
678 .cb = cb_continue,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500679 },
Steve Raed1b5ed02014-08-26 11:47:28 -0700680#ifdef CONFIG_FASTBOOT_FLASH
681 {
682 .cmd = "flash",
683 .cb = cb_flash,
Dileep Katta89792382015-02-17 18:48:23 +0530684 }, {
685 .cmd = "erase",
686 .cb = cb_erase,
Steve Raed1b5ed02014-08-26 11:47:28 -0700687 },
688#endif
Michael Scottde195622015-01-26 15:49:00 -0600689 {
690 .cmd = "oem",
691 .cb = cb_oem,
692 },
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500693};
694
695static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
696{
697 char *cmdbuf = req->buf;
698 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
699 int i;
700
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200701 if (req->status != 0 || req->length == 0)
702 return;
703
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500704 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
705 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
706 func_cb = cmd_dispatch_info[i].cb;
707 break;
708 }
709 }
710
Steve Rae593cbd92014-08-26 11:47:29 -0700711 if (!func_cb) {
Steve Raea18c2702016-01-27 15:02:41 -0800712 error("unknown command: %s", cmdbuf);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500713 fastboot_tx_write_str("FAILunknown command");
Steve Rae593cbd92014-08-26 11:47:29 -0700714 } else {
Eric Nelsone2140582014-10-01 14:30:56 -0700715 if (req->actual < req->length) {
716 u8 *buf = (u8 *)req->buf;
717 buf[req->actual] = 0;
718 func_cb(ep, req);
719 } else {
Steve Raea18c2702016-01-27 15:02:41 -0800720 error("buffer overflow");
Eric Nelsone2140582014-10-01 14:30:56 -0700721 fastboot_tx_write_str("FAILbuffer overflow");
722 }
Steve Rae593cbd92014-08-26 11:47:29 -0700723 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500724
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200725 *cmdbuf = '\0';
726 req->actual = 0;
727 usb_ep_queue(ep, req, 0);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500728}