]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_dfu.c
dfu: command: Extend "dfu" command to handle receiving data via TFTP
[people/ms/u-boot.git] / common / cmd_dfu.c
CommitLineData
a006a5de
ŁM
1/*
2 * cmd_dfu.c -- dfu command
3 *
c2c146fb
LM
4 * Copyright (C) 2015
5 * Lukasz Majewski <l.majewski@majess.pl>
6 *
a006a5de
ŁM
7 * Copyright (C) 2012 Samsung Electronics
8 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
9 * Lukasz Majewski <l.majewski@samsung.com>
10 *
1a459660 11 * SPDX-License-Identifier: GPL-2.0+
a006a5de
ŁM
12 */
13
14#include <common.h>
0a9ac5cb 15#include <watchdog.h>
a006a5de 16#include <dfu.h>
a006a5de 17#include <g_dnl.h>
16297cfb 18#include <usb.h>
c2c146fb 19#include <net.h>
a006a5de
ŁM
20
21static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
22{
1cc03c5c
ŁM
23 bool dfu_reset = false;
24
16297cfb
MZ
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
6bed7ce5 32 int ret, i = 0;
c2c146fb
LM
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
a006a5de 42
dd64827e 43 ret = dfu_init_env_entities(interface, devstring);
a006a5de 44 if (ret)
afb8e71c 45 goto done;
a006a5de 46
afb8e71c 47 ret = CMD_RET_SUCCESS;
16297cfb 48 if (argc > 4 && strcmp(argv[4], "list") == 0) {
a006a5de
ŁM
49 dfu_show_entities();
50 goto done;
51 }
52
16297cfb
MZ
53 int controller_index = simple_strtoul(usb_controller, NULL, 0);
54 board_usb_init(controller_index, USB_INIT_DEVICE);
fe1b28c9 55 g_dnl_clear_detach();
c4d0e856 56 g_dnl_register("usb_dnl_dfu");
a006a5de 57 while (1) {
fe1b28c9 58 if (g_dnl_detach()) {
1cc03c5c
ŁM
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
6bed7ce5
ŁM
69 /*
70 * This extra number of usb_gadget_handle_interrupts()
71 * calls is necessary to assure correct transmission
72 * completion with dfu-util
73 */
1cc03c5c 74 if (++i == 10000)
6bed7ce5 75 goto exit;
1cc03c5c 76 }
6bed7ce5 77
a006a5de
ŁM
78 if (ctrlc())
79 goto exit;
80
0a9ac5cb 81 WATCHDOG_RESET();
2d48aa69 82 usb_gadget_handle_interrupts(controller_index);
a006a5de
ŁM
83 }
84exit:
85 g_dnl_unregister();
db378d78 86 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
a006a5de
ŁM
87done:
88 dfu_free_entities();
a006a5de 89
1cc03c5c 90 if (dfu_reset)
6bed7ce5
ŁM
91 run_command("reset", 0);
92
fe1b28c9 93 g_dnl_clear_detach();
1cc03c5c 94
afb8e71c 95 return ret;
a006a5de
ŁM
96}
97
98U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
99 "Device Firmware Upgrade",
16297cfb
MZ
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"
c2c146fb
LM
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
a006a5de 112);