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