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