xref: /openbmc/u-boot/cmd/dfu.c (revision 9e6866d3)
1 /*
2  * cmd_dfu.c -- dfu command
3  *
4  * Copyright (C) 2015
5  * Lukasz Majewski <l.majewski@majess.pl>
6  *
7  * Copyright (C) 2012 Samsung Electronics
8  * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
9  *	    Lukasz Majewski <l.majewski@samsung.com>
10  *
11  * SPDX-License-Identifier:	GPL-2.0+
12  */
13 
14 #include <common.h>
15 #include <watchdog.h>
16 #include <dfu.h>
17 #include <console.h>
18 #include <g_dnl.h>
19 #include <usb.h>
20 #include <net.h>
21 
22 static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
23 {
24 	bool dfu_reset = false;
25 
26 	if (argc < 4)
27 		return CMD_RET_USAGE;
28 
29 	char *usb_controller = argv[1];
30 	char *interface = argv[2];
31 	char *devstring = argv[3];
32 
33 	int ret, i = 0;
34 #ifdef CONFIG_DFU_TFTP
35 	unsigned long addr = 0;
36 	if (!strcmp(argv[1], "tftp")) {
37 		if (argc == 5)
38 			addr = simple_strtoul(argv[4], NULL, 0);
39 
40 		return update_tftp(addr, interface, devstring);
41 	}
42 #endif
43 
44 	ret = dfu_init_env_entities(interface, devstring);
45 	if (ret)
46 		goto done;
47 
48 	ret = CMD_RET_SUCCESS;
49 	if (argc > 4 && strcmp(argv[4], "list") == 0) {
50 		dfu_show_entities();
51 		goto done;
52 	}
53 
54 	int controller_index = simple_strtoul(usb_controller, NULL, 0);
55 	board_usb_init(controller_index, USB_INIT_DEVICE);
56 	g_dnl_clear_detach();
57 	g_dnl_register("usb_dnl_dfu");
58 	while (1) {
59 		if (g_dnl_detach()) {
60 			/*
61 			 * Check if USB bus reset is performed after detach,
62 			 * which indicates that -R switch has been passed to
63 			 * dfu-util. In this case reboot the device
64 			 */
65 			if (dfu_usb_get_reset()) {
66 				dfu_reset = true;
67 				goto exit;
68 			}
69 
70 			/*
71 			 * This extra number of usb_gadget_handle_interrupts()
72 			 * calls is necessary to assure correct transmission
73 			 * completion with dfu-util
74 			 */
75 			if (++i == 10000)
76 				goto exit;
77 		}
78 
79 		if (ctrlc())
80 			goto exit;
81 
82 		WATCHDOG_RESET();
83 		usb_gadget_handle_interrupts(controller_index);
84 	}
85 exit:
86 	g_dnl_unregister();
87 	board_usb_cleanup(controller_index, USB_INIT_DEVICE);
88 done:
89 	dfu_free_entities();
90 
91 	if (dfu_reset)
92 		run_command("reset", 0);
93 
94 	g_dnl_clear_detach();
95 
96 	return ret;
97 }
98 
99 U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
100 	"Device Firmware Upgrade",
101 	"<USB_controller> <interface> <dev> [list]\n"
102 	"  - device firmware upgrade via <USB_controller>\n"
103 	"    on device <dev>, attached to interface\n"
104 	"    <interface>\n"
105 	"    [list] - list available alt settings\n"
106 #ifdef CONFIG_DFU_TFTP
107 	"dfu tftp <interface> <dev> [<addr>]\n"
108 	"  - device firmware upgrade via TFTP\n"
109 	"    on device <dev>, attached to interface\n"
110 	"    <interface>\n"
111 	"    [<addr>] - address where FIT image has been stored\n"
112 #endif
113 );
114