blob: 25390b0474f4c9032cea1c678a1053e9d195a3f5 [file] [log] [blame]
Tom Rini83d290c2018-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 Glass52f24232020-05-10 11:40:00 -060011#include <bootstage.h>
wdenk38635852002-08-27 05:55:31 +000012#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060013#include <env.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070014#include <image.h>
wdenk38635852002-08-27 05:55:31 +000015#include <net.h>
16
Simon Glass09140112020-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 Hershbergerd7a45ea2018-04-13 15:26:30 -050019#ifdef CONFIG_CMD_BOOTP
Simon Glass09140112020-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 Phillips088f1b12012-10-29 13:34:31 +000023 return netboot_common(BOOTP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000024}
25
wdenk0d498392003-07-01 21:06:45 +000026U_BOOT_CMD(
27 bootp, 3, 1, do_bootp,
Peter Tyser2fb26042009-01-27 18:03:12 -060028 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020029 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000030);
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050031#endif
wdenk8bde7f72003-06-27 21:31:46 +000032
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050033#ifdef CONFIG_CMD_TFTPBOOT
Simon Glass09140112020-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 Glass573f14f2011-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
wdenk0d498392003-07-01 21:06:45 +000044U_BOOT_CMD(
45 tftpboot, 3, 1, do_tftpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060046 "boot image via network using TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020047 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000048);
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050049#endif
wdenk8bde7f72003-06-27 21:31:46 +000050
Simon Glass2d46cf22011-10-24 18:00:08 +000051#ifdef CONFIG_CMD_TFTPPUT
Simon Glass09140112020-05-10 11:40:03 -060052static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
53 char *const argv[])
Simon Glass2d46cf22011-10-24 18:00:08 +000054{
Joe Hershberger85848f02015-03-22 17:09:09 -050055 return netboot_common(TFTPPUT, cmdtp, argc, argv);
Simon Glass2d46cf22011-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 Ceresoli7a83af02011-05-17 00:03:40 +000065#ifdef CONFIG_CMD_TFTPSRV
Simon Glass09140112020-05-10 11:40:03 -060066static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
67 char *const argv[])
Luca Ceresoli7a83af02011-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 Tyserbf6cb242010-09-30 11:25:48 -050083#ifdef CONFIG_CMD_RARP
Simon Glass09140112020-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 Phillips088f1b12012-10-29 13:34:31 +000086 return netboot_common(RARP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000087}
88
wdenk0d498392003-07-01 21:06:45 +000089U_BOOT_CMD(
90 rarpboot, 3, 1, do_rarpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060091 "boot image via network using RARP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020092 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000093);
Peter Tyserbf6cb242010-09-30 11:25:48 -050094#endif
wdenk8bde7f72003-06-27 21:31:46 +000095
Jon Loeligerc76fe472007-07-08 18:02:23 -050096#if defined(CONFIG_CMD_DHCP)
Simon Glass09140112020-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}
wdenk8bde7f72003-06-27 21:31:46 +0000102
wdenk0d498392003-07-01 21:06:45 +0000103U_BOOT_CMD(
104 dhcp, 3, 1, do_dhcp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600105 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200106 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +0000107);
Jon Loeliger90253172007-07-10 11:02:44 -0500108#endif
wdenk38635852002-08-27 05:55:31 +0000109
Jon Loeligerc76fe472007-07-08 18:02:23 -0500110#if defined(CONFIG_CMD_NFS)
Simon Glass09140112020-05-10 11:40:03 -0600111static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
112 char *const argv[])
wdenkcbd8a352004-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 Tyser2fb26042009-01-27 18:03:12 -0600119 "boot image via network using NFS protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200120 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkcbd8a352004-02-24 02:00:03 +0000121);
Jon Loeliger90253172007-07-10 11:02:44 -0500122#endif
wdenkcbd8a352004-02-24 02:00:03 +0000123
Kim Phillips088f1b12012-10-29 13:34:31 +0000124static void netboot_update_env(void)
wdenk38635852002-08-27 05:55:31 +0000125{
wdenk6e592382004-04-18 17:39:38 +0000126 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000127
Joe Hershberger049a95a2015-04-08 01:41:01 -0500128 if (net_gateway.s_addr) {
129 ip_to_string(net_gateway, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600130 env_set("gatewayip", tmp);
wdenk6e592382004-04-18 17:39:38 +0000131 }
wdenk38635852002-08-27 05:55:31 +0000132
Joe Hershberger049a95a2015-04-08 01:41:01 -0500133 if (net_netmask.s_addr) {
134 ip_to_string(net_netmask, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600135 env_set("netmask", tmp);
wdenk6e592382004-04-18 17:39:38 +0000136 }
wdenk38635852002-08-27 05:55:31 +0000137
Joe Hershberger586cbe52015-04-08 01:41:03 -0500138 if (net_hostname[0])
Simon Glass382bee52017-08-03 12:22:09 -0600139 env_set("hostname", net_hostname);
wdenk38635852002-08-27 05:55:31 +0000140
Joe Hershberger586cbe52015-04-08 01:41:03 -0500141 if (net_root_path[0])
Simon Glass382bee52017-08-03 12:22:09 -0600142 env_set("rootpath", net_root_path);
wdenk38635852002-08-27 05:55:31 +0000143
Joe Hershberger049a95a2015-04-08 01:41:01 -0500144 if (net_ip.s_addr) {
145 ip_to_string(net_ip, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600146 env_set("ipaddr", tmp);
wdenk6e592382004-04-18 17:39:38 +0000147 }
Joe Hershbergera3e1a722012-05-23 07:59:17 +0000148#if !defined(CONFIG_BOOTP_SERVERIP)
149 /*
Baruch Siachff78ad22016-12-27 11:03:29 +0200150 * Only attempt to change serverip if net/bootp.c:store_net_params()
Joe Hershbergera3e1a722012-05-23 07:59:17 +0000151 * could have set it
152 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500153 if (net_server_ip.s_addr) {
154 ip_to_string(net_server_ip, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600155 env_set("serverip", tmp);
wdenk6e592382004-04-18 17:39:38 +0000156 }
Joe Hershbergera3e1a722012-05-23 07:59:17 +0000157#endif
Joe Hershberger049a95a2015-04-08 01:41:01 -0500158 if (net_dns_server.s_addr) {
159 ip_to_string(net_dns_server, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600160 env_set("dnsip", tmp);
wdenk6e592382004-04-18 17:39:38 +0000161 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500162#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500163 if (net_dns_server2.s_addr) {
164 ip_to_string(net_dns_server2, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600165 env_set("dnsip2", tmp);
wdenk6e592382004-04-18 17:39:38 +0000166 }
stroesefe389a82003-08-28 14:17:32 +0000167#endif
Joe Hershberger586cbe52015-04-08 01:41:03 -0500168 if (net_nis_domain[0])
Simon Glass382bee52017-08-03 12:22:09 -0600169 env_set("domain", net_nis_domain);
wdenkea287de2005-04-01 00:25:43 +0000170
Joe Hershberger4fd50552015-04-08 01:41:17 -0500171#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500172 if (net_ntp_time_offset) {
173 sprintf(tmp, "%d", net_ntp_time_offset);
Simon Glass382bee52017-08-03 12:22:09 -0600174 env_set("timeoffset", tmp);
wdenkea287de2005-04-01 00:25:43 +0000175 }
176#endif
Joe Hershberger4fd50552015-04-08 01:41:17 -0500177#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500178 if (net_ntp_server.s_addr) {
179 ip_to_string(net_ntp_server, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600180 env_set("ntpserverip", tmp);
wdenkea287de2005-04-01 00:25:43 +0000181 }
182#endif
wdenk38635852002-08-27 05:55:31 +0000183}
wdenk6e592382004-04-18 17:39:38 +0000184
Simon Glass09140112020-05-10 11:40:03 -0600185static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
186 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000187{
188 char *s;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600189 char *end;
wdenk38635852002-08-27 05:55:31 +0000190 int rcode = 0;
191 int size;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600192 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000193
Alexander Graf449312c2018-06-15 10:29:27 +0200194 net_boot_file_name_explicit = false;
195
Simon Glassbb872dd2019-12-28 10:45:02 -0700196 /* pre-set image_load_addr */
Simon Glass00caae62017-08-03 12:22:12 -0600197 s = env_get("loadaddr");
Joe Hershberger4fd50552015-04-08 01:41:17 -0500198 if (s != NULL)
Simon Glassbb872dd2019-12-28 10:45:02 -0700199 image_load_addr = simple_strtoul(s, NULL, 16);
wdenk38635852002-08-27 05:55:31 +0000200
201 switch (argc) {
202 case 1:
Joe Hershbergerf43308f2018-07-03 19:36:42 -0500203 /* refresh bootfile name from env */
204 copy_filename(net_boot_file_name, env_get("bootfile"),
205 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000206 break;
207
Peter Tyser2e4970d2008-12-02 12:59:51 -0600208 case 2: /*
209 * Only one arg - accept two forms:
210 * Just load address, or just boot file name. The latter
211 * form must be written in a format which can not be
212 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000213 */
Peter Tyser2e4970d2008-12-02 12:59:51 -0600214 addr = simple_strtoul(argv[1], &end, 16);
Alexander Graf449312c2018-06-15 10:29:27 +0200215 if (end == (argv[1] + strlen(argv[1]))) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700216 image_load_addr = addr;
Joe Hershbergerf43308f2018-07-03 19:36:42 -0500217 /* refresh bootfile name from env */
218 copy_filename(net_boot_file_name, env_get("bootfile"),
219 sizeof(net_boot_file_name));
Alexander Graf449312c2018-06-15 10:29:27 +0200220 } else {
221 net_boot_file_name_explicit = true;
Joe Hershberger14111572015-04-08 01:41:02 -0500222 copy_filename(net_boot_file_name, argv[1],
223 sizeof(net_boot_file_name));
Alexander Graf449312c2018-06-15 10:29:27 +0200224 }
wdenk38635852002-08-27 05:55:31 +0000225 break;
226
Joe Hershberger4fd50552015-04-08 01:41:17 -0500227 case 3:
Simon Glassbb872dd2019-12-28 10:45:02 -0700228 image_load_addr = simple_strtoul(argv[1], NULL, 16);
Alexander Graf449312c2018-06-15 10:29:27 +0200229 net_boot_file_name_explicit = true;
Joe Hershberger14111572015-04-08 01:41:02 -0500230 copy_filename(net_boot_file_name, argv[2],
231 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000232
233 break;
234
Simon Glass2d46cf22011-10-24 18:00:08 +0000235#ifdef CONFIG_CMD_TFTPPUT
236 case 4:
Simon Glassbb872dd2019-12-28 10:45:02 -0700237 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
238 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
Simon Glass38bd80b2011-12-19 16:10:43 +0000239 printf("Invalid address/size\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500240 return CMD_RET_USAGE;
Simon Glass38bd80b2011-12-19 16:10:43 +0000241 }
Alexander Graf449312c2018-06-15 10:29:27 +0200242 net_boot_file_name_explicit = true;
Joe Hershberger14111572015-04-08 01:41:02 -0500243 copy_filename(net_boot_file_name, argv[3],
244 sizeof(net_boot_file_name));
Simon Glass2d46cf22011-10-24 18:00:08 +0000245 break;
246#endif
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200247 default:
Simon Glass770605e2012-02-13 13:51:18 +0000248 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glass4c12eeb2011-12-10 08:44:01 +0000249 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000250 }
Simon Glass770605e2012-02-13 13:51:18 +0000251 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000252
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500253 size = net_loop(proto);
Joe Hershberger4fd50552015-04-08 01:41:17 -0500254 if (size < 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000255 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
Joe Hershberger85848f02015-03-22 17:09:09 -0500256 return CMD_RET_FAILURE;
Heiko Schocher566a4942007-06-22 19:11:54 +0200257 }
Simon Glass770605e2012-02-13 13:51:18 +0000258 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000259
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500260 /* net_loop ok, update environment */
wdenk38635852002-08-27 05:55:31 +0000261 netboot_update_env();
262
wdenkeb9401e2002-11-11 02:11:37 +0000263 /* done if no file was loaded (no errors though) */
Heiko Schocher566a4942007-06-22 19:11:54 +0200264 if (size == 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000265 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
Joe Hershberger85848f02015-03-22 17:09:09 -0500266 return CMD_RET_SUCCESS;
Heiko Schocher566a4942007-06-22 19:11:54 +0200267 }
wdenkeb9401e2002-11-11 02:11:37 +0000268
Simon Glass770605e2012-02-13 13:51:18 +0000269 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glassc8e66db2012-01-14 15:24:52 +0000270
Mike Frysinger67d668b2011-06-05 13:43:02 +0000271 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000272
Joe Hershberger85848f02015-03-22 17:09:09 -0500273 if (rcode == CMD_RET_SUCCESS)
Simon Glass770605e2012-02-13 13:51:18 +0000274 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
Joe Hershberger85848f02015-03-22 17:09:09 -0500275 else
276 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
wdenk38635852002-08-27 05:55:31 +0000277 return rcode;
278}
279
Jon Loeligerc76fe472007-07-08 18:02:23 -0500280#if defined(CONFIG_CMD_PING)
Simon Glass09140112020-05-10 11:40:03 -0600281static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
282 char *const argv[])
wdenk73a8b272003-06-05 19:27:42 +0000283{
284 if (argc < 2)
Joe Hershberger85848f02015-03-22 17:09:09 -0500285 return CMD_RET_USAGE;
wdenk73a8b272003-06-05 19:27:42 +0000286
Joe Hershberger049a95a2015-04-08 01:41:01 -0500287 net_ping_ip = string_to_ip(argv[1]);
288 if (net_ping_ip.s_addr == 0)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000289 return CMD_RET_USAGE;
wdenk73a8b272003-06-05 19:27:42 +0000290
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500291 if (net_loop(PING) < 0) {
wdenk73a8b272003-06-05 19:27:42 +0000292 printf("ping failed; host %s is not alive\n", argv[1]);
Joe Hershberger85848f02015-03-22 17:09:09 -0500293 return CMD_RET_FAILURE;
wdenk73a8b272003-06-05 19:27:42 +0000294 }
295
296 printf("host %s is alive\n", argv[1]);
297
Joe Hershberger85848f02015-03-22 17:09:09 -0500298 return CMD_RET_SUCCESS;
wdenk73a8b272003-06-05 19:27:42 +0000299}
wdenk6dff5522003-07-15 07:45:49 +0000300
301U_BOOT_CMD(
302 ping, 2, 1, do_ping,
Peter Tyser2fb26042009-01-27 18:03:12 -0600303 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200304 "pingAddress"
wdenk6dff5522003-07-15 07:45:49 +0000305);
Jon Loeliger90253172007-07-10 11:02:44 -0500306#endif
wdenk73a8b272003-06-05 19:27:42 +0000307
Jon Loeligerc76fe472007-07-08 18:02:23 -0500308#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000309
310static void cdp_update_env(void)
311{
312 char tmp[16];
313
Joe Hershberger6aede5b2015-04-08 01:41:14 -0500314 if (cdp_appliance_vlan != htons(-1)) {
315 printf("CDP offered appliance VLAN %d\n",
316 ntohs(cdp_appliance_vlan));
Joe Hershberger4fd50552015-04-08 01:41:17 -0500317 vlan_to_string(cdp_appliance_vlan, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600318 env_set("vlan", tmp);
Joe Hershberger4fd50552015-04-08 01:41:17 -0500319 net_our_vlan = cdp_appliance_vlan;
wdenka3d991b2004-04-15 21:48:45 +0000320 }
321
Joe Hershberger6aede5b2015-04-08 01:41:14 -0500322 if (cdp_native_vlan != htons(-1)) {
323 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
Joe Hershberger4fd50552015-04-08 01:41:17 -0500324 vlan_to_string(cdp_native_vlan, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600325 env_set("nvlan", tmp);
Joe Hershberger4fd50552015-04-08 01:41:17 -0500326 net_native_vlan = cdp_native_vlan;
wdenka3d991b2004-04-15 21:48:45 +0000327 }
wdenka3d991b2004-04-15 21:48:45 +0000328}
329
Simon Glass09140112020-05-10 11:40:03 -0600330int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenka3d991b2004-04-15 21:48:45 +0000331{
332 int r;
333
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500334 r = net_loop(CDP);
wdenka3d991b2004-04-15 21:48:45 +0000335 if (r < 0) {
336 printf("cdp failed; perhaps not a CISCO switch?\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500337 return CMD_RET_FAILURE;
wdenka3d991b2004-04-15 21:48:45 +0000338 }
339
340 cdp_update_env();
341
Joe Hershberger85848f02015-03-22 17:09:09 -0500342 return CMD_RET_SUCCESS;
wdenka3d991b2004-04-15 21:48:45 +0000343}
344
345U_BOOT_CMD(
346 cdp, 1, 1, do_cdp,
Wolfgang Denkec5c04c2010-10-26 23:42:23 +0200347 "Perform CDP network configuration",
Wolfgang Denk4b582662010-12-23 17:02:18 +0100348 "\n"
wdenka3d991b2004-04-15 21:48:45 +0000349);
Jon Loeliger90253172007-07-10 11:02:44 -0500350#endif
wdenka3d991b2004-04-15 21:48:45 +0000351
Jon Loeligerc76fe472007-07-08 18:02:23 -0500352#if defined(CONFIG_CMD_SNTP)
Simon Glass09140112020-05-10 11:40:03 -0600353int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenkea287de2005-04-01 00:25:43 +0000354{
355 char *toff;
356
357 if (argc < 2) {
Simon Glass723806c2017-08-03 12:22:15 -0600358 net_ntp_server = env_get_ip("ntpserverip");
Joe Hershberger049a95a2015-04-08 01:41:01 -0500359 if (net_ntp_server.s_addr == 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000360 printf("ntpserverip not set\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500361 return CMD_RET_FAILURE;
wdenkea287de2005-04-01 00:25:43 +0000362 }
363 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -0500364 net_ntp_server = string_to_ip(argv[1]);
365 if (net_ntp_server.s_addr == 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000366 printf("Bad NTP server IP address\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500367 return CMD_RET_FAILURE;
wdenkea287de2005-04-01 00:25:43 +0000368 }
369 }
370
Simon Glass00caae62017-08-03 12:22:12 -0600371 toff = env_get("timeoffset");
Kim Phillips088f1b12012-10-29 13:34:31 +0000372 if (toff == NULL)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500373 net_ntp_time_offset = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000374 else
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500375 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
wdenkea287de2005-04-01 00:25:43 +0000376
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500377 if (net_loop(SNTP) < 0) {
Luuk Paulussend6840e32011-05-16 18:27:11 +0000378 printf("SNTP failed: host %pI4 not responding\n",
Joe Hershberger4fd50552015-04-08 01:41:17 -0500379 &net_ntp_server);
Joe Hershberger85848f02015-03-22 17:09:09 -0500380 return CMD_RET_FAILURE;
wdenkea287de2005-04-01 00:25:43 +0000381 }
382
Joe Hershberger85848f02015-03-22 17:09:09 -0500383 return CMD_RET_SUCCESS;
wdenkea287de2005-04-01 00:25:43 +0000384}
385
386U_BOOT_CMD(
387 sntp, 2, 1, do_sntp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600388 "synchronize RTC via network",
wdenkea287de2005-04-01 00:25:43 +0000389 "[NTP server IP]\n"
390);
Jon Loeliger90253172007-07-10 11:02:44 -0500391#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400392
393#if defined(CONFIG_CMD_DNS)
Simon Glass09140112020-05-10 11:40:03 -0600394int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Robin Getz1a32bf42009-07-20 14:53:54 -0400395{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200396 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000397 return CMD_RET_USAGE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400398
399 /*
400 * We should check for a valid hostname:
401 * - Each label must be between 1 and 63 characters long
402 * - the entire hostname has a maximum of 255 characters
403 * - only the ASCII letters 'a' through 'z' (case-insensitive),
404 * the digits '0' through '9', and the hyphen
405 * - cannot begin or end with a hyphen
406 * - no other symbols, punctuation characters, or blank spaces are
407 * permitted
408 * but hey - this is a minimalist implmentation, so only check length
409 * and let the name server deal with things.
410 */
411 if (strlen(argv[1]) >= 255) {
412 printf("dns error: hostname too long\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500413 return CMD_RET_FAILURE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400414 }
415
Joe Hershberger786eac52015-04-08 01:41:15 -0500416 net_dns_resolve = argv[1];
Robin Getz1a32bf42009-07-20 14:53:54 -0400417
418 if (argc == 3)
Joe Hershberger786eac52015-04-08 01:41:15 -0500419 net_dns_env_var = argv[2];
Robin Getz1a32bf42009-07-20 14:53:54 -0400420 else
Joe Hershberger786eac52015-04-08 01:41:15 -0500421 net_dns_env_var = NULL;
Robin Getz1a32bf42009-07-20 14:53:54 -0400422
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500423 if (net_loop(DNS) < 0) {
Robin Getz1a32bf42009-07-20 14:53:54 -0400424 printf("dns lookup of %s failed, check setup\n", argv[1]);
Joe Hershberger85848f02015-03-22 17:09:09 -0500425 return CMD_RET_FAILURE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400426 }
427
Joe Hershberger85848f02015-03-22 17:09:09 -0500428 return CMD_RET_SUCCESS;
Robin Getz1a32bf42009-07-20 14:53:54 -0400429}
430
431U_BOOT_CMD(
432 dns, 3, 1, do_dns,
433 "lookup the IP of a hostname",
434 "hostname [envvar]"
435);
436
437#endif /* CONFIG_CMD_DNS */
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000438
439#if defined(CONFIG_CMD_LINK_LOCAL)
Simon Glass09140112020-05-10 11:40:03 -0600440static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
441 char *const argv[])
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000442{
443 char tmp[22];
444
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500445 if (net_loop(LINKLOCAL) < 0)
Joe Hershberger85848f02015-03-22 17:09:09 -0500446 return CMD_RET_FAILURE;
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000447
Joe Hershberger049a95a2015-04-08 01:41:01 -0500448 net_gateway.s_addr = 0;
449 ip_to_string(net_gateway, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600450 env_set("gatewayip", tmp);
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000451
Joe Hershberger049a95a2015-04-08 01:41:01 -0500452 ip_to_string(net_netmask, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600453 env_set("netmask", tmp);
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000454
Joe Hershberger049a95a2015-04-08 01:41:01 -0500455 ip_to_string(net_ip, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600456 env_set("ipaddr", tmp);
457 env_set("llipaddr", tmp); /* store this for next time */
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000458
Joe Hershberger85848f02015-03-22 17:09:09 -0500459 return CMD_RET_SUCCESS;
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000460}
461
462U_BOOT_CMD(
463 linklocal, 1, 1, do_link_local,
464 "acquire a network IP address using the link-local protocol",
465 ""
466);
467
468#endif /* CONFIG_CMD_LINK_LOCAL */