blob: d8aae262232e82a066ae83ea0e82057e41f26c26 [file] [log] [blame]
Lukasz Majewski9273f4f2012-08-06 14:41:09 +02001/*
2 * cmd_dfu.c -- dfu command
3 *
Lukasz Majewski1747a542015-08-24 00:21:48 +02004 * Copyright (C) 2015
5 * Lukasz Majewski <l.majewski@majess.pl>
6 *
Lukasz Majewski9273f4f2012-08-06 14:41:09 +02007 * Copyright (C) 2012 Samsung Electronics
8 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
9 * Lukasz Majewski <l.majewski@samsung.com>
10 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020011 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020012 */
13
14#include <common.h>
Heiko Schocherc8848842015-04-14 09:53:13 +020015#include <watchdog.h>
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020016#include <dfu.h>
Simon Glassa73bda42015-11-08 23:47:45 -070017#include <console.h>
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020018#include <g_dnl.h>
Mateusz Zalegad862f892013-10-04 19:22:26 +020019#include <usb.h>
Lukasz Majewski1747a542015-08-24 00:21:48 +020020#include <net.h>
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020021
22static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
23{
Lukasz Majewskid9eae702014-08-25 11:07:28 +020024 bool dfu_reset = false;
25
Mateusz Zalegad862f892013-10-04 19:22:26 +020026 if (argc < 4)
27 return CMD_RET_USAGE;
28
29 char *usb_controller = argv[1];
30 char *interface = argv[2];
31 char *devstring = argv[3];
32
Lukasz Majewski2968aa22013-07-18 13:19:14 +020033 int ret, i = 0;
Lukasz Majewski1747a542015-08-24 00:21:48 +020034#ifdef CONFIG_DFU_TFTP
35 unsigned long addr = 0;
36 if (!strcmp(argv[1], "tftp")) {
37 if (argc == 5)
38 addr = simple_strtoul(argv[4], NULL, 0);
39
40 return update_tftp(addr, interface, devstring);
41 }
42#endif
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020043
Stephen Warren4afe50f2014-06-11 16:03:33 -060044 ret = dfu_init_env_entities(interface, devstring);
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020045 if (ret)
Stephen Warrenf9d47dd2014-06-10 10:06:41 -060046 goto done;
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020047
Stephen Warrenf9d47dd2014-06-10 10:06:41 -060048 ret = CMD_RET_SUCCESS;
Mateusz Zalegad862f892013-10-04 19:22:26 +020049 if (argc > 4 && strcmp(argv[4], "list") == 0) {
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020050 dfu_show_entities();
51 goto done;
52 }
53
Mateusz Zalegad862f892013-10-04 19:22:26 +020054 int controller_index = simple_strtoul(usb_controller, NULL, 0);
55 board_usb_init(controller_index, USB_INIT_DEVICE);
Rob Herringd38f24e2014-12-10 14:43:03 -060056 g_dnl_clear_detach();
Mateusz Zalega69cb0bb2014-04-28 21:13:28 +020057 g_dnl_register("usb_dnl_dfu");
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020058 while (1) {
Rob Herringd38f24e2014-12-10 14:43:03 -060059 if (g_dnl_detach()) {
Lukasz Majewskid9eae702014-08-25 11:07:28 +020060 /*
61 * Check if USB bus reset is performed after detach,
62 * which indicates that -R switch has been passed to
63 * dfu-util. In this case reboot the device
64 */
65 if (dfu_usb_get_reset()) {
66 dfu_reset = true;
67 goto exit;
68 }
69
Lukasz Majewski2968aa22013-07-18 13:19:14 +020070 /*
71 * This extra number of usb_gadget_handle_interrupts()
72 * calls is necessary to assure correct transmission
73 * completion with dfu-util
74 */
Lukasz Majewskid9eae702014-08-25 11:07:28 +020075 if (++i == 10000)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020076 goto exit;
Lukasz Majewskid9eae702014-08-25 11:07:28 +020077 }
Lukasz Majewski2968aa22013-07-18 13:19:14 +020078
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020079 if (ctrlc())
80 goto exit;
81
Lukasz Majewski365a70d2016-01-28 16:14:49 +010082 if (dfu_get_defer_flush()) {
83 /*
84 * Call to usb_gadget_handle_interrupts() is necessary
85 * to act on ZLP OUT transaction from HOST PC after
86 * transmitting the whole file.
87 *
88 * If this ZLP OUT packet is NAK'ed, the HOST libusb
89 * function fails after timeout (by default it is set to
90 * 5 seconds). In such situation the dfu-util program
91 * exits with error message.
92 */
93 usb_gadget_handle_interrupts(controller_index);
94 ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0);
95 dfu_set_defer_flush(NULL);
96 if (ret) {
97 error("Deferred dfu_flush() failed!");
98 goto exit;
99 }
100 }
101
Heiko Schocherc8848842015-04-14 09:53:13 +0200102 WATCHDOG_RESET();
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +0530103 usb_gadget_handle_interrupts(controller_index);
Lukasz Majewski9273f4f2012-08-06 14:41:09 +0200104 }
105exit:
106 g_dnl_unregister();
Kishon Vijay Abraham Iae07a842015-02-23 18:40:18 +0530107 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Lukasz Majewski9273f4f2012-08-06 14:41:09 +0200108done:
109 dfu_free_entities();
Lukasz Majewski9273f4f2012-08-06 14:41:09 +0200110
Lukasz Majewskid9eae702014-08-25 11:07:28 +0200111 if (dfu_reset)
Lukasz Majewski2968aa22013-07-18 13:19:14 +0200112 run_command("reset", 0);
113
Rob Herringd38f24e2014-12-10 14:43:03 -0600114 g_dnl_clear_detach();
Lukasz Majewskid9eae702014-08-25 11:07:28 +0200115
Stephen Warrenf9d47dd2014-06-10 10:06:41 -0600116 return ret;
Lukasz Majewski9273f4f2012-08-06 14:41:09 +0200117}
118
119U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
120 "Device Firmware Upgrade",
Mateusz Zalegad862f892013-10-04 19:22:26 +0200121 "<USB_controller> <interface> <dev> [list]\n"
122 " - device firmware upgrade via <USB_controller>\n"
123 " on device <dev>, attached to interface\n"
124 " <interface>\n"
125 " [list] - list available alt settings\n"
Lukasz Majewski1747a542015-08-24 00:21:48 +0200126#ifdef CONFIG_DFU_TFTP
127 "dfu tftp <interface> <dev> [<addr>]\n"
128 " - device firmware upgrade via TFTP\n"
129 " on device <dev>, attached to interface\n"
130 " <interface>\n"
131 " [<addr>] - address where FIT image has been stored\n"
132#endif
Lukasz Majewski9273f4f2012-08-06 14:41:09 +0200133);