]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0+ | |
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]> | |
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> | |
18 | ||
19 | static int do_fastboot_udp(int argc, char *const argv[], | |
20 | uintptr_t buf_addr, size_t buf_size) | |
21 | { | |
22 | int err; | |
23 | ||
24 | if (!IS_ENABLED(CONFIG_UDP_FUNCTION_FASTBOOT)) { | |
25 | pr_err("Fastboot UDP not enabled\n"); | |
26 | return CMD_RET_FAILURE; | |
27 | } | |
28 | ||
29 | err = net_loop(FASTBOOT_UDP); | |
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; | |
37 | } | |
38 | ||
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 | ||
59 | static int do_fastboot_usb(int argc, char *const argv[], | |
60 | uintptr_t buf_addr, size_t buf_size) | |
61 | { | |
62 | int controller_index; | |
63 | char *usb_controller; | |
64 | struct udevice *udc; | |
65 | char *endp; | |
66 | int ret; | |
67 | ||
68 | if (!IS_ENABLED(CONFIG_USB_FUNCTION_FASTBOOT)) { | |
69 | pr_err("Fastboot USB not enabled\n"); | |
70 | return CMD_RET_FAILURE; | |
71 | } | |
72 | ||
73 | if (argc < 2) | |
74 | return CMD_RET_USAGE; | |
75 | ||
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; | |
81 | } | |
82 | ||
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; | |
87 | } | |
88 | ||
89 | g_dnl_clear_detach(); | |
90 | ret = g_dnl_register("usb_dnl_fastboot"); | |
91 | if (ret) | |
92 | return ret; | |
93 | ||
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; | |
99 | } | |
100 | ||
101 | while (1) { | |
102 | if (g_dnl_detach()) | |
103 | break; | |
104 | if (ctrlc()) | |
105 | break; | |
106 | schedule(); | |
107 | dm_usb_gadget_handle_interrupts(udc); | |
108 | } | |
109 | ||
110 | ret = CMD_RET_SUCCESS; | |
111 | ||
112 | exit: | |
113 | udc_device_put(udc); | |
114 | g_dnl_unregister(); | |
115 | g_dnl_clear_detach(); | |
116 | ||
117 | return ret; | |
118 | } | |
119 | ||
120 | static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc, | |
121 | char *const argv[]) | |
122 | { | |
123 | uintptr_t buf_addr = (uintptr_t)NULL; | |
124 | size_t buf_size = 0; | |
125 | ||
126 | if (argc < 2) | |
127 | return CMD_RET_USAGE; | |
128 | ||
129 | while (argc > 1 && **(argv + 1) == '-') { | |
130 | char *arg = *++argv; | |
131 | ||
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; | |
140 | ||
141 | case 's': | |
142 | if (--argc <= 0) | |
143 | return CMD_RET_USAGE; | |
144 | buf_size = hextoul(*++argv, NULL); | |
145 | goto NXTARG; | |
146 | ||
147 | default: | |
148 | return CMD_RET_USAGE; | |
149 | } | |
150 | } | |
151 | NXTARG: | |
152 | ; | |
153 | } | |
154 | ||
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; | |
159 | } | |
160 | ||
161 | fastboot_init((void *)buf_addr, buf_size); | |
162 | ||
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--; | |
170 | } | |
171 | ||
172 | return do_fastboot_usb(argc, argv, buf_addr, buf_size); | |
173 | } | |
174 | ||
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) ")" | |
183 | ); |