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> |
Simon Glass | fb64e36 | 2020-05-10 11:40:09 -0600 | [diff] [blame] | 17 | #include <linux/stringify.h> |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 18 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 19 | static int do_fastboot_udp(int argc, char *const argv[], |
| 20 | uintptr_t buf_addr, size_t buf_size) |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 21 | { |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 22 | int err; |
| 23 | |
Simon Glass | d911f3b | 2023-02-05 15:44:20 -0700 | [diff] [blame] | 24 | if (!IS_ENABLED(CONFIG_UDP_FUNCTION_FASTBOOT)) { |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 25 | pr_err("Fastboot UDP not enabled\n"); |
| 26 | return CMD_RET_FAILURE; |
| 27 | } |
| 28 | |
Dmitrii Merkurev | 308252d | 2023-04-12 19:49:30 +0100 | [diff] [blame] | 29 | err = net_loop(FASTBOOT_UDP); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 30 | |
| 31 | if (err < 0) { |
| 32 | printf("fastboot udp error: %d\n", err); |
| 33 | return CMD_RET_FAILURE; |
| 34 | } |
| 35 | |
| 36 | return CMD_RET_SUCCESS; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Dmitrii Merkurev | 308252d | 2023-04-12 19:49:30 +0100 | [diff] [blame] | 39 | static int do_fastboot_tcp(int argc, char *const argv[], |
| 40 | uintptr_t buf_addr, size_t buf_size) |
| 41 | { |
| 42 | int err; |
| 43 | |
| 44 | if (!IS_ENABLED(CONFIG_TCP_FUNCTION_FASTBOOT)) { |
| 45 | pr_err("Fastboot TCP not enabled\n"); |
| 46 | return CMD_RET_FAILURE; |
| 47 | } |
| 48 | |
| 49 | err = net_loop(FASTBOOT_TCP); |
| 50 | |
| 51 | if (err < 0) { |
| 52 | printf("fastboot tcp error: %d\n", err); |
| 53 | return CMD_RET_FAILURE; |
| 54 | } |
| 55 | |
| 56 | return CMD_RET_SUCCESS; |
| 57 | } |
| 58 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 59 | static int do_fastboot_usb(int argc, char *const argv[], |
| 60 | uintptr_t buf_addr, size_t buf_size) |
| 61 | { |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 62 | int controller_index; |
| 63 | char *usb_controller; |
Sam Protsenko | 29f5734 | 2018-06-29 21:59:40 +0300 | [diff] [blame] | 64 | char *endp; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 65 | int ret; |
| 66 | |
Simon Glass | f597ebe | 2023-02-05 15:44:23 -0700 | [diff] [blame] | 67 | if (!IS_ENABLED(CONFIG_USB_FUNCTION_FASTBOOT)) { |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 68 | pr_err("Fastboot USB not enabled\n"); |
| 69 | return CMD_RET_FAILURE; |
| 70 | } |
| 71 | |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 72 | if (argc < 2) |
| 73 | return CMD_RET_USAGE; |
| 74 | |
| 75 | usb_controller = argv[1]; |
Sam Protsenko | 29f5734 | 2018-06-29 21:59:40 +0300 | [diff] [blame] | 76 | controller_index = simple_strtoul(usb_controller, &endp, 0); |
| 77 | if (*endp != '\0') { |
| 78 | pr_err("Error: Wrong USB controller index format\n"); |
| 79 | return CMD_RET_FAILURE; |
| 80 | } |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 81 | |
Jean-Jacques Hiblot | 585e199 | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 82 | ret = usb_gadget_initialize(controller_index); |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 83 | if (ret) { |
Seung-Woo Kim | 591188e | 2018-06-04 16:04:51 +0900 | [diff] [blame] | 84 | pr_err("USB init failed: %d\n", ret); |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 85 | return CMD_RET_FAILURE; |
| 86 | } |
| 87 | |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 88 | g_dnl_clear_detach(); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 89 | ret = g_dnl_register("usb_dnl_fastboot"); |
| 90 | if (ret) |
| 91 | return ret; |
| 92 | |
Rob Herring | ce9d8ee | 2015-01-26 15:14:41 -0600 | [diff] [blame] | 93 | if (!g_dnl_board_usb_cable_connected()) { |
| 94 | puts("\rUSB cable not detected.\n" \ |
| 95 | "Command exit.\n"); |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 96 | ret = CMD_RET_FAILURE; |
| 97 | goto exit; |
Rob Herring | ce9d8ee | 2015-01-26 15:14:41 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 100 | while (1) { |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 101 | if (g_dnl_detach()) |
| 102 | break; |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 103 | if (ctrlc()) |
| 104 | break; |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 105 | schedule(); |
Paul Kocialkowski | 6b9f728 | 2015-05-24 11:41:04 +0200 | [diff] [blame] | 106 | usb_gadget_handle_interrupts(controller_index); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 109 | ret = CMD_RET_SUCCESS; |
| 110 | |
| 111 | exit: |
Mattijs Korpershoek | e9621d8 | 2022-10-07 11:38:22 +0200 | [diff] [blame] | 112 | usb_gadget_release(controller_index); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 113 | g_dnl_unregister(); |
Rob Herring | 5aba398 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 114 | g_dnl_clear_detach(); |
Paul Kocialkowski | d48378f | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 115 | |
| 116 | return ret; |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 119 | static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc, |
| 120 | char *const argv[]) |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 121 | { |
| 122 | uintptr_t buf_addr = (uintptr_t)NULL; |
| 123 | size_t buf_size = 0; |
| 124 | |
| 125 | if (argc < 2) |
| 126 | return CMD_RET_USAGE; |
| 127 | |
| 128 | while (argc > 1 && **(argv + 1) == '-') { |
| 129 | char *arg = *++argv; |
| 130 | |
| 131 | --argc; |
| 132 | while (*++arg) { |
| 133 | switch (*arg) { |
| 134 | case 'l': |
| 135 | if (--argc <= 0) |
| 136 | return CMD_RET_USAGE; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 137 | buf_addr = hextoul(*++argv, NULL); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 138 | goto NXTARG; |
| 139 | |
| 140 | case 's': |
| 141 | if (--argc <= 0) |
| 142 | return CMD_RET_USAGE; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 143 | buf_size = hextoul(*++argv, NULL); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 144 | goto NXTARG; |
| 145 | |
| 146 | default: |
| 147 | return CMD_RET_USAGE; |
| 148 | } |
| 149 | } |
| 150 | NXTARG: |
| 151 | ; |
| 152 | } |
| 153 | |
Sam Protsenko | 29f5734 | 2018-06-29 21:59:40 +0300 | [diff] [blame] | 154 | /* Handle case when USB controller param is just '-' */ |
| 155 | if (argc == 1) { |
| 156 | pr_err("Error: Incorrect USB controller index\n"); |
| 157 | return CMD_RET_USAGE; |
| 158 | } |
| 159 | |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 160 | fastboot_init((void *)buf_addr, buf_size); |
| 161 | |
| 162 | if (!strcmp(argv[1], "udp")) |
| 163 | return do_fastboot_udp(argc, argv, buf_addr, buf_size); |
Dmitrii Merkurev | 308252d | 2023-04-12 19:49:30 +0100 | [diff] [blame] | 164 | if (!strcmp(argv[1], "tcp")) |
| 165 | return do_fastboot_tcp(argc, argv, buf_addr, buf_size); |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 166 | if (!strcmp(argv[1], "usb")) { |
| 167 | argv++; |
| 168 | argc--; |
| 169 | } |
| 170 | |
| 171 | return do_fastboot_usb(argc, argv, buf_addr, buf_size); |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Patrick Delaunay | f82f9e4 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 174 | U_BOOT_CMD( |
| 175 | fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot, |
| 176 | "run as a fastboot usb or udp device", |
Alex Kiernan | d5aa57c | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 177 | "[-l addr] [-s size] usb <controller> | udp\n" |
| 178 | "\taddr - address of buffer used during data transfers (" |
| 179 | __stringify(CONFIG_FASTBOOT_BUF_ADDR) ")\n" |
| 180 | "\tsize - size of buffer used during data transfers (" |
| 181 | __stringify(CONFIG_FASTBOOT_BUF_SIZE) ")" |
Sebastian Siewior | 9d4471e | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 182 | ); |