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