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