blob: 237403977e8006c651182d01534912aaeeca9676 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38635852002-08-27 05:55:31 +00005 */
6
7/*
8 * Boot support
9 */
10#include <common.h>
11#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060012#include <env.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
Alexander Graff43bf5d2018-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 Glass64b723f2017-08-03 12:22:12 -0600190 s = env_get("loadaddr");
Joe Hershberger013d3872015-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 Hershberger2e135272018-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 Tyser24c297d2008-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 Tyser24c297d2008-12-02 12:59:51 -0600207 addr = simple_strtoul(argv[1], &end, 16);
Alexander Graff43bf5d2018-06-15 10:29:27 +0200208 if (end == (argv[1] + strlen(argv[1]))) {
Peter Tyser24c297d2008-12-02 12:59:51 -0600209 load_addr = addr;
Joe Hershberger2e135272018-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 Graff43bf5d2018-06-15 10:29:27 +0200213 } else {
214 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500215 copy_filename(net_boot_file_name, argv[1],
216 sizeof(net_boot_file_name));
Alexander Graff43bf5d2018-06-15 10:29:27 +0200217 }
wdenk38635852002-08-27 05:55:31 +0000218 break;
219
Joe Hershberger013d3872015-04-08 01:41:17 -0500220 case 3:
221 load_addr = simple_strtoul(argv[1], NULL, 16);
Alexander Graff43bf5d2018-06-15 10:29:27 +0200222 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-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 Glass85d96ec2011-10-24 18:00:08 +0000228#ifdef CONFIG_CMD_TFTPPUT
229 case 4:
Simon Glass778fe692011-12-19 16:10:43 +0000230 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
Joe Hershberger013d3872015-04-08 01:41:17 -0500231 strict_strtoul(argv[2], 16, &save_size) < 0) {
Simon Glass778fe692011-12-19 16:10:43 +0000232 printf("Invalid address/size\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500233 return CMD_RET_USAGE;
Simon Glass778fe692011-12-19 16:10:43 +0000234 }
Alexander Graff43bf5d2018-06-15 10:29:27 +0200235 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500236 copy_filename(net_boot_file_name, argv[3],
237 sizeof(net_boot_file_name));
Simon Glass85d96ec2011-10-24 18:00:08 +0000238 break;
239#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200240 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000241 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glassa06dfc72011-12-10 08:44:01 +0000242 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000243 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000244 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000245
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500246 size = net_loop(proto);
Joe Hershberger013d3872015-04-08 01:41:17 -0500247 if (size < 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000248 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
Joe Hershbergerba867832015-03-22 17:09:09 -0500249 return CMD_RET_FAILURE;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200250 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000251 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000252
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500253 /* net_loop ok, update environment */
wdenk38635852002-08-27 05:55:31 +0000254 netboot_update_env();
255
wdenkc217f6d2002-11-11 02:11:37 +0000256 /* done if no file was loaded (no errors though) */
Heiko Schocher633e03a2007-06-22 19:11:54 +0200257 if (size == 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000258 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
Joe Hershbergerba867832015-03-22 17:09:09 -0500259 return CMD_RET_SUCCESS;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200260 }
wdenkc217f6d2002-11-11 02:11:37 +0000261
Simon Glass0169e6b2012-02-13 13:51:18 +0000262 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glass964d1d42012-01-14 15:24:52 +0000263
Mike Frysinger194c2e82011-06-05 13:43:02 +0000264 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000265
Joe Hershbergerba867832015-03-22 17:09:09 -0500266 if (rcode == CMD_RET_SUCCESS)
Simon Glass0169e6b2012-02-13 13:51:18 +0000267 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
Joe Hershbergerba867832015-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}
wdenke6466f62003-06-05 19:27:42 +0000272
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500273#if defined(CONFIG_CMD_PING)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000274static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke6466f62003-06-05 19:27:42 +0000275{
276 if (argc < 2)
Joe Hershbergerba867832015-03-22 17:09:09 -0500277 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000278
Joe Hershberger5874dec2015-04-08 01:41:01 -0500279 net_ping_ip = string_to_ip(argv[1]);
280 if (net_ping_ip.s_addr == 0)
Simon Glassa06dfc72011-12-10 08:44:01 +0000281 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000282
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500283 if (net_loop(PING) < 0) {
wdenke6466f62003-06-05 19:27:42 +0000284 printf("ping failed; host %s is not alive\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500285 return CMD_RET_FAILURE;
wdenke6466f62003-06-05 19:27:42 +0000286 }
287
288 printf("host %s is alive\n", argv[1]);
289
Joe Hershbergerba867832015-03-22 17:09:09 -0500290 return CMD_RET_SUCCESS;
wdenke6466f62003-06-05 19:27:42 +0000291}
wdenkca9bc762003-07-15 07:45:49 +0000292
293U_BOOT_CMD(
294 ping, 2, 1, do_ping,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600295 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200296 "pingAddress"
wdenkca9bc762003-07-15 07:45:49 +0000297);
Jon Loeligerd704d912007-07-10 11:02:44 -0500298#endif
wdenk38635852002-08-27 05:55:31 +0000299
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500300#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000301
302static void cdp_update_env(void)
303{
304 char tmp[16];
305
Joe Hershberger527336f2015-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 Hershberger013d3872015-04-08 01:41:17 -0500309 vlan_to_string(cdp_appliance_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600310 env_set("vlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500311 net_our_vlan = cdp_appliance_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000312 }
313
Joe Hershberger527336f2015-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 Hershberger013d3872015-04-08 01:41:17 -0500316 vlan_to_string(cdp_native_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600317 env_set("nvlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500318 net_native_vlan = cdp_native_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000319 }
wdenk145d2c12004-04-15 21:48:45 +0000320}
321
Kim Phillipsdc00a682012-10-29 13:34:31 +0000322int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk145d2c12004-04-15 21:48:45 +0000323{
324 int r;
325
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500326 r = net_loop(CDP);
wdenk145d2c12004-04-15 21:48:45 +0000327 if (r < 0) {
328 printf("cdp failed; perhaps not a CISCO switch?\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500329 return CMD_RET_FAILURE;
wdenk145d2c12004-04-15 21:48:45 +0000330 }
331
332 cdp_update_env();
333
Joe Hershbergerba867832015-03-22 17:09:09 -0500334 return CMD_RET_SUCCESS;
wdenk145d2c12004-04-15 21:48:45 +0000335}
336
337U_BOOT_CMD(
338 cdp, 1, 1, do_cdp,
Wolfgang Denkd29afc32010-10-26 23:42:23 +0200339 "Perform CDP network configuration",
Wolfgang Denkf003d922010-12-23 17:02:18 +0100340 "\n"
wdenk145d2c12004-04-15 21:48:45 +0000341);
Jon Loeligerd704d912007-07-10 11:02:44 -0500342#endif
wdenk145d2c12004-04-15 21:48:45 +0000343
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500344#if defined(CONFIG_CMD_SNTP)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000345int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkb4ad9622005-04-01 00:25:43 +0000346{
347 char *toff;
348
349 if (argc < 2) {
Simon Glassda1a1342017-08-03 12:22:15 -0600350 net_ntp_server = env_get_ip("ntpserverip");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500351 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000352 printf("ntpserverip not set\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500353 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000354 }
355 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -0500356 net_ntp_server = string_to_ip(argv[1]);
357 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000358 printf("Bad NTP server IP address\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500359 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000360 }
361 }
362
Simon Glass64b723f2017-08-03 12:22:12 -0600363 toff = env_get("timeoffset");
Kim Phillipsdc00a682012-10-29 13:34:31 +0000364 if (toff == NULL)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500365 net_ntp_time_offset = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000366 else
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500367 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
wdenkb4ad9622005-04-01 00:25:43 +0000368
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500369 if (net_loop(SNTP) < 0) {
Luuk Paulussen7cb3f892011-05-16 18:27:11 +0000370 printf("SNTP failed: host %pI4 not responding\n",
Joe Hershberger013d3872015-04-08 01:41:17 -0500371 &net_ntp_server);
Joe Hershbergerba867832015-03-22 17:09:09 -0500372 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000373 }
374
Joe Hershbergerba867832015-03-22 17:09:09 -0500375 return CMD_RET_SUCCESS;
wdenkb4ad9622005-04-01 00:25:43 +0000376}
377
378U_BOOT_CMD(
379 sntp, 2, 1, do_sntp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600380 "synchronize RTC via network",
wdenkb4ad9622005-04-01 00:25:43 +0000381 "[NTP server IP]\n"
382);
Jon Loeligerd704d912007-07-10 11:02:44 -0500383#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400384
385#if defined(CONFIG_CMD_DNS)
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200386int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Robin Getz82f0d232009-07-20 14:53:54 -0400387{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200388 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000389 return CMD_RET_USAGE;
Robin Getz82f0d232009-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 Hershbergerba867832015-03-22 17:09:09 -0500405 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400406 }
407
Joe Hershbergerf725e342015-04-08 01:41:15 -0500408 net_dns_resolve = argv[1];
Robin Getz82f0d232009-07-20 14:53:54 -0400409
410 if (argc == 3)
Joe Hershbergerf725e342015-04-08 01:41:15 -0500411 net_dns_env_var = argv[2];
Robin Getz82f0d232009-07-20 14:53:54 -0400412 else
Joe Hershbergerf725e342015-04-08 01:41:15 -0500413 net_dns_env_var = NULL;
Robin Getz82f0d232009-07-20 14:53:54 -0400414
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500415 if (net_loop(DNS) < 0) {
Robin Getz82f0d232009-07-20 14:53:54 -0400416 printf("dns lookup of %s failed, check setup\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500417 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400418 }
419
Joe Hershbergerba867832015-03-22 17:09:09 -0500420 return CMD_RET_SUCCESS;
Robin Getz82f0d232009-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 Hershbergerb35a3a62012-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 Hershbergerc80b41b02015-04-08 01:41:21 -0500437 if (net_loop(LINKLOCAL) < 0)
Joe Hershbergerba867832015-03-22 17:09:09 -0500438 return CMD_RET_FAILURE;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000439
Joe Hershberger5874dec2015-04-08 01:41:01 -0500440 net_gateway.s_addr = 0;
441 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600442 env_set("gatewayip", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000443
Joe Hershberger5874dec2015-04-08 01:41:01 -0500444 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600445 env_set("netmask", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000446
Joe Hershberger5874dec2015-04-08 01:41:01 -0500447 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600448 env_set("ipaddr", tmp);
449 env_set("llipaddr", tmp); /* store this for next time */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000450
Joe Hershbergerba867832015-03-22 17:09:09 -0500451 return CMD_RET_SUCCESS;
Joe Hershbergerb35a3a62012-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 */