blob: 28b244a8d002c1c6d15c8372477dd755e0e35abe [file] [log] [blame]
Sebastian Siewior9d4471e2014-05-05 15:08:10 -05001/*
2 * (C) Copyright 2008 - 2009
3 * Windriver, <www.windriver.com>
4 * Tom Rix <Tom.Rix@windriver.com>
5 *
6 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
7 *
8 * Copyright 2014 Linaro, Ltd.
9 * Rob Herring <robh@kernel.org>
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 */
Steve Rae56ad7922014-08-26 11:47:29 -070013#include <config.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050014#include <common.h>
15#include <errno.h>
Maxime Ripard00e8eb72015-10-15 14:34:13 +020016#include <fastboot.h>
Sebastian Siewior9d4471e2014-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 Raebfb9ba42014-08-26 11:47:28 -070024#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
25#include <fb_mmc.h>
26#endif
Maxime Ripard33602a92015-10-15 14:34:17 +020027#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
28#include <fb_nand.h>
29#endif
Sebastian Siewior9d4471e2014-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 Siewior9d4471e2014-05-05 15:08:10 -050041#define EP_BUFFER_SIZE 4096
Roger Quadrosaefb0212016-04-19 10:16:59 +030042/*
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 */
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050047
48struct f_fastboot {
49 struct usb_function usb_function;
50
Steve Rae56ad7922014-08-26 11:47:29 -070051 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050052 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;
Maxime Ripardaa3eba22015-10-15 14:34:15 +020062static unsigned int fastboot_flash_session_id;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050063static unsigned int download_size;
64static unsigned int download_bytes;
65
66static struct usb_endpoint_descriptor fs_ep_in = {
67 .bLength = USB_DT_ENDPOINT_SIZE,
68 .bDescriptorType = USB_DT_ENDPOINT,
69 .bEndpointAddress = USB_DIR_IN,
70 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +030071 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050072};
73
74static struct usb_endpoint_descriptor fs_ep_out = {
75 .bLength = USB_DT_ENDPOINT_SIZE,
76 .bDescriptorType = USB_DT_ENDPOINT,
77 .bEndpointAddress = USB_DIR_OUT,
78 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +030079 .wMaxPacketSize = cpu_to_le16(64),
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050080};
81
Roger Quadrosbf4b41a2016-04-12 15:51:48 +030082static struct usb_endpoint_descriptor hs_ep_in = {
83 .bLength = USB_DT_ENDPOINT_SIZE,
84 .bDescriptorType = USB_DT_ENDPOINT,
85 .bEndpointAddress = USB_DIR_IN,
86 .bmAttributes = USB_ENDPOINT_XFER_BULK,
87 .wMaxPacketSize = cpu_to_le16(512),
88};
89
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050090static struct usb_endpoint_descriptor hs_ep_out = {
91 .bLength = USB_DT_ENDPOINT_SIZE,
92 .bDescriptorType = USB_DT_ENDPOINT,
93 .bEndpointAddress = USB_DIR_OUT,
94 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +030095 .wMaxPacketSize = cpu_to_le16(512),
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050096};
97
98static struct usb_interface_descriptor interface_desc = {
99 .bLength = USB_DT_INTERFACE_SIZE,
100 .bDescriptorType = USB_DT_INTERFACE,
101 .bInterfaceNumber = 0x00,
102 .bAlternateSetting = 0x00,
103 .bNumEndpoints = 0x02,
104 .bInterfaceClass = FASTBOOT_INTERFACE_CLASS,
105 .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS,
106 .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL,
107};
108
Roger Quadrosbf4b41a2016-04-12 15:51:48 +0300109static struct usb_descriptor_header *fb_fs_function[] = {
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500110 (struct usb_descriptor_header *)&interface_desc,
111 (struct usb_descriptor_header *)&fs_ep_in,
Roger Quadrosbf4b41a2016-04-12 15:51:48 +0300112 (struct usb_descriptor_header *)&fs_ep_out,
113};
114
115static struct usb_descriptor_header *fb_hs_function[] = {
116 (struct usb_descriptor_header *)&interface_desc,
117 (struct usb_descriptor_header *)&hs_ep_in,
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500118 (struct usb_descriptor_header *)&hs_ep_out,
119 NULL,
120};
121
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300122static struct usb_endpoint_descriptor *
123fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
124 struct usb_endpoint_descriptor *hs)
125{
126 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
127 return hs;
128 return fs;
129}
130
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500131/*
132 * static strings, in UTF-8
133 */
134static const char fastboot_name[] = "Android Fastboot";
135
136static struct usb_string fastboot_string_defs[] = {
137 [0].s = fastboot_name,
138 { } /* end of list */
139};
140
141static struct usb_gadget_strings stringtab_fastboot = {
142 .language = 0x0409, /* en-us */
143 .strings = fastboot_string_defs,
144};
145
146static struct usb_gadget_strings *fastboot_strings[] = {
147 &stringtab_fastboot,
148 NULL,
149};
150
151static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
Alexey Firago96da3062015-02-25 17:10:47 +0300152static int strcmp_l1(const char *s1, const char *s2);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500153
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200154
155void fastboot_fail(char *response, const char *reason)
156{
157 strncpy(response, "FAIL\0", 5);
158 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
159}
160
161void fastboot_okay(char *response, const char *reason)
162{
163 strncpy(response, "OKAY\0", 5);
164 strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
165}
166
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500167static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
168{
169 int status = req->status;
170 if (!status)
171 return;
172 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
173}
174
175static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
176{
177 int id;
178 struct usb_gadget *gadget = c->cdev->gadget;
179 struct f_fastboot *f_fb = func_to_fastboot(f);
Dileep Katta447c9752015-02-13 14:33:43 +0800180 const char *s;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500181
182 /* DYNAMIC interface numbers assignments */
183 id = usb_interface_id(c, f);
184 if (id < 0)
185 return id;
186 interface_desc.bInterfaceNumber = id;
187
188 id = usb_string_id(c->cdev);
189 if (id < 0)
190 return id;
191 fastboot_string_defs[0].id = id;
192 interface_desc.iInterface = id;
193
194 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
195 if (!f_fb->in_ep)
196 return -ENODEV;
197 f_fb->in_ep->driver_data = c->cdev;
198
199 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
200 if (!f_fb->out_ep)
201 return -ENODEV;
202 f_fb->out_ep->driver_data = c->cdev;
203
Roger Quadrosbf4b41a2016-04-12 15:51:48 +0300204 f->descriptors = fb_fs_function;
205
206 if (gadget_is_dualspeed(gadget)) {
207 /* Assume endpoint addresses are the same for both speeds */
208 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
209 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
210 /* copy HS descriptors */
211 f->hs_descriptors = fb_hs_function;
212 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500213
Dileep Katta447c9752015-02-13 14:33:43 +0800214 s = getenv("serial#");
215 if (s)
216 g_dnl_set_serialnumber((char *)s);
217
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500218 return 0;
219}
220
221static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
222{
223 memset(fastboot_func, 0, sizeof(*fastboot_func));
224}
225
226static void fastboot_disable(struct usb_function *f)
227{
228 struct f_fastboot *f_fb = func_to_fastboot(f);
229
230 usb_ep_disable(f_fb->out_ep);
231 usb_ep_disable(f_fb->in_ep);
232
233 if (f_fb->out_req) {
234 free(f_fb->out_req->buf);
235 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
236 f_fb->out_req = NULL;
237 }
238 if (f_fb->in_req) {
239 free(f_fb->in_req->buf);
240 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
241 f_fb->in_req = NULL;
242 }
243}
244
245static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
246{
247 struct usb_request *req;
248
249 req = usb_ep_alloc_request(ep, 0);
250 if (!req)
251 return NULL;
252
253 req->length = EP_BUFFER_SIZE;
254 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
255 if (!req->buf) {
256 usb_ep_free_request(ep, req);
257 return NULL;
258 }
259
260 memset(req->buf, 0, req->length);
261 return req;
262}
263
264static int fastboot_set_alt(struct usb_function *f,
265 unsigned interface, unsigned alt)
266{
267 int ret;
268 struct usb_composite_dev *cdev = f->config->cdev;
269 struct usb_gadget *gadget = cdev->gadget;
270 struct f_fastboot *f_fb = func_to_fastboot(f);
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300271 const struct usb_endpoint_descriptor *d;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500272
273 debug("%s: func: %s intf: %d alt: %d\n",
274 __func__, f->name, interface, alt);
275
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300276 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
277 ret = usb_ep_enable(f_fb->out_ep, d);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500278 if (ret) {
279 puts("failed to enable out ep\n");
280 return ret;
281 }
282
283 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
284 if (!f_fb->out_req) {
285 puts("failed to alloc out req\n");
286 ret = -EINVAL;
287 goto err;
288 }
289 f_fb->out_req->complete = rx_handler_command;
290
Roger Quadros9ab3fdf2016-04-13 11:30:00 +0300291 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
292 ret = usb_ep_enable(f_fb->in_ep, d);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500293 if (ret) {
294 puts("failed to enable in ep\n");
295 goto err;
296 }
297
298 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
299 if (!f_fb->in_req) {
300 puts("failed alloc req in\n");
301 ret = -EINVAL;
302 goto err;
303 }
304 f_fb->in_req->complete = fastboot_complete;
305
306 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
307 if (ret)
308 goto err;
309
310 return 0;
311err:
312 fastboot_disable(f);
313 return ret;
314}
315
316static int fastboot_add(struct usb_configuration *c)
317{
318 struct f_fastboot *f_fb = fastboot_func;
319 int status;
320
321 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
322
323 if (!f_fb) {
324 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
325 if (!f_fb)
326 return -ENOMEM;
327
328 fastboot_func = f_fb;
329 memset(f_fb, 0, sizeof(*f_fb));
330 }
331
332 f_fb->usb_function.name = "f_fastboot";
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500333 f_fb->usb_function.bind = fastboot_bind;
334 f_fb->usb_function.unbind = fastboot_unbind;
335 f_fb->usb_function.set_alt = fastboot_set_alt;
336 f_fb->usb_function.disable = fastboot_disable;
337 f_fb->usb_function.strings = fastboot_strings;
338
339 status = usb_add_function(c, &f_fb->usb_function);
340 if (status) {
341 free(f_fb);
342 fastboot_func = f_fb;
343 }
344
345 return status;
346}
347DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
348
Steve Rae56ad7922014-08-26 11:47:29 -0700349static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500350{
351 struct usb_request *in_req = fastboot_func->in_req;
352 int ret;
353
354 memcpy(in_req->buf, buffer, buffer_size);
355 in_req->length = buffer_size;
Paul Kocialkowski2032bfd2015-07-04 16:46:16 +0200356
357 usb_ep_dequeue(fastboot_func->in_ep, in_req);
358
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500359 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
360 if (ret)
361 printf("Error %d on queue\n", ret);
362 return 0;
363}
364
365static int fastboot_tx_write_str(const char *buffer)
366{
367 return fastboot_tx_write(buffer, strlen(buffer));
368}
369
370static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
371{
372 do_reset(NULL, 0, 0, NULL);
373}
374
Alexey Firago96da3062015-02-25 17:10:47 +0300375int __weak fb_set_reboot_flag(void)
376{
377 return -ENOSYS;
378}
379
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500380static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
381{
Alexey Firago96da3062015-02-25 17:10:47 +0300382 char *cmd = req->buf;
383 if (!strcmp_l1("reboot-bootloader", cmd)) {
384 if (fb_set_reboot_flag()) {
385 fastboot_tx_write_str("FAILCannot set reboot flag");
386 return;
387 }
388 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500389 fastboot_func->in_req->complete = compl_do_reset;
390 fastboot_tx_write_str("OKAY");
391}
392
393static int strcmp_l1(const char *s1, const char *s2)
394{
395 if (!s1 || !s2)
396 return -1;
397 return strncmp(s1, s2, strlen(s1));
398}
399
400static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
401{
402 char *cmd = req->buf;
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200403 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500404 const char *s;
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200405 size_t chars_left;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500406
407 strcpy(response, "OKAY");
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200408 chars_left = sizeof(response) - strlen(response) - 1;
409
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500410 strsep(&cmd, ":");
411 if (!cmd) {
Steve Raee76f86a2016-01-27 15:02:41 -0800412 error("missing variable");
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500413 fastboot_tx_write_str("FAILmissing var");
414 return;
415 }
416
417 if (!strcmp_l1("version", cmd)) {
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200418 strncat(response, FASTBOOT_VERSION, chars_left);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500419 } else if (!strcmp_l1("bootloader-version", cmd)) {
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200420 strncat(response, U_BOOT_VERSION, chars_left);
Eric Nelson70567d9a2014-09-30 12:05:40 -0700421 } else if (!strcmp_l1("downloadsize", cmd) ||
422 !strcmp_l1("max-download-size", cmd)) {
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500423 char str_num[12];
424
Paul Kocialkowskif84f0912015-07-20 12:38:22 +0200425 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200426 strncat(response, str_num, chars_left);
Maxime Ripardaa3eba22015-10-15 14:34:15 +0200427
428 /*
429 * This also indicates the start of a new flashing
430 * "session", in which we could have 1-N buffers to
431 * write to a partition.
432 *
433 * Reset our session counter.
434 */
435 fastboot_flash_session_id = 0;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500436 } else if (!strcmp_l1("serialno", cmd)) {
437 s = getenv("serial#");
438 if (s)
Jeroen Hofstee36a6b402014-06-14 00:57:14 +0200439 strncat(response, s, chars_left);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500440 else
441 strcpy(response, "FAILValue not set");
442 } else {
Rob Herringec00a0d2016-03-17 17:21:23 +0100443 char envstr[32];
444
445 snprintf(envstr, sizeof(envstr) - 1, "fastboot.%s", cmd);
446 s = getenv(envstr);
447 if (s) {
448 strncat(response, s, chars_left);
449 } else {
450 printf("WARNING: unknown variable: %s\n", cmd);
451 strcpy(response, "FAILVariable not implemented");
452 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500453 }
454 fastboot_tx_write_str(response);
455}
456
Roger Quadrosaefb0212016-04-19 10:16:59 +0300457static unsigned int rx_bytes_expected(struct usb_ep *ep)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500458{
459 int rx_remain = download_size - download_bytes;
Roger Quadrosaefb0212016-04-19 10:16:59 +0300460 unsigned int rem;
461 unsigned int maxpacket = ep->maxpacket;
462
463 if (rx_remain <= 0)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500464 return 0;
Roger Quadrosaefb0212016-04-19 10:16:59 +0300465 else if (rx_remain > EP_BUFFER_SIZE)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500466 return EP_BUFFER_SIZE;
Roger Quadrosaefb0212016-04-19 10:16:59 +0300467
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)
Dileep Katta50b4c172015-02-17 02:02:36 +0530476 rx_remain = rx_remain + (maxpacket - rem);
Roger Quadrosaefb0212016-04-19 10:16:59 +0300477
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500478 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{
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200484 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500485 unsigned int transfer_size = download_size - download_bytes;
486 const unsigned char *buffer = req->buf;
487 unsigned int buffer_size = req->actual;
Bo Shenc65503a2014-09-19 14:15:12 +0800488 unsigned int pre_dot_num, now_dot_num;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500489
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
Paul Kocialkowskif84f0912015-07-20 12:38:22 +0200498 memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500499 buffer, transfer_size);
500
Bo Shenc65503a2014-09-19 14:15:12 +0800501 pre_dot_num = download_bytes / BYTES_PER_DOT;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500502 download_bytes += transfer_size;
Bo Shenc65503a2014-09-19 14:15:12 +0800503 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 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500510
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
Ben Whitten34fd6c92015-12-30 13:05:58 +0000521 strcpy(response, "OKAY");
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500522 fastboot_tx_write_str(response);
523
524 printf("\ndownloading of %d bytes finished\n", download_bytes);
525 } else {
Roger Quadrosaefb0212016-04-19 10:16:59 +0300526 req->length = rx_bytes_expected(ep);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500527 }
528
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500529 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;
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200536 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500537
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) {
Ben Whitten34fd6c92015-12-30 13:05:58 +0000545 strcpy(response, "FAILdata invalid size");
Paul Kocialkowskif84f0912015-07-20 12:38:22 +0200546 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500547 download_size = 0;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000548 strcpy(response, "FAILdata too large");
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500549 } else {
550 sprintf(response, "DATA%08x", download_size);
551 req->complete = rx_handler_dl_image;
Roger Quadrosaefb0212016-04-19 10:16:59 +0300552 req->length = rx_bytes_expected(ep);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500553 }
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
564 sprintf(boot_addr_start, "0x%lx", load_addr);
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
Rob Herring5aba3982014-12-10 14:43:04 -0600577static 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
Steve Raebfb9ba42014-08-26 11:47:28 -0700588#ifdef CONFIG_FASTBOOT_FLASH
589static void cb_flash(struct usb_ep *ep, struct usb_request *req)
590{
591 char *cmd = req->buf;
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200592 char response[FASTBOOT_RESPONSE_LEN];
Steve Raebfb9ba42014-08-26 11:47:28 -0700593
594 strsep(&cmd, ":");
595 if (!cmd) {
Steve Raee76f86a2016-01-27 15:02:41 -0800596 error("missing partition name");
Steve Raebfb9ba42014-08-26 11:47:28 -0700597 fastboot_tx_write_str("FAILmissing partition name");
598 return;
599 }
600
601 strcpy(response, "FAILno flash device defined");
602#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
Maxime Ripardaa3eba22015-10-15 14:34:15 +0200603 fb_mmc_flash_write(cmd, fastboot_flash_session_id,
604 (void *)CONFIG_FASTBOOT_BUF_ADDR,
Steve Raebfb9ba42014-08-26 11:47:28 -0700605 download_bytes, response);
606#endif
Maxime Ripard33602a92015-10-15 14:34:17 +0200607#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
608 fb_nand_flash_write(cmd, fastboot_flash_session_id,
609 (void *)CONFIG_FASTBOOT_BUF_ADDR,
610 download_bytes, response);
611#endif
Maxime Ripardaa3eba22015-10-15 14:34:15 +0200612 fastboot_flash_session_id++;
Steve Raebfb9ba42014-08-26 11:47:28 -0700613 fastboot_tx_write_str(response);
614}
615#endif
616
Michael Scott7e23abf2015-01-26 15:49:00 -0600617static void cb_oem(struct usb_ep *ep, struct usb_request *req)
618{
619 char *cmd = req->buf;
Maxime Ripard78a52ca2015-10-15 22:04:04 +0200620#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
Rob Herring13047e52015-01-26 15:49:01 -0600621 if (strncmp("format", cmd + 4, 6) == 0) {
622 char cmdbuf[32];
623 sprintf(cmdbuf, "gpt write mmc %x $partitions",
624 CONFIG_FASTBOOT_FLASH_MMC_DEV);
625 if (run_command(cmdbuf, 0))
626 fastboot_tx_write_str("FAIL");
627 else
628 fastboot_tx_write_str("OKAY");
629 } else
630#endif
Michael Scott7e23abf2015-01-26 15:49:00 -0600631 if (strncmp("unlock", cmd + 4, 8) == 0) {
632 fastboot_tx_write_str("FAILnot implemented");
633 }
634 else {
635 fastboot_tx_write_str("FAILunknown oem command");
636 }
637}
638
Dileep Kattab1228202015-02-17 18:48:23 +0530639#ifdef CONFIG_FASTBOOT_FLASH
640static void cb_erase(struct usb_ep *ep, struct usb_request *req)
641{
642 char *cmd = req->buf;
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200643 char response[FASTBOOT_RESPONSE_LEN];
Dileep Kattab1228202015-02-17 18:48:23 +0530644
645 strsep(&cmd, ":");
646 if (!cmd) {
647 error("missing partition name");
648 fastboot_tx_write_str("FAILmissing partition name");
649 return;
650 }
651
652 strcpy(response, "FAILno flash device defined");
653
654#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
655 fb_mmc_erase(cmd, response);
656#endif
Maxime Ripard33602a92015-10-15 14:34:17 +0200657#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
658 fb_nand_erase(cmd, response);
659#endif
Dileep Kattab1228202015-02-17 18:48:23 +0530660 fastboot_tx_write_str(response);
661}
662#endif
663
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500664struct cmd_dispatch_info {
665 char *cmd;
666 void (*cb)(struct usb_ep *ep, struct usb_request *req);
667};
668
669static const struct cmd_dispatch_info cmd_dispatch_info[] = {
670 {
671 .cmd = "reboot",
672 .cb = cb_reboot,
673 }, {
674 .cmd = "getvar:",
675 .cb = cb_getvar,
676 }, {
677 .cmd = "download:",
678 .cb = cb_download,
679 }, {
680 .cmd = "boot",
681 .cb = cb_boot,
Rob Herring5aba3982014-12-10 14:43:04 -0600682 }, {
683 .cmd = "continue",
684 .cb = cb_continue,
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500685 },
Steve Raebfb9ba42014-08-26 11:47:28 -0700686#ifdef CONFIG_FASTBOOT_FLASH
687 {
688 .cmd = "flash",
689 .cb = cb_flash,
Dileep Kattab1228202015-02-17 18:48:23 +0530690 }, {
691 .cmd = "erase",
692 .cb = cb_erase,
Steve Raebfb9ba42014-08-26 11:47:28 -0700693 },
694#endif
Michael Scott7e23abf2015-01-26 15:49:00 -0600695 {
696 .cmd = "oem",
697 .cb = cb_oem,
698 },
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500699};
700
701static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
702{
703 char *cmdbuf = req->buf;
704 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
705 int i;
706
Paul Kocialkowski4a126b62015-07-04 16:46:15 +0200707 if (req->status != 0 || req->length == 0)
708 return;
709
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500710 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
711 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
712 func_cb = cmd_dispatch_info[i].cb;
713 break;
714 }
715 }
716
Steve Rae56ad7922014-08-26 11:47:29 -0700717 if (!func_cb) {
Steve Raee76f86a2016-01-27 15:02:41 -0800718 error("unknown command: %s", cmdbuf);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500719 fastboot_tx_write_str("FAILunknown command");
Steve Rae56ad7922014-08-26 11:47:29 -0700720 } else {
Eric Nelson3d626cd2014-10-01 14:30:56 -0700721 if (req->actual < req->length) {
722 u8 *buf = (u8 *)req->buf;
723 buf[req->actual] = 0;
724 func_cb(ep, req);
725 } else {
Steve Raee76f86a2016-01-27 15:02:41 -0800726 error("buffer overflow");
Eric Nelson3d626cd2014-10-01 14:30:56 -0700727 fastboot_tx_write_str("FAILbuffer overflow");
728 }
Steve Rae56ad7922014-08-26 11:47:29 -0700729 }
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500730
Paul Kocialkowski4a126b62015-07-04 16:46:15 +0200731 *cmdbuf = '\0';
732 req->actual = 0;
733 usb_ep_queue(ep, req, 0);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500734}