Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 1 | /* |
| 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 Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 12 | #include <dfu.h> |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 13 | #include <g_dnl.h> |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 14 | #include <usb.h> |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 15 | |
| 16 | static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 17 | { |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 18 | bool dfu_reset = false; |
| 19 | |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 20 | 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 Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 27 | int ret, i = 0; |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 28 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 29 | ret = dfu_init_env_entities(interface, devstring); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 30 | if (ret) |
Stephen Warren | f9d47dd | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 31 | goto done; |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 32 | |
Stephen Warren | f9d47dd | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 33 | ret = CMD_RET_SUCCESS; |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 34 | if (argc > 4 && strcmp(argv[4], "list") == 0) { |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 35 | dfu_show_entities(); |
| 36 | goto done; |
| 37 | } |
| 38 | |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 39 | int controller_index = simple_strtoul(usb_controller, NULL, 0); |
| 40 | board_usb_init(controller_index, USB_INIT_DEVICE); |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 41 | g_dnl_clear_detach(); |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 42 | g_dnl_register("usb_dnl_dfu"); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 43 | while (1) { |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 44 | if (g_dnl_detach()) { |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 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 Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 55 | /* |
| 56 | * This extra number of usb_gadget_handle_interrupts() |
| 57 | * calls is necessary to assure correct transmission |
| 58 | * completion with dfu-util |
| 59 | */ |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 60 | if (++i == 10000) |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 61 | goto exit; |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 62 | } |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 63 | |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 64 | if (ctrlc()) |
| 65 | goto exit; |
| 66 | |
| 67 | usb_gadget_handle_interrupts(); |
| 68 | } |
| 69 | exit: |
| 70 | g_dnl_unregister(); |
| 71 | done: |
| 72 | dfu_free_entities(); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 73 | |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 74 | if (dfu_reset) |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 75 | run_command("reset", 0); |
| 76 | |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 77 | g_dnl_clear_detach(); |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 78 | |
Stephen Warren | f9d47dd | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 79 | return ret; |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu, |
| 83 | "Device Firmware Upgrade", |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 84 | "<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 Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 89 | ); |