Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * cmd_dfu.c -- dfu command |
| 3 | * |
Lukasz Majewski | c2c146f | 2015-08-24 00:21:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2015 |
| 5 | * Lukasz Majewski <l.majewski@majess.pl> |
| 6 | * |
Lukasz Majewski | a006a5d | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Heiko Schocher | 0a9ac5c | 2015-04-14 09:53:13 +0200 | [diff] [blame] | 15 | #include <watchdog.h> |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 16 | #include <dfu.h> |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 17 | #include <g_dnl.h> |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 18 | #include <usb.h> |
Lukasz Majewski | c2c146f | 2015-08-24 00:21:48 +0200 | [diff] [blame] | 19 | #include <net.h> |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 20 | |
| 21 | static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 22 | { |
Lukasz Majewski | 1cc03c5 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 23 | bool dfu_reset = false; |
| 24 | |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 25 | if (argc < 4) |
| 26 | return CMD_RET_USAGE; |
| 27 | |
| 28 | char *usb_controller = argv[1]; |
| 29 | char *interface = argv[2]; |
| 30 | char *devstring = argv[3]; |
| 31 | |
Lukasz Majewski | 6bed7ce | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 32 | int ret, i = 0; |
Lukasz Majewski | c2c146f | 2015-08-24 00:21:48 +0200 | [diff] [blame] | 33 | #ifdef CONFIG_DFU_TFTP |
| 34 | unsigned long addr = 0; |
| 35 | if (!strcmp(argv[1], "tftp")) { |
| 36 | if (argc == 5) |
| 37 | addr = simple_strtoul(argv[4], NULL, 0); |
| 38 | |
| 39 | return update_tftp(addr, interface, devstring); |
| 40 | } |
| 41 | #endif |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 42 | |
Stephen Warren | dd64827 | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 43 | ret = dfu_init_env_entities(interface, devstring); |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 44 | if (ret) |
Stephen Warren | afb8e71 | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 45 | goto done; |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 46 | |
Stephen Warren | afb8e71 | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 47 | ret = CMD_RET_SUCCESS; |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 48 | if (argc > 4 && strcmp(argv[4], "list") == 0) { |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 49 | dfu_show_entities(); |
| 50 | goto done; |
| 51 | } |
| 52 | |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 53 | int controller_index = simple_strtoul(usb_controller, NULL, 0); |
| 54 | board_usb_init(controller_index, USB_INIT_DEVICE); |
Rob Herring | fe1b28c | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 55 | g_dnl_clear_detach(); |
Mateusz Zalega | c4d0e85 | 2014-04-28 21:13:28 +0200 | [diff] [blame] | 56 | g_dnl_register("usb_dnl_dfu"); |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 57 | while (1) { |
Rob Herring | fe1b28c | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 58 | if (g_dnl_detach()) { |
Lukasz Majewski | 1cc03c5 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 59 | /* |
| 60 | * Check if USB bus reset is performed after detach, |
| 61 | * which indicates that -R switch has been passed to |
| 62 | * dfu-util. In this case reboot the device |
| 63 | */ |
| 64 | if (dfu_usb_get_reset()) { |
| 65 | dfu_reset = true; |
| 66 | goto exit; |
| 67 | } |
| 68 | |
Lukasz Majewski | 6bed7ce | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 69 | /* |
| 70 | * This extra number of usb_gadget_handle_interrupts() |
| 71 | * calls is necessary to assure correct transmission |
| 72 | * completion with dfu-util |
| 73 | */ |
Lukasz Majewski | 1cc03c5 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 74 | if (++i == 10000) |
Lukasz Majewski | 6bed7ce | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 75 | goto exit; |
Lukasz Majewski | 1cc03c5 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 76 | } |
Lukasz Majewski | 6bed7ce | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 77 | |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 78 | if (ctrlc()) |
| 79 | goto exit; |
| 80 | |
Heiko Schocher | 0a9ac5c | 2015-04-14 09:53:13 +0200 | [diff] [blame] | 81 | WATCHDOG_RESET(); |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 82 | usb_gadget_handle_interrupts(controller_index); |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 83 | } |
| 84 | exit: |
| 85 | g_dnl_unregister(); |
Kishon Vijay Abraham I | db378d7 | 2015-02-23 18:40:18 +0530 | [diff] [blame] | 86 | board_usb_cleanup(controller_index, USB_INIT_DEVICE); |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 87 | done: |
| 88 | dfu_free_entities(); |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 89 | |
Lukasz Majewski | 1cc03c5 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 90 | if (dfu_reset) |
Lukasz Majewski | 6bed7ce | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 91 | run_command("reset", 0); |
| 92 | |
Rob Herring | fe1b28c | 2014-12-10 14:43:03 -0600 | [diff] [blame] | 93 | g_dnl_clear_detach(); |
Lukasz Majewski | 1cc03c5 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 94 | |
Stephen Warren | afb8e71 | 2014-06-10 10:06:41 -0600 | [diff] [blame] | 95 | return ret; |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu, |
| 99 | "Device Firmware Upgrade", |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 100 | "<USB_controller> <interface> <dev> [list]\n" |
| 101 | " - device firmware upgrade via <USB_controller>\n" |
| 102 | " on device <dev>, attached to interface\n" |
| 103 | " <interface>\n" |
| 104 | " [list] - list available alt settings\n" |
Lukasz Majewski | c2c146f | 2015-08-24 00:21:48 +0200 | [diff] [blame] | 105 | #ifdef CONFIG_DFU_TFTP |
| 106 | "dfu tftp <interface> <dev> [<addr>]\n" |
| 107 | " - device firmware upgrade via TFTP\n" |
| 108 | " on device <dev>, attached to interface\n" |
| 109 | " <interface>\n" |
| 110 | " [<addr>] - address where FIT image has been stored\n" |
| 111 | #endif |
Lukasz Majewski | a006a5d | 2012-08-06 14:41:09 +0200 | [diff] [blame] | 112 | ); |