blob: 07d6a6215546c7f5ba2c41935d3daf91adf155ca [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior3aab70a2014-05-05 15:08:10 -05002/*
3 * (C) Copyright 2008 - 2009
4 * Windriver, <www.windriver.com>
5 * Tom Rix <Tom.Rix@windriver.com>
6 *
7 * Copyright 2011 Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Copyright 2014 Linaro, Ltd.
10 * Rob Herring <robh@kernel.org>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050011 */
Steve Rae593cbd92014-08-26 11:47:29 -070012#include <config.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050013#include <common.h>
14#include <errno.h>
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020015#include <fastboot.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050016#include <malloc.h>
17#include <linux/usb/ch9.h>
18#include <linux/usb/gadget.h>
19#include <linux/usb/composite.h>
20#include <linux/compiler.h>
21#include <version.h>
22#include <g_dnl.h>
Alex Kiernan42d8dd42018-05-29 15:30:42 +000023#ifdef CONFIG_FASTBOOT_FLASH_MMC
Steve Raed1b5ed02014-08-26 11:47:28 -070024#include <fb_mmc.h>
25#endif
Alex Kiernan42d8dd42018-05-29 15:30:42 +000026#ifdef CONFIG_FASTBOOT_FLASH_NAND
Maxime Ripardbf8940d2015-10-15 14:34:17 +020027#include <fb_nand.h>
28#endif
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050029
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050030#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
Roger Quadrosac484c52016-04-19 10:16:59 +030039/*
40 * EP_BUFFER_SIZE must always be an integral multiple of maxpacket size
41 * (64 or 512 or 1024), else we break on certain controllers like DWC3
42 * that expect bulk OUT requests to be divisible by maxpacket size.
43 */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050044
45struct f_fastboot {
46 struct usb_function usb_function;
47
Steve Rae593cbd92014-08-26 11:47:29 -070048 /* IN/OUT EP's and corresponding requests */
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050049 struct usb_ep *in_ep, *out_ep;
50 struct usb_request *in_req, *out_req;
51};
52
53static inline struct f_fastboot *func_to_fastboot(struct usb_function *f)
54{
55 return container_of(f, struct f_fastboot, usb_function);
56}
57
58static struct f_fastboot *fastboot_func;
59static unsigned int download_size;
60static unsigned int download_bytes;
61
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
Roger Quadros8b704a02016-04-13 11:30:00 +0300118static struct usb_endpoint_descriptor *
119fb_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
120 struct usb_endpoint_descriptor *hs)
121{
122 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
123 return hs;
124 return fs;
125}
126
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500127/*
128 * static strings, in UTF-8
129 */
130static const char fastboot_name[] = "Android Fastboot";
131
132static struct usb_string fastboot_string_defs[] = {
133 [0].s = fastboot_name,
134 { } /* end of list */
135};
136
137static struct usb_gadget_strings stringtab_fastboot = {
138 .language = 0x0409, /* en-us */
139 .strings = fastboot_string_defs,
140};
141
142static struct usb_gadget_strings *fastboot_strings[] = {
143 &stringtab_fastboot,
144 NULL,
145};
146
147static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300148static int strcmp_l1(const char *s1, const char *s2);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500149
150static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
151{
152 int status = req->status;
153 if (!status)
154 return;
155 printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual);
156}
157
158static int fastboot_bind(struct usb_configuration *c, struct usb_function *f)
159{
160 int id;
161 struct usb_gadget *gadget = c->cdev->gadget;
162 struct f_fastboot *f_fb = func_to_fastboot(f);
Dileep Katta537cd072015-02-13 14:33:43 +0800163 const char *s;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500164
165 /* DYNAMIC interface numbers assignments */
166 id = usb_interface_id(c, f);
167 if (id < 0)
168 return id;
169 interface_desc.bInterfaceNumber = id;
170
171 id = usb_string_id(c->cdev);
172 if (id < 0)
173 return id;
174 fastboot_string_defs[0].id = id;
175 interface_desc.iInterface = id;
176
177 f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in);
178 if (!f_fb->in_ep)
179 return -ENODEV;
180 f_fb->in_ep->driver_data = c->cdev;
181
182 f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out);
183 if (!f_fb->out_ep)
184 return -ENODEV;
185 f_fb->out_ep->driver_data = c->cdev;
186
Roger Quadros718156a2016-04-12 15:51:48 +0300187 f->descriptors = fb_fs_function;
188
189 if (gadget_is_dualspeed(gadget)) {
190 /* Assume endpoint addresses are the same for both speeds */
191 hs_ep_in.bEndpointAddress = fs_ep_in.bEndpointAddress;
192 hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress;
193 /* copy HS descriptors */
194 f->hs_descriptors = fb_hs_function;
195 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500196
Simon Glass00caae62017-08-03 12:22:12 -0600197 s = env_get("serial#");
Dileep Katta537cd072015-02-13 14:33:43 +0800198 if (s)
199 g_dnl_set_serialnumber((char *)s);
200
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500201 return 0;
202}
203
204static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f)
205{
206 memset(fastboot_func, 0, sizeof(*fastboot_func));
207}
208
209static void fastboot_disable(struct usb_function *f)
210{
211 struct f_fastboot *f_fb = func_to_fastboot(f);
212
213 usb_ep_disable(f_fb->out_ep);
214 usb_ep_disable(f_fb->in_ep);
215
216 if (f_fb->out_req) {
217 free(f_fb->out_req->buf);
218 usb_ep_free_request(f_fb->out_ep, f_fb->out_req);
219 f_fb->out_req = NULL;
220 }
221 if (f_fb->in_req) {
222 free(f_fb->in_req->buf);
223 usb_ep_free_request(f_fb->in_ep, f_fb->in_req);
224 f_fb->in_req = NULL;
225 }
226}
227
228static struct usb_request *fastboot_start_ep(struct usb_ep *ep)
229{
230 struct usb_request *req;
231
232 req = usb_ep_alloc_request(ep, 0);
233 if (!req)
234 return NULL;
235
236 req->length = EP_BUFFER_SIZE;
237 req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE);
238 if (!req->buf) {
239 usb_ep_free_request(ep, req);
240 return NULL;
241 }
242
243 memset(req->buf, 0, req->length);
244 return req;
245}
246
247static int fastboot_set_alt(struct usb_function *f,
248 unsigned interface, unsigned alt)
249{
250 int ret;
251 struct usb_composite_dev *cdev = f->config->cdev;
252 struct usb_gadget *gadget = cdev->gadget;
253 struct f_fastboot *f_fb = func_to_fastboot(f);
Roger Quadros8b704a02016-04-13 11:30:00 +0300254 const struct usb_endpoint_descriptor *d;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500255
256 debug("%s: func: %s intf: %d alt: %d\n",
257 __func__, f->name, interface, alt);
258
Roger Quadros8b704a02016-04-13 11:30:00 +0300259 d = fb_ep_desc(gadget, &fs_ep_out, &hs_ep_out);
260 ret = usb_ep_enable(f_fb->out_ep, d);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500261 if (ret) {
262 puts("failed to enable out ep\n");
263 return ret;
264 }
265
266 f_fb->out_req = fastboot_start_ep(f_fb->out_ep);
267 if (!f_fb->out_req) {
268 puts("failed to alloc out req\n");
269 ret = -EINVAL;
270 goto err;
271 }
272 f_fb->out_req->complete = rx_handler_command;
273
Roger Quadros8b704a02016-04-13 11:30:00 +0300274 d = fb_ep_desc(gadget, &fs_ep_in, &hs_ep_in);
275 ret = usb_ep_enable(f_fb->in_ep, d);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500276 if (ret) {
277 puts("failed to enable in ep\n");
278 goto err;
279 }
280
281 f_fb->in_req = fastboot_start_ep(f_fb->in_ep);
282 if (!f_fb->in_req) {
283 puts("failed alloc req in\n");
284 ret = -EINVAL;
285 goto err;
286 }
287 f_fb->in_req->complete = fastboot_complete;
288
289 ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0);
290 if (ret)
291 goto err;
292
293 return 0;
294err:
295 fastboot_disable(f);
296 return ret;
297}
298
299static int fastboot_add(struct usb_configuration *c)
300{
301 struct f_fastboot *f_fb = fastboot_func;
302 int status;
303
304 debug("%s: cdev: 0x%p\n", __func__, c->cdev);
305
306 if (!f_fb) {
307 f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb));
308 if (!f_fb)
309 return -ENOMEM;
310
311 fastboot_func = f_fb;
312 memset(f_fb, 0, sizeof(*f_fb));
313 }
314
315 f_fb->usb_function.name = "f_fastboot";
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500316 f_fb->usb_function.bind = fastboot_bind;
317 f_fb->usb_function.unbind = fastboot_unbind;
318 f_fb->usb_function.set_alt = fastboot_set_alt;
319 f_fb->usb_function.disable = fastboot_disable;
320 f_fb->usb_function.strings = fastboot_strings;
321
322 status = usb_add_function(c, &f_fb->usb_function);
323 if (status) {
324 free(f_fb);
325 fastboot_func = f_fb;
326 }
327
328 return status;
329}
330DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add);
331
Steve Rae593cbd92014-08-26 11:47:29 -0700332static int fastboot_tx_write(const char *buffer, unsigned int buffer_size)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500333{
334 struct usb_request *in_req = fastboot_func->in_req;
335 int ret;
336
337 memcpy(in_req->buf, buffer, buffer_size);
338 in_req->length = buffer_size;
Paul Kocialkowskibc9071c2015-07-04 16:46:16 +0200339
340 usb_ep_dequeue(fastboot_func->in_ep, in_req);
341
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500342 ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0);
343 if (ret)
344 printf("Error %d on queue\n", ret);
345 return 0;
346}
347
348static int fastboot_tx_write_str(const char *buffer)
349{
350 return fastboot_tx_write(buffer, strlen(buffer));
351}
352
353static void compl_do_reset(struct usb_ep *ep, struct usb_request *req)
354{
355 do_reset(NULL, 0, 0, NULL);
356}
357
358static void cb_reboot(struct usb_ep *ep, struct usb_request *req)
359{
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300360 char *cmd = req->buf;
361 if (!strcmp_l1("reboot-bootloader", cmd)) {
Alex Kiernan8a65bd62018-05-29 15:30:46 +0000362 if (fastboot_set_reboot_flag()) {
Alexey Firagoe2ec3e42015-02-25 17:10:47 +0300363 fastboot_tx_write_str("FAILCannot set reboot flag");
364 return;
365 }
366 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500367 fastboot_func->in_req->complete = compl_do_reset;
368 fastboot_tx_write_str("OKAY");
369}
370
371static int strcmp_l1(const char *s1, const char *s2)
372{
373 if (!s1 || !s2)
374 return -1;
375 return strncmp(s1, s2, strlen(s1));
376}
377
378static void cb_getvar(struct usb_ep *ep, struct usb_request *req)
379{
380 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200381 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500382 const char *s;
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200383 size_t chars_left;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500384
385 strcpy(response, "OKAY");
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200386 chars_left = sizeof(response) - strlen(response) - 1;
387
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500388 strsep(&cmd, ":");
389 if (!cmd) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900390 pr_err("missing variable");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500391 fastboot_tx_write_str("FAILmissing var");
392 return;
393 }
394
395 if (!strcmp_l1("version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200396 strncat(response, FASTBOOT_VERSION, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500397 } else if (!strcmp_l1("bootloader-version", cmd)) {
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200398 strncat(response, U_BOOT_VERSION, chars_left);
Eric Nelsonc674a662014-09-30 12:05:40 -0700399 } else if (!strcmp_l1("downloadsize", cmd) ||
400 !strcmp_l1("max-download-size", cmd)) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500401 char str_num[12];
402
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200403 sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE);
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200404 strncat(response, str_num, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500405 } else if (!strcmp_l1("serialno", cmd)) {
Simon Glass00caae62017-08-03 12:22:12 -0600406 s = env_get("serial#");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500407 if (s)
Jeroen Hofstee29425be2014-06-14 00:57:14 +0200408 strncat(response, s, chars_left);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500409 else
410 strcpy(response, "FAILValue not set");
411 } else {
nicolas.le.bayon@st.comb1f2a172017-05-09 15:58:36 +0000412 char *envstr;
Rob Herring74322202016-03-17 17:21:23 +0100413
nicolas.le.bayon@st.comb1f2a172017-05-09 15:58:36 +0000414 envstr = malloc(strlen("fastboot.") + strlen(cmd) + 1);
415 if (!envstr) {
416 fastboot_tx_write_str("FAILmalloc error");
417 return;
418 }
419
420 sprintf(envstr, "fastboot.%s", cmd);
Simon Glass00caae62017-08-03 12:22:12 -0600421 s = env_get(envstr);
Rob Herring74322202016-03-17 17:21:23 +0100422 if (s) {
423 strncat(response, s, chars_left);
424 } else {
425 printf("WARNING: unknown variable: %s\n", cmd);
426 strcpy(response, "FAILVariable not implemented");
427 }
nicolas.le.bayon@st.comb1f2a172017-05-09 15:58:36 +0000428
429 free(envstr);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500430 }
431 fastboot_tx_write_str(response);
432}
433
Roger Quadrosac484c52016-04-19 10:16:59 +0300434static unsigned int rx_bytes_expected(struct usb_ep *ep)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500435{
436 int rx_remain = download_size - download_bytes;
Roger Quadrosac484c52016-04-19 10:16:59 +0300437 unsigned int rem;
438 unsigned int maxpacket = ep->maxpacket;
439
440 if (rx_remain <= 0)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500441 return 0;
Roger Quadrosac484c52016-04-19 10:16:59 +0300442 else if (rx_remain > EP_BUFFER_SIZE)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500443 return EP_BUFFER_SIZE;
Roger Quadrosac484c52016-04-19 10:16:59 +0300444
445 /*
446 * Some controllers e.g. DWC3 don't like OUT transfers to be
447 * not ending in maxpacket boundary. So just make them happy by
448 * always requesting for integral multiple of maxpackets.
449 * This shouldn't bother controllers that don't care about it.
450 */
451 rem = rx_remain % maxpacket;
452 if (rem > 0)
Dileep Katta9e4b5102015-02-17 02:02:36 +0530453 rx_remain = rx_remain + (maxpacket - rem);
Roger Quadrosac484c52016-04-19 10:16:59 +0300454
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500455 return rx_remain;
456}
457
458#define BYTES_PER_DOT 0x20000
459static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req)
460{
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200461 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500462 unsigned int transfer_size = download_size - download_bytes;
463 const unsigned char *buffer = req->buf;
464 unsigned int buffer_size = req->actual;
Bo Shen23d1d102014-09-19 14:15:12 +0800465 unsigned int pre_dot_num, now_dot_num;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500466
467 if (req->status != 0) {
468 printf("Bad status: %d\n", req->status);
469 return;
470 }
471
472 if (buffer_size < transfer_size)
473 transfer_size = buffer_size;
474
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200475 memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500476 buffer, transfer_size);
477
Bo Shen23d1d102014-09-19 14:15:12 +0800478 pre_dot_num = download_bytes / BYTES_PER_DOT;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500479 download_bytes += transfer_size;
Bo Shen23d1d102014-09-19 14:15:12 +0800480 now_dot_num = download_bytes / BYTES_PER_DOT;
481
482 if (pre_dot_num != now_dot_num) {
483 putc('.');
484 if (!(now_dot_num % 74))
485 putc('\n');
486 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500487
488 /* Check if transfer is done */
489 if (download_bytes >= download_size) {
490 /*
491 * Reset global transfer variable, keep download_bytes because
492 * it will be used in the next possible flashing command
493 */
494 download_size = 0;
495 req->complete = rx_handler_command;
496 req->length = EP_BUFFER_SIZE;
497
Ben Whitten192bc692015-12-30 13:05:58 +0000498 strcpy(response, "OKAY");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500499 fastboot_tx_write_str(response);
500
501 printf("\ndownloading of %d bytes finished\n", download_bytes);
502 } else {
Roger Quadrosac484c52016-04-19 10:16:59 +0300503 req->length = rx_bytes_expected(ep);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500504 }
505
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500506 req->actual = 0;
507 usb_ep_queue(ep, req, 0);
508}
509
510static void cb_download(struct usb_ep *ep, struct usb_request *req)
511{
512 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200513 char response[FASTBOOT_RESPONSE_LEN];
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500514
515 strsep(&cmd, ":");
516 download_size = simple_strtoul(cmd, NULL, 16);
517 download_bytes = 0;
518
519 printf("Starting download of %d bytes\n", download_size);
520
521 if (0 == download_size) {
Ben Whitten192bc692015-12-30 13:05:58 +0000522 strcpy(response, "FAILdata invalid size");
Paul Kocialkowskia588d992015-07-20 12:38:22 +0200523 } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) {
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500524 download_size = 0;
Ben Whitten192bc692015-12-30 13:05:58 +0000525 strcpy(response, "FAILdata too large");
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500526 } else {
527 sprintf(response, "DATA%08x", download_size);
528 req->complete = rx_handler_dl_image;
Roger Quadrosac484c52016-04-19 10:16:59 +0300529 req->length = rx_bytes_expected(ep);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500530 }
531 fastboot_tx_write_str(response);
532}
533
534static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req)
535{
536 char boot_addr_start[12];
537 char *bootm_args[] = { "bootm", boot_addr_start, NULL };
538
539 puts("Booting kernel..\n");
540
Tom Rini90ae53c2017-08-22 08:20:02 -0400541 sprintf(boot_addr_start, "0x%lx", (long)CONFIG_FASTBOOT_BUF_ADDR);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500542 do_bootm(NULL, 0, 2, bootm_args);
543
544 /* This only happens if image is somehow faulty so we start over */
545 do_reset(NULL, 0, 0, NULL);
546}
547
548static void cb_boot(struct usb_ep *ep, struct usb_request *req)
549{
550 fastboot_func->in_req->complete = do_bootm_on_complete;
551 fastboot_tx_write_str("OKAY");
552}
553
Rob Herring267abc62014-12-10 14:43:04 -0600554static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req)
555{
556 g_dnl_trigger_detach();
557}
558
559static void cb_continue(struct usb_ep *ep, struct usb_request *req)
560{
561 fastboot_func->in_req->complete = do_exit_on_complete;
562 fastboot_tx_write_str("OKAY");
563}
564
Steve Raed1b5ed02014-08-26 11:47:28 -0700565#ifdef CONFIG_FASTBOOT_FLASH
566static void cb_flash(struct usb_ep *ep, struct usb_request *req)
567{
568 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200569 char response[FASTBOOT_RESPONSE_LEN];
Steve Raed1b5ed02014-08-26 11:47:28 -0700570
571 strsep(&cmd, ":");
572 if (!cmd) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900573 pr_err("missing partition name");
Steve Raed1b5ed02014-08-26 11:47:28 -0700574 fastboot_tx_write_str("FAILmissing partition name");
575 return;
576 }
577
Alex Kiernanc4ded032018-05-29 15:30:40 +0000578 fastboot_fail("no flash device defined", response);
Alex Kiernan42d8dd42018-05-29 15:30:42 +0000579#ifdef CONFIG_FASTBOOT_FLASH_MMC
Alex Kiernand1a119d2018-05-29 15:30:48 +0000580 fastboot_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
581 download_bytes, response);
Steve Raed1b5ed02014-08-26 11:47:28 -0700582#endif
Alex Kiernan42d8dd42018-05-29 15:30:42 +0000583#ifdef CONFIG_FASTBOOT_FLASH_NAND
Alex Kiernand1a119d2018-05-29 15:30:48 +0000584 fastboot_nand_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
585 download_bytes, response);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200586#endif
Steve Raed1b5ed02014-08-26 11:47:28 -0700587 fastboot_tx_write_str(response);
588}
589#endif
590
Michael Scottde195622015-01-26 15:49:00 -0600591static void cb_oem(struct usb_ep *ep, struct usb_request *req)
592{
593 char *cmd = req->buf;
Alex Kiernan42d8dd42018-05-29 15:30:42 +0000594#ifdef CONFIG_FASTBOOT_FLASH_MMC
Rob Herring372d7de2015-01-26 15:49:01 -0600595 if (strncmp("format", cmd + 4, 6) == 0) {
596 char cmdbuf[32];
597 sprintf(cmdbuf, "gpt write mmc %x $partitions",
598 CONFIG_FASTBOOT_FLASH_MMC_DEV);
599 if (run_command(cmdbuf, 0))
600 fastboot_tx_write_str("FAIL");
601 else
602 fastboot_tx_write_str("OKAY");
603 } else
604#endif
Michael Scottde195622015-01-26 15:49:00 -0600605 if (strncmp("unlock", cmd + 4, 8) == 0) {
606 fastboot_tx_write_str("FAILnot implemented");
607 }
608 else {
609 fastboot_tx_write_str("FAILunknown oem command");
610 }
611}
612
Dileep Katta89792382015-02-17 18:48:23 +0530613#ifdef CONFIG_FASTBOOT_FLASH
614static void cb_erase(struct usb_ep *ep, struct usb_request *req)
615{
616 char *cmd = req->buf;
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200617 char response[FASTBOOT_RESPONSE_LEN];
Dileep Katta89792382015-02-17 18:48:23 +0530618
619 strsep(&cmd, ":");
620 if (!cmd) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900621 pr_err("missing partition name");
Dileep Katta89792382015-02-17 18:48:23 +0530622 fastboot_tx_write_str("FAILmissing partition name");
623 return;
624 }
625
Alex Kiernanc4ded032018-05-29 15:30:40 +0000626 fastboot_fail("no flash device defined", response);
Alex Kiernan42d8dd42018-05-29 15:30:42 +0000627#ifdef CONFIG_FASTBOOT_FLASH_MMC
Alex Kiernand1a119d2018-05-29 15:30:48 +0000628 fastboot_mmc_erase(cmd, response);
Dileep Katta89792382015-02-17 18:48:23 +0530629#endif
Alex Kiernan42d8dd42018-05-29 15:30:42 +0000630#ifdef CONFIG_FASTBOOT_FLASH_NAND
Alex Kiernand1a119d2018-05-29 15:30:48 +0000631 fastboot_nand_erase(cmd, response);
Maxime Ripardbf8940d2015-10-15 14:34:17 +0200632#endif
Dileep Katta89792382015-02-17 18:48:23 +0530633 fastboot_tx_write_str(response);
634}
635#endif
636
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500637struct cmd_dispatch_info {
638 char *cmd;
639 void (*cb)(struct usb_ep *ep, struct usb_request *req);
640};
641
642static const struct cmd_dispatch_info cmd_dispatch_info[] = {
643 {
644 .cmd = "reboot",
645 .cb = cb_reboot,
646 }, {
647 .cmd = "getvar:",
648 .cb = cb_getvar,
649 }, {
650 .cmd = "download:",
651 .cb = cb_download,
652 }, {
653 .cmd = "boot",
654 .cb = cb_boot,
Rob Herring267abc62014-12-10 14:43:04 -0600655 }, {
656 .cmd = "continue",
657 .cb = cb_continue,
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500658 },
Steve Raed1b5ed02014-08-26 11:47:28 -0700659#ifdef CONFIG_FASTBOOT_FLASH
660 {
661 .cmd = "flash",
662 .cb = cb_flash,
Dileep Katta89792382015-02-17 18:48:23 +0530663 }, {
664 .cmd = "erase",
665 .cb = cb_erase,
Steve Raed1b5ed02014-08-26 11:47:28 -0700666 },
667#endif
Michael Scottde195622015-01-26 15:49:00 -0600668 {
669 .cmd = "oem",
670 .cb = cb_oem,
671 },
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500672};
673
674static void rx_handler_command(struct usb_ep *ep, struct usb_request *req)
675{
676 char *cmdbuf = req->buf;
677 void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL;
678 int i;
679
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200680 if (req->status != 0 || req->length == 0)
681 return;
682
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500683 for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) {
684 if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) {
685 func_cb = cmd_dispatch_info[i].cb;
686 break;
687 }
688 }
689
Steve Rae593cbd92014-08-26 11:47:29 -0700690 if (!func_cb) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900691 pr_err("unknown command: %.*s", req->actual, cmdbuf);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500692 fastboot_tx_write_str("FAILunknown command");
Steve Rae593cbd92014-08-26 11:47:29 -0700693 } else {
Eric Nelsone2140582014-10-01 14:30:56 -0700694 if (req->actual < req->length) {
695 u8 *buf = (u8 *)req->buf;
696 buf[req->actual] = 0;
697 func_cb(ep, req);
698 } else {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900699 pr_err("buffer overflow");
Eric Nelsone2140582014-10-01 14:30:56 -0700700 fastboot_tx_write_str("FAILbuffer overflow");
701 }
Steve Rae593cbd92014-08-26 11:47:29 -0700702 }
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500703
Paul Kocialkowski94b385f2015-07-04 16:46:15 +0200704 *cmdbuf = '\0';
705 req->actual = 0;
706 usb_ep_queue(ep, req, 0);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500707}