blob: 4a288f74c2c27ec69dd84066fbd6fed6cd02afa9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskia006a5d2012-08-06 14:41:09 +02002/*
3 * cmd_dfu.c -- dfu command
4 *
Lukasz Majewskic2c146f2015-08-24 00:21:48 +02005 * Copyright (C) 2015
6 * Lukasz Majewski <l.majewski@majess.pl>
7 *
Lukasz Majewskia006a5d2012-08-06 14:41:09 +02008 * Copyright (C) 2012 Samsung Electronics
9 * authors: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
10 * Lukasz Majewski <l.majewski@samsung.com>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020011 */
12
13#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060014#include <command.h>
Heiko Schocher0a9ac5c2015-04-14 09:53:13 +020015#include <watchdog.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020016#include <dfu.h>
Simon Glass24b852a2015-11-08 23:47:45 -070017#include <console.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020018#include <g_dnl.h>
Mateusz Zalega16297cf2013-10-04 19:22:26 +020019#include <usb.h>
Lukasz Majewskic2c146f2015-08-24 00:21:48 +020020#include <net.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020021
Simon Glass09140112020-05-10 11:40:03 -060022static int do_dfu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020023{
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020024
Patrick Delaunayfebabe32019-10-14 09:28:02 +020025 if (argc < 2)
Mateusz Zalega16297cf2013-10-04 19:22:26 +020026 return CMD_RET_USAGE;
27
Marek Vasutbb4059a2018-02-16 16:41:18 +010028#ifdef CONFIG_DFU_OVER_USB
Mateusz Zalega16297cf2013-10-04 19:22:26 +020029 char *usb_controller = argv[1];
Marek Vasut0f44d332018-02-16 16:41:17 +010030#endif
Andy Shevchenkocbc10812019-03-04 16:04:44 +020031#if defined(CONFIG_DFU_OVER_USB) || defined(CONFIG_DFU_OVER_TFTP)
Patrick Delaunayfebabe32019-10-14 09:28:02 +020032 char *interface = NULL;
33 char *devstring = NULL;
Andy Shevchenko98a8f442019-11-27 18:12:15 +020034#if defined(CONFIG_DFU_TIMEOUT) || defined(CONFIG_DFU_OVER_TFTP)
Andy Shevchenko2b1f8c22019-11-27 18:12:14 +020035 unsigned long value = 0;
36#endif
Patrick Delaunayfebabe32019-10-14 09:28:02 +020037 if (argc >= 4) {
38 interface = argv[2];
39 devstring = argv[3];
40 }
Andy Shevchenko2b1f8c22019-11-27 18:12:14 +020041
Andy Shevchenko98a8f442019-11-27 18:12:15 +020042#if defined(CONFIG_DFU_TIMEOUT) || defined(CONFIG_DFU_OVER_TFTP)
Andy Shevchenko2b1f8c22019-11-27 18:12:14 +020043 if (argc == 5 || argc == 3)
44 value = simple_strtoul(argv[argc - 1], NULL, 0);
45#endif
Andy Shevchenkocbc10812019-03-04 16:04:44 +020046#endif
Mateusz Zalega16297cf2013-10-04 19:22:26 +020047
Marek Vasut0f44d332018-02-16 16:41:17 +010048 int ret = 0;
Marek Vasutbb4059a2018-02-16 16:41:18 +010049#ifdef CONFIG_DFU_OVER_TFTP
Andy Shevchenko2b1f8c22019-11-27 18:12:14 +020050 if (!strcmp(argv[1], "tftp"))
51 return update_tftp(value, interface, devstring);
Lukasz Majewskic2c146f2015-08-24 00:21:48 +020052#endif
Marek Vasutbb4059a2018-02-16 16:41:18 +010053#ifdef CONFIG_DFU_OVER_USB
Stephen Warrendd648272014-06-11 16:03:33 -060054 ret = dfu_init_env_entities(interface, devstring);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020055 if (ret)
Stephen Warrenafb8e712014-06-10 10:06:41 -060056 goto done;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020057
Andy Shevchenko98a8f442019-11-27 18:12:15 +020058#ifdef CONFIG_DFU_TIMEOUT
59 dfu_set_timeout(value * 1000);
60#endif
61
Stephen Warrenafb8e712014-06-10 10:06:41 -060062 ret = CMD_RET_SUCCESS;
Patrick Delaunayfebabe32019-10-14 09:28:02 +020063 if (strcmp(argv[argc - 1], "list") == 0) {
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020064 dfu_show_entities();
65 goto done;
66 }
67
Mateusz Zalega16297cf2013-10-04 19:22:26 +020068 int controller_index = simple_strtoul(usb_controller, NULL, 0);
Marek Szyprowski9129f2f2020-12-22 11:32:23 +010069 bool retry = false;
70 do {
Michal Simek74fe3f22021-03-31 09:05:52 +020071 ret = run_usb_dnl_gadget(controller_index, "usb_dnl_dfu");
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020072
Marek Szyprowski9129f2f2020-12-22 11:32:23 +010073 if (dfu_reinit_needed) {
74 dfu_free_entities();
75 ret = dfu_init_env_entities(interface, devstring);
76 if (ret)
77 goto done;
78 retry = true;
79 }
80 } while (retry);
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020081
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020082done:
83 dfu_free_entities();
Marek Vasut0f44d332018-02-16 16:41:17 +010084#endif
Stephen Warrenafb8e712014-06-10 10:06:41 -060085 return ret;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020086}
87
88U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
89 "Device Firmware Upgrade",
Andy Shevchenkocbc10812019-03-04 16:04:44 +020090 ""
Marek Vasutbb4059a2018-02-16 16:41:18 +010091#ifdef CONFIG_DFU_OVER_USB
Andy Shevchenko98a8f442019-11-27 18:12:15 +020092#ifdef CONFIG_DFU_TIMEOUT
93 "<USB_controller> [<interface> <dev>] [<timeout>] [list]\n"
94#else
Patrick Delaunayfebabe32019-10-14 09:28:02 +020095 "<USB_controller> [<interface> <dev>] [list]\n"
Andy Shevchenko98a8f442019-11-27 18:12:15 +020096#endif
Mateusz Zalega16297cf2013-10-04 19:22:26 +020097 " - device firmware upgrade via <USB_controller>\n"
98 " on device <dev>, attached to interface\n"
99 " <interface>\n"
Andy Shevchenko98a8f442019-11-27 18:12:15 +0200100#ifdef CONFIG_DFU_TIMEOUT
101 " [<timeout>] - specify inactivity timeout in seconds\n"
102#endif
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200103 " [list] - list available alt settings\n"
Marek Vasut0f44d332018-02-16 16:41:17 +0100104#endif
Marek Vasutbb4059a2018-02-16 16:41:18 +0100105#ifdef CONFIG_DFU_OVER_TFTP
106#ifdef CONFIG_DFU_OVER_USB
Marek Vasut0f44d332018-02-16 16:41:17 +0100107 "dfu "
108#endif
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200109 "tftp [<interface> <dev>] [<addr>]\n"
Lukasz Majewskic2c146f2015-08-24 00:21:48 +0200110 " - device firmware upgrade via TFTP\n"
111 " on device <dev>, attached to interface\n"
112 " <interface>\n"
113 " [<addr>] - address where FIT image has been stored\n"
114#endif
Lukasz Majewskia006a5d2012-08-06 14:41:09 +0200115);