blob: 68183c49af9233666705f2025fad4ec0ccebb84b [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
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>
wdenk38635852002-08-27 05:55:31 +000029#include <net.h>
30
wdenk38635852002-08-27 05:55:31 +000031extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
32
wdenke085e5b2005-04-05 23:32:21 +000033static int netboot_common (proto_t, cmd_tbl_t *, int , char *[]);
wdenk38635852002-08-27 05:55:31 +000034
35int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
36{
37 return netboot_common (BOOTP, cmdtp, argc, argv);
38}
39
wdenkf287a242003-07-01 21:06:45 +000040U_BOOT_CMD(
41 bootp, 3, 1, do_bootp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060042 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020043 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000044);
45
wdenk38635852002-08-27 05:55:31 +000046int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
47{
48 return netboot_common (TFTP, cmdtp, argc, argv);
49}
50
wdenkf287a242003-07-01 21:06:45 +000051U_BOOT_CMD(
52 tftpboot, 3, 1, do_tftpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060053 "boot image via network using TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020054 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000055);
56
wdenk38635852002-08-27 05:55:31 +000057int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
58{
59 return netboot_common (RARP, cmdtp, argc, argv);
60}
61
wdenkf287a242003-07-01 21:06:45 +000062U_BOOT_CMD(
63 rarpboot, 3, 1, do_rarpb,
Peter Tyserdfb72b82009-01-27 18:03:12 -060064 "boot image via network using RARP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020065 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000066);
67
Jon Loeligerd76b5c12007-07-08 18:02:23 -050068#if defined(CONFIG_CMD_DHCP)
wdenk38635852002-08-27 05:55:31 +000069int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
70{
71 return netboot_common(DHCP, cmdtp, argc, argv);
72}
wdenk57b2d802003-06-27 21:31:46 +000073
wdenkf287a242003-07-01 21:06:45 +000074U_BOOT_CMD(
75 dhcp, 3, 1, do_dhcp,
Peter Tyserdfb72b82009-01-27 18:03:12 -060076 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020077 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk57b2d802003-06-27 21:31:46 +000078);
Jon Loeligerd704d912007-07-10 11:02:44 -050079#endif
wdenk38635852002-08-27 05:55:31 +000080
Jon Loeligerd76b5c12007-07-08 18:02:23 -050081#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +000082int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
83{
84 return netboot_common(NFS, cmdtp, argc, argv);
85}
86
87U_BOOT_CMD(
88 nfs, 3, 1, do_nfs,
Peter Tyserdfb72b82009-01-27 18:03:12 -060089 "boot image via network using NFS protocol",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020090 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkbe9c1cb2004-02-24 02:00:03 +000091);
Jon Loeligerd704d912007-07-10 11:02:44 -050092#endif
wdenkbe9c1cb2004-02-24 02:00:03 +000093
wdenk05939202004-04-18 17:39:38 +000094static void netboot_update_env (void)
wdenk38635852002-08-27 05:55:31 +000095{
wdenk05939202004-04-18 17:39:38 +000096 char tmp[22];
wdenk38635852002-08-27 05:55:31 +000097
wdenk05939202004-04-18 17:39:38 +000098 if (NetOurGatewayIP) {
99 ip_to_string (NetOurGatewayIP, tmp);
100 setenv ("gatewayip", tmp);
101 }
wdenk38635852002-08-27 05:55:31 +0000102
wdenk05939202004-04-18 17:39:38 +0000103 if (NetOurSubnetMask) {
104 ip_to_string (NetOurSubnetMask, tmp);
105 setenv ("netmask", tmp);
106 }
wdenk38635852002-08-27 05:55:31 +0000107
wdenk05939202004-04-18 17:39:38 +0000108 if (NetOurHostName[0])
109 setenv ("hostname", NetOurHostName);
wdenk38635852002-08-27 05:55:31 +0000110
wdenk05939202004-04-18 17:39:38 +0000111 if (NetOurRootPath[0])
112 setenv ("rootpath", NetOurRootPath);
wdenk38635852002-08-27 05:55:31 +0000113
wdenk05939202004-04-18 17:39:38 +0000114 if (NetOurIP) {
115 ip_to_string (NetOurIP, tmp);
116 setenv ("ipaddr", tmp);
117 }
wdenk38635852002-08-27 05:55:31 +0000118
wdenk05939202004-04-18 17:39:38 +0000119 if (NetServerIP) {
120 ip_to_string (NetServerIP, tmp);
121 setenv ("serverip", tmp);
122 }
wdenk38635852002-08-27 05:55:31 +0000123
wdenk05939202004-04-18 17:39:38 +0000124 if (NetOurDNSIP) {
125 ip_to_string (NetOurDNSIP, tmp);
126 setenv ("dnsip", tmp);
127 }
Jon Loeliger5336a762007-07-09 22:08:34 -0500128#if defined(CONFIG_BOOTP_DNS2)
wdenk05939202004-04-18 17:39:38 +0000129 if (NetOurDNS2IP) {
130 ip_to_string (NetOurDNS2IP, tmp);
131 setenv ("dnsip2", tmp);
132 }
stroesee0aadfb2003-08-28 14:17:32 +0000133#endif
wdenk05939202004-04-18 17:39:38 +0000134 if (NetOurNISDomain[0])
135 setenv ("domain", NetOurNISDomain);
wdenkb4ad9622005-04-01 00:25:43 +0000136
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500137#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger5336a762007-07-09 22:08:34 -0500138 && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkb4ad9622005-04-01 00:25:43 +0000139 if (NetTimeOffset) {
140 sprintf (tmp, "%d", NetTimeOffset);
141 setenv ("timeoffset", tmp);
142 }
143#endif
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500144#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger5336a762007-07-09 22:08:34 -0500145 && defined(CONFIG_BOOTP_NTPSERVER)
wdenkb4ad9622005-04-01 00:25:43 +0000146 if (NetNtpServerIP) {
147 ip_to_string (NetNtpServerIP, tmp);
148 setenv ("ntpserverip", tmp);
149 }
150#endif
wdenk38635852002-08-27 05:55:31 +0000151}
wdenk05939202004-04-18 17:39:38 +0000152
wdenk38635852002-08-27 05:55:31 +0000153static int
wdenke085e5b2005-04-05 23:32:21 +0000154netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
wdenk38635852002-08-27 05:55:31 +0000155{
156 char *s;
Peter Tyser24c297d2008-12-02 12:59:51 -0600157 char *end;
wdenk38635852002-08-27 05:55:31 +0000158 int rcode = 0;
159 int size;
Peter Tyser24c297d2008-12-02 12:59:51 -0600160 ulong addr;
wdenk38635852002-08-27 05:55:31 +0000161
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
Peter Tyser24c297d2008-12-02 12:59:51 -0600171 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.
wdenk38635852002-08-27 05:55:31 +0000176 */
Peter Tyser24c297d2008-12-02 12:59:51 -0600177 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));
wdenk38635852002-08-27 05:55:31 +0000182 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
Peter Tyserddb3af92009-01-27 18:03:10 -0600189 default: cmd_usage(cmdtp);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200190 show_boot_progress (-80);
wdenk38635852002-08-27 05:55:31 +0000191 return 1;
192 }
193
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200194 show_boot_progress (80);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200195 if ((size = NetLoop(proto)) < 0) {
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200196 show_boot_progress (-81);
wdenk38635852002-08-27 05:55:31 +0000197 return 1;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200198 }
wdenk38635852002-08-27 05:55:31 +0000199
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200200 show_boot_progress (81);
wdenk38635852002-08-27 05:55:31 +0000201 /* NetLoop ok, update environment */
202 netboot_update_env();
203
wdenkc217f6d2002-11-11 02:11:37 +0000204 /* done if no file was loaded (no errors though) */
Heiko Schocher633e03a2007-06-22 19:11:54 +0200205 if (size == 0) {
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200206 show_boot_progress (-82);
wdenkc217f6d2002-11-11 02:11:37 +0000207 return 0;
Heiko Schocher633e03a2007-06-22 19:11:54 +0200208 }
wdenkc217f6d2002-11-11 02:11:37 +0000209
wdenk38635852002-08-27 05:55:31 +0000210 /* 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);
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200221 show_boot_progress (82);
wdenk38635852002-08-27 05:55:31 +0000222 rcode = do_bootm (cmdtp, 0, 1, local_args);
223 }
224
Wolfgang Denk85c25df2009-04-01 23:34:12 +0200225#ifdef CONFIG_SOURCE
wdenk38635852002-08-27 05:55:31 +0000226 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
Wolfgang Denk4f44a6b2009-04-04 16:57:50 +0200227 printf ("Running \"source\" command at addr 0x%08lX",
Wolfgang Denk85c25df2009-04-01 23:34:12 +0200228 load_addr);
Marian Balakowicz23b77a22008-03-12 10:33:00 +0100229
230 s = getenv ("autoscript_uname");
231 if (s)
232 printf (":%s ...\n", s);
233 else
234 puts (" ...\n");
235
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200236 show_boot_progress (83);
Wolfgang Denk85c25df2009-04-01 23:34:12 +0200237 rcode = source (load_addr, s);
wdenk38635852002-08-27 05:55:31 +0000238 }
239#endif
Heiko Schocher633e03a2007-06-22 19:11:54 +0200240 if (rcode < 0)
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200241 show_boot_progress (-83);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200242 else
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200243 show_boot_progress (84);
wdenk38635852002-08-27 05:55:31 +0000244 return rcode;
245}
wdenke6466f62003-06-05 19:27:42 +0000246
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500247#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000248int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
249{
250 if (argc < 2)
251 return -1;
252
253 NetPingIP = string_to_ip(argv[1]);
254 if (NetPingIP == 0) {
Peter Tyserddb3af92009-01-27 18:03:10 -0600255 cmd_usage(cmdtp);
wdenke6466f62003-06-05 19:27:42 +0000256 return -1;
257 }
258
259 if (NetLoop(PING) < 0) {
260 printf("ping failed; host %s is not alive\n", argv[1]);
261 return 1;
262 }
263
264 printf("host %s is alive\n", argv[1]);
265
266 return 0;
267}
wdenkca9bc762003-07-15 07:45:49 +0000268
269U_BOOT_CMD(
270 ping, 2, 1, do_ping,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600271 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200272 "pingAddress"
wdenkca9bc762003-07-15 07:45:49 +0000273);
Jon Loeligerd704d912007-07-10 11:02:44 -0500274#endif
wdenk38635852002-08-27 05:55:31 +0000275
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500276#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000277
278static void cdp_update_env(void)
279{
280 char tmp[16];
281
282 if (CDPApplianceVLAN != htons(-1)) {
283 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
284 VLAN_to_string(CDPApplianceVLAN, tmp);
285 setenv("vlan", tmp);
286 NetOurVLAN = CDPApplianceVLAN;
287 }
288
289 if (CDPNativeVLAN != htons(-1)) {
290 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
291 VLAN_to_string(CDPNativeVLAN, tmp);
292 setenv("nvlan", tmp);
293 NetOurNativeVLAN = CDPNativeVLAN;
294 }
295
296}
297
298int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
299{
300 int r;
301
302 r = NetLoop(CDP);
303 if (r < 0) {
304 printf("cdp failed; perhaps not a CISCO switch?\n");
305 return 1;
306 }
307
308 cdp_update_env();
309
310 return 0;
311}
312
313U_BOOT_CMD(
314 cdp, 1, 1, do_cdp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600315 "Perform CDP network configuration",
wdenk145d2c12004-04-15 21:48:45 +0000316);
Jon Loeligerd704d912007-07-10 11:02:44 -0500317#endif
wdenk145d2c12004-04-15 21:48:45 +0000318
Jon Loeligerd76b5c12007-07-08 18:02:23 -0500319#if defined(CONFIG_CMD_SNTP)
wdenkb4ad9622005-04-01 00:25:43 +0000320int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
321{
322 char *toff;
323
324 if (argc < 2) {
325 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
326 if (NetNtpServerIP == 0) {
327 printf ("ntpserverip not set\n");
328 return (1);
329 }
330 } else {
331 NetNtpServerIP = string_to_ip(argv[1]);
332 if (NetNtpServerIP == 0) {
333 printf ("Bad NTP server IP address\n");
334 return (1);
335 }
336 }
337
338 toff = getenv ("timeoffset");
339 if (toff == NULL) NetTimeOffset = 0;
340 else NetTimeOffset = simple_strtol (toff, NULL, 10);
341
342 if (NetLoop(SNTP) < 0) {
343 printf("SNTP failed: host %s not responding\n", argv[1]);
344 return 1;
345 }
346
347 return 0;
348}
349
350U_BOOT_CMD(
351 sntp, 2, 1, do_sntp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600352 "synchronize RTC via network",
wdenkb4ad9622005-04-01 00:25:43 +0000353 "[NTP server IP]\n"
354);
Jon Loeligerd704d912007-07-10 11:02:44 -0500355#endif