]> git.ipfire.org Git - people/ms/u-boot.git/blame - cmd/net.c
Remove CONFIG_SYS_BOOTCOUNT_SINGLEWORD
[people/ms/u-boot.git] / cmd / net.c
CommitLineData
3863585b
WD
1/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
3863585b
WD
6 */
7
8/*
9 * Boot support
10 */
11#include <common.h>
12#include <command.h>
3863585b
WD
13#include <net.h>
14
e4bf0c5c 15static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
3863585b 16
088f1b19 17static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b 18{
088f1b19 19 return netboot_common(BOOTP, cmdtp, argc, argv);
3863585b
WD
20}
21
0d498393
WD
22U_BOOT_CMD(
23 bootp, 3, 1, do_bootp,
2fb2604d 24 "boot image via network using BOOTP/TFTP protocol",
a89c33db 25 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77
WD
26);
27
088f1b19 28int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b 29{
573f14fe
SG
30 int ret;
31
32 bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
33 ret = netboot_common(TFTPGET, cmdtp, argc, argv);
34 bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
35 return ret;
3863585b
WD
36}
37
0d498393
WD
38U_BOOT_CMD(
39 tftpboot, 3, 1, do_tftpb,
2fb2604d 40 "boot image via network using TFTP protocol",
a89c33db 41 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77
WD
42);
43
2d46cf29 44#ifdef CONFIG_CMD_TFTPPUT
0c1b869b 45static int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
2d46cf29 46{
85848f03 47 return netboot_common(TFTPPUT, cmdtp, argc, argv);
2d46cf29
SG
48}
49
50U_BOOT_CMD(
51 tftpput, 4, 1, do_tftpput,
52 "TFTP put command, for uploading files to a server",
53 "Address Size [[hostIPaddr:]filename]"
54);
55#endif
56
7a83af07
LC
57#ifdef CONFIG_CMD_TFTPSRV
58static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
59{
60 return netboot_common(TFTPSRV, cmdtp, argc, argv);
61}
62
63U_BOOT_CMD(
64 tftpsrv, 2, 1, do_tftpsrv,
65 "act as a TFTP server and boot the first received file",
66 "[loadAddress]\n"
67 "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
68 "The transfer is aborted if a transfer has not been started after\n"
69 "about 50 seconds or if Ctrl-C is pressed."
70);
71#endif
72
73
bf6cb247 74#ifdef CONFIG_CMD_RARP
088f1b19 75int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b 76{
088f1b19 77 return netboot_common(RARP, cmdtp, argc, argv);
3863585b
WD
78}
79
0d498393
WD
80U_BOOT_CMD(
81 rarpboot, 3, 1, do_rarpb,
2fb2604d 82 "boot image via network using RARP/TFTP protocol",
a89c33db 83 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 84);
bf6cb247 85#endif
8bde7f77 86
c76fe474 87#if defined(CONFIG_CMD_DHCP)
088f1b19 88static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b
WD
89{
90 return netboot_common(DHCP, cmdtp, argc, argv);
91}
8bde7f77 92
0d498393
WD
93U_BOOT_CMD(
94 dhcp, 3, 1, do_dhcp,
2fb2604d 95 "boot image via network using DHCP/TFTP protocol",
a89c33db 96 "[loadAddress] [[hostIPaddr:]bootfilename]"
8bde7f77 97);
90253178 98#endif
3863585b 99
c76fe474 100#if defined(CONFIG_CMD_NFS)
088f1b19 101static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
cbd8a35c
WD
102{
103 return netboot_common(NFS, cmdtp, argc, argv);
104}
105
106U_BOOT_CMD(
107 nfs, 3, 1, do_nfs,
2fb2604d 108 "boot image via network using NFS protocol",
a89c33db 109 "[loadAddress] [[hostIPaddr:]bootfilename]"
cbd8a35c 110);
90253178 111#endif
cbd8a35c 112
088f1b19 113static void netboot_update_env(void)
3863585b 114{
6e592385 115 char tmp[22];
3863585b 116
049a95a7
JH
117 if (net_gateway.s_addr) {
118 ip_to_string(net_gateway, tmp);
382bee57 119 env_set("gatewayip", tmp);
6e592385 120 }
3863585b 121
049a95a7
JH
122 if (net_netmask.s_addr) {
123 ip_to_string(net_netmask, tmp);
382bee57 124 env_set("netmask", tmp);
6e592385 125 }
3863585b 126
586cbe51 127 if (net_hostname[0])
382bee57 128 env_set("hostname", net_hostname);
3863585b 129
586cbe51 130 if (net_root_path[0])
382bee57 131 env_set("rootpath", net_root_path);
3863585b 132
049a95a7
JH
133 if (net_ip.s_addr) {
134 ip_to_string(net_ip, tmp);
382bee57 135 env_set("ipaddr", tmp);
6e592385 136 }
a3e1a727
JH
137#if !defined(CONFIG_BOOTP_SERVERIP)
138 /*
ff78ad28 139 * Only attempt to change serverip if net/bootp.c:store_net_params()
a3e1a727
JH
140 * could have set it
141 */
049a95a7
JH
142 if (net_server_ip.s_addr) {
143 ip_to_string(net_server_ip, tmp);
382bee57 144 env_set("serverip", tmp);
6e592385 145 }
a3e1a727 146#endif
049a95a7
JH
147 if (net_dns_server.s_addr) {
148 ip_to_string(net_dns_server, tmp);
382bee57 149 env_set("dnsip", tmp);
6e592385 150 }
1fe80d79 151#if defined(CONFIG_BOOTP_DNS2)
049a95a7
JH
152 if (net_dns_server2.s_addr) {
153 ip_to_string(net_dns_server2, tmp);
382bee57 154 env_set("dnsip2", tmp);
6e592385 155 }
fe389a82 156#endif
586cbe51 157 if (net_nis_domain[0])
382bee57 158 env_set("domain", net_nis_domain);
ea287deb 159
4fd5055f 160#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
bc0571fc
JH
161 if (net_ntp_time_offset) {
162 sprintf(tmp, "%d", net_ntp_time_offset);
382bee57 163 env_set("timeoffset", tmp);
ea287deb
WD
164 }
165#endif
4fd5055f 166#if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
049a95a7
JH
167 if (net_ntp_server.s_addr) {
168 ip_to_string(net_ntp_server, tmp);
382bee57 169 env_set("ntpserverip", tmp);
ea287deb
WD
170 }
171#endif
3863585b 172}
6e592385 173
e4bf0c5c
SG
174static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
175 char * const argv[])
3863585b
WD
176{
177 char *s;
2e4970d8 178 char *end;
3863585b
WD
179 int rcode = 0;
180 int size;
2e4970d8 181 ulong addr;
3863585b
WD
182
183 /* pre-set load_addr */
00caae6d 184 s = env_get("loadaddr");
4fd5055f 185 if (s != NULL)
3863585b 186 load_addr = simple_strtoul(s, NULL, 16);
3863585b
WD
187
188 switch (argc) {
189 case 1:
190 break;
191
2e4970d8
PT
192 case 2: /*
193 * Only one arg - accept two forms:
194 * Just load address, or just boot file name. The latter
195 * form must be written in a format which can not be
196 * mis-interpreted as a valid number.
3863585b 197 */
2e4970d8
PT
198 addr = simple_strtoul(argv[1], &end, 16);
199 if (end == (argv[1] + strlen(argv[1])))
200 load_addr = addr;
201 else
1411157d
JH
202 copy_filename(net_boot_file_name, argv[1],
203 sizeof(net_boot_file_name));
3863585b
WD
204 break;
205
4fd5055f
JH
206 case 3:
207 load_addr = simple_strtoul(argv[1], NULL, 16);
1411157d
JH
208 copy_filename(net_boot_file_name, argv[2],
209 sizeof(net_boot_file_name));
3863585b
WD
210
211 break;
212
2d46cf29
SG
213#ifdef CONFIG_CMD_TFTPPUT
214 case 4:
38bd80b4 215 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
4fd5055f 216 strict_strtoul(argv[2], 16, &save_size) < 0) {
38bd80b4 217 printf("Invalid address/size\n");
85848f03 218 return CMD_RET_USAGE;
38bd80b4 219 }
1411157d
JH
220 copy_filename(net_boot_file_name, argv[3],
221 sizeof(net_boot_file_name));
2d46cf29
SG
222 break;
223#endif
47e26b1b 224 default:
770605e4 225 bootstage_error(BOOTSTAGE_ID_NET_START);
4c12eeb8 226 return CMD_RET_USAGE;
3863585b 227 }
770605e4 228 bootstage_mark(BOOTSTAGE_ID_NET_START);
3863585b 229
bc0571fc 230 size = net_loop(proto);
4fd5055f 231 if (size < 0) {
770605e4 232 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
85848f03 233 return CMD_RET_FAILURE;
566a494f 234 }
770605e4 235 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
3863585b 236
bc0571fc 237 /* net_loop ok, update environment */
3863585b
WD
238 netboot_update_env();
239
eb9401e3 240 /* done if no file was loaded (no errors though) */
566a494f 241 if (size == 0) {
770605e4 242 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
85848f03 243 return CMD_RET_SUCCESS;
566a494f 244 }
eb9401e3 245
770605e4 246 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
c8e66db7 247
67d668bf 248 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
3863585b 249
85848f03 250 if (rcode == CMD_RET_SUCCESS)
770605e4 251 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
85848f03
JH
252 else
253 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
3863585b
WD
254 return rcode;
255}
256
c76fe474 257#if defined(CONFIG_CMD_PING)
088f1b19 258static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
73a8b27c
WD
259{
260 if (argc < 2)
85848f03 261 return CMD_RET_USAGE;
73a8b27c 262
049a95a7
JH
263 net_ping_ip = string_to_ip(argv[1]);
264 if (net_ping_ip.s_addr == 0)
4c12eeb8 265 return CMD_RET_USAGE;
73a8b27c 266
bc0571fc 267 if (net_loop(PING) < 0) {
73a8b27c 268 printf("ping failed; host %s is not alive\n", argv[1]);
85848f03 269 return CMD_RET_FAILURE;
73a8b27c
WD
270 }
271
272 printf("host %s is alive\n", argv[1]);
273
85848f03 274 return CMD_RET_SUCCESS;
73a8b27c 275}
6dff5529
WD
276
277U_BOOT_CMD(
278 ping, 2, 1, do_ping,
2fb2604d 279 "send ICMP ECHO_REQUEST to network host",
a89c33db 280 "pingAddress"
6dff5529 281);
90253178 282#endif
73a8b27c 283
c76fe474 284#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
285
286static void cdp_update_env(void)
287{
288 char tmp[16];
289
6aede5b7
JH
290 if (cdp_appliance_vlan != htons(-1)) {
291 printf("CDP offered appliance VLAN %d\n",
292 ntohs(cdp_appliance_vlan));
4fd5055f 293 vlan_to_string(cdp_appliance_vlan, tmp);
382bee57 294 env_set("vlan", tmp);
4fd5055f 295 net_our_vlan = cdp_appliance_vlan;
a3d991bd
WD
296 }
297
6aede5b7
JH
298 if (cdp_native_vlan != htons(-1)) {
299 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
4fd5055f 300 vlan_to_string(cdp_native_vlan, tmp);
382bee57 301 env_set("nvlan", tmp);
4fd5055f 302 net_native_vlan = cdp_native_vlan;
a3d991bd 303 }
a3d991bd
WD
304}
305
088f1b19 306int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a3d991bd
WD
307{
308 int r;
309
bc0571fc 310 r = net_loop(CDP);
a3d991bd
WD
311 if (r < 0) {
312 printf("cdp failed; perhaps not a CISCO switch?\n");
85848f03 313 return CMD_RET_FAILURE;
a3d991bd
WD
314 }
315
316 cdp_update_env();
317
85848f03 318 return CMD_RET_SUCCESS;
a3d991bd
WD
319}
320
321U_BOOT_CMD(
322 cdp, 1, 1, do_cdp,
ec5c04cd 323 "Perform CDP network configuration",
4b58266e 324 "\n"
a3d991bd 325);
90253178 326#endif
a3d991bd 327
c76fe474 328#if defined(CONFIG_CMD_SNTP)
088f1b19 329int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
ea287deb
WD
330{
331 char *toff;
332
333 if (argc < 2) {
723806cc 334 net_ntp_server = env_get_ip("ntpserverip");
049a95a7 335 if (net_ntp_server.s_addr == 0) {
088f1b19 336 printf("ntpserverip not set\n");
85848f03 337 return CMD_RET_FAILURE;
ea287deb
WD
338 }
339 } else {
049a95a7
JH
340 net_ntp_server = string_to_ip(argv[1]);
341 if (net_ntp_server.s_addr == 0) {
088f1b19 342 printf("Bad NTP server IP address\n");
85848f03 343 return CMD_RET_FAILURE;
ea287deb
WD
344 }
345 }
346
00caae6d 347 toff = env_get("timeoffset");
088f1b19 348 if (toff == NULL)
bc0571fc 349 net_ntp_time_offset = 0;
088f1b19 350 else
bc0571fc 351 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
ea287deb 352
bc0571fc 353 if (net_loop(SNTP) < 0) {
d6840e3d 354 printf("SNTP failed: host %pI4 not responding\n",
4fd5055f 355 &net_ntp_server);
85848f03 356 return CMD_RET_FAILURE;
ea287deb
WD
357 }
358
85848f03 359 return CMD_RET_SUCCESS;
ea287deb
WD
360}
361
362U_BOOT_CMD(
363 sntp, 2, 1, do_sntp,
2fb2604d 364 "synchronize RTC via network",
ea287deb
WD
365 "[NTP server IP]\n"
366);
90253178 367#endif
1a32bf41
RG
368
369#if defined(CONFIG_CMD_DNS)
54841ab5 370int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1a32bf41 371{
47e26b1b 372 if (argc == 1)
4c12eeb8 373 return CMD_RET_USAGE;
1a32bf41
RG
374
375 /*
376 * We should check for a valid hostname:
377 * - Each label must be between 1 and 63 characters long
378 * - the entire hostname has a maximum of 255 characters
379 * - only the ASCII letters 'a' through 'z' (case-insensitive),
380 * the digits '0' through '9', and the hyphen
381 * - cannot begin or end with a hyphen
382 * - no other symbols, punctuation characters, or blank spaces are
383 * permitted
384 * but hey - this is a minimalist implmentation, so only check length
385 * and let the name server deal with things.
386 */
387 if (strlen(argv[1]) >= 255) {
388 printf("dns error: hostname too long\n");
85848f03 389 return CMD_RET_FAILURE;
1a32bf41
RG
390 }
391
786eac5f 392 net_dns_resolve = argv[1];
1a32bf41
RG
393
394 if (argc == 3)
786eac5f 395 net_dns_env_var = argv[2];
1a32bf41 396 else
786eac5f 397 net_dns_env_var = NULL;
1a32bf41 398
bc0571fc 399 if (net_loop(DNS) < 0) {
1a32bf41 400 printf("dns lookup of %s failed, check setup\n", argv[1]);
85848f03 401 return CMD_RET_FAILURE;
1a32bf41
RG
402 }
403
85848f03 404 return CMD_RET_SUCCESS;
1a32bf41
RG
405}
406
407U_BOOT_CMD(
408 dns, 3, 1, do_dns,
409 "lookup the IP of a hostname",
410 "hostname [envvar]"
411);
412
413#endif /* CONFIG_CMD_DNS */
d22c338e
JH
414
415#if defined(CONFIG_CMD_LINK_LOCAL)
416static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
417 char * const argv[])
418{
419 char tmp[22];
420
bc0571fc 421 if (net_loop(LINKLOCAL) < 0)
85848f03 422 return CMD_RET_FAILURE;
d22c338e 423
049a95a7
JH
424 net_gateway.s_addr = 0;
425 ip_to_string(net_gateway, tmp);
382bee57 426 env_set("gatewayip", tmp);
d22c338e 427
049a95a7 428 ip_to_string(net_netmask, tmp);
382bee57 429 env_set("netmask", tmp);
d22c338e 430
049a95a7 431 ip_to_string(net_ip, tmp);
382bee57
SG
432 env_set("ipaddr", tmp);
433 env_set("llipaddr", tmp); /* store this for next time */
d22c338e 434
85848f03 435 return CMD_RET_SUCCESS;
d22c338e
JH
436}
437
438U_BOOT_CMD(
439 linklocal, 1, 1, do_link_local,
440 "acquire a network IP address using the link-local protocol",
441 ""
442);
443
444#endif /* CONFIG_CMD_LINK_LOCAL */