]>
Commit | Line | Data |
---|---|---|
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 | ||
54841ab5 | 31 | static int netboot_common (proto_t, cmd_tbl_t *, int , char * const []); |
3863585b | 32 | |
54841ab5 | 33 | int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b WD |
34 | { |
35 | return netboot_common (BOOTP, cmdtp, argc, argv); | |
36 | } | |
37 | ||
0d498393 WD |
38 | U_BOOT_CMD( |
39 | bootp, 3, 1, do_bootp, | |
2fb2604d | 40 | "boot image via network using BOOTP/TFTP protocol", |
a89c33db | 41 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 WD |
42 | ); |
43 | ||
54841ab5 | 44 | int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b WD |
45 | { |
46 | return netboot_common (TFTP, cmdtp, argc, argv); | |
47 | } | |
48 | ||
0d498393 WD |
49 | U_BOOT_CMD( |
50 | tftpboot, 3, 1, do_tftpb, | |
2fb2604d | 51 | "boot image via network using TFTP protocol", |
a89c33db | 52 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 WD |
53 | ); |
54 | ||
7a83af07 LC |
55 | #ifdef CONFIG_CMD_TFTPSRV |
56 | static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) | |
57 | { | |
58 | return netboot_common(TFTPSRV, cmdtp, argc, argv); | |
59 | } | |
60 | ||
61 | U_BOOT_CMD( | |
62 | tftpsrv, 2, 1, do_tftpsrv, | |
63 | "act as a TFTP server and boot the first received file", | |
64 | "[loadAddress]\n" | |
65 | "Listen for an incoming TFTP transfer, receive a file and boot it.\n" | |
66 | "The transfer is aborted if a transfer has not been started after\n" | |
67 | "about 50 seconds or if Ctrl-C is pressed." | |
68 | ); | |
69 | #endif | |
70 | ||
71 | ||
bf6cb247 | 72 | #ifdef CONFIG_CMD_RARP |
54841ab5 | 73 | int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b WD |
74 | { |
75 | return netboot_common (RARP, cmdtp, argc, argv); | |
76 | } | |
77 | ||
0d498393 WD |
78 | U_BOOT_CMD( |
79 | rarpboot, 3, 1, do_rarpb, | |
2fb2604d | 80 | "boot image via network using RARP/TFTP protocol", |
a89c33db | 81 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 | 82 | ); |
bf6cb247 | 83 | #endif |
8bde7f77 | 84 | |
c76fe474 | 85 | #if defined(CONFIG_CMD_DHCP) |
54841ab5 | 86 | int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b WD |
87 | { |
88 | return netboot_common(DHCP, cmdtp, argc, argv); | |
89 | } | |
8bde7f77 | 90 | |
0d498393 WD |
91 | U_BOOT_CMD( |
92 | dhcp, 3, 1, do_dhcp, | |
2fb2604d | 93 | "boot image via network using DHCP/TFTP protocol", |
a89c33db | 94 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 | 95 | ); |
90253178 | 96 | #endif |
3863585b | 97 | |
c76fe474 | 98 | #if defined(CONFIG_CMD_NFS) |
54841ab5 | 99 | int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
cbd8a35c WD |
100 | { |
101 | return netboot_common(NFS, cmdtp, argc, argv); | |
102 | } | |
103 | ||
104 | U_BOOT_CMD( | |
105 | nfs, 3, 1, do_nfs, | |
2fb2604d | 106 | "boot image via network using NFS protocol", |
a89c33db | 107 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
cbd8a35c | 108 | ); |
90253178 | 109 | #endif |
cbd8a35c | 110 | |
6e592385 | 111 | static void netboot_update_env (void) |
3863585b | 112 | { |
6e592385 | 113 | char tmp[22]; |
3863585b | 114 | |
6e592385 WD |
115 | if (NetOurGatewayIP) { |
116 | ip_to_string (NetOurGatewayIP, tmp); | |
117 | setenv ("gatewayip", tmp); | |
118 | } | |
3863585b | 119 | |
6e592385 WD |
120 | if (NetOurSubnetMask) { |
121 | ip_to_string (NetOurSubnetMask, tmp); | |
122 | setenv ("netmask", tmp); | |
123 | } | |
3863585b | 124 | |
6e592385 WD |
125 | if (NetOurHostName[0]) |
126 | setenv ("hostname", NetOurHostName); | |
3863585b | 127 | |
6e592385 WD |
128 | if (NetOurRootPath[0]) |
129 | setenv ("rootpath", NetOurRootPath); | |
3863585b | 130 | |
6e592385 WD |
131 | if (NetOurIP) { |
132 | ip_to_string (NetOurIP, tmp); | |
133 | setenv ("ipaddr", tmp); | |
134 | } | |
3863585b | 135 | |
6e592385 WD |
136 | if (NetServerIP) { |
137 | ip_to_string (NetServerIP, tmp); | |
138 | setenv ("serverip", tmp); | |
139 | } | |
73a8b27c | 140 | |
6e592385 WD |
141 | if (NetOurDNSIP) { |
142 | ip_to_string (NetOurDNSIP, tmp); | |
143 | setenv ("dnsip", tmp); | |
144 | } | |
1fe80d79 | 145 | #if defined(CONFIG_BOOTP_DNS2) |
6e592385 WD |
146 | if (NetOurDNS2IP) { |
147 | ip_to_string (NetOurDNS2IP, tmp); | |
148 | setenv ("dnsip2", tmp); | |
149 | } | |
fe389a82 | 150 | #endif |
6e592385 WD |
151 | if (NetOurNISDomain[0]) |
152 | setenv ("domain", NetOurNISDomain); | |
ea287deb | 153 | |
c76fe474 | 154 | #if defined(CONFIG_CMD_SNTP) \ |
1fe80d79 | 155 | && defined(CONFIG_BOOTP_TIMEOFFSET) |
ea287deb WD |
156 | if (NetTimeOffset) { |
157 | sprintf (tmp, "%d", NetTimeOffset); | |
158 | setenv ("timeoffset", tmp); | |
159 | } | |
160 | #endif | |
c76fe474 | 161 | #if defined(CONFIG_CMD_SNTP) \ |
1fe80d79 | 162 | && defined(CONFIG_BOOTP_NTPSERVER) |
ea287deb WD |
163 | if (NetNtpServerIP) { |
164 | ip_to_string (NetNtpServerIP, tmp); | |
165 | setenv ("ntpserverip", tmp); | |
166 | } | |
167 | #endif | |
3863585b | 168 | } |
6e592385 | 169 | |
3863585b | 170 | static int |
54841ab5 | 171 | netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char * const argv[]) |
3863585b WD |
172 | { |
173 | char *s; | |
2e4970d8 | 174 | char *end; |
3863585b WD |
175 | int rcode = 0; |
176 | int size; | |
2e4970d8 | 177 | ulong addr; |
3863585b WD |
178 | |
179 | /* pre-set load_addr */ | |
180 | if ((s = getenv("loadaddr")) != NULL) { | |
181 | load_addr = simple_strtoul(s, NULL, 16); | |
182 | } | |
183 | ||
184 | switch (argc) { | |
185 | case 1: | |
186 | break; | |
187 | ||
2e4970d8 PT |
188 | case 2: /* |
189 | * Only one arg - accept two forms: | |
190 | * Just load address, or just boot file name. The latter | |
191 | * form must be written in a format which can not be | |
192 | * mis-interpreted as a valid number. | |
3863585b | 193 | */ |
2e4970d8 PT |
194 | addr = simple_strtoul(argv[1], &end, 16); |
195 | if (end == (argv[1] + strlen(argv[1]))) | |
196 | load_addr = addr; | |
197 | else | |
198 | copy_filename(BootFile, argv[1], sizeof(BootFile)); | |
3863585b WD |
199 | break; |
200 | ||
201 | case 3: load_addr = simple_strtoul(argv[1], NULL, 16); | |
202 | copy_filename (BootFile, argv[2], sizeof(BootFile)); | |
203 | ||
204 | break; | |
205 | ||
47e26b1b | 206 | default: |
fad63407 | 207 | show_boot_progress (-80); |
47e26b1b | 208 | return cmd_usage(cmdtp); |
3863585b WD |
209 | } |
210 | ||
fad63407 | 211 | show_boot_progress (80); |
566a494f | 212 | if ((size = NetLoop(proto)) < 0) { |
fad63407 | 213 | show_boot_progress (-81); |
3863585b | 214 | return 1; |
566a494f | 215 | } |
3863585b | 216 | |
fad63407 | 217 | show_boot_progress (81); |
3863585b WD |
218 | /* NetLoop ok, update environment */ |
219 | netboot_update_env(); | |
220 | ||
eb9401e3 | 221 | /* done if no file was loaded (no errors though) */ |
566a494f | 222 | if (size == 0) { |
fad63407 | 223 | show_boot_progress (-82); |
eb9401e3 | 224 | return 0; |
566a494f | 225 | } |
eb9401e3 | 226 | |
3863585b WD |
227 | /* flush cache */ |
228 | flush_cache(load_addr, size); | |
229 | ||
230 | /* Loading ok, check if we should attempt an auto-start */ | |
3e5ab1af | 231 | if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) { |
3863585b WD |
232 | char *local_args[2]; |
233 | local_args[0] = argv[0]; | |
234 | local_args[1] = NULL; | |
235 | ||
236 | printf ("Automatic boot of image at addr 0x%08lX ...\n", | |
237 | load_addr); | |
fad63407 | 238 | show_boot_progress (82); |
3863585b WD |
239 | rcode = do_bootm (cmdtp, 0, 1, local_args); |
240 | } | |
241 | ||
566a494f | 242 | if (rcode < 0) |
fad63407 | 243 | show_boot_progress (-83); |
566a494f | 244 | else |
fad63407 | 245 | show_boot_progress (84); |
3863585b WD |
246 | return rcode; |
247 | } | |
248 | ||
c76fe474 | 249 | #if defined(CONFIG_CMD_PING) |
54841ab5 | 250 | int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
73a8b27c WD |
251 | { |
252 | if (argc < 2) | |
253 | return -1; | |
254 | ||
255 | NetPingIP = string_to_ip(argv[1]); | |
47e26b1b WD |
256 | if (NetPingIP == 0) |
257 | return cmd_usage(cmdtp); | |
73a8b27c WD |
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 | } | |
6dff5529 WD |
268 | |
269 | U_BOOT_CMD( | |
270 | ping, 2, 1, do_ping, | |
2fb2604d | 271 | "send ICMP ECHO_REQUEST to network host", |
a89c33db | 272 | "pingAddress" |
6dff5529 | 273 | ); |
90253178 | 274 | #endif |
73a8b27c | 275 | |
c76fe474 | 276 | #if defined(CONFIG_CMD_CDP) |
a3d991bd WD |
277 | |
278 | static 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 | ||
54841ab5 | 298 | int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
a3d991bd WD |
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 | ||
313 | U_BOOT_CMD( | |
314 | cdp, 1, 1, do_cdp, | |
ec5c04cd | 315 | "Perform CDP network configuration", |
4b58266e | 316 | "\n" |
a3d991bd | 317 | ); |
90253178 | 318 | #endif |
a3d991bd | 319 | |
c76fe474 | 320 | #if defined(CONFIG_CMD_SNTP) |
54841ab5 | 321 | int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
ea287deb WD |
322 | { |
323 | char *toff; | |
324 | ||
325 | if (argc < 2) { | |
326 | NetNtpServerIP = getenv_IPaddr ("ntpserverip"); | |
327 | if (NetNtpServerIP == 0) { | |
328 | printf ("ntpserverip not set\n"); | |
329 | return (1); | |
330 | } | |
331 | } else { | |
332 | NetNtpServerIP = string_to_ip(argv[1]); | |
333 | if (NetNtpServerIP == 0) { | |
334 | printf ("Bad NTP server IP address\n"); | |
335 | return (1); | |
336 | } | |
337 | } | |
338 | ||
339 | toff = getenv ("timeoffset"); | |
340 | if (toff == NULL) NetTimeOffset = 0; | |
341 | else NetTimeOffset = simple_strtol (toff, NULL, 10); | |
342 | ||
343 | if (NetLoop(SNTP) < 0) { | |
344 | printf("SNTP failed: host %s not responding\n", argv[1]); | |
345 | return 1; | |
346 | } | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | U_BOOT_CMD( | |
352 | sntp, 2, 1, do_sntp, | |
2fb2604d | 353 | "synchronize RTC via network", |
ea287deb WD |
354 | "[NTP server IP]\n" |
355 | ); | |
90253178 | 356 | #endif |
1a32bf41 RG |
357 | |
358 | #if defined(CONFIG_CMD_DNS) | |
54841ab5 | 359 | int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
1a32bf41 | 360 | { |
47e26b1b WD |
361 | if (argc == 1) |
362 | return cmd_usage(cmdtp); | |
1a32bf41 RG |
363 | |
364 | /* | |
365 | * We should check for a valid hostname: | |
366 | * - Each label must be between 1 and 63 characters long | |
367 | * - the entire hostname has a maximum of 255 characters | |
368 | * - only the ASCII letters 'a' through 'z' (case-insensitive), | |
369 | * the digits '0' through '9', and the hyphen | |
370 | * - cannot begin or end with a hyphen | |
371 | * - no other symbols, punctuation characters, or blank spaces are | |
372 | * permitted | |
373 | * but hey - this is a minimalist implmentation, so only check length | |
374 | * and let the name server deal with things. | |
375 | */ | |
376 | if (strlen(argv[1]) >= 255) { | |
377 | printf("dns error: hostname too long\n"); | |
378 | return 1; | |
379 | } | |
380 | ||
381 | NetDNSResolve = argv[1]; | |
382 | ||
383 | if (argc == 3) | |
384 | NetDNSenvvar = argv[2]; | |
385 | else | |
386 | NetDNSenvvar = NULL; | |
387 | ||
388 | if (NetLoop(DNS) < 0) { | |
389 | printf("dns lookup of %s failed, check setup\n", argv[1]); | |
390 | return 1; | |
391 | } | |
392 | ||
393 | return 0; | |
394 | } | |
395 | ||
396 | U_BOOT_CMD( | |
397 | dns, 3, 1, do_dns, | |
398 | "lookup the IP of a hostname", | |
399 | "hostname [envvar]" | |
400 | ); | |
401 | ||
402 | #endif /* CONFIG_CMD_DNS */ |