blob: beb2877dfd50152c26405ce16f5e262803ecbd30 [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>
Simon Glass1ea97892020-05-10 11:40:00 -060011#include <bootstage.h>
wdenk38635852002-08-27 05:55:31 +000012#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060013#include <env.h>
Simon Glass85f13782019-12-28 10:45:03 -070014#include <image.h>
wdenk38635852002-08-27 05:55:31 +000015#include <net.h>
Philippe Reynes2829d992020-09-18 14:13:02 +020016#include <net/udp.h>
17#include <net/sntp.h>
wdenk38635852002-08-27 05:55:31 +000018
Simon Glassed38aef2020-05-10 11:40:03 -060019static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000020
Joe Hershbergerb0429462018-04-13 15:26:30 -050021#ifdef CONFIG_CMD_BOOTP
Simon Glassed38aef2020-05-10 11:40:03 -060022static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
23 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000024{
Kim Phillipsdc00a682012-10-29 13:34:31 +000025 return netboot_common(BOOTP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000026}
27
wdenkf287a242003-07-01 21:06:45 +000028U_BOOT_CMD(
29 bootp, 3, 1, do_bootp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060030 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020031 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000032);
Joe Hershbergerb0429462018-04-13 15:26:30 -050033#endif
wdenk57b2d802003-06-27 21:31:46 +000034
Joe Hershbergerb0429462018-04-13 15:26:30 -050035#ifdef CONFIG_CMD_TFTPBOOT
Simon Glassed38aef2020-05-10 11:40:03 -060036int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk38635852002-08-27 05:55:31 +000037{
Simon Glass768cbf02011-12-10 11:08:06 +000038 int ret;
39
40 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
41 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
42 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
43 return ret;
wdenk38635852002-08-27 05:55:31 +000044}
45
wdenkf287a242003-07-01 21:06:45 +000046U_BOOT_CMD(
47 tftpboot, 3, 1, do_tftpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060048 "boot image via network using TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020049 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000050);
Joe Hershbergerb0429462018-04-13 15:26:30 -050051#endif
wdenk57b2d802003-06-27 21:31:46 +000052
Simon Glass85d96ec2011-10-24 18:00:08 +000053#ifdef CONFIG_CMD_TFTPPUT
Simon Glassed38aef2020-05-10 11:40:03 -060054static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
55 char *const argv[])
Simon Glass85d96ec2011-10-24 18:00:08 +000056{
Joe Hershbergerba867832015-03-22 17:09:09 -050057 return netboot_common(TFTPPUT, cmdtp, argc, argv);
Simon Glass85d96ec2011-10-24 18:00:08 +000058}
59
60U_BOOT_CMD(
61 tftpput, 4, 1, do_tftpput,
62 "TFTP put command, for uploading files to a server",
63 "Address Size [[hostIPaddr:]filename]"
64);
65#endif
66
Luca Ceresoli7aa81a42011-05-17 00:03:40 +000067#ifdef CONFIG_CMD_TFTPSRV
Simon Glassed38aef2020-05-10 11:40:03 -060068static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
69 char *const argv[])
Luca Ceresoli7aa81a42011-05-17 00:03:40 +000070{
71 return netboot_common(TFTPSRV, cmdtp, argc, argv);
72}
73
74U_BOOT_CMD(
75 tftpsrv, 2, 1, do_tftpsrv,
76 "act as a TFTP server and boot the first received file",
77 "[loadAddress]\n"
78 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
79 "The transfer is aborted if a transfer has not been started after\n"
80 "about 50 seconds or if Ctrl-C is pressed."
81);
82#endif
83
84
Peter Tyserf7a48ca2010-09-30 11:25:48 -050085#ifdef CONFIG_CMD_RARP
Simon Glassed38aef2020-05-10 11:40:03 -060086int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk38635852002-08-27 05:55:31 +000087{
Kim Phillipsdc00a682012-10-29 13:34:31 +000088 return netboot_common(RARP, cmdtp, argc, argv);
wdenk38635852002-08-27 05:55:31 +000089}
90
wdenkf287a242003-07-01 21:06:45 +000091U_BOOT_CMD(
92 rarpboot, 3, 1, do_rarpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060093 "boot image via network using RARP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020094 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000095);
Peter Tyserf7a48ca2010-09-30 11:25:48 -050096#endif
wdenk57b2d802003-06-27 21:31:46 +000097
Jon Loeligerd76b5c12007-07-08 18:02:23 -050098#if defined(CONFIG_CMD_DHCP)
Simon Glassed38aef2020-05-10 11:40:03 -060099static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
100 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000101{
102 return netboot_common(DHCP, cmdtp, argc, argv);
103}
wdenk57b2d802003-06-27 21:31:46 +0000104
wdenkf287a242003-07-01 21:06:45 +0000105U_BOOT_CMD(
106 dhcp, 3, 1, do_dhcp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600107 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200108 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +0000109);
Jon Loeligerd704d912007-07-10 11:02:44 -0500110#endif
wdenk38635852002-08-27 05:55:31 +0000111
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500112#if defined(CONFIG_CMD_NFS)
Simon Glassed38aef2020-05-10 11:40:03 -0600113static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
114 char *const argv[])
wdenkbe9c1cb2004-02-24 02:00:03 +0000115{
116 return netboot_common(NFS, cmdtp, argc, argv);
117}
118
119U_BOOT_CMD(
120 nfs, 3, 1, do_nfs,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600121 "boot image via network using NFS protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200122 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkbe9c1cb2004-02-24 02:00:03 +0000123);
Jon Loeligerd704d912007-07-10 11:02:44 -0500124#endif
wdenkbe9c1cb2004-02-24 02:00:03 +0000125
Kim Phillipsdc00a682012-10-29 13:34:31 +0000126static void netboot_update_env(void)
wdenk38635852002-08-27 05:55:31 +0000127{
wdenk05939202004-04-18 17:39:38 +0000128 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000129
Joe Hershberger5874dec2015-04-08 01:41:01 -0500130 if (net_gateway.s_addr) {
131 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600132 env_set("gatewayip", tmp);
wdenk05939202004-04-18 17:39:38 +0000133 }
wdenk38635852002-08-27 05:55:31 +0000134
Joe Hershberger5874dec2015-04-08 01:41:01 -0500135 if (net_netmask.s_addr) {
136 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600137 env_set("netmask", tmp);
wdenk05939202004-04-18 17:39:38 +0000138 }
wdenk38635852002-08-27 05:55:31 +0000139
Krebs, Olafd9249382020-03-09 14:27:55 +0000140#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500141 if (net_hostname[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600142 env_set("hostname", net_hostname);
Krebs, Olafd9249382020-03-09 14:27:55 +0000143#endif
wdenk38635852002-08-27 05:55:31 +0000144
Krebs, Olafd9249382020-03-09 14:27:55 +0000145#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500146 if (net_root_path[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600147 env_set("rootpath", net_root_path);
Krebs, Olafd9249382020-03-09 14:27:55 +0000148#endif
wdenk38635852002-08-27 05:55:31 +0000149
Joe Hershberger5874dec2015-04-08 01:41:01 -0500150 if (net_ip.s_addr) {
151 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600152 env_set("ipaddr", tmp);
wdenk05939202004-04-18 17:39:38 +0000153 }
Joe Hershbergerd4762042012-05-23 07:59:17 +0000154#if !defined(CONFIG_BOOTP_SERVERIP)
155 /*
Baruch Siachbc713252016-12-27 11:03:29 +0200156 * Only attempt to change serverip if net/bootp.c:store_net_params()
Joe Hershbergerd4762042012-05-23 07:59:17 +0000157 * could have set it
158 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500159 if (net_server_ip.s_addr) {
160 ip_to_string(net_server_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600161 env_set("serverip", tmp);
wdenk05939202004-04-18 17:39:38 +0000162 }
Joe Hershbergerd4762042012-05-23 07:59:17 +0000163#endif
Joe Hershberger5874dec2015-04-08 01:41:01 -0500164 if (net_dns_server.s_addr) {
165 ip_to_string(net_dns_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600166 env_set("dnsip", tmp);
wdenk05939202004-04-18 17:39:38 +0000167 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500168#if defined(CONFIG_BOOTP_DNS2)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500169 if (net_dns_server2.s_addr) {
170 ip_to_string(net_dns_server2, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600171 env_set("dnsip2", tmp);
wdenk05939202004-04-18 17:39:38 +0000172 }
stroesee0aadfb2003-08-28 14:17:32 +0000173#endif
Krebs, Olafd9249382020-03-09 14:27:55 +0000174#ifdef CONFIG_CMD_BOOTP
Joe Hershberger6d236432015-04-08 01:41:03 -0500175 if (net_nis_domain[0])
Simon Glass6a38e412017-08-03 12:22:09 -0600176 env_set("domain", net_nis_domain);
Krebs, Olafd9249382020-03-09 14:27:55 +0000177#endif
wdenkb4ad9622005-04-01 00:25:43 +0000178
Joe Hershberger013d3872015-04-08 01:41:17 -0500179#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500180 if (net_ntp_time_offset) {
181 sprintf(tmp, "%d", net_ntp_time_offset);
Simon Glass6a38e412017-08-03 12:22:09 -0600182 env_set("timeoffset", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000183 }
184#endif
Joe Hershberger013d3872015-04-08 01:41:17 -0500185#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
Joe Hershberger5874dec2015-04-08 01:41:01 -0500186 if (net_ntp_server.s_addr) {
187 ip_to_string(net_ntp_server, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600188 env_set("ntpserverip", tmp);
wdenkb4ad9622005-04-01 00:25:43 +0000189 }
190#endif
wdenk38635852002-08-27 05:55:31 +0000191}
wdenk05939202004-04-18 17:39:38 +0000192
Simon Glassed38aef2020-05-10 11:40:03 -0600193static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
194 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000195{
196 char *s;
Peter Tyser24c297d2008-12-02 12:59:51 -0600197 char *end;
wdenk38635852002-08-27 05:55:31 +0000198 int rcode = 0;
199 int size;
Peter Tyser24c297d2008-12-02 12:59:51 -0600200 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000201
Alexander Graff43bf5d2018-06-15 10:29:27 +0200202 net_boot_file_name_explicit = false;
203
Simon Glass892265d2019-12-28 10:45:02 -0700204 /* pre-set image_load_addr */
Simon Glass64b723f2017-08-03 12:22:12 -0600205 s = env_get("loadaddr");
Joe Hershberger013d3872015-04-08 01:41:17 -0500206 if (s != NULL)
Simon Glass892265d2019-12-28 10:45:02 -0700207 image_load_addr = simple_strtoul(s, NULL, 16);
wdenk38635852002-08-27 05:55:31 +0000208
209 switch (argc) {
210 case 1:
Joe Hershberger2e135272018-07-03 19:36:42 -0500211 /* refresh bootfile name from env */
212 copy_filename(net_boot_file_name, env_get("bootfile"),
213 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000214 break;
215
Peter Tyser24c297d2008-12-02 12:59:51 -0600216 case 2: /*
217 * Only one arg - accept two forms:
218 * Just load address, or just boot file name. The latter
219 * form must be written in a format which can not be
220 * mis-interpreted as a valid number.
wdenk38635852002-08-27 05:55:31 +0000221 */
Peter Tyser24c297d2008-12-02 12:59:51 -0600222 addr = simple_strtoul(argv[1], &end, 16);
Alexander Graff43bf5d2018-06-15 10:29:27 +0200223 if (end == (argv[1] + strlen(argv[1]))) {
Simon Glass892265d2019-12-28 10:45:02 -0700224 image_load_addr = addr;
Joe Hershberger2e135272018-07-03 19:36:42 -0500225 /* refresh bootfile name from env */
226 copy_filename(net_boot_file_name, env_get("bootfile"),
227 sizeof(net_boot_file_name));
Alexander Graff43bf5d2018-06-15 10:29:27 +0200228 } else {
229 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500230 copy_filename(net_boot_file_name, argv[1],
231 sizeof(net_boot_file_name));
Alexander Graff43bf5d2018-06-15 10:29:27 +0200232 }
wdenk38635852002-08-27 05:55:31 +0000233 break;
234
Joe Hershberger013d3872015-04-08 01:41:17 -0500235 case 3:
Simon Glass892265d2019-12-28 10:45:02 -0700236 image_load_addr = simple_strtoul(argv[1], NULL, 16);
Alexander Graff43bf5d2018-06-15 10:29:27 +0200237 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500238 copy_filename(net_boot_file_name, argv[2],
239 sizeof(net_boot_file_name));
wdenk38635852002-08-27 05:55:31 +0000240
241 break;
242
Simon Glass85d96ec2011-10-24 18:00:08 +0000243#ifdef CONFIG_CMD_TFTPPUT
244 case 4:
Simon Glass892265d2019-12-28 10:45:02 -0700245 if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
246 strict_strtoul(argv[2], 16, &image_save_size) < 0) {
Simon Glass778fe692011-12-19 16:10:43 +0000247 printf("Invalid address/size\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500248 return CMD_RET_USAGE;
Simon Glass778fe692011-12-19 16:10:43 +0000249 }
Alexander Graff43bf5d2018-06-15 10:29:27 +0200250 net_boot_file_name_explicit = true;
Joe Hershberger290c8992015-04-08 01:41:02 -0500251 copy_filename(net_boot_file_name, argv[3],
252 sizeof(net_boot_file_name));
Simon Glass85d96ec2011-10-24 18:00:08 +0000253 break;
254#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200255 default:
Simon Glass0169e6b2012-02-13 13:51:18 +0000256 bootstage_error(BOOTSTAGE_ID_NET_START);
Simon Glassa06dfc72011-12-10 08:44:01 +0000257 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000258 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000259 bootstage_mark(BOOTSTAGE_ID_NET_START);
wdenk38635852002-08-27 05:55:31 +0000260
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500261 size = net_loop(proto);
Joe Hershberger013d3872015-04-08 01:41:17 -0500262 if (size < 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000263 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
Joe Hershbergerba867832015-03-22 17:09:09 -0500264 return CMD_RET_FAILURE;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200265 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000266 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
wdenk38635852002-08-27 05:55:31 +0000267
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500268 /* net_loop ok, update environment */
wdenk38635852002-08-27 05:55:31 +0000269 netboot_update_env();
270
wdenkc217f6d2002-11-11 02:11:37 +0000271 /* done if no file was loaded (no errors though) */
Heiko Schocher633e03a2007-06-22 19:11:54 +0200272 if (size == 0) {
Simon Glass0169e6b2012-02-13 13:51:18 +0000273 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
Joe Hershbergerba867832015-03-22 17:09:09 -0500274 return CMD_RET_SUCCESS;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200275 }
wdenkc217f6d2002-11-11 02:11:37 +0000276
Simon Glass0169e6b2012-02-13 13:51:18 +0000277 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
Simon Glass964d1d42012-01-14 15:24:52 +0000278
Mike Frysinger194c2e82011-06-05 13:43:02 +0000279 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
wdenk38635852002-08-27 05:55:31 +0000280
Joe Hershbergerba867832015-03-22 17:09:09 -0500281 if (rcode == CMD_RET_SUCCESS)
Simon Glass0169e6b2012-02-13 13:51:18 +0000282 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
Joe Hershbergerba867832015-03-22 17:09:09 -0500283 else
284 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
wdenk38635852002-08-27 05:55:31 +0000285 return rcode;
286}
wdenke6466f62003-06-05 19:27:42 +0000287
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500288#if defined(CONFIG_CMD_PING)
Simon Glassed38aef2020-05-10 11:40:03 -0600289static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
290 char *const argv[])
wdenke6466f62003-06-05 19:27:42 +0000291{
292 if (argc < 2)
Joe Hershbergerba867832015-03-22 17:09:09 -0500293 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000294
Joe Hershberger5874dec2015-04-08 01:41:01 -0500295 net_ping_ip = string_to_ip(argv[1]);
296 if (net_ping_ip.s_addr == 0)
Simon Glassa06dfc72011-12-10 08:44:01 +0000297 return CMD_RET_USAGE;
wdenke6466f62003-06-05 19:27:42 +0000298
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500299 if (net_loop(PING) < 0) {
wdenke6466f62003-06-05 19:27:42 +0000300 printf("ping failed; host %s is not alive\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500301 return CMD_RET_FAILURE;
wdenke6466f62003-06-05 19:27:42 +0000302 }
303
304 printf("host %s is alive\n", argv[1]);
305
Joe Hershbergerba867832015-03-22 17:09:09 -0500306 return CMD_RET_SUCCESS;
wdenke6466f62003-06-05 19:27:42 +0000307}
wdenkca9bc762003-07-15 07:45:49 +0000308
309U_BOOT_CMD(
310 ping, 2, 1, do_ping,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600311 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200312 "pingAddress"
wdenkca9bc762003-07-15 07:45:49 +0000313);
Jon Loeligerd704d912007-07-10 11:02:44 -0500314#endif
wdenk38635852002-08-27 05:55:31 +0000315
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500316#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000317
318static void cdp_update_env(void)
319{
320 char tmp[16];
321
Joe Hershberger527336f2015-04-08 01:41:14 -0500322 if (cdp_appliance_vlan != htons(-1)) {
323 printf("CDP offered appliance VLAN %d\n",
324 ntohs(cdp_appliance_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500325 vlan_to_string(cdp_appliance_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600326 env_set("vlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500327 net_our_vlan = cdp_appliance_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000328 }
329
Joe Hershberger527336f2015-04-08 01:41:14 -0500330 if (cdp_native_vlan != htons(-1)) {
331 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
Joe Hershberger013d3872015-04-08 01:41:17 -0500332 vlan_to_string(cdp_native_vlan, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600333 env_set("nvlan", tmp);
Joe Hershberger013d3872015-04-08 01:41:17 -0500334 net_native_vlan = cdp_native_vlan;
wdenk145d2c12004-04-15 21:48:45 +0000335 }
wdenk145d2c12004-04-15 21:48:45 +0000336}
337
Simon Glassed38aef2020-05-10 11:40:03 -0600338int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk145d2c12004-04-15 21:48:45 +0000339{
340 int r;
341
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500342 r = net_loop(CDP);
wdenk145d2c12004-04-15 21:48:45 +0000343 if (r < 0) {
344 printf("cdp failed; perhaps not a CISCO switch?\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500345 return CMD_RET_FAILURE;
wdenk145d2c12004-04-15 21:48:45 +0000346 }
347
348 cdp_update_env();
349
Joe Hershbergerba867832015-03-22 17:09:09 -0500350 return CMD_RET_SUCCESS;
wdenk145d2c12004-04-15 21:48:45 +0000351}
352
353U_BOOT_CMD(
354 cdp, 1, 1, do_cdp,
Wolfgang Denkd29afc32010-10-26 23:42:23 +0200355 "Perform CDP network configuration",
Wolfgang Denkf003d922010-12-23 17:02:18 +0100356 "\n"
wdenk145d2c12004-04-15 21:48:45 +0000357);
Jon Loeligerd704d912007-07-10 11:02:44 -0500358#endif
wdenk145d2c12004-04-15 21:48:45 +0000359
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500360#if defined(CONFIG_CMD_SNTP)
Philippe Reynes2829d992020-09-18 14:13:02 +0200361static struct udp_ops sntp_ops = {
362 .prereq = sntp_prereq,
363 .start = sntp_start,
364 .data = NULL,
365};
366
Simon Glassed38aef2020-05-10 11:40:03 -0600367int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenkb4ad9622005-04-01 00:25:43 +0000368{
369 char *toff;
370
371 if (argc < 2) {
Simon Glassda1a1342017-08-03 12:22:15 -0600372 net_ntp_server = env_get_ip("ntpserverip");
Joe Hershberger5874dec2015-04-08 01:41:01 -0500373 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000374 printf("ntpserverip not set\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500375 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000376 }
377 } else {
Joe Hershberger5874dec2015-04-08 01:41:01 -0500378 net_ntp_server = string_to_ip(argv[1]);
379 if (net_ntp_server.s_addr == 0) {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000380 printf("Bad NTP server IP address\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500381 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000382 }
383 }
384
Simon Glass64b723f2017-08-03 12:22:12 -0600385 toff = env_get("timeoffset");
Kim Phillipsdc00a682012-10-29 13:34:31 +0000386 if (toff == NULL)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500387 net_ntp_time_offset = 0;
Kim Phillipsdc00a682012-10-29 13:34:31 +0000388 else
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500389 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
wdenkb4ad9622005-04-01 00:25:43 +0000390
Philippe Reynes2829d992020-09-18 14:13:02 +0200391 if (udp_loop(&sntp_ops) < 0) {
Luuk Paulussen7cb3f892011-05-16 18:27:11 +0000392 printf("SNTP failed: host %pI4 not responding\n",
Joe Hershberger013d3872015-04-08 01:41:17 -0500393 &net_ntp_server);
Joe Hershbergerba867832015-03-22 17:09:09 -0500394 return CMD_RET_FAILURE;
wdenkb4ad9622005-04-01 00:25:43 +0000395 }
396
Joe Hershbergerba867832015-03-22 17:09:09 -0500397 return CMD_RET_SUCCESS;
wdenkb4ad9622005-04-01 00:25:43 +0000398}
399
400U_BOOT_CMD(
401 sntp, 2, 1, do_sntp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600402 "synchronize RTC via network",
wdenkb4ad9622005-04-01 00:25:43 +0000403 "[NTP server IP]\n"
404);
Jon Loeligerd704d912007-07-10 11:02:44 -0500405#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400406
407#if defined(CONFIG_CMD_DNS)
Simon Glassed38aef2020-05-10 11:40:03 -0600408int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Robin Getz82f0d232009-07-20 14:53:54 -0400409{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200410 if (argc == 1)
Simon Glassa06dfc72011-12-10 08:44:01 +0000411 return CMD_RET_USAGE;
Robin Getz82f0d232009-07-20 14:53:54 -0400412
413 /*
414 * We should check for a valid hostname:
415 * - Each label must be between 1 and 63 characters long
416 * - the entire hostname has a maximum of 255 characters
417 * - only the ASCII letters 'a' through 'z' (case-insensitive),
418 * the digits '0' through '9', and the hyphen
419 * - cannot begin or end with a hyphen
420 * - no other symbols, punctuation characters, or blank spaces are
421 * permitted
422 * but hey - this is a minimalist implmentation, so only check length
423 * and let the name server deal with things.
424 */
425 if (strlen(argv[1]) >= 255) {
426 printf("dns error: hostname too long\n");
Joe Hershbergerba867832015-03-22 17:09:09 -0500427 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400428 }
429
Joe Hershbergerf725e342015-04-08 01:41:15 -0500430 net_dns_resolve = argv[1];
Robin Getz82f0d232009-07-20 14:53:54 -0400431
432 if (argc == 3)
Joe Hershbergerf725e342015-04-08 01:41:15 -0500433 net_dns_env_var = argv[2];
Robin Getz82f0d232009-07-20 14:53:54 -0400434 else
Joe Hershbergerf725e342015-04-08 01:41:15 -0500435 net_dns_env_var = NULL;
Robin Getz82f0d232009-07-20 14:53:54 -0400436
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500437 if (net_loop(DNS) < 0) {
Robin Getz82f0d232009-07-20 14:53:54 -0400438 printf("dns lookup of %s failed, check setup\n", argv[1]);
Joe Hershbergerba867832015-03-22 17:09:09 -0500439 return CMD_RET_FAILURE;
Robin Getz82f0d232009-07-20 14:53:54 -0400440 }
441
Joe Hershbergerba867832015-03-22 17:09:09 -0500442 return CMD_RET_SUCCESS;
Robin Getz82f0d232009-07-20 14:53:54 -0400443}
444
445U_BOOT_CMD(
446 dns, 3, 1, do_dns,
447 "lookup the IP of a hostname",
448 "hostname [envvar]"
449);
450
451#endif /* CONFIG_CMD_DNS */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000452
453#if defined(CONFIG_CMD_LINK_LOCAL)
Simon Glassed38aef2020-05-10 11:40:03 -0600454static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
455 char *const argv[])
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000456{
457 char tmp[22];
458
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500459 if (net_loop(LINKLOCAL) < 0)
Joe Hershbergerba867832015-03-22 17:09:09 -0500460 return CMD_RET_FAILURE;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000461
Joe Hershberger5874dec2015-04-08 01:41:01 -0500462 net_gateway.s_addr = 0;
463 ip_to_string(net_gateway, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600464 env_set("gatewayip", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000465
Joe Hershberger5874dec2015-04-08 01:41:01 -0500466 ip_to_string(net_netmask, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600467 env_set("netmask", tmp);
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000468
Joe Hershberger5874dec2015-04-08 01:41:01 -0500469 ip_to_string(net_ip, tmp);
Simon Glass6a38e412017-08-03 12:22:09 -0600470 env_set("ipaddr", tmp);
471 env_set("llipaddr", tmp); /* store this for next time */
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000472
Joe Hershbergerba867832015-03-22 17:09:09 -0500473 return CMD_RET_SUCCESS;
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000474}
475
476U_BOOT_CMD(
477 linklocal, 1, 1, do_link_local,
478 "acquire a network IP address using the link-local protocol",
479 ""
480);
481
482#endif /* CONFIG_CMD_LINK_LOCAL */