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