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