Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008 - 2009 Windriver, <www.windriver.com> |
| 4 | * Author: Tom Rix <Tom.Rix@windriver.com> |
| 5 | * |
| 6 | * (C) Copyright 2014 Linaro, Ltd. |
| 7 | * Rob Herring <robh@kernel.org> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 12 | #include <g_dnl.h> |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 13 | #include <fastboot.h> |
| 14 | #include <net.h> |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 15 | #include <usb.h> |
Sean Nyekjaer | 674f662 | 2019-01-24 15:17:21 +0100 | [diff] [blame] | 16 | #include <watchdog.h> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 17 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 18 | static int do_fastboot_udp(int argc, char *const argv[], |
| 19 | uintptr_t buf_addr, size_t buf_size) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 20 | { |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 21 | #if CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT) |
| 22 | int err = net_loop(FASTBOOT); |
| 23 | |
| 24 | if (err < 0) { |
| 25 | printf("fastboot udp error: %d\n", err); |
| 26 | return CMD_RET_FAILURE; |
| 27 | } |
| 28 | |
| 29 | return CMD_RET_SUCCESS; |
| 30 | #else |
| 31 | pr_err("Fastboot UDP not enabled\n"); |
| 32 | return CMD_RET_FAILURE; |
| 33 | #endif |
| 34 | } |
| 35 | |
| 36 | static int do_fastboot_usb(int argc, char *const argv[], |
| 37 | uintptr_t buf_addr, size_t buf_size) |
| 38 | { |
| 39 | #if CONFIG_IS_ENABLED(USB_FUNCTION_FASTBOOT) |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 40 | int controller_index; |
| 41 | char *usb_controller; |
Sam Protsenko | 29f5734 | 2018-06-29 21:59:40 +0300 | [diff] [blame] | 42 | char *endp; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 43 | int ret; |
| 44 | |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 45 | if (argc < 2) |
| 46 | return CMD_RET_USAGE; |
| 47 | |
| 48 | usb_controller = argv[1]; |
Sam Protsenko | 29f5734 | 2018-06-29 21:59:40 +0300 | [diff] [blame] | 49 | controller_index = simple_strtoul(usb_controller, &endp, 0); |
| 50 | if (*endp != '\0') { |
| 51 | pr_err("Error: Wrong USB controller index format\n"); |
| 52 | return CMD_RET_FAILURE; |
| 53 | } |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 54 | |
Jean-Jacques Hiblot | 585e199 | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 55 | ret = usb_gadget_initialize(controller_index); |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 56 | if (ret) { |
Seung-Woo Kim | 591188e | 2018-06-04 16:04:51 +0900 | [diff] [blame] | 57 | pr_err("USB init failed: %d\n", ret); |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 58 | return CMD_RET_FAILURE; |
| 59 | } |
| 60 | |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 61 | g_dnl_clear_detach(); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 62 | ret = g_dnl_register("usb_dnl_fastboot"); |
| 63 | if (ret) |
| 64 | return ret; |
| 65 | |
Rob Herring | ce9d8ee | 2015-01-26 15:14:41 -0600 | [diff] [blame] | 66 | if (!g_dnl_board_usb_cable_connected()) { |
| 67 | puts("\rUSB cable not detected.\n" \ |
| 68 | "Command exit.\n"); |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 69 | ret = CMD_RET_FAILURE; |
| 70 | goto exit; |
Rob Herring | ce9d8ee | 2015-01-26 15:14:41 -0600 | [diff] [blame] | 71 | } |
| 72 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 73 | while (1) { |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 74 | if (g_dnl_detach()) |
| 75 | break; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 76 | if (ctrlc()) |
| 77 | break; |
Sean Nyekjaer | 674f662 | 2019-01-24 15:17:21 +0100 | [diff] [blame] | 78 | WATCHDOG_RESET(); |
Paul Kocialkowski | 6b9f728 | 2015-05-24 11:41:04 +0200 | [diff] [blame] | 79 | usb_gadget_handle_interrupts(controller_index); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 82 | ret = CMD_RET_SUCCESS; |
| 83 | |
| 84 | exit: |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 85 | g_dnl_unregister(); |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 86 | g_dnl_clear_detach(); |
Jean-Jacques Hiblot | 585e199 | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 87 | usb_gadget_release(controller_index); |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 88 | |
| 89 | return ret; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 90 | #else |
| 91 | pr_err("Fastboot USB not enabled\n"); |
| 92 | return CMD_RET_FAILURE; |
| 93 | #endif |
| 94 | } |
| 95 | |
| 96 | static int do_fastboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 97 | { |
| 98 | uintptr_t buf_addr = (uintptr_t)NULL; |
| 99 | size_t buf_size = 0; |
| 100 | |
| 101 | if (argc < 2) |
| 102 | return CMD_RET_USAGE; |
| 103 | |
| 104 | while (argc > 1 && **(argv + 1) == '-') { |
| 105 | char *arg = *++argv; |
| 106 | |
| 107 | --argc; |
| 108 | while (*++arg) { |
| 109 | switch (*arg) { |
| 110 | case 'l': |
| 111 | if (--argc <= 0) |
| 112 | return CMD_RET_USAGE; |
| 113 | buf_addr = simple_strtoul(*++argv, NULL, 16); |
| 114 | goto NXTARG; |
| 115 | |
| 116 | case 's': |
| 117 | if (--argc <= 0) |
| 118 | return CMD_RET_USAGE; |
| 119 | buf_size = simple_strtoul(*++argv, NULL, 16); |
| 120 | goto NXTARG; |
| 121 | |
| 122 | default: |
| 123 | return CMD_RET_USAGE; |
| 124 | } |
| 125 | } |
| 126 | NXTARG: |
| 127 | ; |
| 128 | } |
| 129 | |
Sam Protsenko | 29f5734 | 2018-06-29 21:59:40 +0300 | [diff] [blame] | 130 | /* Handle case when USB controller param is just '-' */ |
| 131 | if (argc == 1) { |
| 132 | pr_err("Error: Incorrect USB controller index\n"); |
| 133 | return CMD_RET_USAGE; |
| 134 | } |
| 135 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 136 | fastboot_init((void *)buf_addr, buf_size); |
| 137 | |
| 138 | if (!strcmp(argv[1], "udp")) |
| 139 | return do_fastboot_udp(argc, argv, buf_addr, buf_size); |
| 140 | |
| 141 | if (!strcmp(argv[1], "usb")) { |
| 142 | argv++; |
| 143 | argc--; |
| 144 | } |
| 145 | |
| 146 | return do_fastboot_usb(argc, argv, buf_addr, buf_size); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 149 | #ifdef CONFIG_SYS_LONGHELP |
| 150 | static char fastboot_help_text[] = |
| 151 | "[-l addr] [-s size] usb <controller> | udp\n" |
| 152 | "\taddr - address of buffer used during data transfers (" |
| 153 | __stringify(CONFIG_FASTBOOT_BUF_ADDR) ")\n" |
| 154 | "\tsize - size of buffer used during data transfers (" |
| 155 | __stringify(CONFIG_FASTBOOT_BUF_SIZE) ")" |
| 156 | ; |
| 157 | #endif |
| 158 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 159 | U_BOOT_CMD( |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 160 | fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot, |
| 161 | "run as a fastboot usb or udp device", fastboot_help_text |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 162 | ); |