net.c 10 KB

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