blob: 3cdb07fdc72dc0a88a3b8c8512537017e73909ae [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
Wolfgang Denk54841ab2010-06-28 22:00:46 +020031extern int do_bootm (cmd_tbl_t *, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000032
Wolfgang Denk54841ab2010-06-28 22:00:46 +020033static int netboot_common (proto_t, cmd_tbl_t *, int , char * const []);
wdenk38635852002-08-27 05:55:31 +000034
Wolfgang Denk54841ab2010-06-28 22:00:46 +020035int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000036{
37 return netboot_common (BOOTP, cmdtp, argc, argv);
38}
39
wdenk0d498392003-07-01 21:06:45 +000040U_BOOT_CMD(
41 bootp, 3, 1, do_bootp,
Peter Tyser2fb26042009-01-27 18:03:12 -060042 "boot image via network using BOOTP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020043 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000044);
45
Wolfgang Denk54841ab2010-06-28 22:00:46 +020046int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000047{
48 return netboot_common (TFTP, cmdtp, argc, argv);
49}
50
wdenk0d498392003-07-01 21:06:45 +000051U_BOOT_CMD(
52 tftpboot, 3, 1, do_tftpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060053 "boot image via network using TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020054 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000055);
56
Wolfgang Denk54841ab2010-06-28 22:00:46 +020057int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000058{
59 return netboot_common (RARP, cmdtp, argc, argv);
60}
61
wdenk0d498392003-07-01 21:06:45 +000062U_BOOT_CMD(
63 rarpboot, 3, 1, do_rarpb,
Peter Tyser2fb26042009-01-27 18:03:12 -060064 "boot image via network using RARP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020065 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000066);
67
Jon Loeligerc76fe472007-07-08 18:02:23 -050068#if defined(CONFIG_CMD_DHCP)
Wolfgang Denk54841ab2010-06-28 22:00:46 +020069int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000070{
71 return netboot_common(DHCP, cmdtp, argc, argv);
72}
wdenk8bde7f72003-06-27 21:31:46 +000073
wdenk0d498392003-07-01 21:06:45 +000074U_BOOT_CMD(
75 dhcp, 3, 1, do_dhcp,
Peter Tyser2fb26042009-01-27 18:03:12 -060076 "boot image via network using DHCP/TFTP protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020077 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenk8bde7f72003-06-27 21:31:46 +000078);
Jon Loeliger90253172007-07-10 11:02:44 -050079#endif
wdenk38635852002-08-27 05:55:31 +000080
Jon Loeligerc76fe472007-07-08 18:02:23 -050081#if defined(CONFIG_CMD_NFS)
Wolfgang Denk54841ab2010-06-28 22:00:46 +020082int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkcbd8a352004-02-24 02:00:03 +000083{
84 return netboot_common(NFS, cmdtp, argc, argv);
85}
86
87U_BOOT_CMD(
88 nfs, 3, 1, do_nfs,
Peter Tyser2fb26042009-01-27 18:03:12 -060089 "boot image via network using NFS protocol",
Wolfgang Denka89c33d2009-05-24 17:06:54 +020090 "[loadAddress] [[hostIPaddr:]bootfilename]"
wdenkcbd8a352004-02-24 02:00:03 +000091);
Jon Loeliger90253172007-07-10 11:02:44 -050092#endif
wdenkcbd8a352004-02-24 02:00:03 +000093
wdenk6e592382004-04-18 17:39:38 +000094static void netboot_update_env (void)
wdenk38635852002-08-27 05:55:31 +000095{
wdenk6e592382004-04-18 17:39:38 +000096 char tmp[22];
wdenk38635852002-08-27 05:55:31 +000097
wdenk6e592382004-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
wdenk6e592382004-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
wdenk6e592382004-04-18 17:39:38 +0000108 if (NetOurHostName[0])
109 setenv ("hostname", NetOurHostName);
wdenk38635852002-08-27 05:55:31 +0000110
wdenk6e592382004-04-18 17:39:38 +0000111 if (NetOurRootPath[0])
112 setenv ("rootpath", NetOurRootPath);
wdenk38635852002-08-27 05:55:31 +0000113
wdenk6e592382004-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
wdenk6e592382004-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
wdenk6e592382004-04-18 17:39:38 +0000124 if (NetOurDNSIP) {
125 ip_to_string (NetOurDNSIP, tmp);
126 setenv ("dnsip", tmp);
127 }
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500128#if defined(CONFIG_BOOTP_DNS2)
wdenk6e592382004-04-18 17:39:38 +0000129 if (NetOurDNS2IP) {
130 ip_to_string (NetOurDNS2IP, tmp);
131 setenv ("dnsip2", tmp);
132 }
stroesefe389a82003-08-28 14:17:32 +0000133#endif
wdenk6e592382004-04-18 17:39:38 +0000134 if (NetOurNISDomain[0])
135 setenv ("domain", NetOurNISDomain);
wdenkea287de2005-04-01 00:25:43 +0000136
Jon Loeligerc76fe472007-07-08 18:02:23 -0500137#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500138 && defined(CONFIG_BOOTP_TIMEOFFSET)
wdenkea287de2005-04-01 00:25:43 +0000139 if (NetTimeOffset) {
140 sprintf (tmp, "%d", NetTimeOffset);
141 setenv ("timeoffset", tmp);
142 }
143#endif
Jon Loeligerc76fe472007-07-08 18:02:23 -0500144#if defined(CONFIG_CMD_SNTP) \
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500145 && defined(CONFIG_BOOTP_NTPSERVER)
wdenkea287de2005-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}
wdenk6e592382004-04-18 17:39:38 +0000152
wdenk38635852002-08-27 05:55:31 +0000153static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200154netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000155{
156 char *s;
Peter Tyser2e4970d2008-12-02 12:59:51 -0600157 char *end;
wdenk38635852002-08-27 05:55:31 +0000158 int rcode = 0;
159 int size;
Peter Tyser2e4970d2008-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 Tyser2e4970d2008-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 Tyser2e4970d2008-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 Tyser62c3ae72009-01-27 18:03:10 -0600189 default: cmd_usage(cmdtp);
Heiko Schocherfad63402007-07-13 09:54:17 +0200190 show_boot_progress (-80);
wdenk38635852002-08-27 05:55:31 +0000191 return 1;
192 }
193
Heiko Schocherfad63402007-07-13 09:54:17 +0200194 show_boot_progress (80);
Heiko Schocher566a4942007-06-22 19:11:54 +0200195 if ((size = NetLoop(proto)) < 0) {
Heiko Schocherfad63402007-07-13 09:54:17 +0200196 show_boot_progress (-81);
wdenk38635852002-08-27 05:55:31 +0000197 return 1;
Heiko Schocher566a4942007-06-22 19:11:54 +0200198 }
wdenk38635852002-08-27 05:55:31 +0000199
Heiko Schocherfad63402007-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
wdenkeb9401e2002-11-11 02:11:37 +0000204 /* done if no file was loaded (no errors though) */
Heiko Schocher566a4942007-06-22 19:11:54 +0200205 if (size == 0) {
Heiko Schocherfad63402007-07-13 09:54:17 +0200206 show_boot_progress (-82);
wdenkeb9401e2002-11-11 02:11:37 +0000207 return 0;
Heiko Schocher566a4942007-06-22 19:11:54 +0200208 }
wdenkeb9401e2002-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 Schocherfad63402007-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
Heiko Schocher566a4942007-06-22 19:11:54 +0200225 if (rcode < 0)
Heiko Schocherfad63402007-07-13 09:54:17 +0200226 show_boot_progress (-83);
Heiko Schocher566a4942007-06-22 19:11:54 +0200227 else
Heiko Schocherfad63402007-07-13 09:54:17 +0200228 show_boot_progress (84);
wdenk38635852002-08-27 05:55:31 +0000229 return rcode;
230}
231
Jon Loeligerc76fe472007-07-08 18:02:23 -0500232#if defined(CONFIG_CMD_PING)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200233int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk73a8b272003-06-05 19:27:42 +0000234{
235 if (argc < 2)
236 return -1;
237
238 NetPingIP = string_to_ip(argv[1]);
239 if (NetPingIP == 0) {
Peter Tyser62c3ae72009-01-27 18:03:10 -0600240 cmd_usage(cmdtp);
wdenk73a8b272003-06-05 19:27:42 +0000241 return -1;
242 }
243
244 if (NetLoop(PING) < 0) {
245 printf("ping failed; host %s is not alive\n", argv[1]);
246 return 1;
247 }
248
249 printf("host %s is alive\n", argv[1]);
250
251 return 0;
252}
wdenk6dff5522003-07-15 07:45:49 +0000253
254U_BOOT_CMD(
255 ping, 2, 1, do_ping,
Peter Tyser2fb26042009-01-27 18:03:12 -0600256 "send ICMP ECHO_REQUEST to network host",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200257 "pingAddress"
wdenk6dff5522003-07-15 07:45:49 +0000258);
Jon Loeliger90253172007-07-10 11:02:44 -0500259#endif
wdenk73a8b272003-06-05 19:27:42 +0000260
Jon Loeligerc76fe472007-07-08 18:02:23 -0500261#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000262
263static void cdp_update_env(void)
264{
265 char tmp[16];
266
267 if (CDPApplianceVLAN != htons(-1)) {
268 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
269 VLAN_to_string(CDPApplianceVLAN, tmp);
270 setenv("vlan", tmp);
271 NetOurVLAN = CDPApplianceVLAN;
272 }
273
274 if (CDPNativeVLAN != htons(-1)) {
275 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
276 VLAN_to_string(CDPNativeVLAN, tmp);
277 setenv("nvlan", tmp);
278 NetOurNativeVLAN = CDPNativeVLAN;
279 }
280
281}
282
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200283int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenka3d991b2004-04-15 21:48:45 +0000284{
285 int r;
286
287 r = NetLoop(CDP);
288 if (r < 0) {
289 printf("cdp failed; perhaps not a CISCO switch?\n");
290 return 1;
291 }
292
293 cdp_update_env();
294
295 return 0;
296}
297
298U_BOOT_CMD(
299 cdp, 1, 1, do_cdp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600300 "Perform CDP network configuration",
wdenka3d991b2004-04-15 21:48:45 +0000301);
Jon Loeliger90253172007-07-10 11:02:44 -0500302#endif
wdenka3d991b2004-04-15 21:48:45 +0000303
Jon Loeligerc76fe472007-07-08 18:02:23 -0500304#if defined(CONFIG_CMD_SNTP)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200305int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkea287de2005-04-01 00:25:43 +0000306{
307 char *toff;
308
309 if (argc < 2) {
310 NetNtpServerIP = getenv_IPaddr ("ntpserverip");
311 if (NetNtpServerIP == 0) {
312 printf ("ntpserverip not set\n");
313 return (1);
314 }
315 } else {
316 NetNtpServerIP = string_to_ip(argv[1]);
317 if (NetNtpServerIP == 0) {
318 printf ("Bad NTP server IP address\n");
319 return (1);
320 }
321 }
322
323 toff = getenv ("timeoffset");
324 if (toff == NULL) NetTimeOffset = 0;
325 else NetTimeOffset = simple_strtol (toff, NULL, 10);
326
327 if (NetLoop(SNTP) < 0) {
328 printf("SNTP failed: host %s not responding\n", argv[1]);
329 return 1;
330 }
331
332 return 0;
333}
334
335U_BOOT_CMD(
336 sntp, 2, 1, do_sntp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600337 "synchronize RTC via network",
wdenkea287de2005-04-01 00:25:43 +0000338 "[NTP server IP]\n"
339);
Jon Loeliger90253172007-07-10 11:02:44 -0500340#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400341
342#if defined(CONFIG_CMD_DNS)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200343int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Robin Getz1a32bf42009-07-20 14:53:54 -0400344{
345 if (argc == 1) {
346 cmd_usage(cmdtp);
347 return -1;
348 }
349
350 /*
351 * We should check for a valid hostname:
352 * - Each label must be between 1 and 63 characters long
353 * - the entire hostname has a maximum of 255 characters
354 * - only the ASCII letters 'a' through 'z' (case-insensitive),
355 * the digits '0' through '9', and the hyphen
356 * - cannot begin or end with a hyphen
357 * - no other symbols, punctuation characters, or blank spaces are
358 * permitted
359 * but hey - this is a minimalist implmentation, so only check length
360 * and let the name server deal with things.
361 */
362 if (strlen(argv[1]) >= 255) {
363 printf("dns error: hostname too long\n");
364 return 1;
365 }
366
367 NetDNSResolve = argv[1];
368
369 if (argc == 3)
370 NetDNSenvvar = argv[2];
371 else
372 NetDNSenvvar = NULL;
373
374 if (NetLoop(DNS) < 0) {
375 printf("dns lookup of %s failed, check setup\n", argv[1]);
376 return 1;
377 }
378
379 return 0;
380}
381
382U_BOOT_CMD(
383 dns, 3, 1, do_dns,
384 "lookup the IP of a hostname",
385 "hostname [envvar]"
386);
387
388#endif /* CONFIG_CMD_DNS */