]>
Commit | Line | Data |
---|---|---|
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> |
1af3c7f4 | 17 | #include <linux/stringify.h> |
3aab70af | 18 | |
f73a7df9 AK |
19 | static int do_fastboot_udp(int argc, char *const argv[], |
20 | uintptr_t buf_addr, size_t buf_size) | |
3aab70af | 21 | { |
f73a7df9 AK |
22 | #if CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT) |
23 | int err = net_loop(FASTBOOT); | |
24 | ||
25 | if (err < 0) { | |
26 | printf("fastboot udp error: %d\n", err); | |
27 | return CMD_RET_FAILURE; | |
28 | } | |
29 | ||
30 | return CMD_RET_SUCCESS; | |
31 | #else | |
32 | pr_err("Fastboot UDP not enabled\n"); | |
33 | return CMD_RET_FAILURE; | |
34 | #endif | |
35 | } | |
36 | ||
37 | static int do_fastboot_usb(int argc, char *const argv[], | |
38 | uintptr_t buf_addr, size_t buf_size) | |
39 | { | |
40 | #if CONFIG_IS_ENABLED(USB_FUNCTION_FASTBOOT) | |
8d2f0039 PK |
41 | int controller_index; |
42 | char *usb_controller; | |
aa51579f | 43 | char *endp; |
3aab70af SS |
44 | int ret; |
45 | ||
8d2f0039 PK |
46 | if (argc < 2) |
47 | return CMD_RET_USAGE; | |
48 | ||
49 | usb_controller = argv[1]; | |
aa51579f SP |
50 | controller_index = simple_strtoul(usb_controller, &endp, 0); |
51 | if (*endp != '\0') { | |
52 | pr_err("Error: Wrong USB controller index format\n"); | |
53 | return CMD_RET_FAILURE; | |
54 | } | |
8d2f0039 | 55 | |
a06955ae | 56 | ret = usb_gadget_initialize(controller_index); |
8d2f0039 | 57 | if (ret) { |
71002b50 | 58 | pr_err("USB init failed: %d\n", ret); |
8d2f0039 PK |
59 | return CMD_RET_FAILURE; |
60 | } | |
61 | ||
267abc62 | 62 | g_dnl_clear_detach(); |
3aab70af SS |
63 | ret = g_dnl_register("usb_dnl_fastboot"); |
64 | if (ret) | |
65 | return ret; | |
66 | ||
7c23bcb9 RH |
67 | if (!g_dnl_board_usb_cable_connected()) { |
68 | puts("\rUSB cable not detected.\n" \ | |
69 | "Command exit.\n"); | |
8d2f0039 PK |
70 | ret = CMD_RET_FAILURE; |
71 | goto exit; | |
7c23bcb9 RH |
72 | } |
73 | ||
3aab70af | 74 | while (1) { |
267abc62 RH |
75 | if (g_dnl_detach()) |
76 | break; | |
3aab70af SS |
77 | if (ctrlc()) |
78 | break; | |
731785df | 79 | WATCHDOG_RESET(); |
eca76b74 | 80 | usb_gadget_handle_interrupts(controller_index); |
3aab70af SS |
81 | } |
82 | ||
8d2f0039 PK |
83 | ret = CMD_RET_SUCCESS; |
84 | ||
85 | exit: | |
3aab70af | 86 | g_dnl_unregister(); |
267abc62 | 87 | g_dnl_clear_detach(); |
a06955ae | 88 | usb_gadget_release(controller_index); |
8d2f0039 PK |
89 | |
90 | return ret; | |
f73a7df9 AK |
91 | #else |
92 | pr_err("Fastboot USB not enabled\n"); | |
93 | return CMD_RET_FAILURE; | |
94 | #endif | |
95 | } | |
96 | ||
09140113 SG |
97 | static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc, |
98 | char *const argv[]) | |
f73a7df9 AK |
99 | { |
100 | uintptr_t buf_addr = (uintptr_t)NULL; | |
101 | size_t buf_size = 0; | |
102 | ||
103 | if (argc < 2) | |
104 | return CMD_RET_USAGE; | |
105 | ||
106 | while (argc > 1 && **(argv + 1) == '-') { | |
107 | char *arg = *++argv; | |
108 | ||
109 | --argc; | |
110 | while (*++arg) { | |
111 | switch (*arg) { | |
112 | case 'l': | |
113 | if (--argc <= 0) | |
114 | return CMD_RET_USAGE; | |
7e5f460e | 115 | buf_addr = hextoul(*++argv, NULL); |
f73a7df9 AK |
116 | goto NXTARG; |
117 | ||
118 | case 's': | |
119 | if (--argc <= 0) | |
120 | return CMD_RET_USAGE; | |
7e5f460e | 121 | buf_size = hextoul(*++argv, NULL); |
f73a7df9 AK |
122 | goto NXTARG; |
123 | ||
124 | default: | |
125 | return CMD_RET_USAGE; | |
126 | } | |
127 | } | |
128 | NXTARG: | |
129 | ; | |
130 | } | |
131 | ||
aa51579f SP |
132 | /* Handle case when USB controller param is just '-' */ |
133 | if (argc == 1) { | |
134 | pr_err("Error: Incorrect USB controller index\n"); | |
135 | return CMD_RET_USAGE; | |
136 | } | |
137 | ||
f73a7df9 AK |
138 | fastboot_init((void *)buf_addr, buf_size); |
139 | ||
140 | if (!strcmp(argv[1], "udp")) | |
141 | return do_fastboot_udp(argc, argv, buf_addr, buf_size); | |
142 | ||
143 | if (!strcmp(argv[1], "usb")) { | |
144 | argv++; | |
145 | argc--; | |
146 | } | |
147 | ||
148 | return do_fastboot_usb(argc, argv, buf_addr, buf_size); | |
3aab70af SS |
149 | } |
150 | ||
f73a7df9 AK |
151 | #ifdef CONFIG_SYS_LONGHELP |
152 | static char fastboot_help_text[] = | |
153 | "[-l addr] [-s size] usb <controller> | udp\n" | |
154 | "\taddr - address of buffer used during data transfers (" | |
155 | __stringify(CONFIG_FASTBOOT_BUF_ADDR) ")\n" | |
156 | "\tsize - size of buffer used during data transfers (" | |
157 | __stringify(CONFIG_FASTBOOT_BUF_SIZE) ")" | |
158 | ; | |
159 | #endif | |
160 | ||
3aab70af | 161 | U_BOOT_CMD( |
f73a7df9 AK |
162 | fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot, |
163 | "run as a fastboot usb or udp device", fastboot_help_text | |
3aab70af | 164 | ); |