]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_net.c
[PCS440EP] - The DIAG LEDs are now blinking, if an error occur
[people/ms/u-boot.git] / common / cmd_net.c
CommitLineData
3863585b
WD
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>
3863585b
WD
29#include <net.h>
30
31#if (CONFIG_COMMANDS & CFG_CMD_NET)
32
566a494f
HS
33#ifdef CONFIG_SHOW_BOOT_PROGRESS
34# include <status_led.h>
35extern 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
3863585b
WD
40
41extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
42
3c2b3d45 43static int netboot_common (proto_t, cmd_tbl_t *, int , char *[]);
3863585b
WD
44
45int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
46{
47 return netboot_common (BOOTP, cmdtp, argc, argv);
48}
49
0d498393
WD
50U_BOOT_CMD(
51 bootp, 3, 1, do_bootp,
aa5590b6 52 "bootp\t- boot image via network using BootP/TFTP protocol\n",
8bde7f77
WD
53 "[loadAddress] [bootfilename]\n"
54);
55
3863585b
WD
56int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
57{
58 return netboot_common (TFTP, cmdtp, argc, argv);
59}
60
0d498393
WD
61U_BOOT_CMD(
62 tftpboot, 3, 1, do_tftpb,
eeacb89c 63 "tftpboot- boot image via network using TFTP protocol\n",
8bde7f77
WD
64 "[loadAddress] [bootfilename]\n"
65);
66
3863585b
WD
67int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
68{
69 return netboot_common (RARP, cmdtp, argc, argv);
70}
71
0d498393
WD
72U_BOOT_CMD(
73 rarpboot, 3, 1, do_rarpb,
8bde7f77
WD
74 "rarpboot- boot image via network using RARP/TFTP protocol\n",
75 "[loadAddress] [bootfilename]\n"
76);
77
3863585b
WD
78#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
79int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
80{
81 return netboot_common(DHCP, cmdtp, argc, argv);
82}
8bde7f77 83
0d498393
WD
84U_BOOT_CMD(
85 dhcp, 3, 1, do_dhcp,
aa5590b6 86 "dhcp\t- invoke DHCP client to obtain IP/boot params\n",
8bde7f77
WD
87 "\n"
88);
3863585b
WD
89#endif /* CFG_CMD_DHCP */
90
cbd8a35c
WD
91#if (CONFIG_COMMANDS & CFG_CMD_NFS)
92int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
93{
94 return netboot_common(NFS, cmdtp, argc, argv);
95}
96
97U_BOOT_CMD(
98 nfs, 3, 1, do_nfs,
aa5590b6 99 "nfs\t- boot image via network using NFS protocol\n",
cbd8a35c
WD
100 "[loadAddress] [host ip addr:bootfilename]\n"
101);
102#endif /* CFG_CMD_NFS */
103
6e592385 104static void netboot_update_env (void)
3863585b 105{
6e592385 106 char tmp[22];
3863585b 107
6e592385
WD
108 if (NetOurGatewayIP) {
109 ip_to_string (NetOurGatewayIP, tmp);
110 setenv ("gatewayip", tmp);
111 }
3863585b 112
6e592385
WD
113 if (NetOurSubnetMask) {
114 ip_to_string (NetOurSubnetMask, tmp);
115 setenv ("netmask", tmp);
116 }
3863585b 117
6e592385
WD
118 if (NetOurHostName[0])
119 setenv ("hostname", NetOurHostName);
3863585b 120
6e592385
WD
121 if (NetOurRootPath[0])
122 setenv ("rootpath", NetOurRootPath);
3863585b 123
6e592385
WD
124 if (NetOurIP) {
125 ip_to_string (NetOurIP, tmp);
126 setenv ("ipaddr", tmp);
127 }
3863585b 128
6e592385
WD
129 if (NetServerIP) {
130 ip_to_string (NetServerIP, tmp);
131 setenv ("serverip", tmp);
132 }
73a8b27c 133
6e592385
WD
134 if (NetOurDNSIP) {
135 ip_to_string (NetOurDNSIP, tmp);
136 setenv ("dnsip", tmp);
137 }
fe389a82 138#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
6e592385
WD
139 if (NetOurDNS2IP) {
140 ip_to_string (NetOurDNS2IP, tmp);
141 setenv ("dnsip2", tmp);
142 }
fe389a82 143#endif
6e592385
WD
144 if (NetOurNISDomain[0])
145 setenv ("domain", NetOurNISDomain);
ea287deb 146
414eec35 147#if (CONFIG_COMMANDS & CFG_CMD_SNTP) && (CONFIG_BOOTP_MASK & CONFIG_BOOTP_TIMEOFFSET)
ea287deb
WD
148 if (NetTimeOffset) {
149 sprintf (tmp, "%d", NetTimeOffset);
150 setenv ("timeoffset", tmp);
151 }
152#endif
414eec35 153#if (CONFIG_COMMANDS & CFG_CMD_SNTP) && (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NTPSERVER)
ea287deb
WD
154 if (NetNtpServerIP) {
155 ip_to_string (NetNtpServerIP, tmp);
156 setenv ("ntpserverip", tmp);
157 }
158#endif
3863585b 159}
6e592385 160
3863585b 161static int
3c2b3d45 162netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
3863585b
WD
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);
566a494f 194 SHOW_BOOT_PROGRESS(-80);
3863585b
WD
195 return 1;
196 }
197
566a494f
HS
198 SHOW_BOOT_PROGRESS(80);
199 if ((size = NetLoop(proto)) < 0) {
200 SHOW_BOOT_PROGRESS(-81);
3863585b 201 return 1;
566a494f 202 }
3863585b 203
566a494f 204 SHOW_BOOT_PROGRESS(81);
3863585b
WD
205 /* NetLoop ok, update environment */
206 netboot_update_env();
207
eb9401e3 208 /* done if no file was loaded (no errors though) */
566a494f
HS
209 if (size == 0) {
210 SHOW_BOOT_PROGRESS(-82);
eb9401e3 211 return 0;
566a494f 212 }
eb9401e3 213
3863585b
WD
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);
566a494f 225 SHOW_BOOT_PROGRESS(82);
3863585b
WD
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);
566a494f 232 SHOW_BOOT_PROGRESS(83);
3863585b
WD
233 rcode = autoscript (load_addr);
234 }
566a494f
HS
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);
3863585b
WD
241#endif
242 return rcode;
243}
244
73a8b27c
WD
245#if (CONFIG_COMMANDS & CFG_CMD_PING)
246int 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}
6dff5529
WD
266
267U_BOOT_CMD(
268 ping, 2, 1, do_ping,
aa5590b6 269 "ping\t- send ICMP ECHO_REQUEST to network host\n",
6dff5529
WD
270 "pingAddress\n"
271);
73a8b27c
WD
272#endif /* CFG_CMD_PING */
273
a3d991bd
WD
274#if (CONFIG_COMMANDS & CFG_CMD_CDP)
275
276static 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
296int 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
311U_BOOT_CMD(
312 cdp, 1, 1, do_cdp,
aa5590b6 313 "cdp\t- Perform CDP network configuration\n",
a3d991bd
WD
314);
315#endif /* CFG_CMD_CDP */
316
ea287deb
WD
317#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
318int 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
348U_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
3863585b 355#endif /* CFG_CMD_NET */