cmd_net.c 9.7 KB

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