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