blob: 87e54ebec76be84669129be60e30a9c01429b7de [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,
67 .wMaxPacketSize = TX_ENDPOINT_MAXIMUM_PACKET_SIZE,
68 .bInterval = 0x00,
69};
70
71static struct usb_endpoint_descriptor fs_ep_out = {
72 .bLength = USB_DT_ENDPOINT_SIZE,
73 .bDescriptorType = USB_DT_ENDPOINT,
74 .bEndpointAddress = USB_DIR_OUT,
75 .bmAttributes = USB_ENDPOINT_XFER_BULK,
76 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1,
77 .bInterval = 0x00,
78};
79
80static struct usb_endpoint_descriptor hs_ep_out = {
81 .bLength = USB_DT_ENDPOINT_SIZE,
82 .bDescriptorType = USB_DT_ENDPOINT,
83 .bEndpointAddress = USB_DIR_OUT,
84 .bmAttributes = USB_ENDPOINT_XFER_BULK,
85 .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0,
86 .bInterval = 0x00,
87};
88
89static struct usb_interface_descriptor interface_desc = {
90 .bLength = USB_DT_INTERFACE_SIZE,
91 .bDescriptorType = USB_DT_INTERFACE,
92 .bInterfaceNumber = 0x00,
93 .bAlternateSetting = 0x00,
94 .bNumEndpoints = 0x02,
95 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
96 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
97 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
98};
99
100static struct usb_descriptor_header *fb_runtime_descs[] = {
101 (struct usb_descriptor_header *)&interface_desc,
102 (struct usb_descriptor_header *)&fs_ep_in,
103 (struct usb_descriptor_header *)&hs_ep_out,
104 NULL,
105};
106
107/*
108 * static strings, in UTF-8
109 */
110static const char fastboot_name[] = "Android Fastboot";
111
112static struct usb_string fastboot_string_defs[] = {
113 [0].s = fastboot_name,
114 { } /* end of list */
115};
116
117static struct usb_gadget_strings stringtab_fastboot = {
118 .language = 0x0409, /* en-us */
119 .strings = fastboot_string_defs,
120};
121
122static struct usb_gadget_strings *fastboot_strings[] = {
123 &stringtab_fastboot,
124 NULL,
125};
126
127static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300128static int strcmp_l1(const char *s1, const char *s2);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500129
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200130
131void fastboot_fail(char *response, const char *reason)
132{
133 strncpy(response, "FAIL\0", 5);
134 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
135}
136
137void fastboot_okay(char *response, const char *reason)
138{
139 strncpy(response, "OKAY\0", 5);
140 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
141}
142
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500143static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
144{
145 int status = req->status;
146 if (!status)
147 return;
148 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
149}
150
151static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
152{
153 int id;
154 struct usb_gadget *gadget = c->cdev->gadget;
155 struct f_fastboot *f_fb = func_to_fastboot(f);
Dileep Katta537cd072015-02-13 14:33:43 +0800156 const char *s;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500157
158 /* DYNAMIC interface numbers assignments */
159 id = usb_interface_id(c, f);
160 if (id < 0)
161 return id;
162 interface_desc.bInterfaceNumber = id;
163
164 id = usb_string_id(c->cdev);
165 if (id < 0)
166 return id;
167 fastboot_string_defs[0].id = id;
168 interface_desc.iInterface = id;
169
170 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
171 if (!f_fb->in_ep)
172 return -ENODEV;
173 f_fb->in_ep->driver_data = c->cdev;
174
175 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
176 if (!f_fb->out_ep)
177 return -ENODEV;
178 f_fb->out_ep->driver_data = c->cdev;
179
180 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
181
Dileep Katta537cd072015-02-13 14:33:43 +0800182 s = getenv("serial#");
183 if (s)
184 g_dnl_set_serialnumber((char *)s);
185
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500186 return 0;
187}
188
189static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
190{
191 memset(fastboot_func, 0, sizeof(*fastboot_func));
192}
193
194static void fastboot_disable(struct usb_function *f)
195{
196 struct f_fastboot *f_fb = func_to_fastboot(f);
197
198 usb_ep_disable(f_fb->out_ep);
199 usb_ep_disable(f_fb->in_ep);
200
201 if (f_fb->out_req) {
202 free(f_fb->out_req->buf);
203 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
204 f_fb->out_req = NULL;
205 }
206 if (f_fb->in_req) {
207 free(f_fb->in_req->buf);
208 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
209 f_fb->in_req = NULL;
210 }
211}
212
213static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
214{
215 struct usb_request *req;
216
217 req = usb_ep_alloc_request(ep, 0);
218 if (!req)
219 return NULL;
220
221 req->length = EP_BUFFER_SIZE;
222 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
223 if (!req->buf) {
224 usb_ep_free_request(ep, req);
225 return NULL;
226 }
227
228 memset(req->buf, 0, req->length);
229 return req;
230}
231
232static int fastboot_set_alt(struct usb_function *f,
233 unsigned interface, unsigned alt)
234{
235 int ret;
236 struct usb_composite_dev *cdev = f->config->cdev;
237 struct usb_gadget *gadget = cdev->gadget;
238 struct f_fastboot *f_fb = func_to_fastboot(f);
239
240 debug("%s: func: %s intf: %d alt: %d\n",
241 __func__, f->name, interface, alt);
242
243 /* make sure we don't enable the ep twice */
Dileep Katta9e4b5102015-02-17 02:02:36 +0530244 if (gadget->speed == USB_SPEED_HIGH) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500245 ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out);
Dileep Katta9e4b5102015-02-17 02:02:36 +0530246 is_high_speed = true;
247 } else {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500248 ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out);
Dileep Katta9e4b5102015-02-17 02:02:36 +0530249 is_high_speed = false;
250 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500251 if (ret) {
252 puts("failed to enable out ep\n");
253 return ret;
254 }
255
256 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
257 if (!f_fb->out_req) {
258 puts("failed to alloc out req\n");
259 ret = -EINVAL;
260 goto err;
261 }
262 f_fb->out_req->complete = rx_handler_command;
263
264 ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in);
265 if (ret) {
266 puts("failed to enable in ep\n");
267 goto err;
268 }
269
270 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
271 if (!f_fb->in_req) {
272 puts("failed alloc req in\n");
273 ret = -EINVAL;
274 goto err;
275 }
276 f_fb->in_req->complete = fastboot_complete;
277
278 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
279 if (ret)
280 goto err;
281
282 return 0;
283err:
284 fastboot_disable(f);
285 return ret;
286}
287
288static int fastboot_add(struct usb_configuration *c)
289{
290 struct f_fastboot *f_fb = fastboot_func;
291 int status;
292
293 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
294
295 if (!f_fb) {
296 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
297 if (!f_fb)
298 return -ENOMEM;
299
300 fastboot_func = f_fb;
301 memset(f_fb, 0, sizeof(*f_fb));
302 }
303
304 f_fb->usb_function.name = "f_fastboot";
305 f_fb->usb_function.hs_descriptors = fb_runtime_descs;
306 f_fb->usb_function.bind = fastboot_bind;
307 f_fb->usb_function.unbind = fastboot_unbind;
308 f_fb->usb_function.set_alt = fastboot_set_alt;
309 f_fb->usb_function.disable = fastboot_disable;
310 f_fb->usb_function.strings = fastboot_strings;
311
312 status = usb_add_function(c, &f_fb->usb_function);
313 if (status) {
314 free(f_fb);
315 fastboot_func = f_fb;
316 }
317
318 return status;
319}
320DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
321
Steve Rae593cbd92014-08-26 11:47:29 -0700322static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500323{
324 struct usb_request *in_req = fastboot_func->in_req;
325 int ret;
326
327 memcpy(in_req->buf, buffer, buffer_size);
328 in_req->length = buffer_size;
Paul Kocialkowskibc9071c2015-07-04 16:46:16 +0200329
330 usb_ep_dequeue(fastboot_func->in_ep, in_req);
331
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500332 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
333 if (ret)
334 printf("Error %d on queue\n", ret);
335 return 0;
336}
337
338static int fastboot_tx_write_str(const char *buffer)
339{
340 return fastboot_tx_write(buffer, strlen(buffer));
341}
342
343static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
344{
345 do_reset(NULL, 0, 0, NULL);
346}
347
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300348int __weak fb_set_reboot_flag(void)
349{
350 return -ENOSYS;
351}
352
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500353static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
354{
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300355 char *cmd = req->buf;
356 if (!strcmp_l1("reboot-bootloader", cmd)) {
357 if (fb_set_reboot_flag()) {
358 fastboot_tx_write_str("FAILCannot set reboot flag");
359 return;
360 }
361 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500362 fastboot_func->in_req->complete = compl_do_reset;
363 fastboot_tx_write_str("OKAY");
364}
365
366static int strcmp_l1(const char *s1, const char *s2)
367{
368 if (!s1 || !s2)
369 return -1;
370 return strncmp(s1, s2, strlen(s1));
371}
372
373static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
374{
375 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200376 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500377 const char *s;
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200378 size_t chars_left;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500379
380 strcpy(response, "OKAY");
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200381 chars_left = sizeof(response) - strlen(response) - 1;
382
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500383 strsep(&cmd, ":");
384 if (!cmd) {
Steve Rae593cbd92014-08-26 11:47:29 -0700385 error("missing variable\n");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500386 fastboot_tx_write_str("FAILmissing var");
387 return;
388 }
389
390 if (!strcmp_l1("version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200391 strncat(response, FASTBOOT_VERSION, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500392 } else if (!strcmp_l1("bootloader-version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200393 strncat(response, U_BOOT_VERSION, chars_left);
Eric Nelsonc674a662014-09-30 12:05:40 -0700394 } else if (!strcmp_l1("downloadsize", cmd) ||
395 !strcmp_l1("max-download-size", cmd)) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500396 char str_num[12];
397
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200398 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200399 strncat(response, str_num, chars_left);
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200400
401 /*
402 * This also indicates the start of a new flashing
403 * "session", in which we could have 1-N buffers to
404 * write to a partition.
405 *
406 * Reset our session counter.
407 */
408 fastboot_flash_session_id = 0;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500409 } else if (!strcmp_l1("serialno", cmd)) {
410 s = getenv("serial#");
411 if (s)
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200412 strncat(response, s, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500413 else
414 strcpy(response, "FAILValue not set");
415 } else {
Steve Rae593cbd92014-08-26 11:47:29 -0700416 error("unknown variable: %s\n", cmd);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500417 strcpy(response, "FAILVariable not implemented");
418 }
419 fastboot_tx_write_str(response);
420}
421
Dileep Katta9e4b5102015-02-17 02:02:36 +0530422static unsigned int rx_bytes_expected(unsigned int maxpacket)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500423{
424 int rx_remain = download_size - download_bytes;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530425 int rem = 0;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500426 if (rx_remain < 0)
427 return 0;
428 if (rx_remain > EP_BUFFER_SIZE)
429 return EP_BUFFER_SIZE;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530430 if (rx_remain < maxpacket) {
431 rx_remain = maxpacket;
432 } else if (rx_remain % maxpacket != 0) {
433 rem = rx_remain % maxpacket;
434 rx_remain = rx_remain + (maxpacket - rem);
435 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500436 return rx_remain;
437}
438
439#define BYTES_PER_DOT 0x20000
440static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
441{
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200442 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500443 unsigned int transfer_size = download_size - download_bytes;
444 const unsigned char *buffer = req->buf;
445 unsigned int buffer_size = req->actual;
Bo Shen23d1d102014-09-19 14:15:12 +0800446 unsigned int pre_dot_num, now_dot_num;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530447 unsigned int max;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500448
449 if (req->status != 0) {
450 printf("Bad status: %d\n", req->status);
451 return;
452 }
453
454 if (buffer_size < transfer_size)
455 transfer_size = buffer_size;
456
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200457 memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500458 buffer, transfer_size);
459
Bo Shen23d1d102014-09-19 14:15:12 +0800460 pre_dot_num = download_bytes / BYTES_PER_DOT;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500461 download_bytes += transfer_size;
Bo Shen23d1d102014-09-19 14:15:12 +0800462 now_dot_num = download_bytes / BYTES_PER_DOT;
463
464 if (pre_dot_num != now_dot_num) {
465 putc('.');
466 if (!(now_dot_num % 74))
467 putc('\n');
468 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500469
470 /* Check if transfer is done */
471 if (download_bytes >= download_size) {
472 /*
473 * Reset global transfer variable, keep download_bytes because
474 * it will be used in the next possible flashing command
475 */
476 download_size = 0;
477 req->complete = rx_handler_command;
478 req->length = EP_BUFFER_SIZE;
479
Ben Whitten192bc692015-12-30 13:05:58 +0000480 strcpy(response, "OKAY");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500481 fastboot_tx_write_str(response);
482
483 printf("\ndownloading of %d bytes finished\n", download_bytes);
484 } else {
Dileep Katta9e4b5102015-02-17 02:02:36 +0530485 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
486 fs_ep_out.wMaxPacketSize;
487 req->length = rx_bytes_expected(max);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500488 if (req->length < ep->maxpacket)
489 req->length = ep->maxpacket;
490 }
491
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500492 req->actual = 0;
493 usb_ep_queue(ep, req, 0);
494}
495
496static void cb_download(struct usb_ep *ep, struct usb_request *req)
497{
498 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200499 char response[FASTBOOT_RESPONSE_LEN];
Dileep Katta9e4b5102015-02-17 02:02:36 +0530500 unsigned int max;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500501
502 strsep(&cmd, ":");
503 download_size = simple_strtoul(cmd, NULL, 16);
504 download_bytes = 0;
505
506 printf("Starting download of %d bytes\n", download_size);
507
508 if (0 == download_size) {
Ben Whitten192bc692015-12-30 13:05:58 +0000509 strcpy(response, "FAILdata invalid size");
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200510 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500511 download_size = 0;
Ben Whitten192bc692015-12-30 13:05:58 +0000512 strcpy(response, "FAILdata too large");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500513 } else {
514 sprintf(response, "DATA%08x", download_size);
515 req->complete = rx_handler_dl_image;
Dileep Katta9e4b5102015-02-17 02:02:36 +0530516 max = is_high_speed ? hs_ep_out.wMaxPacketSize :
517 fs_ep_out.wMaxPacketSize;
518 req->length = rx_bytes_expected(max);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500519 if (req->length < ep->maxpacket)
520 req->length = ep->maxpacket;
521 }
522 fastboot_tx_write_str(response);
523}
524
525static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
526{
527 char boot_addr_start[12];
528 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
529
530 puts("Booting kernel..\n");
531
532 sprintf(boot_addr_start, "0x%lx", load_addr);
533 do_bootm(NULL, 0, 2, bootm_args);
534
535 /* This only happens if image is somehow faulty so we start over */
536 do_reset(NULL, 0, 0, NULL);
537}
538
539static void cb_boot(struct usb_ep *ep, struct usb_request *req)
540{
541 fastboot_func->in_req->complete = do_bootm_on_complete;
542 fastboot_tx_write_str("OKAY");
543}
544
Rob Herring267abc62014-12-10 14:43:04 -0600545static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
546{
547 g_dnl_trigger_detach();
548}
549
550static void cb_continue(struct usb_ep *ep, struct usb_request *req)
551{
552 fastboot_func->in_req->complete = do_exit_on_complete;
553 fastboot_tx_write_str("OKAY");
554}
555
Steve Raed1b5ed02014-08-26 11:47:28 -0700556#ifdef CONFIG_FASTBOOT_FLASH
557static void cb_flash(struct usb_ep *ep, struct usb_request *req)
558{
559 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200560 char response[FASTBOOT_RESPONSE_LEN];
Steve Raed1b5ed02014-08-26 11:47:28 -0700561
562 strsep(&cmd, ":");
563 if (!cmd) {
Steve Rae593cbd92014-08-26 11:47:29 -0700564 error("missing partition name\n");
Steve Raed1b5ed02014-08-26 11:47:28 -0700565 fastboot_tx_write_str("FAILmissing partition name");
566 return;
567 }
568
569 strcpy(response, "FAILno flash device defined");
570#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200571 fb_mmc_flash_write(cmd, fastboot_flash_session_id,
572 (void *)CONFIG_FASTBOOT_BUF_ADDR,
Steve Raed1b5ed02014-08-26 11:47:28 -0700573 download_bytes, response);
574#endif
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200575#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
576 fb_nand_flash_write(cmd, fastboot_flash_session_id,
577 (void *)CONFIG_FASTBOOT_BUF_ADDR,
578 download_bytes, response);
579#endif
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200580 fastboot_flash_session_id++;
Steve Raed1b5ed02014-08-26 11:47:28 -0700581 fastboot_tx_write_str(response);
582}
583#endif
584
Michael Scottde195622015-01-26 15:49:00 -0600585static void cb_oem(struct usb_ep *ep, struct usb_request *req)
586{
587 char *cmd = req->buf;
Maxime Ripard4adef272015-10-15 22:04:04 +0200588#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
Rob Herring372d7de2015-01-26 15:49:01 -0600589 if (strncmp("format", cmd + 4, 6) == 0) {
590 char cmdbuf[32];
591 sprintf(cmdbuf, "gpt write mmc %x $partitions",
592 CONFIG_FASTBOOT_FLASH_MMC_DEV);
593 if (run_command(cmdbuf, 0))
594 fastboot_tx_write_str("FAIL");
595 else
596 fastboot_tx_write_str("OKAY");
597 } else
598#endif
Michael Scottde195622015-01-26 15:49:00 -0600599 if (strncmp("unlock", cmd + 4, 8) == 0) {
600 fastboot_tx_write_str("FAILnot implemented");
601 }
602 else {
603 fastboot_tx_write_str("FAILunknown oem command");
604 }
605}
606
Dileep Katta89792382015-02-17 18:48:23 +0530607#ifdef CONFIG_FASTBOOT_FLASH
608static void cb_erase(struct usb_ep *ep, struct usb_request *req)
609{
610 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200611 char response[FASTBOOT_RESPONSE_LEN];
Dileep Katta89792382015-02-17 18:48:23 +0530612
613 strsep(&cmd, ":");
614 if (!cmd) {
615 error("missing partition name");
616 fastboot_tx_write_str("FAILmissing partition name");
617 return;
618 }
619
620 strcpy(response, "FAILno flash device defined");
621
622#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
623 fb_mmc_erase(cmd, response);
624#endif
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200625#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
626 fb_nand_erase(cmd, response);
627#endif
Dileep Katta89792382015-02-17 18:48:23 +0530628 fastboot_tx_write_str(response);
629}
630#endif
631
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500632struct cmd_dispatch_info {
633 char *cmd;
634 void (*cb)(struct usb_ep *ep, struct usb_request *req);
635};
636
637static const struct cmd_dispatch_info cmd_dispatch_info[] = {
638 {
639 .cmd = "reboot",
640 .cb = cb_reboot,
641 }, {
642 .cmd = "getvar:",
643 .cb = cb_getvar,
644 }, {
645 .cmd = "download:",
646 .cb = cb_download,
647 }, {
648 .cmd = "boot",
649 .cb = cb_boot,
Rob Herring267abc62014-12-10 14:43:04 -0600650 }, {
651 .cmd = "continue",
652 .cb = cb_continue,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500653 },
Steve Raed1b5ed02014-08-26 11:47:28 -0700654#ifdef CONFIG_FASTBOOT_FLASH
655 {
656 .cmd = "flash",
657 .cb = cb_flash,
Dileep Katta89792382015-02-17 18:48:23 +0530658 }, {
659 .cmd = "erase",
660 .cb = cb_erase,
Steve Raed1b5ed02014-08-26 11:47:28 -0700661 },
662#endif
Michael Scottde195622015-01-26 15:49:00 -0600663 {
664 .cmd = "oem",
665 .cb = cb_oem,
666 },
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500667};
668
669static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
670{
671 char *cmdbuf = req->buf;
672 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
673 int i;
674
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200675 if (req->status != 0 || req->length == 0)
676 return;
677
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500678 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
679 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
680 func_cb = cmd_dispatch_info[i].cb;
681 break;
682 }
683 }
684
Steve Rae593cbd92014-08-26 11:47:29 -0700685 if (!func_cb) {
686 error("unknown command: %s\n", cmdbuf);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500687 fastboot_tx_write_str("FAILunknown command");
Steve Rae593cbd92014-08-26 11:47:29 -0700688 } else {
Eric Nelsone2140582014-10-01 14:30:56 -0700689 if (req->actual < req->length) {
690 u8 *buf = (u8 *)req->buf;
691 buf[req->actual] = 0;
692 func_cb(ep, req);
693 } else {
694 error("buffer overflow\n");
695 fastboot_tx_write_str("FAILbuffer overflow");
696 }
Steve Rae593cbd92014-08-26 11:47:29 -0700697 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500698
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200699 *cmdbuf = '\0';
700 req->actual = 0;
701 usb_ep_queue(ep, req, 0);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500702}