blob: 282b38ccf998bdd653c1d82cc635e1a955b7a392 [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 Glasse4bf0c52011-10-24 18:00:02 +000017static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000018
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050019#ifdef CONFIG_CMD_BOOTP
Kim Phillips088f1b12012-10-29 13:34:31 +000020static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000021{
Kim Phillips088f1b12012-10-29 13:34:31 +000022 return netboot_common(BOOTP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000023}
24
wdenk0d498392003-07-01 21:06:45 +000025U_BOOT_CMD(
26 bootp, 3, 1, do_bootp,
Peter Tyser2fb26042009-01-27 18:03:12 -060027 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020028 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000029);
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050030#endif
wdenk8bde7f72003-06-27 21:31:46 +000031
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050032#ifdef CONFIG_CMD_TFTPBOOT
Kim Phillips088f1b12012-10-29 13:34:31 +000033int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000034{
Simon Glass573f14f2011-12-10 11:08:06 +000035 int ret;
36
37 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
38 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
39 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
40 return ret;
wdenk38635852002-08-27 05:55:31 +000041}
42
wdenk0d498392003-07-01 21:06:45 +000043U_BOOT_CMD(
44 tftpboot, 3, 1, do_tftpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060045 "boot image via network using TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020046 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000047);
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050048#endif
wdenk8bde7f72003-06-27 21:31:46 +000049
Simon Glass2d46cf22011-10-24 18:00:08 +000050#ifdef CONFIG_CMD_TFTPPUT
Masahiro Yamada0c1b8692017-06-22 16:49:10 +090051static int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Simon Glass2d46cf22011-10-24 18:00:08 +000052{
Joe Hershberger85848f02015-03-22 17:09:09 -050053 return netboot_common(TFTPPUT, cmdtp, argc, argv);
Simon Glass2d46cf22011-10-24 18:00:08 +000054}
55
56U_BOOT_CMD(
57 tftpput, 4, 1, do_tftpput,
58 "TFTP put command, for uploading files to a server",
59 "Address Size [[hostIPaddr:]filename]"
60);
61#endif
62
Luca Ceresoli7a83af02011-05-17 00:03:40 +000063#ifdef CONFIG_CMD_TFTPSRV
64static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
65{
66 return netboot_common(TFTPSRV, cmdtp, argc, argv);
67}
68
69U_BOOT_CMD(
70 tftpsrv, 2, 1, do_tftpsrv,
71 "act as a TFTP server and boot the first received file",
72 "[loadAddress]\n"
73 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
74 "The transfer is aborted if a transfer has not been started after\n"
75 "about 50 seconds or if Ctrl-C is pressed."
76);
77#endif
78
79
Peter Tyserbf6cb242010-09-30 11:25:48 -050080#ifdef CONFIG_CMD_RARP
Kim Phillips088f1b12012-10-29 13:34:31 +000081int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000082{
Kim Phillips088f1b12012-10-29 13:34:31 +000083 return netboot_common(RARP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000084}
85
wdenk0d498392003-07-01 21:06:45 +000086U_BOOT_CMD(
87 rarpboot, 3, 1, do_rarpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060088 "boot image via network using RARP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020089 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000090);
Peter Tyserbf6cb242010-09-30 11:25:48 -050091#endif
wdenk8bde7f72003-06-27 21:31:46 +000092
Jon Loeligerc76fe472007-07-08 18:02:23 -050093#if defined(CONFIG_CMD_DHCP)
Kim Phillips088f1b12012-10-29 13:34:31 +000094static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000095{
96 return netboot_common(DHCP, cmdtp, argc, argv);
97}
wdenk8bde7f72003-06-27 21:31:46 +000098
wdenk0d498392003-07-01 21:06:45 +000099U_BOOT_CMD(
100 dhcp, 3, 1, do_dhcp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600101 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200102 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +0000103);
Jon Loeliger90253172007-07-10 11:02:44 -0500104#endif
wdenk38635852002-08-27 05:55:31 +0000105
Jon Loeligerc76fe472007-07-08 18:02:23 -0500106#if defined(CONFIG_CMD_NFS)
Kim Phillips088f1b12012-10-29 13:34:31 +0000107static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkcbd8a352004-02-24 02:00:03 +0000108{
109 return netboot_common(NFS, cmdtp, argc, argv);
110}
111
112U_BOOT_CMD(
113 nfs, 3, 1, do_nfs,
Peter Tyser2fb26042009-01-27 18:03:12 -0600114 "boot image via network using NFS protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200115 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkcbd8a352004-02-24 02:00:03 +0000116);
Jon Loeliger90253172007-07-10 11:02:44 -0500117#endif
wdenkcbd8a352004-02-24 02:00:03 +0000118
Kim Phillips088f1b12012-10-29 13:34:31 +0000119static void netboot_update_env(void)
wdenk38635852002-08-27 05:55:31 +0000120{
wdenk6e592382004-04-18 17:39:38 +0000121 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000122
Joe Hershberger049a95a2015-04-08 01:41:01 -0500123 if (net_gateway.s_addr) {
124 ip_to_string(net_gateway, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600125 env_set("gatewayip", tmp);
wdenk6e592382004-04-18 17:39:38 +0000126 }
wdenk38635852002-08-27 05:55:31 +0000127
Joe Hershberger049a95a2015-04-08 01:41:01 -0500128 if (net_netmask.s_addr) {
129 ip_to_string(net_netmask, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600130 env_set("netmask", tmp);
wdenk6e592382004-04-18 17:39:38 +0000131 }
wdenk38635852002-08-27 05:55:31 +0000132
Joe Hershberger586cbe52015-04-08 01:41:03 -0500133 if (net_hostname[0])
Simon Glass382bee52017-08-03 12:22:09 -0600134 env_set("hostname", net_hostname);
wdenk38635852002-08-27 05:55:31 +0000135
Joe Hershberger586cbe52015-04-08 01:41:03 -0500136 if (net_root_path[0])
Simon Glass382bee52017-08-03 12:22:09 -0600137 env_set("rootpath", net_root_path);
wdenk38635852002-08-27 05:55:31 +0000138
Joe Hershberger049a95a2015-04-08 01:41:01 -0500139 if (net_ip.s_addr) {
140 ip_to_string(net_ip, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600141 env_set("ipaddr", tmp);
wdenk6e592382004-04-18 17:39:38 +0000142 }
Joe Hershbergera3e1a722012-05-23 07:59:17 +0000143#if !defined(CONFIG_BOOTP_SERVERIP)
144 /*
Baruch Siachff78ad22016-12-27 11:03:29 +0200145 * Only attempt to change serverip if net/bootp.c:store_net_params()
Joe Hershbergera3e1a722012-05-23 07:59:17 +0000146 * could have set it
147 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500148 if (net_server_ip.s_addr) {
149 ip_to_string(net_server_ip, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600150 env_set("serverip", tmp);
wdenk6e592382004-04-18 17:39:38 +0000151 }
Joe Hershbergera3e1a722012-05-23 07:59:17 +0000152#endif
Joe Hershberger049a95a2015-04-08 01:41:01 -0500153 if (net_dns_server.s_addr) {
154 ip_to_string(net_dns_server, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600155 env_set("dnsip", tmp);
wdenk6e592382004-04-18 17:39:38 +0000156 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500157#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500158 if (net_dns_server2.s_addr) {
159 ip_to_string(net_dns_server2, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600160 env_set("dnsip2", tmp);
wdenk6e592382004-04-18 17:39:38 +0000161 }
stroesefe389a82003-08-28 14:17:32 +0000162#endif
Joe Hershberger586cbe52015-04-08 01:41:03 -0500163 if (net_nis_domain[0])
Simon Glass382bee52017-08-03 12:22:09 -0600164 env_set("domain", net_nis_domain);
wdenkea287de2005-04-01 00:25:43 +0000165
Joe Hershberger4fd50552015-04-08 01:41:17 -0500166#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500167 if (net_ntp_time_offset) {
168 sprintf(tmp, "%d", net_ntp_time_offset);
Simon Glass382bee52017-08-03 12:22:09 -0600169 env_set("timeoffset", tmp);
wdenkea287de2005-04-01 00:25:43 +0000170 }
171#endif
Joe Hershberger4fd50552015-04-08 01:41:17 -0500172#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500173 if (net_ntp_server.s_addr) {
174 ip_to_string(net_ntp_server, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600175 env_set("ntpserverip", tmp);
wdenkea287de2005-04-01 00:25:43 +0000176 }
177#endif
wdenk38635852002-08-27 05:55:31 +0000178}
wdenk6e592382004-04-18 17:39:38 +0000179
Simon Glasse4bf0c52011-10-24 18:00:02 +0000180static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
181 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000182{
183 char *s;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600184 char *end;
wdenk38635852002-08-27 05:55:31 +0000185 int rcode = 0;
186 int size;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600187 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000188
Alexander Graf449312c2018-06-15 10:29:27 +0200189 net_boot_file_name_explicit = false;
190
Simon Glassbb872dd2019-12-28 10:45:02 -0700191 /* pre-set image_load_addr */
Simon Glass00caae62017-08-03 12:22:12 -0600192 s = env_get("loadaddr");
Joe Hershberger4fd50552015-04-08 01:41:17 -0500193 if (s != NULL)
Simon Glassbb872dd2019-12-28 10:45:02 -0700194 image_load_addr = simple_strtoul(s, NULL, 16);
wdenk38635852002-08-27 05:55:31 +0000195
196 switch (argc) {
197 case 1:
Joe Hershbergerf43308f2018-07-03 19:36:42 -0500198 /* refresh bootfile name from env */
199 copy_filename(net_boot_file_name, env_get("bootfile"),
200 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000201 break;
202
Peter Tyser2e4970d2008-12-02 12:59:51 -0600203 case 2: /*
204 * Only one arg - accept two forms:
205 * Just load address, or just boot file name. The latter
206 * form must be written in a format which can not be
207 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000208 */
Peter Tyser2e4970d2008-12-02 12:59:51 -0600209 addr = simple_strtoul(argv[1], &end, 16);
Alexander Graf449312c2018-06-15 10:29:27 +0200210 if (end == (argv[1] + strlen(argv[1]))) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700211 image_load_addr = addr;
Joe Hershbergerf43308f2018-07-03 19:36:42 -0500212 /* refresh bootfile name from env */
213 copy_filename(net_boot_file_name, env_get("bootfile"),
214 sizeof(net_boot_file_name));
Alexander Graf449312c2018-06-15 10:29:27 +0200215 } else {
216 net_boot_file_name_explicit = true;
Joe Hershberger14111572015-04-08 01:41:02 -0500217 copy_filename(net_boot_file_name, argv[1],
218 sizeof(net_boot_file_name));
Alexander Graf449312c2018-06-15 10:29:27 +0200219 }
wdenk38635852002-08-27 05:55:31 +0000220 break;
221
Joe Hershberger4fd50552015-04-08 01:41:17 -0500222 case 3:
Simon Glassbb872dd2019-12-28 10:45:02 -0700223 image_load_addr = simple_strtoul(argv[1], NULL, 16);
Alexander Graf449312c2018-06-15 10:29:27 +0200224 net_boot_file_name_explicit = true;
Joe Hershberger14111572015-04-08 01:41:02 -0500225 copy_filename(net_boot_file_name, argv[2],
226 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000227
228 break;
229
Simon Glass2d46cf22011-10-24 18:00:08 +0000230#ifdef CONFIG_CMD_TFTPPUT
231 case 4:
Simon Glassbb872dd2019-12-28 10:45:02 -0700232 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
233 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
Simon Glass38bd80b2011-12-19 16:10:43 +0000234 printf("Invalid address/size\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500235 return CMD_RET_USAGE;
Simon Glass38bd80b2011-12-19 16:10:43 +0000236 }
Alexander Graf449312c2018-06-15 10:29:27 +0200237 net_boot_file_name_explicit = true;
Joe Hershberger14111572015-04-08 01:41:02 -0500238 copy_filename(net_boot_file_name, argv[3],
239 sizeof(net_boot_file_name));
Simon Glass2d46cf22011-10-24 18:00:08 +0000240 break;
241#endif
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200242 default:
Simon Glass770605e2012-02-13 13:51:18 +0000243 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glass4c12eeb2011-12-10 08:44:01 +0000244 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000245 }
Simon Glass770605e2012-02-13 13:51:18 +0000246 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000247
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500248 size = net_loop(proto);
Joe Hershberger4fd50552015-04-08 01:41:17 -0500249 if (size < 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000250 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
Joe Hershberger85848f02015-03-22 17:09:09 -0500251 return CMD_RET_FAILURE;
Heiko Schocher566a4942007-06-22 19:11:54 +0200252 }
Simon Glass770605e2012-02-13 13:51:18 +0000253 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000254
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500255 /* net_loop ok, update environment */
wdenk38635852002-08-27 05:55:31 +0000256 netboot_update_env();
257
wdenkeb9401e2002-11-11 02:11:37 +0000258 /* done if no file was loaded (no errors though) */
Heiko Schocher566a4942007-06-22 19:11:54 +0200259 if (size == 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000260 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
Joe Hershberger85848f02015-03-22 17:09:09 -0500261 return CMD_RET_SUCCESS;
Heiko Schocher566a4942007-06-22 19:11:54 +0200262 }
wdenkeb9401e2002-11-11 02:11:37 +0000263
Simon Glass770605e2012-02-13 13:51:18 +0000264 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glassc8e66db2012-01-14 15:24:52 +0000265
Mike Frysinger67d668b2011-06-05 13:43:02 +0000266 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000267
Joe Hershberger85848f02015-03-22 17:09:09 -0500268 if (rcode == CMD_RET_SUCCESS)
Simon Glass770605e2012-02-13 13:51:18 +0000269 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
Joe Hershberger85848f02015-03-22 17:09:09 -0500270 else
271 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
wdenk38635852002-08-27 05:55:31 +0000272 return rcode;
273}
274
Jon Loeligerc76fe472007-07-08 18:02:23 -0500275#if defined(CONFIG_CMD_PING)
Kim Phillips088f1b12012-10-29 13:34:31 +0000276static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk73a8b272003-06-05 19:27:42 +0000277{
278 if (argc < 2)
Joe Hershberger85848f02015-03-22 17:09:09 -0500279 return CMD_RET_USAGE;
wdenk73a8b272003-06-05 19:27:42 +0000280
Joe Hershberger049a95a2015-04-08 01:41:01 -0500281 net_ping_ip = string_to_ip(argv[1]);
282 if (net_ping_ip.s_addr == 0)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000283 return CMD_RET_USAGE;
wdenk73a8b272003-06-05 19:27:42 +0000284
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500285 if (net_loop(PING) < 0) {
wdenk73a8b272003-06-05 19:27:42 +0000286 printf("ping failed; host %s is not alive\n", argv[1]);
Joe Hershberger85848f02015-03-22 17:09:09 -0500287 return CMD_RET_FAILURE;
wdenk73a8b272003-06-05 19:27:42 +0000288 }
289
290 printf("host %s is alive\n", argv[1]);
291
Joe Hershberger85848f02015-03-22 17:09:09 -0500292 return CMD_RET_SUCCESS;
wdenk73a8b272003-06-05 19:27:42 +0000293}
wdenk6dff5522003-07-15 07:45:49 +0000294
295U_BOOT_CMD(
296 ping, 2, 1, do_ping,
Peter Tyser2fb26042009-01-27 18:03:12 -0600297 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200298 "pingAddress"
wdenk6dff5522003-07-15 07:45:49 +0000299);
Jon Loeliger90253172007-07-10 11:02:44 -0500300#endif
wdenk73a8b272003-06-05 19:27:42 +0000301
Jon Loeligerc76fe472007-07-08 18:02:23 -0500302#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000303
304static void cdp_update_env(void)
305{
306 char tmp[16];
307
Joe Hershberger6aede5b2015-04-08 01:41:14 -0500308 if (cdp_appliance_vlan != htons(-1)) {
309 printf("CDP offered appliance VLAN %d\n",
310 ntohs(cdp_appliance_vlan));
Joe Hershberger4fd50552015-04-08 01:41:17 -0500311 vlan_to_string(cdp_appliance_vlan, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600312 env_set("vlan", tmp);
Joe Hershberger4fd50552015-04-08 01:41:17 -0500313 net_our_vlan = cdp_appliance_vlan;
wdenka3d991b2004-04-15 21:48:45 +0000314 }
315
Joe Hershberger6aede5b2015-04-08 01:41:14 -0500316 if (cdp_native_vlan != htons(-1)) {
317 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
Joe Hershberger4fd50552015-04-08 01:41:17 -0500318 vlan_to_string(cdp_native_vlan, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600319 env_set("nvlan", tmp);
Joe Hershberger4fd50552015-04-08 01:41:17 -0500320 net_native_vlan = cdp_native_vlan;
wdenka3d991b2004-04-15 21:48:45 +0000321 }
wdenka3d991b2004-04-15 21:48:45 +0000322}
323
Kim Phillips088f1b12012-10-29 13:34:31 +0000324int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka3d991b2004-04-15 21:48:45 +0000325{
326 int r;
327
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500328 r = net_loop(CDP);
wdenka3d991b2004-04-15 21:48:45 +0000329 if (r < 0) {
330 printf("cdp failed; perhaps not a CISCO switch?\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500331 return CMD_RET_FAILURE;
wdenka3d991b2004-04-15 21:48:45 +0000332 }
333
334 cdp_update_env();
335
Joe Hershberger85848f02015-03-22 17:09:09 -0500336 return CMD_RET_SUCCESS;
wdenka3d991b2004-04-15 21:48:45 +0000337}
338
339U_BOOT_CMD(
340 cdp, 1, 1, do_cdp,
Wolfgang Denkec5c04c2010-10-26 23:42:23 +0200341 "Perform CDP network configuration",
Wolfgang Denk4b582662010-12-23 17:02:18 +0100342 "\n"
wdenka3d991b2004-04-15 21:48:45 +0000343);
Jon Loeliger90253172007-07-10 11:02:44 -0500344#endif
wdenka3d991b2004-04-15 21:48:45 +0000345
Jon Loeligerc76fe472007-07-08 18:02:23 -0500346#if defined(CONFIG_CMD_SNTP)
Kim Phillips088f1b12012-10-29 13:34:31 +0000347int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkea287de2005-04-01 00:25:43 +0000348{
349 char *toff;
350
351 if (argc < 2) {
Simon Glass723806c2017-08-03 12:22:15 -0600352 net_ntp_server = env_get_ip("ntpserverip");
Joe Hershberger049a95a2015-04-08 01:41:01 -0500353 if (net_ntp_server.s_addr == 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000354 printf("ntpserverip not set\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500355 return CMD_RET_FAILURE;
wdenkea287de2005-04-01 00:25:43 +0000356 }
357 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -0500358 net_ntp_server = string_to_ip(argv[1]);
359 if (net_ntp_server.s_addr == 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000360 printf("Bad NTP server IP address\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500361 return CMD_RET_FAILURE;
wdenkea287de2005-04-01 00:25:43 +0000362 }
363 }
364
Simon Glass00caae62017-08-03 12:22:12 -0600365 toff = env_get("timeoffset");
Kim Phillips088f1b12012-10-29 13:34:31 +0000366 if (toff == NULL)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500367 net_ntp_time_offset = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000368 else
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500369 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
wdenkea287de2005-04-01 00:25:43 +0000370
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500371 if (net_loop(SNTP) < 0) {
Luuk Paulussend6840e32011-05-16 18:27:11 +0000372 printf("SNTP failed: host %pI4 not responding\n",
Joe Hershberger4fd50552015-04-08 01:41:17 -0500373 &net_ntp_server);
Joe Hershberger85848f02015-03-22 17:09:09 -0500374 return CMD_RET_FAILURE;
wdenkea287de2005-04-01 00:25:43 +0000375 }
376
Joe Hershberger85848f02015-03-22 17:09:09 -0500377 return CMD_RET_SUCCESS;
wdenkea287de2005-04-01 00:25:43 +0000378}
379
380U_BOOT_CMD(
381 sntp, 2, 1, do_sntp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600382 "synchronize RTC via network",
wdenkea287de2005-04-01 00:25:43 +0000383 "[NTP server IP]\n"
384);
Jon Loeliger90253172007-07-10 11:02:44 -0500385#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400386
387#if defined(CONFIG_CMD_DNS)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200388int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Robin Getz1a32bf42009-07-20 14:53:54 -0400389{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200390 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000391 return CMD_RET_USAGE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400392
393 /*
394 * We should check for a valid hostname:
395 * - Each label must be between 1 and 63 characters long
396 * - the entire hostname has a maximum of 255 characters
397 * - only the ASCII letters 'a' through 'z' (case-insensitive),
398 * the digits '0' through '9', and the hyphen
399 * - cannot begin or end with a hyphen
400 * - no other symbols, punctuation characters, or blank spaces are
401 * permitted
402 * but hey - this is a minimalist implmentation, so only check length
403 * and let the name server deal with things.
404 */
405 if (strlen(argv[1]) >= 255) {
406 printf("dns error: hostname too long\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500407 return CMD_RET_FAILURE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400408 }
409
Joe Hershberger786eac52015-04-08 01:41:15 -0500410 net_dns_resolve = argv[1];
Robin Getz1a32bf42009-07-20 14:53:54 -0400411
412 if (argc == 3)
Joe Hershberger786eac52015-04-08 01:41:15 -0500413 net_dns_env_var = argv[2];
Robin Getz1a32bf42009-07-20 14:53:54 -0400414 else
Joe Hershberger786eac52015-04-08 01:41:15 -0500415 net_dns_env_var = NULL;
Robin Getz1a32bf42009-07-20 14:53:54 -0400416
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500417 if (net_loop(DNS) < 0) {
Robin Getz1a32bf42009-07-20 14:53:54 -0400418 printf("dns lookup of %s failed, check setup\n", argv[1]);
Joe Hershberger85848f02015-03-22 17:09:09 -0500419 return CMD_RET_FAILURE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400420 }
421
Joe Hershberger85848f02015-03-22 17:09:09 -0500422 return CMD_RET_SUCCESS;
Robin Getz1a32bf42009-07-20 14:53:54 -0400423}
424
425U_BOOT_CMD(
426 dns, 3, 1, do_dns,
427 "lookup the IP of a hostname",
428 "hostname [envvar]"
429);
430
431#endif /* CONFIG_CMD_DNS */
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000432
433#if defined(CONFIG_CMD_LINK_LOCAL)
434static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
435 char * const argv[])
436{
437 char tmp[22];
438
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500439 if (net_loop(LINKLOCAL) < 0)
Joe Hershberger85848f02015-03-22 17:09:09 -0500440 return CMD_RET_FAILURE;
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000441
Joe Hershberger049a95a2015-04-08 01:41:01 -0500442 net_gateway.s_addr = 0;
443 ip_to_string(net_gateway, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600444 env_set("gatewayip", tmp);
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000445
Joe Hershberger049a95a2015-04-08 01:41:01 -0500446 ip_to_string(net_netmask, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600447 env_set("netmask", tmp);
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000448
Joe Hershberger049a95a2015-04-08 01:41:01 -0500449 ip_to_string(net_ip, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600450 env_set("ipaddr", tmp);
451 env_set("llipaddr", tmp); /* store this for next time */
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000452
Joe Hershberger85848f02015-03-22 17:09:09 -0500453 return CMD_RET_SUCCESS;
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000454}
455
456U_BOOT_CMD(
457 linklocal, 1, 1, do_link_local,
458 "acquire a network IP address using the link-local protocol",
459 ""
460);
461
462#endif /* CONFIG_CMD_LINK_LOCAL */