blob: d7bfb535dc68f9628f5ea7a85f56d7fcd580d60e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski9273f4f2012-08-06 14:41:09 +02002/*
3 * cmd_dfu.c -- dfu command
4 *
Lukasz Majewski1747a542015-08-24 00:21:48 +02005 * Copyright (C) 2015
6 * Lukasz Majewski <l.majewski@majess.pl>
7 *
Lukasz Majewski9273f4f2012-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 Majewski9273f4f2012-08-06 14:41:09 +020011 */
12
13#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060014#include <command.h>
Heiko Schocherc8848842015-04-14 09:53:13 +020015#include <watchdog.h>
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020016#include <dfu.h>
Simon Glassa73bda42015-11-08 23:47:45 -070017#include <console.h>
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020018#include <g_dnl.h>
Mateusz Zalegad862f892013-10-04 19:22:26 +020019#include <usb.h>
Lukasz Majewski1747a542015-08-24 00:21:48 +020020#include <net.h>
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020021
Simon Glassed38aef2020-05-10 11:40:03 -060022static int do_dfu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020023{
Lukasz Majewskid9eae702014-08-25 11:07:28 +020024
Patrick Delaunay06164032019-10-14 09:28:02 +020025 if (argc < 2)
Mateusz Zalegad862f892013-10-04 19:22:26 +020026 return CMD_RET_USAGE;
27
Marek Vasut7f8d4362018-02-16 16:41:18 +010028#ifdef CONFIG_DFU_OVER_USB
Mateusz Zalegad862f892013-10-04 19:22:26 +020029 char *usb_controller = argv[1];
Marek Vasutea84f802018-02-16 16:41:17 +010030#endif
Patrick Delaunay06164032019-10-14 09:28:02 +020031 char *interface = NULL;
32 char *devstring = NULL;
Andy Shevchenko1e414012019-11-27 18:12:15 +020033#if defined(CONFIG_DFU_TIMEOUT) || defined(CONFIG_DFU_OVER_TFTP)
Andy Shevchenko9bb63ec2019-11-27 18:12:14 +020034 unsigned long value = 0;
35#endif
Patrick Delaunay06164032019-10-14 09:28:02 +020036 if (argc >= 4) {
37 interface = argv[2];
38 devstring = argv[3];
39 }
Andy Shevchenko9bb63ec2019-11-27 18:12:14 +020040
Andy Shevchenko1e414012019-11-27 18:12:15 +020041#if defined(CONFIG_DFU_TIMEOUT) || defined(CONFIG_DFU_OVER_TFTP)
Andy Shevchenko9bb63ec2019-11-27 18:12:14 +020042 if (argc == 5 || argc == 3)
43 value = simple_strtoul(argv[argc - 1], NULL, 0);
44#endif
Mateusz Zalegad862f892013-10-04 19:22:26 +020045
Marek Vasutea84f802018-02-16 16:41:17 +010046 int ret = 0;
Marek Vasut7f8d4362018-02-16 16:41:18 +010047#ifdef CONFIG_DFU_OVER_TFTP
Andy Shevchenko9bb63ec2019-11-27 18:12:14 +020048 if (!strcmp(argv[1], "tftp"))
49 return update_tftp(value, interface, devstring);
Lukasz Majewski1747a542015-08-24 00:21:48 +020050#endif
Stephen Warren4afe50f2014-06-11 16:03:33 -060051 ret = dfu_init_env_entities(interface, devstring);
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020052 if (ret)
Stephen Warrenf9d47dd2014-06-10 10:06:41 -060053 goto done;
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020054
Andy Shevchenko1e414012019-11-27 18:12:15 +020055#ifdef CONFIG_DFU_TIMEOUT
56 dfu_set_timeout(value * 1000);
57#endif
58
Stephen Warrenf9d47dd2014-06-10 10:06:41 -060059 ret = CMD_RET_SUCCESS;
Patrick Delaunay06164032019-10-14 09:28:02 +020060 if (strcmp(argv[argc - 1], "list") == 0) {
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020061 dfu_show_entities();
62 goto done;
63 }
64
Masami Hiramatsuf77f6a82022-01-31 11:52:54 +090065#ifdef CONFIG_DFU_OVER_USB
Mateusz Zalegad862f892013-10-04 19:22:26 +020066 int controller_index = simple_strtoul(usb_controller, NULL, 0);
Marek Szyprowskif5af8e42020-12-22 11:32:23 +010067 bool retry = false;
68 do {
Michal Simeke13fa3e2021-03-31 09:05:52 +020069 ret = run_usb_dnl_gadget(controller_index, "usb_dnl_dfu");
Lukasz Majewskid9eae702014-08-25 11:07:28 +020070
Marek Szyprowskif5af8e42020-12-22 11:32:23 +010071 if (dfu_reinit_needed) {
72 dfu_free_entities();
73 ret = dfu_init_env_entities(interface, devstring);
74 if (ret)
75 goto done;
76 retry = true;
77 }
78 } while (retry);
Lukasz Majewski2968aa22013-07-18 13:19:14 +020079
Masami Hiramatsuf77f6a82022-01-31 11:52:54 +090080#endif
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020081done:
82 dfu_free_entities();
Stephen Warrenf9d47dd2014-06-10 10:06:41 -060083 return ret;
Lukasz Majewski9273f4f2012-08-06 14:41:09 +020084}
85
86U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
87 "Device Firmware Upgrade",
Andy Shevchenko1cfb9562019-03-04 16:04:44 +020088 ""
Marek Vasut7f8d4362018-02-16 16:41:18 +010089#ifdef CONFIG_DFU_OVER_USB
Andy Shevchenko1e414012019-11-27 18:12:15 +020090#ifdef CONFIG_DFU_TIMEOUT
91 "<USB_controller> [<interface> <dev>] [<timeout>] [list]\n"
92#else
Patrick Delaunay06164032019-10-14 09:28:02 +020093 "<USB_controller> [<interface> <dev>] [list]\n"
Andy Shevchenko1e414012019-11-27 18:12:15 +020094#endif
Mateusz Zalegad862f892013-10-04 19:22:26 +020095 " - device firmware upgrade via <USB_controller>\n"
96 " on device <dev>, attached to interface\n"
97 " <interface>\n"
Andy Shevchenko1e414012019-11-27 18:12:15 +020098#ifdef CONFIG_DFU_TIMEOUT
99 " [<timeout>] - specify inactivity timeout in seconds\n"
100#endif
Marek Vasutea84f802018-02-16 16:41:17 +0100101#endif
Masami Hiramatsuf77f6a82022-01-31 11:52:54 +0900102 " [list] - list available alt settings\n"
Marek Vasut7f8d4362018-02-16 16:41:18 +0100103#ifdef CONFIG_DFU_OVER_TFTP
104#ifdef CONFIG_DFU_OVER_USB
Marek Vasutea84f802018-02-16 16:41:17 +0100105 "dfu "
106#endif
Patrick Delaunay06164032019-10-14 09:28:02 +0200107 "tftp [<interface> <dev>] [<addr>]\n"
Lukasz Majewski1747a542015-08-24 00:21:48 +0200108 " - 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 Majewski9273f4f2012-08-06 14:41:09 +0200113);