blob: 85a90237342796f7e835e06a73771620cec1b655 [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
31#if (CONFIG_COMMANDS & CFG_CMD_NET)
32
wdenk38635852002-08-27 05:55:31 +000033
34extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
35
36static int netboot_common (int, cmd_tbl_t *, int , char *[]);
37
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,
wdenk8bde7f72003-06-27 21:31:46 +000045 "bootp - boot image via network using BootP/TFTP protocol\n",
46 "[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
wdenk38635852002-08-27 05:55:31 +000071#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
72int 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,
wdenk8bde7f72003-06-27 21:31:46 +000079 "dhcp - invoke DHCP client to obtain IP/boot params\n",
80 "\n"
81);
wdenk38635852002-08-27 05:55:31 +000082#endif /* CFG_CMD_DHCP */
83
wdenkcbd8a352004-02-24 02:00:03 +000084#if (CONFIG_COMMANDS & CFG_CMD_NFS)
85int 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,
92 "nfs - boot image via network using NFS protocol\n",
93 "[loadAddress] [host ip addr:bootfilename]\n"
94);
95#endif /* CFG_CMD_NFS */
96
wdenk38635852002-08-27 05:55:31 +000097static void netboot_update_env(void)
98{
wdenka3d991b2004-04-15 21:48:45 +000099 char tmp[22] ;
wdenk38635852002-08-27 05:55:31 +0000100
101 if (NetOurGatewayIP) {
102 ip_to_string (NetOurGatewayIP, tmp);
103 setenv("gatewayip", tmp);
104 }
105
106 if (NetOurSubnetMask) {
107 ip_to_string (NetOurSubnetMask, tmp);
108 setenv("netmask", tmp);
109 }
110
111 if (NetOurHostName[0])
112 setenv("hostname", NetOurHostName);
113
114 if (NetOurRootPath[0])
115 setenv("rootpath", NetOurRootPath);
116
117 if (NetOurIP) {
118 ip_to_string (NetOurIP, tmp);
119 setenv("ipaddr", tmp);
120 }
121
122 if (NetServerIP) {
123 ip_to_string (NetServerIP, tmp);
124 setenv("serverip", tmp);
125 }
126
127 if (NetOurDNSIP) {
128 ip_to_string (NetOurDNSIP, tmp);
129 setenv("dnsip", tmp);
130 }
wdenk73a8b272003-06-05 19:27:42 +0000131
stroesefe389a82003-08-28 14:17:32 +0000132#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
133 if (NetOurDNS2IP) {
134 ip_to_string (NetOurDNS2IP, tmp);
135 setenv("dnsip2", tmp);
136 }
137#endif
138
wdenk73a8b272003-06-05 19:27:42 +0000139 if (NetOurNISDomain[0])
140 setenv("domain", NetOurNISDomain);
141
wdenka3d991b2004-04-15 21:48:45 +0000142 if (ntohs(NetOurVLAN) != (ushort)-1) {
143 VLAN_to_string(NetOurVLAN, tmp);
144 setenv("vlan", tmp);
145 }
146
147 if (ntohs(NetOurNativeVLAN) != (ushort)-1) {
148 VLAN_to_string(NetOurNativeVLAN, tmp);
149 setenv("vlan", tmp);
150 }
151
wdenk38635852002-08-27 05:55:31 +0000152}
153static int
154netboot_common (int proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
155{
156 char *s;
157 int rcode = 0;
158 int size;
159
160 /* pre-set load_addr */
161 if ((s = getenv("loadaddr")) != NULL) {
162 load_addr = simple_strtoul(s, NULL, 16);
163 }
164
165 switch (argc) {
166 case 1:
167 break;
168
169 case 2: /* only one arg - accept two forms:
170 * just load address, or just boot file name.
171 * The latter form must be written "filename" here.
172 */
173 if (argv[1][0] == '"') { /* just boot filename */
174 copy_filename (BootFile, argv[1], sizeof(BootFile));
175 } else { /* load address */
176 load_addr = simple_strtoul(argv[1], NULL, 16);
177 }
178 break;
179
180 case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
181 copy_filename (BootFile, argv[2], sizeof(BootFile));
182
183 break;
184
185 default: printf ("Usage:\n%s\n", cmdtp->usage);
186 return 1;
187 }
188
wdenkeb9401e2002-11-11 02:11:37 +0000189 if ((size = NetLoop(proto)) < 0)
wdenk38635852002-08-27 05:55:31 +0000190 return 1;
191
192 /* NetLoop ok, update environment */
193 netboot_update_env();
194
wdenkeb9401e2002-11-11 02:11:37 +0000195 /* done if no file was loaded (no errors though) */
196 if (size == 0)
197 return 0;
198
wdenk38635852002-08-27 05:55:31 +0000199 /* flush cache */
200 flush_cache(load_addr, size);
201
202 /* Loading ok, check if we should attempt an auto-start */
203 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
204 char *local_args[2];
205 local_args[0] = argv[0];
206 local_args[1] = NULL;
207
208 printf ("Automatic boot of image at addr 0x%08lX ...\n",
209 load_addr);
210 rcode = do_bootm (cmdtp, 0, 1, local_args);
211 }
212
213#ifdef CONFIG_AUTOSCRIPT
214 if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
215 printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
216 rcode = autoscript (load_addr);
217 }
218#endif
219 return rcode;
220}
221
wdenk73a8b272003-06-05 19:27:42 +0000222#if (CONFIG_COMMANDS & CFG_CMD_PING)
223int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
224{
225 if (argc < 2)
226 return -1;
227
228 NetPingIP = string_to_ip(argv[1]);
229 if (NetPingIP == 0) {
230 printf ("Usage:\n%s\n", cmdtp->usage);
231 return -1;
232 }
233
234 if (NetLoop(PING) < 0) {
235 printf("ping failed; host %s is not alive\n", argv[1]);
236 return 1;
237 }
238
239 printf("host %s is alive\n", argv[1]);
240
241 return 0;
242}
wdenk6dff5522003-07-15 07:45:49 +0000243
244U_BOOT_CMD(
245 ping, 2, 1, do_ping,
246 "ping - send ICMP ECHO_REQUEST to network host\n",
247 "pingAddress\n"
248);
wdenk73a8b272003-06-05 19:27:42 +0000249#endif /* CFG_CMD_PING */
250
wdenka3d991b2004-04-15 21:48:45 +0000251#if (CONFIG_COMMANDS & CFG_CMD_CDP)
252
253static void cdp_update_env(void)
254{
255 char tmp[16];
256
257 if (CDPApplianceVLAN != htons(-1)) {
258 printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
259 VLAN_to_string(CDPApplianceVLAN, tmp);
260 setenv("vlan", tmp);
261 NetOurVLAN = CDPApplianceVLAN;
262 }
263
264 if (CDPNativeVLAN != htons(-1)) {
265 printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
266 VLAN_to_string(CDPNativeVLAN, tmp);
267 setenv("nvlan", tmp);
268 NetOurNativeVLAN = CDPNativeVLAN;
269 }
270
271}
272
273int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
274{
275 int r;
276
277 r = NetLoop(CDP);
278 if (r < 0) {
279 printf("cdp failed; perhaps not a CISCO switch?\n");
280 return 1;
281 }
282
283 cdp_update_env();
284
285 return 0;
286}
287
288U_BOOT_CMD(
289 cdp, 1, 1, do_cdp,
290 "cdp - Perform CDP network configuration\n",
291);
292#endif /* CFG_CMD_CDP */
293
wdenk38635852002-08-27 05:55:31 +0000294#endif /* CFG_CMD_NET */