]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_net.c
Standardize command usage messages with cmd_usage()
[people/ms/u-boot.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 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
32
33 static int netboot_common (proto_t, cmd_tbl_t *, int , char *[]);
34
35 int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
36 {
37 return netboot_common (BOOTP, cmdtp, argc, argv);
38 }
39
40 U_BOOT_CMD(
41 bootp, 3, 1, do_bootp,
42 "bootp\t- boot image via network using BOOTP/TFTP protocol\n",
43 "[loadAddress] [[hostIPaddr:]bootfilename]\n"
44 );
45
46 int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
47 {
48 return netboot_common (TFTP, cmdtp, argc, argv);
49 }
50
51 U_BOOT_CMD(
52 tftpboot, 3, 1, do_tftpb,
53 "tftpboot- boot image via network using TFTP protocol\n",
54 "[loadAddress] [[hostIPaddr:]bootfilename]\n"
55 );
56
57 int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
58 {
59 return netboot_common (RARP, cmdtp, argc, argv);
60 }
61
62 U_BOOT_CMD(
63 rarpboot, 3, 1, do_rarpb,
64 "rarpboot- boot image via network using RARP/TFTP protocol\n",
65 "[loadAddress] [[hostIPaddr:]bootfilename]\n"
66 );
67
68 #if defined(CONFIG_CMD_DHCP)
69 int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
70 {
71 return netboot_common(DHCP, cmdtp, argc, argv);
72 }
73
74 U_BOOT_CMD(
75 dhcp, 3, 1, do_dhcp,
76 "dhcp\t- boot image via network using DHCP/TFTP protocol\n",
77 "[loadAddress] [[hostIPaddr:]bootfilename]\n"
78 );
79 #endif
80
81 #if defined(CONFIG_CMD_NFS)
82 int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
83 {
84 return netboot_common(NFS, cmdtp, argc, argv);
85 }
86
87 U_BOOT_CMD(
88 nfs, 3, 1, do_nfs,
89 "nfs\t- boot image via network using NFS protocol\n",
90 "[loadAddress] [[hostIPaddr:]bootfilename]\n"
91 );
92 #endif
93
94 static void netboot_update_env (void)
95 {
96 char tmp[22];
97
98 if (NetOurGatewayIP) {
99 ip_to_string (NetOurGatewayIP, tmp);
100 setenv ("gatewayip", tmp);
101 }
102
103 if (NetOurSubnetMask) {
104 ip_to_string (NetOurSubnetMask, tmp);
105 setenv ("netmask", tmp);
106 }
107
108 if (NetOurHostName[0])
109 setenv ("hostname", NetOurHostName);
110
111 if (NetOurRootPath[0])
112 setenv ("rootpath", NetOurRootPath);
113
114 if (NetOurIP) {
115 ip_to_string (NetOurIP, tmp);
116 setenv ("ipaddr", tmp);
117 }
118
119 if (NetServerIP) {
120 ip_to_string (NetServerIP, tmp);
121 setenv ("serverip", tmp);
122 }
123
124 if (NetOurDNSIP) {
125 ip_to_string (NetOurDNSIP, tmp);
126 setenv ("dnsip", tmp);
127 }
128 #if defined(CONFIG_BOOTP_DNS2)
129 if (NetOurDNS2IP) {
130 ip_to_string (NetOurDNS2IP, tmp);
131 setenv ("dnsip2", tmp);
132 }
133 #endif
134 if (NetOurNISDomain[0])
135 setenv ("domain", NetOurNISDomain);
136
137 #if defined(CONFIG_CMD_SNTP) \
138 && defined(CONFIG_BOOTP_TIMEOFFSET)
139 if (NetTimeOffset) {
140 sprintf (tmp, "%d", NetTimeOffset);
141 setenv ("timeoffset", tmp);
142 }
143 #endif
144 #if defined(CONFIG_CMD_SNTP) \
145 && defined(CONFIG_BOOTP_NTPSERVER)
146 if (NetNtpServerIP) {
147 ip_to_string (NetNtpServerIP, tmp);
148 setenv ("ntpserverip", tmp);
149 }
150 #endif
151 }
152
153 static int
154 netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
155 {
156 char *s;
157 char *end;
158 int rcode = 0;
159 int size;
160 ulong addr;
161
162 /* pre-set load_addr */
163 if ((s = getenv("loadaddr")) != NULL) {
164 load_addr = simple_strtoul(s, NULL, 16);
165 }
166
167 switch (argc) {
168 case 1:
169 break;
170
171 case 2: /*
172 * Only one arg - accept two forms:
173 * Just load address, or just boot file name. The latter
174 * form must be written in a format which can not be
175 * mis-interpreted as a valid number.
176 */
177 addr = simple_strtoul(argv[1], &end, 16);
178 if (end == (argv[1] + strlen(argv[1])))
179 load_addr = addr;
180 else
181 copy_filename(BootFile, argv[1], sizeof(BootFile));
182 break;
183
184 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
185 copy_filename (BootFile, argv[2], sizeof(BootFile));
186
187 break;
188
189 default: cmd_usage(cmdtp);
190 show_boot_progress (-80);
191 return 1;
192 }
193
194 show_boot_progress (80);
195 if ((size = NetLoop(proto)) < 0) {
196 show_boot_progress (-81);
197 return 1;
198 }
199
200 show_boot_progress (81);
201 /* NetLoop ok, update environment */
202 netboot_update_env();
203
204 /* done if no file was loaded (no errors though) */
205 if (size == 0) {
206 show_boot_progress (-82);
207 return 0;
208 }
209
210 /* flush cache */
211 flush_cache(load_addr, size);
212
213 /* Loading ok, check if we should attempt an auto-start */
214 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
215 char *local_args[2];
216 local_args[0] = argv[0];
217 local_args[1] = NULL;
218
219 printf ("Automatic boot of image at addr 0x%08lX ...\n",
220 load_addr);
221 show_boot_progress (82);
222 rcode = do_bootm (cmdtp, 0, 1, local_args);
223 }
224
225 #ifdef CONFIG_AUTOSCRIPT
226 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
227 printf ("Running autoscript at addr 0x%08lX", load_addr);
228
229 s = getenv ("autoscript_uname");
230 if (s)
231 printf (":%s ...\n", s);
232 else
233 puts (" ...\n");
234
235 show_boot_progress (83);
236 rcode = autoscript (load_addr, s);
237 }
238 #endif
239 if (rcode < 0)
240 show_boot_progress (-83);
241 else
242 show_boot_progress (84);
243 return rcode;
244 }
245
246 #if defined(CONFIG_CMD_PING)
247 int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
248 {
249 if (argc < 2)
250 return -1;
251
252 NetPingIP = string_to_ip(argv[1]);
253 if (NetPingIP == 0) {
254 cmd_usage(cmdtp);
255 return -1;
256 }
257
258 if (NetLoop(PING) < 0) {
259 printf("ping failed; host %s is not alive\n", argv[1]);
260 return 1;
261 }
262
263 printf("host %s is alive\n", argv[1]);
264
265 return 0;
266 }
267
268 U_BOOT_CMD(
269 ping, 2, 1, do_ping,
270 "ping\t- send ICMP ECHO_REQUEST to network host\n",
271 "pingAddress\n"
272 );
273 #endif
274
275 #if defined(CONFIG_CMD_CDP)
276
277 static void cdp_update_env(void)
278 {
279 char tmp[16];
280
281 if (CDPApplianceVLAN != htons(-1)) {
282 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
283 VLAN_to_string(CDPApplianceVLAN, tmp);
284 setenv("vlan", tmp);
285 NetOurVLAN = CDPApplianceVLAN;
286 }
287
288 if (CDPNativeVLAN != htons(-1)) {
289 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
290 VLAN_to_string(CDPNativeVLAN, tmp);
291 setenv("nvlan", tmp);
292 NetOurNativeVLAN = CDPNativeVLAN;
293 }
294
295 }
296
297 int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
298 {
299 int r;
300
301 r = NetLoop(CDP);
302 if (r < 0) {
303 printf("cdp failed; perhaps not a CISCO switch?\n");
304 return 1;
305 }
306
307 cdp_update_env();
308
309 return 0;
310 }
311
312 U_BOOT_CMD(
313 cdp, 1, 1, do_cdp,
314 "cdp\t- Perform CDP network configuration\n",
315 );
316 #endif
317
318 #if defined(CONFIG_CMD_SNTP)
319 int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
320 {
321 char *toff;
322
323 if (argc < 2) {
324 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
325 if (NetNtpServerIP == 0) {
326 printf ("ntpserverip not set\n");
327 return (1);
328 }
329 } else {
330 NetNtpServerIP = string_to_ip(argv[1]);
331 if (NetNtpServerIP == 0) {
332 printf ("Bad NTP server IP address\n");
333 return (1);
334 }
335 }
336
337 toff = getenv ("timeoffset");
338 if (toff == NULL) NetTimeOffset = 0;
339 else NetTimeOffset = simple_strtol (toff, NULL, 10);
340
341 if (NetLoop(SNTP) < 0) {
342 printf("SNTP failed: host %s not responding\n", argv[1]);
343 return 1;
344 }
345
346 return 0;
347 }
348
349 U_BOOT_CMD(
350 sntp, 2, 1, do_sntp,
351 "sntp\t- synchronize RTC via network\n",
352 "[NTP server IP]\n"
353 );
354 #endif