Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * cmd_dfu.c -- dfu command |
| 3 | * |
Lukasz Majewski | 1747a54 | 2015-08-24 00:21:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2015 |
| 5 | * Lukasz Majewski <l.majewski@majess.pl> |
| 6 | * |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 7 | * Copyright (C) 2012 Samsung Electronics |
| 8 | * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com> |
| 9 | * Lukasz Majewski <l.majewski@samsung.com> |
| 10 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Heiko Schocher | c884884 | 2015-04-14 09:53:13 +0200 | [diff] [blame] | 15 | #include <watchdog.h> |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 16 | #include <dfu.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 17 | #include <console.h> |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 18 | #include <g_dnl.h> |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 19 | #include <usb.h> |
Lukasz Majewski | 1747a54 | 2015-08-24 00:21:48 +0200 | [diff] [blame] | 20 | #include <net.h> |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 21 | |
| 22 | static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 23 | { |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 24 | bool dfu_reset = false; |
| 25 | |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 26 | 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 Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 33 | int ret, i = 0; |
Lukasz Majewski | 1747a54 | 2015-08-24 00:21:48 +0200 | [diff] [blame] | 34 | #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 Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 43 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 44 | ret = dfu_init_env_entities(interface, devstring); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 45 | if (ret) |
Stephen Warren | f9d47dd | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 46 | goto done; |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 47 | |
Stephen Warren | f9d47dd | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 48 | ret = CMD_RET_SUCCESS; |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 49 | if (argc > 4 && strcmp(argv[4], "list") == 0) { |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 50 | dfu_show_entities(); |
| 51 | goto done; |
| 52 | } |
| 53 | |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 54 | int controller_index = simple_strtoul(usb_controller, NULL, 0); |
| 55 | board_usb_init(controller_index, USB_INIT_DEVICE); |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 56 | g_dnl_clear_detach(); |
Mateusz Zalega | 69cb0bb | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 57 | g_dnl_register("usb_dnl_dfu"); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 58 | while (1) { |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 59 | if (g_dnl_detach()) { |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 60 | /* |
| 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 Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 70 | /* |
| 71 | * This extra number of usb_gadget_handle_interrupts() |
| 72 | * calls is necessary to assure correct transmission |
| 73 | * completion with dfu-util |
| 74 | */ |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 75 | if (++i == 10000) |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 76 | goto exit; |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 77 | } |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 78 | |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 79 | if (ctrlc()) |
| 80 | goto exit; |
| 81 | |
Heiko Schocher | c884884 | 2015-04-14 09:53:13 +0200 | [diff] [blame] | 82 | WATCHDOG_RESET(); |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 83 | usb_gadget_handle_interrupts(controller_index); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 84 | } |
| 85 | exit: |
| 86 | g_dnl_unregister(); |
Kishon Vijay Abraham I | ae07a84 | 2015-02-23 18:40:18 +0530 | [diff] [blame] | 87 | board_usb_cleanup(controller_index, USB_INIT_DEVICE); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 88 | done: |
| 89 | dfu_free_entities(); |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 90 | |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 91 | if (dfu_reset) |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 92 | run_command("reset", 0); |
| 93 | |
Rob Herring | d38f24e | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 94 | g_dnl_clear_detach(); |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 95 | |
Stephen Warren | f9d47dd | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 96 | return ret; |
Lukasz Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu, |
| 100 | "Device Firmware Upgrade", |
Mateusz Zalega | d862f89 | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 101 | "<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 Majewski | 1747a54 | 2015-08-24 00:21:48 +0200 | [diff] [blame] | 106 | #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 Majewski | 9273f4f | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 113 | ); |