net.c 9.9 KB

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