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