1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * cmd_dfu.c -- dfu command
4  *
5  * Copyright (C) 2015
6  * Lukasz Majewski <l.majewski@majess.pl>
7  *
8  * Copyright (C) 2012 Samsung Electronics
9  * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
10  *	    Lukasz Majewski <l.majewski@samsung.com>
11  */
12 
13 #include <common.h>
14 #include <command.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 
do_dfu(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])22 static int do_dfu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
23 {
24 
25 	if (argc < 2)
26 		return CMD_RET_USAGE;
27 
28 #ifdef CONFIG_DFU_OVER_USB
29 	char *usb_controller = argv[1];
30 #endif
31 #if defined(CONFIG_DFU_OVER_USB) || defined(CONFIG_DFU_OVER_TFTP)
32 	char *interface = NULL;
33 	char *devstring = NULL;
34 #if defined(CONFIG_DFU_TIMEOUT) || defined(CONFIG_DFU_OVER_TFTP)
35 	unsigned long value = 0;
36 #endif
37 	if (argc >= 4) {
38 		interface = argv[2];
39 		devstring = argv[3];
40 	}
41 
42 #if defined(CONFIG_DFU_TIMEOUT) || defined(CONFIG_DFU_OVER_TFTP)
43 	if (argc == 5 || argc == 3)
44 		value = simple_strtoul(argv[argc - 1], NULL, 0);
45 #endif
46 #endif
47 
48 	int ret = 0;
49 #ifdef CONFIG_DFU_OVER_TFTP
50 	if (!strcmp(argv[1], "tftp"))
51 		return update_tftp(value, interface, devstring);
52 #endif
53 #ifdef CONFIG_DFU_OVER_USB
54 	ret = dfu_init_env_entities(interface, devstring);
55 	if (ret)
56 		goto done;
57 
58 #ifdef CONFIG_DFU_TIMEOUT
59 	dfu_set_timeout(value * 1000);
60 #endif
61 
62 	ret = CMD_RET_SUCCESS;
63 	if (strcmp(argv[argc - 1], "list") == 0) {
64 		dfu_show_entities();
65 		goto done;
66 	}
67 
68 	int controller_index = simple_strtoul(usb_controller, NULL, 0);
69 	bool retry = false;
70 	do {
71 		run_usb_dnl_gadget(controller_index, "usb_dnl_dfu");
72 
73 		if (dfu_reinit_needed) {
74 			dfu_free_entities();
75 			ret = dfu_init_env_entities(interface, devstring);
76 			if (ret)
77 				goto done;
78 			retry = true;
79 		}
80 	} while (retry);
81 
82 done:
83 	dfu_free_entities();
84 #endif
85 	return ret;
86 }
87 
88 U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
89 	"Device Firmware Upgrade",
90 	""
91 #ifdef CONFIG_DFU_OVER_USB
92 #ifdef CONFIG_DFU_TIMEOUT
93 	"<USB_controller> [<interface> <dev>] [<timeout>] [list]\n"
94 #else
95 	"<USB_controller> [<interface> <dev>] [list]\n"
96 #endif
97 	"  - device firmware upgrade via <USB_controller>\n"
98 	"    on device <dev>, attached to interface\n"
99 	"    <interface>\n"
100 #ifdef CONFIG_DFU_TIMEOUT
101 	"    [<timeout>] - specify inactivity timeout in seconds\n"
102 #endif
103 	"    [list] - list available alt settings\n"
104 #endif
105 #ifdef CONFIG_DFU_OVER_TFTP
106 #ifdef CONFIG_DFU_OVER_USB
107 	"dfu "
108 #endif
109 	"tftp [<interface> <dev>] [<addr>]\n"
110 	"  - device firmware upgrade via TFTP\n"
111 	"    on device <dev>, attached to interface\n"
112 	"    <interface>\n"
113 	"    [<addr>] - address where FIT image has been stored\n"
114 #endif
115 );
116