Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sebastian Siewior | 3aab70a | 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 | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 12 | #include <g_dnl.h> |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 13 | #include <fastboot.h> |
| 14 | #include <net.h> |
Paul Kocialkowski | 8d2f003 | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 15 | #include <usb.h> |
Sean Nyekjaer | 731785d | 2019-01-24 15:17:21 +0100 | [diff] [blame] | 16 | #include <watchdog.h> |
Simon Glass | 1af3c7f | 2020-05-10 11:40:09 -0600 | [diff] [blame] | 17 | #include <linux/stringify.h> |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 18 | |
Alex Kiernan | f73a7df | 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 | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 21 | { |
Patrick Delaunay | d037990 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 22 | int err; |
| 23 | |
| 24 | if (!CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT)) { |
| 25 | pr_err("Fastboot UDP not enabled\n"); |
| 26 | return CMD_RET_FAILURE; |
| 27 | } |
| 28 | |
| 29 | err = net_loop(FASTBOOT); |
Alex Kiernan | f73a7df | 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 | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static int do_fastboot_usb(int argc, char *const argv[], |
| 40 | uintptr_t buf_addr, size_t buf_size) |
| 41 | { |
Paul Kocialkowski | 8d2f003 | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 42 | int controller_index; |
| 43 | char *usb_controller; |
Sam Protsenko | aa51579 | 2018-06-29 21:59:40 +0300 | [diff] [blame] | 44 | char *endp; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 45 | int ret; |
| 46 | |
Patrick Delaunay | d037990 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 47 | if (!CONFIG_IS_ENABLED(USB_FUNCTION_FASTBOOT)) { |
| 48 | pr_err("Fastboot USB not enabled\n"); |
| 49 | return CMD_RET_FAILURE; |
| 50 | } |
| 51 | |
Paul Kocialkowski | 8d2f003 | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 52 | if (argc < 2) |
| 53 | return CMD_RET_USAGE; |
| 54 | |
| 55 | usb_controller = argv[1]; |
Sam Protsenko | aa51579 | 2018-06-29 21:59:40 +0300 | [diff] [blame] | 56 | controller_index = simple_strtoul(usb_controller, &endp, 0); |
| 57 | if (*endp != '\0') { |
| 58 | pr_err("Error: Wrong USB controller index format\n"); |
| 59 | return CMD_RET_FAILURE; |
| 60 | } |
Paul Kocialkowski | 8d2f003 | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 61 | |
Jean-Jacques Hiblot | a06955a | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 62 | ret = usb_gadget_initialize(controller_index); |
Paul Kocialkowski | 8d2f003 | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 63 | if (ret) { |
Seung-Woo Kim | 71002b5 | 2018-06-04 16:04:51 +0900 | [diff] [blame] | 64 | pr_err("USB init failed: %d\n", ret); |
Paul Kocialkowski | 8d2f003 | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 65 | return CMD_RET_FAILURE; |
| 66 | } |
| 67 | |
Rob Herring | 267abc6 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 68 | g_dnl_clear_detach(); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 69 | ret = g_dnl_register("usb_dnl_fastboot"); |
| 70 | if (ret) |
| 71 | return ret; |
| 72 | |
Rob Herring | 7c23bcb | 2015-01-26 15:14:41 -0600 | [diff] [blame] | 73 | if (!g_dnl_board_usb_cable_connected()) { |
| 74 | puts("\rUSB cable not detected.\n" \ |
| 75 | "Command exit.\n"); |
Paul Kocialkowski | 8d2f003 | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 76 | ret = CMD_RET_FAILURE; |
| 77 | goto exit; |
Rob Herring | 7c23bcb | 2015-01-26 15:14:41 -0600 | [diff] [blame] | 78 | } |
| 79 | |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 80 | while (1) { |
Rob Herring | 267abc6 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 81 | if (g_dnl_detach()) |
| 82 | break; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 83 | if (ctrlc()) |
| 84 | break; |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 85 | schedule(); |
Paul Kocialkowski | eca76b7 | 2015-05-24 11:41:04 +0200 | [diff] [blame] | 86 | usb_gadget_handle_interrupts(controller_index); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Paul Kocialkowski | 8d2f003 | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 89 | ret = CMD_RET_SUCCESS; |
| 90 | |
| 91 | exit: |
Mattijs Korpershoek | 5f7e01e | 2022-10-07 11:38:22 +0200 | [diff] [blame] | 92 | usb_gadget_release(controller_index); |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 93 | g_dnl_unregister(); |
Rob Herring | 267abc6 | 2014-12-10 14:43:04 -0600 | [diff] [blame] | 94 | g_dnl_clear_detach(); |
Paul Kocialkowski | 8d2f003 | 2015-06-12 19:57:00 +0200 | [diff] [blame] | 95 | |
| 96 | return ret; |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 99 | static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc, |
| 100 | char *const argv[]) |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 101 | { |
| 102 | uintptr_t buf_addr = (uintptr_t)NULL; |
| 103 | size_t buf_size = 0; |
| 104 | |
| 105 | if (argc < 2) |
| 106 | return CMD_RET_USAGE; |
| 107 | |
| 108 | while (argc > 1 && **(argv + 1) == '-') { |
| 109 | char *arg = *++argv; |
| 110 | |
| 111 | --argc; |
| 112 | while (*++arg) { |
| 113 | switch (*arg) { |
| 114 | case 'l': |
| 115 | if (--argc <= 0) |
| 116 | return CMD_RET_USAGE; |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 117 | buf_addr = hextoul(*++argv, NULL); |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 118 | goto NXTARG; |
| 119 | |
| 120 | case 's': |
| 121 | if (--argc <= 0) |
| 122 | return CMD_RET_USAGE; |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 123 | buf_size = hextoul(*++argv, NULL); |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 124 | goto NXTARG; |
| 125 | |
| 126 | default: |
| 127 | return CMD_RET_USAGE; |
| 128 | } |
| 129 | } |
| 130 | NXTARG: |
| 131 | ; |
| 132 | } |
| 133 | |
Sam Protsenko | aa51579 | 2018-06-29 21:59:40 +0300 | [diff] [blame] | 134 | /* Handle case when USB controller param is just '-' */ |
| 135 | if (argc == 1) { |
| 136 | pr_err("Error: Incorrect USB controller index\n"); |
| 137 | return CMD_RET_USAGE; |
| 138 | } |
| 139 | |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 140 | fastboot_init((void *)buf_addr, buf_size); |
| 141 | |
| 142 | if (!strcmp(argv[1], "udp")) |
| 143 | return do_fastboot_udp(argc, argv, buf_addr, buf_size); |
| 144 | |
| 145 | if (!strcmp(argv[1], "usb")) { |
| 146 | argv++; |
| 147 | argc--; |
| 148 | } |
| 149 | |
| 150 | return do_fastboot_usb(argc, argv, buf_addr, buf_size); |
| 151 | } |
| 152 | |
Patrick Delaunay | d037990 | 2022-12-15 10:15:50 +0100 | [diff] [blame] | 153 | U_BOOT_CMD( |
| 154 | fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot, |
| 155 | "run as a fastboot usb or udp device", |
Alex Kiernan | f73a7df | 2018-05-29 15:30:53 +0000 | [diff] [blame] | 156 | "[-l addr] [-s size] usb <controller> | udp\n" |
| 157 | "\taddr - address of buffer used during data transfers (" |
| 158 | __stringify(CONFIG_FASTBOOT_BUF_ADDR) ")\n" |
| 159 | "\tsize - size of buffer used during data transfers (" |
| 160 | __stringify(CONFIG_FASTBOOT_BUF_SIZE) ")" |
Sebastian Siewior | 3aab70a | 2014-05-05 15:08:10 -0500 | [diff] [blame] | 161 | ); |