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