7ce92cec87f4fcb156b2d84ff487cb319635f6a4
[oweals/u-boot.git] / common / cmd_dfu.c
1 /*
2  * cmd_dfu.c -- dfu command
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
6  *          Lukasz Majewski <l.majewski@samsung.com>
7  *
8  * SPDX-License-Identifier:     GPL-2.0+
9  */
10
11 #include <common.h>
12 #include <dfu.h>
13 #include <g_dnl.h>
14
15 static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
16 {
17         char *s = "dfu";
18         int ret, i = 0;
19
20         if (argc < 3)
21                 return CMD_RET_USAGE;
22
23         ret = dfu_init_env_entities(argv[1], simple_strtoul(argv[2], NULL, 10));
24         if (ret)
25                 return ret;
26
27         if (argc > 3 && strcmp(argv[3], "list") == 0) {
28                 dfu_show_entities();
29                 goto done;
30         }
31
32 #ifdef CONFIG_TRATS
33         board_usb_init();
34 #endif
35
36         g_dnl_register(s);
37         while (1) {
38                 if (dfu_reset())
39                         /*
40                          * This extra number of usb_gadget_handle_interrupts()
41                          * calls is necessary to assure correct transmission
42                          * completion with dfu-util
43                          */
44                         if (++i == 10)
45                                 goto exit;
46
47                 if (ctrlc())
48                         goto exit;
49
50                 usb_gadget_handle_interrupts();
51         }
52 exit:
53         g_dnl_unregister();
54 done:
55         dfu_free_entities();
56
57         if (dfu_reset())
58                 run_command("reset", 0);
59
60         return CMD_RET_SUCCESS;
61 }
62
63 U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
64         "Device Firmware Upgrade",
65         "<interface> <dev> [list]\n"
66         "  - device firmware upgrade on a device <dev>\n"
67         "    attached to interface <interface>\n"
68         "    [list] - list available alt settings"
69 );