blob: 6d95ce922312af4dd4d303fceeddd7d751e36ff4 [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
Heiko Schocherc8848842015-04-14 09:53:13 +020082 WATCHDOG_RESET();
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +053083 usb_gadget_handle_interrupts(controller_index);
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020084 }
85exit:
86 g_dnl_unregister();
Kishon Vijay Abraham Iae07a842015-02-23 18:40:18 +053087 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020088done:
89 dfu_free_entities();
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020090
Lukasz Majewskid9eae702014-08-25 11:07:28 +020091 if (dfu_reset)
Lukasz Majewski2968aa22013-07-18 13:19:14 +020092 run_command("reset", 0);
93
Rob Herringd38f24e2014-12-10 14:43:03 -060094 g_dnl_clear_detach();
Lukasz Majewskid9eae702014-08-25 11:07:28 +020095
Stephen Warrenf9d47dd2014-06-10 10:06:41 -060096 return ret;
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020097}
98
99U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
100 "Device Firmware Upgrade",
Mateusz Zalegad862f892013-10-04 19:22:26 +0200101 "<USB_controller> <interface> <dev> [list]\n"
102 " - device firmware upgrade via <USB_controller>\n"
103 " on device <dev>, attached to interface\n"
104 " <interface>\n"
105 " [list] - list available alt settings\n"
Lukasz Majewski1747a542015-08-24 00:21:48 +0200106#ifdef CONFIG_DFU_TFTP
107 "dfu tftp <interface> <dev> [<addr>]\n"
108 " - device firmware upgrade via TFTP\n"
109 " on device <dev>, attached to interface\n"
110 " <interface>\n"
111 " [<addr>] - address where FIT image has been stored\n"
112#endif
Lukasz Majewski9273f4f2012-08-06 14:41:09 +0200113);