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