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