blob: 46f0190588e05b160c2eb3071ab8e229e512c77d [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
Simon Glass09140112020-05-10 11:40:03 -060013#include <command.h>
Heiko Schocher0a9ac5c2015-04-14 09:53:13 +020014#include <watchdog.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020015#include <dfu.h>
Simon Glass24b852a2015-11-08 23:47:45 -070016#include <console.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020017#include <g_dnl.h>
Mateusz Zalega16297cf2013-10-04 19:22:26 +020018#include <usb.h>
Lukasz Majewskic2c146f2015-08-24 00:21:48 +020019#include <net.h>
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020020
Simon Glass09140112020-05-10 11:40:03 -060021static int do_dfu(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020022{
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020023
Patrick Delaunayfebabe32019-10-14 09:28:02 +020024 if (argc < 2)
Mateusz Zalega16297cf2013-10-04 19:22:26 +020025 return CMD_RET_USAGE;
26
Marek Vasutbb4059a2018-02-16 16:41:18 +010027#ifdef CONFIG_DFU_OVER_USB
Mateusz Zalega16297cf2013-10-04 19:22:26 +020028 char *usb_controller = argv[1];
Marek Vasut0f44d332018-02-16 16:41:17 +010029#endif
Patrick Delaunayfebabe32019-10-14 09:28:02 +020030 char *interface = NULL;
31 char *devstring = NULL;
Andy Shevchenko98a8f442019-11-27 18:12:15 +020032#if defined(CONFIG_DFU_TIMEOUT) || defined(CONFIG_DFU_OVER_TFTP)
Andy Shevchenko2b1f8c22019-11-27 18:12:14 +020033 unsigned long value = 0;
34#endif
Patrick Delaunayfebabe32019-10-14 09:28:02 +020035 if (argc >= 4) {
36 interface = argv[2];
37 devstring = argv[3];
38 }
Andy Shevchenko2b1f8c22019-11-27 18:12:14 +020039
Andy Shevchenko98a8f442019-11-27 18:12:15 +020040#if defined(CONFIG_DFU_TIMEOUT) || defined(CONFIG_DFU_OVER_TFTP)
Andy Shevchenko2b1f8c22019-11-27 18:12:14 +020041 if (argc == 5 || argc == 3)
42 value = simple_strtoul(argv[argc - 1], NULL, 0);
43#endif
Mateusz Zalega16297cf2013-10-04 19:22:26 +020044
Marek Vasut0f44d332018-02-16 16:41:17 +010045 int ret = 0;
Marek Vasutbb4059a2018-02-16 16:41:18 +010046#ifdef CONFIG_DFU_OVER_TFTP
Andy Shevchenko2b1f8c22019-11-27 18:12:14 +020047 if (!strcmp(argv[1], "tftp"))
48 return update_tftp(value, interface, devstring);
Lukasz Majewskic2c146f2015-08-24 00:21:48 +020049#endif
Stephen Warrendd648272014-06-11 16:03:33 -060050 ret = dfu_init_env_entities(interface, devstring);
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020051 if (ret)
Stephen Warrenafb8e712014-06-10 10:06:41 -060052 goto done;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020053
Andy Shevchenko98a8f442019-11-27 18:12:15 +020054#ifdef CONFIG_DFU_TIMEOUT
55 dfu_set_timeout(value * 1000);
56#endif
57
Stephen Warrenafb8e712014-06-10 10:06:41 -060058 ret = CMD_RET_SUCCESS;
Patrick Delaunayfebabe32019-10-14 09:28:02 +020059 if (strcmp(argv[argc - 1], "list") == 0) {
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020060 dfu_show_entities();
61 goto done;
62 }
63
Masami Hiramatsue9b0fd82022-01-31 11:52:54 +090064#ifdef CONFIG_DFU_OVER_USB
Mateusz Zalega16297cf2013-10-04 19:22:26 +020065 int controller_index = simple_strtoul(usb_controller, NULL, 0);
Marek Szyprowski9129f2f2020-12-22 11:32:23 +010066 bool retry = false;
67 do {
Michal Simek74fe3f22021-03-31 09:05:52 +020068 ret = run_usb_dnl_gadget(controller_index, "usb_dnl_dfu");
Lukasz Majewski1cc03c52014-08-25 11:07:28 +020069
Marek Szyprowski9129f2f2020-12-22 11:32:23 +010070 if (dfu_reinit_needed) {
71 dfu_free_entities();
72 ret = dfu_init_env_entities(interface, devstring);
73 if (ret)
74 goto done;
75 retry = true;
76 }
77 } while (retry);
Lukasz Majewski6bed7ce2013-07-18 13:19:14 +020078
Masami Hiramatsue9b0fd82022-01-31 11:52:54 +090079#endif
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020080done:
81 dfu_free_entities();
Stephen Warrenafb8e712014-06-10 10:06:41 -060082 return ret;
Lukasz Majewskia006a5d2012-08-06 14:41:09 +020083}
84
85U_BOOT_CMD(dfu, CONFIG_SYS_MAXARGS, 1, do_dfu,
86 "Device Firmware Upgrade",
Andy Shevchenkocbc10812019-03-04 16:04:44 +020087 ""
Marek Vasutbb4059a2018-02-16 16:41:18 +010088#ifdef CONFIG_DFU_OVER_USB
Andy Shevchenko98a8f442019-11-27 18:12:15 +020089#ifdef CONFIG_DFU_TIMEOUT
90 "<USB_controller> [<interface> <dev>] [<timeout>] [list]\n"
91#else
Patrick Delaunayfebabe32019-10-14 09:28:02 +020092 "<USB_controller> [<interface> <dev>] [list]\n"
Andy Shevchenko98a8f442019-11-27 18:12:15 +020093#endif
Mateusz Zalega16297cf2013-10-04 19:22:26 +020094 " - device firmware upgrade via <USB_controller>\n"
95 " on device <dev>, attached to interface\n"
96 " <interface>\n"
Andy Shevchenko98a8f442019-11-27 18:12:15 +020097#ifdef CONFIG_DFU_TIMEOUT
98 " [<timeout>] - specify inactivity timeout in seconds\n"
99#endif
Marek Vasut0f44d332018-02-16 16:41:17 +0100100#endif
Masami Hiramatsue9b0fd82022-01-31 11:52:54 +0900101 " [list] - list available alt settings\n"
Marek Vasutbb4059a2018-02-16 16:41:18 +0100102#ifdef CONFIG_DFU_OVER_TFTP
103#ifdef CONFIG_DFU_OVER_USB
Marek Vasut0f44d332018-02-16 16:41:17 +0100104 "dfu "
105#endif
Patrick Delaunayfebabe32019-10-14 09:28:02 +0200106 "tftp [<interface> <dev>] [<addr>]\n"
Lukasz Majewskic2c146f2015-08-24 00:21:48 +0200107 " - 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 Majewskia006a5d2012-08-06 14:41:09 +0200112);