blob: 9bbcdbcfe0762f50d1c39c1554def117e8ccdfed [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>
Simon Glass5e6201b2019-08-01 09:46:51 -060013#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070014#include <image.h>
wdenk38635852002-08-27 05:55:31 +000015#include <net.h>
16
Simon Glassed38aef2020-05-10 11:40:03 -060017static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000018
Joe Hershbergerb0429462018-04-13 15:26:30 -050019#ifdef CONFIG_CMD_BOOTP
Simon Glassed38aef2020-05-10 11:40:03 -060020static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
21 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000022{
Kim Phillipsdc00a682012-10-29 13:34:31 +000023 return netboot_common(BOOTP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000024}
25
wdenkf287a242003-07-01 21:06:45 +000026U_BOOT_CMD(
27 bootp, 3, 1, do_bootp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060028 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020029 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000030);
Joe Hershbergerb0429462018-04-13 15:26:30 -050031#endif
wdenk57b2d802003-06-27 21:31:46 +000032
Joe Hershbergerb0429462018-04-13 15:26:30 -050033#ifdef CONFIG_CMD_TFTPBOOT
Simon Glassed38aef2020-05-10 11:40:03 -060034int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk38635852002-08-27 05:55:31 +000035{
Simon Glass768cbf02011-12-10 11:08:06 +000036 int ret;
37
38 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
39 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
40 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
41 return ret;
wdenk38635852002-08-27 05:55:31 +000042}
43
wdenkf287a242003-07-01 21:06:45 +000044U_BOOT_CMD(
45 tftpboot, 3, 1, do_tftpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060046 "boot image via network using TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020047 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000048);
Joe Hershbergerb0429462018-04-13 15:26:30 -050049#endif
wdenk57b2d802003-06-27 21:31:46 +000050
Simon Glass85d96ec2011-10-24 18:00:08 +000051#ifdef CONFIG_CMD_TFTPPUT
Simon Glassed38aef2020-05-10 11:40:03 -060052static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
53 char *const argv[])
Simon Glass85d96ec2011-10-24 18:00:08 +000054{
Joe Hershbergerba867832015-03-22 17:09:09 -050055 return netboot_common(TFTPPUT, cmdtp, argc, argv);
Simon Glass85d96ec2011-10-24 18:00:08 +000056}
57
58U_BOOT_CMD(
59 tftpput, 4, 1, do_tftpput,
60 "TFTP put command, for uploading files to a server",
61 "Address Size [[hostIPaddr:]filename]"
62);
63#endif
64
Luca Ceresoli7aa81a42011-05-17 00:03:40 +000065#ifdef CONFIG_CMD_TFTPSRV
Simon Glassed38aef2020-05-10 11:40:03 -060066static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
67 char *const argv[])
Luca Ceresoli7aa81a42011-05-17 00:03:40 +000068{
69 return netboot_common(TFTPSRV, cmdtp, argc, argv);
70}
71
72U_BOOT_CMD(
73 tftpsrv, 2, 1, do_tftpsrv,
74 "act as a TFTP server and boot the first received file",
75 "[loadAddress]\n"
76 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
77 "The transfer is aborted if a transfer has not been started after\n"
78 "about 50 seconds or if Ctrl-C is pressed."
79);
80#endif
81
82
Peter Tyserf7a48ca2010-09-30 11:25:48 -050083#ifdef CONFIG_CMD_RARP
Simon Glassed38aef2020-05-10 11:40:03 -060084int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk38635852002-08-27 05:55:31 +000085{
Kim Phillipsdc00a682012-10-29 13:34:31 +000086 return netboot_common(RARP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000087}
88
wdenkf287a242003-07-01 21:06:45 +000089U_BOOT_CMD(
90 rarpboot, 3, 1, do_rarpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060091 "boot image via network using RARP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020092 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000093);
Peter Tyserf7a48ca2010-09-30 11:25:48 -050094#endif
wdenk57b2d802003-06-27 21:31:46 +000095
Jon Loeligerd76b5c12007-07-08 18:02:23 -050096#if defined(CONFIG_CMD_DHCP)
Simon Glassed38aef2020-05-10 11:40:03 -060097static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
98 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000099{
100 return netboot_common(DHCP, cmdtp, argc, argv);
101}
wdenk57b2d802003-06-27 21:31:46 +0000102
wdenkf287a242003-07-01 21:06:45 +0000103U_BOOT_CMD(
104 dhcp, 3, 1, do_dhcp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600105 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200106 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +0000107);
Jon Loeligerd704d912007-07-10 11:02:44 -0500108#endif
wdenk38635852002-08-27 05:55:31 +0000109
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500110#if defined(CONFIG_CMD_NFS)
Simon Glassed38aef2020-05-10 11:40:03 -0600111static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
112 char *const argv[])
wdenkbe9c1cb2004-02-24 02:00:03 +0000113{
114 return netboot_common(NFS, cmdtp, argc, argv);
115}
116
117U_BOOT_CMD(
118 nfs, 3, 1, do_nfs,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600119 "boot image via network using NFS protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200120 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkbe9c1cb2004-02-24 02:00:03 +0000121);
Jon Loeligerd704d912007-07-10 11:02:44 -0500122#endif
wdenkbe9c1cb2004-02-24 02:00:03 +0000123
Kim Phillipsdc00a682012-10-29 13:34:31 +0000124static void netboot_update_env(void)
wdenk38635852002-08-27 05:55:31 +0000125{
wdenk05939202004-04-18 17:39:38 +0000126 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000127
Joe Hershberger5874dec2015-04-08 01:41:01 -0500128 if (net_gateway.s_addr) {
129 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600130 env_set("gatewayip", tmp);
wdenk05939202004-04-18 17:39:38 +0000131 }
wdenk38635852002-08-27 05:55:31 +0000132
Joe Hershberger5874dec2015-04-08 01:41:01 -0500133 if (net_netmask.s_addr) {
134 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600135 env_set("netmask", tmp);
wdenk05939202004-04-18 17:39:38 +0000136 }
wdenk38635852002-08-27 05:55:31 +0000137
Krebs, Olafd9249382020-03-09 14:27:55 +0000138#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500139 if (net_hostname[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600140 env_set("hostname", net_hostname);
Krebs, Olafd9249382020-03-09 14:27:55 +0000141#endif
wdenk38635852002-08-27 05:55:31 +0000142
Krebs, Olafd9249382020-03-09 14:27:55 +0000143#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500144 if (net_root_path[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600145 env_set("rootpath", net_root_path);
Krebs, Olafd9249382020-03-09 14:27:55 +0000146#endif
wdenk38635852002-08-27 05:55:31 +0000147
Joe Hershberger5874dec2015-04-08 01:41:01 -0500148 if (net_ip.s_addr) {
149 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600150 env_set("ipaddr", tmp);
wdenk05939202004-04-18 17:39:38 +0000151 }
Joe Hershbergerd4762042012-05-23 07:59:17 +0000152#if !defined(CONFIG_BOOTP_SERVERIP)
153 /*
Baruch Siachbc713252016-12-27 11:03:29 +0200154 * Only attempt to change serverip if net/bootp.c:store_net_params()
Joe Hershbergerd4762042012-05-23 07:59:17 +0000155 * could have set it
156 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500157 if (net_server_ip.s_addr) {
158 ip_to_string(net_server_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600159 env_set("serverip", tmp);
wdenk05939202004-04-18 17:39:38 +0000160 }
Joe Hershbergerd4762042012-05-23 07:59:17 +0000161#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500162 if (net_dns_server.s_addr) {
163 ip_to_string(net_dns_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600164 env_set("dnsip", tmp);
wdenk05939202004-04-18 17:39:38 +0000165 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500166#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500167 if (net_dns_server2.s_addr) {
168 ip_to_string(net_dns_server2, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600169 env_set("dnsip2", tmp);
wdenk05939202004-04-18 17:39:38 +0000170 }
stroesee0aadfb2003-08-28 14:17:32 +0000171#endif
Krebs, Olafd9249382020-03-09 14:27:55 +0000172#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500173 if (net_nis_domain[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600174 env_set("domain", net_nis_domain);
Krebs, Olafd9249382020-03-09 14:27:55 +0000175#endif
wdenkb4ad9622005-04-01 00:25:43 +0000176
Joe Hershberger013d3872015-04-08 01:41:17 -0500177#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500178 if (net_ntp_time_offset) {
179 sprintf(tmp, "%d", net_ntp_time_offset);
Simon Glass6a38e412017-08-03 12:22:09 -0600180 env_set("timeoffset", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000181 }
182#endif
Joe Hershberger013d3872015-04-08 01:41:17 -0500183#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500184 if (net_ntp_server.s_addr) {
185 ip_to_string(net_ntp_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600186 env_set("ntpserverip", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000187 }
188#endif
wdenk38635852002-08-27 05:55:31 +0000189}
wdenk05939202004-04-18 17:39:38 +0000190
Simon Glassed38aef2020-05-10 11:40:03 -0600191static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
192 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000193{
194 char *s;
Peter Tyser24c297d2008-12-02 12:59:51 -0600195 char *end;
wdenk38635852002-08-27 05:55:31 +0000196 int rcode = 0;
197 int size;
Peter Tyser24c297d2008-12-02 12:59:51 -0600198 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000199
Alexander Graff43bf5d2018-06-15 10:29:27 +0200200 net_boot_file_name_explicit = false;
201
Simon Glass892265d2019-12-28 10:45:02 -0700202 /* pre-set image_load_addr */
Simon Glass64b723f2017-08-03 12:22:12 -0600203 s = env_get("loadaddr");
Joe Hershberger013d3872015-04-08 01:41:17 -0500204 if (s != NULL)
Simon Glass892265d2019-12-28 10:45:02 -0700205 image_load_addr = simple_strtoul(s, NULL, 16);
wdenk38635852002-08-27 05:55:31 +0000206
207 switch (argc) {
208 case 1:
Joe Hershberger2e135272018-07-03 19:36:42 -0500209 /* refresh bootfile name from env */
210 copy_filename(net_boot_file_name, env_get("bootfile"),
211 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000212 break;
213
Peter Tyser24c297d2008-12-02 12:59:51 -0600214 case 2: /*
215 * Only one arg - accept two forms:
216 * Just load address, or just boot file name. The latter
217 * form must be written in a format which can not be
218 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000219 */
Peter Tyser24c297d2008-12-02 12:59:51 -0600220 addr = simple_strtoul(argv[1], &end, 16);
Alexander Graff43bf5d2018-06-15 10:29:27 +0200221 if (end == (argv[1] + strlen(argv[1]))) {
Simon Glass892265d2019-12-28 10:45:02 -0700222 image_load_addr = addr;
Joe Hershberger2e135272018-07-03 19:36:42 -0500223 /* refresh bootfile name from env */
224 copy_filename(net_boot_file_name, env_get("bootfile"),
225 sizeof(net_boot_file_name));
Alexander Graff43bf5d2018-06-15 10:29:27 +0200226 } else {
227 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500228 copy_filename(net_boot_file_name, argv[1],
229 sizeof(net_boot_file_name));
Alexander Graff43bf5d2018-06-15 10:29:27 +0200230 }
wdenk38635852002-08-27 05:55:31 +0000231 break;
232
Joe Hershberger013d3872015-04-08 01:41:17 -0500233 case 3:
Simon Glass892265d2019-12-28 10:45:02 -0700234 image_load_addr = simple_strtoul(argv[1], NULL, 16);
Alexander Graff43bf5d2018-06-15 10:29:27 +0200235 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500236 copy_filename(net_boot_file_name, argv[2],
237 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000238
239 break;
240
Simon Glass85d96ec2011-10-24 18:00:08 +0000241#ifdef CONFIG_CMD_TFTPPUT
242 case 4:
Simon Glass892265d2019-12-28 10:45:02 -0700243 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
244 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
Simon Glass778fe692011-12-19 16:10:43 +0000245 printf("Invalid address/size\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500246 return CMD_RET_USAGE;
Simon Glass778fe692011-12-19 16:10:43 +0000247 }
Alexander Graff43bf5d2018-06-15 10:29:27 +0200248 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500249 copy_filename(net_boot_file_name, argv[3],
250 sizeof(net_boot_file_name));
Simon Glass85d96ec2011-10-24 18:00:08 +0000251 break;
252#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200253 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000254 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glassa06dfc72011-12-10 08:44:01 +0000255 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000256 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000257 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000258
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500259 size = net_loop(proto);
Joe Hershberger013d3872015-04-08 01:41:17 -0500260 if (size < 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000261 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
Joe Hershbergerba867832015-03-22 17:09:09 -0500262 return CMD_RET_FAILURE;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200263 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000264 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000265
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500266 /* net_loop ok, update environment */
wdenk38635852002-08-27 05:55:31 +0000267 netboot_update_env();
268
wdenkc217f6d2002-11-11 02:11:37 +0000269 /* done if no file was loaded (no errors though) */
Heiko Schocher633e03a2007-06-22 19:11:54 +0200270 if (size == 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000271 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
Joe Hershbergerba867832015-03-22 17:09:09 -0500272 return CMD_RET_SUCCESS;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200273 }
wdenkc217f6d2002-11-11 02:11:37 +0000274
Simon Glass0169e6b2012-02-13 13:51:18 +0000275 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glass964d1d42012-01-14 15:24:52 +0000276
Mike Frysinger194c2e82011-06-05 13:43:02 +0000277 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000278
Joe Hershbergerba867832015-03-22 17:09:09 -0500279 if (rcode == CMD_RET_SUCCESS)
Simon Glass0169e6b2012-02-13 13:51:18 +0000280 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
Joe Hershbergerba867832015-03-22 17:09:09 -0500281 else
282 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
wdenk38635852002-08-27 05:55:31 +0000283 return rcode;
284}
wdenke6466f62003-06-05 19:27:42 +0000285
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500286#if defined(CONFIG_CMD_PING)
Simon Glassed38aef2020-05-10 11:40:03 -0600287static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
288 char *const argv[])
wdenke6466f62003-06-05 19:27:42 +0000289{
290 if (argc < 2)
Joe Hershbergerba867832015-03-22 17:09:09 -0500291 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000292
Joe Hershberger5874dec2015-04-08 01:41:01 -0500293 net_ping_ip = string_to_ip(argv[1]);
294 if (net_ping_ip.s_addr == 0)
Simon Glassa06dfc72011-12-10 08:44:01 +0000295 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000296
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500297 if (net_loop(PING) < 0) {
wdenke6466f62003-06-05 19:27:42 +0000298 printf("ping failed; host %s is not alive\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500299 return CMD_RET_FAILURE;
wdenke6466f62003-06-05 19:27:42 +0000300 }
301
302 printf("host %s is alive\n", argv[1]);
303
Joe Hershbergerba867832015-03-22 17:09:09 -0500304 return CMD_RET_SUCCESS;
wdenke6466f62003-06-05 19:27:42 +0000305}
wdenkca9bc762003-07-15 07:45:49 +0000306
307U_BOOT_CMD(
308 ping, 2, 1, do_ping,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600309 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200310 "pingAddress"
wdenkca9bc762003-07-15 07:45:49 +0000311);
Jon Loeligerd704d912007-07-10 11:02:44 -0500312#endif
wdenk38635852002-08-27 05:55:31 +0000313
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500314#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000315
316static void cdp_update_env(void)
317{
318 char tmp[16];
319
Joe Hershberger527336f2015-04-08 01:41:14 -0500320 if (cdp_appliance_vlan != htons(-1)) {
321 printf("CDP offered appliance VLAN %d\n",
322 ntohs(cdp_appliance_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500323 vlan_to_string(cdp_appliance_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600324 env_set("vlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500325 net_our_vlan = cdp_appliance_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000326 }
327
Joe Hershberger527336f2015-04-08 01:41:14 -0500328 if (cdp_native_vlan != htons(-1)) {
329 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500330 vlan_to_string(cdp_native_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600331 env_set("nvlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500332 net_native_vlan = cdp_native_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000333 }
wdenk145d2c12004-04-15 21:48:45 +0000334}
335
Simon Glassed38aef2020-05-10 11:40:03 -0600336int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk145d2c12004-04-15 21:48:45 +0000337{
338 int r;
339
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500340 r = net_loop(CDP);
wdenk145d2c12004-04-15 21:48:45 +0000341 if (r < 0) {
342 printf("cdp failed; perhaps not a CISCO switch?\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500343 return CMD_RET_FAILURE;
wdenk145d2c12004-04-15 21:48:45 +0000344 }
345
346 cdp_update_env();
347
Joe Hershbergerba867832015-03-22 17:09:09 -0500348 return CMD_RET_SUCCESS;
wdenk145d2c12004-04-15 21:48:45 +0000349}
350
351U_BOOT_CMD(
352 cdp, 1, 1, do_cdp,
Wolfgang Denkd29afc32010-10-26 23:42:23 +0200353 "Perform CDP network configuration",
Wolfgang Denkf003d922010-12-23 17:02:18 +0100354 "\n"
wdenk145d2c12004-04-15 21:48:45 +0000355);
Jon Loeligerd704d912007-07-10 11:02:44 -0500356#endif
wdenk145d2c12004-04-15 21:48:45 +0000357
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500358#if defined(CONFIG_CMD_SNTP)
Simon Glassed38aef2020-05-10 11:40:03 -0600359int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenkb4ad9622005-04-01 00:25:43 +0000360{
361 char *toff;
362
363 if (argc < 2) {
Simon Glassda1a1342017-08-03 12:22:15 -0600364 net_ntp_server = env_get_ip("ntpserverip");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500365 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000366 printf("ntpserverip not set\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500367 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000368 }
369 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -0500370 net_ntp_server = string_to_ip(argv[1]);
371 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000372 printf("Bad NTP server IP address\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500373 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000374 }
375 }
376
Simon Glass64b723f2017-08-03 12:22:12 -0600377 toff = env_get("timeoffset");
Kim Phillipsdc00a682012-10-29 13:34:31 +0000378 if (toff == NULL)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500379 net_ntp_time_offset = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000380 else
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500381 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
wdenkb4ad9622005-04-01 00:25:43 +0000382
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500383 if (net_loop(SNTP) < 0) {
Luuk Paulussen7cb3f892011-05-16 18:27:11 +0000384 printf("SNTP failed: host %pI4 not responding\n",
Joe Hershberger013d3872015-04-08 01:41:17 -0500385 &net_ntp_server);
Joe Hershbergerba867832015-03-22 17:09:09 -0500386 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000387 }
388
Joe Hershbergerba867832015-03-22 17:09:09 -0500389 return CMD_RET_SUCCESS;
wdenkb4ad9622005-04-01 00:25:43 +0000390}
391
392U_BOOT_CMD(
393 sntp, 2, 1, do_sntp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600394 "synchronize RTC via network",
wdenkb4ad9622005-04-01 00:25:43 +0000395 "[NTP server IP]\n"
396);
Jon Loeligerd704d912007-07-10 11:02:44 -0500397#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400398
399#if defined(CONFIG_CMD_DNS)
Simon Glassed38aef2020-05-10 11:40:03 -0600400int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Robin Getz82f0d232009-07-20 14:53:54 -0400401{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200402 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000403 return CMD_RET_USAGE;
Robin Getz82f0d232009-07-20 14:53:54 -0400404
405 /*
406 * We should check for a valid hostname:
407 * - Each label must be between 1 and 63 characters long
408 * - the entire hostname has a maximum of 255 characters
409 * - only the ASCII letters 'a' through 'z' (case-insensitive),
410 * the digits '0' through '9', and the hyphen
411 * - cannot begin or end with a hyphen
412 * - no other symbols, punctuation characters, or blank spaces are
413 * permitted
414 * but hey - this is a minimalist implmentation, so only check length
415 * and let the name server deal with things.
416 */
417 if (strlen(argv[1]) >= 255) {
418 printf("dns error: hostname too long\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500419 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400420 }
421
Joe Hershbergerf725e342015-04-08 01:41:15 -0500422 net_dns_resolve = argv[1];
Robin Getz82f0d232009-07-20 14:53:54 -0400423
424 if (argc == 3)
Joe Hershbergerf725e342015-04-08 01:41:15 -0500425 net_dns_env_var = argv[2];
Robin Getz82f0d232009-07-20 14:53:54 -0400426 else
Joe Hershbergerf725e342015-04-08 01:41:15 -0500427 net_dns_env_var = NULL;
Robin Getz82f0d232009-07-20 14:53:54 -0400428
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500429 if (net_loop(DNS) < 0) {
Robin Getz82f0d232009-07-20 14:53:54 -0400430 printf("dns lookup of %s failed, check setup\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500431 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400432 }
433
Joe Hershbergerba867832015-03-22 17:09:09 -0500434 return CMD_RET_SUCCESS;
Robin Getz82f0d232009-07-20 14:53:54 -0400435}
436
437U_BOOT_CMD(
438 dns, 3, 1, do_dns,
439 "lookup the IP of a hostname",
440 "hostname [envvar]"
441);
442
443#endif /* CONFIG_CMD_DNS */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000444
445#if defined(CONFIG_CMD_LINK_LOCAL)
Simon Glassed38aef2020-05-10 11:40:03 -0600446static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
447 char *const argv[])
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000448{
449 char tmp[22];
450
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500451 if (net_loop(LINKLOCAL) < 0)
Joe Hershbergerba867832015-03-22 17:09:09 -0500452 return CMD_RET_FAILURE;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000453
Joe Hershberger5874dec2015-04-08 01:41:01 -0500454 net_gateway.s_addr = 0;
455 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600456 env_set("gatewayip", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000457
Joe Hershberger5874dec2015-04-08 01:41:01 -0500458 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600459 env_set("netmask", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000460
Joe Hershberger5874dec2015-04-08 01:41:01 -0500461 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600462 env_set("ipaddr", tmp);
463 env_set("llipaddr", tmp); /* store this for next time */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000464
Joe Hershbergerba867832015-03-22 17:09:09 -0500465 return CMD_RET_SUCCESS;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000466}
467
468U_BOOT_CMD(
469 linklocal, 1, 1, do_link_local,
470 "acquire a network IP address using the link-local protocol",
471 ""
472);
473
474#endif /* CONFIG_CMD_LINK_LOCAL */