]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_net.c
net: cosmetic: Change IPaddr_t to struct in_addr
[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{
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);
088f1b19 119 setenv("gatewayip", tmp);
6e592385 120 }
3863585b 121
049a95a7
JH
122 if (net_netmask.s_addr) {
123 ip_to_string(net_netmask, tmp);
088f1b19 124 setenv("netmask", tmp);
6e592385 125 }
3863585b 126
6e592385 127 if (NetOurHostName[0])
088f1b19 128 setenv("hostname", NetOurHostName);
3863585b 129
6e592385 130 if (NetOurRootPath[0])
088f1b19 131 setenv("rootpath", NetOurRootPath);
3863585b 132
049a95a7
JH
133 if (net_ip.s_addr) {
134 ip_to_string(net_ip, tmp);
088f1b19 135 setenv("ipaddr", tmp);
6e592385 136 }
a3e1a727
JH
137#if !defined(CONFIG_BOOTP_SERVERIP)
138 /*
139 * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
140 * could have set it
141 */
049a95a7
JH
142 if (net_server_ip.s_addr) {
143 ip_to_string(net_server_ip, tmp);
088f1b19 144 setenv("serverip", tmp);
6e592385 145 }
a3e1a727 146#endif
049a95a7
JH
147 if (net_dns_server.s_addr) {
148 ip_to_string(net_dns_server, tmp);
088f1b19 149 setenv("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);
088f1b19 154 setenv("dnsip2", tmp);
6e592385 155 }
fe389a82 156#endif
6e592385 157 if (NetOurNISDomain[0])
088f1b19 158 setenv("domain", NetOurNISDomain);
ea287deb 159
c76fe474 160#if defined(CONFIG_CMD_SNTP) \
1fe80d79 161 && defined(CONFIG_BOOTP_TIMEOFFSET)
ea287deb 162 if (NetTimeOffset) {
088f1b19
KP
163 sprintf(tmp, "%d", NetTimeOffset);
164 setenv("timeoffset", tmp);
ea287deb
WD
165 }
166#endif
c76fe474 167#if defined(CONFIG_CMD_SNTP) \
1fe80d79 168 && defined(CONFIG_BOOTP_NTPSERVER)
049a95a7
JH
169 if (net_ntp_server.s_addr) {
170 ip_to_string(net_ntp_server, tmp);
088f1b19 171 setenv("ntpserverip", tmp);
ea287deb
WD
172 }
173#endif
3863585b 174}
6e592385 175
e4bf0c5c
SG
176static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
177 char * const argv[])
3863585b
WD
178{
179 char *s;
2e4970d8 180 char *end;
3863585b
WD
181 int rcode = 0;
182 int size;
2e4970d8 183 ulong addr;
3863585b
WD
184
185 /* pre-set load_addr */
186 if ((s = getenv("loadaddr")) != NULL) {
187 load_addr = simple_strtoul(s, NULL, 16);
188 }
189
190 switch (argc) {
191 case 1:
192 break;
193
2e4970d8
PT
194 case 2: /*
195 * Only one arg - accept two forms:
196 * Just load address, or just boot file name. The latter
197 * form must be written in a format which can not be
198 * mis-interpreted as a valid number.
3863585b 199 */
2e4970d8
PT
200 addr = simple_strtoul(argv[1], &end, 16);
201 if (end == (argv[1] + strlen(argv[1])))
202 load_addr = addr;
203 else
204 copy_filename(BootFile, argv[1], sizeof(BootFile));
3863585b
WD
205 break;
206
207 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
088f1b19 208 copy_filename(BootFile, argv[2], sizeof(BootFile));
3863585b
WD
209
210 break;
211
2d46cf29
SG
212#ifdef CONFIG_CMD_TFTPPUT
213 case 4:
38bd80b4
SG
214 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
215 strict_strtoul(argv[2], 16, &save_size) < 0) {
216 printf("Invalid address/size\n");
85848f03 217 return CMD_RET_USAGE;
38bd80b4 218 }
2d46cf29
SG
219 copy_filename(BootFile, argv[3], sizeof(BootFile));
220 break;
221#endif
47e26b1b 222 default:
770605e4 223 bootstage_error(BOOTSTAGE_ID_NET_START);
4c12eeb8 224 return CMD_RET_USAGE;
3863585b 225 }
770605e4 226 bootstage_mark(BOOTSTAGE_ID_NET_START);
3863585b 227
566a494f 228 if ((size = NetLoop(proto)) < 0) {
770605e4 229 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
85848f03 230 return CMD_RET_FAILURE;
566a494f 231 }
770605e4 232 bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
3863585b
WD
233
234 /* NetLoop ok, update environment */
235 netboot_update_env();
236
eb9401e3 237 /* done if no file was loaded (no errors though) */
566a494f 238 if (size == 0) {
770605e4 239 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
85848f03 240 return CMD_RET_SUCCESS;
566a494f 241 }
eb9401e3 242
3863585b
WD
243 /* flush cache */
244 flush_cache(load_addr, size);
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
WD
266
267 if (NetLoop(PING) < 0) {
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
290 if (CDPApplianceVLAN != htons(-1)) {
291 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
292 VLAN_to_string(CDPApplianceVLAN, tmp);
293 setenv("vlan", tmp);
294 NetOurVLAN = CDPApplianceVLAN;
295 }
296
297 if (CDPNativeVLAN != htons(-1)) {
298 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
299 VLAN_to_string(CDPNativeVLAN, tmp);
300 setenv("nvlan", tmp);
301 NetOurNativeVLAN = CDPNativeVLAN;
302 }
303
304}
305
088f1b19 306int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a3d991bd
WD
307{
308 int r;
309
310 r = NetLoop(CDP);
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) {
049a95a7
JH
334 net_ntp_server = getenv_ip("ntpserverip");
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
088f1b19
KP
347 toff = getenv("timeoffset");
348 if (toff == NULL)
349 NetTimeOffset = 0;
350 else
351 NetTimeOffset = simple_strtol(toff, NULL, 10);
ea287deb
WD
352
353 if (NetLoop(SNTP) < 0) {
d6840e3d 354 printf("SNTP failed: host %pI4 not responding\n",
049a95a7 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
392 NetDNSResolve = argv[1];
393
394 if (argc == 3)
395 NetDNSenvvar = argv[2];
396 else
397 NetDNSenvvar = NULL;
398
399 if (NetLoop(DNS) < 0) {
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
421 if (NetLoop(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);
d22c338e
JH
426 setenv("gatewayip", tmp);
427
049a95a7 428 ip_to_string(net_netmask, tmp);
d22c338e
JH
429 setenv("netmask", tmp);
430
049a95a7 431 ip_to_string(net_ip, tmp);
d22c338e
JH
432 setenv("ipaddr", tmp);
433 setenv("llipaddr", tmp); /* store this for next time */
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 */