blob: c3c19231c9889aa1e8ab9b56db4e81f31b10af43 [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 */
9#include <common.h>
10#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050012#include <g_dnl.h>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000013#include <fastboot.h>
14#include <net.h>
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020015#include <usb.h>
Sean Nyekjaer674f6622019-01-24 15:17:21 +010016#include <watchdog.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060017#include <linux/printk.h>
Simon Glassfb64e362020-05-10 11:40:09 -060018#include <linux/stringify.h>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050019
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}
59
Alex Kiernand5aa57c2018-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 Kocialkowskid48378f2015-06-12 19:57:00 +020063 int controller_index;
64 char *usb_controller;
Marek Vasut7f2e8982023-09-01 11:49:50 +020065 struct udevice *udc;
Sam Protsenko29f57342018-06-29 21:59:40 +030066 char *endp;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050067 int ret;
68
Simon Glassf597ebe2023-02-05 15:44:23 -070069 if (!IS_ENABLED(CONFIG_USB_FUNCTION_FASTBOOT)) {
Patrick Delaunayf82f9e42022-12-15 10:15:50 +010070 pr_err("Fastboot USB not enabled\n");
71 return CMD_RET_FAILURE;
72 }
73
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020074 if (argc < 2)
75 return CMD_RET_USAGE;
76
77 usb_controller = argv[1];
Sam Protsenko29f57342018-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 Kocialkowskid48378f2015-06-12 19:57:00 +020083
Marek Vasut7f2e8982023-09-01 11:49:50 +020084 ret = udc_device_get_by_index(controller_index, &udc);
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020085 if (ret) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +090086 pr_err("USB init failed: %d\n", ret);
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020087 return CMD_RET_FAILURE;
88 }
89
Rob Herring5aba3982014-12-10 14:43:04 -060090 g_dnl_clear_detach();
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050091 ret = g_dnl_register("usb_dnl_fastboot");
92 if (ret)
93 return ret;
94
Rob Herringce9d8ee2015-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 Kocialkowskid48378f2015-06-12 19:57:00 +020098 ret = CMD_RET_FAILURE;
99 goto exit;
Rob Herringce9d8ee2015-01-26 15:14:41 -0600100 }
101
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500102 while (1) {
Rob Herring5aba3982014-12-10 14:43:04 -0600103 if (g_dnl_detach())
104 break;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500105 if (ctrlc())
106 break;
Stefan Roese80877fa2022-09-02 14:10:46 +0200107 schedule();
Marek Vasut7f2e8982023-09-01 11:49:50 +0200108 dm_usb_gadget_handle_interrupts(udc);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500109 }
110
Paul Kocialkowskid48378f2015-06-12 19:57:00 +0200111 ret = CMD_RET_SUCCESS;
112
113exit:
Marek Vasut7f2e8982023-09-01 11:49:50 +0200114 udc_device_put(udc);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500115 g_dnl_unregister();
Rob Herring5aba3982014-12-10 14:43:04 -0600116 g_dnl_clear_detach();
Paul Kocialkowskid48378f2015-06-12 19:57:00 +0200117
118 return ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000119}
120
Simon Glassed38aef2020-05-10 11:40:03 -0600121static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc,
122 char *const argv[])
Alex Kiernand5aa57c2018-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 Glass3ff49ec2021-07-24 09:03:29 -0600139 buf_addr = hextoul(*++argv, NULL);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000140 goto NXTARG;
141
142 case 's':
143 if (--argc <= 0)
144 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600145 buf_size = hextoul(*++argv, NULL);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000146 goto NXTARG;
147
148 default:
149 return CMD_RET_USAGE;
150 }
151 }
152NXTARG:
153 ;
154 }
155
Sam Protsenko29f57342018-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
Tom Rini793921e2024-04-18 08:29:35 -0600162 fastboot_init((void *)buf_addr, buf_size);
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000163
164 if (!strcmp(argv[1], "udp"))
165 return do_fastboot_udp(argc, argv, buf_addr, buf_size);
Dmitrii Merkurev308252d2023-04-12 19:49:30 +0100166 if (!strcmp(argv[1], "tcp"))
167 return do_fastboot_tcp(argc, argv, buf_addr, buf_size);
Alex Kiernand5aa57c2018-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);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500174}
175
Patrick Delaunayf82f9e42022-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 Kiernand5aa57c2018-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 Siewior9d4471e2014-05-05 15:08:10 -0500184);