blob: 6d95ce922312af4dd4d303fceeddd7d751e36ff4 [file] [log] [blame]
Lukasz Majewskia006a5d2012-08-06 14:41:09 +02001/*
2 * cmd_dfu.c -- dfu command
3 *
Lukasz Majewskic2c146f2015-08-24 00:21:48 +02004 * Copyright (C) 2015
5 * Lukasz Majewski <l.majewski@majess.pl>
6 *
Lukasz Majewskia006a5d2012-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 Denk1a459662013-07-08 09:37:19 +020011 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020012 */
13
14#include <common.h>
Heiko Schocher0a9ac5c2015-04-14 09:53:13 +020015#include <watchdog.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020016#include <dfu.h>
Simon Glass24b852a2015-11-08 23:47:45 -070017#include <console.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020018#include <g_dnl.h>
Mateusz Zalega16297cf2013-10-04 19:22:26 +020019#include <usb.h>
Lukasz Majewskic2c146f2015-08-24 00:21:48 +020020#include <net.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020021
22static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
23{
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020024 bool dfu_reset = false;
25
Mateusz Zalega16297cf2013-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 Majewski6bed7ce2013-07-18 13:19:14 +020033 int ret, i = 0;
Lukasz Majewskic2c146f2015-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 Majewskia006a5d2012-08-06 14:41:09 +020043
Stephen Warrendd648272014-06-11 16:03:33 -060044 ret = dfu_init_env_entities(interface, devstring);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020045 if (ret)
Stephen Warrenafb8e712014-06-10 10:06:41 -060046 goto done;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020047
Stephen Warrenafb8e712014-06-10 10:06:41 -060048 ret = CMD_RET_SUCCESS;
Mateusz Zalega16297cf2013-10-04 19:22:26 +020049 if (argc > 4 && strcmp(argv[4], "list") == 0) {
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020050 dfu_show_entities();
51 goto done;
52 }
53
Mateusz Zalega16297cf2013-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 Herringfe1b28c2014-12-10 14:43:03 -060056 g_dnl_clear_detach();
Mateusz Zalegac4d0e852014-04-28 21:13:28 +020057 g_dnl_register("usb_dnl_dfu");
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020058 while (1) {
Rob Herringfe1b28c2014-12-10 14:43:03 -060059 if (g_dnl_detach()) {
Lukasz Majewski1cc03c52014-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 Majewski6bed7ce2013-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 Majewski1cc03c52014-08-25 11:07:28 +020075 if (++i == 10000)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020076 goto exit;
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020077 }
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020078
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020079 if (ctrlc())
80 goto exit;
81
Heiko Schocher0a9ac5c2015-04-14 09:53:13 +020082 WATCHDOG_RESET();
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +053083 usb_gadget_handle_interrupts(controller_index);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020084 }
85exit:
86 g_dnl_unregister();
Kishon Vijay Abraham Idb378d72015-02-23 18:40:18 +053087 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020088done:
89 dfu_free_entities();
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020090
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020091 if (dfu_reset)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020092 run_command("reset", 0);
93
Rob Herringfe1b28c2014-12-10 14:43:03 -060094 g_dnl_clear_detach();
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020095
Stephen Warrenafb8e712014-06-10 10:06:41 -060096 return ret;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020097}
98
99U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
100 "Device Firmware Upgrade",
Mateusz Zalega16297cf2013-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 Majewskic2c146f2015-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 Majewskia006a5d2012-08-06 14:41:09 +0200113);