blob: 9e020b40be8a853e5e9bfd6a1dfbcd36100ae5b0 [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);
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020041 dfu_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) {
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020044 if (dfu_detach()) {
45 /*
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
67 usb_gadget_handle_interrupts();
68 }
69exit:
70 g_dnl_unregister();
71done:
72 dfu_free_entities();
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020073
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020074 if (dfu_reset)
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020075 run_command("reset", 0);
76
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020077 dfu_clear_detach();
78
Stephen Warrenafb8e712014-06-10 10:06:41 -060079 return ret;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020080}
81
82U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
83 "Device Firmware Upgrade",
Mateusz Zalega16297cf2013-10-04 19:22:26 +020084 "<USB_controller> <interface> <dev> [list]\n"
85 " - device firmware upgrade via <USB_controller>\n"
86 " on device <dev>, attached to interface\n"
87 " <interface>\n"
88 " [list] - list available alt settings\n"
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020089);