blob: c3c19231c9889aa1e8ab9b56db4e81f31b10af43 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior3aab70a2014-05-05 15:08:10 -05002/*
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 Siewior3aab70a2014-05-05 15:08:10 -05008 */
9#include <common.h>
10#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070011#include <console.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050012#include <g_dnl.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000013#include <fastboot.h>
14#include <net.h>
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020015#include <usb.h>
Sean Nyekjaer731785d2019-01-24 15:17:21 +010016#include <watchdog.h>
Simon Glass1e94b462023-09-14 18:21:46 -060017#include <linux/printk.h>
Simon Glass1af3c7f2020-05-10 11:40:09 -060018#include <linux/stringify.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050019
Alex Kiernanf73a7df2018-05-29 15:30:53 +000020static int do_fastboot_udp(int argc, char *const argv[],
21 uintptr_t buf_addr, size_t buf_size)
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050022{
Patrick Delaunayd0379902022-12-15 10:15:50 +010023 int err;
24
Simon Glass942b3092023-02-05 15:44:20 -070025 if (!IS_ENABLED(CONFIG_UDP_FUNCTION_FASTBOOT)) {
Patrick Delaunayd0379902022-12-15 10:15:50 +010026 pr_err("Fastboot UDP not enabled\n");
27 return CMD_RET_FAILURE;
28 }
29
Dmitrii Merkurev443d3192023-04-12 19:49:30 +010030 err = net_loop(FASTBOOT_UDP);
Alex Kiernanf73a7df2018-05-29 15:30:53 +000031
32 if (err < 0) {
33 printf("fastboot udp error: %d\n", err);
34 return CMD_RET_FAILURE;
35 }
36
37 return CMD_RET_SUCCESS;
Alex Kiernanf73a7df2018-05-29 15:30:53 +000038}
39
Dmitrii Merkurev443d3192023-04-12 19:49:30 +010040static 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
Alex Kiernanf73a7df2018-05-29 15:30:53 +000060static int do_fastboot_usb(int argc, char *const argv[],
61 uintptr_t buf_addr, size_t buf_size)
62{
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020063 int controller_index;
64 char *usb_controller;
Marek Vasutbac356c2023-09-01 11:49:50 +020065 struct udevice *udc;
Sam Protsenkoaa515792018-06-29 21:59:40 +030066 char *endp;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050067 int ret;
68
Simon Glass37407ac2023-02-05 15:44:23 -070069 if (!IS_ENABLED(CONFIG_USB_FUNCTION_FASTBOOT)) {
Patrick Delaunayd0379902022-12-15 10:15:50 +010070 pr_err("Fastboot USB not enabled\n");
71 return CMD_RET_FAILURE;
72 }
73
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020074 if (argc < 2)
75 return CMD_RET_USAGE;
76
77 usb_controller = argv[1];
Sam Protsenkoaa515792018-06-29 21:59:40 +030078 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 }
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020083
Marek Vasutbac356c2023-09-01 11:49:50 +020084 ret = udc_device_get_by_index(controller_index, &udc);
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020085 if (ret) {
Seung-Woo Kim71002b52018-06-04 16:04:51 +090086 pr_err("USB init failed: %d\n", ret);
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020087 return CMD_RET_FAILURE;
88 }
89
Rob Herring267abc62014-12-10 14:43:04 -060090 g_dnl_clear_detach();
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050091 ret = g_dnl_register("usb_dnl_fastboot");
92 if (ret)
93 return ret;
94
Rob Herring7c23bcb2015-01-26 15:14:41 -060095 if (!g_dnl_board_usb_cable_connected()) {
96 puts("\rUSB cable not detected.\n" \
97 "Command exit.\n");
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020098 ret = CMD_RET_FAILURE;
99 goto exit;
Rob Herring7c23bcb2015-01-26 15:14:41 -0600100 }
101
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500102 while (1) {
Rob Herring267abc62014-12-10 14:43:04 -0600103 if (g_dnl_detach())
104 break;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500105 if (ctrlc())
106 break;
Stefan Roese29caf932022-09-02 14:10:46 +0200107 schedule();
Marek Vasutbac356c2023-09-01 11:49:50 +0200108 dm_usb_gadget_handle_interrupts(udc);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500109 }
110
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +0200111 ret = CMD_RET_SUCCESS;
112
113exit:
Marek Vasutbac356c2023-09-01 11:49:50 +0200114 udc_device_put(udc);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500115 g_dnl_unregister();
Rob Herring267abc62014-12-10 14:43:04 -0600116 g_dnl_clear_detach();
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +0200117
118 return ret;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500119}
120
Simon Glass09140112020-05-10 11:40:03 -0600121static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc,
122 char *const argv[])
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000123{
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;
Simon Glass7e5f4602021-07-24 09:03:29 -0600139 buf_addr = hextoul(*++argv, NULL);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000140 goto NXTARG;
141
142 case 's':
143 if (--argc <= 0)
144 return CMD_RET_USAGE;
Simon Glass7e5f4602021-07-24 09:03:29 -0600145 buf_size = hextoul(*++argv, NULL);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000146 goto NXTARG;
147
148 default:
149 return CMD_RET_USAGE;
150 }
151 }
152NXTARG:
153 ;
154 }
155
Sam Protsenkoaa515792018-06-29 21:59:40 +0300156 /* 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
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000162 fastboot_init((void *)buf_addr, buf_size);
163
164 if (!strcmp(argv[1], "udp"))
165 return do_fastboot_udp(argc, argv, buf_addr, buf_size);
Dmitrii Merkurev443d3192023-04-12 19:49:30 +0100166 if (!strcmp(argv[1], "tcp"))
167 return do_fastboot_tcp(argc, argv, buf_addr, buf_size);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000168 if (!strcmp(argv[1], "usb")) {
169 argv++;
170 argc--;
171 }
172
173 return do_fastboot_usb(argc, argv, buf_addr, buf_size);
174}
175
Patrick Delaunayd0379902022-12-15 10:15:50 +0100176U_BOOT_CMD(
177 fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot,
178 "run as a fastboot usb or udp device",
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000179 "[-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) ")"
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500184);