Merge tag 'xilinx-for-v2025.01-rc3-v2' of https://source.denx.de/u-boot/custodians...
[u-boot.git] / cmd / fastboot.c
blobd4cfc0c7a282b8697f50c207fa8cb46171797ee1
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2008 - 2009 Windriver, <www.windriver.com>
4 * Author: Tom Rix <Tom.Rix@windriver.com>
6 * (C) Copyright 2014 Linaro, Ltd.
7 * Rob Herring <robh@kernel.org>
8 */
9 #include <command.h>
10 #include <console.h>
11 #include <g_dnl.h>
12 #include <fastboot.h>
13 #include <net.h>
14 #include <usb.h>
15 #include <watchdog.h>
16 #include <linux/printk.h>
17 #include <linux/stringify.h>
19 static int do_fastboot_udp(int argc, char *const argv[],
20 uintptr_t buf_addr, size_t buf_size)
22 int err;
24 if (!IS_ENABLED(CONFIG_UDP_FUNCTION_FASTBOOT)) {
25 pr_err("Fastboot UDP not enabled\n");
26 return CMD_RET_FAILURE;
29 err = net_loop(FASTBOOT_UDP);
31 if (err < 0) {
32 printf("fastboot udp error: %d\n", err);
33 return CMD_RET_FAILURE;
36 return CMD_RET_SUCCESS;
39 static int do_fastboot_tcp(int argc, char *const argv[],
40 uintptr_t buf_addr, size_t buf_size)
42 int err;
44 if (!IS_ENABLED(CONFIG_TCP_FUNCTION_FASTBOOT)) {
45 pr_err("Fastboot TCP not enabled\n");
46 return CMD_RET_FAILURE;
49 err = net_loop(FASTBOOT_TCP);
51 if (err < 0) {
52 printf("fastboot tcp error: %d\n", err);
53 return CMD_RET_FAILURE;
56 return CMD_RET_SUCCESS;
59 static int do_fastboot_usb(int argc, char *const argv[],
60 uintptr_t buf_addr, size_t buf_size)
62 int controller_index;
63 char *usb_controller;
64 struct udevice *udc;
65 char *endp;
66 int ret;
68 if (!IS_ENABLED(CONFIG_USB_FUNCTION_FASTBOOT)) {
69 pr_err("Fastboot USB not enabled\n");
70 return CMD_RET_FAILURE;
73 if (argc < 2)
74 return CMD_RET_USAGE;
76 usb_controller = argv[1];
77 controller_index = simple_strtoul(usb_controller, &endp, 0);
78 if (*endp != '\0') {
79 pr_err("Error: Wrong USB controller index format\n");
80 return CMD_RET_FAILURE;
83 ret = udc_device_get_by_index(controller_index, &udc);
84 if (ret) {
85 pr_err("USB init failed: %d\n", ret);
86 return CMD_RET_FAILURE;
89 g_dnl_clear_detach();
90 ret = g_dnl_register("usb_dnl_fastboot");
91 if (ret)
92 return ret;
94 if (!g_dnl_board_usb_cable_connected()) {
95 puts("\rUSB cable not detected.\n" \
96 "Command exit.\n");
97 ret = CMD_RET_FAILURE;
98 goto exit;
101 while (1) {
102 if (g_dnl_detach())
103 break;
104 if (ctrlc())
105 break;
106 schedule();
107 dm_usb_gadget_handle_interrupts(udc);
110 ret = CMD_RET_SUCCESS;
112 exit:
113 udc_device_put(udc);
114 g_dnl_unregister();
115 g_dnl_clear_detach();
117 return ret;
120 static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc,
121 char *const argv[])
123 uintptr_t buf_addr = (uintptr_t)NULL;
124 size_t buf_size = 0;
126 if (argc < 2)
127 return CMD_RET_USAGE;
129 while (argc > 1 && **(argv + 1) == '-') {
130 char *arg = *++argv;
132 --argc;
133 while (*++arg) {
134 switch (*arg) {
135 case 'l':
136 if (--argc <= 0)
137 return CMD_RET_USAGE;
138 buf_addr = hextoul(*++argv, NULL);
139 goto NXTARG;
141 case 's':
142 if (--argc <= 0)
143 return CMD_RET_USAGE;
144 buf_size = hextoul(*++argv, NULL);
145 goto NXTARG;
147 default:
148 return CMD_RET_USAGE;
151 NXTARG:
155 /* Handle case when USB controller param is just '-' */
156 if (argc == 1) {
157 pr_err("Error: Incorrect USB controller index\n");
158 return CMD_RET_USAGE;
161 fastboot_init((void *)buf_addr, buf_size);
163 if (!strcmp(argv[1], "udp"))
164 return do_fastboot_udp(argc, argv, buf_addr, buf_size);
165 if (!strcmp(argv[1], "tcp"))
166 return do_fastboot_tcp(argc, argv, buf_addr, buf_size);
167 if (!strcmp(argv[1], "usb")) {
168 argv++;
169 argc--;
172 return do_fastboot_usb(argc, argv, buf_addr, buf_size);
175 U_BOOT_CMD(
176 fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot,
177 "run as a fastboot usb or udp device",
178 "[-l addr] [-s size] usb <controller> | udp\n"
179 "\taddr - address of buffer used during data transfers ("
180 __stringify(CONFIG_FASTBOOT_BUF_ADDR) ")\n"
181 "\tsize - size of buffer used during data transfers ("
182 __stringify(CONFIG_FASTBOOT_BUF_SIZE) ")"