net.c 10 KB

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