ddr: fsl: Remove CONFIG_MEM_INIT_VALUE
[platform/kernel/u-boot.git] / cmd / net.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 /*
8  * Boot support
9  */
10 #include <common.h>
11 #include <bootstage.h>
12 #include <command.h>
13 #include <dm.h>
14 #include <env.h>
15 #include <image.h>
16 #include <net.h>
17 #include <net6.h>
18 #include <net/udp.h>
19 #include <net/sntp.h>
20 #include <net/ncsi.h>
21
22 static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
23
24 #ifdef CONFIG_CMD_BOOTP
25 static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
26                     char *const argv[])
27 {
28         return netboot_common(BOOTP, cmdtp, argc, argv);
29 }
30
31 U_BOOT_CMD(
32         bootp,  3,      1,      do_bootp,
33         "boot image via network using BOOTP/TFTP protocol",
34         "[loadAddress] [[hostIPaddr:]bootfilename]"
35 );
36 #endif
37
38 #ifdef CONFIG_CMD_TFTPBOOT
39 int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
40 {
41         int ret;
42
43         bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
44         ret = netboot_common(TFTPGET, cmdtp, argc, argv);
45         bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
46         return ret;
47 }
48
49 #if IS_ENABLED(CONFIG_IPV6)
50 U_BOOT_CMD(
51         tftpboot,       4,      1,      do_tftpb,
52         "boot image via network using TFTP protocol\n"
53         "To use IPv6 add -ipv6 parameter or use IPv6 hostIPaddr framed "
54         "with [] brackets",
55         "[loadAddress] [[hostIPaddr:]bootfilename] [" USE_IP6_CMD_PARAM "]"
56 );
57 #else
58 U_BOOT_CMD(
59         tftpboot,       3,      1,      do_tftpb,
60         "load file via network using TFTP protocol",
61         "[loadAddress] [[hostIPaddr:]bootfilename]"
62 );
63 #endif
64 #endif
65
66 #ifdef CONFIG_CMD_TFTPPUT
67 static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
68                       char *const argv[])
69 {
70         return netboot_common(TFTPPUT, cmdtp, argc, argv);
71 }
72
73 U_BOOT_CMD(
74         tftpput,        4,      1,      do_tftpput,
75         "TFTP put command, for uploading files to a server",
76         "Address Size [[hostIPaddr:]filename]"
77 );
78 #endif
79
80 #ifdef CONFIG_CMD_TFTPSRV
81 static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
82                       char *const argv[])
83 {
84         return netboot_common(TFTPSRV, cmdtp, argc, argv);
85 }
86
87 U_BOOT_CMD(
88         tftpsrv,        2,      1,      do_tftpsrv,
89         "act as a TFTP server and boot the first received file",
90         "[loadAddress]\n"
91         "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
92         "The transfer is aborted if a transfer has not been started after\n"
93         "about 50 seconds or if Ctrl-C is pressed."
94 );
95 #endif
96
97
98 #ifdef CONFIG_CMD_RARP
99 int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
100 {
101         return netboot_common(RARP, cmdtp, argc, argv);
102 }
103
104 U_BOOT_CMD(
105         rarpboot,       3,      1,      do_rarpb,
106         "boot image via network using RARP/TFTP protocol",
107         "[loadAddress] [[hostIPaddr:]bootfilename]"
108 );
109 #endif
110
111 #if defined(CONFIG_CMD_DHCP)
112 static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
113                    char *const argv[])
114 {
115         return netboot_common(DHCP, cmdtp, argc, argv);
116 }
117
118 U_BOOT_CMD(
119         dhcp,   3,      1,      do_dhcp,
120         "boot image via network using DHCP/TFTP protocol",
121         "[loadAddress] [[hostIPaddr:]bootfilename]"
122 );
123 #endif
124
125 #if defined(CONFIG_CMD_NFS)
126 static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
127                   char *const argv[])
128 {
129         return netboot_common(NFS, cmdtp, argc, argv);
130 }
131
132 U_BOOT_CMD(
133         nfs,    3,      1,      do_nfs,
134         "boot image via network using NFS protocol",
135         "[loadAddress] [[hostIPaddr:]bootfilename]"
136 );
137 #endif
138
139 #if defined(CONFIG_CMD_WGET)
140 static int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
141 {
142         return netboot_common(WGET, cmdtp, argc, argv);
143 }
144
145 U_BOOT_CMD(
146         wget,   3,      1,      do_wget,
147         "boot image via network using HTTP protocol",
148         "[loadAddress] [[hostIPaddr:]path and image name]"
149 );
150 #endif
151
152 static void netboot_update_env(void)
153 {
154         char tmp[22];
155
156         if (net_gateway.s_addr) {
157                 ip_to_string(net_gateway, tmp);
158                 env_set("gatewayip", tmp);
159         }
160
161         if (net_netmask.s_addr) {
162                 ip_to_string(net_netmask, tmp);
163                 env_set("netmask", tmp);
164         }
165
166 #ifdef CONFIG_CMD_BOOTP
167         if (net_hostname[0])
168                 env_set("hostname", net_hostname);
169 #endif
170
171 #ifdef CONFIG_CMD_BOOTP
172         if (net_root_path[0])
173                 env_set("rootpath", net_root_path);
174 #endif
175
176         if (net_ip.s_addr) {
177                 ip_to_string(net_ip, tmp);
178                 env_set("ipaddr", tmp);
179         }
180         /*
181          * Only attempt to change serverip if net/bootp.c:store_net_params()
182          * could have set it
183          */
184         if (!IS_ENABLED(CONFIG_BOOTP_SERVERIP) && net_server_ip.s_addr) {
185                 ip_to_string(net_server_ip, tmp);
186                 env_set("serverip", tmp);
187         }
188         if (net_dns_server.s_addr) {
189                 ip_to_string(net_dns_server, tmp);
190                 env_set("dnsip", tmp);
191         }
192 #if defined(CONFIG_BOOTP_DNS2)
193         if (net_dns_server2.s_addr) {
194                 ip_to_string(net_dns_server2, tmp);
195                 env_set("dnsip2", tmp);
196         }
197 #endif
198 #ifdef CONFIG_CMD_BOOTP
199         if (net_nis_domain[0])
200                 env_set("domain", net_nis_domain);
201 #endif
202
203 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
204         if (net_ntp_time_offset) {
205                 sprintf(tmp, "%d", net_ntp_time_offset);
206                 env_set("timeoffset", tmp);
207         }
208 #endif
209 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
210         if (net_ntp_server.s_addr) {
211                 ip_to_string(net_ntp_server, tmp);
212                 env_set("ntpserverip", tmp);
213         }
214 #endif
215 }
216
217 /**
218  * parse_addr_size() - parse address and size arguments for tftpput
219  *
220  * @argv:       command line arguments
221  * Return:      0 on success
222  */
223 static int parse_addr_size(char * const argv[])
224 {
225         if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
226             strict_strtoul(argv[2], 16, &image_save_size) < 0) {
227                 printf("Invalid address/size\n");
228                 return CMD_RET_USAGE;
229         }
230         return 0;
231 }
232
233 /**
234  * parse_args() - parse command line arguments
235  *
236  * @proto:      command prototype
237  * @argc:       number of arguments
238  * @argv:       command line arguments
239  * Return:      0 on success
240  */
241 static int parse_args(enum proto_t proto, int argc, char *const argv[])
242 {
243         ulong addr;
244         char *end;
245
246         switch (argc) {
247         case 1:
248                 if (CONFIG_IS_ENABLED(CMD_TFTPPUT) && proto == TFTPPUT)
249                         return 1;
250
251                 /* refresh bootfile name from env */
252                 copy_filename(net_boot_file_name, env_get("bootfile"),
253                               sizeof(net_boot_file_name));
254                 break;
255
256         case 2:
257                 if (CONFIG_IS_ENABLED(CMD_TFTPPUT) && proto == TFTPPUT)
258                         return 1;
259                 /*
260                  * Only one arg - accept two forms:
261                  * Just load address, or just boot file name. The latter
262                  * form must be written in a format which can not be
263                  * mis-interpreted as a valid number.
264                  */
265                 addr = hextoul(argv[1], &end);
266                 if (end == (argv[1] + strlen(argv[1]))) {
267                         image_load_addr = addr;
268                         /* refresh bootfile name from env */
269                         copy_filename(net_boot_file_name, env_get("bootfile"),
270                                       sizeof(net_boot_file_name));
271                 } else {
272                         net_boot_file_name_explicit = true;
273                         copy_filename(net_boot_file_name, argv[1],
274                                       sizeof(net_boot_file_name));
275                 }
276                 break;
277
278         case 3:
279                 if (CONFIG_IS_ENABLED(CMD_TFTPPUT) && proto == TFTPPUT) {
280                         if (parse_addr_size(argv))
281                                 return 1;
282                 } else {
283                         image_load_addr = hextoul(argv[1], NULL);
284                         net_boot_file_name_explicit = true;
285                         copy_filename(net_boot_file_name, argv[2],
286                                       sizeof(net_boot_file_name));
287                 }
288                 break;
289
290 #ifdef CONFIG_CMD_TFTPPUT
291         case 4:
292                 if (parse_addr_size(argv))
293                         return 1;
294                 net_boot_file_name_explicit = true;
295                 copy_filename(net_boot_file_name, argv[3],
296                               sizeof(net_boot_file_name));
297                 break;
298 #endif
299         default:
300                 return 1;
301         }
302         return 0;
303 }
304
305 static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
306                           char *const argv[])
307 {
308         char *s;
309         int   rcode = 0;
310         int   size;
311
312         net_boot_file_name_explicit = false;
313         *net_boot_file_name = '\0';
314
315         /* pre-set image_load_addr */
316         s = env_get("loadaddr");
317         if (s != NULL)
318                 image_load_addr = hextoul(s, NULL);
319
320         if (IS_ENABLED(CONFIG_IPV6)) {
321                 use_ip6 = false;
322
323                 /* IPv6 parameter has to be always *last* */
324                 if (!strcmp(argv[argc - 1], USE_IP6_CMD_PARAM)) {
325                         use_ip6 = true;
326                         /* It is a hack not to break switch/case code */
327                         --argc;
328                 }
329         }
330
331         if (parse_args(proto, argc, argv)) {
332                 bootstage_error(BOOTSTAGE_ID_NET_START);
333                 return CMD_RET_USAGE;
334         }
335
336         bootstage_mark(BOOTSTAGE_ID_NET_START);
337
338         if (IS_ENABLED(CONFIG_IPV6) && !use_ip6) {
339                 char *s, *e;
340                 size_t len;
341
342                 s = strchr(net_boot_file_name, '[');
343                 e = strchr(net_boot_file_name, ']');
344                 if (s && e) {
345                         len = e - s;
346                         if (!string_to_ip6(s + 1, len - 1, &net_server_ip6))
347                                 use_ip6 = true;
348                 }
349         }
350
351         size = net_loop(proto);
352         if (size < 0) {
353                 bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
354                 return CMD_RET_FAILURE;
355         }
356         bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
357
358         /* net_loop ok, update environment */
359         netboot_update_env();
360
361         /* done if no file was loaded (no errors though) */
362         if (size == 0) {
363                 bootstage_error(BOOTSTAGE_ID_NET_LOADED);
364                 return CMD_RET_SUCCESS;
365         }
366
367         bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
368
369         rcode = bootm_maybe_autostart(cmdtp, argv[0]);
370
371         if (rcode == CMD_RET_SUCCESS)
372                 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
373         else
374                 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
375         return rcode;
376 }
377
378 #if defined(CONFIG_CMD_PING)
379 static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
380                    char *const argv[])
381 {
382         if (argc < 2)
383                 return CMD_RET_USAGE;
384
385         net_ping_ip = string_to_ip(argv[1]);
386         if (net_ping_ip.s_addr == 0)
387                 return CMD_RET_USAGE;
388
389         if (net_loop(PING) < 0) {
390                 printf("ping failed; host %s is not alive\n", argv[1]);
391                 return CMD_RET_FAILURE;
392         }
393
394         printf("host %s is alive\n", argv[1]);
395
396         return CMD_RET_SUCCESS;
397 }
398
399 U_BOOT_CMD(
400         ping,   2,      1,      do_ping,
401         "send ICMP ECHO_REQUEST to network host",
402         "pingAddress"
403 );
404 #endif
405
406 #if IS_ENABLED(CONFIG_CMD_PING6)
407 int do_ping6(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
408 {
409         if (string_to_ip6(argv[1], strlen(argv[1]), &net_ping_ip6))
410                 return CMD_RET_USAGE;
411
412         use_ip6 = true;
413         if (net_loop(PING6) < 0) {
414                 use_ip6 = false;
415                 printf("ping6 failed; host %pI6c is not alive\n",
416                        &net_ping_ip6);
417                 return 1;
418         }
419
420         use_ip6 = false;
421         printf("host %pI6c is alive\n", &net_ping_ip6);
422         return 0;
423 }
424
425 U_BOOT_CMD(
426         ping6,  2,      1,      do_ping6,
427         "send ICMPv6 ECHO_REQUEST to network host",
428         "pingAddress"
429 );
430 #endif /* CONFIG_CMD_PING6 */
431
432 #if defined(CONFIG_CMD_CDP)
433
434 static void cdp_update_env(void)
435 {
436         char tmp[16];
437
438         if (cdp_appliance_vlan != htons(-1)) {
439                 printf("CDP offered appliance VLAN %d\n",
440                        ntohs(cdp_appliance_vlan));
441                 vlan_to_string(cdp_appliance_vlan, tmp);
442                 env_set("vlan", tmp);
443                 net_our_vlan = cdp_appliance_vlan;
444         }
445
446         if (cdp_native_vlan != htons(-1)) {
447                 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
448                 vlan_to_string(cdp_native_vlan, tmp);
449                 env_set("nvlan", tmp);
450                 net_native_vlan = cdp_native_vlan;
451         }
452 }
453
454 int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
455 {
456         int r;
457
458         r = net_loop(CDP);
459         if (r < 0) {
460                 printf("cdp failed; perhaps not a CISCO switch?\n");
461                 return CMD_RET_FAILURE;
462         }
463
464         cdp_update_env();
465
466         return CMD_RET_SUCCESS;
467 }
468
469 U_BOOT_CMD(
470         cdp,    1,      1,      do_cdp,
471         "Perform CDP network configuration",
472         "\n"
473 );
474 #endif
475
476 #if defined(CONFIG_CMD_SNTP)
477 static struct udp_ops sntp_ops = {
478         .prereq = sntp_prereq,
479         .start = sntp_start,
480         .data = NULL,
481 };
482
483 int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
484 {
485         char *toff;
486
487         if (argc < 2) {
488                 net_ntp_server = env_get_ip("ntpserverip");
489                 if (net_ntp_server.s_addr == 0) {
490                         printf("ntpserverip not set\n");
491                         return CMD_RET_FAILURE;
492                 }
493         } else {
494                 net_ntp_server = string_to_ip(argv[1]);
495                 if (net_ntp_server.s_addr == 0) {
496                         printf("Bad NTP server IP address\n");
497                         return CMD_RET_FAILURE;
498                 }
499         }
500
501         toff = env_get("timeoffset");
502         if (toff == NULL)
503                 net_ntp_time_offset = 0;
504         else
505                 net_ntp_time_offset = simple_strtol(toff, NULL, 10);
506
507         if (udp_loop(&sntp_ops) < 0) {
508                 printf("SNTP failed: host %pI4 not responding\n",
509                        &net_ntp_server);
510                 return CMD_RET_FAILURE;
511         }
512
513         return CMD_RET_SUCCESS;
514 }
515
516 U_BOOT_CMD(
517         sntp,   2,      1,      do_sntp,
518         "synchronize RTC via network",
519         "[NTP server IP]\n"
520 );
521 #endif
522
523 #if defined(CONFIG_CMD_DNS)
524 int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
525 {
526         if (argc == 1)
527                 return CMD_RET_USAGE;
528
529         /*
530          * We should check for a valid hostname:
531          * - Each label must be between 1 and 63 characters long
532          * - the entire hostname has a maximum of 255 characters
533          * - only the ASCII letters 'a' through 'z' (case-insensitive),
534          *   the digits '0' through '9', and the hyphen
535          * - cannot begin or end with a hyphen
536          * - no other symbols, punctuation characters, or blank spaces are
537          *   permitted
538          * but hey - this is a minimalist implmentation, so only check length
539          * and let the name server deal with things.
540          */
541         if (strlen(argv[1]) >= 255) {
542                 printf("dns error: hostname too long\n");
543                 return CMD_RET_FAILURE;
544         }
545
546         net_dns_resolve = argv[1];
547
548         if (argc == 3)
549                 net_dns_env_var = argv[2];
550         else
551                 net_dns_env_var = NULL;
552
553         if (net_loop(DNS) < 0) {
554                 printf("dns lookup of %s failed, check setup\n", argv[1]);
555                 return CMD_RET_FAILURE;
556         }
557
558         return CMD_RET_SUCCESS;
559 }
560
561 U_BOOT_CMD(
562         dns,    3,      1,      do_dns,
563         "lookup the IP of a hostname",
564         "hostname [envvar]"
565 );
566
567 #endif  /* CONFIG_CMD_DNS */
568
569 #if defined(CONFIG_CMD_LINK_LOCAL)
570 static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
571                          char *const argv[])
572 {
573         char tmp[22];
574
575         if (net_loop(LINKLOCAL) < 0)
576                 return CMD_RET_FAILURE;
577
578         net_gateway.s_addr = 0;
579         ip_to_string(net_gateway, tmp);
580         env_set("gatewayip", tmp);
581
582         ip_to_string(net_netmask, tmp);
583         env_set("netmask", tmp);
584
585         ip_to_string(net_ip, tmp);
586         env_set("ipaddr", tmp);
587         env_set("llipaddr", tmp); /* store this for next time */
588
589         return CMD_RET_SUCCESS;
590 }
591
592 U_BOOT_CMD(
593         linklocal,      1,      1,      do_link_local,
594         "acquire a network IP address using the link-local protocol",
595         ""
596 );
597
598 #endif  /* CONFIG_CMD_LINK_LOCAL */
599
600 static int do_net_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
601 {
602         const struct udevice *current = eth_get_dev();
603         unsigned char env_enetaddr[ARP_HLEN];
604         const struct udevice *dev;
605         struct uclass *uc;
606
607         uclass_id_foreach_dev(UCLASS_ETH, dev, uc) {
608                 eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
609                 printf("eth%d : %s %pM %s\n", dev_seq(dev), dev->name, env_enetaddr,
610                        current == dev ? "active" : "");
611         }
612         return CMD_RET_SUCCESS;
613 }
614
615 static struct cmd_tbl cmd_net[] = {
616         U_BOOT_CMD_MKENT(list, 1, 0, do_net_list, "", ""),
617 };
618
619 static int do_net(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
620 {
621         struct cmd_tbl *cp;
622
623         cp = find_cmd_tbl(argv[1], cmd_net, ARRAY_SIZE(cmd_net));
624
625         /* Drop the net command */
626         argc--;
627         argv++;
628
629         if (!cp || argc > cp->maxargs)
630                 return CMD_RET_USAGE;
631         if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
632                 return CMD_RET_SUCCESS;
633
634         return cp->cmd(cmdtp, flag, argc, argv);
635 }
636
637 U_BOOT_CMD(
638         net, 2, 1, do_net,
639         "NET sub-system",
640         "list - list available devices\n"
641 );
642
643 #if defined(CONFIG_CMD_NCSI)
644 static int do_ncsi(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
645 {
646         if (!phy_interface_is_ncsi() || !ncsi_active()) {
647                 printf("Device not configured for NC-SI\n");
648                 return CMD_RET_FAILURE;
649         }
650
651         if (net_loop(NCSI) < 0)
652                 return CMD_RET_FAILURE;
653
654         return CMD_RET_SUCCESS;
655 }
656
657 U_BOOT_CMD(
658         ncsi,   1,      1,      do_ncsi,
659         "Configure attached NIC via NC-SI",
660         ""
661 );
662 #endif  /* CONFIG_CMD_NCSI */