blob: 237403977e8006c651182d01534912aaeeca9676 [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>
11#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060012#include <env.h>
wdenk38635852002-08-27 05:55:31 +000013#include <net.h>
14
Simon Glasse4bf0c52011-10-24 18:00:02 +000015static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000016
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050017#ifdef CONFIG_CMD_BOOTP
Kim Phillips088f1b12012-10-29 13:34:31 +000018static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000019{
Kim Phillips088f1b12012-10-29 13:34:31 +000020 return netboot_common(BOOTP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000021}
22
wdenk0d498392003-07-01 21:06:45 +000023U_BOOT_CMD(
24 bootp, 3, 1, do_bootp,
Peter Tyser2fb26042009-01-27 18:03:12 -060025 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020026 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000027);
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050028#endif
wdenk8bde7f72003-06-27 21:31:46 +000029
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050030#ifdef CONFIG_CMD_TFTPBOOT
Kim Phillips088f1b12012-10-29 13:34:31 +000031int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000032{
Simon Glass573f14f2011-12-10 11:08:06 +000033 int ret;
34
35 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
36 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
37 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
38 return ret;
wdenk38635852002-08-27 05:55:31 +000039}
40
wdenk0d498392003-07-01 21:06:45 +000041U_BOOT_CMD(
42 tftpboot, 3, 1, do_tftpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060043 "boot image via network using TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020044 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000045);
Joe Hershbergerd7a45ea2018-04-13 15:26:30 -050046#endif
wdenk8bde7f72003-06-27 21:31:46 +000047
Simon Glass2d46cf22011-10-24 18:00:08 +000048#ifdef CONFIG_CMD_TFTPPUT
Masahiro Yamada0c1b8692017-06-22 16:49:10 +090049static int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Simon Glass2d46cf22011-10-24 18:00:08 +000050{
Joe Hershberger85848f02015-03-22 17:09:09 -050051 return netboot_common(TFTPPUT, cmdtp, argc, argv);
Simon Glass2d46cf22011-10-24 18:00:08 +000052}
53
54U_BOOT_CMD(
55 tftpput, 4, 1, do_tftpput,
56 "TFTP put command, for uploading files to a server",
57 "Address Size [[hostIPaddr:]filename]"
58);
59#endif
60
Luca Ceresoli7a83af02011-05-17 00:03:40 +000061#ifdef CONFIG_CMD_TFTPSRV
62static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
63{
64 return netboot_common(TFTPSRV, cmdtp, argc, argv);
65}
66
67U_BOOT_CMD(
68 tftpsrv, 2, 1, do_tftpsrv,
69 "act as a TFTP server and boot the first received file",
70 "[loadAddress]\n"
71 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
72 "The transfer is aborted if a transfer has not been started after\n"
73 "about 50 seconds or if Ctrl-C is pressed."
74);
75#endif
76
77
Peter Tyserbf6cb242010-09-30 11:25:48 -050078#ifdef CONFIG_CMD_RARP
Kim Phillips088f1b12012-10-29 13:34:31 +000079int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000080{
Kim Phillips088f1b12012-10-29 13:34:31 +000081 return netboot_common(RARP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000082}
83
wdenk0d498392003-07-01 21:06:45 +000084U_BOOT_CMD(
85 rarpboot, 3, 1, do_rarpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060086 "boot image via network using RARP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020087 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000088);
Peter Tyserbf6cb242010-09-30 11:25:48 -050089#endif
wdenk8bde7f72003-06-27 21:31:46 +000090
Jon Loeligerc76fe472007-07-08 18:02:23 -050091#if defined(CONFIG_CMD_DHCP)
Kim Phillips088f1b12012-10-29 13:34:31 +000092static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000093{
94 return netboot_common(DHCP, cmdtp, argc, argv);
95}
wdenk8bde7f72003-06-27 21:31:46 +000096
wdenk0d498392003-07-01 21:06:45 +000097U_BOOT_CMD(
98 dhcp, 3, 1, do_dhcp,
Peter Tyser2fb26042009-01-27 18:03:12 -060099 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200100 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +0000101);
Jon Loeliger90253172007-07-10 11:02:44 -0500102#endif
wdenk38635852002-08-27 05:55:31 +0000103
Jon Loeligerc76fe472007-07-08 18:02:23 -0500104#if defined(CONFIG_CMD_NFS)
Kim Phillips088f1b12012-10-29 13:34:31 +0000105static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkcbd8a352004-02-24 02:00:03 +0000106{
107 return netboot_common(NFS, cmdtp, argc, argv);
108}
109
110U_BOOT_CMD(
111 nfs, 3, 1, do_nfs,
Peter Tyser2fb26042009-01-27 18:03:12 -0600112 "boot image via network using NFS protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200113 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkcbd8a352004-02-24 02:00:03 +0000114);
Jon Loeliger90253172007-07-10 11:02:44 -0500115#endif
wdenkcbd8a352004-02-24 02:00:03 +0000116
Kim Phillips088f1b12012-10-29 13:34:31 +0000117static void netboot_update_env(void)
wdenk38635852002-08-27 05:55:31 +0000118{
wdenk6e592382004-04-18 17:39:38 +0000119 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000120
Joe Hershberger049a95a2015-04-08 01:41:01 -0500121 if (net_gateway.s_addr) {
122 ip_to_string(net_gateway, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600123 env_set("gatewayip", tmp);
wdenk6e592382004-04-18 17:39:38 +0000124 }
wdenk38635852002-08-27 05:55:31 +0000125
Joe Hershberger049a95a2015-04-08 01:41:01 -0500126 if (net_netmask.s_addr) {
127 ip_to_string(net_netmask, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600128 env_set("netmask", tmp);
wdenk6e592382004-04-18 17:39:38 +0000129 }
wdenk38635852002-08-27 05:55:31 +0000130
Joe Hershberger586cbe52015-04-08 01:41:03 -0500131 if (net_hostname[0])
Simon Glass382bee52017-08-03 12:22:09 -0600132 env_set("hostname", net_hostname);
wdenk38635852002-08-27 05:55:31 +0000133
Joe Hershberger586cbe52015-04-08 01:41:03 -0500134 if (net_root_path[0])
Simon Glass382bee52017-08-03 12:22:09 -0600135 env_set("rootpath", net_root_path);
wdenk38635852002-08-27 05:55:31 +0000136
Joe Hershberger049a95a2015-04-08 01:41:01 -0500137 if (net_ip.s_addr) {
138 ip_to_string(net_ip, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600139 env_set("ipaddr", tmp);
wdenk6e592382004-04-18 17:39:38 +0000140 }
Joe Hershbergera3e1a722012-05-23 07:59:17 +0000141#if !defined(CONFIG_BOOTP_SERVERIP)
142 /*
Baruch Siachff78ad22016-12-27 11:03:29 +0200143 * Only attempt to change serverip if net/bootp.c:store_net_params()
Joe Hershbergera3e1a722012-05-23 07:59:17 +0000144 * could have set it
145 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500146 if (net_server_ip.s_addr) {
147 ip_to_string(net_server_ip, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600148 env_set("serverip", tmp);
wdenk6e592382004-04-18 17:39:38 +0000149 }
Joe Hershbergera3e1a722012-05-23 07:59:17 +0000150#endif
Joe Hershberger049a95a2015-04-08 01:41:01 -0500151 if (net_dns_server.s_addr) {
152 ip_to_string(net_dns_server, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600153 env_set("dnsip", tmp);
wdenk6e592382004-04-18 17:39:38 +0000154 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500155#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500156 if (net_dns_server2.s_addr) {
157 ip_to_string(net_dns_server2, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600158 env_set("dnsip2", tmp);
wdenk6e592382004-04-18 17:39:38 +0000159 }
stroesefe389a82003-08-28 14:17:32 +0000160#endif
Joe Hershberger586cbe52015-04-08 01:41:03 -0500161 if (net_nis_domain[0])
Simon Glass382bee52017-08-03 12:22:09 -0600162 env_set("domain", net_nis_domain);
wdenkea287de2005-04-01 00:25:43 +0000163
Joe Hershberger4fd50552015-04-08 01:41:17 -0500164#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500165 if (net_ntp_time_offset) {
166 sprintf(tmp, "%d", net_ntp_time_offset);
Simon Glass382bee52017-08-03 12:22:09 -0600167 env_set("timeoffset", tmp);
wdenkea287de2005-04-01 00:25:43 +0000168 }
169#endif
Joe Hershberger4fd50552015-04-08 01:41:17 -0500170#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger049a95a2015-04-08 01:41:01 -0500171 if (net_ntp_server.s_addr) {
172 ip_to_string(net_ntp_server, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600173 env_set("ntpserverip", tmp);
wdenkea287de2005-04-01 00:25:43 +0000174 }
175#endif
wdenk38635852002-08-27 05:55:31 +0000176}
wdenk6e592382004-04-18 17:39:38 +0000177
Simon Glasse4bf0c52011-10-24 18:00:02 +0000178static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
179 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000180{
181 char *s;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600182 char *end;
wdenk38635852002-08-27 05:55:31 +0000183 int rcode = 0;
184 int size;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600185 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000186
Alexander Graf449312c2018-06-15 10:29:27 +0200187 net_boot_file_name_explicit = false;
188
wdenk38635852002-08-27 05:55:31 +0000189 /* pre-set load_addr */
Simon Glass00caae62017-08-03 12:22:12 -0600190 s = env_get("loadaddr");
Joe Hershberger4fd50552015-04-08 01:41:17 -0500191 if (s != NULL)
wdenk38635852002-08-27 05:55:31 +0000192 load_addr = simple_strtoul(s, NULL, 16);
wdenk38635852002-08-27 05:55:31 +0000193
194 switch (argc) {
195 case 1:
Joe Hershbergerf43308f2018-07-03 19:36:42 -0500196 /* refresh bootfile name from env */
197 copy_filename(net_boot_file_name, env_get("bootfile"),
198 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000199 break;
200
Peter Tyser2e4970d2008-12-02 12:59:51 -0600201 case 2: /*
202 * Only one arg - accept two forms:
203 * Just load address, or just boot file name. The latter
204 * form must be written in a format which can not be
205 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000206 */
Peter Tyser2e4970d2008-12-02 12:59:51 -0600207 addr = simple_strtoul(argv[1], &end, 16);
Alexander Graf449312c2018-06-15 10:29:27 +0200208 if (end == (argv[1] + strlen(argv[1]))) {
Peter Tyser2e4970d2008-12-02 12:59:51 -0600209 load_addr = addr;
Joe Hershbergerf43308f2018-07-03 19:36:42 -0500210 /* refresh bootfile name from env */
211 copy_filename(net_boot_file_name, env_get("bootfile"),
212 sizeof(net_boot_file_name));
Alexander Graf449312c2018-06-15 10:29:27 +0200213 } else {
214 net_boot_file_name_explicit = true;
Joe Hershberger14111572015-04-08 01:41:02 -0500215 copy_filename(net_boot_file_name, argv[1],
216 sizeof(net_boot_file_name));
Alexander Graf449312c2018-06-15 10:29:27 +0200217 }
wdenk38635852002-08-27 05:55:31 +0000218 break;
219
Joe Hershberger4fd50552015-04-08 01:41:17 -0500220 case 3:
221 load_addr = simple_strtoul(argv[1], NULL, 16);
Alexander Graf449312c2018-06-15 10:29:27 +0200222 net_boot_file_name_explicit = true;
Joe Hershberger14111572015-04-08 01:41:02 -0500223 copy_filename(net_boot_file_name, argv[2],
224 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000225
226 break;
227
Simon Glass2d46cf22011-10-24 18:00:08 +0000228#ifdef CONFIG_CMD_TFTPPUT
229 case 4:
Simon Glass38bd80b2011-12-19 16:10:43 +0000230 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
Joe Hershberger4fd50552015-04-08 01:41:17 -0500231 strict_strtoul(argv[2], 16, &save_size) < 0) {
Simon Glass38bd80b2011-12-19 16:10:43 +0000232 printf("Invalid address/size\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500233 return CMD_RET_USAGE;
Simon Glass38bd80b2011-12-19 16:10:43 +0000234 }
Alexander Graf449312c2018-06-15 10:29:27 +0200235 net_boot_file_name_explicit = true;
Joe Hershberger14111572015-04-08 01:41:02 -0500236 copy_filename(net_boot_file_name, argv[3],
237 sizeof(net_boot_file_name));
Simon Glass2d46cf22011-10-24 18:00:08 +0000238 break;
239#endif
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200240 default:
Simon Glass770605e2012-02-13 13:51:18 +0000241 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glass4c12eeb2011-12-10 08:44:01 +0000242 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000243 }
Simon Glass770605e2012-02-13 13:51:18 +0000244 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000245
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500246 size = net_loop(proto);
Joe Hershberger4fd50552015-04-08 01:41:17 -0500247 if (size < 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000248 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
Joe Hershberger85848f02015-03-22 17:09:09 -0500249 return CMD_RET_FAILURE;
Heiko Schocher566a4942007-06-22 19:11:54 +0200250 }
Simon Glass770605e2012-02-13 13:51:18 +0000251 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000252
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500253 /* net_loop ok, update environment */
wdenk38635852002-08-27 05:55:31 +0000254 netboot_update_env();
255
wdenkeb9401e2002-11-11 02:11:37 +0000256 /* done if no file was loaded (no errors though) */
Heiko Schocher566a4942007-06-22 19:11:54 +0200257 if (size == 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000258 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
Joe Hershberger85848f02015-03-22 17:09:09 -0500259 return CMD_RET_SUCCESS;
Heiko Schocher566a4942007-06-22 19:11:54 +0200260 }
wdenkeb9401e2002-11-11 02:11:37 +0000261
Simon Glass770605e2012-02-13 13:51:18 +0000262 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glassc8e66db2012-01-14 15:24:52 +0000263
Mike Frysinger67d668b2011-06-05 13:43:02 +0000264 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000265
Joe Hershberger85848f02015-03-22 17:09:09 -0500266 if (rcode == CMD_RET_SUCCESS)
Simon Glass770605e2012-02-13 13:51:18 +0000267 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
Joe Hershberger85848f02015-03-22 17:09:09 -0500268 else
269 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
wdenk38635852002-08-27 05:55:31 +0000270 return rcode;
271}
272
Jon Loeligerc76fe472007-07-08 18:02:23 -0500273#if defined(CONFIG_CMD_PING)
Kim Phillips088f1b12012-10-29 13:34:31 +0000274static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk73a8b272003-06-05 19:27:42 +0000275{
276 if (argc < 2)
Joe Hershberger85848f02015-03-22 17:09:09 -0500277 return CMD_RET_USAGE;
wdenk73a8b272003-06-05 19:27:42 +0000278
Joe Hershberger049a95a2015-04-08 01:41:01 -0500279 net_ping_ip = string_to_ip(argv[1]);
280 if (net_ping_ip.s_addr == 0)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000281 return CMD_RET_USAGE;
wdenk73a8b272003-06-05 19:27:42 +0000282
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500283 if (net_loop(PING) < 0) {
wdenk73a8b272003-06-05 19:27:42 +0000284 printf("ping failed; host %s is not alive\n", argv[1]);
Joe Hershberger85848f02015-03-22 17:09:09 -0500285 return CMD_RET_FAILURE;
wdenk73a8b272003-06-05 19:27:42 +0000286 }
287
288 printf("host %s is alive\n", argv[1]);
289
Joe Hershberger85848f02015-03-22 17:09:09 -0500290 return CMD_RET_SUCCESS;
wdenk73a8b272003-06-05 19:27:42 +0000291}
wdenk6dff5522003-07-15 07:45:49 +0000292
293U_BOOT_CMD(
294 ping, 2, 1, do_ping,
Peter Tyser2fb26042009-01-27 18:03:12 -0600295 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200296 "pingAddress"
wdenk6dff5522003-07-15 07:45:49 +0000297);
Jon Loeliger90253172007-07-10 11:02:44 -0500298#endif
wdenk73a8b272003-06-05 19:27:42 +0000299
Jon Loeligerc76fe472007-07-08 18:02:23 -0500300#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000301
302static void cdp_update_env(void)
303{
304 char tmp[16];
305
Joe Hershberger6aede5b2015-04-08 01:41:14 -0500306 if (cdp_appliance_vlan != htons(-1)) {
307 printf("CDP offered appliance VLAN %d\n",
308 ntohs(cdp_appliance_vlan));
Joe Hershberger4fd50552015-04-08 01:41:17 -0500309 vlan_to_string(cdp_appliance_vlan, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600310 env_set("vlan", tmp);
Joe Hershberger4fd50552015-04-08 01:41:17 -0500311 net_our_vlan = cdp_appliance_vlan;
wdenka3d991b2004-04-15 21:48:45 +0000312 }
313
Joe Hershberger6aede5b2015-04-08 01:41:14 -0500314 if (cdp_native_vlan != htons(-1)) {
315 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
Joe Hershberger4fd50552015-04-08 01:41:17 -0500316 vlan_to_string(cdp_native_vlan, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600317 env_set("nvlan", tmp);
Joe Hershberger4fd50552015-04-08 01:41:17 -0500318 net_native_vlan = cdp_native_vlan;
wdenka3d991b2004-04-15 21:48:45 +0000319 }
wdenka3d991b2004-04-15 21:48:45 +0000320}
321
Kim Phillips088f1b12012-10-29 13:34:31 +0000322int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka3d991b2004-04-15 21:48:45 +0000323{
324 int r;
325
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500326 r = net_loop(CDP);
wdenka3d991b2004-04-15 21:48:45 +0000327 if (r < 0) {
328 printf("cdp failed; perhaps not a CISCO switch?\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500329 return CMD_RET_FAILURE;
wdenka3d991b2004-04-15 21:48:45 +0000330 }
331
332 cdp_update_env();
333
Joe Hershberger85848f02015-03-22 17:09:09 -0500334 return CMD_RET_SUCCESS;
wdenka3d991b2004-04-15 21:48:45 +0000335}
336
337U_BOOT_CMD(
338 cdp, 1, 1, do_cdp,
Wolfgang Denkec5c04c2010-10-26 23:42:23 +0200339 "Perform CDP network configuration",
Wolfgang Denk4b582662010-12-23 17:02:18 +0100340 "\n"
wdenka3d991b2004-04-15 21:48:45 +0000341);
Jon Loeliger90253172007-07-10 11:02:44 -0500342#endif
wdenka3d991b2004-04-15 21:48:45 +0000343
Jon Loeligerc76fe472007-07-08 18:02:23 -0500344#if defined(CONFIG_CMD_SNTP)
Kim Phillips088f1b12012-10-29 13:34:31 +0000345int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkea287de2005-04-01 00:25:43 +0000346{
347 char *toff;
348
349 if (argc < 2) {
Simon Glass723806c2017-08-03 12:22:15 -0600350 net_ntp_server = env_get_ip("ntpserverip");
Joe Hershberger049a95a2015-04-08 01:41:01 -0500351 if (net_ntp_server.s_addr == 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000352 printf("ntpserverip not set\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500353 return CMD_RET_FAILURE;
wdenkea287de2005-04-01 00:25:43 +0000354 }
355 } else {
Joe Hershberger049a95a2015-04-08 01:41:01 -0500356 net_ntp_server = string_to_ip(argv[1]);
357 if (net_ntp_server.s_addr == 0) {
Kim Phillips088f1b12012-10-29 13:34:31 +0000358 printf("Bad NTP server IP address\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500359 return CMD_RET_FAILURE;
wdenkea287de2005-04-01 00:25:43 +0000360 }
361 }
362
Simon Glass00caae62017-08-03 12:22:12 -0600363 toff = env_get("timeoffset");
Kim Phillips088f1b12012-10-29 13:34:31 +0000364 if (toff == NULL)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500365 net_ntp_time_offset = 0;
Kim Phillips088f1b12012-10-29 13:34:31 +0000366 else
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500367 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
wdenkea287de2005-04-01 00:25:43 +0000368
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500369 if (net_loop(SNTP) < 0) {
Luuk Paulussend6840e32011-05-16 18:27:11 +0000370 printf("SNTP failed: host %pI4 not responding\n",
Joe Hershberger4fd50552015-04-08 01:41:17 -0500371 &net_ntp_server);
Joe Hershberger85848f02015-03-22 17:09:09 -0500372 return CMD_RET_FAILURE;
wdenkea287de2005-04-01 00:25:43 +0000373 }
374
Joe Hershberger85848f02015-03-22 17:09:09 -0500375 return CMD_RET_SUCCESS;
wdenkea287de2005-04-01 00:25:43 +0000376}
377
378U_BOOT_CMD(
379 sntp, 2, 1, do_sntp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600380 "synchronize RTC via network",
wdenkea287de2005-04-01 00:25:43 +0000381 "[NTP server IP]\n"
382);
Jon Loeliger90253172007-07-10 11:02:44 -0500383#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400384
385#if defined(CONFIG_CMD_DNS)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200386int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Robin Getz1a32bf42009-07-20 14:53:54 -0400387{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200388 if (argc == 1)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000389 return CMD_RET_USAGE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400390
391 /*
392 * We should check for a valid hostname:
393 * - Each label must be between 1 and 63 characters long
394 * - the entire hostname has a maximum of 255 characters
395 * - only the ASCII letters 'a' through 'z' (case-insensitive),
396 * the digits '0' through '9', and the hyphen
397 * - cannot begin or end with a hyphen
398 * - no other symbols, punctuation characters, or blank spaces are
399 * permitted
400 * but hey - this is a minimalist implmentation, so only check length
401 * and let the name server deal with things.
402 */
403 if (strlen(argv[1]) >= 255) {
404 printf("dns error: hostname too long\n");
Joe Hershberger85848f02015-03-22 17:09:09 -0500405 return CMD_RET_FAILURE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400406 }
407
Joe Hershberger786eac52015-04-08 01:41:15 -0500408 net_dns_resolve = argv[1];
Robin Getz1a32bf42009-07-20 14:53:54 -0400409
410 if (argc == 3)
Joe Hershberger786eac52015-04-08 01:41:15 -0500411 net_dns_env_var = argv[2];
Robin Getz1a32bf42009-07-20 14:53:54 -0400412 else
Joe Hershberger786eac52015-04-08 01:41:15 -0500413 net_dns_env_var = NULL;
Robin Getz1a32bf42009-07-20 14:53:54 -0400414
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500415 if (net_loop(DNS) < 0) {
Robin Getz1a32bf42009-07-20 14:53:54 -0400416 printf("dns lookup of %s failed, check setup\n", argv[1]);
Joe Hershberger85848f02015-03-22 17:09:09 -0500417 return CMD_RET_FAILURE;
Robin Getz1a32bf42009-07-20 14:53:54 -0400418 }
419
Joe Hershberger85848f02015-03-22 17:09:09 -0500420 return CMD_RET_SUCCESS;
Robin Getz1a32bf42009-07-20 14:53:54 -0400421}
422
423U_BOOT_CMD(
424 dns, 3, 1, do_dns,
425 "lookup the IP of a hostname",
426 "hostname [envvar]"
427);
428
429#endif /* CONFIG_CMD_DNS */
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000430
431#if defined(CONFIG_CMD_LINK_LOCAL)
432static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
433 char * const argv[])
434{
435 char tmp[22];
436
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500437 if (net_loop(LINKLOCAL) < 0)
Joe Hershberger85848f02015-03-22 17:09:09 -0500438 return CMD_RET_FAILURE;
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000439
Joe Hershberger049a95a2015-04-08 01:41:01 -0500440 net_gateway.s_addr = 0;
441 ip_to_string(net_gateway, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600442 env_set("gatewayip", tmp);
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000443
Joe Hershberger049a95a2015-04-08 01:41:01 -0500444 ip_to_string(net_netmask, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600445 env_set("netmask", tmp);
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000446
Joe Hershberger049a95a2015-04-08 01:41:01 -0500447 ip_to_string(net_ip, tmp);
Simon Glass382bee52017-08-03 12:22:09 -0600448 env_set("ipaddr", tmp);
449 env_set("llipaddr", tmp); /* store this for next time */
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000450
Joe Hershberger85848f02015-03-22 17:09:09 -0500451 return CMD_RET_SUCCESS;
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000452}
453
454U_BOOT_CMD(
455 linklocal, 1, 1, do_link_local,
456 "acquire a network IP address using the link-local protocol",
457 ""
458);
459
460#endif /* CONFIG_CMD_LINK_LOCAL */