blob: 67888d4e184add6a53531ff17ff758cfad41869b [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00006 */
7
8/*
9 * Boot support
10 */
11#include <common.h>
12#include <command.h>
wdenk38635852002-08-27 05:55:31 +000013#include <net.h>
14
Simon Glassd6c5f552011-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 Hershbergerb0429462018-04-13 15:26:30 -050017#ifdef CONFIG_CMD_BOOTP
Kim Phillipsdc00a682012-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 Phillipsdc00a682012-10-29 13:34:31 +000020 return netboot_common(BOOTP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000021}
22
wdenkf287a242003-07-01 21:06:45 +000023U_BOOT_CMD(
24 bootp, 3, 1, do_bootp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060025 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020026 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000027);
Joe Hershbergerb0429462018-04-13 15:26:30 -050028#endif
wdenk57b2d802003-06-27 21:31:46 +000029
Joe Hershbergerb0429462018-04-13 15:26:30 -050030#ifdef CONFIG_CMD_TFTPBOOT
Kim Phillipsdc00a682012-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 Glass768cbf02011-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
wdenkf287a242003-07-01 21:06:45 +000041U_BOOT_CMD(
42 tftpboot, 3, 1, do_tftpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060043 "boot image via network using TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020044 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000045);
Joe Hershbergerb0429462018-04-13 15:26:30 -050046#endif
wdenk57b2d802003-06-27 21:31:46 +000047
Simon Glass85d96ec2011-10-24 18:00:08 +000048#ifdef CONFIG_CMD_TFTPPUT
Masahiro Yamada3dcd7142017-06-22 16:49:10 +090049static int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Simon Glass85d96ec2011-10-24 18:00:08 +000050{
Joe Hershbergerba867832015-03-22 17:09:09 -050051 return netboot_common(TFTPPUT, cmdtp, argc, argv);
Simon Glass85d96ec2011-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 Ceresoli7aa81a42011-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 Tyserf7a48ca2010-09-30 11:25:48 -050078#ifdef CONFIG_CMD_RARP
Kim Phillipsdc00a682012-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 Phillipsdc00a682012-10-29 13:34:31 +000081 return netboot_common(RARP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000082}
83
wdenkf287a242003-07-01 21:06:45 +000084U_BOOT_CMD(
85 rarpboot, 3, 1, do_rarpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060086 "boot image via network using RARP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020087 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000088);
Peter Tyserf7a48ca2010-09-30 11:25:48 -050089#endif
wdenk57b2d802003-06-27 21:31:46 +000090
Jon Loeligerd76b5c12007-07-08 18:02:23 -050091#if defined(CONFIG_CMD_DHCP)
Kim Phillipsdc00a682012-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}
wdenk57b2d802003-06-27 21:31:46 +000096
wdenkf287a242003-07-01 21:06:45 +000097U_BOOT_CMD(
98 dhcp, 3, 1, do_dhcp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060099 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200100 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +0000101);
Jon Loeligerd704d912007-07-10 11:02:44 -0500102#endif
wdenk38635852002-08-27 05:55:31 +0000103
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500104#if defined(CONFIG_CMD_NFS)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000105static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkbe9c1cb2004-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 Tyserdfb72b82009-01-27 18:03:12 -0600112 "boot image via network using NFS protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200113 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkbe9c1cb2004-02-24 02:00:03 +0000114);
Jon Loeligerd704d912007-07-10 11:02:44 -0500115#endif
wdenkbe9c1cb2004-02-24 02:00:03 +0000116
Kim Phillipsdc00a682012-10-29 13:34:31 +0000117static void netboot_update_env(void)
wdenk38635852002-08-27 05:55:31 +0000118{
wdenk05939202004-04-18 17:39:38 +0000119 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000120
Joe Hershberger5874dec2015-04-08 01:41:01 -0500121 if (net_gateway.s_addr) {
122 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600123 env_set("gatewayip", tmp);
wdenk05939202004-04-18 17:39:38 +0000124 }
wdenk38635852002-08-27 05:55:31 +0000125
Joe Hershberger5874dec2015-04-08 01:41:01 -0500126 if (net_netmask.s_addr) {
127 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600128 env_set("netmask", tmp);
wdenk05939202004-04-18 17:39:38 +0000129 }
wdenk38635852002-08-27 05:55:31 +0000130
Joe Hershberger6d236432015-04-08 01:41:03 -0500131 if (net_hostname[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600132 env_set("hostname", net_hostname);
wdenk38635852002-08-27 05:55:31 +0000133
Joe Hershberger6d236432015-04-08 01:41:03 -0500134 if (net_root_path[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600135 env_set("rootpath", net_root_path);
wdenk38635852002-08-27 05:55:31 +0000136
Joe Hershberger5874dec2015-04-08 01:41:01 -0500137 if (net_ip.s_addr) {
138 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600139 env_set("ipaddr", tmp);
wdenk05939202004-04-18 17:39:38 +0000140 }
Joe Hershbergerd4762042012-05-23 07:59:17 +0000141#if !defined(CONFIG_BOOTP_SERVERIP)
142 /*
Baruch Siachbc713252016-12-27 11:03:29 +0200143 * Only attempt to change serverip if net/bootp.c:store_net_params()
Joe Hershbergerd4762042012-05-23 07:59:17 +0000144 * could have set it
145 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500146 if (net_server_ip.s_addr) {
147 ip_to_string(net_server_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600148 env_set("serverip", tmp);
wdenk05939202004-04-18 17:39:38 +0000149 }
Joe Hershbergerd4762042012-05-23 07:59:17 +0000150#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500151 if (net_dns_server.s_addr) {
152 ip_to_string(net_dns_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600153 env_set("dnsip", tmp);
wdenk05939202004-04-18 17:39:38 +0000154 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500155#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500156 if (net_dns_server2.s_addr) {
157 ip_to_string(net_dns_server2, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600158 env_set("dnsip2", tmp);
wdenk05939202004-04-18 17:39:38 +0000159 }
stroesee0aadfb2003-08-28 14:17:32 +0000160#endif
Joe Hershberger6d236432015-04-08 01:41:03 -0500161 if (net_nis_domain[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600162 env_set("domain", net_nis_domain);
wdenkb4ad9622005-04-01 00:25:43 +0000163
Joe Hershberger013d3872015-04-08 01:41:17 -0500164#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500165 if (net_ntp_time_offset) {
166 sprintf(tmp, "%d", net_ntp_time_offset);
Simon Glass6a38e412017-08-03 12:22:09 -0600167 env_set("timeoffset", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000168 }
169#endif
Joe Hershberger013d3872015-04-08 01:41:17 -0500170#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500171 if (net_ntp_server.s_addr) {
172 ip_to_string(net_ntp_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600173 env_set("ntpserverip", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000174 }
175#endif
wdenk38635852002-08-27 05:55:31 +0000176}
wdenk05939202004-04-18 17:39:38 +0000177
Simon Glassd6c5f552011-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 Tyser24c297d2008-12-02 12:59:51 -0600182 char *end;
wdenk38635852002-08-27 05:55:31 +0000183 int rcode = 0;
184 int size;
Peter Tyser24c297d2008-12-02 12:59:51 -0600185 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000186
187 /* pre-set load_addr */
Simon Glass64b723f2017-08-03 12:22:12 -0600188 s = env_get("loadaddr");
Joe Hershberger013d3872015-04-08 01:41:17 -0500189 if (s != NULL)
wdenk38635852002-08-27 05:55:31 +0000190 load_addr = simple_strtoul(s, NULL, 16);
wdenk38635852002-08-27 05:55:31 +0000191
192 switch (argc) {
193 case 1:
194 break;
195
Peter Tyser24c297d2008-12-02 12:59:51 -0600196 case 2: /*
197 * Only one arg - accept two forms:
198 * Just load address, or just boot file name. The latter
199 * form must be written in a format which can not be
200 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000201 */
Peter Tyser24c297d2008-12-02 12:59:51 -0600202 addr = simple_strtoul(argv[1], &end, 16);
203 if (end == (argv[1] + strlen(argv[1])))
204 load_addr = addr;
205 else
Joe Hershberger290c8992015-04-08 01:41:02 -0500206 copy_filename(net_boot_file_name, argv[1],
207 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000208 break;
209
Joe Hershberger013d3872015-04-08 01:41:17 -0500210 case 3:
211 load_addr = simple_strtoul(argv[1], NULL, 16);
Joe Hershberger290c8992015-04-08 01:41:02 -0500212 copy_filename(net_boot_file_name, argv[2],
213 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000214
215 break;
216
Simon Glass85d96ec2011-10-24 18:00:08 +0000217#ifdef CONFIG_CMD_TFTPPUT
218 case 4:
Simon Glass778fe692011-12-19 16:10:43 +0000219 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
Joe Hershberger013d3872015-04-08 01:41:17 -0500220 strict_strtoul(argv[2], 16, &save_size) < 0) {
Simon Glass778fe692011-12-19 16:10:43 +0000221 printf("Invalid address/size\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500222 return CMD_RET_USAGE;
Simon Glass778fe692011-12-19 16:10:43 +0000223 }
Joe Hershberger290c8992015-04-08 01:41:02 -0500224 copy_filename(net_boot_file_name, argv[3],
225 sizeof(net_boot_file_name));
Simon Glass85d96ec2011-10-24 18:00:08 +0000226 break;
227#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200228 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000229 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glassa06dfc72011-12-10 08:44:01 +0000230 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000231 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000232 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000233
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500234 size = net_loop(proto);
Joe Hershberger013d3872015-04-08 01:41:17 -0500235 if (size < 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000236 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
Joe Hershbergerba867832015-03-22 17:09:09 -0500237 return CMD_RET_FAILURE;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200238 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000239 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000240
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500241 /* net_loop ok, update environment */
wdenk38635852002-08-27 05:55:31 +0000242 netboot_update_env();
243
wdenkc217f6d2002-11-11 02:11:37 +0000244 /* done if no file was loaded (no errors though) */
Heiko Schocher633e03a2007-06-22 19:11:54 +0200245 if (size == 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000246 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
Joe Hershbergerba867832015-03-22 17:09:09 -0500247 return CMD_RET_SUCCESS;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200248 }
wdenkc217f6d2002-11-11 02:11:37 +0000249
Simon Glass0169e6b2012-02-13 13:51:18 +0000250 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glass964d1d42012-01-14 15:24:52 +0000251
Mike Frysinger194c2e82011-06-05 13:43:02 +0000252 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000253
Joe Hershbergerba867832015-03-22 17:09:09 -0500254 if (rcode == CMD_RET_SUCCESS)
Simon Glass0169e6b2012-02-13 13:51:18 +0000255 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
Joe Hershbergerba867832015-03-22 17:09:09 -0500256 else
257 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
wdenk38635852002-08-27 05:55:31 +0000258 return rcode;
259}
wdenke6466f62003-06-05 19:27:42 +0000260
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500261#if defined(CONFIG_CMD_PING)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000262static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke6466f62003-06-05 19:27:42 +0000263{
264 if (argc < 2)
Joe Hershbergerba867832015-03-22 17:09:09 -0500265 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000266
Joe Hershberger5874dec2015-04-08 01:41:01 -0500267 net_ping_ip = string_to_ip(argv[1]);
268 if (net_ping_ip.s_addr == 0)
Simon Glassa06dfc72011-12-10 08:44:01 +0000269 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000270
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500271 if (net_loop(PING) < 0) {
wdenke6466f62003-06-05 19:27:42 +0000272 printf("ping failed; host %s is not alive\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500273 return CMD_RET_FAILURE;
wdenke6466f62003-06-05 19:27:42 +0000274 }
275
276 printf("host %s is alive\n", argv[1]);
277
Joe Hershbergerba867832015-03-22 17:09:09 -0500278 return CMD_RET_SUCCESS;
wdenke6466f62003-06-05 19:27:42 +0000279}
wdenkca9bc762003-07-15 07:45:49 +0000280
281U_BOOT_CMD(
282 ping, 2, 1, do_ping,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600283 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200284 "pingAddress"
wdenkca9bc762003-07-15 07:45:49 +0000285);
Jon Loeligerd704d912007-07-10 11:02:44 -0500286#endif
wdenk38635852002-08-27 05:55:31 +0000287
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500288#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000289
290static void cdp_update_env(void)
291{
292 char tmp[16];
293
Joe Hershberger527336f2015-04-08 01:41:14 -0500294 if (cdp_appliance_vlan != htons(-1)) {
295 printf("CDP offered appliance VLAN %d\n",
296 ntohs(cdp_appliance_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500297 vlan_to_string(cdp_appliance_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600298 env_set("vlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500299 net_our_vlan = cdp_appliance_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000300 }
301
Joe Hershberger527336f2015-04-08 01:41:14 -0500302 if (cdp_native_vlan != htons(-1)) {
303 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500304 vlan_to_string(cdp_native_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600305 env_set("nvlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500306 net_native_vlan = cdp_native_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000307 }
wdenk145d2c12004-04-15 21:48:45 +0000308}
309
Kim Phillipsdc00a682012-10-29 13:34:31 +0000310int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk145d2c12004-04-15 21:48:45 +0000311{
312 int r;
313
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500314 r = net_loop(CDP);
wdenk145d2c12004-04-15 21:48:45 +0000315 if (r < 0) {
316 printf("cdp failed; perhaps not a CISCO switch?\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500317 return CMD_RET_FAILURE;
wdenk145d2c12004-04-15 21:48:45 +0000318 }
319
320 cdp_update_env();
321
Joe Hershbergerba867832015-03-22 17:09:09 -0500322 return CMD_RET_SUCCESS;
wdenk145d2c12004-04-15 21:48:45 +0000323}
324
325U_BOOT_CMD(
326 cdp, 1, 1, do_cdp,
Wolfgang Denkd29afc32010-10-26 23:42:23 +0200327 "Perform CDP network configuration",
Wolfgang Denkf003d922010-12-23 17:02:18 +0100328 "\n"
wdenk145d2c12004-04-15 21:48:45 +0000329);
Jon Loeligerd704d912007-07-10 11:02:44 -0500330#endif
wdenk145d2c12004-04-15 21:48:45 +0000331
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500332#if defined(CONFIG_CMD_SNTP)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000333int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkb4ad9622005-04-01 00:25:43 +0000334{
335 char *toff;
336
337 if (argc < 2) {
Simon Glassda1a1342017-08-03 12:22:15 -0600338 net_ntp_server = env_get_ip("ntpserverip");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500339 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000340 printf("ntpserverip not set\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500341 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000342 }
343 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -0500344 net_ntp_server = string_to_ip(argv[1]);
345 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000346 printf("Bad NTP server IP address\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500347 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000348 }
349 }
350
Simon Glass64b723f2017-08-03 12:22:12 -0600351 toff = env_get("timeoffset");
Kim Phillipsdc00a682012-10-29 13:34:31 +0000352 if (toff == NULL)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500353 net_ntp_time_offset = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000354 else
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500355 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
wdenkb4ad9622005-04-01 00:25:43 +0000356
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500357 if (net_loop(SNTP) < 0) {
Luuk Paulussen7cb3f892011-05-16 18:27:11 +0000358 printf("SNTP failed: host %pI4 not responding\n",
Joe Hershberger013d3872015-04-08 01:41:17 -0500359 &net_ntp_server);
Joe Hershbergerba867832015-03-22 17:09:09 -0500360 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000361 }
362
Joe Hershbergerba867832015-03-22 17:09:09 -0500363 return CMD_RET_SUCCESS;
wdenkb4ad9622005-04-01 00:25:43 +0000364}
365
366U_BOOT_CMD(
367 sntp, 2, 1, do_sntp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600368 "synchronize RTC via network",
wdenkb4ad9622005-04-01 00:25:43 +0000369 "[NTP server IP]\n"
370);
Jon Loeligerd704d912007-07-10 11:02:44 -0500371#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400372
373#if defined(CONFIG_CMD_DNS)
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200374int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Robin Getz82f0d232009-07-20 14:53:54 -0400375{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200376 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000377 return CMD_RET_USAGE;
Robin Getz82f0d232009-07-20 14:53:54 -0400378
379 /*
380 * We should check for a valid hostname:
381 * - Each label must be between 1 and 63 characters long
382 * - the entire hostname has a maximum of 255 characters
383 * - only the ASCII letters 'a' through 'z' (case-insensitive),
384 * the digits '0' through '9', and the hyphen
385 * - cannot begin or end with a hyphen
386 * - no other symbols, punctuation characters, or blank spaces are
387 * permitted
388 * but hey - this is a minimalist implmentation, so only check length
389 * and let the name server deal with things.
390 */
391 if (strlen(argv[1]) >= 255) {
392 printf("dns error: hostname too long\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500393 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400394 }
395
Joe Hershbergerf725e342015-04-08 01:41:15 -0500396 net_dns_resolve = argv[1];
Robin Getz82f0d232009-07-20 14:53:54 -0400397
398 if (argc == 3)
Joe Hershbergerf725e342015-04-08 01:41:15 -0500399 net_dns_env_var = argv[2];
Robin Getz82f0d232009-07-20 14:53:54 -0400400 else
Joe Hershbergerf725e342015-04-08 01:41:15 -0500401 net_dns_env_var = NULL;
Robin Getz82f0d232009-07-20 14:53:54 -0400402
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500403 if (net_loop(DNS) < 0) {
Robin Getz82f0d232009-07-20 14:53:54 -0400404 printf("dns lookup of %s failed, check setup\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500405 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400406 }
407
Joe Hershbergerba867832015-03-22 17:09:09 -0500408 return CMD_RET_SUCCESS;
Robin Getz82f0d232009-07-20 14:53:54 -0400409}
410
411U_BOOT_CMD(
412 dns, 3, 1, do_dns,
413 "lookup the IP of a hostname",
414 "hostname [envvar]"
415);
416
417#endif /* CONFIG_CMD_DNS */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000418
419#if defined(CONFIG_CMD_LINK_LOCAL)
420static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
421 char * const argv[])
422{
423 char tmp[22];
424
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500425 if (net_loop(LINKLOCAL) < 0)
Joe Hershbergerba867832015-03-22 17:09:09 -0500426 return CMD_RET_FAILURE;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000427
Joe Hershberger5874dec2015-04-08 01:41:01 -0500428 net_gateway.s_addr = 0;
429 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600430 env_set("gatewayip", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000431
Joe Hershberger5874dec2015-04-08 01:41:01 -0500432 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600433 env_set("netmask", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000434
Joe Hershberger5874dec2015-04-08 01:41:01 -0500435 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600436 env_set("ipaddr", tmp);
437 env_set("llipaddr", tmp); /* store this for next time */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000438
Joe Hershbergerba867832015-03-22 17:09:09 -0500439 return CMD_RET_SUCCESS;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000440}
441
442U_BOOT_CMD(
443 linklocal, 1, 1, do_link_local,
444 "acquire a network IP address using the link-local protocol",
445 ""
446);
447
448#endif /* CONFIG_CMD_LINK_LOCAL */