]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
3863585b WD |
2 | /* |
3 | * (C) Copyright 2000 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
3863585b WD |
5 | */ |
6 | ||
7 | /* | |
8 | * Boot support | |
9 | */ | |
10 | #include <common.h> | |
11 | #include <command.h> | |
9fb625ce | 12 | #include <env.h> |
3863585b WD |
13 | #include <net.h> |
14 | ||
e4bf0c5c | 15 | static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []); |
3863585b | 16 | |
d7a45eaf | 17 | #ifdef CONFIG_CMD_BOOTP |
088f1b19 | 18 | static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b | 19 | { |
088f1b19 | 20 | return netboot_common(BOOTP, cmdtp, argc, argv); |
3863585b WD |
21 | } |
22 | ||
0d498393 WD |
23 | U_BOOT_CMD( |
24 | bootp, 3, 1, do_bootp, | |
2fb2604d | 25 | "boot image via network using BOOTP/TFTP protocol", |
a89c33db | 26 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 | 27 | ); |
d7a45eaf | 28 | #endif |
8bde7f77 | 29 | |
d7a45eaf | 30 | #ifdef CONFIG_CMD_TFTPBOOT |
088f1b19 | 31 | int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b | 32 | { |
573f14fe SG |
33 | int ret; |
34 | ||
35 | bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start"); | |
36 | ret = netboot_common(TFTPGET, cmdtp, argc, argv); | |
37 | bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done"); | |
38 | return ret; | |
3863585b WD |
39 | } |
40 | ||
0d498393 WD |
41 | U_BOOT_CMD( |
42 | tftpboot, 3, 1, do_tftpb, | |
2fb2604d | 43 | "boot image via network using TFTP protocol", |
a89c33db | 44 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 | 45 | ); |
d7a45eaf | 46 | #endif |
8bde7f77 | 47 | |
2d46cf29 | 48 | #ifdef CONFIG_CMD_TFTPPUT |
0c1b869b | 49 | static int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
2d46cf29 | 50 | { |
85848f03 | 51 | return netboot_common(TFTPPUT, cmdtp, argc, argv); |
2d46cf29 SG |
52 | } |
53 | ||
54 | U_BOOT_CMD( | |
55 | tftpput, 4, 1, do_tftpput, | |
56 | "TFTP put command, for uploading files to a server", | |
57 | "Address Size [[hostIPaddr:]filename]" | |
58 | ); | |
59 | #endif | |
60 | ||
7a83af07 LC |
61 | #ifdef CONFIG_CMD_TFTPSRV |
62 | static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) | |
63 | { | |
64 | return netboot_common(TFTPSRV, cmdtp, argc, argv); | |
65 | } | |
66 | ||
67 | U_BOOT_CMD( | |
68 | tftpsrv, 2, 1, do_tftpsrv, | |
69 | "act as a TFTP server and boot the first received file", | |
70 | "[loadAddress]\n" | |
71 | "Listen for an incoming TFTP transfer, receive a file and boot it.\n" | |
72 | "The transfer is aborted if a transfer has not been started after\n" | |
73 | "about 50 seconds or if Ctrl-C is pressed." | |
74 | ); | |
75 | #endif | |
76 | ||
77 | ||
bf6cb247 | 78 | #ifdef CONFIG_CMD_RARP |
088f1b19 | 79 | int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b | 80 | { |
088f1b19 | 81 | return netboot_common(RARP, cmdtp, argc, argv); |
3863585b WD |
82 | } |
83 | ||
0d498393 WD |
84 | U_BOOT_CMD( |
85 | rarpboot, 3, 1, do_rarpb, | |
2fb2604d | 86 | "boot image via network using RARP/TFTP protocol", |
a89c33db | 87 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 | 88 | ); |
bf6cb247 | 89 | #endif |
8bde7f77 | 90 | |
c76fe474 | 91 | #if defined(CONFIG_CMD_DHCP) |
088f1b19 | 92 | static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
3863585b WD |
93 | { |
94 | return netboot_common(DHCP, cmdtp, argc, argv); | |
95 | } | |
8bde7f77 | 96 | |
0d498393 WD |
97 | U_BOOT_CMD( |
98 | dhcp, 3, 1, do_dhcp, | |
2fb2604d | 99 | "boot image via network using DHCP/TFTP protocol", |
a89c33db | 100 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
8bde7f77 | 101 | ); |
90253178 | 102 | #endif |
3863585b | 103 | |
c76fe474 | 104 | #if defined(CONFIG_CMD_NFS) |
088f1b19 | 105 | static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
cbd8a35c WD |
106 | { |
107 | return netboot_common(NFS, cmdtp, argc, argv); | |
108 | } | |
109 | ||
110 | U_BOOT_CMD( | |
111 | nfs, 3, 1, do_nfs, | |
2fb2604d | 112 | "boot image via network using NFS protocol", |
a89c33db | 113 | "[loadAddress] [[hostIPaddr:]bootfilename]" |
cbd8a35c | 114 | ); |
90253178 | 115 | #endif |
cbd8a35c | 116 | |
088f1b19 | 117 | static void netboot_update_env(void) |
3863585b | 118 | { |
6e592385 | 119 | char tmp[22]; |
3863585b | 120 | |
049a95a7 JH |
121 | if (net_gateway.s_addr) { |
122 | ip_to_string(net_gateway, tmp); | |
382bee57 | 123 | env_set("gatewayip", tmp); |
6e592385 | 124 | } |
3863585b | 125 | |
049a95a7 JH |
126 | if (net_netmask.s_addr) { |
127 | ip_to_string(net_netmask, tmp); | |
382bee57 | 128 | env_set("netmask", tmp); |
6e592385 | 129 | } |
3863585b | 130 | |
586cbe51 | 131 | if (net_hostname[0]) |
382bee57 | 132 | env_set("hostname", net_hostname); |
3863585b | 133 | |
586cbe51 | 134 | if (net_root_path[0]) |
382bee57 | 135 | env_set("rootpath", net_root_path); |
3863585b | 136 | |
049a95a7 JH |
137 | if (net_ip.s_addr) { |
138 | ip_to_string(net_ip, tmp); | |
382bee57 | 139 | env_set("ipaddr", tmp); |
6e592385 | 140 | } |
a3e1a727 JH |
141 | #if !defined(CONFIG_BOOTP_SERVERIP) |
142 | /* | |
ff78ad28 | 143 | * Only attempt to change serverip if net/bootp.c:store_net_params() |
a3e1a727 JH |
144 | * could have set it |
145 | */ | |
049a95a7 JH |
146 | if (net_server_ip.s_addr) { |
147 | ip_to_string(net_server_ip, tmp); | |
382bee57 | 148 | env_set("serverip", tmp); |
6e592385 | 149 | } |
a3e1a727 | 150 | #endif |
049a95a7 JH |
151 | if (net_dns_server.s_addr) { |
152 | ip_to_string(net_dns_server, tmp); | |
382bee57 | 153 | env_set("dnsip", tmp); |
6e592385 | 154 | } |
1fe80d79 | 155 | #if defined(CONFIG_BOOTP_DNS2) |
049a95a7 JH |
156 | if (net_dns_server2.s_addr) { |
157 | ip_to_string(net_dns_server2, tmp); | |
382bee57 | 158 | env_set("dnsip2", tmp); |
6e592385 | 159 | } |
fe389a82 | 160 | #endif |
586cbe51 | 161 | if (net_nis_domain[0]) |
382bee57 | 162 | env_set("domain", net_nis_domain); |
ea287deb | 163 | |
4fd5055f | 164 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET) |
bc0571fc JH |
165 | if (net_ntp_time_offset) { |
166 | sprintf(tmp, "%d", net_ntp_time_offset); | |
382bee57 | 167 | env_set("timeoffset", tmp); |
ea287deb WD |
168 | } |
169 | #endif | |
4fd5055f | 170 | #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER) |
049a95a7 JH |
171 | if (net_ntp_server.s_addr) { |
172 | ip_to_string(net_ntp_server, tmp); | |
382bee57 | 173 | env_set("ntpserverip", tmp); |
ea287deb WD |
174 | } |
175 | #endif | |
3863585b | 176 | } |
6e592385 | 177 | |
e4bf0c5c SG |
178 | static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc, |
179 | char * const argv[]) | |
3863585b WD |
180 | { |
181 | char *s; | |
2e4970d8 | 182 | char *end; |
3863585b WD |
183 | int rcode = 0; |
184 | int size; | |
2e4970d8 | 185 | ulong addr; |
3863585b | 186 | |
449312c1 AG |
187 | net_boot_file_name_explicit = false; |
188 | ||
3863585b | 189 | /* pre-set load_addr */ |
00caae6d | 190 | s = env_get("loadaddr"); |
4fd5055f | 191 | if (s != NULL) |
3863585b | 192 | load_addr = simple_strtoul(s, NULL, 16); |
3863585b WD |
193 | |
194 | switch (argc) { | |
195 | case 1: | |
f43308fa JH |
196 | /* refresh bootfile name from env */ |
197 | copy_filename(net_boot_file_name, env_get("bootfile"), | |
198 | sizeof(net_boot_file_name)); | |
3863585b WD |
199 | break; |
200 | ||
2e4970d8 PT |
201 | case 2: /* |
202 | * Only one arg - accept two forms: | |
203 | * Just load address, or just boot file name. The latter | |
204 | * form must be written in a format which can not be | |
205 | * mis-interpreted as a valid number. | |
3863585b | 206 | */ |
2e4970d8 | 207 | addr = simple_strtoul(argv[1], &end, 16); |
449312c1 | 208 | if (end == (argv[1] + strlen(argv[1]))) { |
2e4970d8 | 209 | load_addr = addr; |
f43308fa JH |
210 | /* refresh bootfile name from env */ |
211 | copy_filename(net_boot_file_name, env_get("bootfile"), | |
212 | sizeof(net_boot_file_name)); | |
449312c1 AG |
213 | } else { |
214 | net_boot_file_name_explicit = true; | |
1411157d JH |
215 | copy_filename(net_boot_file_name, argv[1], |
216 | sizeof(net_boot_file_name)); | |
449312c1 | 217 | } |
3863585b WD |
218 | break; |
219 | ||
4fd5055f JH |
220 | case 3: |
221 | load_addr = simple_strtoul(argv[1], NULL, 16); | |
449312c1 | 222 | net_boot_file_name_explicit = true; |
1411157d JH |
223 | copy_filename(net_boot_file_name, argv[2], |
224 | sizeof(net_boot_file_name)); | |
3863585b WD |
225 | |
226 | break; | |
227 | ||
2d46cf29 SG |
228 | #ifdef CONFIG_CMD_TFTPPUT |
229 | case 4: | |
38bd80b4 | 230 | if (strict_strtoul(argv[1], 16, &save_addr) < 0 || |
4fd5055f | 231 | strict_strtoul(argv[2], 16, &save_size) < 0) { |
38bd80b4 | 232 | printf("Invalid address/size\n"); |
85848f03 | 233 | return CMD_RET_USAGE; |
38bd80b4 | 234 | } |
449312c1 | 235 | net_boot_file_name_explicit = true; |
1411157d JH |
236 | copy_filename(net_boot_file_name, argv[3], |
237 | sizeof(net_boot_file_name)); | |
2d46cf29 SG |
238 | break; |
239 | #endif | |
47e26b1b | 240 | default: |
770605e4 | 241 | bootstage_error(BOOTSTAGE_ID_NET_START); |
4c12eeb8 | 242 | return CMD_RET_USAGE; |
3863585b | 243 | } |
770605e4 | 244 | bootstage_mark(BOOTSTAGE_ID_NET_START); |
3863585b | 245 | |
bc0571fc | 246 | size = net_loop(proto); |
4fd5055f | 247 | if (size < 0) { |
770605e4 | 248 | bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK); |
85848f03 | 249 | return CMD_RET_FAILURE; |
566a494f | 250 | } |
770605e4 | 251 | bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK); |
3863585b | 252 | |
bc0571fc | 253 | /* net_loop ok, update environment */ |
3863585b WD |
254 | netboot_update_env(); |
255 | ||
eb9401e3 | 256 | /* done if no file was loaded (no errors though) */ |
566a494f | 257 | if (size == 0) { |
770605e4 | 258 | bootstage_error(BOOTSTAGE_ID_NET_LOADED); |
85848f03 | 259 | return CMD_RET_SUCCESS; |
566a494f | 260 | } |
eb9401e3 | 261 | |
770605e4 | 262 | bootstage_mark(BOOTSTAGE_ID_NET_LOADED); |
c8e66db7 | 263 | |
67d668bf | 264 | rcode = bootm_maybe_autostart(cmdtp, argv[0]); |
3863585b | 265 | |
85848f03 | 266 | if (rcode == CMD_RET_SUCCESS) |
770605e4 | 267 | bootstage_mark(BOOTSTAGE_ID_NET_DONE); |
85848f03 JH |
268 | else |
269 | bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR); | |
3863585b WD |
270 | return rcode; |
271 | } | |
272 | ||
c76fe474 | 273 | #if defined(CONFIG_CMD_PING) |
088f1b19 | 274 | static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
73a8b27c WD |
275 | { |
276 | if (argc < 2) | |
85848f03 | 277 | return CMD_RET_USAGE; |
73a8b27c | 278 | |
049a95a7 JH |
279 | net_ping_ip = string_to_ip(argv[1]); |
280 | if (net_ping_ip.s_addr == 0) | |
4c12eeb8 | 281 | return CMD_RET_USAGE; |
73a8b27c | 282 | |
bc0571fc | 283 | if (net_loop(PING) < 0) { |
73a8b27c | 284 | printf("ping failed; host %s is not alive\n", argv[1]); |
85848f03 | 285 | return CMD_RET_FAILURE; |
73a8b27c WD |
286 | } |
287 | ||
288 | printf("host %s is alive\n", argv[1]); | |
289 | ||
85848f03 | 290 | return CMD_RET_SUCCESS; |
73a8b27c | 291 | } |
6dff5529 WD |
292 | |
293 | U_BOOT_CMD( | |
294 | ping, 2, 1, do_ping, | |
2fb2604d | 295 | "send ICMP ECHO_REQUEST to network host", |
a89c33db | 296 | "pingAddress" |
6dff5529 | 297 | ); |
90253178 | 298 | #endif |
73a8b27c | 299 | |
c76fe474 | 300 | #if defined(CONFIG_CMD_CDP) |
a3d991bd WD |
301 | |
302 | static void cdp_update_env(void) | |
303 | { | |
304 | char tmp[16]; | |
305 | ||
6aede5b7 JH |
306 | if (cdp_appliance_vlan != htons(-1)) { |
307 | printf("CDP offered appliance VLAN %d\n", | |
308 | ntohs(cdp_appliance_vlan)); | |
4fd5055f | 309 | vlan_to_string(cdp_appliance_vlan, tmp); |
382bee57 | 310 | env_set("vlan", tmp); |
4fd5055f | 311 | net_our_vlan = cdp_appliance_vlan; |
a3d991bd WD |
312 | } |
313 | ||
6aede5b7 JH |
314 | if (cdp_native_vlan != htons(-1)) { |
315 | printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan)); | |
4fd5055f | 316 | vlan_to_string(cdp_native_vlan, tmp); |
382bee57 | 317 | env_set("nvlan", tmp); |
4fd5055f | 318 | net_native_vlan = cdp_native_vlan; |
a3d991bd | 319 | } |
a3d991bd WD |
320 | } |
321 | ||
088f1b19 | 322 | int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
a3d991bd WD |
323 | { |
324 | int r; | |
325 | ||
bc0571fc | 326 | r = net_loop(CDP); |
a3d991bd WD |
327 | if (r < 0) { |
328 | printf("cdp failed; perhaps not a CISCO switch?\n"); | |
85848f03 | 329 | return CMD_RET_FAILURE; |
a3d991bd WD |
330 | } |
331 | ||
332 | cdp_update_env(); | |
333 | ||
85848f03 | 334 | return CMD_RET_SUCCESS; |
a3d991bd WD |
335 | } |
336 | ||
337 | U_BOOT_CMD( | |
338 | cdp, 1, 1, do_cdp, | |
ec5c04cd | 339 | "Perform CDP network configuration", |
4b58266e | 340 | "\n" |
a3d991bd | 341 | ); |
90253178 | 342 | #endif |
a3d991bd | 343 | |
c76fe474 | 344 | #if defined(CONFIG_CMD_SNTP) |
088f1b19 | 345 | int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
ea287deb WD |
346 | { |
347 | char *toff; | |
348 | ||
349 | if (argc < 2) { | |
723806cc | 350 | net_ntp_server = env_get_ip("ntpserverip"); |
049a95a7 | 351 | if (net_ntp_server.s_addr == 0) { |
088f1b19 | 352 | printf("ntpserverip not set\n"); |
85848f03 | 353 | return CMD_RET_FAILURE; |
ea287deb WD |
354 | } |
355 | } else { | |
049a95a7 JH |
356 | net_ntp_server = string_to_ip(argv[1]); |
357 | if (net_ntp_server.s_addr == 0) { | |
088f1b19 | 358 | printf("Bad NTP server IP address\n"); |
85848f03 | 359 | return CMD_RET_FAILURE; |
ea287deb WD |
360 | } |
361 | } | |
362 | ||
00caae6d | 363 | toff = env_get("timeoffset"); |
088f1b19 | 364 | if (toff == NULL) |
bc0571fc | 365 | net_ntp_time_offset = 0; |
088f1b19 | 366 | else |
bc0571fc | 367 | net_ntp_time_offset = simple_strtol(toff, NULL, 10); |
ea287deb | 368 | |
bc0571fc | 369 | if (net_loop(SNTP) < 0) { |
d6840e3d | 370 | printf("SNTP failed: host %pI4 not responding\n", |
4fd5055f | 371 | &net_ntp_server); |
85848f03 | 372 | return CMD_RET_FAILURE; |
ea287deb WD |
373 | } |
374 | ||
85848f03 | 375 | return CMD_RET_SUCCESS; |
ea287deb WD |
376 | } |
377 | ||
378 | U_BOOT_CMD( | |
379 | sntp, 2, 1, do_sntp, | |
2fb2604d | 380 | "synchronize RTC via network", |
ea287deb WD |
381 | "[NTP server IP]\n" |
382 | ); | |
90253178 | 383 | #endif |
1a32bf41 RG |
384 | |
385 | #if defined(CONFIG_CMD_DNS) | |
54841ab5 | 386 | int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
1a32bf41 | 387 | { |
47e26b1b | 388 | if (argc == 1) |
4c12eeb8 | 389 | return CMD_RET_USAGE; |
1a32bf41 RG |
390 | |
391 | /* | |
392 | * We should check for a valid hostname: | |
393 | * - Each label must be between 1 and 63 characters long | |
394 | * - the entire hostname has a maximum of 255 characters | |
395 | * - only the ASCII letters 'a' through 'z' (case-insensitive), | |
396 | * the digits '0' through '9', and the hyphen | |
397 | * - cannot begin or end with a hyphen | |
398 | * - no other symbols, punctuation characters, or blank spaces are | |
399 | * permitted | |
400 | * but hey - this is a minimalist implmentation, so only check length | |
401 | * and let the name server deal with things. | |
402 | */ | |
403 | if (strlen(argv[1]) >= 255) { | |
404 | printf("dns error: hostname too long\n"); | |
85848f03 | 405 | return CMD_RET_FAILURE; |
1a32bf41 RG |
406 | } |
407 | ||
786eac5f | 408 | net_dns_resolve = argv[1]; |
1a32bf41 RG |
409 | |
410 | if (argc == 3) | |
786eac5f | 411 | net_dns_env_var = argv[2]; |
1a32bf41 | 412 | else |
786eac5f | 413 | net_dns_env_var = NULL; |
1a32bf41 | 414 | |
bc0571fc | 415 | if (net_loop(DNS) < 0) { |
1a32bf41 | 416 | printf("dns lookup of %s failed, check setup\n", argv[1]); |
85848f03 | 417 | return CMD_RET_FAILURE; |
1a32bf41 RG |
418 | } |
419 | ||
85848f03 | 420 | return CMD_RET_SUCCESS; |
1a32bf41 RG |
421 | } |
422 | ||
423 | U_BOOT_CMD( | |
424 | dns, 3, 1, do_dns, | |
425 | "lookup the IP of a hostname", | |
426 | "hostname [envvar]" | |
427 | ); | |
428 | ||
429 | #endif /* CONFIG_CMD_DNS */ | |
d22c338e JH |
430 | |
431 | #if defined(CONFIG_CMD_LINK_LOCAL) | |
432 | static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc, | |
433 | char * const argv[]) | |
434 | { | |
435 | char tmp[22]; | |
436 | ||
bc0571fc | 437 | if (net_loop(LINKLOCAL) < 0) |
85848f03 | 438 | return CMD_RET_FAILURE; |
d22c338e | 439 | |
049a95a7 JH |
440 | net_gateway.s_addr = 0; |
441 | ip_to_string(net_gateway, tmp); | |
382bee57 | 442 | env_set("gatewayip", tmp); |
d22c338e | 443 | |
049a95a7 | 444 | ip_to_string(net_netmask, tmp); |
382bee57 | 445 | env_set("netmask", tmp); |
d22c338e | 446 | |
049a95a7 | 447 | ip_to_string(net_ip, tmp); |
382bee57 SG |
448 | env_set("ipaddr", tmp); |
449 | env_set("llipaddr", tmp); /* store this for next time */ | |
d22c338e | 450 | |
85848f03 | 451 | return CMD_RET_SUCCESS; |
d22c338e JH |
452 | } |
453 | ||
454 | U_BOOT_CMD( | |
455 | linklocal, 1, 1, do_link_local, | |
456 | "acquire a network IP address using the link-local protocol", | |
457 | "" | |
458 | ); | |
459 | ||
460 | #endif /* CONFIG_CMD_LINK_LOCAL */ |