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