]>
Commit | Line | Data |
---|---|---|
a006a5de ŁM |
1 | /* |
2 | * cmd_dfu.c -- dfu command | |
3 | * | |
c2c146fb LM |
4 | * Copyright (C) 2015 |
5 | * Lukasz Majewski <[email protected]> | |
6 | * | |
a006a5de ŁM |
7 | * Copyright (C) 2012 Samsung Electronics |
8 | * authors: Andrzej Pietrasiewicz <[email protected]> | |
9 | * Lukasz Majewski <[email protected]> | |
10 | * | |
1a459660 | 11 | * SPDX-License-Identifier: GPL-2.0+ |
a006a5de ŁM |
12 | */ |
13 | ||
14 | #include <common.h> | |
0a9ac5cb | 15 | #include <watchdog.h> |
a006a5de | 16 | #include <dfu.h> |
24b852a7 | 17 | #include <console.h> |
a006a5de | 18 | #include <g_dnl.h> |
16297cfb | 19 | #include <usb.h> |
c2c146fb | 20 | #include <net.h> |
a006a5de ŁM |
21 | |
22 | static int do_dfu(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
23 | { | |
1cc03c5c ŁM |
24 | bool dfu_reset = false; |
25 | ||
16297cfb MZ |
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 | ||
6bed7ce5 | 33 | int ret, i = 0; |
c2c146fb LM |
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 | |
a006a5de | 43 | |
dd64827e | 44 | ret = dfu_init_env_entities(interface, devstring); |
a006a5de | 45 | if (ret) |
afb8e71c | 46 | goto done; |
a006a5de | 47 | |
afb8e71c | 48 | ret = CMD_RET_SUCCESS; |
16297cfb | 49 | if (argc > 4 && strcmp(argv[4], "list") == 0) { |
a006a5de ŁM |
50 | dfu_show_entities(); |
51 | goto done; | |
52 | } | |
53 | ||
16297cfb MZ |
54 | int controller_index = simple_strtoul(usb_controller, NULL, 0); |
55 | board_usb_init(controller_index, USB_INIT_DEVICE); | |
fe1b28c9 | 56 | g_dnl_clear_detach(); |
c4d0e856 | 57 | g_dnl_register("usb_dnl_dfu"); |
a006a5de | 58 | while (1) { |
fe1b28c9 | 59 | if (g_dnl_detach()) { |
1cc03c5c ŁM |
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 | ||
6bed7ce5 ŁM |
70 | /* |
71 | * This extra number of usb_gadget_handle_interrupts() | |
72 | * calls is necessary to assure correct transmission | |
73 | * completion with dfu-util | |
74 | */ | |
1cc03c5c | 75 | if (++i == 10000) |
6bed7ce5 | 76 | goto exit; |
1cc03c5c | 77 | } |
6bed7ce5 | 78 | |
a006a5de ŁM |
79 | if (ctrlc()) |
80 | goto exit; | |
81 | ||
fc18f8d1 ŁM |
82 | if (dfu_get_defer_flush()) { |
83 | /* | |
84 | * Call to usb_gadget_handle_interrupts() is necessary | |
85 | * to act on ZLP OUT transaction from HOST PC after | |
86 | * transmitting the whole file. | |
87 | * | |
88 | * If this ZLP OUT packet is NAK'ed, the HOST libusb | |
89 | * function fails after timeout (by default it is set to | |
90 | * 5 seconds). In such situation the dfu-util program | |
91 | * exits with error message. | |
92 | */ | |
93 | usb_gadget_handle_interrupts(controller_index); | |
94 | ret = dfu_flush(dfu_get_defer_flush(), NULL, 0, 0); | |
95 | dfu_set_defer_flush(NULL); | |
96 | if (ret) { | |
97 | error("Deferred dfu_flush() failed!"); | |
98 | goto exit; | |
99 | } | |
100 | } | |
101 | ||
0a9ac5cb | 102 | WATCHDOG_RESET(); |
2d48aa69 | 103 | usb_gadget_handle_interrupts(controller_index); |
a006a5de ŁM |
104 | } |
105 | exit: | |
106 | g_dnl_unregister(); | |
db378d78 | 107 | board_usb_cleanup(controller_index, USB_INIT_DEVICE); |
a006a5de ŁM |
108 | done: |
109 | dfu_free_entities(); | |
a006a5de | 110 | |
1cc03c5c | 111 | if (dfu_reset) |
6bed7ce5 ŁM |
112 | run_command("reset", 0); |
113 | ||
fe1b28c9 | 114 | g_dnl_clear_detach(); |
1cc03c5c | 115 | |
afb8e71c | 116 | return ret; |
a006a5de ŁM |
117 | } |
118 | ||
119 | U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu, | |
120 | "Device Firmware Upgrade", | |
16297cfb MZ |
121 | "<USB_controller> <interface> <dev> [list]\n" |
122 | " - device firmware upgrade via <USB_controller>\n" | |
123 | " on device <dev>, attached to interface\n" | |
124 | " <interface>\n" | |
125 | " [list] - list available alt settings\n" | |
c2c146fb LM |
126 | #ifdef CONFIG_DFU_TFTP |
127 | "dfu tftp <interface> <dev> [<addr>]\n" | |
128 | " - device firmware upgrade via TFTP\n" | |
129 | " on device <dev>, attached to interface\n" | |
130 | " <interface>\n" | |
131 | " [<addr>] - address where FIT image has been stored\n" | |
132 | #endif | |
a006a5de | 133 | ); |