blob: d4cfc0c7a282b8697f50c207fa8cb46171797ee1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sebastian Siewior9d4471e2014-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 Siewior9d4471e2014-05-05 15:08:10 -05008 */
Sebastian Siewior9d4471e2014-05-05 15:08:10 -05009#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070010#include <console.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050011#include <g_dnl.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000012#include <fastboot.h>
13#include <net.h>
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020014#include <usb.h>
Sean Nyekjaer674f6622019-01-24 15:17:21 +010015#include <watchdog.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060016#include <linux/printk.h>
Simon Glassfb64e362020-05-10 11:40:09 -060017#include <linux/stringify.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050018
Alex Kiernand5aa57c2018-05-29 15:30:53 +000019static int do_fastboot_udp(int argc, char *const argv[],
20 uintptr_t buf_addr, size_t buf_size)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050021{
Patrick Delaunayf82f9e42022-12-15 10:15:50 +010022 int err;
23
Simon Glassd911f3b2023-02-05 15:44:20 -070024 if (!IS_ENABLED(CONFIG_UDP_FUNCTION_FASTBOOT)) {
Patrick Delaunayf82f9e42022-12-15 10:15:50 +010025 pr_err("Fastboot UDP not enabled\n");
26 return CMD_RET_FAILURE;
27 }
28
Dmitrii Merkurev308252d2023-04-12 19:49:30 +010029 err = net_loop(FASTBOOT_UDP);
Alex Kiernand5aa57c2018-05-29 15:30:53 +000030
31 if (err < 0) {
32 printf("fastboot udp error: %d\n", err);
33 return CMD_RET_FAILURE;
34 }
35
36 return CMD_RET_SUCCESS;
Alex Kiernand5aa57c2018-05-29 15:30:53 +000037}
38
Dmitrii Merkurev308252d2023-04-12 19:49:30 +010039static 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
Alex Kiernand5aa57c2018-05-29 15:30:53 +000059static int do_fastboot_usb(int argc, char *const argv[],
60 uintptr_t buf_addr, size_t buf_size)
61{
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020062 int controller_index;
63 char *usb_controller;
Marek Vasut7f2e8982023-09-01 11:49:50 +020064 struct udevice *udc;
Sam Protsenko29f57342018-06-29 21:59:40 +030065 char *endp;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050066 int ret;
67
Simon Glassf597ebe2023-02-05 15:44:23 -070068 if (!IS_ENABLED(CONFIG_USB_FUNCTION_FASTBOOT)) {
Patrick Delaunayf82f9e42022-12-15 10:15:50 +010069 pr_err("Fastboot USB not enabled\n");
70 return CMD_RET_FAILURE;
71 }
72
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020073 if (argc < 2)
74 return CMD_RET_USAGE;
75
76 usb_controller = argv[1];
Sam Protsenko29f57342018-06-29 21:59:40 +030077 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 }
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020082
Marek Vasut7f2e8982023-09-01 11:49:50 +020083 ret = udc_device_get_by_index(controller_index, &udc);
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020084 if (ret) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +090085 pr_err("USB init failed: %d\n", ret);
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020086 return CMD_RET_FAILURE;
87 }
88
Rob Herring5aba3982014-12-10 14:43:04 -060089 g_dnl_clear_detach();
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050090 ret = g_dnl_register("usb_dnl_fastboot");
91 if (ret)
92 return ret;
93
Rob Herringce9d8ee2015-01-26 15:14:41 -060094 if (!g_dnl_board_usb_cable_connected()) {
95 puts("\rUSB cable not detected.\n" \
96 "Command exit.\n");
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020097 ret = CMD_RET_FAILURE;
98 goto exit;
Rob Herringce9d8ee2015-01-26 15:14:41 -060099 }
100
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500101 while (1) {
Rob Herring5aba3982014-12-10 14:43:04 -0600102 if (g_dnl_detach())
103 break;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500104 if (ctrlc())
105 break;
Stefan Roese80877fa2022-09-02 14:10:46 +0200106 schedule();
Marek Vasut7f2e8982023-09-01 11:49:50 +0200107 dm_usb_gadget_handle_interrupts(udc);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500108 }
109
Paul Kocialkowskid48378f2015-06-12 19:57:00 +0200110 ret = CMD_RET_SUCCESS;
111
112exit:
Marek Vasut7f2e8982023-09-01 11:49:50 +0200113 udc_device_put(udc);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500114 g_dnl_unregister();
Rob Herring5aba3982014-12-10 14:43:04 -0600115 g_dnl_clear_detach();
Paul Kocialkowskid48378f2015-06-12 19:57:00 +0200116
117 return ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000118}
119
Simon Glassed38aef2020-05-10 11:40:03 -0600120static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc,
121 char *const argv[])
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000122{
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;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600138 buf_addr = hextoul(*++argv, NULL);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000139 goto NXTARG;
140
141 case 's':
142 if (--argc <= 0)
143 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600144 buf_size = hextoul(*++argv, NULL);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000145 goto NXTARG;
146
147 default:
148 return CMD_RET_USAGE;
149 }
150 }
151NXTARG:
152 ;
153 }
154
Sam Protsenko29f57342018-06-29 21:59:40 +0300155 /* 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
Tom Rini793921e2024-04-18 08:29:35 -0600161 fastboot_init((void *)buf_addr, buf_size);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000162
163 if (!strcmp(argv[1], "udp"))
164 return do_fastboot_udp(argc, argv, buf_addr, buf_size);
Dmitrii Merkurev308252d2023-04-12 19:49:30 +0100165 if (!strcmp(argv[1], "tcp"))
166 return do_fastboot_tcp(argc, argv, buf_addr, buf_size);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000167 if (!strcmp(argv[1], "usb")) {
168 argv++;
169 argc--;
170 }
171
172 return do_fastboot_usb(argc, argv, buf_addr, buf_size);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500173}
174
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100175U_BOOT_CMD(
176 fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot,
177 "run as a fastboot usb or udp device",
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000178 "[-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) ")"
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500183);