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