blob: be84a482b810d022c91126a04fc564b7d387cc8e [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
Michael Walleadd31702025-03-12 08:36:55 +010019#if CONFIG_IS_ENABLED(NET)
Alex Kiernand5aa57c2018-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 Siewior9d4471e2014-05-05 15:08:10 -050022{
Patrick Delaunayf82f9e42022-12-15 10:15:50 +010023 int err;
24
Simon Glassd911f3b2023-02-05 15:44:20 -070025 if (!IS_ENABLED(CONFIG_UDP_FUNCTION_FASTBOOT)) {
Patrick Delaunayf82f9e42022-12-15 10:15:50 +010026 pr_err("Fastboot UDP not enabled\n");
27 return CMD_RET_FAILURE;
28 }
29
Dmitrii Merkurev308252d2023-04-12 19:49:30 +010030 err = net_loop(FASTBOOT_UDP);
Alex Kiernand5aa57c2018-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 Kiernand5aa57c2018-05-29 15:30:53 +000038}
39
Dmitrii Merkurev308252d2023-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}
Michael Walleadd31702025-03-12 08:36:55 +010059#endif
Dmitrii Merkurev308252d2023-04-12 19:49:30 +010060
Alex Kiernand5aa57c2018-05-29 15:30:53 +000061static int do_fastboot_usb(int argc, char *const argv[],
62 uintptr_t buf_addr, size_t buf_size)
63{
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020064 int controller_index;
65 char *usb_controller;
Marek Vasut7f2e8982023-09-01 11:49:50 +020066 struct udevice *udc;
Sam Protsenko29f57342018-06-29 21:59:40 +030067 char *endp;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050068 int ret;
69
Simon Glassf597ebe2023-02-05 15:44:23 -070070 if (!IS_ENABLED(CONFIG_USB_FUNCTION_FASTBOOT)) {
Patrick Delaunayf82f9e42022-12-15 10:15:50 +010071 pr_err("Fastboot USB not enabled\n");
72 return CMD_RET_FAILURE;
73 }
74
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020075 if (argc < 2)
76 return CMD_RET_USAGE;
77
78 usb_controller = argv[1];
Sam Protsenko29f57342018-06-29 21:59:40 +030079 controller_index = simple_strtoul(usb_controller, &endp, 0);
80 if (*endp != '\0') {
81 pr_err("Error: Wrong USB controller index format\n");
82 return CMD_RET_FAILURE;
83 }
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020084
Marek Vasut7f2e8982023-09-01 11:49:50 +020085 ret = udc_device_get_by_index(controller_index, &udc);
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020086 if (ret) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +090087 pr_err("USB init failed: %d\n", ret);
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020088 return CMD_RET_FAILURE;
89 }
90
Rob Herring5aba3982014-12-10 14:43:04 -060091 g_dnl_clear_detach();
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050092 ret = g_dnl_register("usb_dnl_fastboot");
93 if (ret)
94 return ret;
95
Rob Herringce9d8ee2015-01-26 15:14:41 -060096 if (!g_dnl_board_usb_cable_connected()) {
97 puts("\rUSB cable not detected.\n" \
98 "Command exit.\n");
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020099 ret = CMD_RET_FAILURE;
100 goto exit;
Rob Herringce9d8ee2015-01-26 15:14:41 -0600101 }
102
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500103 while (1) {
Rob Herring5aba3982014-12-10 14:43:04 -0600104 if (g_dnl_detach())
105 break;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500106 if (ctrlc())
107 break;
Stefan Roese80877fa2022-09-02 14:10:46 +0200108 schedule();
Marek Vasut7f2e8982023-09-01 11:49:50 +0200109 dm_usb_gadget_handle_interrupts(udc);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500110 }
111
Paul Kocialkowskid48378f2015-06-12 19:57:00 +0200112 ret = CMD_RET_SUCCESS;
113
114exit:
Marek Vasut7f2e8982023-09-01 11:49:50 +0200115 udc_device_put(udc);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500116 g_dnl_unregister();
Rob Herring5aba3982014-12-10 14:43:04 -0600117 g_dnl_clear_detach();
Paul Kocialkowskid48378f2015-06-12 19:57:00 +0200118
119 return ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000120}
121
Simon Glassed38aef2020-05-10 11:40:03 -0600122static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc,
123 char *const argv[])
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000124{
125 uintptr_t buf_addr = (uintptr_t)NULL;
126 size_t buf_size = 0;
127
128 if (argc < 2)
129 return CMD_RET_USAGE;
130
131 while (argc > 1 && **(argv + 1) == '-') {
132 char *arg = *++argv;
133
134 --argc;
135 while (*++arg) {
136 switch (*arg) {
137 case 'l':
138 if (--argc <= 0)
139 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600140 buf_addr = hextoul(*++argv, NULL);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000141 goto NXTARG;
142
143 case 's':
144 if (--argc <= 0)
145 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600146 buf_size = hextoul(*++argv, NULL);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000147 goto NXTARG;
148
149 default:
150 return CMD_RET_USAGE;
151 }
152 }
153NXTARG:
154 ;
155 }
156
Sam Protsenko29f57342018-06-29 21:59:40 +0300157 /* Handle case when USB controller param is just '-' */
158 if (argc == 1) {
159 pr_err("Error: Incorrect USB controller index\n");
160 return CMD_RET_USAGE;
161 }
162
Tom Rini793921e2024-04-18 08:29:35 -0600163 fastboot_init((void *)buf_addr, buf_size);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000164
Michael Walleadd31702025-03-12 08:36:55 +0100165#if CONFIG_IS_ENABLED(NET)
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000166 if (!strcmp(argv[1], "udp"))
167 return do_fastboot_udp(argc, argv, buf_addr, buf_size);
Dmitrii Merkurev308252d2023-04-12 19:49:30 +0100168 if (!strcmp(argv[1], "tcp"))
169 return do_fastboot_tcp(argc, argv, buf_addr, buf_size);
Michael Walleadd31702025-03-12 08:36:55 +0100170#endif
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000171 if (!strcmp(argv[1], "usb")) {
172 argv++;
173 argc--;
174 }
175
176 return do_fastboot_usb(argc, argv, buf_addr, buf_size);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500177}
178
Patrick Delaunayf82f9e42022-12-15 10:15:50 +0100179U_BOOT_CMD(
180 fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot,
181 "run as a fastboot usb or udp device",
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000182 "[-l addr] [-s size] usb <controller> | udp\n"
183 "\taddr - address of buffer used during data transfers ("
184 __stringify(CONFIG_FASTBOOT_BUF_ADDR) ")\n"
185 "\tsize - size of buffer used during data transfers ("
186 __stringify(CONFIG_FASTBOOT_BUF_SIZE) ")"
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500187);