]> Git Repo - u-boot.git/blame - cmd/fastboot.c
Merge tag 'u-boot-dfu-20240419' of https://source.denx.de/u-boot/custodians/u-boot-dfu
[u-boot.git] / cmd / fastboot.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
3aab70af
SS
2/*
3 * Copyright 2008 - 2009 Windriver, <www.windriver.com>
4 * Author: Tom Rix <[email protected]>
5 *
6 * (C) Copyright 2014 Linaro, Ltd.
7 * Rob Herring <[email protected]>
3aab70af
SS
8 */
9#include <common.h>
10#include <command.h>
24b852a7 11#include <console.h>
3aab70af 12#include <g_dnl.h>
f73a7df9
AK
13#include <fastboot.h>
14#include <net.h>
8d2f0039 15#include <usb.h>
731785df 16#include <watchdog.h>
1e94b46f 17#include <linux/printk.h>
1af3c7f4 18#include <linux/stringify.h>
3aab70af 19
f73a7df9
AK
20static int do_fastboot_udp(int argc, char *const argv[],
21 uintptr_t buf_addr, size_t buf_size)
3aab70af 22{
d0379900
PD
23 int err;
24
942b3096 25 if (!IS_ENABLED(CONFIG_UDP_FUNCTION_FASTBOOT)) {
d0379900
PD
26 pr_err("Fastboot UDP not enabled\n");
27 return CMD_RET_FAILURE;
28 }
29
443d3191 30 err = net_loop(FASTBOOT_UDP);
f73a7df9
AK
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;
f73a7df9
AK
38}
39
443d3191
DM
40static 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
f73a7df9
AK
60static int do_fastboot_usb(int argc, char *const argv[],
61 uintptr_t buf_addr, size_t buf_size)
62{
8d2f0039
PK
63 int controller_index;
64 char *usb_controller;
bac356c3 65 struct udevice *udc;
aa51579f 66 char *endp;
3aab70af
SS
67 int ret;
68
37407ac2 69 if (!IS_ENABLED(CONFIG_USB_FUNCTION_FASTBOOT)) {
d0379900
PD
70 pr_err("Fastboot USB not enabled\n");
71 return CMD_RET_FAILURE;
72 }
73
8d2f0039
PK
74 if (argc < 2)
75 return CMD_RET_USAGE;
76
77 usb_controller = argv[1];
aa51579f
SP
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 }
8d2f0039 83
bac356c3 84 ret = udc_device_get_by_index(controller_index, &udc);
8d2f0039 85 if (ret) {
71002b50 86 pr_err("USB init failed: %d\n", ret);
8d2f0039
PK
87 return CMD_RET_FAILURE;
88 }
89
267abc62 90 g_dnl_clear_detach();
3aab70af
SS
91 ret = g_dnl_register("usb_dnl_fastboot");
92 if (ret)
93 return ret;
94
7c23bcb9
RH
95 if (!g_dnl_board_usb_cable_connected()) {
96 puts("\rUSB cable not detected.\n" \
97 "Command exit.\n");
8d2f0039
PK
98 ret = CMD_RET_FAILURE;
99 goto exit;
7c23bcb9
RH
100 }
101
3aab70af 102 while (1) {
267abc62
RH
103 if (g_dnl_detach())
104 break;
3aab70af
SS
105 if (ctrlc())
106 break;
29caf930 107 schedule();
bac356c3 108 dm_usb_gadget_handle_interrupts(udc);
3aab70af
SS
109 }
110
8d2f0039
PK
111 ret = CMD_RET_SUCCESS;
112
113exit:
bac356c3 114 udc_device_put(udc);
3aab70af 115 g_dnl_unregister();
267abc62 116 g_dnl_clear_detach();
8d2f0039
PK
117
118 return ret;
f73a7df9
AK
119}
120
09140113
SG
121static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc,
122 char *const argv[])
f73a7df9
AK
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;
7e5f460e 139 buf_addr = hextoul(*++argv, NULL);
f73a7df9
AK
140 goto NXTARG;
141
142 case 's':
143 if (--argc <= 0)
144 return CMD_RET_USAGE;
7e5f460e 145 buf_size = hextoul(*++argv, NULL);
f73a7df9
AK
146 goto NXTARG;
147
148 default:
149 return CMD_RET_USAGE;
150 }
151 }
152NXTARG:
153 ;
154 }
155
aa51579f
SP
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
cdd20e3f 162 fastboot_init((void *)buf_addr, buf_size);
f73a7df9
AK
163
164 if (!strcmp(argv[1], "udp"))
165 return do_fastboot_udp(argc, argv, buf_addr, buf_size);
443d3191
DM
166 if (!strcmp(argv[1], "tcp"))
167 return do_fastboot_tcp(argc, argv, buf_addr, buf_size);
f73a7df9
AK
168 if (!strcmp(argv[1], "usb")) {
169 argv++;
170 argc--;
171 }
172
173 return do_fastboot_usb(argc, argv, buf_addr, buf_size);
3aab70af
SS
174}
175
d0379900
PD
176U_BOOT_CMD(
177 fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot,
178 "run as a fastboot usb or udp device",
f73a7df9
AK
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) ")"
3aab70af 184);
This page took 0.263428 seconds and 4 git commands to generate.