blob: e6ae0570d5b2f29bc4f8125c78603cac7f6fa126 [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>
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050016
Alex Kiernand5aa57c2018-05-29 15:30:53 +000017static int do_fastboot_udp(int argc, char *const argv[],
18 uintptr_t buf_addr, size_t buf_size)
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050019{
Alex Kiernand5aa57c2018-05-29 15:30:53 +000020#if CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT)
21 int err = net_loop(FASTBOOT);
22
23 if (err < 0) {
24 printf("fastboot udp error: %d\n", err);
25 return CMD_RET_FAILURE;
26 }
27
28 return CMD_RET_SUCCESS;
29#else
30 pr_err("Fastboot UDP not enabled\n");
31 return CMD_RET_FAILURE;
32#endif
33}
34
35static int do_fastboot_usb(int argc, char *const argv[],
36 uintptr_t buf_addr, size_t buf_size)
37{
38#if CONFIG_IS_ENABLED(USB_FUNCTION_FASTBOOT)
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020039 int controller_index;
40 char *usb_controller;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050041 int ret;
42
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020043 if (argc < 2)
44 return CMD_RET_USAGE;
45
46 usb_controller = argv[1];
47 controller_index = simple_strtoul(usb_controller, NULL, 0);
48
49 ret = board_usb_init(controller_index, USB_INIT_DEVICE);
50 if (ret) {
Seung-Woo Kim591188e2018-06-04 16:04:51 +090051 pr_err("USB init failed: %d\n", ret);
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020052 return CMD_RET_FAILURE;
53 }
54
Rob Herring5aba3982014-12-10 14:43:04 -060055 g_dnl_clear_detach();
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050056 ret = g_dnl_register("usb_dnl_fastboot");
57 if (ret)
58 return ret;
59
Rob Herringce9d8ee2015-01-26 15:14:41 -060060 if (!g_dnl_board_usb_cable_connected()) {
61 puts("\rUSB cable not detected.\n" \
62 "Command exit.\n");
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020063 ret = CMD_RET_FAILURE;
64 goto exit;
Rob Herringce9d8ee2015-01-26 15:14:41 -060065 }
66
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050067 while (1) {
Rob Herring5aba3982014-12-10 14:43:04 -060068 if (g_dnl_detach())
69 break;
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050070 if (ctrlc())
71 break;
Paul Kocialkowski6b9f7282015-05-24 11:41:04 +020072 usb_gadget_handle_interrupts(controller_index);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050073 }
74
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020075 ret = CMD_RET_SUCCESS;
76
77exit:
Sebastian Siewior9d4471e2014-05-05 15:08:10 -050078 g_dnl_unregister();
Rob Herring5aba3982014-12-10 14:43:04 -060079 g_dnl_clear_detach();
Paul Kocialkowskid48378f2015-06-12 19:57:00 +020080 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
81
82 return ret;
Alex Kiernand5aa57c2018-05-29 15:30:53 +000083#else
84 pr_err("Fastboot USB not enabled\n");
85 return CMD_RET_FAILURE;
86#endif
87}
88
89static int do_fastboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
90{
91 uintptr_t buf_addr = (uintptr_t)NULL;
92 size_t buf_size = 0;
93
94 if (argc < 2)
95 return CMD_RET_USAGE;
96
97 while (argc > 1 && **(argv + 1) == '-') {
98 char *arg = *++argv;
99
100 --argc;
101 while (*++arg) {
102 switch (*arg) {
103 case 'l':
104 if (--argc <= 0)
105 return CMD_RET_USAGE;
106 buf_addr = simple_strtoul(*++argv, NULL, 16);
107 goto NXTARG;
108
109 case 's':
110 if (--argc <= 0)
111 return CMD_RET_USAGE;
112 buf_size = simple_strtoul(*++argv, NULL, 16);
113 goto NXTARG;
114
115 default:
116 return CMD_RET_USAGE;
117 }
118 }
119NXTARG:
120 ;
121 }
122
123 fastboot_init((void *)buf_addr, buf_size);
124
125 if (!strcmp(argv[1], "udp"))
126 return do_fastboot_udp(argc, argv, buf_addr, buf_size);
127
128 if (!strcmp(argv[1], "usb")) {
129 argv++;
130 argc--;
131 }
132
133 return do_fastboot_usb(argc, argv, buf_addr, buf_size);
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500134}
135
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000136#ifdef CONFIG_SYS_LONGHELP
137static char fastboot_help_text[] =
138 "[-l addr] [-s size] usb <controller> | udp\n"
139 "\taddr - address of buffer used during data transfers ("
140 __stringify(CONFIG_FASTBOOT_BUF_ADDR) ")\n"
141 "\tsize - size of buffer used during data transfers ("
142 __stringify(CONFIG_FASTBOOT_BUF_SIZE) ")"
143 ;
144#endif
145
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500146U_BOOT_CMD(
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000147 fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot,
148 "run as a fastboot usb or udp device", fastboot_help_text
Sebastian Siewior9d4471e2014-05-05 15:08:10 -0500149);