Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 1 | /* |
| 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 Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 13 | #include <config.h> |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 14 | #include <common.h> |
| 15 | #include <errno.h> |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 16 | #include <fastboot.h> |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 17 | #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 Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 24 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
| 25 | #include <fb_mmc.h> |
| 26 | #endif |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 27 | |
| 28 | #define FASTBOOT_VERSION "0.4" |
| 29 | |
| 30 | #define FASTBOOT_INTERFACE_CLASS 0xff |
| 31 | #define FASTBOOT_INTERFACE_SUB_CLASS 0x42 |
| 32 | #define FASTBOOT_INTERFACE_PROTOCOL 0x03 |
| 33 | |
| 34 | #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0 (0x0200) |
| 35 | #define RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1 (0x0040) |
| 36 | #define TX_ENDPOINT_MAXIMUM_PACKET_SIZE (0x0040) |
| 37 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 38 | #define EP_BUFFER_SIZE 4096 |
| 39 | |
| 40 | struct f_fastboot { |
| 41 | struct usb_function usb_function; |
| 42 | |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 43 | /* IN/OUT EP's and corresponding requests */ |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 44 | struct usb_ep *in_ep, *out_ep; |
| 45 | struct usb_request *in_req, *out_req; |
| 46 | }; |
| 47 | |
| 48 | static inline struct f_fastboot *func_to_fastboot(struct usb_function *f) |
| 49 | { |
| 50 | return container_of(f, struct f_fastboot, usb_function); |
| 51 | } |
| 52 | |
| 53 | static struct f_fastboot *fastboot_func; |
Maxime Ripard | 6c9e00e | 2015-10-15 14:34:15 +0200 | [diff] [blame^] | 54 | static unsigned int fastboot_flash_session_id; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 55 | static unsigned int download_size; |
| 56 | static unsigned int download_bytes; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 57 | static bool is_high_speed; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 58 | |
| 59 | static struct usb_endpoint_descriptor fs_ep_in = { |
| 60 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 61 | .bDescriptorType = USB_DT_ENDPOINT, |
| 62 | .bEndpointAddress = USB_DIR_IN, |
| 63 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 64 | .wMaxPacketSize = TX_ENDPOINT_MAXIMUM_PACKET_SIZE, |
| 65 | .bInterval = 0x00, |
| 66 | }; |
| 67 | |
| 68 | static struct usb_endpoint_descriptor fs_ep_out = { |
| 69 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 70 | .bDescriptorType = USB_DT_ENDPOINT, |
| 71 | .bEndpointAddress = USB_DIR_OUT, |
| 72 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 73 | .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_1_1, |
| 74 | .bInterval = 0x00, |
| 75 | }; |
| 76 | |
| 77 | static struct usb_endpoint_descriptor hs_ep_out = { |
| 78 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 79 | .bDescriptorType = USB_DT_ENDPOINT, |
| 80 | .bEndpointAddress = USB_DIR_OUT, |
| 81 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 82 | .wMaxPacketSize = RX_ENDPOINT_MAXIMUM_PACKET_SIZE_2_0, |
| 83 | .bInterval = 0x00, |
| 84 | }; |
| 85 | |
| 86 | static struct usb_interface_descriptor interface_desc = { |
| 87 | .bLength = USB_DT_INTERFACE_SIZE, |
| 88 | .bDescriptorType = USB_DT_INTERFACE, |
| 89 | .bInterfaceNumber = 0x00, |
| 90 | .bAlternateSetting = 0x00, |
| 91 | .bNumEndpoints = 0x02, |
| 92 | .bInterfaceClass = FASTBOOT_INTERFACE_CLASS, |
| 93 | .bInterfaceSubClass = FASTBOOT_INTERFACE_SUB_CLASS, |
| 94 | .bInterfaceProtocol = FASTBOOT_INTERFACE_PROTOCOL, |
| 95 | }; |
| 96 | |
| 97 | static struct usb_descriptor_header *fb_runtime_descs[] = { |
| 98 | (struct usb_descriptor_header *)&interface_desc, |
| 99 | (struct usb_descriptor_header *)&fs_ep_in, |
| 100 | (struct usb_descriptor_header *)&hs_ep_out, |
| 101 | NULL, |
| 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * static strings, in UTF-8 |
| 106 | */ |
| 107 | static const char fastboot_name[] = "Android Fastboot"; |
| 108 | |
| 109 | static struct usb_string fastboot_string_defs[] = { |
| 110 | [0].s = fastboot_name, |
| 111 | { } /* end of list */ |
| 112 | }; |
| 113 | |
| 114 | static struct usb_gadget_strings stringtab_fastboot = { |
| 115 | .language = 0x0409, /* en-us */ |
| 116 | .strings = fastboot_string_defs, |
| 117 | }; |
| 118 | |
| 119 | static struct usb_gadget_strings *fastboot_strings[] = { |
| 120 | &stringtab_fastboot, |
| 121 | NULL, |
| 122 | }; |
| 123 | |
| 124 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req); |
Alexey Firago | e2ec3e4 | 2015-02-25 17:10:47 +0300 | [diff] [blame] | 125 | static int strcmp_l1(const char *s1, const char *s2); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 126 | |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 127 | |
| 128 | void fastboot_fail(char *response, const char *reason) |
| 129 | { |
| 130 | strncpy(response, "FAIL\0", 5); |
| 131 | strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1); |
| 132 | } |
| 133 | |
| 134 | void fastboot_okay(char *response, const char *reason) |
| 135 | { |
| 136 | strncpy(response, "OKAY\0", 5); |
| 137 | strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1); |
| 138 | } |
| 139 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 140 | static void fastboot_complete(struct usb_ep *ep, struct usb_request *req) |
| 141 | { |
| 142 | int status = req->status; |
| 143 | if (!status) |
| 144 | return; |
| 145 | printf("status: %d ep '%s' trans: %d\n", status, ep->name, req->actual); |
| 146 | } |
| 147 | |
| 148 | static int fastboot_bind(struct usb_configuration *c, struct usb_function *f) |
| 149 | { |
| 150 | int id; |
| 151 | struct usb_gadget *gadget = c->cdev->gadget; |
| 152 | struct f_fastboot *f_fb = func_to_fastboot(f); |
Dileep Katta | 537cd07 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 153 | const char *s; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 154 | |
| 155 | /* DYNAMIC interface numbers assignments */ |
| 156 | id = usb_interface_id(c, f); |
| 157 | if (id < 0) |
| 158 | return id; |
| 159 | interface_desc.bInterfaceNumber = id; |
| 160 | |
| 161 | id = usb_string_id(c->cdev); |
| 162 | if (id < 0) |
| 163 | return id; |
| 164 | fastboot_string_defs[0].id = id; |
| 165 | interface_desc.iInterface = id; |
| 166 | |
| 167 | f_fb->in_ep = usb_ep_autoconfig(gadget, &fs_ep_in); |
| 168 | if (!f_fb->in_ep) |
| 169 | return -ENODEV; |
| 170 | f_fb->in_ep->driver_data = c->cdev; |
| 171 | |
| 172 | f_fb->out_ep = usb_ep_autoconfig(gadget, &fs_ep_out); |
| 173 | if (!f_fb->out_ep) |
| 174 | return -ENODEV; |
| 175 | f_fb->out_ep->driver_data = c->cdev; |
| 176 | |
| 177 | hs_ep_out.bEndpointAddress = fs_ep_out.bEndpointAddress; |
| 178 | |
Dileep Katta | 537cd07 | 2015-02-13 14:33:43 +0800 | [diff] [blame] | 179 | s = getenv("serial#"); |
| 180 | if (s) |
| 181 | g_dnl_set_serialnumber((char *)s); |
| 182 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static void fastboot_unbind(struct usb_configuration *c, struct usb_function *f) |
| 187 | { |
| 188 | memset(fastboot_func, 0, sizeof(*fastboot_func)); |
| 189 | } |
| 190 | |
| 191 | static void fastboot_disable(struct usb_function *f) |
| 192 | { |
| 193 | struct f_fastboot *f_fb = func_to_fastboot(f); |
| 194 | |
| 195 | usb_ep_disable(f_fb->out_ep); |
| 196 | usb_ep_disable(f_fb->in_ep); |
| 197 | |
| 198 | if (f_fb->out_req) { |
| 199 | free(f_fb->out_req->buf); |
| 200 | usb_ep_free_request(f_fb->out_ep, f_fb->out_req); |
| 201 | f_fb->out_req = NULL; |
| 202 | } |
| 203 | if (f_fb->in_req) { |
| 204 | free(f_fb->in_req->buf); |
| 205 | usb_ep_free_request(f_fb->in_ep, f_fb->in_req); |
| 206 | f_fb->in_req = NULL; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static struct usb_request *fastboot_start_ep(struct usb_ep *ep) |
| 211 | { |
| 212 | struct usb_request *req; |
| 213 | |
| 214 | req = usb_ep_alloc_request(ep, 0); |
| 215 | if (!req) |
| 216 | return NULL; |
| 217 | |
| 218 | req->length = EP_BUFFER_SIZE; |
| 219 | req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, EP_BUFFER_SIZE); |
| 220 | if (!req->buf) { |
| 221 | usb_ep_free_request(ep, req); |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | memset(req->buf, 0, req->length); |
| 226 | return req; |
| 227 | } |
| 228 | |
| 229 | static int fastboot_set_alt(struct usb_function *f, |
| 230 | unsigned interface, unsigned alt) |
| 231 | { |
| 232 | int ret; |
| 233 | struct usb_composite_dev *cdev = f->config->cdev; |
| 234 | struct usb_gadget *gadget = cdev->gadget; |
| 235 | struct f_fastboot *f_fb = func_to_fastboot(f); |
| 236 | |
| 237 | debug("%s: func: %s intf: %d alt: %d\n", |
| 238 | __func__, f->name, interface, alt); |
| 239 | |
| 240 | /* make sure we don't enable the ep twice */ |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 241 | if (gadget->speed == USB_SPEED_HIGH) { |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 242 | ret = usb_ep_enable(f_fb->out_ep, &hs_ep_out); |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 243 | is_high_speed = true; |
| 244 | } else { |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 245 | ret = usb_ep_enable(f_fb->out_ep, &fs_ep_out); |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 246 | is_high_speed = false; |
| 247 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 248 | if (ret) { |
| 249 | puts("failed to enable out ep\n"); |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | f_fb->out_req = fastboot_start_ep(f_fb->out_ep); |
| 254 | if (!f_fb->out_req) { |
| 255 | puts("failed to alloc out req\n"); |
| 256 | ret = -EINVAL; |
| 257 | goto err; |
| 258 | } |
| 259 | f_fb->out_req->complete = rx_handler_command; |
| 260 | |
| 261 | ret = usb_ep_enable(f_fb->in_ep, &fs_ep_in); |
| 262 | if (ret) { |
| 263 | puts("failed to enable in ep\n"); |
| 264 | goto err; |
| 265 | } |
| 266 | |
| 267 | f_fb->in_req = fastboot_start_ep(f_fb->in_ep); |
| 268 | if (!f_fb->in_req) { |
| 269 | puts("failed alloc req in\n"); |
| 270 | ret = -EINVAL; |
| 271 | goto err; |
| 272 | } |
| 273 | f_fb->in_req->complete = fastboot_complete; |
| 274 | |
| 275 | ret = usb_ep_queue(f_fb->out_ep, f_fb->out_req, 0); |
| 276 | if (ret) |
| 277 | goto err; |
| 278 | |
| 279 | return 0; |
| 280 | err: |
| 281 | fastboot_disable(f); |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static int fastboot_add(struct usb_configuration *c) |
| 286 | { |
| 287 | struct f_fastboot *f_fb = fastboot_func; |
| 288 | int status; |
| 289 | |
| 290 | debug("%s: cdev: 0x%p\n", __func__, c->cdev); |
| 291 | |
| 292 | if (!f_fb) { |
| 293 | f_fb = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*f_fb)); |
| 294 | if (!f_fb) |
| 295 | return -ENOMEM; |
| 296 | |
| 297 | fastboot_func = f_fb; |
| 298 | memset(f_fb, 0, sizeof(*f_fb)); |
| 299 | } |
| 300 | |
| 301 | f_fb->usb_function.name = "f_fastboot"; |
| 302 | f_fb->usb_function.hs_descriptors = fb_runtime_descs; |
| 303 | f_fb->usb_function.bind = fastboot_bind; |
| 304 | f_fb->usb_function.unbind = fastboot_unbind; |
| 305 | f_fb->usb_function.set_alt = fastboot_set_alt; |
| 306 | f_fb->usb_function.disable = fastboot_disable; |
| 307 | f_fb->usb_function.strings = fastboot_strings; |
| 308 | |
| 309 | status = usb_add_function(c, &f_fb->usb_function); |
| 310 | if (status) { |
| 311 | free(f_fb); |
| 312 | fastboot_func = f_fb; |
| 313 | } |
| 314 | |
| 315 | return status; |
| 316 | } |
| 317 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_fastboot, fastboot_add); |
| 318 | |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 319 | static int fastboot_tx_write(const char *buffer, unsigned int buffer_size) |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 320 | { |
| 321 | struct usb_request *in_req = fastboot_func->in_req; |
| 322 | int ret; |
| 323 | |
| 324 | memcpy(in_req->buf, buffer, buffer_size); |
| 325 | in_req->length = buffer_size; |
Paul Kocialkowski | bc9071c | 2015-07-04 16:46:16 +0200 | [diff] [blame] | 326 | |
| 327 | usb_ep_dequeue(fastboot_func->in_ep, in_req); |
| 328 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 329 | ret = usb_ep_queue(fastboot_func->in_ep, in_req, 0); |
| 330 | if (ret) |
| 331 | printf("Error %d on queue\n", ret); |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static int fastboot_tx_write_str(const char *buffer) |
| 336 | { |
| 337 | return fastboot_tx_write(buffer, strlen(buffer)); |
| 338 | } |
| 339 | |
| 340 | static void compl_do_reset(struct usb_ep *ep, struct usb_request *req) |
| 341 | { |
| 342 | do_reset(NULL, 0, 0, NULL); |
| 343 | } |
| 344 | |
Alexey Firago | e2ec3e4 | 2015-02-25 17:10:47 +0300 | [diff] [blame] | 345 | int __weak fb_set_reboot_flag(void) |
| 346 | { |
| 347 | return -ENOSYS; |
| 348 | } |
| 349 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 350 | static void cb_reboot(struct usb_ep *ep, struct usb_request *req) |
| 351 | { |
Alexey Firago | e2ec3e4 | 2015-02-25 17:10:47 +0300 | [diff] [blame] | 352 | char *cmd = req->buf; |
| 353 | if (!strcmp_l1("reboot-bootloader", cmd)) { |
| 354 | if (fb_set_reboot_flag()) { |
| 355 | fastboot_tx_write_str("FAILCannot set reboot flag"); |
| 356 | return; |
| 357 | } |
| 358 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 359 | fastboot_func->in_req->complete = compl_do_reset; |
| 360 | fastboot_tx_write_str("OKAY"); |
| 361 | } |
| 362 | |
| 363 | static int strcmp_l1(const char *s1, const char *s2) |
| 364 | { |
| 365 | if (!s1 || !s2) |
| 366 | return -1; |
| 367 | return strncmp(s1, s2, strlen(s1)); |
| 368 | } |
| 369 | |
| 370 | static void cb_getvar(struct usb_ep *ep, struct usb_request *req) |
| 371 | { |
| 372 | char *cmd = req->buf; |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 373 | char response[FASTBOOT_RESPONSE_LEN]; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 374 | const char *s; |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 375 | size_t chars_left; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 376 | |
| 377 | strcpy(response, "OKAY"); |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 378 | chars_left = sizeof(response) - strlen(response) - 1; |
| 379 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 380 | strsep(&cmd, ":"); |
| 381 | if (!cmd) { |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 382 | error("missing variable\n"); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 383 | fastboot_tx_write_str("FAILmissing var"); |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | if (!strcmp_l1("version", cmd)) { |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 388 | strncat(response, FASTBOOT_VERSION, chars_left); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 389 | } else if (!strcmp_l1("bootloader-version", cmd)) { |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 390 | strncat(response, U_BOOT_VERSION, chars_left); |
Eric Nelson | c674a66 | 2014-09-30 12:05:40 -0700 | [diff] [blame] | 391 | } else if (!strcmp_l1("downloadsize", cmd) || |
| 392 | !strcmp_l1("max-download-size", cmd)) { |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 393 | char str_num[12]; |
| 394 | |
Paul Kocialkowski | a588d99 | 2015-07-20 12:38:22 +0200 | [diff] [blame] | 395 | sprintf(str_num, "0x%08x", CONFIG_FASTBOOT_BUF_SIZE); |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 396 | strncat(response, str_num, chars_left); |
Maxime Ripard | 6c9e00e | 2015-10-15 14:34:15 +0200 | [diff] [blame^] | 397 | |
| 398 | /* |
| 399 | * This also indicates the start of a new flashing |
| 400 | * "session", in which we could have 1-N buffers to |
| 401 | * write to a partition. |
| 402 | * |
| 403 | * Reset our session counter. |
| 404 | */ |
| 405 | fastboot_flash_session_id = 0; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 406 | } else if (!strcmp_l1("serialno", cmd)) { |
| 407 | s = getenv("serial#"); |
| 408 | if (s) |
Jeroen Hofstee | 29425be | 2014-06-14 00:57:14 +0200 | [diff] [blame] | 409 | strncat(response, s, chars_left); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 410 | else |
| 411 | strcpy(response, "FAILValue not set"); |
| 412 | } else { |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 413 | error("unknown variable: %s\n", cmd); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 414 | strcpy(response, "FAILVariable not implemented"); |
| 415 | } |
| 416 | fastboot_tx_write_str(response); |
| 417 | } |
| 418 | |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 419 | static unsigned int rx_bytes_expected(unsigned int maxpacket) |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 420 | { |
| 421 | int rx_remain = download_size - download_bytes; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 422 | int rem = 0; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 423 | if (rx_remain < 0) |
| 424 | return 0; |
| 425 | if (rx_remain > EP_BUFFER_SIZE) |
| 426 | return EP_BUFFER_SIZE; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 427 | if (rx_remain < maxpacket) { |
| 428 | rx_remain = maxpacket; |
| 429 | } else if (rx_remain % maxpacket != 0) { |
| 430 | rem = rx_remain % maxpacket; |
| 431 | rx_remain = rx_remain + (maxpacket - rem); |
| 432 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 433 | return rx_remain; |
| 434 | } |
| 435 | |
| 436 | #define BYTES_PER_DOT 0x20000 |
| 437 | static void rx_handler_dl_image(struct usb_ep *ep, struct usb_request *req) |
| 438 | { |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 439 | char response[FASTBOOT_RESPONSE_LEN]; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 440 | unsigned int transfer_size = download_size - download_bytes; |
| 441 | const unsigned char *buffer = req->buf; |
| 442 | unsigned int buffer_size = req->actual; |
Bo Shen | 23d1d10 | 2014-09-19 14:15:12 +0800 | [diff] [blame] | 443 | unsigned int pre_dot_num, now_dot_num; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 444 | unsigned int max; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 445 | |
| 446 | if (req->status != 0) { |
| 447 | printf("Bad status: %d\n", req->status); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | if (buffer_size < transfer_size) |
| 452 | transfer_size = buffer_size; |
| 453 | |
Paul Kocialkowski | a588d99 | 2015-07-20 12:38:22 +0200 | [diff] [blame] | 454 | memcpy((void *)CONFIG_FASTBOOT_BUF_ADDR + download_bytes, |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 455 | buffer, transfer_size); |
| 456 | |
Bo Shen | 23d1d10 | 2014-09-19 14:15:12 +0800 | [diff] [blame] | 457 | pre_dot_num = download_bytes / BYTES_PER_DOT; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 458 | download_bytes += transfer_size; |
Bo Shen | 23d1d10 | 2014-09-19 14:15:12 +0800 | [diff] [blame] | 459 | now_dot_num = download_bytes / BYTES_PER_DOT; |
| 460 | |
| 461 | if (pre_dot_num != now_dot_num) { |
| 462 | putc('.'); |
| 463 | if (!(now_dot_num % 74)) |
| 464 | putc('\n'); |
| 465 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 466 | |
| 467 | /* Check if transfer is done */ |
| 468 | if (download_bytes >= download_size) { |
| 469 | /* |
| 470 | * Reset global transfer variable, keep download_bytes because |
| 471 | * it will be used in the next possible flashing command |
| 472 | */ |
| 473 | download_size = 0; |
| 474 | req->complete = rx_handler_command; |
| 475 | req->length = EP_BUFFER_SIZE; |
| 476 | |
| 477 | sprintf(response, "OKAY"); |
| 478 | fastboot_tx_write_str(response); |
| 479 | |
| 480 | printf("\ndownloading of %d bytes finished\n", download_bytes); |
| 481 | } else { |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 482 | max = is_high_speed ? hs_ep_out.wMaxPacketSize : |
| 483 | fs_ep_out.wMaxPacketSize; |
| 484 | req->length = rx_bytes_expected(max); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 485 | if (req->length < ep->maxpacket) |
| 486 | req->length = ep->maxpacket; |
| 487 | } |
| 488 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 489 | req->actual = 0; |
| 490 | usb_ep_queue(ep, req, 0); |
| 491 | } |
| 492 | |
| 493 | static void cb_download(struct usb_ep *ep, struct usb_request *req) |
| 494 | { |
| 495 | char *cmd = req->buf; |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 496 | char response[FASTBOOT_RESPONSE_LEN]; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 497 | unsigned int max; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 498 | |
| 499 | strsep(&cmd, ":"); |
| 500 | download_size = simple_strtoul(cmd, NULL, 16); |
| 501 | download_bytes = 0; |
| 502 | |
| 503 | printf("Starting download of %d bytes\n", download_size); |
| 504 | |
| 505 | if (0 == download_size) { |
| 506 | sprintf(response, "FAILdata invalid size"); |
Paul Kocialkowski | a588d99 | 2015-07-20 12:38:22 +0200 | [diff] [blame] | 507 | } else if (download_size > CONFIG_FASTBOOT_BUF_SIZE) { |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 508 | download_size = 0; |
| 509 | sprintf(response, "FAILdata too large"); |
| 510 | } else { |
| 511 | sprintf(response, "DATA%08x", download_size); |
| 512 | req->complete = rx_handler_dl_image; |
Dileep Katta | 9e4b510 | 2015-02-17 02:02:36 +0530 | [diff] [blame] | 513 | max = is_high_speed ? hs_ep_out.wMaxPacketSize : |
| 514 | fs_ep_out.wMaxPacketSize; |
| 515 | req->length = rx_bytes_expected(max); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 516 | if (req->length < ep->maxpacket) |
| 517 | req->length = ep->maxpacket; |
| 518 | } |
| 519 | fastboot_tx_write_str(response); |
| 520 | } |
| 521 | |
| 522 | static void do_bootm_on_complete(struct usb_ep *ep, struct usb_request *req) |
| 523 | { |
| 524 | char boot_addr_start[12]; |
| 525 | char *bootm_args[] = { "bootm", boot_addr_start, NULL }; |
| 526 | |
| 527 | puts("Booting kernel..\n"); |
| 528 | |
| 529 | sprintf(boot_addr_start, "0x%lx", load_addr); |
| 530 | do_bootm(NULL, 0, 2, bootm_args); |
| 531 | |
| 532 | /* This only happens if image is somehow faulty so we start over */ |
| 533 | do_reset(NULL, 0, 0, NULL); |
| 534 | } |
| 535 | |
| 536 | static void cb_boot(struct usb_ep *ep, struct usb_request *req) |
| 537 | { |
| 538 | fastboot_func->in_req->complete = do_bootm_on_complete; |
| 539 | fastboot_tx_write_str("OKAY"); |
| 540 | } |
| 541 | |
Rob Herring | 267abc6 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 542 | static void do_exit_on_complete(struct usb_ep *ep, struct usb_request *req) |
| 543 | { |
| 544 | g_dnl_trigger_detach(); |
| 545 | } |
| 546 | |
| 547 | static void cb_continue(struct usb_ep *ep, struct usb_request *req) |
| 548 | { |
| 549 | fastboot_func->in_req->complete = do_exit_on_complete; |
| 550 | fastboot_tx_write_str("OKAY"); |
| 551 | } |
| 552 | |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 553 | #ifdef CONFIG_FASTBOOT_FLASH |
| 554 | static void cb_flash(struct usb_ep *ep, struct usb_request *req) |
| 555 | { |
| 556 | char *cmd = req->buf; |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 557 | char response[FASTBOOT_RESPONSE_LEN]; |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 558 | |
| 559 | strsep(&cmd, ":"); |
| 560 | if (!cmd) { |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 561 | error("missing partition name\n"); |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 562 | fastboot_tx_write_str("FAILmissing partition name"); |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | strcpy(response, "FAILno flash device defined"); |
| 567 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
Maxime Ripard | 6c9e00e | 2015-10-15 14:34:15 +0200 | [diff] [blame^] | 568 | fb_mmc_flash_write(cmd, fastboot_flash_session_id, |
| 569 | (void *)CONFIG_FASTBOOT_BUF_ADDR, |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 570 | download_bytes, response); |
| 571 | #endif |
Maxime Ripard | 6c9e00e | 2015-10-15 14:34:15 +0200 | [diff] [blame^] | 572 | fastboot_flash_session_id++; |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 573 | fastboot_tx_write_str(response); |
| 574 | } |
| 575 | #endif |
| 576 | |
Michael Scott | de19562 | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 577 | static void cb_oem(struct usb_ep *ep, struct usb_request *req) |
| 578 | { |
| 579 | char *cmd = req->buf; |
Maxime Ripard | 4adef27 | 2015-10-15 22:04:04 +0200 | [diff] [blame] | 580 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
Rob Herring | 372d7de | 2015-01-26 15:49:01 -0600 | [diff] [blame] | 581 | if (strncmp("format", cmd + 4, 6) == 0) { |
| 582 | char cmdbuf[32]; |
| 583 | sprintf(cmdbuf, "gpt write mmc %x $partitions", |
| 584 | CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 585 | if (run_command(cmdbuf, 0)) |
| 586 | fastboot_tx_write_str("FAIL"); |
| 587 | else |
| 588 | fastboot_tx_write_str("OKAY"); |
| 589 | } else |
| 590 | #endif |
Michael Scott | de19562 | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 591 | if (strncmp("unlock", cmd + 4, 8) == 0) { |
| 592 | fastboot_tx_write_str("FAILnot implemented"); |
| 593 | } |
| 594 | else { |
| 595 | fastboot_tx_write_str("FAILunknown oem command"); |
| 596 | } |
| 597 | } |
| 598 | |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 599 | #ifdef CONFIG_FASTBOOT_FLASH |
| 600 | static void cb_erase(struct usb_ep *ep, struct usb_request *req) |
| 601 | { |
| 602 | char *cmd = req->buf; |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 603 | char response[FASTBOOT_RESPONSE_LEN]; |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 604 | |
| 605 | strsep(&cmd, ":"); |
| 606 | if (!cmd) { |
| 607 | error("missing partition name"); |
| 608 | fastboot_tx_write_str("FAILmissing partition name"); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | strcpy(response, "FAILno flash device defined"); |
| 613 | |
| 614 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
| 615 | fb_mmc_erase(cmd, response); |
| 616 | #endif |
| 617 | fastboot_tx_write_str(response); |
| 618 | } |
| 619 | #endif |
| 620 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 621 | struct cmd_dispatch_info { |
| 622 | char *cmd; |
| 623 | void (*cb)(struct usb_ep *ep, struct usb_request *req); |
| 624 | }; |
| 625 | |
| 626 | static const struct cmd_dispatch_info cmd_dispatch_info[] = { |
| 627 | { |
| 628 | .cmd = "reboot", |
| 629 | .cb = cb_reboot, |
| 630 | }, { |
| 631 | .cmd = "getvar:", |
| 632 | .cb = cb_getvar, |
| 633 | }, { |
| 634 | .cmd = "download:", |
| 635 | .cb = cb_download, |
| 636 | }, { |
| 637 | .cmd = "boot", |
| 638 | .cb = cb_boot, |
Rob Herring | 267abc6 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 639 | }, { |
| 640 | .cmd = "continue", |
| 641 | .cb = cb_continue, |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 642 | }, |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 643 | #ifdef CONFIG_FASTBOOT_FLASH |
| 644 | { |
| 645 | .cmd = "flash", |
| 646 | .cb = cb_flash, |
Dileep Katta | 8979238 | 2015-02-17 18:48:23 +0530 | [diff] [blame] | 647 | }, { |
| 648 | .cmd = "erase", |
| 649 | .cb = cb_erase, |
Steve Rae | d1b5ed0 | 2014-08-26 11:47:28 -0700 | [diff] [blame] | 650 | }, |
| 651 | #endif |
Michael Scott | de19562 | 2015-01-26 15:49:00 -0600 | [diff] [blame] | 652 | { |
| 653 | .cmd = "oem", |
| 654 | .cb = cb_oem, |
| 655 | }, |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 656 | }; |
| 657 | |
| 658 | static void rx_handler_command(struct usb_ep *ep, struct usb_request *req) |
| 659 | { |
| 660 | char *cmdbuf = req->buf; |
| 661 | void (*func_cb)(struct usb_ep *ep, struct usb_request *req) = NULL; |
| 662 | int i; |
| 663 | |
Paul Kocialkowski | 94b385f | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 664 | if (req->status != 0 || req->length == 0) |
| 665 | return; |
| 666 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 667 | for (i = 0; i < ARRAY_SIZE(cmd_dispatch_info); i++) { |
| 668 | if (!strcmp_l1(cmd_dispatch_info[i].cmd, cmdbuf)) { |
| 669 | func_cb = cmd_dispatch_info[i].cb; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 674 | if (!func_cb) { |
| 675 | error("unknown command: %s\n", cmdbuf); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 676 | fastboot_tx_write_str("FAILunknown command"); |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 677 | } else { |
Eric Nelson | e214058 | 2014-10-01 14:30:56 -0700 | [diff] [blame] | 678 | if (req->actual < req->length) { |
| 679 | u8 *buf = (u8 *)req->buf; |
| 680 | buf[req->actual] = 0; |
| 681 | func_cb(ep, req); |
| 682 | } else { |
| 683 | error("buffer overflow\n"); |
| 684 | fastboot_tx_write_str("FAILbuffer overflow"); |
| 685 | } |
Steve Rae | 593cbd9 | 2014-08-26 11:47:29 -0700 | [diff] [blame] | 686 | } |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 687 | |
Paul Kocialkowski | 94b385f | 2015-07-04 16:46:15 +0200 | [diff] [blame] | 688 | *cmdbuf = '\0'; |
| 689 | req->actual = 0; |
| 690 | usb_ep_queue(ep, req, 0); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 691 | } |