blob: 0e9f200ca97e6959cfb1985c0e1af0f9302a2ee3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38635852002-08-27 05:55:31 +00005 */
6
7/*
8 * Boot support
9 */
10#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -060011#include <bootstage.h>
wdenk38635852002-08-27 05:55:31 +000012#include <command.h>
Tim Harveyc4e71e72021-06-18 15:26:21 -070013#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060014#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070015#include <image.h>
wdenk38635852002-08-27 05:55:31 +000016#include <net.h>
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +030017#include <net6.h>
Philippe Reynes2829d992020-09-18 14:13:02 +020018#include <net/udp.h>
19#include <net/sntp.h>
Samuel Mendoza-Jonasfeebd6c2022-08-08 21:46:04 +093020#include <net/ncsi.h>
wdenk38635852002-08-27 05:55:31 +000021
Simon Glassed38aef2020-05-10 11:40:03 -060022static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000023
Joe Hershbergerb0429462018-04-13 15:26:30 -050024#ifdef CONFIG_CMD_BOOTP
Simon Glassed38aef2020-05-10 11:40:03 -060025static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
26 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000027{
Kim Phillipsdc00a682012-10-29 13:34:31 +000028 return netboot_common(BOOTP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000029}
30
wdenkf287a242003-07-01 21:06:45 +000031U_BOOT_CMD(
32 bootp, 3, 1, do_bootp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060033 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020034 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000035);
Joe Hershbergerb0429462018-04-13 15:26:30 -050036#endif
wdenk57b2d802003-06-27 21:31:46 +000037
Joe Hershbergerb0429462018-04-13 15:26:30 -050038#ifdef CONFIG_CMD_TFTPBOOT
Simon Glassed38aef2020-05-10 11:40:03 -060039int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk38635852002-08-27 05:55:31 +000040{
Simon Glass768cbf02011-12-10 11:08:06 +000041 int ret;
42
43 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
44 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
45 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
46 return ret;
wdenk38635852002-08-27 05:55:31 +000047}
48
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +030049#if IS_ENABLED(CONFIG_IPV6)
50U_BOOT_CMD(
51 tftpboot, 4, 1, do_tftpb,
52 "boot image via network using TFTP protocol\n"
53 "To use IPv6 add -ipv6 parameter or use IPv6 hostIPaddr framed "
54 "with [] brackets",
55 "[loadAddress] [[hostIPaddr:]bootfilename] [" USE_IP6_CMD_PARAM "]"
56);
57#else
wdenkf287a242003-07-01 21:06:45 +000058U_BOOT_CMD(
59 tftpboot, 3, 1, do_tftpb,
Heinrich Schuchardt7d289f12022-09-04 09:08:11 +020060 "load file via network using TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020061 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000062);
Joe Hershbergerb0429462018-04-13 15:26:30 -050063#endif
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +030064#endif
wdenk57b2d802003-06-27 21:31:46 +000065
Simon Glass85d96ec2011-10-24 18:00:08 +000066#ifdef CONFIG_CMD_TFTPPUT
Simon Glassed38aef2020-05-10 11:40:03 -060067static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
68 char *const argv[])
Simon Glass85d96ec2011-10-24 18:00:08 +000069{
Joe Hershbergerba867832015-03-22 17:09:09 -050070 return netboot_common(TFTPPUT, cmdtp, argc, argv);
Simon Glass85d96ec2011-10-24 18:00:08 +000071}
72
73U_BOOT_CMD(
74 tftpput, 4, 1, do_tftpput,
75 "TFTP put command, for uploading files to a server",
76 "Address Size [[hostIPaddr:]filename]"
77);
78#endif
79
Luca Ceresoli7aa81a42011-05-17 00:03:40 +000080#ifdef CONFIG_CMD_TFTPSRV
Simon Glassed38aef2020-05-10 11:40:03 -060081static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
82 char *const argv[])
Luca Ceresoli7aa81a42011-05-17 00:03:40 +000083{
84 return netboot_common(TFTPSRV, cmdtp, argc, argv);
85}
86
87U_BOOT_CMD(
88 tftpsrv, 2, 1, do_tftpsrv,
89 "act as a TFTP server and boot the first received file",
90 "[loadAddress]\n"
91 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
92 "The transfer is aborted if a transfer has not been started after\n"
93 "about 50 seconds or if Ctrl-C is pressed."
94);
95#endif
96
97
Peter Tyserf7a48ca2010-09-30 11:25:48 -050098#ifdef CONFIG_CMD_RARP
Simon Glassed38aef2020-05-10 11:40:03 -060099int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000100{
Kim Phillipsdc00a682012-10-29 13:34:31 +0000101 return netboot_common(RARP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +0000102}
103
wdenkf287a242003-07-01 21:06:45 +0000104U_BOOT_CMD(
105 rarpboot, 3, 1, do_rarpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600106 "boot image via network using RARP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200107 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +0000108);
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500109#endif
wdenk57b2d802003-06-27 21:31:46 +0000110
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500111#if defined(CONFIG_CMD_DHCP)
Simon Glassed38aef2020-05-10 11:40:03 -0600112static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
113 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000114{
115 return netboot_common(DHCP, cmdtp, argc, argv);
116}
wdenk57b2d802003-06-27 21:31:46 +0000117
wdenkf287a242003-07-01 21:06:45 +0000118U_BOOT_CMD(
119 dhcp, 3, 1, do_dhcp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600120 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200121 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +0000122);
Jon Loeligerd704d912007-07-10 11:02:44 -0500123#endif
wdenk38635852002-08-27 05:55:31 +0000124
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500125#if defined(CONFIG_CMD_NFS)
Simon Glassed38aef2020-05-10 11:40:03 -0600126static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
127 char *const argv[])
wdenkbe9c1cb2004-02-24 02:00:03 +0000128{
129 return netboot_common(NFS, cmdtp, argc, argv);
130}
131
132U_BOOT_CMD(
133 nfs, 3, 1, do_nfs,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600134 "boot image via network using NFS protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200135 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkbe9c1cb2004-02-24 02:00:03 +0000136);
Jon Loeligerd704d912007-07-10 11:02:44 -0500137#endif
wdenkbe9c1cb2004-02-24 02:00:03 +0000138
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800139#if defined(CONFIG_CMD_WGET)
140static int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
141{
142 return netboot_common(WGET, cmdtp, argc, argv);
143}
144
145U_BOOT_CMD(
146 wget, 3, 1, do_wget,
147 "boot image via network using HTTP protocol",
148 "[loadAddress] [[hostIPaddr:]path and image name]"
149);
150#endif
151
Kim Phillipsdc00a682012-10-29 13:34:31 +0000152static void netboot_update_env(void)
wdenk38635852002-08-27 05:55:31 +0000153{
wdenk05939202004-04-18 17:39:38 +0000154 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000155
Joe Hershberger5874dec2015-04-08 01:41:01 -0500156 if (net_gateway.s_addr) {
157 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600158 env_set("gatewayip", tmp);
wdenk05939202004-04-18 17:39:38 +0000159 }
wdenk38635852002-08-27 05:55:31 +0000160
Joe Hershberger5874dec2015-04-08 01:41:01 -0500161 if (net_netmask.s_addr) {
162 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600163 env_set("netmask", tmp);
wdenk05939202004-04-18 17:39:38 +0000164 }
wdenk38635852002-08-27 05:55:31 +0000165
Krebs, Olafd9249382020-03-09 14:27:55 +0000166#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500167 if (net_hostname[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600168 env_set("hostname", net_hostname);
Krebs, Olafd9249382020-03-09 14:27:55 +0000169#endif
wdenk38635852002-08-27 05:55:31 +0000170
Krebs, Olafd9249382020-03-09 14:27:55 +0000171#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500172 if (net_root_path[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600173 env_set("rootpath", net_root_path);
Krebs, Olafd9249382020-03-09 14:27:55 +0000174#endif
wdenk38635852002-08-27 05:55:31 +0000175
Joe Hershberger5874dec2015-04-08 01:41:01 -0500176 if (net_ip.s_addr) {
177 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600178 env_set("ipaddr", tmp);
wdenk05939202004-04-18 17:39:38 +0000179 }
Joe Hershbergerd4762042012-05-23 07:59:17 +0000180 /*
Baruch Siachbc713252016-12-27 11:03:29 +0200181 * Only attempt to change serverip if net/bootp.c:store_net_params()
Joe Hershbergerd4762042012-05-23 07:59:17 +0000182 * could have set it
183 */
Simon Glass297fc202021-12-18 11:27:52 -0700184 if (!IS_ENABLED(CONFIG_BOOTP_SERVERIP) && net_server_ip.s_addr) {
Joe Hershberger5874dec2015-04-08 01:41:01 -0500185 ip_to_string(net_server_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600186 env_set("serverip", tmp);
wdenk05939202004-04-18 17:39:38 +0000187 }
Joe Hershberger5874dec2015-04-08 01:41:01 -0500188 if (net_dns_server.s_addr) {
189 ip_to_string(net_dns_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600190 env_set("dnsip", tmp);
wdenk05939202004-04-18 17:39:38 +0000191 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500192#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500193 if (net_dns_server2.s_addr) {
194 ip_to_string(net_dns_server2, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600195 env_set("dnsip2", tmp);
wdenk05939202004-04-18 17:39:38 +0000196 }
stroesee0aadfb2003-08-28 14:17:32 +0000197#endif
Krebs, Olafd9249382020-03-09 14:27:55 +0000198#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500199 if (net_nis_domain[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600200 env_set("domain", net_nis_domain);
Krebs, Olafd9249382020-03-09 14:27:55 +0000201#endif
wdenkb4ad9622005-04-01 00:25:43 +0000202
Joe Hershberger013d3872015-04-08 01:41:17 -0500203#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500204 if (net_ntp_time_offset) {
205 sprintf(tmp, "%d", net_ntp_time_offset);
Simon Glass6a38e412017-08-03 12:22:09 -0600206 env_set("timeoffset", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000207 }
208#endif
Joe Hershberger013d3872015-04-08 01:41:17 -0500209#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500210 if (net_ntp_server.s_addr) {
211 ip_to_string(net_ntp_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600212 env_set("ntpserverip", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000213 }
214#endif
wdenk38635852002-08-27 05:55:31 +0000215}
wdenk05939202004-04-18 17:39:38 +0000216
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000217/**
218 * parse_addr_size() - parse address and size arguments for tftpput
219 *
220 * @argv: command line arguments
221 * Return: 0 on success
222 */
223static int parse_addr_size(char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000224{
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000225 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
226 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
227 printf("Invalid address/size\n");
228 return CMD_RET_USAGE;
229 }
230 return 0;
231}
Alexander Graff43bf5d2018-06-15 10:29:27 +0200232
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000233/**
234 * parse_args() - parse command line arguments
235 *
236 * @proto: command prototype
237 * @argc: number of arguments
238 * @argv: command line arguments
239 * Return: 0 on success
240 */
241static int parse_args(enum proto_t proto, int argc, char *const argv[])
242{
243 ulong addr;
244 char *end;
wdenk38635852002-08-27 05:55:31 +0000245
246 switch (argc) {
247 case 1:
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000248 if (CONFIG_IS_ENABLED(CMD_TFTPPUT) && proto == TFTPPUT)
249 return 1;
250
Joe Hershberger2e135272018-07-03 19:36:42 -0500251 /* refresh bootfile name from env */
252 copy_filename(net_boot_file_name, env_get("bootfile"),
253 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000254 break;
255
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000256 case 2:
257 if (CONFIG_IS_ENABLED(CMD_TFTPPUT) && proto == TFTPPUT)
258 return 1;
259 /*
Peter Tyser24c297d2008-12-02 12:59:51 -0600260 * Only one arg - accept two forms:
261 * Just load address, or just boot file name. The latter
262 * form must be written in a format which can not be
263 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000264 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600265 addr = hextoul(argv[1], &end);
Alexander Graff43bf5d2018-06-15 10:29:27 +0200266 if (end == (argv[1] + strlen(argv[1]))) {
Simon Glass892265d2019-12-28 10:45:02 -0700267 image_load_addr = addr;
Joe Hershberger2e135272018-07-03 19:36:42 -0500268 /* refresh bootfile name from env */
269 copy_filename(net_boot_file_name, env_get("bootfile"),
270 sizeof(net_boot_file_name));
Alexander Graff43bf5d2018-06-15 10:29:27 +0200271 } else {
272 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500273 copy_filename(net_boot_file_name, argv[1],
274 sizeof(net_boot_file_name));
Alexander Graff43bf5d2018-06-15 10:29:27 +0200275 }
wdenk38635852002-08-27 05:55:31 +0000276 break;
277
Joe Hershberger013d3872015-04-08 01:41:17 -0500278 case 3:
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000279 if (CONFIG_IS_ENABLED(CMD_TFTPPUT) && proto == TFTPPUT) {
280 if (parse_addr_size(argv))
281 return 1;
282 } else {
283 image_load_addr = hextoul(argv[1], NULL);
284 net_boot_file_name_explicit = true;
285 copy_filename(net_boot_file_name, argv[2],
286 sizeof(net_boot_file_name));
287 }
wdenk38635852002-08-27 05:55:31 +0000288 break;
289
Simon Glass85d96ec2011-10-24 18:00:08 +0000290#ifdef CONFIG_CMD_TFTPPUT
291 case 4:
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000292 if (parse_addr_size(argv))
293 return 1;
Alexander Graff43bf5d2018-06-15 10:29:27 +0200294 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500295 copy_filename(net_boot_file_name, argv[3],
296 sizeof(net_boot_file_name));
Simon Glass85d96ec2011-10-24 18:00:08 +0000297 break;
298#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200299 default:
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000300 return 1;
301 }
302 return 0;
303}
304
305static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
306 char *const argv[])
307{
308 char *s;
309 int rcode = 0;
310 int size;
311
312 net_boot_file_name_explicit = false;
313 *net_boot_file_name = '\0';
314
315 /* pre-set image_load_addr */
316 s = env_get("loadaddr");
317 if (s != NULL)
318 image_load_addr = hextoul(s, NULL);
319
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +0300320 if (IS_ENABLED(CONFIG_IPV6)) {
321 use_ip6 = false;
322
323 /* IPv6 parameter has to be always *last* */
324 if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM)) {
325 use_ip6 = true;
326 /* It is a hack not to break switch/case code */
327 --argc;
328 }
329 }
330
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000331 if (parse_args(proto, argc, argv)) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000332 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glassa06dfc72011-12-10 08:44:01 +0000333 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000334 }
Heinrich Schuchardt107b3a42022-09-03 12:21:09 +0000335
Simon Glass0169e6b2012-02-13 13:51:18 +0000336 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000337
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +0300338 if (IS_ENABLED(CONFIG_IPV6) && !use_ip6) {
339 char *s, *e;
340 size_t len;
341
342 s = strchr(net_boot_file_name, '[');
343 e = strchr(net_boot_file_name, ']');
344 if (s && e) {
345 len = e - s;
346 if (!string_to_ip6(s + 1, len - 1, &net_server_ip6))
347 use_ip6 = true;
348 }
349 }
350
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500351 size = net_loop(proto);
Joe Hershberger013d3872015-04-08 01:41:17 -0500352 if (size < 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000353 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
Joe Hershbergerba867832015-03-22 17:09:09 -0500354 return CMD_RET_FAILURE;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200355 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000356 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000357
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500358 /* net_loop ok, update environment */
wdenk38635852002-08-27 05:55:31 +0000359 netboot_update_env();
360
wdenkc217f6d2002-11-11 02:11:37 +0000361 /* done if no file was loaded (no errors though) */
Heiko Schocher633e03a2007-06-22 19:11:54 +0200362 if (size == 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000363 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
Joe Hershbergerba867832015-03-22 17:09:09 -0500364 return CMD_RET_SUCCESS;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200365 }
wdenkc217f6d2002-11-11 02:11:37 +0000366
Simon Glass0169e6b2012-02-13 13:51:18 +0000367 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glass964d1d42012-01-14 15:24:52 +0000368
Mike Frysinger194c2e82011-06-05 13:43:02 +0000369 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000370
Joe Hershbergerba867832015-03-22 17:09:09 -0500371 if (rcode == CMD_RET_SUCCESS)
Simon Glass0169e6b2012-02-13 13:51:18 +0000372 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
Joe Hershbergerba867832015-03-22 17:09:09 -0500373 else
374 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
wdenk38635852002-08-27 05:55:31 +0000375 return rcode;
376}
wdenke6466f62003-06-05 19:27:42 +0000377
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500378#if defined(CONFIG_CMD_PING)
Simon Glassed38aef2020-05-10 11:40:03 -0600379static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
380 char *const argv[])
wdenke6466f62003-06-05 19:27:42 +0000381{
382 if (argc < 2)
Joe Hershbergerba867832015-03-22 17:09:09 -0500383 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000384
Joe Hershberger5874dec2015-04-08 01:41:01 -0500385 net_ping_ip = string_to_ip(argv[1]);
386 if (net_ping_ip.s_addr == 0)
Simon Glassa06dfc72011-12-10 08:44:01 +0000387 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000388
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500389 if (net_loop(PING) < 0) {
wdenke6466f62003-06-05 19:27:42 +0000390 printf("ping failed; host %s is not alive\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500391 return CMD_RET_FAILURE;
wdenke6466f62003-06-05 19:27:42 +0000392 }
393
394 printf("host %s is alive\n", argv[1]);
395
Joe Hershbergerba867832015-03-22 17:09:09 -0500396 return CMD_RET_SUCCESS;
wdenke6466f62003-06-05 19:27:42 +0000397}
wdenkca9bc762003-07-15 07:45:49 +0000398
399U_BOOT_CMD(
400 ping, 2, 1, do_ping,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600401 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200402 "pingAddress"
wdenkca9bc762003-07-15 07:45:49 +0000403);
Jon Loeligerd704d912007-07-10 11:02:44 -0500404#endif
wdenk38635852002-08-27 05:55:31 +0000405
Viacheslav Mitrofanove03c8aa2022-12-02 12:18:08 +0300406#if IS_ENABLED(CONFIG_CMD_PING6)
407int do_ping6(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
408{
409 if (string_to_ip6(argv[1], strlen(argv[1]), &net_ping_ip6))
410 return CMD_RET_USAGE;
411
412 use_ip6 = true;
413 if (net_loop(PING6) < 0) {
414 use_ip6 = false;
415 printf("ping6 failed; host %pI6c is not alive\n",
416 &net_ping_ip6);
417 return 1;
418 }
419
420 use_ip6 = false;
421 printf("host %pI6c is alive\n", &net_ping_ip6);
422 return 0;
423}
424
425U_BOOT_CMD(
426 ping6, 2, 1, do_ping6,
427 "send ICMPv6 ECHO_REQUEST to network host",
428 "pingAddress"
429);
430#endif /* CONFIG_CMD_PING6 */
431
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500432#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000433
434static void cdp_update_env(void)
435{
436 char tmp[16];
437
Joe Hershberger527336f2015-04-08 01:41:14 -0500438 if (cdp_appliance_vlan != htons(-1)) {
439 printf("CDP offered appliance VLAN %d\n",
440 ntohs(cdp_appliance_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500441 vlan_to_string(cdp_appliance_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600442 env_set("vlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500443 net_our_vlan = cdp_appliance_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000444 }
445
Joe Hershberger527336f2015-04-08 01:41:14 -0500446 if (cdp_native_vlan != htons(-1)) {
447 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500448 vlan_to_string(cdp_native_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600449 env_set("nvlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500450 net_native_vlan = cdp_native_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000451 }
wdenk145d2c12004-04-15 21:48:45 +0000452}
453
Simon Glassed38aef2020-05-10 11:40:03 -0600454int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk145d2c12004-04-15 21:48:45 +0000455{
456 int r;
457
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500458 r = net_loop(CDP);
wdenk145d2c12004-04-15 21:48:45 +0000459 if (r < 0) {
460 printf("cdp failed; perhaps not a CISCO switch?\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500461 return CMD_RET_FAILURE;
wdenk145d2c12004-04-15 21:48:45 +0000462 }
463
464 cdp_update_env();
465
Joe Hershbergerba867832015-03-22 17:09:09 -0500466 return CMD_RET_SUCCESS;
wdenk145d2c12004-04-15 21:48:45 +0000467}
468
469U_BOOT_CMD(
470 cdp, 1, 1, do_cdp,
Wolfgang Denkd29afc32010-10-26 23:42:23 +0200471 "Perform CDP network configuration",
Wolfgang Denkf003d922010-12-23 17:02:18 +0100472 "\n"
wdenk145d2c12004-04-15 21:48:45 +0000473);
Jon Loeligerd704d912007-07-10 11:02:44 -0500474#endif
wdenk145d2c12004-04-15 21:48:45 +0000475
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500476#if defined(CONFIG_CMD_SNTP)
Philippe Reynes2829d992020-09-18 14:13:02 +0200477static struct udp_ops sntp_ops = {
478 .prereq = sntp_prereq,
479 .start = sntp_start,
480 .data = NULL,
481};
482
Simon Glassed38aef2020-05-10 11:40:03 -0600483int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenkb4ad9622005-04-01 00:25:43 +0000484{
485 char *toff;
486
487 if (argc < 2) {
Simon Glassda1a1342017-08-03 12:22:15 -0600488 net_ntp_server = env_get_ip("ntpserverip");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500489 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000490 printf("ntpserverip not set\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500491 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000492 }
493 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -0500494 net_ntp_server = string_to_ip(argv[1]);
495 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000496 printf("Bad NTP server IP address\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500497 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000498 }
499 }
500
Simon Glass64b723f2017-08-03 12:22:12 -0600501 toff = env_get("timeoffset");
Kim Phillipsdc00a682012-10-29 13:34:31 +0000502 if (toff == NULL)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500503 net_ntp_time_offset = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000504 else
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500505 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
wdenkb4ad9622005-04-01 00:25:43 +0000506
Philippe Reynes2829d992020-09-18 14:13:02 +0200507 if (udp_loop(&sntp_ops) < 0) {
Luuk Paulussen7cb3f892011-05-16 18:27:11 +0000508 printf("SNTP failed: host %pI4 not responding\n",
Joe Hershberger013d3872015-04-08 01:41:17 -0500509 &net_ntp_server);
Joe Hershbergerba867832015-03-22 17:09:09 -0500510 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000511 }
512
Joe Hershbergerba867832015-03-22 17:09:09 -0500513 return CMD_RET_SUCCESS;
wdenkb4ad9622005-04-01 00:25:43 +0000514}
515
516U_BOOT_CMD(
517 sntp, 2, 1, do_sntp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600518 "synchronize RTC via network",
wdenkb4ad9622005-04-01 00:25:43 +0000519 "[NTP server IP]\n"
520);
Jon Loeligerd704d912007-07-10 11:02:44 -0500521#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400522
523#if defined(CONFIG_CMD_DNS)
Simon Glassed38aef2020-05-10 11:40:03 -0600524int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Robin Getz82f0d232009-07-20 14:53:54 -0400525{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200526 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000527 return CMD_RET_USAGE;
Robin Getz82f0d232009-07-20 14:53:54 -0400528
529 /*
530 * We should check for a valid hostname:
531 * - Each label must be between 1 and 63 characters long
532 * - the entire hostname has a maximum of 255 characters
533 * - only the ASCII letters 'a' through 'z' (case-insensitive),
534 * the digits '0' through '9', and the hyphen
535 * - cannot begin or end with a hyphen
536 * - no other symbols, punctuation characters, or blank spaces are
537 * permitted
538 * but hey - this is a minimalist implmentation, so only check length
539 * and let the name server deal with things.
540 */
541 if (strlen(argv[1]) >= 255) {
542 printf("dns error: hostname too long\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500543 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400544 }
545
Joe Hershbergerf725e342015-04-08 01:41:15 -0500546 net_dns_resolve = argv[1];
Robin Getz82f0d232009-07-20 14:53:54 -0400547
548 if (argc == 3)
Joe Hershbergerf725e342015-04-08 01:41:15 -0500549 net_dns_env_var = argv[2];
Robin Getz82f0d232009-07-20 14:53:54 -0400550 else
Joe Hershbergerf725e342015-04-08 01:41:15 -0500551 net_dns_env_var = NULL;
Robin Getz82f0d232009-07-20 14:53:54 -0400552
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500553 if (net_loop(DNS) < 0) {
Robin Getz82f0d232009-07-20 14:53:54 -0400554 printf("dns lookup of %s failed, check setup\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500555 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400556 }
557
Joe Hershbergerba867832015-03-22 17:09:09 -0500558 return CMD_RET_SUCCESS;
Robin Getz82f0d232009-07-20 14:53:54 -0400559}
560
561U_BOOT_CMD(
562 dns, 3, 1, do_dns,
563 "lookup the IP of a hostname",
564 "hostname [envvar]"
565);
566
567#endif /* CONFIG_CMD_DNS */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000568
569#if defined(CONFIG_CMD_LINK_LOCAL)
Simon Glassed38aef2020-05-10 11:40:03 -0600570static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
571 char *const argv[])
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000572{
573 char tmp[22];
574
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500575 if (net_loop(LINKLOCAL) < 0)
Joe Hershbergerba867832015-03-22 17:09:09 -0500576 return CMD_RET_FAILURE;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000577
Joe Hershberger5874dec2015-04-08 01:41:01 -0500578 net_gateway.s_addr = 0;
579 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600580 env_set("gatewayip", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000581
Joe Hershberger5874dec2015-04-08 01:41:01 -0500582 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600583 env_set("netmask", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000584
Joe Hershberger5874dec2015-04-08 01:41:01 -0500585 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600586 env_set("ipaddr", tmp);
587 env_set("llipaddr", tmp); /* store this for next time */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000588
Joe Hershbergerba867832015-03-22 17:09:09 -0500589 return CMD_RET_SUCCESS;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000590}
591
592U_BOOT_CMD(
593 linklocal, 1, 1, do_link_local,
594 "acquire a network IP address using the link-local protocol",
595 ""
596);
597
598#endif /* CONFIG_CMD_LINK_LOCAL */
Tim Harveyc4e71e72021-06-18 15:26:21 -0700599
600#ifdef CONFIG_DM_ETH
601static int do_net_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
602{
603 const struct udevice *current = eth_get_dev();
604 unsigned char env_enetaddr[ARP_HLEN];
605 const struct udevice *dev;
606 struct uclass *uc;
607
608 uclass_id_foreach_dev(UCLASS_ETH, dev, uc) {
609 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
610 printf("eth%d : %s %pM %s\n", dev_seq(dev), dev->name, env_enetaddr,
611 current == dev ? "active" : "");
612 }
613 return CMD_RET_SUCCESS;
614}
615
616static struct cmd_tbl cmd_net[] = {
617 U_BOOT_CMD_MKENT(list, 1, 0, do_net_list, "", ""),
618};
619
620static int do_net(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
621{
622 struct cmd_tbl *cp;
623
624 cp = find_cmd_tbl(argv[1], cmd_net, ARRAY_SIZE(cmd_net));
625
626 /* Drop the net command */
627 argc--;
628 argv++;
629
630 if (!cp || argc > cp->maxargs)
631 return CMD_RET_USAGE;
632 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
633 return CMD_RET_SUCCESS;
634
635 return cp->cmd(cmdtp, flag, argc, argv);
636}
637
638U_BOOT_CMD(
639 net, 2, 1, do_net,
640 "NET sub-system",
641 "list - list available devices\n"
642);
643#endif // CONFIG_DM_ETH
Samuel Mendoza-Jonasfeebd6c2022-08-08 21:46:04 +0930644
645#if defined(CONFIG_CMD_NCSI)
646static int do_ncsi(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
647{
648 if (!phy_interface_is_ncsi() || !ncsi_active()) {
649 printf("Device not configured for NC-SI\n");
650 return CMD_RET_FAILURE;
651 }
652
653 if (net_loop(NCSI) < 0)
654 return CMD_RET_FAILURE;
655
656 return CMD_RET_SUCCESS;
657}
658
659U_BOOT_CMD(
660 ncsi, 1, 1, do_ncsi,
661 "Configure attached NIC via NC-SI",
662 ""
663);
664#endif /* CONFIG_CMD_NCSI */