blob: ec909981762185bb498c8b2330dda79d76666152 [file] [log] [blame]
Lukasz Majewskia006a5d2012-08-06 14:41:09 +02001/*
2 * cmd_dfu.c -- dfu command
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
6 * Lukasz Majewski <l.majewski@samsung.com>
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskia006a5d2012-08-06 14:41:09 +02009 */
10
11#include <common.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020012#include <dfu.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020013#include <g_dnl.h>
Mateusz Zalega16297cf2013-10-04 19:22:26 +020014#include <usb.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020015
16static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
17{
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020018 bool dfu_reset = false;
19
Mateusz Zalega16297cf2013-10-04 19:22:26 +020020 if (argc < 4)
21 return CMD_RET_USAGE;
22
23 char *usb_controller = argv[1];
24 char *interface = argv[2];
25 char *devstring = argv[3];
26
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020027 int ret, i = 0;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020028
Stephen Warrendd648272014-06-11 16:03:33 -060029 ret = dfu_init_env_entities(interface, devstring);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020030 if (ret)
Stephen Warrenafb8e712014-06-10 10:06:41 -060031 goto done;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020032
Stephen Warrenafb8e712014-06-10 10:06:41 -060033 ret = CMD_RET_SUCCESS;
Mateusz Zalega16297cf2013-10-04 19:22:26 +020034 if (argc > 4 && strcmp(argv[4], "list") == 0) {
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020035 dfu_show_entities();
36 goto done;
37 }
38
Mateusz Zalega16297cf2013-10-04 19:22:26 +020039 int controller_index = simple_strtoul(usb_controller, NULL, 0);
40 board_usb_init(controller_index, USB_INIT_DEVICE);
Rob Herringfe1b28c2014-12-10 14:43:03 -060041 g_dnl_clear_detach();
Mateusz Zalegac4d0e852014-04-28 21:13:28 +020042 g_dnl_register("usb_dnl_dfu");
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020043 while (1) {
Rob Herringfe1b28c2014-12-10 14:43:03 -060044 if (g_dnl_detach()) {
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020045 /*
46 * Check if USB bus reset is performed after detach,
47 * which indicates that -R switch has been passed to
48 * dfu-util. In this case reboot the device
49 */
50 if (dfu_usb_get_reset()) {
51 dfu_reset = true;
52 goto exit;
53 }
54
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020055 /*
56 * This extra number of usb_gadget_handle_interrupts()
57 * calls is necessary to assure correct transmission
58 * completion with dfu-util
59 */
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020060 if (++i == 10000)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020061 goto exit;
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020062 }
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020063
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020064 if (ctrlc())
65 goto exit;
66
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +053067 usb_gadget_handle_interrupts(controller_index);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020068 }
69exit:
70 g_dnl_unregister();
Kishon Vijay Abraham Idb378d72015-02-23 18:40:18 +053071 board_usb_cleanup(controller_index, USB_INIT_DEVICE);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020072done:
73 dfu_free_entities();
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020074
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020075 if (dfu_reset)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020076 run_command("reset", 0);
77
Rob Herringfe1b28c2014-12-10 14:43:03 -060078 g_dnl_clear_detach();
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020079
Stephen Warrenafb8e712014-06-10 10:06:41 -060080 return ret;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020081}
82
83U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
84 "Device Firmware Upgrade",
Mateusz Zalega16297cf2013-10-04 19:22:26 +020085 "<USB_controller> <interface> <dev> [list]\n"
86 " - device firmware upgrade via <USB_controller>\n"
87 " on device <dev>, attached to interface\n"
88 " <interface>\n"
89 " [list] - list available alt settings\n"
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020090);