]>
Commit | Line | Data |
---|---|---|
3863585b WD |
1 | /* |
2 | * (C) Copyright 2000 | |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
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 | ||
e4bf0c5c | 31 | static int netboot_common(enum 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 | 45 | { |
e4bf0c5c | 46 | return netboot_common(TFTPGET, cmdtp, argc, argv); |
3863585b WD |
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 | ||
2d46cf29 SG |
55 | #ifdef CONFIG_CMD_TFTPPUT |
56 | int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
57 | { | |
58 | int ret; | |
59 | ||
60 | ret = netboot_common(TFTPPUT, cmdtp, argc, argv); | |
61 | return ret; | |
62 | } | |
63 | ||
64 | U_BOOT_CMD( | |
65 | tftpput, 4, 1, do_tftpput, | |
66 | "TFTP put command, for uploading files to a server", | |
67 | "Address Size [[hostIPaddr:]filename]" | |
68 | ); | |
69 | #endif | |
70 | ||
7a83af07 LC |
71 | #ifdef CONFIG_CMD_TFTPSRV |
72 | static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) | |
73 | { | |
74 | return netboot_common(TFTPSRV, cmdtp, argc, argv); | |
75 | } | |
76 | ||
77 | U_BOOT_CMD( | |
78 | tftpsrv, 2, 1, do_tftpsrv, | |
79 | "act as a TFTP server and boot the first received file", | |
80 | "[loadAddress]\n" | |
81 | "Listen for an incoming TFTP transfer, receive a file and boot it.\n" | |
82 | "The transfer is aborted if a transfer has not been started after\n" | |
83 | "about 50 seconds or if Ctrl-C is pressed." | |
84 | ); | |
85 | #endif | |
86 | ||
87 | ||
bf6cb247 | 88 | #ifdef CONFIG_CMD_RARP |
54841ab5 | 89 | int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b WD |
90 | { |
91 | return netboot_common (RARP, cmdtp, argc, argv); | |
92 | } | |
93 | ||
0d498393 WD |
94 | U_BOOT_CMD( |
95 | rarpboot, 3, 1, do_rarpb, | |
2fb2604d | 96 | "boot image via network using RARP/TFTP protocol", |
a89c33db | 97 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 | 98 | ); |
bf6cb247 | 99 | #endif |
8bde7f77 | 100 | |
c76fe474 | 101 | #if defined(CONFIG_CMD_DHCP) |
54841ab5 | 102 | int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b WD |
103 | { |
104 | return netboot_common(DHCP, cmdtp, argc, argv); | |
105 | } | |
8bde7f77 | 106 | |
0d498393 WD |
107 | U_BOOT_CMD( |
108 | dhcp, 3, 1, do_dhcp, | |
2fb2604d | 109 | "boot image via network using DHCP/TFTP protocol", |
a89c33db | 110 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 | 111 | ); |
90253178 | 112 | #endif |
3863585b | 113 | |
c76fe474 | 114 | #if defined(CONFIG_CMD_NFS) |
54841ab5 | 115 | int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
cbd8a35c WD |
116 | { |
117 | return netboot_common(NFS, cmdtp, argc, argv); | |
118 | } | |
119 | ||
120 | U_BOOT_CMD( | |
121 | nfs, 3, 1, do_nfs, | |
2fb2604d | 122 | "boot image via network using NFS protocol", |
a89c33db | 123 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
cbd8a35c | 124 | ); |
90253178 | 125 | #endif |
cbd8a35c | 126 | |
6e592385 | 127 | static void netboot_update_env (void) |
3863585b | 128 | { |
6e592385 | 129 | char tmp[22]; |
3863585b | 130 | |
6e592385 WD |
131 | if (NetOurGatewayIP) { |
132 | ip_to_string (NetOurGatewayIP, tmp); | |
133 | setenv ("gatewayip", tmp); | |
134 | } | |
3863585b | 135 | |
6e592385 WD |
136 | if (NetOurSubnetMask) { |
137 | ip_to_string (NetOurSubnetMask, tmp); | |
138 | setenv ("netmask", tmp); | |
139 | } | |
3863585b | 140 | |
6e592385 WD |
141 | if (NetOurHostName[0]) |
142 | setenv ("hostname", NetOurHostName); | |
3863585b | 143 | |
6e592385 WD |
144 | if (NetOurRootPath[0]) |
145 | setenv ("rootpath", NetOurRootPath); | |
3863585b | 146 | |
6e592385 WD |
147 | if (NetOurIP) { |
148 | ip_to_string (NetOurIP, tmp); | |
149 | setenv ("ipaddr", tmp); | |
150 | } | |
3863585b | 151 | |
6e592385 WD |
152 | if (NetServerIP) { |
153 | ip_to_string (NetServerIP, tmp); | |
154 | setenv ("serverip", tmp); | |
155 | } | |
73a8b27c | 156 | |
6e592385 WD |
157 | if (NetOurDNSIP) { |
158 | ip_to_string (NetOurDNSIP, tmp); | |
159 | setenv ("dnsip", tmp); | |
160 | } | |
1fe80d79 | 161 | #if defined(CONFIG_BOOTP_DNS2) |
6e592385 WD |
162 | if (NetOurDNS2IP) { |
163 | ip_to_string (NetOurDNS2IP, tmp); | |
164 | setenv ("dnsip2", tmp); | |
165 | } | |
fe389a82 | 166 | #endif |
6e592385 WD |
167 | if (NetOurNISDomain[0]) |
168 | setenv ("domain", NetOurNISDomain); | |
ea287deb | 169 | |
c76fe474 | 170 | #if defined(CONFIG_CMD_SNTP) \ |
1fe80d79 | 171 | && defined(CONFIG_BOOTP_TIMEOFFSET) |
ea287deb WD |
172 | if (NetTimeOffset) { |
173 | sprintf (tmp, "%d", NetTimeOffset); | |
174 | setenv ("timeoffset", tmp); | |
175 | } | |
176 | #endif | |
c76fe474 | 177 | #if defined(CONFIG_CMD_SNTP) \ |
1fe80d79 | 178 | && defined(CONFIG_BOOTP_NTPSERVER) |
ea287deb WD |
179 | if (NetNtpServerIP) { |
180 | ip_to_string (NetNtpServerIP, tmp); | |
181 | setenv ("ntpserverip", tmp); | |
182 | } | |
183 | #endif | |
3863585b | 184 | } |
6e592385 | 185 | |
e4bf0c5c SG |
186 | static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc, |
187 | char * const argv[]) | |
3863585b WD |
188 | { |
189 | char *s; | |
2e4970d8 | 190 | char *end; |
3863585b WD |
191 | int rcode = 0; |
192 | int size; | |
2e4970d8 | 193 | ulong addr; |
3863585b WD |
194 | |
195 | /* pre-set load_addr */ | |
196 | if ((s = getenv("loadaddr")) != NULL) { | |
197 | load_addr = simple_strtoul(s, NULL, 16); | |
198 | } | |
199 | ||
200 | switch (argc) { | |
201 | case 1: | |
202 | break; | |
203 | ||
2e4970d8 PT |
204 | case 2: /* |
205 | * Only one arg - accept two forms: | |
206 | * Just load address, or just boot file name. The latter | |
207 | * form must be written in a format which can not be | |
208 | * mis-interpreted as a valid number. | |
3863585b | 209 | */ |
2e4970d8 PT |
210 | addr = simple_strtoul(argv[1], &end, 16); |
211 | if (end == (argv[1] + strlen(argv[1]))) | |
212 | load_addr = addr; | |
213 | else | |
214 | copy_filename(BootFile, argv[1], sizeof(BootFile)); | |
3863585b WD |
215 | break; |
216 | ||
217 | case 3: load_addr = simple_strtoul(argv[1], NULL, 16); | |
218 | copy_filename (BootFile, argv[2], sizeof(BootFile)); | |
219 | ||
220 | break; | |
221 | ||
2d46cf29 SG |
222 | #ifdef CONFIG_CMD_TFTPPUT |
223 | case 4: | |
38bd80b4 SG |
224 | if (strict_strtoul(argv[1], 16, &save_addr) < 0 || |
225 | strict_strtoul(argv[2], 16, &save_size) < 0) { | |
226 | printf("Invalid address/size\n"); | |
227 | return cmd_usage(cmdtp); | |
228 | } | |
2d46cf29 SG |
229 | copy_filename(BootFile, argv[3], sizeof(BootFile)); |
230 | break; | |
231 | #endif | |
47e26b1b | 232 | default: |
5ddb118d | 233 | show_boot_error(80); |
4c12eeb8 | 234 | return CMD_RET_USAGE; |
3863585b WD |
235 | } |
236 | ||
5ddb118d | 237 | show_boot_progress(80); |
566a494f | 238 | if ((size = NetLoop(proto)) < 0) { |
5ddb118d | 239 | show_boot_error(81); |
3863585b | 240 | return 1; |
566a494f | 241 | } |
3863585b | 242 | |
5ddb118d | 243 | show_boot_progress(81); |
3863585b WD |
244 | /* NetLoop ok, update environment */ |
245 | netboot_update_env(); | |
246 | ||
eb9401e3 | 247 | /* done if no file was loaded (no errors though) */ |
566a494f | 248 | if (size == 0) { |
5ddb118d | 249 | show_boot_error(82); |
eb9401e3 | 250 | return 0; |
566a494f | 251 | } |
eb9401e3 | 252 | |
3863585b WD |
253 | /* flush cache */ |
254 | flush_cache(load_addr, size); | |
255 | ||
67d668bf MF |
256 | show_boot_progress(82); |
257 | rcode = bootm_maybe_autostart(cmdtp, argv[0]); | |
3863585b | 258 | |
566a494f | 259 | if (rcode < 0) |
5ddb118d | 260 | show_boot_error(83); |
566a494f | 261 | else |
5ddb118d | 262 | show_boot_progress(84); |
3863585b WD |
263 | return rcode; |
264 | } | |
265 | ||
c76fe474 | 266 | #if defined(CONFIG_CMD_PING) |
54841ab5 | 267 | int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
73a8b27c WD |
268 | { |
269 | if (argc < 2) | |
270 | return -1; | |
271 | ||
272 | NetPingIP = string_to_ip(argv[1]); | |
47e26b1b | 273 | if (NetPingIP == 0) |
4c12eeb8 | 274 | return CMD_RET_USAGE; |
73a8b27c WD |
275 | |
276 | if (NetLoop(PING) < 0) { | |
277 | printf("ping failed; host %s is not alive\n", argv[1]); | |
278 | return 1; | |
279 | } | |
280 | ||
281 | printf("host %s is alive\n", argv[1]); | |
282 | ||
283 | return 0; | |
284 | } | |
6dff5529 WD |
285 | |
286 | U_BOOT_CMD( | |
287 | ping, 2, 1, do_ping, | |
2fb2604d | 288 | "send ICMP ECHO_REQUEST to network host", |
a89c33db | 289 | "pingAddress" |
6dff5529 | 290 | ); |
90253178 | 291 | #endif |
73a8b27c | 292 | |
c76fe474 | 293 | #if defined(CONFIG_CMD_CDP) |
a3d991bd WD |
294 | |
295 | static void cdp_update_env(void) | |
296 | { | |
297 | char tmp[16]; | |
298 | ||
299 | if (CDPApplianceVLAN != htons(-1)) { | |
300 | printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN)); | |
301 | VLAN_to_string(CDPApplianceVLAN, tmp); | |
302 | setenv("vlan", tmp); | |
303 | NetOurVLAN = CDPApplianceVLAN; | |
304 | } | |
305 | ||
306 | if (CDPNativeVLAN != htons(-1)) { | |
307 | printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN)); | |
308 | VLAN_to_string(CDPNativeVLAN, tmp); | |
309 | setenv("nvlan", tmp); | |
310 | NetOurNativeVLAN = CDPNativeVLAN; | |
311 | } | |
312 | ||
313 | } | |
314 | ||
54841ab5 | 315 | int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
a3d991bd WD |
316 | { |
317 | int r; | |
318 | ||
319 | r = NetLoop(CDP); | |
320 | if (r < 0) { | |
321 | printf("cdp failed; perhaps not a CISCO switch?\n"); | |
322 | return 1; | |
323 | } | |
324 | ||
325 | cdp_update_env(); | |
326 | ||
327 | return 0; | |
328 | } | |
329 | ||
330 | U_BOOT_CMD( | |
331 | cdp, 1, 1, do_cdp, | |
ec5c04cd | 332 | "Perform CDP network configuration", |
4b58266e | 333 | "\n" |
a3d991bd | 334 | ); |
90253178 | 335 | #endif |
a3d991bd | 336 | |
c76fe474 | 337 | #if defined(CONFIG_CMD_SNTP) |
54841ab5 | 338 | int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
ea287deb WD |
339 | { |
340 | char *toff; | |
341 | ||
342 | if (argc < 2) { | |
343 | NetNtpServerIP = getenv_IPaddr ("ntpserverip"); | |
344 | if (NetNtpServerIP == 0) { | |
345 | printf ("ntpserverip not set\n"); | |
346 | return (1); | |
347 | } | |
348 | } else { | |
349 | NetNtpServerIP = string_to_ip(argv[1]); | |
350 | if (NetNtpServerIP == 0) { | |
351 | printf ("Bad NTP server IP address\n"); | |
352 | return (1); | |
353 | } | |
354 | } | |
355 | ||
356 | toff = getenv ("timeoffset"); | |
357 | if (toff == NULL) NetTimeOffset = 0; | |
358 | else NetTimeOffset = simple_strtol (toff, NULL, 10); | |
359 | ||
360 | if (NetLoop(SNTP) < 0) { | |
d6840e3d LP |
361 | printf("SNTP failed: host %pI4 not responding\n", |
362 | &NetNtpServerIP); | |
ea287deb WD |
363 | return 1; |
364 | } | |
365 | ||
366 | return 0; | |
367 | } | |
368 | ||
369 | U_BOOT_CMD( | |
370 | sntp, 2, 1, do_sntp, | |
2fb2604d | 371 | "synchronize RTC via network", |
ea287deb WD |
372 | "[NTP server IP]\n" |
373 | ); | |
90253178 | 374 | #endif |
1a32bf41 RG |
375 | |
376 | #if defined(CONFIG_CMD_DNS) | |
54841ab5 | 377 | int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
1a32bf41 | 378 | { |
47e26b1b | 379 | if (argc == 1) |
4c12eeb8 | 380 | return CMD_RET_USAGE; |
1a32bf41 RG |
381 | |
382 | /* | |
383 | * We should check for a valid hostname: | |
384 | * - Each label must be between 1 and 63 characters long | |
385 | * - the entire hostname has a maximum of 255 characters | |
386 | * - only the ASCII letters 'a' through 'z' (case-insensitive), | |
387 | * the digits '0' through '9', and the hyphen | |
388 | * - cannot begin or end with a hyphen | |
389 | * - no other symbols, punctuation characters, or blank spaces are | |
390 | * permitted | |
391 | * but hey - this is a minimalist implmentation, so only check length | |
392 | * and let the name server deal with things. | |
393 | */ | |
394 | if (strlen(argv[1]) >= 255) { | |
395 | printf("dns error: hostname too long\n"); | |
396 | return 1; | |
397 | } | |
398 | ||
399 | NetDNSResolve = argv[1]; | |
400 | ||
401 | if (argc == 3) | |
402 | NetDNSenvvar = argv[2]; | |
403 | else | |
404 | NetDNSenvvar = NULL; | |
405 | ||
406 | if (NetLoop(DNS) < 0) { | |
407 | printf("dns lookup of %s failed, check setup\n", argv[1]); | |
408 | return 1; | |
409 | } | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | U_BOOT_CMD( | |
415 | dns, 3, 1, do_dns, | |
416 | "lookup the IP of a hostname", | |
417 | "hostname [envvar]" | |
418 | ); | |
419 | ||
420 | #endif /* CONFIG_CMD_DNS */ |