]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/cmd_net.c
1deebf2b876685965d06700e031659371a0a9940
[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         return netboot_common(TFTPPUT, cmdtp, argc, argv);
48 }
49
50 U_BOOT_CMD(
51         tftpput,        4,      1,      do_tftpput,
52         "TFTP put command, for uploading files to a server",
53         "Address Size [[hostIPaddr:]filename]"
54 );
55 #endif
56
57 #ifdef CONFIG_CMD_TFTPSRV
58 static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
59 {
60         return netboot_common(TFTPSRV, cmdtp, argc, argv);
61 }
62
63 U_BOOT_CMD(
64         tftpsrv,        2,      1,      do_tftpsrv,
65         "act as a TFTP server and boot the first received file",
66         "[loadAddress]\n"
67         "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
68         "The transfer is aborted if a transfer has not been started after\n"
69         "about 50 seconds or if Ctrl-C is pressed."
70 );
71 #endif
72
73
74 #ifdef CONFIG_CMD_RARP
75 int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
76 {
77         return netboot_common(RARP, cmdtp, argc, argv);
78 }
79
80 U_BOOT_CMD(
81         rarpboot,       3,      1,      do_rarpb,
82         "boot image via network using RARP/TFTP protocol",
83         "[loadAddress] [[hostIPaddr:]bootfilename]"
84 );
85 #endif
86
87 #if defined(CONFIG_CMD_DHCP)
88 static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
89 {
90         return netboot_common(DHCP, cmdtp, argc, argv);
91 }
92
93 U_BOOT_CMD(
94         dhcp,   3,      1,      do_dhcp,
95         "boot image via network using DHCP/TFTP protocol",
96         "[loadAddress] [[hostIPaddr:]bootfilename]"
97 );
98 #endif
99
100 #if defined(CONFIG_CMD_NFS)
101 static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
102 {
103         return netboot_common(NFS, cmdtp, argc, argv);
104 }
105
106 U_BOOT_CMD(
107         nfs,    3,      1,      do_nfs,
108         "boot image via network using NFS protocol",
109         "[loadAddress] [[hostIPaddr:]bootfilename]"
110 );
111 #endif
112
113 static void netboot_update_env(void)
114 {
115         char tmp[22];
116
117         if (net_gateway.s_addr) {
118                 ip_to_string(net_gateway, tmp);
119                 setenv("gatewayip", tmp);
120         }
121
122         if (net_netmask.s_addr) {
123                 ip_to_string(net_netmask, tmp);
124                 setenv("netmask", tmp);
125         }
126
127         if (net_hostname[0])
128                 setenv("hostname", net_hostname);
129
130         if (net_root_path[0])
131                 setenv("rootpath", net_root_path);
132
133         if (net_ip.s_addr) {
134                 ip_to_string(net_ip, tmp);
135                 setenv("ipaddr", tmp);
136         }
137 #if !defined(CONFIG_BOOTP_SERVERIP)
138         /*
139          * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
140          * could have set it
141          */
142         if (net_server_ip.s_addr) {
143                 ip_to_string(net_server_ip, tmp);
144                 setenv("serverip", tmp);
145         }
146 #endif
147         if (net_dns_server.s_addr) {
148                 ip_to_string(net_dns_server, tmp);
149                 setenv("dnsip", tmp);
150         }
151 #if defined(CONFIG_BOOTP_DNS2)
152         if (net_dns_server2.s_addr) {
153                 ip_to_string(net_dns_server2, tmp);
154                 setenv("dnsip2", tmp);
155         }
156 #endif
157         if (net_nis_domain[0])
158                 setenv("domain", net_nis_domain);
159
160 #if defined(CONFIG_CMD_SNTP) \
161     && defined(CONFIG_BOOTP_TIMEOFFSET)
162         if (NetTimeOffset) {
163                 sprintf(tmp, "%d", NetTimeOffset);
164                 setenv("timeoffset", tmp);
165         }
166 #endif
167 #if defined(CONFIG_CMD_SNTP) \
168     && defined(CONFIG_BOOTP_NTPSERVER)
169         if (net_ntp_server.s_addr) {
170                 ip_to_string(net_ntp_server, tmp);
171                 setenv("ntpserverip", tmp);
172         }
173 #endif
174 }
175
176 static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
177                 char * const argv[])
178 {
179         char *s;
180         char *end;
181         int   rcode = 0;
182         int   size;
183         ulong addr;
184
185         /* pre-set load_addr */
186         if ((s = getenv("loadaddr")) != NULL) {
187                 load_addr = simple_strtoul(s, NULL, 16);
188         }
189
190         switch (argc) {
191         case 1:
192                 break;
193
194         case 2: /*
195                  * Only one arg - accept two forms:
196                  * Just load address, or just boot file name. The latter
197                  * form must be written in a format which can not be
198                  * mis-interpreted as a valid number.
199                  */
200                 addr = simple_strtoul(argv[1], &end, 16);
201                 if (end == (argv[1] + strlen(argv[1])))
202                         load_addr = addr;
203                 else
204                         copy_filename(net_boot_file_name, argv[1],
205                                       sizeof(net_boot_file_name));
206                 break;
207
208         case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
209                 copy_filename(net_boot_file_name, argv[2],
210                               sizeof(net_boot_file_name));
211
212                 break;
213
214 #ifdef CONFIG_CMD_TFTPPUT
215         case 4:
216                 if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
217                         strict_strtoul(argv[2], 16, &save_size) < 0) {
218                         printf("Invalid address/size\n");
219                         return CMD_RET_USAGE;
220                 }
221                 copy_filename(net_boot_file_name, argv[3],
222                               sizeof(net_boot_file_name));
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 CMD_RET_FAILURE;
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 CMD_RET_SUCCESS;
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 == CMD_RET_SUCCESS)
254                 bootstage_mark(BOOTSTAGE_ID_NET_DONE);
255         else
256                 bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
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 CMD_RET_USAGE;
265
266         net_ping_ip = string_to_ip(argv[1]);
267         if (net_ping_ip.s_addr == 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 CMD_RET_FAILURE;
273         }
274
275         printf("host %s is alive\n", argv[1]);
276
277         return CMD_RET_SUCCESS;
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 (cdp_appliance_vlan != htons(-1)) {
294                 printf("CDP offered appliance VLAN %d\n",
295                        ntohs(cdp_appliance_vlan));
296                 VLAN_to_string(cdp_appliance_vlan, tmp);
297                 setenv("vlan", tmp);
298                 NetOurVLAN = cdp_appliance_vlan;
299         }
300
301         if (cdp_native_vlan != htons(-1)) {
302                 printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
303                 VLAN_to_string(cdp_native_vlan, tmp);
304                 setenv("nvlan", tmp);
305                 NetOurNativeVLAN = cdp_native_vlan;
306         }
307
308 }
309
310 int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
311 {
312         int r;
313
314         r = NetLoop(CDP);
315         if (r < 0) {
316                 printf("cdp failed; perhaps not a CISCO switch?\n");
317                 return CMD_RET_FAILURE;
318         }
319
320         cdp_update_env();
321
322         return CMD_RET_SUCCESS;
323 }
324
325 U_BOOT_CMD(
326         cdp,    1,      1,      do_cdp,
327         "Perform CDP network configuration",
328         "\n"
329 );
330 #endif
331
332 #if defined(CONFIG_CMD_SNTP)
333 int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
334 {
335         char *toff;
336
337         if (argc < 2) {
338                 net_ntp_server = getenv_ip("ntpserverip");
339                 if (net_ntp_server.s_addr == 0) {
340                         printf("ntpserverip not set\n");
341                         return CMD_RET_FAILURE;
342                 }
343         } else {
344                 net_ntp_server = string_to_ip(argv[1]);
345                 if (net_ntp_server.s_addr == 0) {
346                         printf("Bad NTP server IP address\n");
347                         return CMD_RET_FAILURE;
348                 }
349         }
350
351         toff = getenv("timeoffset");
352         if (toff == NULL)
353                 NetTimeOffset = 0;
354         else
355                 NetTimeOffset = simple_strtol(toff, NULL, 10);
356
357         if (NetLoop(SNTP) < 0) {
358                 printf("SNTP failed: host %pI4 not responding\n",
359                         &net_ntp_server);
360                 return CMD_RET_FAILURE;
361         }
362
363         return CMD_RET_SUCCESS;
364 }
365
366 U_BOOT_CMD(
367         sntp,   2,      1,      do_sntp,
368         "synchronize RTC via network",
369         "[NTP server IP]\n"
370 );
371 #endif
372
373 #if defined(CONFIG_CMD_DNS)
374 int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
375 {
376         if (argc == 1)
377                 return CMD_RET_USAGE;
378
379         /*
380          * We should check for a valid hostname:
381          * - Each label must be between 1 and 63 characters long
382          * - the entire hostname has a maximum of 255 characters
383          * - only the ASCII letters 'a' through 'z' (case-insensitive),
384          *   the digits '0' through '9', and the hyphen
385          * - cannot begin or end with a hyphen
386          * - no other symbols, punctuation characters, or blank spaces are
387          *   permitted
388          * but hey - this is a minimalist implmentation, so only check length
389          * and let the name server deal with things.
390          */
391         if (strlen(argv[1]) >= 255) {
392                 printf("dns error: hostname too long\n");
393                 return CMD_RET_FAILURE;
394         }
395
396         NetDNSResolve = argv[1];
397
398         if (argc == 3)
399                 NetDNSenvvar = argv[2];
400         else
401                 NetDNSenvvar = NULL;
402
403         if (NetLoop(DNS) < 0) {
404                 printf("dns lookup of %s failed, check setup\n", argv[1]);
405                 return CMD_RET_FAILURE;
406         }
407
408         return CMD_RET_SUCCESS;
409 }
410
411 U_BOOT_CMD(
412         dns,    3,      1,      do_dns,
413         "lookup the IP of a hostname",
414         "hostname [envvar]"
415 );
416
417 #endif  /* CONFIG_CMD_DNS */
418
419 #if defined(CONFIG_CMD_LINK_LOCAL)
420 static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
421                         char * const argv[])
422 {
423         char tmp[22];
424
425         if (NetLoop(LINKLOCAL) < 0)
426                 return CMD_RET_FAILURE;
427
428         net_gateway.s_addr = 0;
429         ip_to_string(net_gateway, tmp);
430         setenv("gatewayip", tmp);
431
432         ip_to_string(net_netmask, tmp);
433         setenv("netmask", tmp);
434
435         ip_to_string(net_ip, tmp);
436         setenv("ipaddr", tmp);
437         setenv("llipaddr", tmp); /* store this for next time */
438
439         return CMD_RET_SUCCESS;
440 }
441
442 U_BOOT_CMD(
443         linklocal,      1,      1,      do_link_local,
444         "acquire a network IP address using the link-local protocol",
445         ""
446 );
447
448 #endif  /* CONFIG_CMD_LINK_LOCAL */