]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_net.c
arc: introduce U-Boot port for ARCv2 ISA
[people/ms/u-boot.git] / common / 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
SG
44#ifdef CONFIG_CMD_TFTPPUT
45int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
46{
47 int ret;
48
49 ret = netboot_common(TFTPPUT, cmdtp, argc, argv);
50 return ret;
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
6e592385 120 if (NetOurGatewayIP) {
088f1b19
KP
121 ip_to_string(NetOurGatewayIP, tmp);
122 setenv("gatewayip", tmp);
6e592385 123 }
3863585b 124
6e592385 125 if (NetOurSubnetMask) {
088f1b19
KP
126 ip_to_string(NetOurSubnetMask, tmp);
127 setenv("netmask", tmp);
6e592385 128 }
3863585b 129
6e592385 130 if (NetOurHostName[0])
088f1b19 131 setenv("hostname", NetOurHostName);
3863585b 132
6e592385 133 if (NetOurRootPath[0])
088f1b19 134 setenv("rootpath", NetOurRootPath);
3863585b 135
6e592385 136 if (NetOurIP) {
088f1b19
KP
137 ip_to_string(NetOurIP, tmp);
138 setenv("ipaddr", tmp);
6e592385 139 }
a3e1a727
JH
140#if !defined(CONFIG_BOOTP_SERVERIP)
141 /*
142 * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
143 * could have set it
144 */
6e592385 145 if (NetServerIP) {
088f1b19
KP
146 ip_to_string(NetServerIP, tmp);
147 setenv("serverip", tmp);
6e592385 148 }
a3e1a727 149#endif
6e592385 150 if (NetOurDNSIP) {
088f1b19
KP
151 ip_to_string(NetOurDNSIP, tmp);
152 setenv("dnsip", tmp);
6e592385 153 }
1fe80d79 154#if defined(CONFIG_BOOTP_DNS2)
6e592385 155 if (NetOurDNS2IP) {
088f1b19
KP
156 ip_to_string(NetOurDNS2IP, tmp);
157 setenv("dnsip2", tmp);
6e592385 158 }
fe389a82 159#endif
6e592385 160 if (NetOurNISDomain[0])
088f1b19 161 setenv("domain", NetOurNISDomain);
ea287deb 162
c76fe474 163#if defined(CONFIG_CMD_SNTP) \
1fe80d79 164 && defined(CONFIG_BOOTP_TIMEOFFSET)
ea287deb 165 if (NetTimeOffset) {
088f1b19
KP
166 sprintf(tmp, "%d", NetTimeOffset);
167 setenv("timeoffset", tmp);
ea287deb
WD
168 }
169#endif
c76fe474 170#if defined(CONFIG_CMD_SNTP) \
1fe80d79 171 && defined(CONFIG_BOOTP_NTPSERVER)
ea287deb 172 if (NetNtpServerIP) {
088f1b19
KP
173 ip_to_string(NetNtpServerIP, tmp);
174 setenv("ntpserverip", tmp);
ea287deb
WD
175 }
176#endif
3863585b 177}
6e592385 178
e4bf0c5c
SG
179static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
180 char * const argv[])
3863585b
WD
181{
182 char *s;
2e4970d8 183 char *end;
3863585b
WD
184 int rcode = 0;
185 int size;
2e4970d8 186 ulong addr;
3863585b
WD
187
188 /* pre-set load_addr */
189 if ((s = getenv("loadaddr")) != NULL) {
190 load_addr = simple_strtoul(s, NULL, 16);
191 }
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
PT
203 addr = simple_strtoul(argv[1], &end, 16);
204 if (end == (argv[1] + strlen(argv[1])))
205 load_addr = addr;
206 else
207 copy_filename(BootFile, argv[1], sizeof(BootFile));
3863585b
WD
208 break;
209
210 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
088f1b19 211 copy_filename(BootFile, argv[2], sizeof(BootFile));
3863585b
WD
212
213 break;
214
2d46cf29
SG
215#ifdef CONFIG_CMD_TFTPPUT
216 case 4:
38bd80b4
SG
217 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
218 strict_strtoul(argv[2], 16, &save_size) < 0) {
219 printf("Invalid address/size\n");
220 return cmd_usage(cmdtp);
221 }
2d46cf29
SG
222 copy_filename(BootFile, argv[3], sizeof(BootFile));
223 break;
224#endif
47e26b1b 225 default:
770605e4 226 bootstage_error(BOOTSTAGE_ID_NET_START);
4c12eeb8 227 return CMD_RET_USAGE;
3863585b 228 }
770605e4 229 bootstage_mark(BOOTSTAGE_ID_NET_START);
3863585b 230
566a494f 231 if ((size = NetLoop(proto)) < 0) {
770605e4 232 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
3863585b 233 return 1;
566a494f 234 }
770605e4 235 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
3863585b
WD
236
237 /* NetLoop ok, update environment */
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);
eb9401e3 243 return 0;
566a494f 244 }
eb9401e3 245
3863585b
WD
246 /* flush cache */
247 flush_cache(load_addr, size);
248
770605e4 249 bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
c8e66db7 250
67d668bf 251 rcode = bootm_maybe_autostart(cmdtp, argv[0]);
3863585b 252
566a494f 253 if (rcode < 0)
770605e4 254 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
566a494f 255 else
770605e4 256 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
3863585b
WD
257 return rcode;
258}
259
c76fe474 260#if defined(CONFIG_CMD_PING)
088f1b19 261static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
73a8b27c
WD
262{
263 if (argc < 2)
264 return -1;
265
266 NetPingIP = string_to_ip(argv[1]);
47e26b1b 267 if (NetPingIP == 0)
4c12eeb8 268 return CMD_RET_USAGE;
73a8b27c
WD
269
270 if (NetLoop(PING) < 0) {
271 printf("ping failed; host %s is not alive\n", argv[1]);
272 return 1;
273 }
274
275 printf("host %s is alive\n", argv[1]);
276
277 return 0;
278}
6dff5529
WD
279
280U_BOOT_CMD(
281 ping, 2, 1, do_ping,
2fb2604d 282 "send ICMP ECHO_REQUEST to network host",
a89c33db 283 "pingAddress"
6dff5529 284);
90253178 285#endif
73a8b27c 286
c76fe474 287#if defined(CONFIG_CMD_CDP)
a3d991bd
WD
288
289static void cdp_update_env(void)
290{
291 char tmp[16];
292
293 if (CDPApplianceVLAN != htons(-1)) {
294 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
295 VLAN_to_string(CDPApplianceVLAN, tmp);
296 setenv("vlan", tmp);
297 NetOurVLAN = CDPApplianceVLAN;
298 }
299
300 if (CDPNativeVLAN != htons(-1)) {
301 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
302 VLAN_to_string(CDPNativeVLAN, tmp);
303 setenv("nvlan", tmp);
304 NetOurNativeVLAN = CDPNativeVLAN;
305 }
306
307}
308
088f1b19 309int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a3d991bd
WD
310{
311 int r;
312
313 r = NetLoop(CDP);
314 if (r < 0) {
315 printf("cdp failed; perhaps not a CISCO switch?\n");
316 return 1;
317 }
318
319 cdp_update_env();
320
321 return 0;
322}
323
324U_BOOT_CMD(
325 cdp, 1, 1, do_cdp,
ec5c04cd 326 "Perform CDP network configuration",
4b58266e 327 "\n"
a3d991bd 328);
90253178 329#endif
a3d991bd 330
c76fe474 331#if defined(CONFIG_CMD_SNTP)
088f1b19 332int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
ea287deb
WD
333{
334 char *toff;
335
336 if (argc < 2) {
088f1b19 337 NetNtpServerIP = getenv_IPaddr("ntpserverip");
ea287deb 338 if (NetNtpServerIP == 0) {
088f1b19 339 printf("ntpserverip not set\n");
ea287deb
WD
340 return (1);
341 }
342 } else {
343 NetNtpServerIP = string_to_ip(argv[1]);
344 if (NetNtpServerIP == 0) {
088f1b19 345 printf("Bad NTP server IP address\n");
ea287deb
WD
346 return (1);
347 }
348 }
349
088f1b19
KP
350 toff = getenv("timeoffset");
351 if (toff == NULL)
352 NetTimeOffset = 0;
353 else
354 NetTimeOffset = simple_strtol(toff, NULL, 10);
ea287deb
WD
355
356 if (NetLoop(SNTP) < 0) {
d6840e3d
LP
357 printf("SNTP failed: host %pI4 not responding\n",
358 &NetNtpServerIP);
ea287deb
WD
359 return 1;
360 }
361
362 return 0;
363}
364
365U_BOOT_CMD(
366 sntp, 2, 1, do_sntp,
2fb2604d 367 "synchronize RTC via network",
ea287deb
WD
368 "[NTP server IP]\n"
369);
90253178 370#endif
1a32bf41
RG
371
372#if defined(CONFIG_CMD_DNS)
54841ab5 373int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1a32bf41 374{
47e26b1b 375 if (argc == 1)
4c12eeb8 376 return CMD_RET_USAGE;
1a32bf41
RG
377
378 /*
379 * We should check for a valid hostname:
380 * - Each label must be between 1 and 63 characters long
381 * - the entire hostname has a maximum of 255 characters
382 * - only the ASCII letters 'a' through 'z' (case-insensitive),
383 * the digits '0' through '9', and the hyphen
384 * - cannot begin or end with a hyphen
385 * - no other symbols, punctuation characters, or blank spaces are
386 * permitted
387 * but hey - this is a minimalist implmentation, so only check length
388 * and let the name server deal with things.
389 */
390 if (strlen(argv[1]) >= 255) {
391 printf("dns error: hostname too long\n");
392 return 1;
393 }
394
395 NetDNSResolve = argv[1];
396
397 if (argc == 3)
398 NetDNSenvvar = argv[2];
399 else
400 NetDNSenvvar = NULL;
401
402 if (NetLoop(DNS) < 0) {
403 printf("dns lookup of %s failed, check setup\n", argv[1]);
404 return 1;
405 }
406
407 return 0;
408}
409
410U_BOOT_CMD(
411 dns, 3, 1, do_dns,
412 "lookup the IP of a hostname",
413 "hostname [envvar]"
414);
415
416#endif /* CONFIG_CMD_DNS */
d22c338e
JH
417
418#if defined(CONFIG_CMD_LINK_LOCAL)
419static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
420 char * const argv[])
421{
422 char tmp[22];
423
424 if (NetLoop(LINKLOCAL) < 0)
425 return 1;
426
427 NetOurGatewayIP = 0;
428 ip_to_string(NetOurGatewayIP, tmp);
429 setenv("gatewayip", tmp);
430
431 ip_to_string(NetOurSubnetMask, tmp);
432 setenv("netmask", tmp);
433
434 ip_to_string(NetOurIP, tmp);
435 setenv("ipaddr", tmp);
436 setenv("llipaddr", tmp); /* store this for next time */
437
438 return 0;
439}
440
441U_BOOT_CMD(
442 linklocal, 1, 1, do_link_local,
443 "acquire a network IP address using the link-local protocol",
444 ""
445);
446
447#endif /* CONFIG_CMD_LINK_LOCAL */