dfu: Extract common DFU code to handle "dfu_alt_info" environment variable
[kernel/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 <command.h>
13 #include <malloc.h>
14 #include <dfu.h>
15 #include <asm/errno.h>
16 #include <g_dnl.h>
17
18 static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
19 {
20         char *s = "dfu";
21         int ret, i = 0;
22
23         if (argc < 3)
24                 return CMD_RET_USAGE;
25
26         ret = dfu_init_env_entities(argv[1], simple_strtoul(argv[2], NULL, 10));
27         if (ret)
28                 return ret;
29
30         if (argc > 3 && strcmp(argv[3], "list") == 0) {
31                 dfu_show_entities();
32                 goto done;
33         }
34
35 #ifdef CONFIG_TRATS
36         board_usb_init();
37 #endif
38
39         g_dnl_register(s);
40         while (1) {
41                 if (dfu_reset())
42                         /*
43                          * This extra number of usb_gadget_handle_interrupts()
44                          * calls is necessary to assure correct transmission
45                          * completion with dfu-util
46                          */
47                         if (++i == 10)
48                                 goto exit;
49
50                 if (ctrlc())
51                         goto exit;
52
53                 usb_gadget_handle_interrupts();
54         }
55 exit:
56         g_dnl_unregister();
57 done:
58         dfu_free_entities();
59
60         if (dfu_reset())
61                 run_command("reset", 0);
62
63         return CMD_RET_SUCCESS;
64 }
65
66 U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
67         "Device Firmware Upgrade",
68         "<interface> <dev> [list]\n"
69         "  - device firmware upgrade on a device <dev>\n"
70         "    attached to interface <interface>\n"
71         "    [list] - list available alt settings"
72 );