blob: 033a2c95e8f0b64524204d207874569ca0ccb6be [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 Glass1af3c7f2020-05-10 11:40:09 -060017#include <linux/stringify.h>
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050018
Alex Kiernanf73a7df2018-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 Siewior3aab70a2014-05-05 15:08:10 -050021{
Alex Kiernanf73a7df2018-05-29 15:30:53 +000022#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
37static 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)
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020041 int controller_index;
42 char *usb_controller;
Sam Protsenkoaa515792018-06-29 21:59:40 +030043 char *endp;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050044 int ret;
45
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020046 if (argc < 2)
47 return CMD_RET_USAGE;
48
49 usb_controller = argv[1];
Sam Protsenkoaa515792018-06-29 21:59:40 +030050 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 }
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020055
Jean-Jacques Hiblota06955a2018-11-29 10:52:41 +010056 ret = usb_gadget_initialize(controller_index);
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020057 if (ret) {
Seung-Woo Kim71002b52018-06-04 16:04:51 +090058 pr_err("USB init failed: %d\n", ret);
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020059 return CMD_RET_FAILURE;
60 }
61
Rob Herring267abc62014-12-10 14:43:04 -060062 g_dnl_clear_detach();
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050063 ret = g_dnl_register("usb_dnl_fastboot");
64 if (ret)
65 return ret;
66
Rob Herring7c23bcb2015-01-26 15:14:41 -060067 if (!g_dnl_board_usb_cable_connected()) {
68 puts("\rUSB cable not detected.\n" \
69 "Command exit.\n");
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020070 ret = CMD_RET_FAILURE;
71 goto exit;
Rob Herring7c23bcb2015-01-26 15:14:41 -060072 }
73
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050074 while (1) {
Rob Herring267abc62014-12-10 14:43:04 -060075 if (g_dnl_detach())
76 break;
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050077 if (ctrlc())
78 break;
Sean Nyekjaer731785d2019-01-24 15:17:21 +010079 WATCHDOG_RESET();
Paul Kocialkowskieca76b72015-05-24 11:41:04 +020080 usb_gadget_handle_interrupts(controller_index);
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050081 }
82
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020083 ret = CMD_RET_SUCCESS;
84
85exit:
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050086 g_dnl_unregister();
Rob Herring267abc62014-12-10 14:43:04 -060087 g_dnl_clear_detach();
Jean-Jacques Hiblota06955a2018-11-29 10:52:41 +010088 usb_gadget_release(controller_index);
Paul Kocialkowski8d2f0032015-06-12 19:57:00 +020089
90 return ret;
Alex Kiernanf73a7df2018-05-29 15:30:53 +000091#else
92 pr_err("Fastboot USB not enabled\n");
93 return CMD_RET_FAILURE;
94#endif
Sebastian Siewior3aab70a2014-05-05 15:08:10 -050095}
96
Simon Glass09140112020-05-10 11:40:03 -060097static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc,
98 char *const argv[])
Alex Kiernanf73a7df2018-05-29 15:30:53 +000099{
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;
Simon Glass7e5f4602021-07-24 09:03:29 -0600115 buf_addr = hextoul(*++argv, NULL);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000116 goto NXTARG;
117
118 case 's':
119 if (--argc <= 0)
120 return CMD_RET_USAGE;
Simon Glass7e5f4602021-07-24 09:03:29 -0600121 buf_size = hextoul(*++argv, NULL);
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000122 goto NXTARG;
123
124 default:
125 return CMD_RET_USAGE;
126 }
127 }
128NXTARG:
129 ;
130 }
131
Sam Protsenkoaa515792018-06-29 21:59:40 +0300132 /* 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
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000138 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);
149}
150
151#ifdef CONFIG_SYS_LONGHELP
152static 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
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500161U_BOOT_CMD(
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000162 fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot,
163 "run as a fastboot usb or udp device", fastboot_help_text
Sebastian Siewior3aab70a2014-05-05 15:08:10 -0500164);