2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-05-05 20:08:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2008 - 2009 Windriver, <www.windriver.com>
|
|
|
|
* Author: Tom Rix <Tom.Rix@windriver.com>
|
|
|
|
*
|
|
|
|
* (C) Copyright 2014 Linaro, Ltd.
|
|
|
|
* Rob Herring <robh@kernel.org>
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
2015-11-09 06:47:45 +00:00
|
|
|
#include <console.h>
|
2014-05-05 20:08:10 +00:00
|
|
|
#include <g_dnl.h>
|
2018-05-29 15:30:53 +00:00
|
|
|
#include <fastboot.h>
|
|
|
|
#include <net.h>
|
2015-06-12 17:57:00 +00:00
|
|
|
#include <usb.h>
|
2019-01-24 14:17:21 +00:00
|
|
|
#include <watchdog.h>
|
2020-05-10 17:40:09 +00:00
|
|
|
#include <linux/stringify.h>
|
2014-05-05 20:08:10 +00:00
|
|
|
|
2018-05-29 15:30:53 +00:00
|
|
|
static int do_fastboot_udp(int argc, char *const argv[],
|
|
|
|
uintptr_t buf_addr, size_t buf_size)
|
2014-05-05 20:08:10 +00:00
|
|
|
{
|
2022-12-15 09:15:50 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT)) {
|
|
|
|
pr_err("Fastboot UDP not enabled\n");
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = net_loop(FASTBOOT);
|
2018-05-29 15:30:53 +00:00
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
printf("fastboot udp error: %d\n", err);
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMD_RET_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_fastboot_usb(int argc, char *const argv[],
|
|
|
|
uintptr_t buf_addr, size_t buf_size)
|
|
|
|
{
|
2015-06-12 17:57:00 +00:00
|
|
|
int controller_index;
|
|
|
|
char *usb_controller;
|
2018-06-29 18:59:40 +00:00
|
|
|
char *endp;
|
2014-05-05 20:08:10 +00:00
|
|
|
int ret;
|
|
|
|
|
2022-12-15 09:15:50 +00:00
|
|
|
if (!CONFIG_IS_ENABLED(USB_FUNCTION_FASTBOOT)) {
|
|
|
|
pr_err("Fastboot USB not enabled\n");
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-06-12 17:57:00 +00:00
|
|
|
if (argc < 2)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
|
|
|
usb_controller = argv[1];
|
2018-06-29 18:59:40 +00:00
|
|
|
controller_index = simple_strtoul(usb_controller, &endp, 0);
|
|
|
|
if (*endp != '\0') {
|
|
|
|
pr_err("Error: Wrong USB controller index format\n");
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
2015-06-12 17:57:00 +00:00
|
|
|
|
2018-11-29 09:52:41 +00:00
|
|
|
ret = usb_gadget_initialize(controller_index);
|
2015-06-12 17:57:00 +00:00
|
|
|
if (ret) {
|
2018-06-04 07:04:51 +00:00
|
|
|
pr_err("USB init failed: %d\n", ret);
|
2015-06-12 17:57:00 +00:00
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-12-10 20:43:04 +00:00
|
|
|
g_dnl_clear_detach();
|
2014-05-05 20:08:10 +00:00
|
|
|
ret = g_dnl_register("usb_dnl_fastboot");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-01-26 21:14:41 +00:00
|
|
|
if (!g_dnl_board_usb_cable_connected()) {
|
|
|
|
puts("\rUSB cable not detected.\n" \
|
|
|
|
"Command exit.\n");
|
2015-06-12 17:57:00 +00:00
|
|
|
ret = CMD_RET_FAILURE;
|
|
|
|
goto exit;
|
2015-01-26 21:14:41 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 20:08:10 +00:00
|
|
|
while (1) {
|
2014-12-10 20:43:04 +00:00
|
|
|
if (g_dnl_detach())
|
|
|
|
break;
|
2014-05-05 20:08:10 +00:00
|
|
|
if (ctrlc())
|
|
|
|
break;
|
2022-09-02 12:10:46 +00:00
|
|
|
schedule();
|
2015-05-24 09:41:04 +00:00
|
|
|
usb_gadget_handle_interrupts(controller_index);
|
2014-05-05 20:08:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 17:57:00 +00:00
|
|
|
ret = CMD_RET_SUCCESS;
|
|
|
|
|
|
|
|
exit:
|
2022-10-07 09:38:22 +00:00
|
|
|
usb_gadget_release(controller_index);
|
2014-05-05 20:08:10 +00:00
|
|
|
g_dnl_unregister();
|
2014-12-10 20:43:04 +00:00
|
|
|
g_dnl_clear_detach();
|
2015-06-12 17:57:00 +00:00
|
|
|
|
|
|
|
return ret;
|
2018-05-29 15:30:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 17:40:03 +00:00
|
|
|
static int do_fastboot(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
2018-05-29 15:30:53 +00:00
|
|
|
{
|
|
|
|
uintptr_t buf_addr = (uintptr_t)NULL;
|
|
|
|
size_t buf_size = 0;
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
|
|
|
while (argc > 1 && **(argv + 1) == '-') {
|
|
|
|
char *arg = *++argv;
|
|
|
|
|
|
|
|
--argc;
|
|
|
|
while (*++arg) {
|
|
|
|
switch (*arg) {
|
|
|
|
case 'l':
|
|
|
|
if (--argc <= 0)
|
|
|
|
return CMD_RET_USAGE;
|
2021-07-24 15:03:29 +00:00
|
|
|
buf_addr = hextoul(*++argv, NULL);
|
2018-05-29 15:30:53 +00:00
|
|
|
goto NXTARG;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
if (--argc <= 0)
|
|
|
|
return CMD_RET_USAGE;
|
2021-07-24 15:03:29 +00:00
|
|
|
buf_size = hextoul(*++argv, NULL);
|
2018-05-29 15:30:53 +00:00
|
|
|
goto NXTARG;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NXTARG:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2018-06-29 18:59:40 +00:00
|
|
|
/* Handle case when USB controller param is just '-' */
|
|
|
|
if (argc == 1) {
|
|
|
|
pr_err("Error: Incorrect USB controller index\n");
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:30:53 +00:00
|
|
|
fastboot_init((void *)buf_addr, buf_size);
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "udp"))
|
|
|
|
return do_fastboot_udp(argc, argv, buf_addr, buf_size);
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "usb")) {
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return do_fastboot_usb(argc, argv, buf_addr, buf_size);
|
2014-05-05 20:08:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 09:15:50 +00:00
|
|
|
U_BOOT_CMD(
|
|
|
|
fastboot, CONFIG_SYS_MAXARGS, 1, do_fastboot,
|
|
|
|
"run as a fastboot usb or udp device",
|
2018-05-29 15:30:53 +00:00
|
|
|
"[-l addr] [-s size] usb <controller> | udp\n"
|
|
|
|
"\taddr - address of buffer used during data transfers ("
|
|
|
|
__stringify(CONFIG_FASTBOOT_BUF_ADDR) ")\n"
|
|
|
|
"\tsize - size of buffer used during data transfers ("
|
|
|
|
__stringify(CONFIG_FASTBOOT_BUF_SIZE) ")"
|
2014-05-05 20:08:10 +00:00
|
|
|
);
|