blob: 26efc6d20656a80c0f31aa2c052cca8b0eac0661 [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
Jon Loeligerc76fe472007-07-08 18:02:23 -050031#if defined(CONFIG_CMD_NET)
wdenk38635852002-08-27 05:55:31 +000032
wdenk38635852002-08-27 05:55:31 +000033
34extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
35
wdenk3c2b3d42005-04-05 23:32:21 +000036static int netboot_common (proto_t, cmd_tbl_t *, int , char *[]);
wdenk38635852002-08-27 05:55:31 +000037
38int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
39{
40 return netboot_common (BOOTP, cmdtp, argc, argv);
41}
42
wdenk0d498392003-07-01 21:06:45 +000043U_BOOT_CMD(
44 bootp, 3, 1, do_bootp,
wdenkaa5590b2004-06-09 12:42:26 +000045 "bootp\t- boot image via network using BootP/TFTP protocol\n",
wdenk8bde7f72003-06-27 21:31:46 +000046 "[loadAddress] [bootfilename]\n"
47);
48
wdenk38635852002-08-27 05:55:31 +000049int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
50{
51 return netboot_common (TFTP, cmdtp, argc, argv);
52}
53
wdenk0d498392003-07-01 21:06:45 +000054U_BOOT_CMD(
55 tftpboot, 3, 1, do_tftpb,
wdenkeeacb892003-06-28 23:18:28 +000056 "tftpboot- boot image via network using TFTP protocol\n",
wdenk8bde7f72003-06-27 21:31:46 +000057 "[loadAddress] [bootfilename]\n"
58);
59
wdenk38635852002-08-27 05:55:31 +000060int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
61{
62 return netboot_common (RARP, cmdtp, argc, argv);
63}
64
wdenk0d498392003-07-01 21:06:45 +000065U_BOOT_CMD(
66 rarpboot, 3, 1, do_rarpb,
wdenk8bde7f72003-06-27 21:31:46 +000067 "rarpboot- boot image via network using RARP/TFTP protocol\n",
68 "[loadAddress] [bootfilename]\n"
69);
70
Jon Loeligerc76fe472007-07-08 18:02:23 -050071#if defined(CONFIG_CMD_DHCP)
wdenk38635852002-08-27 05:55:31 +000072int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
73{
74 return netboot_common(DHCP, cmdtp, argc, argv);
75}
wdenk8bde7f72003-06-27 21:31:46 +000076
wdenk0d498392003-07-01 21:06:45 +000077U_BOOT_CMD(
78 dhcp, 3, 1, do_dhcp,
wdenkaa5590b2004-06-09 12:42:26 +000079 "dhcp\t- invoke DHCP client to obtain IP/boot params\n",
wdenk8bde7f72003-06-27 21:31:46 +000080 "\n"
81);
Jon Loeliger90253172007-07-10 11:02:44 -050082#endif
wdenk38635852002-08-27 05:55:31 +000083
Jon Loeligerc76fe472007-07-08 18:02:23 -050084#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +000085int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
86{
87 return netboot_common(NFS, cmdtp, argc, argv);
88}
89
90U_BOOT_CMD(
91 nfs, 3, 1, do_nfs,
wdenkaa5590b2004-06-09 12:42:26 +000092 "nfs\t- boot image via network using NFS protocol\n",
wdenkcbd8a352004-02-24 02:00:03 +000093 "[loadAddress] [host ip addr:bootfilename]\n"
94);
Jon Loeliger90253172007-07-10 11:02:44 -050095#endif
wdenkcbd8a352004-02-24 02:00:03 +000096
wdenk6e592382004-04-18 17:39:38 +000097static void netboot_update_env (void)
wdenk38635852002-08-27 05:55:31 +000098{
wdenk6e592382004-04-18 17:39:38 +000099 char tmp[22];
wdenk38635852002-08-27 05:55:31 +0000100
wdenk6e592382004-04-18 17:39:38 +0000101 if (NetOurGatewayIP) {
102 ip_to_string (NetOurGatewayIP, tmp);
103 setenv ("gatewayip", tmp);
104 }
wdenk38635852002-08-27 05:55:31 +0000105
wdenk6e592382004-04-18 17:39:38 +0000106 if (NetOurSubnetMask) {
107 ip_to_string (NetOurSubnetMask, tmp);
108 setenv ("netmask", tmp);
109 }
wdenk38635852002-08-27 05:55:31 +0000110
wdenk6e592382004-04-18 17:39:38 +0000111 if (NetOurHostName[0])
112 setenv ("hostname", NetOurHostName);
wdenk38635852002-08-27 05:55:31 +0000113
wdenk6e592382004-04-18 17:39:38 +0000114 if (NetOurRootPath[0])
115 setenv ("rootpath", NetOurRootPath);
wdenk38635852002-08-27 05:55:31 +0000116
wdenk6e592382004-04-18 17:39:38 +0000117 if (NetOurIP) {
118 ip_to_string (NetOurIP, tmp);
119 setenv ("ipaddr", tmp);
120 }
wdenk38635852002-08-27 05:55:31 +0000121
wdenk6e592382004-04-18 17:39:38 +0000122 if (NetServerIP) {
123 ip_to_string (NetServerIP, tmp);
124 setenv ("serverip", tmp);
125 }
wdenk38635852002-08-27 05:55:31 +0000126
wdenk6e592382004-04-18 17:39:38 +0000127 if (NetOurDNSIP) {
128 ip_to_string (NetOurDNSIP, tmp);
129 setenv ("dnsip", tmp);
130 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500131#if defined(CONFIG_BOOTP_DNS2)
wdenk6e592382004-04-18 17:39:38 +0000132 if (NetOurDNS2IP) {
133 ip_to_string (NetOurDNS2IP, tmp);
134 setenv ("dnsip2", tmp);
135 }
stroesefe389a82003-08-28 14:17:32 +0000136#endif
wdenk6e592382004-04-18 17:39:38 +0000137 if (NetOurNISDomain[0])
138 setenv ("domain", NetOurNISDomain);
wdenkea287de2005-04-01 00:25:43 +0000139
Jon Loeligerc76fe472007-07-08 18:02:23 -0500140#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500141 && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000142 if (NetTimeOffset) {
143 sprintf (tmp, "%d", NetTimeOffset);
144 setenv ("timeoffset", tmp);
145 }
146#endif
Jon Loeligerc76fe472007-07-08 18:02:23 -0500147#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500148 && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-04-01 00:25:43 +0000149 if (NetNtpServerIP) {
150 ip_to_string (NetNtpServerIP, tmp);
151 setenv ("ntpserverip", tmp);
152 }
153#endif
wdenk38635852002-08-27 05:55:31 +0000154}
wdenk6e592382004-04-18 17:39:38 +0000155
wdenk38635852002-08-27 05:55:31 +0000156static int
wdenk3c2b3d42005-04-05 23:32:21 +0000157netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
wdenk38635852002-08-27 05:55:31 +0000158{
159 char *s;
160 int rcode = 0;
161 int size;
162
163 /* pre-set load_addr */
164 if ((s = getenv("loadaddr")) != NULL) {
165 load_addr = simple_strtoul(s, NULL, 16);
166 }
167
168 switch (argc) {
169 case 1:
170 break;
171
172 case 2: /* only one arg - accept two forms:
173 * just load address, or just boot file name.
174 * The latter form must be written "filename" here.
175 */
176 if (argv[1][0] == '"') { /* just boot filename */
177 copy_filename (BootFile, argv[1], sizeof(BootFile));
178 } else { /* load address */
179 load_addr = simple_strtoul(argv[1], NULL, 16);
180 }
181 break;
182
183 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
184 copy_filename (BootFile, argv[2], sizeof(BootFile));
185
186 break;
187
188 default: printf ("Usage:\n%s\n", cmdtp->usage);
189 return 1;
190 }
191
wdenkeb9401e2002-11-11 02:11:37 +0000192 if ((size = NetLoop(proto)) < 0)
wdenk38635852002-08-27 05:55:31 +0000193 return 1;
194
195 /* NetLoop ok, update environment */
196 netboot_update_env();
197
wdenkeb9401e2002-11-11 02:11:37 +0000198 /* done if no file was loaded (no errors though) */
199 if (size == 0)
200 return 0;
201
wdenk38635852002-08-27 05:55:31 +0000202 /* flush cache */
203 flush_cache(load_addr, size);
204
205 /* Loading ok, check if we should attempt an auto-start */
206 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
207 char *local_args[2];
208 local_args[0] = argv[0];
209 local_args[1] = NULL;
210
211 printf ("Automatic boot of image at addr 0x%08lX ...\n",
212 load_addr);
213 rcode = do_bootm (cmdtp, 0, 1, local_args);
214 }
215
216#ifdef CONFIG_AUTOSCRIPT
217 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
218 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
219 rcode = autoscript (load_addr);
220 }
221#endif
222 return rcode;
223}
224
Jon Loeligerc76fe472007-07-08 18:02:23 -0500225#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000226int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
227{
228 if (argc < 2)
229 return -1;
230
231 NetPingIP = string_to_ip(argv[1]);
232 if (NetPingIP == 0) {
233 printf ("Usage:\n%s\n", cmdtp->usage);
234 return -1;
235 }
236
237 if (NetLoop(PING) < 0) {
238 printf("ping failed; host %s is not alive\n", argv[1]);
239 return 1;
240 }
241
242 printf("host %s is alive\n", argv[1]);
243
244 return 0;
245}
wdenk6dff5522003-07-15 07:45:49 +0000246
247U_BOOT_CMD(
248 ping, 2, 1, do_ping,
wdenkaa5590b2004-06-09 12:42:26 +0000249 "ping\t- send ICMP ECHO_REQUEST to network host\n",
wdenk6dff5522003-07-15 07:45:49 +0000250 "pingAddress\n"
251);
Jon Loeliger90253172007-07-10 11:02:44 -0500252#endif
wdenk73a8b272003-06-05 19:27:42 +0000253
Jon Loeligerc76fe472007-07-08 18:02:23 -0500254#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000255
256static void cdp_update_env(void)
257{
258 char tmp[16];
259
260 if (CDPApplianceVLAN != htons(-1)) {
261 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
262 VLAN_to_string(CDPApplianceVLAN, tmp);
263 setenv("vlan", tmp);
264 NetOurVLAN = CDPApplianceVLAN;
265 }
266
267 if (CDPNativeVLAN != htons(-1)) {
268 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
269 VLAN_to_string(CDPNativeVLAN, tmp);
270 setenv("nvlan", tmp);
271 NetOurNativeVLAN = CDPNativeVLAN;
272 }
273
274}
275
276int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
277{
278 int r;
279
280 r = NetLoop(CDP);
281 if (r < 0) {
282 printf("cdp failed; perhaps not a CISCO switch?\n");
283 return 1;
284 }
285
286 cdp_update_env();
287
288 return 0;
289}
290
291U_BOOT_CMD(
292 cdp, 1, 1, do_cdp,
wdenkaa5590b2004-06-09 12:42:26 +0000293 "cdp\t- Perform CDP network configuration\n",
wdenka3d991b2004-04-15 21:48:45 +0000294);
Jon Loeliger90253172007-07-10 11:02:44 -0500295#endif
wdenka3d991b2004-04-15 21:48:45 +0000296
Jon Loeligerc76fe472007-07-08 18:02:23 -0500297#if defined(CONFIG_CMD_SNTP)
wdenkea287de2005-04-01 00:25:43 +0000298int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
299{
300 char *toff;
301
302 if (argc < 2) {
303 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
304 if (NetNtpServerIP == 0) {
305 printf ("ntpserverip not set\n");
306 return (1);
307 }
308 } else {
309 NetNtpServerIP = string_to_ip(argv[1]);
310 if (NetNtpServerIP == 0) {
311 printf ("Bad NTP server IP address\n");
312 return (1);
313 }
314 }
315
316 toff = getenv ("timeoffset");
317 if (toff == NULL) NetTimeOffset = 0;
318 else NetTimeOffset = simple_strtol (toff, NULL, 10);
319
320 if (NetLoop(SNTP) < 0) {
321 printf("SNTP failed: host %s not responding\n", argv[1]);
322 return 1;
323 }
324
325 return 0;
326}
327
328U_BOOT_CMD(
329 sntp, 2, 1, do_sntp,
330 "sntp\t- synchronize RTC via network\n",
331 "[NTP server IP]\n"
332);
Jon Loeliger90253172007-07-10 11:02:44 -0500333#endif
wdenkea287de2005-04-01 00:25:43 +0000334
Jon Loeliger90253172007-07-10 11:02:44 -0500335#endif