net.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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:store_net_params()
  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. bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
  211. rcode = bootm_maybe_autostart(cmdtp, argv[0]);
  212. if (rcode == CMD_RET_SUCCESS)
  213. bootstage_mark(BOOTSTAGE_ID_NET_DONE);
  214. else
  215. bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
  216. return rcode;
  217. }
  218. #if defined(CONFIG_CMD_PING)
  219. static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  220. {
  221. if (argc < 2)
  222. return CMD_RET_USAGE;
  223. net_ping_ip = string_to_ip(argv[1]);
  224. if (net_ping_ip.s_addr == 0)
  225. return CMD_RET_USAGE;
  226. if (net_loop(PING) < 0) {
  227. printf("ping failed; host %s is not alive\n", argv[1]);
  228. return CMD_RET_FAILURE;
  229. }
  230. printf("host %s is alive\n", argv[1]);
  231. return CMD_RET_SUCCESS;
  232. }
  233. U_BOOT_CMD(
  234. ping, 2, 1, do_ping,
  235. "send ICMP ECHO_REQUEST to network host",
  236. "pingAddress"
  237. );
  238. #endif
  239. #if defined(CONFIG_CMD_CDP)
  240. static void cdp_update_env(void)
  241. {
  242. char tmp[16];
  243. if (cdp_appliance_vlan != htons(-1)) {
  244. printf("CDP offered appliance VLAN %d\n",
  245. ntohs(cdp_appliance_vlan));
  246. vlan_to_string(cdp_appliance_vlan, tmp);
  247. setenv("vlan", tmp);
  248. net_our_vlan = cdp_appliance_vlan;
  249. }
  250. if (cdp_native_vlan != htons(-1)) {
  251. printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
  252. vlan_to_string(cdp_native_vlan, tmp);
  253. setenv("nvlan", tmp);
  254. net_native_vlan = cdp_native_vlan;
  255. }
  256. }
  257. int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  258. {
  259. int r;
  260. r = net_loop(CDP);
  261. if (r < 0) {
  262. printf("cdp failed; perhaps not a CISCO switch?\n");
  263. return CMD_RET_FAILURE;
  264. }
  265. cdp_update_env();
  266. return CMD_RET_SUCCESS;
  267. }
  268. U_BOOT_CMD(
  269. cdp, 1, 1, do_cdp,
  270. "Perform CDP network configuration",
  271. "\n"
  272. );
  273. #endif
  274. #if defined(CONFIG_CMD_SNTP)
  275. int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  276. {
  277. char *toff;
  278. if (argc < 2) {
  279. net_ntp_server = getenv_ip("ntpserverip");
  280. if (net_ntp_server.s_addr == 0) {
  281. printf("ntpserverip not set\n");
  282. return CMD_RET_FAILURE;
  283. }
  284. } else {
  285. net_ntp_server = string_to_ip(argv[1]);
  286. if (net_ntp_server.s_addr == 0) {
  287. printf("Bad NTP server IP address\n");
  288. return CMD_RET_FAILURE;
  289. }
  290. }
  291. toff = getenv("timeoffset");
  292. if (toff == NULL)
  293. net_ntp_time_offset = 0;
  294. else
  295. net_ntp_time_offset = simple_strtol(toff, NULL, 10);
  296. if (net_loop(SNTP) < 0) {
  297. printf("SNTP failed: host %pI4 not responding\n",
  298. &net_ntp_server);
  299. return CMD_RET_FAILURE;
  300. }
  301. return CMD_RET_SUCCESS;
  302. }
  303. U_BOOT_CMD(
  304. sntp, 2, 1, do_sntp,
  305. "synchronize RTC via network",
  306. "[NTP server IP]\n"
  307. );
  308. #endif
  309. #if defined(CONFIG_CMD_DNS)
  310. int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  311. {
  312. if (argc == 1)
  313. return CMD_RET_USAGE;
  314. /*
  315. * We should check for a valid hostname:
  316. * - Each label must be between 1 and 63 characters long
  317. * - the entire hostname has a maximum of 255 characters
  318. * - only the ASCII letters 'a' through 'z' (case-insensitive),
  319. * the digits '0' through '9', and the hyphen
  320. * - cannot begin or end with a hyphen
  321. * - no other symbols, punctuation characters, or blank spaces are
  322. * permitted
  323. * but hey - this is a minimalist implmentation, so only check length
  324. * and let the name server deal with things.
  325. */
  326. if (strlen(argv[1]) >= 255) {
  327. printf("dns error: hostname too long\n");
  328. return CMD_RET_FAILURE;
  329. }
  330. net_dns_resolve = argv[1];
  331. if (argc == 3)
  332. net_dns_env_var = argv[2];
  333. else
  334. net_dns_env_var = NULL;
  335. if (net_loop(DNS) < 0) {
  336. printf("dns lookup of %s failed, check setup\n", argv[1]);
  337. return CMD_RET_FAILURE;
  338. }
  339. return CMD_RET_SUCCESS;
  340. }
  341. U_BOOT_CMD(
  342. dns, 3, 1, do_dns,
  343. "lookup the IP of a hostname",
  344. "hostname [envvar]"
  345. );
  346. #endif /* CONFIG_CMD_DNS */
  347. #if defined(CONFIG_CMD_LINK_LOCAL)
  348. static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
  349. char * const argv[])
  350. {
  351. char tmp[22];
  352. if (net_loop(LINKLOCAL) < 0)
  353. return CMD_RET_FAILURE;
  354. net_gateway.s_addr = 0;
  355. ip_to_string(net_gateway, tmp);
  356. setenv("gatewayip", tmp);
  357. ip_to_string(net_netmask, tmp);
  358. setenv("netmask", tmp);
  359. ip_to_string(net_ip, tmp);
  360. setenv("ipaddr", tmp);
  361. setenv("llipaddr", tmp); /* store this for next time */
  362. return CMD_RET_SUCCESS;
  363. }
  364. U_BOOT_CMD(
  365. linklocal, 1, 1, do_link_local,
  366. "acquire a network IP address using the link-local protocol",
  367. ""
  368. );
  369. #endif /* CONFIG_CMD_LINK_LOCAL */