dfu.c 3.11 KB
Newer Older
1 2 3
/*
 * cmd_dfu.c -- dfu command
 *
4 5 6
 * Copyright (C) 2015
 * Lukasz Majewski <l.majewski@majess.pl>
 *
7 8 9 10
 * Copyright (C) 2012 Samsung Electronics
 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
 *	    Lukasz Majewski <l.majewski@samsung.com>
 *
11
 * SPDX-License-Identifier:	GPL-2.0+
12 13 14
 */

#include <common.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 22 23

static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
24 25
	bool dfu_reset = false;

26 27 28 29 30 31 32
	if (argc < 4)
		return CMD_RET_USAGE;

	char *usb_controller = argv[1];
	char *interface = argv[2];
	char *devstring = argv[3];

33
	int ret, i = 0;
34 35 36 37 38 39 40 41 42
#ifdef CONFIG_DFU_TFTP
	unsigned long addr = 0;
	if (!strcmp(argv[1], "tftp")) {
		if (argc == 5)
			addr = simple_strtoul(argv[4], NULL, 0);

		return update_tftp(addr, interface, devstring);
	}
#endif
43

44
	ret = dfu_init_env_entities(interface, devstring);
45
	if (ret)
46
		goto done;
47

48
	ret = CMD_RET_SUCCESS;
49
	if (argc > 4 && strcmp(argv[4], "list") == 0) {
50 51 52 53
		dfu_show_entities();
		goto done;
	}

54 55
	int controller_index = simple_strtoul(usb_controller, NULL, 0);
	board_usb_init(controller_index, USB_INIT_DEVICE);
56
	g_dnl_clear_detach();
57
	g_dnl_register("usb_dnl_dfu");
58
	while (1) {
59
		if (g_dnl_detach()) {
60 61 62 63 64 65 66 67 68 69
			/*
			 * Check if USB bus reset is performed after detach,
			 * which indicates that -R switch has been passed to
			 * dfu-util. In this case reboot the device
			 */
			if (dfu_usb_get_reset()) {
				dfu_reset = true;
				goto exit;
			}

70 71 72 73 74
			/*
			 * This extra number of usb_gadget_handle_interrupts()
			 * calls is necessary to assure correct transmission
			 * completion with dfu-util
			 */
75
			if (++i == 10000)
76
				goto exit;
77
		}
78

79 80 81
		if (ctrlc())
			goto exit;

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		if (dfu_get_defer_flush()) {
			/*
			 * Call to usb_gadget_handle_interrupts() is necessary
			 * to act on ZLP OUT transaction from HOST PC after
			 * transmitting the whole file.
			 *
			 * If this ZLP OUT packet is NAK'ed, the HOST libusb
			 * function fails after timeout (by default it is set to
			 * 5 seconds). In such situation the dfu-util program
			 * exits with error message.
			 */
			usb_gadget_handle_interrupts(controller_index);
			ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
			dfu_set_defer_flush(NULL);
			if (ret) {
				error("Deferred dfu_flush() failed!");
				goto exit;
			}
		}

102
		WATCHDOG_RESET();
103
		usb_gadget_handle_interrupts(controller_index);
104 105 106
	}
exit:
	g_dnl_unregister();
107
	board_usb_cleanup(controller_index, USB_INIT_DEVICE);
108 109 110
done:
	dfu_free_entities();

111
	if (dfu_reset)
112 113
		run_command("reset", 0);

114
	g_dnl_clear_detach();
115

116
	return ret;
117 118 119 120
}

U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
	"Device Firmware Upgrade",
121 122 123 124 125
	"<USB_controller> <interface> <dev> [list]\n"
	"  - device firmware upgrade via <USB_controller>\n"
	"    on device <dev>, attached to interface\n"
	"    <interface>\n"
	"    [list] - list available alt settings\n"
126 127 128 129 130 131 132
#ifdef CONFIG_DFU_TFTP
	"dfu tftp <interface> <dev> [<addr>]\n"
	"  - device firmware upgrade via TFTP\n"
	"    on device <dev>, attached to interface\n"
	"    <interface>\n"
	"    [<addr>] - address where FIT image has been stored\n"
#endif
133
);