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